[LLVMdev] [patch] Change llvm_unreachable to use __builtin_unreachable() in -asserts
John McCall
rjmccall at apple.com
Sat Mar 12 10:29:16 PST 2011
On Mar 12, 2011, at 3:01 AM, Duncan Sands wrote:
> if the point is to have a better assert, why not introduce llvm_assert and
> do a bulk replace of all asserts with it?
The point is not to have a better assert; it's to have a better unreachable
marker. There are lots of reasons to use a dedicated unreachable marker
instead of assert(0):
- the intent is much more obvious in the code;
- assertions disappear entirely in -Asserts builds, which is obviously
desirable, but we don't necessarily want that for unreachable markers
because of the point Sebastian raised; and
- we can't optimize based on generic assert conditions (*) because it's
extremely common to assert on relatively expensive conditions that
the programmer certainly doesn't want to see evaluated in -Asserts builds.
By contrast, optimizing based on unreachability is quite simple.
(*) I'm aware of research into doing this, but it requires fairly sophisticated
compiler support so that you don't start running those expensive conditions
all the time.
> By the way, GCC does this:
Note how GCC also has different macros for normal assertion checks vs.
marking unreachable code, presumably for exactly the reasons above.
We're following that same model, except our unreachable macro:
- takes an explanatory string, which we honor in +Asserts builds, and
- can be optimized in -Asserts builds.
John.
More information about the llvm-dev
mailing list