Skip to content

Commit

Permalink
[WebAssembly] Use RefTypeMem2Local instead of Mem2Reg (#83196)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aheejin authored Mar 6, 2024
1 parent ae709c1 commit 403b9cf
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/Transforms/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extern char &LCSSAID;
// %Y = load i32* %X
// ret i32 %Y
//
FunctionPass *createPromoteMemoryToRegisterPass(bool IsForced = false);
FunctionPass *createPromoteMemoryToRegisterPass();

//===----------------------------------------------------------------------===//
//
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,9 @@ void WebAssemblyPassConfig::addISelPrepare() {
WasmTM->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
std::string(WasmTM->getTargetFeatureString()));
if (Subtarget->hasReferenceTypes()) {
// We need to remove allocas for reference types
addPass(createPromoteMemoryToRegisterPass(true));
// We need to move reference type allocas to WASM_ADDRESS_SPACE_VAR so that
// loads and stores are promoted to local.gets/local.sets.
addPass(createWebAssemblyRefTypeMem2Local());
}
// Lower atomics and TLS if necessary
addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
Expand Down
12 changes: 4 additions & 8 deletions llvm/lib/Transforms/Utils/Mem2Reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,15 @@ namespace {
struct PromoteLegacyPass : public FunctionPass {
// Pass identification, replacement for typeid
static char ID;
bool ForcePass; /// If true, forces pass to execute, instead of skipping.

PromoteLegacyPass() : FunctionPass(ID), ForcePass(false) {
initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
}
PromoteLegacyPass(bool IsForced) : FunctionPass(ID), ForcePass(IsForced) {
PromoteLegacyPass() : FunctionPass(ID) {
initializePromoteLegacyPassPass(*PassRegistry::getPassRegistry());
}

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

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

// createPromoteMemoryToRegister - Provide an entry point to create this pass.
FunctionPass *llvm::createPromoteMemoryToRegisterPass(bool IsForced) {
return new PromoteLegacyPass(IsForced);
FunctionPass *llvm::createPromoteMemoryToRegisterPass() {
return new PromoteLegacyPass();
}
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/WebAssembly/ref-type-mem2local.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: opt < %s -wasm-ref-type-mem2local -S | FileCheck %s
; RUN: llc < %s -mattr=+reference-types -stop-after=wasm-ref-type-mem2local | FileCheck %s

target triple = "wasm32-unknown-unknown"

Expand Down

0 comments on commit 403b9cf

Please sign in to comment.