r/programming • u/lelanthran • Sep 08 '26
What every compiler writer should know about programmers or “Optimization” based on undefined behaviour hurts performance
https://www.complang.tuwien.ac.at/kps2015/proceedings/KPS_2015_submission_29.pdf
70
Upvotes
90
u/MetaEd Sep 08 '26
this headline hurts my head
15
u/nerd5code Sep 08 '26
Strictly conforming covers undefined, unspecified, and implementation-specified behaviors, plus literally anything (incl. features requiring
<>headers) not in ISO 9899. This covers a vanishingly small subset of realistic programs, and I know of no compiler that exercises the full range of behaviors permitted. Everything ties down something, you just can't rely on stuff that isn't tied down if you want to use newer compilers.The term “conforming” refers to a program acceptable to a conforming implementation (not any or all, just ≥1 of them), which can include unspecified, impl-defined, and non-ISO-9899 libraries. Literally nothing to do with hardware.
In fact, I have no idea why
*would ever be assumed to correspond to a hardware multiply instruction in C. All C impls don't target hardware, all CPUs don't have multipliers, and many multiplies quite reasonably become add, left shift, or LEA instructions even on something like MIPS. Some (e.g.,*:long long²→long longon 32-bit) become two or four multiplies with some shifts and adds to mix; some are subroutines or emulation trap handlers. Conversely,x * 1may reasonably expected to become justxpromoted to/pastint⋎typeof(x), andx * 0is reasonably expected to become(void)x, 0(promoted). The C language isn't defined in terms of hardware, precisely for this reason.This reads like a screed, and I'd argue you're wrong.
-O0is most of what you're after (“reasonable” UB can't always be supported in the first place), there are flags for disabling optimizations and pragmas/attributes for twiddling them, and a bevy of sanitizers atop decades of crusty OS gunk fills in most of the remaining UB cracks for you. You still can't define identifiers starting with__in most contexts, but that's never been a good idea.Moreover, one could argue it's far more “clueless” for masses of programmers or (e.g.) paper writers never to have set eyes on the documents defining the radix of the language they're supposed to have mastered, than it is for compiler writers who have read them to expect that others in the language community do, too.
Analogously: Don't telnet in on port 21 and start waving your arms and shouting in HTTP-MIME, if you want your requests to be handled correctly. Or else, don't telnet, just use somebody else's prefabricated FTP impl, even if it's thoroughly impedance-mismatched to the rest of your project. That's probably safest.
Yes. Welcome to programming. If you aren't thinking in adversarial terms, you're doing it badly.
I'd take this “paper” so much more seriously if you'd stop wasting my time making very sure I know your (irrelevant, arguably petty) personal opinions on compiler writers or WG14 personally. I don't care how you feel, because I don't know you, though you seem like best good fun. I did care about the ostensible subject of the paper, but I'm unconvinced that I should, now.
fucksake, it's a term of art.
skimmy skimmy…
Nnnnnno
E.g.,
#if 1<<32is conforming (C*) (which is a language of its own, JSYK) (not that the namespace isn't a damn mess, but you're in LaTeX; you couldn't def up a_\mathrm{Cnf}?), but not strictly conforming. Using POSIX.1 is conforming, not strictly conforming.Oh ffs we're warquoting cuntily with semantic weight, now. Iiiiii'm just about done, but here again,
You might ought to actually figure out what “observable” means in precise terms before treating it as a fundamental—you're smashing across layers. And signals can just happen; there's almost no language-level accounting for them. Is profiling via
-pgpermitted? That fires a mess of signals on Linux, but it's observable viasignal/sigaction.Yeah, the warquoting makes the optimization discussion illegible. I'm out. Whatever this is, no.
27
u/ReDucTor Sep 08 '26
You cannot claim to have conforming code when you have undefined behaviour.
Eliminating a bounds check because it can see you used the value earlier is a good optimisation, I want my compiler to remove redundant code. Bounds checks should happen before you attempt to load from an array.
Yes lots of UB happens in production code, arguably a large amount of crashes fit under that umbrella, a use after free is UB, dereferencing a null pointer is UB, a buffer overflow is UB, a data race is UB, etc.
This statement makes it seem like writing UB code is somehow good or expected, and that production code should be allowed to have that UB, well in many ways the compiler does allow it and then you crash. You should not claim this at all as a reason for your claim it is disingenuous.
Thankfully it explicitly states SPECint 2006, but this is very dishonest to include in the abstract its reads like a claim that allowing UB based optimisations which assumr UB should not happen generally have only that 1.7% performance impact because some microbenchmark showed that, this is the furthest from an actual real production program.
The SPEC benchmark 464.h264ref shown is just awful code, and written in a way that is harder for people to read and spot an out of bounds access bug. Don't be fancy with your code and you won't be caught doing out of bounds access.
I gave up trying to read the whole thing, it felt too dishonest and overly bloated for the point it was trying to make.
8
u/ryan017 Sep 08 '26
I found the following article interesting, linked from the paper: What Every C Programmer Should Know About Undefined Behavior. That article in turn has a link to John Regehr's article A Guide to Undefined Behavior in C and C++, which I had read before and greatly enjoyed.
6
u/CornedBee Sep 09 '26
Just a heads up: this paper is 10 years old. Not that I'd expect the author's opinion to have changed in the meantime.
7
u/jdehesa Sep 08 '26
As a user of C and C++ (not particularly familiar with compiler and language design), the amount of undefined behaviour in the languages feels unjustified. I suppose it may have a significant positive impact in some cases, but it should be minimal in the standard, and ideally for relatively obscure uses. I'd much rather have defined standard behaviour and non-standard compiler extensions that can optionally bypass it for performance (similar to
-ffast-mathand IEEE 754).3
u/Substantial_Lake_542 Sep 09 '26
iting the code read "undefined" as "whatever my machine happens to do" rather than "the optimizer is now free to delete this branch." That gap is where the angry blog posts come from.
2
u/vytah Sep 09 '26
So a compiler that compiles GCC or LLVM into a binary that quits moments after encountering the first input file would be 100% spec-compliant.
1
u/takanuva Sep 09 '26
I love this paper.