Skip to content

Commit 91b3489

Browse files
authored
Add llvm assertions trap and no strings (#6838)
Add option LLVM_ASSERTIONS_NO_STRINGS When defined, drop the stringized expression, __FILE__, and __FUNCTION__ strings passed to llvm_assert. This dramatically reduces the binary size, which is useful when enabling assertions in a non-debug build. Add option LLVM_ASSERTIONS_TRAP When enabled, this forces asserts to always trap. Currently, on Windows asserts calls RaiseException, while they trap on non-Windows. This option makes the assertion behaviour consistent across platforms.
1 parent 71d6766 commit 91b3489

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ else()
325325
option(LLVM_ENABLE_ASSERTIONS "Enable assertions" ON)
326326
endif()
327327

328+
option(LLVM_ASSERTIONS_TRAP "Force assertions to always trap, rather than the default unspecified behavior (e.g. RaiseException on Windows)" OFF)
329+
option(LLVM_ASSERTIONS_NO_STRINGS "Make assertion macro drops strings to reduce binary size" OFF)
330+
328331
set(LLVM_ABI_BREAKING_CHECKS "WITH_ASSERTS" CACHE STRING
329332
"Enable abi-breaking checks. Can be WITH_ASSERTS, FORCE_ON or FORCE_OFF.")
330333

cmake/modules/HandleLLVMOptions.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ if( LLVM_ENABLE_ASSERTIONS )
7878
"${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
7979
endforeach()
8080
endif()
81+
82+
if (LLVM_ASSERTIONS_TRAP)
83+
add_definitions( -DLLVM_ASSERTIONS_TRAP )
84+
endif()
85+
if (LLVM_ASSERTIONS_NO_STRINGS)
86+
add_definitions( -DLLVM_ASSERTIONS_NO_STRINGS )
87+
endif()
8188
else()
8289
# Disable assertions in Debug builds
8390
if( uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )

include/llvm/llvm_assert/assert.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ void llvm_assert(const char *Message, const char *File, unsigned Line,
3737
}
3838
#endif
3939

40+
// If LLVM_ASSERTIONS_NO_STRINGS is defined, pass empty strings to llvm_assert
41+
// to reduce binary size.
42+
#ifdef LLVM_ASSERTIONS_NO_STRINGS
43+
#define assert(Expression) \
44+
((void)((!!(Expression)) || (llvm_assert("", "", 0, ""), 0)))
45+
#else
4046
#define assert(Expression) \
4147
((void)((!!(Expression)) || \
4248
(llvm_assert(#Expression, __FILE__, __LINE__, __FUNCTION__), 0)))
49+
#endif
4350

4451
#endif /* NDEBUG */

lib/Support/assert.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,37 @@
88
///////////////////////////////////////////////////////////////////////////////
99

1010
#include "assert.h"
11+
#include "llvm/Support/Compiler.h"
12+
#include "llvm/Support/raw_ostream.h"
13+
namespace {
14+
void llvm_assert_trap(const char *_Message, const char *_File, unsigned _Line,
15+
const char *_Function) {
16+
llvm::errs() << "Error: assert(" << _Message << ")\nFile:\n"
17+
<< _File << "(" << _Line << ")\nFunc:\t" << _Function << "\n";
18+
LLVM_BUILTIN_TRAP;
19+
}
20+
} // namespace
1121

1222
#ifdef _WIN32
13-
1423
#include "dxc/Support/Global.h"
1524
#include "windows.h"
1625

1726
void llvm_assert(const char *Message, const char *File, unsigned Line,
1827
const char *Function) {
28+
#ifdef LLVM_ASSERTIONS_TRAP
29+
llvm_assert_trap(Message, File, Line, Function);
30+
#else
1931
OutputDebugFormatA("Error: assert(%s)\nFile:\n%s(%d)\nFunc:\t%s\n", Message,
2032
File, Line, Function);
2133
RaiseException(STATUS_LLVM_ASSERT, 0, 0, 0);
34+
#endif
2235
}
2336

24-
#else
25-
26-
#include "llvm/Support/Compiler.h"
27-
#include "llvm/Support/raw_ostream.h"
37+
#else /* _WIN32 */
2838

2939
void llvm_assert(const char *Message, const char *File, unsigned Line,
3040
const char *Function) {
31-
llvm::errs() << "Error: assert(" << Message << ")\nFile:\n"
32-
<< File << "(" << Line << ")\nFunc:\t" << Function << "\n";
33-
LLVM_BUILTIN_TRAP;
41+
llvm_assert_trap(Message, File, Line, Function);
3442
}
3543

36-
#endif
44+
#endif /* _WIN32 */

0 commit comments

Comments
 (0)