4 ms·
Hm, I think "ignoring the situation completely with unpredictable results" pretty much covers everything. It's how optimizations generally work. Each optimizati
by ChrisSD 5y ago
Hm, I think "ignoring the situation completely with unpredictable results" pretty much covers everything. It's how optimizations generally work. Each optimization pass simply assumes there's no UB in the input and will optimize based on that assumption.
Optimizing compilers don't intentionally break programs with UB, they just ignore the possibility... with very unpredictable results.
- mpweiher 5y agoI don't think "optimising based on UB" is covered by "ignoring". And neither is "optimising assuming UB never happens", especially if said UB is explicitly written in the code, thus contradicting the assumption.
- ChrisSD 5y agoThe compiler doesn't know if the program contains UB or not. How could it? Sure it can have warnings for some well know UB patterns but it can't catch them all. There are dedicated tools designed to detect UB which are not only very slow but fail to be totally effective (they have both false positive and negatives). So the compiler simply ignores this possibility and optimizes assuming the input is a valid C program.
- mpweiher 5y agoThis isn't remotely true. Unless you use the sleight of hand that is commonly used in vastly expanding the meaning of the word "valid" to mean "the compiler can assume there is no UB". Which, again, isn't "ignoring the situation".
- SAI_Peregrinus 5y agoI disagree that it isn't "ignoring the situation". By analogy, if you saw a purple streetlight would you keep driving, stop, turn left, turn right, or make a U turn? You have to do something! You'd probably assume it's a prank, and treat it as a green light by proceeding normally. Compiler code gen around UB is no more complex than this, though the consequences can cause complex chains of reasoning in later optimization passes.
- mpweiher 5y agoHard disagree, your analogy is (still) incorrectly equating UB with "this should never happen". That's not what UB is. UB is "the standard doesn't say anything about this particular situation, because we intentionally left the standard incomplete". It never said "UB cannot happen". But even assuming your "purple streetlight" analogy, it exactly does not cover the "complex chains of reasoning" that the compilers then do. I saw a purple streetlight, therefore I can just assume it is green. Nope. I saw a purple streetlight, therefore I can now run over pedestrians. Nope. I saw a purple streetlight, therefore this now an airport runway. Nope. The compiler-writers' justifications don't pass even the most minimal test for coherence.