[llvm] 0e3d17a - [WebAssembly] Port WebAssemblyReplacePhysRegsPass

via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 18 16:22:37 PDT 2026


Author: Aiden Grossman
Date: 2026-07-18T16:22:33-07:00
New Revision: 0e3d17a080646825df5cdff6997c633b6292b6ff

URL: https://github.com/llvm/llvm-project/commit/0e3d17a080646825df5cdff6997c633b6292b6ff
DIFF: https://github.com/llvm/llvm-project/commit/0e3d17a080646825df5cdff6997c633b6292b6ff.diff

LOG: [WebAssembly] Port WebAssemblyReplacePhysRegsPass

Standard NewPM pass porting.

Reviewers: dschuff, aheejin, sbc100

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/WebAssembly/WebAssembly.h b/llvm/lib/Target/WebAssembly/WebAssembly.h
index 098058ecd0a45..c8a0c8dab5bb2 100644
--- a/llvm/lib/Target/WebAssembly/WebAssembly.h
+++ b/llvm/lib/Target/WebAssembly/WebAssembly.h
@@ -152,7 +152,14 @@ class WebAssemblyCleanCodeAfterTrapPass
 FunctionPass *createWebAssemblyCleanCodeAfterTrapLegacyPass();
 
 // Late passes.
-FunctionPass *createWebAssemblyReplacePhysRegs();
+class WebAssemblyReplacePhysRegsPass
+    : public RequiredPassInfoMixin<WebAssemblyReplacePhysRegsPass> {
+public:
+  PreservedAnalyses run(MachineFunction &MF,
+                        MachineFunctionAnalysisManager &MFAM);
+};
+
+FunctionPass *createWebAssemblyReplacePhysRegsLegacyPass();
 
 class WebAssemblyNullifyDebugValueListsPass
     : public RequiredPassInfoMixin<WebAssemblyNullifyDebugValueListsPass> {
@@ -230,7 +237,7 @@ void initializeWebAssemblyPeepholePass(PassRegistry &);
 void initializeWebAssemblyRegColoringPass(PassRegistry &);
 void initializeWebAssemblyRegNumberingPass(PassRegistry &);
 void initializeWebAssemblyRegStackifyPass(PassRegistry &);
-void initializeWebAssemblyReplacePhysRegsPass(PassRegistry &);
+void initializeWebAssemblyReplacePhysRegsLegacyPass(PassRegistry &);
 void initializeWebAssemblySetP2AlignOperandsLegacyPass(PassRegistry &);
 void initializeWebAssemblyCoalesceFeaturesAndStripAtomicsLegacyPass(
     PassRegistry &);

diff  --git a/llvm/lib/Target/WebAssembly/WebAssemblyCodeGenPassBuilder.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyCodeGenPassBuilder.cpp
index fd9a76e5e2513..f09ba39d7913a 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCodeGenPassBuilder.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCodeGenPassBuilder.cpp
@@ -189,7 +189,7 @@ void WebAssemblyCodeGenPassBuilder::addPreEmitPass(
   // Now that we have a prologue and epilogue and all frame indices are
   // rewritten, eliminate SP and FP. This allows them to be stackified,
   // colored, and numbered with the rest of the registers.
-  // TODO(boomanaiden154): WebAssemblyReplacePhysRegs
+  addMachineFunctionPass(WebAssemblyReplacePhysRegsPass(), PMW);
 
   // Preparations and optimizations related to register stackification.
   if (getOptLevel() != CodeGenOptLevel::None) {

diff  --git a/llvm/lib/Target/WebAssembly/WebAssemblyPassRegistry.def b/llvm/lib/Target/WebAssembly/WebAssemblyPassRegistry.def
index 7b66b6429e238..4df8117073d82 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyPassRegistry.def
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyPassRegistry.def
@@ -43,6 +43,7 @@ MACHINE_FUNCTION_PASS("wasm-fix-irreducible-control-flow",
 MACHINE_FUNCTION_PASS("wasm-late-eh-prepare", WebAssemblyLateEHPreparePass())
 MACHINE_FUNCTION_PASS("wasm-nullify-dbg-value-lists",
                       WebAssemblyNullifyDebugValueListsPass())
+MACHINE_FUNCTION_PASS("wasm-replace-phys-regs", WebAssemblyReplacePhysRegsPass())
 MACHINE_FUNCTION_PASS("wasm-set-p2align-operands",
                       WebAssemblySetP2AlignOperandsPass())
 #undef MACHINE_FUNCTION_PASS

diff  --git a/llvm/lib/Target/WebAssembly/WebAssemblyReplacePhysRegs.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyReplacePhysRegs.cpp
index cb152f500436a..f2d3fc12b6af9 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyReplacePhysRegs.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyReplacePhysRegs.cpp
@@ -22,9 +22,12 @@
 #include "WebAssembly.h"
 #include "WebAssemblyMachineFunctionInfo.h"
 #include "WebAssemblySubtarget.h"
+#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
 #include "llvm/CodeGen/MachineFunctionPass.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;
@@ -32,10 +35,10 @@ using namespace llvm;
 #define DEBUG_TYPE "wasm-replace-phys-regs"
 
 namespace {
-class WebAssemblyReplacePhysRegs final : public MachineFunctionPass {
+class WebAssemblyReplacePhysRegsLegacy final : public MachineFunctionPass {
 public:
   static char ID; // Pass identification, replacement for typeid
-  WebAssemblyReplacePhysRegs() : MachineFunctionPass(ID) {}
+  WebAssemblyReplacePhysRegsLegacy() : MachineFunctionPass(ID) {}
 
 private:
   StringRef getPassName() const override {
@@ -51,16 +54,16 @@ class WebAssemblyReplacePhysRegs final : public MachineFunctionPass {
 };
 } // end anonymous namespace
 
-char WebAssemblyReplacePhysRegs::ID = 0;
-INITIALIZE_PASS(WebAssemblyReplacePhysRegs, DEBUG_TYPE,
+char WebAssemblyReplacePhysRegsLegacy::ID = 0;
+INITIALIZE_PASS(WebAssemblyReplacePhysRegsLegacy, DEBUG_TYPE,
                 "Replace physical registers with virtual registers", false,
                 false)
 
-FunctionPass *llvm::createWebAssemblyReplacePhysRegs() {
-  return new WebAssemblyReplacePhysRegs();
+FunctionPass *llvm::createWebAssemblyReplacePhysRegsLegacyPass() {
+  return new WebAssemblyReplacePhysRegsLegacy();
 }
 
-bool WebAssemblyReplacePhysRegs::runOnMachineFunction(MachineFunction &MF) {
+static bool replacePhysRegs(MachineFunction &MF) {
   LLVM_DEBUG({
     dbgs() << "********** Replace Physical Registers **********\n"
            << "********** Function: " << MF.getName() << '\n';
@@ -70,9 +73,6 @@ bool WebAssemblyReplacePhysRegs::runOnMachineFunction(MachineFunction &MF) {
   auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
   bool Changed = false;
 
-  assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
-         "LiveIntervals shouldn't be active yet!");
-
   for (unsigned PReg = WebAssembly::NoRegister + 1;
        PReg < WebAssembly::NUM_TARGET_REGS; ++PReg) {
     // Skip fake registers that are never used explicitly.
@@ -105,3 +105,19 @@ bool WebAssemblyReplacePhysRegs::runOnMachineFunction(MachineFunction &MF) {
 
   return Changed;
 }
+
+bool WebAssemblyReplacePhysRegsLegacy::runOnMachineFunction(
+    MachineFunction &MF) {
+  assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
+         "LiveIntervals shouldn't be active yet!");
+
+  return replacePhysRegs(MF);
+}
+
+PreservedAnalyses
+WebAssemblyReplacePhysRegsPass::run(MachineFunction &MF,
+                                    MachineFunctionAnalysisManager &MFAM) {
+  return replacePhysRegs(MF) ? getMachineFunctionPassPreservedAnalyses()
+                                   .preserveSet<CFGAnalyses>()
+                             : PreservedAnalyses::all();
+}

diff  --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
index 31169fa1f0b98..c1f53bba36a70 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
@@ -102,7 +102,7 @@ LLVMInitializeWebAssemblyTarget() {
   initializeWebAssemblyArgumentMoveLegacyPass(PR);
   initializeWebAssemblyAsmPrinterPass(PR);
   initializeWebAssemblySetP2AlignOperandsLegacyPass(PR);
-  initializeWebAssemblyReplacePhysRegsPass(PR);
+  initializeWebAssemblyReplacePhysRegsLegacyPass(PR);
   initializeWebAssemblyOptimizeLiveIntervalsPass(PR);
   initializeWebAssemblyMemIntrinsicResultsPass(PR);
   initializeWebAssemblyRegStackifyPass(PR);
@@ -456,7 +456,7 @@ void WebAssemblyPassConfig::addPreEmitPass() {
   // Now that we have a prologue and epilogue and all frame indices are
   // rewritten, eliminate SP and FP. This allows them to be stackified,
   // colored, and numbered with the rest of the registers.
-  addPass(createWebAssemblyReplacePhysRegs());
+  addPass(createWebAssemblyReplacePhysRegsLegacyPass());
 
   // Preparations and optimizations related to register stackification.
   if (getOptLevel() != CodeGenOptLevel::None) {


        


More information about the llvm-commits mailing list