Skip to content

Commit 41689a5

Browse files
jujkripken
authored andcommitted
Add initialization functions for passes to avoid missing pass registration due to linker dead code elimination. Fixes WebAssembly#577.
1 parent cd788a1 commit 41689a5

28 files changed

Lines changed: 177 additions & 49 deletions

CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ FOREACH(SUFFIX "_DEBUG" "_RELEASE" "_RELWITHDEBINFO" "_MINSIZEREL" "")
3838
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib")
3939
ENDFOREACH()
4040

41+
SET(all_passes passes)
42+
4143
IF(MSVC)
4244
IF(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.0") # VS2013 and older explicitly need /arch:sse2 set, VS2015 no longer has that option, but always enabled.
4345
ADD_COMPILE_FLAG("/arch:sse2")
@@ -51,7 +53,6 @@ IF(MSVC)
5153
ENDIF()
5254
ADD_COMPILE_FLAG("-D_CRT_SECURE_NO_WARNINGS")
5355
ADD_COMPILE_FLAG("-D_SCL_SECURE_NO_WARNINGS")
54-
SET(all_passes passes)
5556
ELSE()
5657
SET(THREADS_PREFER_PTHREAD_FLAG ON)
5758
SET(CMAKE_THREAD_PREFER_PTHREAD ON)
@@ -72,11 +73,6 @@ ELSE()
7273
ADD_COMPILE_FLAG("-O2")
7374
ADD_DEFINITIONS("-UNDEBUG") # Keep asserts.
7475
ENDIF()
75-
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
76-
SET(all_passes "-force_load" passes)
77-
ELSE()
78-
SET(all_passes "-Wl,--whole-archive" passes "-Wl,-no-whole-archive")
79-
ENDIF()
8076
ENDIF()
8177

8278
# clang doesn't print colored diagnostics when invoked from Ninja

src/asm2wasm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "mixed_arena.h"
2828
#include "asmjs/shared-constants.h"
2929
#include "asm_v_wasm.h"
30+
#include "passes/passes.h"
3031
#include "pass.h"
3132
#include "ast_utils.h"
3233
#include "wasm-builder.h"

src/pass.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Pass;
3232
// Global registry of all passes in /passes/
3333
//
3434
struct PassRegistry {
35+
PassRegistry();
36+
3537
static PassRegistry* get();
3638

3739
typedef std::function<Pass* ()> Creator;
@@ -42,6 +44,8 @@ struct PassRegistry {
4244
std::string getPassDescription(std::string name);
4345

4446
private:
47+
void registerPasses();
48+
4549
struct PassInfo {
4650
std::string description;
4751
Creator create;
@@ -51,18 +55,6 @@ struct PassRegistry {
5155
std::map<std::string, PassInfo> passInfos;
5256
};
5357

54-
//
55-
// Utility class to register a pass. See pass files for usage.
56-
//
57-
template<class P>
58-
struct RegisterPass {
59-
RegisterPass(const char* name, const char *description) {
60-
PassRegistry::get()->registerPass(name, description, []() {
61-
return new P();
62-
});
63-
}
64-
};
65-
6658
//
6759
// Runs a set of passes, in order
6860
//

src/passes/CoalesceLocals.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,12 @@ void CoalesceLocalsWithLearning::pickIndices(std::vector<Index>& indices) {
595595

596596
// declare passes
597597

598-
static RegisterPass<CoalesceLocals> registerPass1("coalesce-locals", "reduce # of locals by coalescing");
598+
Pass *createCoalesceLocalsPass() {
599+
return new CoalesceLocals();
600+
}
599601

600-
static RegisterPass<CoalesceLocalsWithLearning> registerPass2("coalesce-locals-learning", "reduce # of locals by coalescing and learning");
602+
Pass *createCoalesceLocalsWithLearningPass() {
603+
return new CoalesceLocalsWithLearning();
604+
}
601605

602606
} // namespace wasm

src/passes/DeadCodeElimination.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,9 @@ struct DeadCodeElimination : public WalkerPass<PostWalker<DeadCodeElimination, V
346346
}
347347
};
348348

349-
static RegisterPass<DeadCodeElimination> registerPass("dce", "removes unreachable code");
349+
Pass *createDeadCodeEliminationPass() {
350+
return new DeadCodeElimination();
351+
}
350352

351353
} // namespace wasm
352354

src/passes/DropReturnValues.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ struct DropReturnValues : public WalkerPass<PostWalker<DropReturnValues, Visitor
7575
}
7676
};
7777

78-
static RegisterPass<DropReturnValues> registerPass("drop-return-values", "stops relying on return values from set_local and store");
78+
Pass *createDropReturnValuesPass() {
79+
return new DropReturnValues();
80+
}
7981

8082
} // namespace wasm
8183

src/passes/DuplicateFunctionElimination.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ struct DuplicateFunctionElimination : public Pass {
164164
}
165165
};
166166

167-
static RegisterPass<DuplicateFunctionElimination> registerPass("duplicate-function-elimination", "removes duplicate functions");
167+
Pass *createDuplicateFunctionEliminationPass() {
168+
return new DuplicateFunctionElimination();
169+
}
168170

169171
} // namespace wasm
170172

src/passes/LowerIfElse.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ struct LowerIfElse : public WalkerPass<PostWalker<LowerIfElse, Visitor<LowerIfEl
6060
}
6161
};
6262

63-
static RegisterPass<LowerIfElse> registerPass("lower-if-else", "lowers if-elses into ifs, blocks and branches");
63+
Pass *createLowerIfElsePass() {
64+
return new LowerIfElse();
65+
}
6466

6567
} // namespace wasm

src/passes/LowerInt64.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ struct LowerInt64 : public Pass {
189189
}
190190
};
191191

192-
static RegisterPass<LowerInt64> registerPass("lower-i64", "lowers i64 into pairs of i32s");
192+
Pass *createLowerInt64Pass() {
193+
return new LowerInt64();
194+
}
193195

194196
} // namespace wasm

src/passes/MergeBlocks.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks, Visitor<MergeBloc
178178
}
179179
};
180180

181-
static RegisterPass<MergeBlocks> registerPass("merge-blocks", "merges blocks to their parents");
181+
Pass *createMergeBlocksPass() {
182+
return new MergeBlocks();
183+
}
182184

183185
} // namespace wasm
184186

0 commit comments

Comments
 (0)