HNHacker News
TopNewBestAskShowJobs

vyodaiken

146 karma · joined January 26, 2016

submissionscomments
vyodaiken··on Undefined behavior in C is a reading error
Wang et al tried this an experiment and found no serious wins.
vyodaiken··on Undefined behavior in C is a reading error
I don't know how you can say that

z = x+y; if(z< x )overflowerror(); //important computation and

for(i=start, i >= start && i < n; i++)docomputation(x[i]); etc.

vyodaiken··on Undefined behavior in C is a reading error
So I create a thread with a custom stack which is an allocated buffer. At the top, I write a sequence of bytes in some order. Then I periodically read the top of the stack to see if the stack is getting close to overflow. Meanwhile, the thread code is also addressing the same store.
vyodaiken··on Undefined behavior in C is a reading error
What indicates the the current semantics won't change next week? After all, it is completely up to the compiler, no?
vyodaiken··on Undefined behavior in C is a reading error
Good question. I'd love to know. Is there any way to find out?
vyodaiken··on Undefined behavior in C is a reading error
That article is nonsensical. Seymour Cray and colleagues and Tomasula and colleagues invented ILP, branch prediction, etc. in the 1960s before C was thought of. The PDP11 is much more similar to modern x86s than to the weird architectures of the 1970s.
vyodaiken··on Undefined behavior in C is a reading error
The Chisnall article is a tutorial in incorrect Computer Architecture. PDP11s had caches by the 1970s and always had memory management. IPL was invented in the 1960s and has nothing to do with C. Branch predictors were invented in the 1960s too and one of the first machines C was ported to was the IBM370 which had super sophisticated IPL. Etc.
vyodaiken··on Undefined behavior in C is a reading error
And switch the i to a size_t and get vector code without the possibility of writing to random memory because your int overflows and GCC wants to pretend it cannot.

This is a poorly written loop. C design model is that if it is not critical, we don't care, and if it is, the programmer should fix it so optimization can work. https://gcc.godbolt.org/z/ErMP4cn6s

vyodaiken··on Undefined behavior in C is a reading error
It's not designed to overflow, and automobiles are not designed to crash, but airbags are good engineering anyways
vyodaiken··on Undefined behavior in C is a reading error
I think the author of that blog was correct: the preferred path is for the compiler to provide data to the programmer to simplify the loop.

For your godbolt example, use the C compiler not c++

vyodaiken··on Undefined behavior in C is a reading error
in which case the overflow will cause the loop to change some random memory, but its ok since removing a single instruction test that is easy to pipeline is worth incorrect results!
vyodaiken··on Undefined behavior in C is a reading error
link?
vyodaiken··on Undefined behavior in C is a reading error
Exactly - it was done for meaningless benchmarking reasons. C programmers would be happy to use "restrict" as an opt-in for those, but this argument about FORTRAN goes back to the initial days of the standard when Dennis Ritchie had to push "noalias" out of the proposed standard.
vyodaiken··on Undefined behavior in C is a reading error
The compiler should have valid rules, not invalid ones.
vyodaiken··on Undefined behavior in C is a reading error
There is not a single compelling example. Just contrived examples with dubious speedups and obvious errors.
vyodaiken··on Undefined behavior in C is a reading error
There is zero evidence that this dichotomy is our only choice. You are also conflating two different things. Ignoring possible out of bounds pointers is perfectly fine. Deleting assignments from aliased pointers is not. Very different things.
vyodaiken··on Undefined behavior in C is a reading error
a) the assumption is not false if it is not false! b) the speedup is not shown anywhere
vyodaiken··on Undefined behavior in C is a reading error
Linux clone, pthreads, and os code commonly look at stack boundaries
vyodaiken··on Undefined behavior in C is a reading error
You are advocating incorrect code that uses a few less machine operations than correct code.
vyodaiken··on Undefined behavior in C is a reading error
Sure, if you leave out error checks code runs faster. Compile mutexes to no-ops and get an even better speedup.
vyodaiken··on Undefined behavior in C is a reading error
Clang uses vectors for both. https://gcc.godbolt.org/z/G997Ge9KT
vyodaiken··on Undefined behavior in C is a reading error
Leaving overflow to the processor is an example of ignoring it with unpredictable results. Deleting overflow checks because you assume, incorrectly, that overflow is impossible is not an example of ignoring with unpredictable effects, it does produce unpredictable effects, though.
vyodaiken··on Undefined behavior in C is a reading error
There is zero customer demand for less reliable code as a tradeoff for "performance"
vyodaiken··on Undefined behavior in C is a reading error
I bet the performance differences are minimal except perhaps on some contrived cases. The compiler should not assume things it doesn't know. But show me a benchmark.
vyodaiken··on Undefined behavior in C is a reading error
e.g. assuming UB can't happen: deleting overflow or null pointer checks, deleting comparisons between pointers that are assumed to point at different objects, ...
vyodaiken··on Undefined behavior in C is a reading error
That's good example, because nobody would complain if stack layouts changed and those programs failed. But if the compiler chooses to "optimize away" checks on stack layout, that's a different thing altogether. Also note that if you use pthreads or Linux clone or you are writing an operating system you can need to rely on exact stack layouts even today.
vyodaiken··on Undefined behavior in C is a reading error
More than slightly convoluted. The obvious intention is that the compiler ignores overflow and lets the processor architecture make the decision. Assuming that overflow doesn't happen is assuming something false. There's no excuse for that and it doesn't "optimize" anything.
vyodaiken··on Undefined behavior in C is a reading error
C programmers prefer control to "optimization". And if you assume UB cannot occur, you should not generate code that makes it happen. Radical UB is not required for optimization: in fact it appears to mostly do nothing positive. There is not a single paper or study showing significant better performance for substantial C code that depends on assuming UB can't happen - just a bunch of hand waving.
vyodaiken··on Undefined behavior in C is a reading error
The net result of your argument is the language has no semantics. I write and test with -O0 and show that f(k)=m. Then I run with -O3 and f(k)=random. Am I required to be an expert on C Standard and compiler development in order to know that, with no warning, my code has always been wrong? What about if f(k)=m under Gcc 10, but now under Gcc 10.1 that whole section of code is skipped? What you are asking programmers to do is to both master the fine points of UB (which is impractical) and look into the future to see what changes may be invisibly committed to the compiler code.
vyodaiken··on Undefined behavior in C is a reading error
For your last point, the extent of UB driven changes to semantics is still not widely known in the programmer community. Programmers don't read the standard - they read K&R, and K&R is right now describing a different language. We've had 15 years of programmers repeatedly filing bug reports to be told that the expected, tested, relied on, behavior was ephemeral. Only very sophisticated projects figure out about UB.

Of course compilers have to make assumptions. The debate is (a) over what assumptions it is proper to make and (b) what are the permissible behaviors. The false dichotomy: either do without any optimizations at all or accept whatever UB gives you, is not a useful approach.

← PreviousPage 2 of 7Next →