Llvm_unreachable is widely misused

I feel like I’m stepping into a trap by commenting here, but I think historically and culturally, the fact that assert(X) doesn’t compile to __buitlin_assume(!X) in NDEBUG builds is considered to be a missing feature by the people who wrote the guidance that you referenced. In that light, unreachable is an upgrade over assert(0): it implements the missing feature. I think there’s a strong argument that LLVM could make different decisions on switch lookup table bounds checks, but folding away conditional branches is kind of the point here. The guidance was written by compiler engineers with the intent to provide the compiler as much information as possible so that it could optimize as much as possible.

It’s totally legitimate to question these values. This is just my attempt to capture the zeitgeist from an earlier period in LLVM’s history.

3 Likes