Skip to content

Commit 403b9cf

Browse files
authored
[WebAssembly] Use RefTypeMem2Local instead of Mem2Reg (#83196)
When reference-types feature is enabled, forcing mem2reg unconditionally even in `-O0` has some problems described in #81575. This uses RefTypeMem2Local pass added in #81965 instead. This also removes `IsForced` parameter added in 890146b given that we don't need it anymore. This may still hurt debug info related to reference type variables a little during the backend transformation given that they are not stored in memory anymore, but reference type variables are presumably rare and it would be still a lot less damage than forcing mem2reg on the whole program. Also this fixes the EH problem described in #81575. Fixes #81575.
1 parent ae709c1 commit 403b9cf

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

llvm/include/llvm/Transforms/Utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extern char &LCSSAID;
7070
// %Y = load i32* %X
7171
// ret i32 %Y
7272
//
73-
FunctionPass *createPromoteMemoryToRegisterPass(bool IsForced = false);
73+
FunctionPass *createPromoteMemoryToRegisterPass();
7474

7575
//===----------------------------------------------------------------------===//
7676
//

llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,9 @@ void WebAssemblyPassConfig::addISelPrepare() {
478478
WasmTM->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
479479
std::string(WasmTM->getTargetFeatureString()));
480480
if (Subtarget->hasReferenceTypes()) {
481-
// We need to remove allocas for reference types
482-
addPass(createPromoteMemoryToRegisterPass(true));
481+
// We need to move reference type allocas to WASM_ADDRESS_SPACE_VAR so that
482+
// loads and stores are promoted to local.gets/local.sets.
483+
addPass(createWebAssemblyRefTypeMem2Local());
483484
}
484485
// Lower atomics and TLS if necessary
485486
addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));

llvm/lib/Transforms/Utils/Mem2Reg.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,15 @@ namespace {
7474
struct PromoteLegacyPass : public FunctionPass {
7575
// Pass identification, replacement for typeid
7676
static char ID;
77-
bool ForcePass; /// If true, forces pass to execute, instead of skipping.
7877

79-
PromoteLegacyPass() : FunctionPass(ID), ForcePass(false) {
80-
initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
81-
}
82-
PromoteLegacyPass(bool IsForced) : FunctionPass(ID), ForcePass(IsForced) {
78+
PromoteLegacyPass() : FunctionPass(ID) {
8379
initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
8480
}
8581

8682
// runOnFunction - To run this pass, first we calculate the alloca
8783
// instructions that are safe for promotion, then we promote each one.
8884
bool runOnFunction(Function &F) override {
89-
if (!ForcePass && skipFunction(F))
85+
if (skipFunction(F))
9086
return false;
9187

9288
DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
@@ -115,6 +111,6 @@ INITIALIZE_PASS_END(PromoteLegacyPass, "mem2reg", "Promote Memory to Register",
115111
false, false)
116112

117113
// createPromoteMemoryToRegister - Provide an entry point to create this pass.
118-
FunctionPass *llvm::createPromoteMemoryToRegisterPass(bool IsForced) {
119-
return new PromoteLegacyPass(IsForced);
114+
FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
115+
return new PromoteLegacyPass();
120116
}

llvm/test/CodeGen/WebAssembly/ref-type-mem2local.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt < %s -wasm-ref-type-mem2local -S | FileCheck %s
1+
; RUN: llc < %s -mattr=+reference-types -stop-after=wasm-ref-type-mem2local | FileCheck %s
22

33
target triple = "wasm32-unknown-unknown"
44

0 commit comments

Comments
 (0)