The complaint isn't about alignment at all, it's that the optimizer assumes that two pointers to the same basic type cannot overlap in memory. The generated code is correct except for the fact that the two arguments are distinct pointers to the same three bytes in memory. I believe this behavior is actually specified in the standard, actually, in the same section that defines the aliasing rules.
Isn’t that in practice the same as an alignment problem? Can you have two ints overlap without a misaligned access?
GCC always assumes aligned pointer accesses
11–20 of 93 posts
Re: GCC always assumes aligned pointer accesses
#12Earlier quoted context omitted.
Isn’t that in practice the same as an alignment problem? Can you have two ints overlap without a misaligned access?
No, but you can have two otherwise-perfectly-legal aligned accesses that overlap (consider two long longs on a 32 bit platform), and that's disallowed by the standard.
Re: GCC always assumes aligned pointer accesses
#13No. [as in: this isn't about GCC.] The C standard requires that pointers generally be created from referencing a valid, existing object. A misaligned integer is not a valid "object", thus the compiler may assume that all pointers to ints are aligned (to 4 bytes.) https://en.cppreference.com/w/cpp/language/pointer Every value of pointer type is one of the following: - a pointer to an object or function (in which case…
Re: GCC always assumes aligned pointer accesses
#14No. [as in: this isn't about GCC.] The C standard requires that pointers generally be created from referencing a valid, existing object. A misaligned integer is not a valid "object", thus the compiler may assume that all pointers to ints are aligned (to 4 bytes.) https://en.cppreference.com/w/cpp/language/pointer Every value of pointer type is one of the following: - a pointer to an object or function (in which case…
Why "no", you're agreeing with the conclusion
But, yes. Edited to clarify.
FWIW this isn't a new problem either. If you wrote some C code in the 90ies using misaligned pointers, and then tried porting it to, say, a DEC Alpha, you'd get a SIGBUS in your face on first dereferencing such a pointer.
Re: GCC always assumes aligned pointer accesses
#15Earlier quoted context omitted.
No, but you can have two otherwise-perfectly-legal aligned accesses that overlap (consider two long longs on a 32 bit platform), and that's disallowed by the standard.
The compiler would know that the alignment is smaller than the type itself, which would disable the optimization mentioned in the post.
It's true that fancy tricks like LTO can make that kind of behavior visible to the optimizer, but the standard predates that kind of magic by several decades.
Re: GCC always assumes aligned pointer accesses
#16Is there an example where things break on misaligned access to non-overlapping objects?
Re: GCC always assumes aligned pointer accesses
#17Earlier quoted context omitted.
-fno-strict-aliasing: https://stackoverflow.com/questions/98650/what-is-the-strict... Torvalds rant: https://www.yodaiken.com/2018/06/07/torvalds-on-aliasing edit: I misread the parent as wondering about a compiler flag
Irrelevant to the post, even without strict aliasing the compiler will assume pointers/objects are properly aligned.
Many programs rely on misaligned access, and features like pragma packed do too. I doubt they’ll be able to break all of that legacy code.
Re: GCC always assumes aligned pointer accesses
#18Re: GCC always assumes aligned pointer accesses
#19The example of bad gcc behavior was essentially type punning, which is well-known to cause trouble. Is there an example where things break on misaligned access to non-overlapping objects?
That's also why this rule is in the C standard to begin with. Misaligned accesses need different / multiple instructions on some of these architectures, making them significantly more expensive. So the decision was made in favor of assuming things are aligned.
Re: GCC always assumes aligned pointer accesses
#20The complaint isn't about alignment at all, it's that the optimizer assumes that two pointers to the same basic type cannot overlap in memory. The generated code is correct except for the fact that the two arguments are distinct pointers to the same three bytes in memory. I believe this behavior is actually specified in the standard, actually, in the same section that defines the aliasing rules.
Yes, this is the compiler optimizing UB due to aliasing , not due to alignment.
C was portable assembly back in the early 1980s. The result was that it was crushed in performance by Fortran for scientific codes, and most of the difference had to do with assumptions the compiler could make about aliasing (or the lack of it). This was fixed by the first ANSI C standard (finalized in 1989).
Yet I still see people who weren't even born then, or were young children then, talk as if C is supposed to be "portable assembly". It isn't.