[llvm] [CodeGen] Port mir-strip-debug to new pass manager (PR #190738)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 15 00:16:02 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-debuginfo

Author: juan.vazquez (juanvazquez)

<details>
<summary>Changes</summary>



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


7 Files Affected:

- (added) llvm/include/llvm/CodeGen/MachineStripDebug.h (+32) 
- (modified) llvm/include/llvm/CodeGen/Passes.h (+2-1) 
- (modified) llvm/include/llvm/Passes/MachinePassRegistry.def (+1-1) 
- (modified) llvm/lib/CodeGen/MachineStripDebug.cpp (+76-44) 
- (modified) llvm/lib/CodeGen/TargetPassConfig.cpp (+1-1) 
- (modified) llvm/lib/Passes/PassBuilder.cpp (+1) 
- (modified) llvm/lib/Passes/PassRegistry.def (+1) 


``````````diff
diff --git a/llvm/include/llvm/CodeGen/MachineStripDebug.h b/llvm/include/llvm/CodeGen/MachineStripDebug.h
new file mode 100644
index 0000000000000..0f5ccb1338ac3
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/MachineStripDebug.h
@@ -0,0 +1,32 @@
+//===- MachineStripDebug.h - Strip debug info -----------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This removes debug info from everything. It can be used to ensure tests can
+// be debugified without affecting the output MIR.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_MACHINESTRIPDEBUG_H
+#define LLVM_CODEGEN_MACHINESTRIPDEBUG_H
+
+#include "llvm/IR/Analysis.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/Support/Compiler.h"
+
+namespace llvm {
+
+class StripDebugMachineModulePass
+    : public PassInfoMixin<StripDebugMachineModulePass> {
+public:
+  LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_MACHINESTRIPDEBUG_H
diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index bd5844f715de5..44b702e3676b9 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -600,7 +600,8 @@ LLVM_ABI ModulePass *createDebugifyMachineModulePass();
 /// If OnlyDebugified is true then it will only strip debug info if it was
 /// added by a Debugify pass. The module will be left unchanged if the debug
 /// info was generated by another source such as clang.
-LLVM_ABI ModulePass *createStripDebugMachineModulePass(bool OnlyDebugified);
+LLVM_ABI ModulePass *
+createStripDebugMachineModuleLegacyPass(bool OnlyDebugified);
 
 /// Creates MIR Check Debug pass. \see MachineCheckDebugify.cpp
 LLVM_ABI ModulePass *createCheckDebugMachineModulePass();
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 98fa48604e7a2..ec1209fc5dc18 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -27,6 +27,7 @@ MODULE_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
 MODULE_PASS("global-merge", GlobalMergePass(TM, GlobalMergeOptions()))
 MODULE_PASS("jmc-instrumenter", JMCInstrumenterPass())
 MODULE_PASS("lower-emutls", LowerEmuTLSPass())
+MODULE_PASS("mir-strip-debug", StripDebugMachineModulePass())
 MODULE_PASS("pre-isel-intrinsic-lowering", PreISelIntrinsicLoweringPass())
 MODULE_PASS("print<regusage>", PhysicalRegisterUsageInfoPrinterPass(errs()))
 MODULE_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass())
@@ -261,7 +262,6 @@ DUMMY_MACHINE_MODULE_PASS("static-data-annotator", StaticDataAnnotator)
 DUMMY_MACHINE_MODULE_PASS("pseudo-probe-inserter", PseudoProbeInserterPass)
 DUMMY_MACHINE_MODULE_PASS("mir-debugify", DebugifyMachineModule)
 DUMMY_MACHINE_MODULE_PASS("mir-check-debugify", CheckDebugMachineModulePass)
-DUMMY_MACHINE_MODULE_PASS("mir-strip-debug", StripDebugMachineModulePass)
 #undef DUMMY_MACHINE_MODULE_PASS
 
 #ifndef DUMMY_MACHINE_FUNCTION_PASS
diff --git a/llvm/lib/CodeGen/MachineStripDebug.cpp b/llvm/lib/CodeGen/MachineStripDebug.cpp
index d54fe023a4a7e..866f02c9fc7bd 100644
--- a/llvm/lib/CodeGen/MachineStripDebug.cpp
+++ b/llvm/lib/CodeGen/MachineStripDebug.cpp
@@ -10,10 +10,15 @@
 /// tests can be debugified without affecting the output MIR.
 //===----------------------------------------------------------------------===//
 
+#include "llvm/CodeGen/MachineStripDebug.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineFunctionAnalysis.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/CodeGen/Passes.h"
+#include "llvm/IR/Analysis.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Transforms/Utils/Debugify.h"
@@ -23,60 +28,69 @@
 using namespace llvm;
 
 namespace {
-cl::opt<bool>
-    OnlyDebugifiedDefault("mir-strip-debugify-only",
-                          cl::desc("Should mir-strip-debug only strip debug "
-                                   "info from debugified modules by default"),
-                          cl::init(true));
 
-struct StripDebugMachineModule : public ModulePass {
-  bool runOnModule(Module &M) override {
-    if (OnlyDebugified) {
-      NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
-      if (!DebugifyMD) {
-        LLVM_DEBUG(dbgs() << "Not stripping debug info"
-                             " (debugify metadata not found)?\n");
-        return false;
-      }
+bool stripDebugMachineModuleImpl(
+    Module &M, bool OnlyDebugified,
+    llvm::function_ref<MachineFunction *(Function &)> GetMF) {
+  if (OnlyDebugified) {
+    NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
+    if (!DebugifyMD) {
+      LLVM_DEBUG(dbgs() << "Not stripping debug info"
+                           " (debugify metadata not found)?\n");
+      return false;
     }
+  }
 
-    MachineModuleInfo &MMI =
-        getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
-
-    bool Changed = false;
-    for (Function &F : M.functions()) {
-      MachineFunction *MaybeMF = MMI.getMachineFunction(F);
-      if (!MaybeMF)
-        continue;
-      MachineFunction &MF = *MaybeMF;
-      for (MachineBasicBlock &MBB : MF) {
-        for (MachineInstr &MI : llvm::make_early_inc_range(MBB.instrs())) {
-          if (MI.isDebugInstr()) {
-            // FIXME: We should remove all of them. However, AArch64 emits an
-            //        invalid `DBG_VALUE $lr` with only one operand instead of
-            //        the usual three and has a test that depends on it's
-            //        preservation. Preserve it for now.
-            if (MI.getNumOperands() > 1) {
-              LLVM_DEBUG(dbgs() << "Removing debug instruction " << MI);
-              MBB.erase_instr(&MI);
-              Changed |= true;
-              continue;
-            }
-          }
-          if (MI.getDebugLoc()) {
-            LLVM_DEBUG(dbgs() << "Removing location " << MI);
-            MI.setDebugLoc(DebugLoc());
+  bool Changed = false;
+  for (Function &F : M.functions()) {
+    MachineFunction *MaybeMF = GetMF(F);
+    if (!MaybeMF)
+      continue;
+    MachineFunction &MF = *MaybeMF;
+    for (MachineBasicBlock &MBB : MF) {
+      for (MachineInstr &MI : llvm::make_early_inc_range(MBB.instrs())) {
+        if (MI.isDebugInstr()) {
+          // FIXME: We should remove all of them. However, AArch64 emits an
+          //        invalid `DBG_VALUE $lr` with only one operand instead of
+          //        the usual three and has a test that depends on it's
+          //        preservation. Preserve it for now.
+          if (MI.getNumOperands() > 1) {
+            LLVM_DEBUG(dbgs() << "Removing debug instruction " << MI);
+            MBB.erase_instr(&MI);
             Changed |= true;
             continue;
           }
-          LLVM_DEBUG(dbgs() << "Keeping " << MI);
         }
+        if (MI.getDebugLoc()) {
+          LLVM_DEBUG(dbgs() << "Removing location " << MI);
+          MI.setDebugLoc(DebugLoc());
+          Changed |= true;
+          continue;
+        }
+        LLVM_DEBUG(dbgs() << "Keeping " << MI);
       }
     }
+  }
 
-    Changed |= stripDebugifyMetadata(M);
+  Changed |= stripDebugifyMetadata(M);
 
-    return Changed;
+  return Changed;
+}
+
+cl::opt<bool>
+    OnlyDebugifiedDefault("mir-strip-debugify-only",
+                          cl::desc("Should mir-strip-debug only strip debug "
+                                   "info from debugified modules by default"),
+                          cl::init(true));
+
+struct StripDebugMachineModule : public ModulePass {
+  bool runOnModule(Module &M) override {
+    MachineModuleInfo &MMI =
+        getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
+    return stripDebugMachineModuleImpl(
+        M, OnlyDebugified, [&MMI](Function &F) -> MachineFunction * {
+          return MMI.getMachineFunction(F);
+        });
   }
 
   StripDebugMachineModule() : StripDebugMachineModule(OnlyDebugifiedDefault) {}
@@ -103,6 +117,24 @@ INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE,
 INITIALIZE_PASS_END(StripDebugMachineModule, DEBUG_TYPE,
                     "Machine Strip Debug Module", false, false)
 
-ModulePass *llvm::createStripDebugMachineModulePass(bool OnlyDebugified) {
+ModulePass *llvm::createStripDebugMachineModuleLegacyPass(bool OnlyDebugified) {
   return new StripDebugMachineModule(OnlyDebugified);
 }
+
+PreservedAnalyses StripDebugMachineModulePass::run(Module &M,
+                                                   ModuleAnalysisManager &AM) {
+  FunctionAnalysisManager &FAM =
+      AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
+  bool Changed = stripDebugMachineModuleImpl(
+      M, OnlyDebugifiedDefault, [&FAM](Function &F) -> MachineFunction * {
+        return &FAM.getResult<MachineFunctionAnalysis>(F).getMF();
+      });
+  if (!Changed)
+    return PreservedAnalyses::all();
+
+  PreservedAnalyses PA;
+  PA.preserve<MachineModuleAnalysis>();
+  PA.preserve<FunctionAnalysisManagerModuleProxy>();
+  PA.preserveSet<CFGAnalyses>();
+  return PA;
+}
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index 4aa99340ef1d4..8e5b0f0d4221e 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -810,7 +810,7 @@ void TargetPassConfig::addDebugifyPass() {
 }
 
 void TargetPassConfig::addStripDebugPass() {
-  PM->add(createStripDebugMachineModulePass(/*OnlyDebugified=*/true));
+  PM->add(createStripDebugMachineModuleLegacyPass(/*OnlyDebugified=*/true));
 }
 
 void TargetPassConfig::addCheckDebugPass() {
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 55a4e99e7402e..cf044d9c6ed79 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -134,6 +134,7 @@
 #include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/CodeGen/MachineScheduler.h"
 #include "llvm/CodeGen/MachineSink.h"
+#include "llvm/CodeGen/MachineStripDebug.h"
 #include "llvm/CodeGen/MachineTraceMetrics.h"
 #include "llvm/CodeGen/MachineUniformityAnalysis.h"
 #include "llvm/CodeGen/MachineVerifier.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index c92d93d7ae396..7fbbe6e612a4a 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -119,6 +119,7 @@ MODULE_PASS("memprof-remove-attributes", MemProfRemoveInfo())
 MODULE_PASS("memprof-module", ModuleMemProfilerPass())
 MODULE_PASS("mergefunc", MergeFunctionsPass())
 MODULE_PASS("metarenamer", MetaRenamerPass())
+MODULE_PASS("mir-strip-debug", StripDebugMachineModulePass())
 MODULE_PASS("module-inline", ModuleInlinerPass())
 MODULE_PASS("name-anon-globals", NameAnonGlobalPass())
 MODULE_PASS("no-op-module", NoOpModulePass())

``````````

</details>


https://github.com/llvm/llvm-project/pull/190738


More information about the llvm-commits mailing list