r/cpp • • Jul 09 '19

Why LLVM may call a never called function?

https://habr.com/en/post/458442/
70 Upvotes

19 comments sorted by

46

u/[deleted] Jul 09 '19

"Learning the source code of LLVM optimizers will help you write your optimizations, allowing you to improve the code for some specific cases."

As if I don't have enough to do already.

46

u/gvargh Jul 09 '19

"don't optimize by hand when the compiler can do it better"

"... btw you have to write your code so that the compiler can actually do it properly. here's the source. have fun!"

13

u/[deleted] Jul 09 '19

warning: may not be the best use of time. ymmv. this advice is given without even the implication of warranty

18

u/grumbelbart2 Jul 09 '19

15

u/ruler501 Jul 09 '19

Note to anyone going to read that a lot of the comments are pretty toxic. A lot of strong feelings around optimization of UB.

-12

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 09 '19

Which should serve as yet another warning signal that the current state of UB - and compiler writers interpretation of it - is simply not workable.

6

u/Guvante Jul 10 '19

Most compilers give you the ability to turn off the weird optimizers. The problem is all of them provide performance benefits in most cases since that is why they were written.

This example can provide more performant initialization than the standard allows for as an example.

Heck in normal code this might "fix" a race condition by ensuring your only initialization function is called first.

3

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 10 '19

I'm not really sold on the performance benefits being significant. And what I'm definitely not sold on is them being significant enough to warrant turning such unsafe optimizations (escalating "simple" bugs to potentially order of magnitude worse ones) on by default.

That kind of points to the root of the problem: We have a bunch of behavior that depends on things that aren't actually listed in any or two places (you basically have to read the whole standard to find all cases of UB), is potentially very dangerous (turning f.ex. segmentation fault DOS into privilege escalation), cannot be disabled by any single compiler switch and is enabled by default.

I'd be all for allowing any optimizations based on UB as long as 1) the behaviors were clearly listed, 2) they could always be turned off by a single switch ("-fno-unsafe-opts") and 3) such optimizations were never on by default with any of the normal -O switches. So, rather like the current -ffast-math. Which incidentally also has never produced measurable speedup in my 15+ years of writing computationally intensive floating point code. But at least that is not on by default.

4

u/Guvante Jul 10 '19

A universal unsafe would probably be just dropping optimizations basically. If you want the safest option you don't want any standard breaking settings which most optimization passes usually violate in subtle ways.

The performance gains are significant which is why every major compiler uses them. Fast math is mostly about checks for denormals and such IIRC.

1

u/[deleted] Jul 10 '19

I think this can't happen in Rust:

  • statics must be initialized, and

  • function types can't be null (you'd need to wrap them into an Option<Function>).

Translating the example to Rust, either the example is optimized even better (the whole body of main is removed), or only a function that the user did set into the static gets called.

-1

u/neuroblaster Jul 10 '19

I think it's a compiler bug.

in accordance with the C standard, all global variables have zero values when a program starts

The rest is a lengthy description of how some optimization is bugging out. AFAIK, according to standard, that pointer should have been initialized with zero, if it's not, then it was compiled with substandard compiler.

LLVM is very cool technology, but contradictory to common belief, clang has a long way to go.

8

u/tvaneerd C++ Committee, lockfree, PostModernCpp Jul 10 '19

It was (in effect) initialized with zero. There is an overriding "as-if" rule - basically, if your well-formed program can't tell, the compiler doesn't need to do it. If you had 1000 global variables that are never accessed, the compiler doesn't need to initialize them, or even include them in the program.

"If a tree falls in a forest, does it make a sound?"
Yes, but the C++ simulation of it might not bother producing the sound, because you can't tell if it did or not.

-1

u/neuroblaster Jul 10 '19

Yeah, that's true, but in that case variable was accessed and standard insists that this variable has to be initialized with zero when application starts.

This is either a compiler bug or substandard compiler, it simply assumes what it shouldn't assume. I would expect compiled program to make a function call on `nullptr`. Yeah, it's UB, but this is what it was programmed to do and this is what compiled program should do when computer programmer tell it to.

11

u/tvaneerd C++ Committee, lockfree, PostModernCpp Jul 10 '19

It did. It performed UB, as requested.

7

u/[deleted] Jul 10 '19

Yeah, it's UB

So all bets are off. That's the point of UB.

-3

u/neuroblaster Jul 10 '19

UB occurs when `nullptr` is called, not before that. Before that program should behave according to standard if it's compiled with conforming compiler. As far as i'm concerned, this means that at entry point `Do` should be initialized with zero.

Do you see the difference between "program execution results in UB" and "compiler execution results in UB"? In former case it's application bug, in latter case it's a compiler bug.

8

u/dodheim Jul 10 '19

From [intro.execution]:

This International Standard imposes no requirements on the behavior of programs that contain undefined behavior.

Individual operations are undefined, yes; but when a program contains any operation that is undefined, as was said, all bets are off.

8

u/NotAYakk Jul 11 '19

You seem to be under the misunderstanding that your personal, naive mapping of C++ to assembly/machine code on the machine you are compiling the code to run on is what the code means.

And I get it. That naive mapping is a great way to get a bit of intuition about what your code is doing. But if you stop there, you'll (a) be confused by what C++ actually does, and (b) not be able to write code that runs as fast.

C++ program behaviour is defined in terms of an abstract machine. At runtime, the compiler is responsible to make code that is consistent with that abstract machine's behaviour.

But the actual abstract machine variables, values, etc? There is zero requirement that the compiled program contain any of it. A value being zero in the abstract machine does not require any zero bytes at any time exist within the compiled executing program.

Now, any defined behaviour that would generate observable behaviour where the value matters must behave as-if the zero was there.

For a fun example, there are platforms where null pointers are 0xffffffff. Assigning literal zero to a pointer makes it 0xfffffff.

Undefined behaviour where it would, if actually zero bytes, do something specific on the target platform? Need not be zero in the target platform.

You may think you want a "convert code line by line to platform specific assembly, then as-if optimize", but that leads to horrid assembly code in even simple loops. The restrictions on what you are allowed to do permit the compiler to not have to prove intractable statements about your program in order to locally optimize really useful stuff.

This example here is contrived, but the technique used here lets loops be inverted, complex equations be refactored and simplified, expensive function calls be elided, and lets programmers write clear structured code that converts to crazy optimized assembly.

Note that if you want a hard crashing pointer you can write one. Just whip up a smart pointer, where *, ->, ->* and () all check for null and explicitly fast terminate your program. Find the right built-in and it could plausibly even compile to the asm you'd want.