Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to enable link-time optimization while only disabling strict aliasing for some functions?

My program conforms to the strict aliasing rule, except for one place: a compilation unit which contains hashing functions such as MurmurHash3, SpookyHash, etc. On x86 and x86_64, these hashing functions accept a const char *, cast them to uint32, and process data in blocks of 4 bytes. This makes them much faster compared to processing data byte-by-byte, but I believe this breaks the strict aliasing rule. Right now, I compile this compilation unit with -fno-strict-aliasing, while I compile the rest of the program with -fstrict-aliasing.

But I'm wondering what happens if I enable link-time optimization. As far as I know, GCC and Clang implement link-time optimization sort-of by storing the program source into .o files, so that at the linking phase the compiler knows the source code of the entire program. Is it then still possible to disable strict aliasing for the hashing functions only? Or do I now have to disable strict aliasing for the entire program? Or did I misunderstand things completely, and MurmurHash3/SpookyHash are in fact strict aliasing compliant?

like image 428
Hongli Avatar asked Sep 10 '14 11:09

Hongli


1 Answers

Right now, I compile this compilation unit with -fno-strict-aliasing, while I compile the rest of the program with -fstrict-aliasing.

You can do the same with link time optimizations. Just do not compile the specific object code with link time optimizations.

Example with clang (same with gcc):

 clang -flto -O3 -c a.c
 clang -O3 -fno-strict-aliasing b.c     # no -flto and with -fno-strict-aliasing
 clang -flto -O3 -c main.c
 clang a.o b.o main.o -o main
like image 82
ouah Avatar answered Sep 27 '22 19:09

ouah