[PATCH] D121750: Add a cmake flag to turn `llvm_unreachable()` into builtin_trap() when assertions are disabled

Duncan P. N. Exon Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 21 12:33:40 PDT 2022


dexonsmith added a comment.

I suggest:

  #ifndef NDEBUG
  #define llvm_unreachable(msg) \
    ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
  #elif !defined(LLVM_BUILTIN_UNREACHABLE)
  #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal()
  #elif LLVM_UNREACHABLE_OPTIMIZE
  #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
  #else
  #define llvm_unreachable(msg) LLVM_BUILTIN_TRAP, LLVM_BUILTIN_UNREACHABLE
  #endif

I *think* this will have all the intended effects:

- If asserts are off, use the function (like a fancy assertion).
- Else, if there is no LLVM_BUILTIN_UNREACHABLE, call the function without args (works again as of https://reviews.llvm.org/D122167).
- Else, always call LLVM_BUILTIN_UNREACHABLE to suppress warnings about this control flow path...
  - ... but if !LLVM_UNREACHABLE_OPTIMIZE, first call LLVM_BUILTIN_TRAP to block optimizations.
  - ... and if LLVM_BUILTIN_TRAP happens to be marked no-return, the extra LLVM_BUILTIN_UNREACHABLE should get cleaned up by the optimizer.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121750/new/

https://reviews.llvm.org/D121750



More information about the llvm-commits mailing list