Skip to content

Commit 7b98391

Browse files
committed
Add a cmake flag to turn llvm_unreachable() into builtin_trap() when assertions are disabled
This re-lands 6316129 after fixing the condition logic. The new flag seems to not be working yet on Windows, where the builtin trap isn't "no return". Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D121750
1 parent 89d8035 commit 7b98391

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

llvm/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
465465
option(LLVM_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
466466

467467
option(LLVM_ENABLE_DUMP "Enable dump functions even when assertions are disabled" OFF)
468+
option(LLVM_UNREACHABLE_OPTIMIZE "Optimize llvm_unreachable() as undefined behavior (default), guaranteed trap when OFF" ON)
468469

469470
if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
470471
option(LLVM_ENABLE_ASSERTIONS "Enable assertions" OFF)

llvm/docs/CMake.rst

+6
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ enabled sub-projects. Nearly all of these variable names begin with
300300
enabled or not. A version of LLVM built with ABI breaking checks
301301
is not ABI compatible with a version built without it.
302302

303+
**LLVM_UNREACHABLE_OPTIMIZE**:BOOL
304+
This flag controls the behavior of `llvm_unreachable()` in release build
305+
(when assertions are disabled in general). When ON (default) then
306+
`llvm_unreachable()` is considered "undefined behavior" and optimized as
307+
such. When OFF it is instead replaced with a guaranteed "trap".
308+
303309
**LLVM_APPEND_VC_REV**:BOOL
304310
Embed version control revision info (Git revision id).
305311
The version info is provided by the ``LLVM_REVISION`` macro in

llvm/include/llvm/Config/llvm-config.h.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,8 @@
112112
/* Define if building LLVM with LLVM_FORCE_USE_OLD_TOOLCHAIN_LIBS */
113113
#cmakedefine LLVM_FORCE_USE_OLD_TOOLCHAIN ${LLVM_FORCE_USE_OLD_TOOLCHAIN}
114114

115+
/* Define if llvm_unreachable should be optimized with undefined behavior
116+
* in non assert builds */
117+
#cmakedefine01 LLVM_UNREACHABLE_OPTIMIZE
118+
115119
#endif

llvm/include/llvm/Support/ErrorHandling.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,23 @@ llvm_unreachable_internal(const char *msg = nullptr, const char *file = nullptr,
124124

125125
/// Marks that the current location is not supposed to be reachable.
126126
/// In !NDEBUG builds, prints the message and location info to stderr.
127-
/// In NDEBUG builds, becomes an optimizer hint that the current location
128-
/// is not supposed to be reachable. On compilers that don't support
129-
/// such hints, prints a reduced message instead and aborts the program.
127+
/// In NDEBUG builds, the behavior is controlled by the CMake flag
128+
/// -DLLVM_UNREACHABLE_OPTIMIZE
129+
/// * When "ON" (default) llvm_unreachable() becomes an optimizer hint
130+
/// that the current location is not supposed to be reachable: the hint
131+
/// turns such code path into undefined behavior. On compilers that don't
132+
/// support such hints, prints a reduced message instead and aborts the
133+
/// program.
134+
/// * When "OFF", a builtin_trap is emitted instead of an
135+
// optimizer hint or printing a reduced message.
130136
///
131137
/// Use this instead of assert(0). It conveys intent more clearly and
132138
/// allows compilers to omit some unnecessary code.
133139
#ifndef NDEBUG
134140
#define llvm_unreachable(msg) \
135141
::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
142+
#elif !LLVM_UNREACHABLE_OPTIMIZE
143+
#define llvm_unreachable(msg) LLVM_BUILTIN_TRAP
136144
#elif defined(LLVM_BUILTIN_UNREACHABLE)
137145
#define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
138146
#else

0 commit comments

Comments
 (0)