4 ms·
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 co
by ajross 7y ago
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.
- loeg 7y agoYes, this is the compiler optimizing UB due to aliasing, not due to alignment.
- not2b 7y agoYes, the optimization assumes that given two pointers to int, either they point to the same int, or they point to distinct, non-overlapping ints. Under that restriction the result is guaranteed to be 1. It's allowed to do so by the standard because those are the rules. 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.
- naniwaduni 7y ago> 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 Calling it "fixed", as if it were a problem of the language rather than programmers' expectations, is a rather slanted view of the history.
- pascal_cuoq 7y agoCould you clarify which clause of the C standard you are referring to when you say “due to aliasing, not due to alignment”? I make sense of the C standard for a living (this is literally my day job) and I do not see what clause of the C standard you are referring to. It would be very useful to me to know which clause you are referring to, and I would be eternally thankful.
- loeg 7y agohttps://news.ycombinator.com/item?id=22911286 https://news.ycombinator.com/item?id=22911286
- pascal_cuoq 7y agoSo the clause about assignment? Thanks, that was helpful.
- Spivak 7y agoIsn’t that in practice the same as an alignment problem? Can you have two ints overlap without a misaligned access?
- deleted 7y ago[deleted]
- ajross 7y agoNo, 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.
- eqvinox 7y agoThe compiler would know that the alignment is smaller than the type itself, which would disable the optimization mentioned in the post.
- ajross 7y agoIt literally cannot. The function in the linked article, that takes two pointers, must be compiled in isolation. If it needs to depend on alignment or overlapping, it needs to emit code to check it. And the standard does not require it to, nor do typical users want that. 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.
- eqvinox 7y agoThat's not what I meant. On an architecture where "long long" is 8 bytes, but only has a 4 byte alignment requirement, the compiler knows this, and knows that 2 pointers of type "long long * " may overlap. Therefore it would disable the optimization. (Except the pointers still aren't supposed to overlap, but...)
- bonzini 7y ago
- __s 7y ago-fno-strict-aliasing: https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule https://stackoverflow.com/questions/98650/what-is-the-strict... Torvalds rant: https://www.yodaiken.com/2018/06/07/torvalds-on-aliasing https://www.yodaiken.com/2018/06/07/torvalds-on-aliasing edit: I misread the parent as wondering about a compiler flag
- eqvinox 7y agoIrrelevant to the post, even without strict aliasing the compiler will assume pointers/objects are properly aligned.
- hedora 7y agoDo you have an example where the compiler generates bad code that doesn’t involve misaligned aliases? 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.
- eqvinox 7y ago> 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. It's already broken. You're just not seeing it on x86. (Not sure how tolerant ARM is.) I work in network software engineering, i.e. PowerPC used to be a common architecture. Those never accepted misaligned pointers (albeit you could of course trap the misalignment exception and emulate the access, or print a backtrace to identify the location.) Really this is a problem caused by the "common case" (x86) being permissive in what it accepts, creating fallout further down the line if you ever need the code to run on something less permissive. [Edit: actually, oops, it's not PowerPC; those are relatively permissive too. Pretty sure DEC Alpha and m68k are restrictive.]
- loeg 7y agoI think newer aarch64 allows misaligned access to regular memory.
- eqvinox 7y ago> I believe this behavior is actually specified in the standard, actually, in the same section that defines the aliasing rules. It is, indirectly, since pointers are required to point to valid objects, and valid objects are required to be correctly aligned.
- pascal_cuoq 7y agoI have heard this reaction to this article a lot, but sorry, there is nothing in the C standard that says that objects should not overlap, except a rule that only apply to “lvalue = lvalue;” assignments and is not relevant here. Plus on a some 32-bit ISA, a long long and a double only need to be aligned to 32-bit boundaries, so I note that in the made-up C rules that you are referring to, “basic type” is not very well defined. > I believe this behavior is actually specified in the standard, actually, > in the same section that defines the aliasing rules. The strict aliasing rules are here: https://port70.net/~nsz/c/c11/n1570.html#6.5p7 https://port70.net/~nsz/c/c11/n1570.html#6.5p7 Go ahead and point to the rule that says that “basic types” cannot overlap with themselves.
- ajross 7y agoI genuinely can't follow you completely, but half suspect you're violently agreeing with me. Are you saying the optimization in the linked article is, or is not, in violation of the standard? Edit: this is the text I was remembering, from 6.5.16.1 ("Simple Assignment"): "If the value being stored in an object is read from another object that overlaps in any way the storage of the first object, then the overlap shall be exact and the two objects shall have qualified or unqualified versions of a compatible type; otherwise, the behavior is undefined.". That pretty much matches exactly what I was saying: compilers are free to assume that basic types don't overlap, because if they do then any generated code will be undefined behavior anyway.
- pascal_cuoq 7y ago6.5.16.1 is the “rule that only apply to “lvalue = lvalue;” assignments and is not relevant here” It does not apply to “lvalue = 1;” or to “lvalue = 2;”, which are the two relevant assignments in the example in the article. For context, I think I made it clear in the article that the program being discussed is UB, and therefore that the compiler is not to blame. But since I wrote this article, I have had people telling me “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”. My reply to this specific sentence is: No. You are wrong. There are no words in the standard that say that “basic types cannot overlap in memory”. There is not even a notion of “basic type”. There are clauses about pointer alignment, that are explicitly cited in the article, and there are clauses about strict aliasing, that are shown in the article not to be the reason for GCC optimizing the program by using -fno-strict-aliasing. There are no rules about “basic types not overlapping” in the C standard. You only think there are. Or please cite them. (6.5.16.1 is a rule about assignment, it only applies for the code pattern lvalue1 = lvalue2;)