3 ms·
It goes further than that. The debates around UB a more about whether the compiler can assume that there are no buffer overflows and perform optimizations based
by layer8 6y ago
It goes further than that. The debates around UB a more about whether the compiler can assume that there are no buffer overflows and perform optimizations based on that. For example, if you have a local variable `char buffer[16]`, and there is an access `buffer[i]`, should the compiler be allowed to derive that 0 <= i < 16, and perform optimizations on other uses of `i` based on that "knowledge"? If all bets are off for `buffer[i]` when i < 0 or i >= 16, why shouldn't it? But some argue that the compiler shouldn't, exactly because such automated formal reasoning can lead to arbitrarily wide reaching effects. The problem is that it's hard to see where a middle ground could be.
- rectang 6y agoThe result is a perpetually unstable situation fostering inevitable errors when the programmer is surprised by the compiler. There's no remedy except to move away from C to more tightly specified languages.
- pjmlp 6y agoThe problem is ecosystems like UNIX based platforms, that will never move to something else. The only solution is to throw them away, e.g. Android style.
- throwaway2048 6y agoAndroid threw away a unix based platform? News to me.
- pjmlp 6y agoIt did, ask termux guys. The Linux kernel is an implementation detail. There is nothing UNIX related exposed to userspace. Java and Kotlin applications, while NDK APIs are clearly defined, https://developer.android.com/ndk/guides/stable_apis https://developer.android.com/ndk/guides/stable_apis Or Treble Project for that matter, https://source.android.com/devices/architecture/hidl https://source.android.com/devices/architecture/hidl Try to use private APIs (which is what the Linux underpinnings are) and the application might be blessed with being killed by the OS.
- sgtnoodle 6y agoIf the array index is unconditional, then sure that seems like a reasonable undefined behavior? If the access is conditional and the compiler can't infer whether the branch is taken, though, then it shouldn't make that assumption. This reminds me of a bug in the msp430 variant of gcc. The compiler would replace "x <= literal" with "x < literal + 1" because it generated more efficient code. If the literal was UINT_MAX, though, the compiler would trudge ahead and roll over the literal to 0. It would then subsequently reason that the comparison was unconditionally false, and then completely optimize away the nominal path of my code. That was a frustrating day of debugging!
- im3w1l 6y agoWhat I never see mentioned in this type of discussion is what this compiler trick buys us. Like how much faster does my program run because of deductions like these? And are there any patterns that particularly benefit from it? Could they hint the constraint manually?
- mpcjanssen 6y agoThere are plenty of examples in this thread alone. In my opinion hinting won't help. Everyone who cares about the performance gain will enable the hint (which I expect will be almost everyone) so you have won nothing but added noise with the hint.
- vyodaiken 6y agoThere is not a single compelling example. Just contrived examples with dubious speedups and obvious errors.