Skip to content

Commit 5baf66f

Browse files
authored
[CodeGen] Port WasmEHPrepare to new pass manager (llvm#74435)
Port `WasmEHPrepare` to new pass manager, also rename `wasmehprepare` to `wasm-eh-prepare`.
1 parent e07c92a commit 5baf66f

File tree

7 files changed

+68
-15
lines changed

7 files changed

+68
-15
lines changed

llvm/include/llvm/CodeGen/CodeGenPassBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/CodeGen/ReplaceWithVeclib.h"
3030
#include "llvm/CodeGen/SafeStack.h"
3131
#include "llvm/CodeGen/UnreachableBlockElim.h"
32+
#include "llvm/CodeGen/WasmEHPrepare.h"
3233
#include "llvm/CodeGen/WinEHPrepare.h"
3334
#include "llvm/IR/PassManager.h"
3435
#include "llvm/IR/Verifier.h"
@@ -691,7 +692,7 @@ void CodeGenPassBuilder<Derived>::addPassesToHandleExceptions(
691692
// funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
692693
// should remove PHIs there.
693694
addPass(WinEHPreparePass(/*DemoteCatchSwitchPHIOnly=*/false));
694-
addPass(WasmEHPass());
695+
addPass(WasmEHPreparePass());
695696
break;
696697
case ExceptionHandling::None:
697698
addPass(LowerInvokePass());

llvm/include/llvm/CodeGen/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ FUNCTION_PASS("scalarize-masked-mem-intrin", ScalarizeMaskedMemIntrinPass, ())
5454
FUNCTION_PASS("tlshoist", TLSVariableHoistPass, ())
5555
FUNCTION_PASS("unreachableblockelim", UnreachableBlockElimPass, ())
5656
FUNCTION_PASS("verify", VerifierPass, ())
57+
FUNCTION_PASS("wasm-eh-prepare", WasmEHPreparePass, ())
5758
FUNCTION_PASS("win-eh-prepare", WinEHPreparePass, ())
5859
#undef FUNCTION_PASS
5960

@@ -131,7 +132,6 @@ DUMMY_FUNCTION_PASS("select-optimize", SelectOptimizePass, ())
131132
DUMMY_FUNCTION_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass, ())
132133
DUMMY_FUNCTION_PASS("sjljehprepare", SjLjEHPreparePass, ())
133134
DUMMY_FUNCTION_PASS("stack-protector", StackProtectorPass, ())
134-
DUMMY_FUNCTION_PASS("wasmehprepare", WasmEHPass, ())
135135
#undef DUMMY_FUNCTION_PASS
136136

137137
#ifndef DUMMY_MODULE_PASS
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===--- llvm/CodeGen/WasmEHPrepare.h ---------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CODEGEN_WASMEHPREPARE_H
10+
#define LLVM_CODEGEN_WASMEHPREPARE_H
11+
12+
#include "llvm/IR/PassManager.h"
13+
14+
namespace llvm {
15+
16+
class WasmEHPreparePass : public PassInfoMixin<WasmEHPreparePass> {
17+
public:
18+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
19+
};
20+
21+
} // namespace llvm
22+
23+
#endif // LLVM_CODEGEN_WASMEHPREPARE_H

llvm/lib/CodeGen/WasmEHPrepare.cpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
//
7878
//===----------------------------------------------------------------------===//
7979

80+
#include "llvm/CodeGen/WasmEHPrepare.h"
8081
#include "llvm/CodeGen/MachineBasicBlock.h"
8182
#include "llvm/CodeGen/Passes.h"
8283
#include "llvm/CodeGen/WasmEHFuncInfo.h"
@@ -88,10 +89,12 @@
8889

8990
using namespace llvm;
9091

91-
#define DEBUG_TYPE "wasmehprepare"
92+
#define DEBUG_TYPE "wasm-eh-prepare"
9293

9394
namespace {
94-
class WasmEHPrepare : public FunctionPass {
95+
class WasmEHPrepareImpl {
96+
friend class WasmEHPrepare;
97+
9598
Type *LPadContextTy = nullptr; // type of 'struct _Unwind_LandingPadContext'
9699
GlobalVariable *LPadContextGV = nullptr; // __wasm_lpad_context
97100

@@ -113,19 +116,41 @@ class WasmEHPrepare : public FunctionPass {
113116
bool prepareEHPads(Function &F);
114117
void prepareEHPad(BasicBlock *BB, bool NeedPersonality, unsigned Index = 0);
115118

119+
public:
120+
WasmEHPrepareImpl() = default;
121+
WasmEHPrepareImpl(Type *LPadContextTy_) : LPadContextTy(LPadContextTy_) {}
122+
bool runOnFunction(Function &F);
123+
};
124+
125+
class WasmEHPrepare : public FunctionPass {
126+
WasmEHPrepareImpl P;
127+
116128
public:
117129
static char ID; // Pass identification, replacement for typeid
118130

119131
WasmEHPrepare() : FunctionPass(ID) {}
120132
bool doInitialization(Module &M) override;
121-
bool runOnFunction(Function &F) override;
133+
bool runOnFunction(Function &F) override { return P.runOnFunction(F); }
122134

123135
StringRef getPassName() const override {
124136
return "WebAssembly Exception handling preparation";
125137
}
126138
};
139+
127140
} // end anonymous namespace
128141

142+
PreservedAnalyses WasmEHPreparePass::run(Function &F,
143+
FunctionAnalysisManager &) {
144+
auto &Context = F.getContext();
145+
auto *I32Ty = Type::getInt32Ty(Context);
146+
auto *PtrTy = PointerType::get(Context, 0);
147+
auto *LPadContextTy =
148+
StructType::get(I32Ty /*lpad_index*/, PtrTy /*lsda*/, I32Ty /*selector*/);
149+
WasmEHPrepareImpl P(LPadContextTy);
150+
bool Changed = P.runOnFunction(F);
151+
return Changed ? PreservedAnalyses::none() : PreservedAnalyses ::all();
152+
}
153+
129154
char WasmEHPrepare::ID = 0;
130155
INITIALIZE_PASS_BEGIN(WasmEHPrepare, DEBUG_TYPE,
131156
"Prepare WebAssembly exceptions", false, false)
@@ -136,9 +161,9 @@ FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); }
136161

137162
bool WasmEHPrepare::doInitialization(Module &M) {
138163
IRBuilder<> IRB(M.getContext());
139-
LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index
140-
IRB.getPtrTy(), // lsda
141-
IRB.getInt32Ty() // selector
164+
P.LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index
165+
IRB.getPtrTy(), // lsda
166+
IRB.getInt32Ty() // selector
142167
);
143168
return false;
144169
}
@@ -157,14 +182,14 @@ static void eraseDeadBBsAndChildren(const Container &BBs) {
157182
}
158183
}
159184

160-
bool WasmEHPrepare::runOnFunction(Function &F) {
185+
bool WasmEHPrepareImpl::runOnFunction(Function &F) {
161186
bool Changed = false;
162187
Changed |= prepareThrows(F);
163188
Changed |= prepareEHPads(F);
164189
return Changed;
165190
}
166191

167-
bool WasmEHPrepare::prepareThrows(Function &F) {
192+
bool WasmEHPrepareImpl::prepareThrows(Function &F) {
168193
Module &M = *F.getParent();
169194
IRBuilder<> IRB(F.getContext());
170195
bool Changed = false;
@@ -192,7 +217,7 @@ bool WasmEHPrepare::prepareThrows(Function &F) {
192217
return Changed;
193218
}
194219

195-
bool WasmEHPrepare::prepareEHPads(Function &F) {
220+
bool WasmEHPrepareImpl::prepareEHPads(Function &F) {
196221
Module &M = *F.getParent();
197222
IRBuilder<> IRB(F.getContext());
198223

@@ -275,8 +300,8 @@ bool WasmEHPrepare::prepareEHPads(Function &F) {
275300

276301
// Prepare an EH pad for Wasm EH handling. If NeedPersonality is false, Index is
277302
// ignored.
278-
void WasmEHPrepare::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
279-
unsigned Index) {
303+
void WasmEHPrepareImpl::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
304+
unsigned Index) {
280305
assert(BB->isEHPad() && "BB is not an EHPad!");
281306
IRBuilder<> IRB(BB->getContext());
282307
IRB.SetInsertPoint(BB, BB->getFirstInsertionPt());

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include "llvm/CodeGen/HardwareLoops.h"
8080
#include "llvm/CodeGen/SafeStack.h"
8181
#include "llvm/CodeGen/TypePromotion.h"
82+
#include "llvm/CodeGen/WasmEHPrepare.h"
8283
#include "llvm/CodeGen/WinEHPrepare.h"
8384
#include "llvm/IR/DebugInfo.h"
8485
#include "llvm/IR/Dominators.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ FUNCTION_PASS("view-dom", DomViewer())
423423
FUNCTION_PASS("view-dom-only", DomOnlyViewer())
424424
FUNCTION_PASS("view-post-dom", PostDomViewer())
425425
FUNCTION_PASS("view-post-dom-only", PostDomOnlyViewer())
426+
FUNCTION_PASS("wasm-eh-prepare", WasmEHPreparePass())
426427
#undef FUNCTION_PASS
427428

428429
#ifndef FUNCTION_PASS_WITH_PARAMS

llvm/test/CodeGen/WebAssembly/wasmehprepare.ll renamed to llvm/test/CodeGen/WebAssembly/wasm-eh-prepare.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S | FileCheck %s
2-
; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S --mattr=+atomics,+bulk-memory | FileCheck %s
1+
; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasm-eh-prepare -S | FileCheck %s
2+
; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasm-eh-prepare -S --mattr=+atomics,+bulk-memory | FileCheck %s
3+
; RUN: opt < %s -passes='win-eh-prepare<demote-catchswitch-only>,wasm-eh-prepare' -S | FileCheck %s
4+
; RUN: opt < %s -passes='win-eh-prepare<demote-catchswitch-only>,wasm-eh-prepare' -S --mattr=+atomics,+bulk-memory | FileCheck %s
35

46
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
57
target triple = "wasm32-unknown-unknown"

0 commit comments

Comments
 (0)