LLVM Discussion Forums

Optimizer ub notification in llvmc

In some cases when a compiler or user creates code with undefined behavior, llvms optimizer kills that code. Is there a way to get notified of this happening from llvm-c?

The compiler is never “creating” undefined behavior as far as I know, maybe you’re referring to something I am not understanding well?

LLVM optimizers don’t notify in these cases, because the compiler never detects undefined behavior and nukes it. Instead the optimizer assumes that there is not undefined behavior in the code provided by the user and will optimize accordingly. As such most of the “warnings” would actually not be about actual undefined behavior but about impossible condition happening in the code.

My compiler accidentally did. Someone in discord posted that my code triggers this:

WARNING: Ignoring 4 byte store @0 which extends past the end of the 2 byte alloca:
    alloca:   %0 = alloca %TuplePair
       use:   store i32 %1, i32* %2

What I want is to capture that happening c side.

I am the one who posted this in Discord :slight_smile:
So I confirm that the compiler didn’t “create code with undefined behavior”.
You provided an input to the compiler that exhibited undefined behavior. I’d argue that the best the optimizer should have done is nuking the entire body: the only way the program is valid is if this function is never executed.

If we were to warn in such cases, we may have a lot of warning from dead code: it is only UB if it executes.

The best way to be warned about Undefined Behavior is to compiled with UBSAN (clang -fsanitize=undefined).

1 Like

The compiler in this case is my frontend hence the confusion. Thanks!