Skip to content

Commit cbf121d

Browse files
authored
Add BYN_ENABLE_ASSERTSION option to allow assertions to be disabled. (#2500)
We always enable assertions by default, but this options allows for a build without them. Fix all errors in the ASSERTIONS=OFF build, even though we don't normally build this its good to keep it building.
1 parent 255d28a commit cbf121d

10 files changed

Lines changed: 43 additions & 8 deletions

File tree

CMakeLists.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ if(NOT CMAKE_BUILD_TYPE)
77
set(CMAKE_BUILD_TYPE "Release")
88
endif()
99

10+
# We default to assertions enabled, even in release builds so that we get
11+
# more useful error reports from users.
12+
option(BYN_ENABLE_ASSERTIONS "Enable assertions" ON)
13+
1014
find_package(Git QUIET REQUIRED)
1115
execute_process(COMMAND
1216
"${GIT_EXECUTABLE}" --git-dir=${CMAKE_CURRENT_SOURCE_DIR}/.git describe --tags
@@ -99,7 +103,11 @@ if(MSVC)
99103
# Don't warn about using "strdup" as a reserved name.
100104
add_compile_flag("/D_CRT_NONSTDC_NO_DEPRECATE")
101105

102-
add_nondebug_compile_flag("/UNDEBUG") # Keep asserts.
106+
if(BYN_ENABLE_ASSERTIONS)
107+
# On non-Debug builds cmake automatically defines NDEBUG, so we
108+
# explicitly undefine it:
109+
add_nondebug_compile_flag("/UNDEBUG") # Keep asserts.
110+
endif()
103111
# Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
104112
if( NOT CMAKE_BUILD_TYPE MATCHES "Debug" )
105113
foreach(flags_var_to_scrub
@@ -167,7 +175,11 @@ else()
167175
else()
168176
add_nondebug_compile_flag("-O2")
169177
endif()
170-
add_nondebug_compile_flag("-UNDEBUG") # Keep asserts.
178+
if(BYN_ENABLE_ASSERTIONS)
179+
# On non-Debug builds cmake automatically defines NDEBUG, so we
180+
# explicitly undefine it:
181+
add_nondebug_compile_flag("-UNDEBUG")
182+
endif()
171183
endif()
172184

173185
if(EMSCRIPTEN)

src/asm2wasm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ Name EMSCRIPTEN_DEBUGINFO("emscripten_debuginfo");
139139

140140
// Utilities
141141

142-
static void abort_on(std::string why, Ref element) {
142+
static WASM_NORETURN void abort_on(std::string why, Ref element) {
143143
std::cerr << why << ' ';
144144
element->stringify(std::cerr);
145145
std::cerr << '\n';
146146
abort();
147147
}
148-
static void abort_on(std::string why, IString element) {
148+
static WASM_NORETURN void abort_on(std::string why, IString element) {
149149
std::cerr << why << ' ' << element.str << '\n';
150150
abort();
151151
}

src/cfg/Relooper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,9 +785,11 @@ struct Optimizer : public RelooperRecursor {
785785
}
786786
} else {
787787
// If the block has no switch, the branches must not as well.
788+
#ifndef NDEBUG
788789
for (auto& iter : ParentBlock->BranchesOut) {
789790
assert(!iter.second->SwitchValues);
790791
}
792+
#endif
791793
}
792794
}
793795
return Worked;

src/dataflow/graph.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,11 @@ struct Graph : public UnifiedExpressionVisitor<Graph, Node*> {
652652
// Merge local state for multiple control flow paths, creating phis as needed.
653653
void merge(std::vector<FlowState>& states, Locals& out) {
654654
// We should only receive reachable states.
655+
#ifndef NDEBUG
655656
for (auto& state : states) {
656657
assert(!isInUnreachable(state.locals));
657658
}
659+
#endif
658660
Index numStates = states.size();
659661
if (numStates == 0) {
660662
// We were unreachable, and still are.

src/ir/memory-utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ inline bool ensureLimitedSegments(Module& module) {
120120

121121
// check if we have too many dynamic data segments, which we can do nothing
122122
// about
123-
auto num = numConstant + numDynamic;
124123
if (numDynamic + 1 >= WebLimitations::MaxDataSegments) {
125124
return false;
126125
}
127126

128127
// we'll merge constant segments if we must
129128
if (numConstant + numDynamic >= WebLimitations::MaxDataSegments) {
130129
numConstant = WebLimitations::MaxDataSegments - numDynamic - 1;
131-
num = numConstant + numDynamic;
130+
auto num = numConstant + numDynamic;
131+
WASM_UNUSED(num);
132132
assert(num == WebLimitations::MaxDataSegments - 1);
133133
}
134134

src/passes/PostAssemblyScript.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ static bool isRetain(LocalSet* expr) {
101101
return false;
102102
}
103103

104+
#ifndef NDEBUG
104105
// Tests if the given location is that of a full retain pattern.
105106
static bool isRetainLocation(Expression** expr) {
106107
if (expr != nullptr) {
@@ -110,6 +111,7 @@ static bool isRetainLocation(Expression** expr) {
110111
}
111112
return false;
112113
}
114+
#endif
113115

114116
// Tests if the given call calls release. Note that this differs from what we
115117
// consider a full release pattern, which must also get a local.

src/support/debug.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <set>
2121
#include <string>
2222

23+
#ifndef NDEBUG
24+
2325
static bool debugEnabled = false;
2426
static std::set<std::string> debugTypesEnabled;
2527

@@ -49,3 +51,5 @@ void wasm::setDebugEnabled(const char* types) {
4951
start += type_size + 1;
5052
}
5153
}
54+
55+
#endif

src/support/debug.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,20 @@ void setDebugEnabled(const char* types);
4444

4545
#else
4646

47+
// We have an option to build with assertions disabled
48+
// BYN_ASSERTIONS_ENABLED=OFF, but we currently don't recommend using and we
49+
// don't test with it.
50+
#error "binaryen is currently designed to be built with assertions enabled."
51+
#error "remove these #errors if you want to build without them anyway."
52+
4753
#define BYN_DEBUG_WITH_TYPE(...) \
4854
do { \
4955
} while (false)
5056
#define BYN_TRACE_WITH_TYPE(...) \
5157
do { \
5258
} while (false)
53-
#define isDebugEnabled() (false)
54-
#define setDebugEnabled()
59+
#define isDebugEnabled(type) (false)
60+
#define setDebugEnabled(types)
5561

5662
#endif
5763

src/support/threads.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <thread>
3030
#include <vector>
3131

32+
#include "compiler-support.h"
33+
3234
namespace wasm {
3335

3436
// The work state of a helper thread - is there more to do,
@@ -124,6 +126,7 @@ class OnlyOnce {
124126

125127
void verify() {
126128
auto before = created.fetch_add(1);
129+
WASM_UNUSED(before);
127130
assert(before == 0);
128131
}
129132
};

src/wasm-binary.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ class BufferWithRandomAccess : public std::vector<uint8_t> {
205205
}
206206
BufferWithRandomAccess& operator<<(U32LEB x) {
207207
size_t before = -1;
208+
WASM_UNUSED(before);
208209
BYN_DEBUG(before = size(); std::cerr << "writeU32LEB: " << x.value
209210
<< " (at " << before << ")"
210211
<< std::endl;);
@@ -216,6 +217,7 @@ class BufferWithRandomAccess : public std::vector<uint8_t> {
216217
}
217218
BufferWithRandomAccess& operator<<(U64LEB x) {
218219
size_t before = -1;
220+
WASM_UNUSED(before);
219221
BYN_DEBUG(before = size(); std::cerr << "writeU64LEB: " << x.value
220222
<< " (at " << before << ")"
221223
<< std::endl;);
@@ -227,6 +229,7 @@ class BufferWithRandomAccess : public std::vector<uint8_t> {
227229
}
228230
BufferWithRandomAccess& operator<<(S32LEB x) {
229231
size_t before = -1;
232+
WASM_UNUSED(before);
230233
BYN_DEBUG(before = size(); std::cerr << "writeS32LEB: " << x.value
231234
<< " (at " << before << ")"
232235
<< std::endl;);
@@ -238,6 +241,7 @@ class BufferWithRandomAccess : public std::vector<uint8_t> {
238241
}
239242
BufferWithRandomAccess& operator<<(S64LEB x) {
240243
size_t before = -1;
244+
WASM_UNUSED(before);
241245
BYN_DEBUG(before = size(); std::cerr << "writeS64LEB: " << x.value
242246
<< " (at " << before << ")"
243247
<< std::endl;);

0 commit comments

Comments
 (0)