Is it possible to enable connection time optimization only by disabling a strict alias for some functions?

My program complies with the strict rule of aliases, with one exception: a compilation unit that contains hash functions such as MurmurHash3, SpookyHash, etc. On x86 and x86_64, these hash functions accept const char * , discard them on uint32 and process the data in blocks of 4 bytes. This makes them much faster than byte-by-bit data processing, but I believe this violates the strict anti-aliasing rule. Right now, I'm compiling this compilation block with -fno-strict-aliasing, while I'm compiling the rest of the program with -fstrict-aliasing.

But I wonder what will happen if I turn on connection time optimization. As far as I know, GCC and Clang implement sorting by binding time, saving the source code of the program in .o files, so that at the stage of compilation, the compiler knows the source code of the whole program. Is it still possible to turn off line anti-aliasing only for hashing functions? Or do I now need to disable a strict alias for the entire program? Or didnโ€™t I completely understand things, and MurmurHash3 / SpookyHash are actually strict aliasing-compatible?

+8
c ++ c strict-aliasing
source share
3 answers

There are three things to consider:

  • performance
  • portability
  • standard compliance

You will get maximum performance if you do not copy the data and can guarantee consistent access.

Unrelated access and anti-aliasing are portability issues. If you decide to copy the data, this will take care of everyone. Otherwise, you need to configure the algorithm for processing incorrectly aligned input data and ensure that there is no competing access through incompatible type pointers:

If you access only data with one type of pointer, violating the rules of proper typing will make your program inappropriate, but probably will not be a problem in practice, even if you do not pass -fno-strict-aliasing - this is where with unit tests itโ€™s very conveniently.

For SpookyHash, I actually have my own version of C99 (which also commits out of turn in the V2 reference implementation). If you are having trouble with efficient typing and your architecture supports uneven access (or all input is aligned), you can pass the -DSPOOKY_NOCOPY flag -DSPOOKY_NOCOPY compiler. On my x86-64 machine, the performance gain was about 10-20% depending on the size of the input.

+6
source share

Right now, I'm compiling this compilation block with -fno-strict-aliasing, while I'm compiling the rest of the program with -fstrict-aliasing.

You can do the same with link time optimization. Just donโ€™t compile specific object code with link time optimization.

Example with clang (same with gcc ):

  clang -flto -O3 -c ac clang -O3 -fno-strict-aliasing bc # no -flto and with -fno-strict-aliasing clang -flto -O3 -c main.c clang ao bo main.o -o main 
+6
source share

This should be possible (at least you could try) since recent GCC provides a parameter specific function . You can try adding something like

  #pragma GCC optimize ("-fstrict-aliasing") 

in front of your overlay functions and put

  #pragma GCC reset_options 

after them.

Maybe you need to wrap them

 #if __GNUC__ >= 4 

and of course some #endif

Alternatively, use constructor tricks (e.g. autoconf , cmake or the complex GNU make 4.0 rule, etc.) to define your own HAVE_GENUINE_GCC as 1 for genuine GCC only , and your own HAVE_GENUINE_CLANG as 1 for genuine Clang / LLVM compiler etc. Or, perhaps find that the pragmas above are understood in some sample code, and then define HAVE_WORKING_PRAGMA_GCC_OPTIMIZE as 1.

BTW, at least according to GCC, -flto does not store the program source representation in object files, but only a generalized form of some GCC internal representations (such as Gimple, etc.) obtained when compiling your source code. This is completely different!

PS. I have not tried, so perhaps this is not so simple.

+3
source share

All Articles