3 ms·
I'll just say it: that's a clang bug. The assumption being made by the optimizer is wrong, for exactly the reason that overflow exists. I get very tired of pe
by ajross 8y ago
I'll just say it: that's a clang bug. The assumption being made by the optimizer is wrong, for exactly the reason that overflow exists.
I get very tired of people trying to make this "really" about "undefined behavior" sanity when real compilers are real software with real behavior. And here there is a clearly desired behavior (whether or not it got written into a standard somehwere), and clang implemented something else. That's wrong.
- keybuk 8y agoThis particular undefined case is a really interesting one; it seems utterly non-obvious why it exists, until you remember one thing... The most popular architecture today is 64-bit, with ABI specifying that the integer type is 32-bits wide. So when faced with performing an operation on two 32-bit signed integers, on a 64-bit platform, you have to either: 1) perform the operation in 32-bit registers - if available - and even if available, usually far far far fewer in number thus massive performance & resource penalties to code 2) perform the operation in 64-bit registers, then add code to check the result, and if it would overflow in 32-bit, compute what the overflowed result would have looked like in a 32-bit register, and return that instead - again quite a performance hit 3) perform the operation in 64-bit registers, and just return the lower 32-bits, whatever that might be The standard declined to pick a choice, which is why we have UB here. (For unsigned integers, you can just truncate the result and it works every time, which is why the standard defines the result)
- vyodaiken 8y agoWait a second: both ARM and IA-64 have 32 bit operations.
- nayuki 8y ago> 1) ... perform the operation in 32-bit registers - if available - and even if available, usually far far far fewer in number False for x86-64. There are sixteen 32-bit general-purpose registers: eax, ebx, ..., r8d, ..., r16d. There are sixteen 64-bit registers too: rax, rbx, ..., r8, ..., r16.
- pcwalton 8y agoOn the contrary: if clang assumed that signed integers could overflow by default, then that would be a bug. Why? Because it would result in missed optimizations, and optimizations are the reason why we use an optimizing compiler. Here's an example of what can happen if the compiler doesn't perform these optimizations: https://gist.github.com/rygorous/e0f055bfb74e3d5f0af20690759de5a7 https://gist.github.com/rygorous/e0f055bfb74e3d5f0af20690759... The loop codegen when the optimization is not performed is pretty atrocious. That is the bug, and solving that bug is the reason why compilers assume signed overflow cannot happen. If you want to blame something, blame the C language. Compiler developers are just doing their jobs.
- nkurz 8y agoHere's an example of what can happen if the compiler doesn't perform these optimizations: That's a great article, although I don't think it shows the advantage of assuming that undefined behavior will never occur. Rather than assuming "no undefined behavior", wouldn't it be legal (and preferable) for a compiler to just let the overflow (or lack of overflow) occur, and make the same optimization? For me the entire point of contention is the difference between "undefined" and "implementation defined". Maybe I'm wrong about the details here, but I think this is a case where a compiler could be just as fast without the nasal daemons.
- BeeOnRope 8y agoIt is doing exactly what you say - the code as generated simply lets the "overflow" occur, which is what makes it fast. In particular, if the index variable overflows the size of `int` it will just keep increasing. Effectively, the compiler has replaced int with long under the covers, since the cases where the behavior between int and long differ are exactly the cases where the int has overflowed. So it's not always the case that the compiler assumes "overflow won't happen" and then _removes code_ - it just compiles code in a way that is ideal for the non overflow case and then what happens in the overflow case just falls out of the generated assembly (usually it does something bad like OOB access, but the same would generally be true of a wrapping overflow also). You can't make this "implementation defined though", because the behavior is from a C-source point of view, it is effectively: "In the case an int overflows, we silently add 32 more bits and keep incrementing". Obviously you can't support that behavior in general, and even in specific cases it leads to nonsense like ints have non-representable values, etc.