[llvm] [CodeGen] Port WasmEHPrepare to new pass manager (PR #74435)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 5 03:48:42 PST 2023
https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/74435
>From 5f5bc99a5e2002de0cdfbf11baa4b3b1a095e5b7 Mon Sep 17 00:00:00 2001
From: PaperChalice <29250197+paperchalice at users.noreply.github.com>
Date: Sun, 3 Dec 2023 20:52:45 +0800
Subject: [PATCH] [CodeGen] Port WasmEHPrepare to new pass manager
---
.../include/llvm/CodeGen/CodeGenPassBuilder.h | 3 +-
.../llvm/CodeGen/MachinePassRegistry.def | 2 +-
llvm/include/llvm/CodeGen/WasmEHPrepare.h | 23 +++++++++
llvm/lib/CodeGen/WasmEHPrepare.cpp | 47 ++++++++++++++-----
llvm/lib/Passes/PassBuilder.cpp | 1 +
llvm/lib/Passes/PassRegistry.def | 1 +
.../{wasmehprepare.ll => wasm-eh-prepare.ll} | 6 ++-
7 files changed, 68 insertions(+), 15 deletions(-)
create mode 100644 llvm/include/llvm/CodeGen/WasmEHPrepare.h
rename llvm/test/CodeGen/WebAssembly/{wasmehprepare.ll => wasm-eh-prepare.ll} (95%)
diff --git a/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h b/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
index a8ab670ad77be..076719abd0356 100644
--- a/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
@@ -29,6 +29,7 @@
#include "llvm/CodeGen/ReplaceWithVeclib.h"
#include "llvm/CodeGen/SafeStack.h"
#include "llvm/CodeGen/UnreachableBlockElim.h"
+#include "llvm/CodeGen/WasmEHPrepare.h"
#include "llvm/CodeGen/WinEHPrepare.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Verifier.h"
@@ -691,7 +692,7 @@ void CodeGenPassBuilder<Derived>::addPassesToHandleExceptions(
// funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
// should remove PHIs there.
addPass(WinEHPreparePass(/*DemoteCatchSwitchPHIOnly=*/false));
- addPass(WasmEHPass());
+ addPass(WasmEHPreparePass());
break;
case ExceptionHandling::None:
addPass(LowerInvokePass());
diff --git a/llvm/include/llvm/CodeGen/MachinePassRegistry.def b/llvm/include/llvm/CodeGen/MachinePassRegistry.def
index 4c6706cf42532..1e9e5838841b2 100644
--- a/llvm/include/llvm/CodeGen/MachinePassRegistry.def
+++ b/llvm/include/llvm/CodeGen/MachinePassRegistry.def
@@ -54,6 +54,7 @@ FUNCTION_PASS("scalarize-masked-mem-intrin", ScalarizeMaskedMemIntrinPass, ())
FUNCTION_PASS("tlshoist", TLSVariableHoistPass, ())
FUNCTION_PASS("unreachableblockelim", UnreachableBlockElimPass, ())
FUNCTION_PASS("verify", VerifierPass, ())
+FUNCTION_PASS("wasm-eh-prepare", WasmEHPreparePass, ())
FUNCTION_PASS("win-eh-prepare", WinEHPreparePass, ())
#undef FUNCTION_PASS
@@ -131,7 +132,6 @@ DUMMY_FUNCTION_PASS("select-optimize", SelectOptimizePass, ())
DUMMY_FUNCTION_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass, ())
DUMMY_FUNCTION_PASS("sjljehprepare", SjLjEHPreparePass, ())
DUMMY_FUNCTION_PASS("stack-protector", StackProtectorPass, ())
-DUMMY_FUNCTION_PASS("wasmehprepare", WasmEHPass, ())
#undef DUMMY_FUNCTION_PASS
#ifndef DUMMY_MODULE_PASS
diff --git a/llvm/include/llvm/CodeGen/WasmEHPrepare.h b/llvm/include/llvm/CodeGen/WasmEHPrepare.h
new file mode 100644
index 0000000000000..8b3c07573a0d3
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/WasmEHPrepare.h
@@ -0,0 +1,23 @@
+//===--- llvm/CodeGen/WasmEHPrepare.h ---------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_WASMEHPREPARE_H
+#define LLVM_CODEGEN_WASMEHPREPARE_H
+
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+
+class WasmEHPreparePass : public PassInfoMixin<WasmEHPreparePass> {
+public:
+ PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_WASMEHPREPARE_H
diff --git a/llvm/lib/CodeGen/WasmEHPrepare.cpp b/llvm/lib/CodeGen/WasmEHPrepare.cpp
index d99b10f4207f9..55d71128e7821 100644
--- a/llvm/lib/CodeGen/WasmEHPrepare.cpp
+++ b/llvm/lib/CodeGen/WasmEHPrepare.cpp
@@ -77,6 +77,7 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/CodeGen/WasmEHPrepare.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/WasmEHFuncInfo.h"
@@ -88,10 +89,12 @@
using namespace llvm;
-#define DEBUG_TYPE "wasmehprepare"
+#define DEBUG_TYPE "wasm-eh-prepare"
namespace {
-class WasmEHPrepare : public FunctionPass {
+class WasmEHPrepareImpl {
+ friend class WasmEHPrepare;
+
Type *LPadContextTy = nullptr; // type of 'struct _Unwind_LandingPadContext'
GlobalVariable *LPadContextGV = nullptr; // __wasm_lpad_context
@@ -113,19 +116,41 @@ class WasmEHPrepare : public FunctionPass {
bool prepareEHPads(Function &F);
void prepareEHPad(BasicBlock *BB, bool NeedPersonality, unsigned Index = 0);
+public:
+ WasmEHPrepareImpl() = default;
+ WasmEHPrepareImpl(Type *LPadContextTy_) : LPadContextTy(LPadContextTy_) {}
+ bool runOnFunction(Function &F);
+};
+
+class WasmEHPrepare : public FunctionPass {
+ WasmEHPrepareImpl P;
+
public:
static char ID; // Pass identification, replacement for typeid
WasmEHPrepare() : FunctionPass(ID) {}
bool doInitialization(Module &M) override;
- bool runOnFunction(Function &F) override;
+ bool runOnFunction(Function &F) override { return P.runOnFunction(F); }
StringRef getPassName() const override {
return "WebAssembly Exception handling preparation";
}
};
+
} // end anonymous namespace
+PreservedAnalyses WasmEHPreparePass::run(Function &F,
+ FunctionAnalysisManager &) {
+ auto &Context = F.getContext();
+ auto *I32Ty = Type::getInt32Ty(Context);
+ auto *PtrTy = PointerType::get(Context, F.getAddressSpace());
+ auto *LPadContextTy =
+ StructType::get(I32Ty /*lpad_index*/, PtrTy /*lsda*/, I32Ty /*selector*/);
+ WasmEHPrepareImpl P(LPadContextTy);
+ bool Changed = P.runOnFunction(F);
+ return Changed ? PreservedAnalyses::none() : PreservedAnalyses ::all();
+}
+
char WasmEHPrepare::ID = 0;
INITIALIZE_PASS_BEGIN(WasmEHPrepare, DEBUG_TYPE,
"Prepare WebAssembly exceptions", false, false)
@@ -136,9 +161,9 @@ FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); }
bool WasmEHPrepare::doInitialization(Module &M) {
IRBuilder<> IRB(M.getContext());
- LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index
- IRB.getPtrTy(), // lsda
- IRB.getInt32Ty() // selector
+ P.LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index
+ IRB.getPtrTy(), // lsda
+ IRB.getInt32Ty() // selector
);
return false;
}
@@ -157,14 +182,14 @@ static void eraseDeadBBsAndChildren(const Container &BBs) {
}
}
-bool WasmEHPrepare::runOnFunction(Function &F) {
+bool WasmEHPrepareImpl::runOnFunction(Function &F) {
bool Changed = false;
Changed |= prepareThrows(F);
Changed |= prepareEHPads(F);
return Changed;
}
-bool WasmEHPrepare::prepareThrows(Function &F) {
+bool WasmEHPrepareImpl::prepareThrows(Function &F) {
Module &M = *F.getParent();
IRBuilder<> IRB(F.getContext());
bool Changed = false;
@@ -192,7 +217,7 @@ bool WasmEHPrepare::prepareThrows(Function &F) {
return Changed;
}
-bool WasmEHPrepare::prepareEHPads(Function &F) {
+bool WasmEHPrepareImpl::prepareEHPads(Function &F) {
Module &M = *F.getParent();
IRBuilder<> IRB(F.getContext());
@@ -275,8 +300,8 @@ bool WasmEHPrepare::prepareEHPads(Function &F) {
// Prepare an EH pad for Wasm EH handling. If NeedPersonality is false, Index is
// ignored.
-void WasmEHPrepare::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
- unsigned Index) {
+void WasmEHPrepareImpl::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
+ unsigned Index) {
assert(BB->isEHPad() && "BB is not an EHPad!");
IRBuilder<> IRB(BB->getContext());
IRB.SetInsertPoint(BB, BB->getFirstInsertionPt());
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 729b050c5d1d3..a5f9b5424358e 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -79,6 +79,7 @@
#include "llvm/CodeGen/HardwareLoops.h"
#include "llvm/CodeGen/SafeStack.h"
#include "llvm/CodeGen/TypePromotion.h"
+#include "llvm/CodeGen/WasmEHPrepare.h"
#include "llvm/CodeGen/WinEHPrepare.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/Dominators.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 37c0d6c29b429..7462704ec2df8 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -423,6 +423,7 @@ FUNCTION_PASS("view-dom", DomViewer())
FUNCTION_PASS("view-dom-only", DomOnlyViewer())
FUNCTION_PASS("view-post-dom", PostDomViewer())
FUNCTION_PASS("view-post-dom-only", PostDomOnlyViewer())
+FUNCTION_PASS("wasm-eh-prepare", WasmEHPreparePass())
#undef FUNCTION_PASS
#ifndef FUNCTION_PASS_WITH_PARAMS
diff --git a/llvm/test/CodeGen/WebAssembly/wasmehprepare.ll b/llvm/test/CodeGen/WebAssembly/wasm-eh-prepare.ll
similarity index 95%
rename from llvm/test/CodeGen/WebAssembly/wasmehprepare.ll
rename to llvm/test/CodeGen/WebAssembly/wasm-eh-prepare.ll
index a418eb4ec890e..e3de251017384 100644
--- a/llvm/test/CodeGen/WebAssembly/wasmehprepare.ll
+++ b/llvm/test/CodeGen/WebAssembly/wasm-eh-prepare.ll
@@ -1,5 +1,7 @@
-; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S | FileCheck %s
-; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S --mattr=+atomics,+bulk-memory | FileCheck %s
+; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasm-eh-prepare -S | FileCheck %s
+; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasm-eh-prepare -S --mattr=+atomics,+bulk-memory | FileCheck %s
+; RUN: opt < %s -passes='win-eh-prepare<demote-catchswitch-only>,wasm-eh-prepare' -S | FileCheck %s
+; RUN: opt < %s -passes='win-eh-prepare<demote-catchswitch-only>,wasm-eh-prepare' -S --mattr=+atomics,+bulk-memory | FileCheck %s
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
target triple = "wasm32-unknown-unknown"
More information about the llvm-commits
mailing list