In C, what exactly are the performance benefits that come with observing strict aliasing?
1 Answers
There is a page that describes aliasing very thoroughly here.
There are also some SO topics here and here.
To summarize, the compiler cannot assume the value of data when two pointers of different types are accessing the same location (i.e. it must read the value every time and therefore cannot make optimizations).
This only occurs when strict aliasing is not being enforced. Strict aliasing options:
- gcc: -fstrict-aliasing [default] and -fno-strict-aliasing
- msvc: Strict aliasing is off by default. (If somebody knows how to turn it on, please say so.)
Example
Copy-paste this code into main.c:
void f(unsigned u)
{
unsigned short* const bad = (unsigned short*)&u;
}
int main(void)
{
f(5);
return 0;
}
Then compile the code with these options:
gcc main.c -Wall -O2
And you will get:
main.c:3: warning: dereferencing type-punned pointer will break strict-aliasing rules
Disable aliasing with:
gcc main.c -fno-strict-aliasing -Wall -O2
And the warning goes away. (Or just take out -Wall but...don't compile without it)
Try as I might I could not get MSVC to give me a warning.
-
+1, but please make it clear that the compiler cannot make that assumption when strict aliasing is *not* observed. – j_random_hacker Apr 16 '09 at 06:35
-
1Perfect! :) Unfortunate that MSVC++ doesn't seem to have an option for strict aliasing. It does have __restrict and __declspec(restrict) though, which can be used in individual cases. – j_random_hacker Apr 16 '09 at 12:30
-
Looks like a typo “-fno-strict-aliasing [default] and -fno-strict-aliasing” I'm guessing the default is actually -fstrict-aliasing (at least in modern GCC) rather than -fno-strict-aliasing, but in any case one of the two GCC options listed should be -fstrict-aliasing, and right now they both say -fno-strict-aliasing. – tialaramex Jul 17 '09 at 15:23
-
Ha, nice catch, I'm surprised nobody saw that until now. – GManNickG Jul 17 '09 at 17:54
-
default for my gcc is -fno-strict-aliasing (at least, I see a difference in assembly output when I set -fstrict-aliasing over default options) – James Jan 29 '12 at 00:29
-
The first article linked in the answer (Understanding strict aliasing on Cellperformance.com) can be found here: http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html – Koarl Mar 13 '12 at 16:59
-
MSVC has the [/Oa and /Ow options](http://msdn.microsoft.com/en-us/library/aa984741(v=vs.71).aspx) but they assume that there's no aliasing whatsoever. – nwellnhof Mar 24 '13 at 14:48
-
4How much research has been done on the extent to which applying type-based aliasing offers performance benefits that cannot be obtained just as well with `restrict`? Your first linked article suggests "If a program requires `-fno-strict-alias` one should figure that the programmer probably didn't bother to use `restrict`, but if `restrict` can offer all the performance advantages of type-based aliasing without the semantic limitations, why shouldn't one disable type-based aliasing, especially given that there's no way of knowing how gcc's interpretation of the rules may change in the future? – supercat Jun 23 '16 at 19:27
-
Which GCC version prints this? I'm having trouble reproducing the warning with GCC 6 and 7. – rustyx Jan 22 '18 at 10:13