On Sat, Apr 26, 2025 at 5:31 PM Linus Torvalds <torva...@linux-foundation.org> wrote: > So please. Clang people need to get a clue. Yes, we care *deeply* > about performance in the kernel, but a C compiler that thinks that > using UD to generate "better" code is a disgrace and pure garbage. > Because security matters a whole lot too, and the downsides of turning > undefined behavior into random garbage are about a million times > bigger than the "I can remove one integer instruction for zero gain". > > For the kerrnel, we want to disable absolutely all undefined behavior > crap ideas by the compiler. It's why we use -fwrapv and have for > years. It's why we disable the idiotic "strict alias" stuff that > should never have become part of C. And it's why I want that "turn UD > into unreachable" mindfart fixed. > > The notion of "optimizing" unreachable code is crazy. And the notion > of thinking that "UD means unreachable" is so incredibly insane that > any compiler person that thinks it is reasonable should have his head > examined.
I tend to agree that generating bad code in the face of UB is bad; there was another, unrelated, instance where Clang silently generated bad code with UB. The problem borders on the philosophical. But I believe the argument is roughly "generating 'good' code in the face of UB is a never-ending game of Wack-A-Mole(tm), where we don't actually know what the correct outcome should be." ("Correct" implying that the compiler could somehow divine what the programmer was hoping for, or at least get close to it, but it's not possible.) This situation is one of the easier ones: "do something other than fall into the next function"; but there are far more involved examples, of course. And even in this case, the compiler needs to know if a "trap" is okay, or would returning with garbage in %rax be okay. -bw