[llvm] 3897623 - [WebAssembly] Port WebAssemblyCFGSortPass

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 22 20:48:15 PDT 2026


Author: Aiden Grossman
Date: 2026-07-22T20:48:11-07:00
New Revision: 389762329d50abc2d4997460ed2b4d7e7fc8ecfb

URL: https://github.com/llvm/llvm-project/commit/389762329d50abc2d4997460ed2b4d7e7fc8ecfb
DIFF: https://github.com/llvm/llvm-project/commit/389762329d50abc2d4997460ed2b4d7e7fc8ecfb.diff

LOG: [WebAssembly] Port WebAssemblyCFGSortPass

Standard NewPM pass porting.

Reviewers: dschuff, aheejin, sbc100

Pull Request: https://github.com/llvm/llvm-project/pull/210247

Added: 
    

Modified: 
    llvm/lib/Target/WebAssembly/WebAssembly.h
    llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
    llvm/lib/Target/WebAssembly/WebAssemblyCodeGenPassBuilder.cpp
    llvm/lib/Target/WebAssembly/WebAssemblyPassRegistry.def
    llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/WebAssembly/WebAssembly.h b/llvm/lib/Target/WebAssembly/WebAssembly.h
index 93328514db014..57a1cc0282690 100644
--- a/llvm/lib/Target/WebAssembly/WebAssembly.h
+++ b/llvm/lib/Target/WebAssembly/WebAssembly.h
@@ -237,7 +237,15 @@ class WebAssemblyLateEHPreparePass
 };
 
 FunctionPass *createWebAssemblyLateEHPrepareLegacyPass();
-FunctionPass *createWebAssemblyCFGSort();
+
+class WebAssemblyCFGSortPass
+    : public RequiredPassInfoMixin<WebAssemblyCFGSortPass> {
+public:
+  PreservedAnalyses run(MachineFunction &MF,
+                        MachineFunctionAnalysisManager &MFAM);
+};
+
+FunctionPass *createWebAssemblyCFGSortLegacyPass();
 FunctionPass *createWebAssemblyCFGStackify();
 FunctionPass *createWebAssemblyExplicitLocals();
 FunctionPass *createWebAssemblyLowerBrUnless();
@@ -254,7 +262,7 @@ void initializeWebAssemblyAddMissingPrototypesLegacyPass(PassRegistry &);
 void initializeWebAssemblyArgumentMoveLegacyPass(PassRegistry &);
 void initializeWebAssemblyAsmPrinterPass(PassRegistry &);
 void initializeWebAssemblyCleanCodeAfterTrapLegacyPass(PassRegistry &);
-void initializeWebAssemblyCFGSortPass(PassRegistry &);
+void initializeWebAssemblyCFGSortLegacyPass(PassRegistry &);
 void initializeWebAssemblyCFGStackifyPass(PassRegistry &);
 void initializeWebAssemblyDAGToDAGISelLegacyPass(PassRegistry &);
 void initializeWebAssemblyDebugFixupPass(PassRegistry &);

diff  --git a/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
index 4d92f8666e1ab..810190b956485 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
@@ -23,9 +23,12 @@
 #include "llvm/ADT/PriorityQueue.h"
 #include "llvm/CodeGen/MachineDominators.h"
 #include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
 #include "llvm/CodeGen/MachineLoopInfo.h"
+#include "llvm/CodeGen/MachinePassManager.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/CodeGen/Passes.h"
+#include "llvm/IR/Analysis.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace llvm;
@@ -44,7 +47,7 @@ static cl::opt<bool> WasmDisableEHPadSort(
 
 namespace {
 
-class WebAssemblyCFGSort final : public MachineFunctionPass {
+class WebAssemblyCFGSortLegacy final : public MachineFunctionPass {
   StringRef getPassName() const override { return "WebAssembly CFG Sort"; }
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -62,16 +65,16 @@ class WebAssemblyCFGSort final : public MachineFunctionPass {
 
 public:
   static char ID; // Pass identification, replacement for typeid
-  WebAssemblyCFGSort() : MachineFunctionPass(ID) {}
+  WebAssemblyCFGSortLegacy() : MachineFunctionPass(ID) {}
 };
 } // end anonymous namespace
 
-char WebAssemblyCFGSort::ID = 0;
-INITIALIZE_PASS(WebAssemblyCFGSort, DEBUG_TYPE,
+char WebAssemblyCFGSortLegacy::ID = 0;
+INITIALIZE_PASS(WebAssemblyCFGSortLegacy, DEBUG_TYPE,
                 "Reorders blocks in topological order", false, false)
 
-FunctionPass *llvm::createWebAssemblyCFGSort() {
-  return new WebAssemblyCFGSort();
+FunctionPass *llvm::createWebAssemblyCFGSortLegacyPass() {
+  return new WebAssemblyCFGSortLegacy();
 }
 
 static void maybeUpdateTerminator(MachineBasicBlock *MBB) {
@@ -379,14 +382,12 @@ static void sortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
 #endif
 }
 
-bool WebAssemblyCFGSort::runOnMachineFunction(MachineFunction &MF) {
+static bool sortCFG(MachineFunction &MF, MachineLoopInfo &MLI,
+                    WebAssemblyExceptionInfo &WEI, MachineDominatorTree &MDT) {
   LLVM_DEBUG(dbgs() << "********** CFG Sorting **********\n"
                        "********** Function: "
                     << MF.getName() << '\n');
 
-  const auto &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
-  const auto &WEI = getAnalysis<WebAssemblyExceptionInfoWrapperPass>().getWEI();
-  auto &MDT = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
   // Liveness is not tracked for VALUE_STACK physreg.
   MF.getRegInfo().invalidateLiveness();
 
@@ -395,3 +396,24 @@ bool WebAssemblyCFGSort::runOnMachineFunction(MachineFunction &MF) {
 
   return true;
 }
+
+bool WebAssemblyCFGSortLegacy::runOnMachineFunction(MachineFunction &MF) {
+  MachineLoopInfo &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
+  WebAssemblyExceptionInfo &WEI =
+      getAnalysis<WebAssemblyExceptionInfoWrapperPass>().getWEI();
+  MachineDominatorTree &MDT =
+      getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
+  return sortCFG(MF, MLI, WEI, MDT);
+}
+
+PreservedAnalyses
+WebAssemblyCFGSortPass::run(MachineFunction &MF,
+                            MachineFunctionAnalysisManager &MFAM) {
+  MachineLoopInfo &MLI = MFAM.getResult<MachineLoopAnalysis>(MF);
+  WebAssemblyExceptionInfo &WEI =
+      MFAM.getResult<WebAssemblyExceptionAnalysis>(MF);
+  MachineDominatorTree &MDT = MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
+  return sortCFG(MF, MLI, WEI, MDT) ? getMachineFunctionPassPreservedAnalyses()
+                                          .preserveSet<CFGAnalyses>()
+                                    : PreservedAnalyses::all();
+}

diff  --git a/llvm/lib/Target/WebAssembly/WebAssemblyCodeGenPassBuilder.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyCodeGenPassBuilder.cpp
index ba9caf288ee88..4cf950fa5e332 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCodeGenPassBuilder.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCodeGenPassBuilder.cpp
@@ -217,7 +217,7 @@ void WebAssemblyCodeGenPassBuilder::addPreEmitPass(
 
   // Sort the blocks of the CFG into topological order, a prerequisite for
   // BLOCK and LOOP markers.
-  // TODO(boomanaiden154): WebAssemblyCFGSort
+  addMachineFunctionPass(WebAssemblyCFGSortPass(), PMW);
 
   // Insert BLOCK and LOOP markers.
   // TODO(boomanaiden154): WebAssemblyCFGStackify

diff  --git a/llvm/lib/Target/WebAssembly/WebAssemblyPassRegistry.def b/llvm/lib/Target/WebAssembly/WebAssemblyPassRegistry.def
index 1b708652336f4..93e9d5180386c 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyPassRegistry.def
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyPassRegistry.def
@@ -39,6 +39,7 @@ MACHINE_FUNCTION_ANALYSIS("wasm-exception-info", WebAssemblyExceptionAnalysis())
 #define MACHINE_FUNCTION_PASS(NAME, CREATE_PASS)
 #endif
 MACHINE_FUNCTION_PASS("wasm-argument-move", WebAssemblyArgumentMovePass())
+MACHINE_FUNCTION_PASS("wasm-cfg-sort", WebAssemblyCFGSortPass())
 MACHINE_FUNCTION_PASS("wasm-clean-code-after-trap",
                       WebAssemblyCleanCodeAfterTrapPass())
 MACHINE_FUNCTION_PASS("wasm-isel", WebAssemblyISelDAGToDAGPass(*this, getOptLevel()))

diff  --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
index ef7e6ce290a7e..ef2add03ead16 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
@@ -111,7 +111,7 @@ LLVMInitializeWebAssemblyTarget() {
   initializeWebAssemblyFixIrreducibleControlFlowLegacyPass(PR);
   initializeWebAssemblyLateEHPrepareLegacyPass(PR);
   initializeWebAssemblyExceptionInfoWrapperPassPass(PR);
-  initializeWebAssemblyCFGSortPass(PR);
+  initializeWebAssemblyCFGSortLegacyPass(PR);
   initializeWebAssemblyCFGStackifyPass(PR);
   initializeWebAssemblyExplicitLocalsPass(PR);
   initializeWebAssemblyLowerBrUnlessPass(PR);
@@ -483,7 +483,7 @@ void WebAssemblyPassConfig::addPreEmitPass() {
 
   // Sort the blocks of the CFG into topological order, a prerequisite for
   // BLOCK and LOOP markers.
-  addPass(createWebAssemblyCFGSort());
+  addPass(createWebAssemblyCFGSortLegacyPass());
 
   // Insert BLOCK and LOOP markers.
   addPass(createWebAssemblyCFGStackify());


        


More information about the llvm-commits mailing list