Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeGen] Port WasmEHPrepare to new pass manager #74435

Merged
merged 1 commit into from
Dec 6, 2023

Conversation

paperchalice
Copy link
Contributor

Port WasmEHPrepare to new pass manager, also rename wasmehprepare to wasm-eh-prepare.

@llvmbot
Copy link
Collaborator

llvmbot commented Dec 5, 2023

@llvm/pr-subscribers-backend-webassembly

Author: None (paperchalice)

Changes

Port WasmEHPrepare to new pass manager, also rename wasmehprepare to wasm-eh-prepare.


Full diff: https://github.com/llvm/llvm-project/pull/74435.diff

7 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/CodeGenPassBuilder.h (+2-1)
  • (modified) llvm/include/llvm/CodeGen/MachinePassRegistry.def (+1-1)
  • (added) llvm/include/llvm/CodeGen/WasmEHPrepare.h (+23)
  • (modified) llvm/lib/CodeGen/WasmEHPrepare.cpp (+36-11)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (modified) llvm/lib/Passes/PassRegistry.def (+1)
  • (renamed) llvm/test/CodeGen/WebAssembly/wasm-eh-prepare.ll (+4-2)
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"

@paperchalice paperchalice force-pushed the NPM/CodeGen/wasmehprepare branch 2 times, most recently from 8020c0e to 5f5bc99 Compare December 5, 2023 11:48
Copy link
Member

@dschuff dschuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @aheejin

} // end anonymous namespace

PreservedAnalyses WasmEHPreparePass::run(Function &F,
FunctionAnalysisManager &) {
auto &Context = F.getContext();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super familiar with the new pass manager, but this new code seems to be the same as what's in WasmEHPrepare::doInitialization() below. Are they both necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no doInitialization/doFinalization concept in new pass manager. I add the same code here to ensure the same behavior for new and legacy pass.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should delete the current doInitialization. But then where do we do stuff that was only possible in doInitialization before, like things listed here? https://llvm.org/docs/WritingAnLLVMPass.html#the-doinitialization-module-method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an analysis pass to fetch information about module may be an option, or convert it to module pass. If it is a machine function pass, the machine function pass manager provides ability to run doInitialization/doFinalization.


namespace {
class WasmEHPrepare : public FunctionPass {
class WasmEHPrepareImpl {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the new pass manager. Does this additional Impl class necessary for the new pass manager? If there is a guide to port passes to the new pass manager, can you point it out? (I searched a little but didn't find much other than https://llvm.org/docs/NewPassManager.html)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, there is no particular porting guide, but discourse has some topics about it. If we can remove the legacy pass entirely in future, we can merge WasmEHPrepareImpl and WasmEHPreparePass.

Copy link
Member

@aheejin aheejin Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What makes the current WasmEHPrepare class not sharable between the legacy and the new PM and why do we need the additional Impl class? Isn't the Impl class an implementation detail within WasmEHPrepare? It looks the interface of WasmEHPrepare hasn't changed.

Also I'd appreciate if you can point to some related discussions in discourse!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK, I think I now get it. This PR makes WasmEHPrepare supported in both the old and new PM. I thought we are removing the old PM support, which was the source of the confusion.. Thanks for the info.

@paperchalice
Copy link
Contributor Author

CC @arsenm @aeubanks @boomanaiden154.

FunctionAnalysisManager &) {
auto &Context = F.getContext();
auto *I32Ty = Type::getInt32Ty(Context);
auto *PtrTy = PointerType::get(Context, F.getAddressSpace());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're being precise about the address space, it should probably come from LSDA instead of the calling function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked existing code, the address space is 0 here, will update later.

Copy link
Member

@aheejin aheejin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@aheejin aheejin merged commit 5baf66f into llvm:main Dec 6, 2023
3 of 4 checks passed
@paperchalice paperchalice deleted the NPM/CodeGen/wasmehprepare branch December 7, 2023 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants