How to deal with Compiler crash caused by UB

Hi, everyone.
I have a problem. For example, there is an LLVM IR

%result = shl i32 %ori, 33

Accroding to cppreference, such operation should be a UB. In this case, LLVM converts the value to a poison.


So the problem is in one of the subsequent optimization PASSes that tries to cast the value to insrtuction and causes an error. Like this code:

WideInc = cast<Instruction>(WidePhi->getIncomingValueForBlock(LatchBlock));

I don’t think it’s a problem to do this directly when there is no UB.
But I want to know, should we just let this problem run its course? Or should it produce a more friendly compiler error message?
However, I think this is a UB found during optimization. We don’t even know if the pre-optimization was wrong or the input source actually has UB.

The compiler itself should not crash even if the input code contains UB. If that happens, it’s a bug that should be fixed.

5 Likes