[llvm] 4e3bac3 - [CodeGen] Port mir-debugify to new pass manager (#193201)

via llvm-commits llvm-commits at lists.llvm.org
Mon May 4 09:17:24 PDT 2026


Author: juan.vazquez
Date: 2026-05-04T17:17:20+01:00
New Revision: 4e3bac3ea2cc6fd778d53e317dd9fc27c1ddfc4f

URL: https://github.com/llvm/llvm-project/commit/4e3bac3ea2cc6fd778d53e317dd9fc27c1ddfc4f
DIFF: https://github.com/llvm/llvm-project/commit/4e3bac3ea2cc6fd778d53e317dd9fc27c1ddfc4f.diff

LOG: [CodeGen] Port mir-debugify to new pass manager (#193201)

Added: 
    llvm/include/llvm/CodeGen/MachineDebugify.h

Modified: 
    llvm/include/llvm/Passes/MachinePassRegistry.def
    llvm/include/llvm/Transforms/Utils/Debugify.h
    llvm/lib/CodeGen/MachineDebugify.cpp
    llvm/lib/Passes/PassBuilder.cpp
    llvm/lib/Passes/PassRegistry.def
    llvm/lib/Transforms/Utils/Debugify.cpp
    llvm/test/CodeGen/AArch64/GlobalISel/constant-mir-debugify.mir
    llvm/test/CodeGen/AArch64/GlobalISel/phi-mir-debugify.mir
    llvm/test/CodeGen/Generic/MIRDebugify/locations-and-values.mir
    llvm/test/CodeGen/Generic/MIRStripDebug/all.mir
    llvm/test/CodeGen/Generic/MIRStripDebug/bundles.mir
    llvm/test/CodeGen/Generic/MIRStripDebug/dont-strip-real-debug-info.mir
    llvm/test/CodeGen/Generic/MIRStripDebug/multiple-moduleflags.mir
    llvm/test/CodeGen/X86/machinesink-debug-inv-0.mir

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/MachineDebugify.h b/llvm/include/llvm/CodeGen/MachineDebugify.h
new file mode 100644
index 0000000000000..209d505ebd70d
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/MachineDebugify.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Attaches synthetic debug info to the MachineFunction for a Function. To be
+// used both by the legacy and the new pass manager.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_MACHINEDEBUGIFY_H_
+#define LLVM_CODEGEN_MACHINEDEBUGIFY_H_
+
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/IR/DIBuilder.h"
+#include "llvm/IR/Function.h"
+#include "llvm/Support/Compiler.h"
+
+namespace llvm {
+
+LLVM_ABI bool applyDebugifyMetadataToMachineFunction(
+    DIBuilder &DIB, Function &F,
+    llvm::function_ref<MachineFunction *(Function &)> GetMF);
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_MACHINEDEBUGIFY_H_

diff  --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index f76e1650c528d..148d15c95f919 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -264,7 +264,6 @@ MACHINE_FUNCTION_PASS_WITH_PARAMS(
 DUMMY_MACHINE_MODULE_PASS("machine-outliner", MachineOutlinerPass)
 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)
 #undef DUMMY_MACHINE_MODULE_PASS
 

diff  --git a/llvm/include/llvm/Transforms/Utils/Debugify.h b/llvm/include/llvm/Transforms/Utils/Debugify.h
index 64404efabed1b..a72a9144e13e8 100644
--- a/llvm/include/llvm/Transforms/Utils/Debugify.h
+++ b/llvm/include/llvm/Transforms/Utils/Debugify.h
@@ -97,6 +97,9 @@ LLVM_ABI bool checkDebugInfoMetadata(Module &M,
 /// Used to check whether we track synthetic or original debug info.
 enum class DebugifyMode { NoDebugify, SyntheticDebugInfo, OriginalDebugInfo };
 
+using DebugifyApplyToMFCallback = llvm::function_ref<bool(
+    llvm::DIBuilder &, llvm::Function &, llvm::ModuleAnalysisManager &)>;
+
 LLVM_ABI llvm::ModulePass *createDebugifyModulePass(
     enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
     llvm::StringRef NameOfWrappedPass = "",
@@ -107,6 +110,7 @@ LLVM_ABI llvm::FunctionPass *createDebugifyFunctionPass(
     DebugInfoPerPass *DebugInfoBeforePass = nullptr);
 
 class NewPMDebugifyPass : public llvm::PassInfoMixin<NewPMDebugifyPass> {
+  DebugifyApplyToMFCallback ApplyToMF = nullptr;
   llvm::StringRef NameOfWrappedPass;
   DebugInfoPerPass *DebugInfoBeforePass = nullptr;
   enum DebugifyMode Mode = DebugifyMode::NoDebugify;
@@ -117,6 +121,8 @@ class NewPMDebugifyPass : public llvm::PassInfoMixin<NewPMDebugifyPass> {
       DebugInfoPerPass *DebugInfoBeforePass = nullptr)
       : NameOfWrappedPass(NameOfWrappedPass),
         DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
+  NewPMDebugifyPass(DebugifyApplyToMFCallback ApplyToMF)
+      : ApplyToMF(ApplyToMF), Mode(DebugifyMode::SyntheticDebugInfo) {}
 
   LLVM_ABI llvm::PreservedAnalyses run(llvm::Module &M,
                                        llvm::ModuleAnalysisManager &AM);

diff  --git a/llvm/lib/CodeGen/MachineDebugify.cpp b/llvm/lib/CodeGen/MachineDebugify.cpp
index c3d436ded60a8..5236ee58b87a4 100644
--- a/llvm/lib/CodeGen/MachineDebugify.cpp
+++ b/llvm/lib/CodeGen/MachineDebugify.cpp
@@ -13,6 +13,7 @@
 /// This isn't intended to have feature parity with Debugify.
 //===----------------------------------------------------------------------===//
 
+#include "llvm/CodeGen/MachineDebugify.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -28,10 +29,10 @@
 
 using namespace llvm;
 
-namespace {
-bool applyDebugifyMetadataToMachineFunction(MachineModuleInfo &MMI,
-                                            DIBuilder &DIB, Function &F) {
-  MachineFunction *MaybeMF = MMI.getMachineFunction(F);
+bool llvm::applyDebugifyMetadataToMachineFunction(
+    DIBuilder &DIB, Function &F,
+    llvm::function_ref<MachineFunction *(Function &)> GetMF) {
+  MachineFunction *MaybeMF = GetMF(F);
   if (!MaybeMF)
     return false;
   MachineFunction &MF = *MaybeMF;
@@ -170,6 +171,8 @@ bool applyDebugifyMetadataToMachineFunction(MachineModuleInfo &MMI,
   return true;
 }
 
+namespace {
+
 /// ModulePass for attaching synthetic debug info to everything, used with the
 /// legacy module pass manager.
 struct DebugifyMachineModule : public ModulePass {
@@ -182,7 +185,10 @@ struct DebugifyMachineModule : public ModulePass {
     return applyDebugifyMetadata(
         M, M.functions(),
         "ModuleDebugify: ", [&](DIBuilder &DIB, Function &F) -> bool {
-          return applyDebugifyMetadataToMachineFunction(MMI, DIB, F);
+          return applyDebugifyMetadataToMachineFunction(
+              DIB, F, [&MMI](Function &F) -> MachineFunction * {
+                return MMI.getMachineFunction(F);
+              });
         });
   }
 

diff  --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 2352161dc1aae..35ef547b36e80 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -125,6 +125,7 @@
 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
 #include "llvm/CodeGen/MachineCSE.h"
 #include "llvm/CodeGen/MachineCopyPropagation.h"
+#include "llvm/CodeGen/MachineDebugify.h"
 #include "llvm/CodeGen/MachineDominanceFrontier.h"
 #include "llvm/CodeGen/MachineDominators.h"
 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
@@ -405,6 +406,19 @@ AnalysisKey NoOpLoopAnalysis::Key;
 
 namespace {
 
+bool applyMIRDebugify(DIBuilder &DIB, Function &F, ModuleAnalysisManager &AM) {
+  FunctionAnalysisManager &FAM =
+      AM.getResult<FunctionAnalysisManagerModuleProxy>(*F.getParent())
+          .getManager();
+
+  return applyDebugifyMetadataToMachineFunction(
+      DIB, F, [&](Function &Func) -> MachineFunction * {
+        MachineFunctionAnalysis::Result *MFA =
+            FAM.getCachedResult<MachineFunctionAnalysis>(Func);
+        return MFA ? &MFA->getMF() : nullptr;
+      });
+}
+
 // Passes for testing crashes.
 // DO NOT USE THIS EXCEPT FOR TESTING!
 class TriggerCrashModulePass : public PassInfoMixin<TriggerCrashModulePass> {

diff  --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 16d0e9e0a0c2e..b05b1d3a4a8f5 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-debugify", NewPMDebugifyPass(applyMIRDebugify))
 MODULE_PASS("mir-strip-debug", StripDebugMachineModulePass())
 MODULE_PASS("module-inline", ModuleInlinerPass())
 MODULE_PASS("name-anon-globals", NameAnonGlobalPass())

diff  --git a/llvm/lib/Transforms/Utils/Debugify.cpp b/llvm/lib/Transforms/Utils/Debugify.cpp
index 2923633f29d7a..a9f5716b5c396 100644
--- a/llvm/lib/Transforms/Utils/Debugify.cpp
+++ b/llvm/lib/Transforms/Utils/Debugify.cpp
@@ -1061,16 +1061,26 @@ createDebugifyFunctionPass(enum DebugifyMode Mode,
   return new DebugifyFunctionPass(Mode, NameOfWrappedPass, DebugInfoBeforePass);
 }
 
-PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) {
-  if (Mode == DebugifyMode::SyntheticDebugInfo)
-    applyDebugifyMetadata(M, M.functions(),
-                          "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
-  else
+PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &AM) {
+  if (Mode == DebugifyMode::SyntheticDebugInfo) {
+    if (ApplyToMF) {
+      auto ApplyToMFWrapper = [&](DIBuilder &DIB, Function &F) -> bool {
+        return ApplyToMF(DIB, F, AM);
+      };
+      applyDebugifyMetadata(M, M.functions(),
+                            "ModuleDebugify: ", ApplyToMFWrapper);
+    } else {
+      applyDebugifyMetadata(M, M.functions(), "ModuleDebugify: ", nullptr);
+    }
+  } else {
     collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
                              "ModuleDebugify (original debuginfo)",
                               NameOfWrappedPass);
+  }
 
   PreservedAnalyses PA;
+  if (ApplyToMF)
+    PA.preserve<FunctionAnalysisManagerModuleProxy>();
   PA.preserveSet<CFGAnalyses>();
   return PA;
 }

diff  --git a/llvm/test/CodeGen/AArch64/GlobalISel/constant-mir-debugify.mir b/llvm/test/CodeGen/AArch64/GlobalISel/constant-mir-debugify.mir
index c05ddf98640f8..304b2ac3becee 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/constant-mir-debugify.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/constant-mir-debugify.mir
@@ -1,5 +1,7 @@
 # NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
 # RUN: llc -debugify-and-strip-all-safe=0 -run-pass=mir-debugify -verify-machineinstrs -mtriple aarch64-unknown-unknown %s -o - | FileCheck %s
+
+# RUN: llc -debugify-and-strip-all-safe=0 -passes=mir-debugify -verify-machineinstrs -mtriple aarch64-unknown-unknown %s -o - | FileCheck %s
 ...
 ---
 name:            fconstant_to_constant_s32

diff  --git a/llvm/test/CodeGen/AArch64/GlobalISel/phi-mir-debugify.mir b/llvm/test/CodeGen/AArch64/GlobalISel/phi-mir-debugify.mir
index 7968c7be5d308..9fbdc331fadb6 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/phi-mir-debugify.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/phi-mir-debugify.mir
@@ -1,5 +1,7 @@
 # NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
 # RUN: llc -debugify-and-strip-all-safe=0 -run-pass=mir-debugify -verify-machineinstrs %s -o - | FileCheck %s
+
+# RUN: llc -debugify-and-strip-all-safe=0 -passes=mir-debugify -verify-machineinstrs %s -o - | FileCheck %s
 --- |
   target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
   target triple = "aarch64-unknown-unknown"

diff  --git a/llvm/test/CodeGen/Generic/MIRDebugify/locations-and-values.mir b/llvm/test/CodeGen/Generic/MIRDebugify/locations-and-values.mir
index f96d83fede40f..009dd5c249b9d 100644
--- a/llvm/test/CodeGen/Generic/MIRDebugify/locations-and-values.mir
+++ b/llvm/test/CodeGen/Generic/MIRDebugify/locations-and-values.mir
@@ -3,6 +3,11 @@
 # RUN: llc -run-pass=mir-debugify,mir-strip-debug,mir-debugify -o - %s | FileCheck --check-prefixes=ALL,VALUE %s
 # RUN: llc -run-pass=mir-debugify,mir-strip-debug -o - %s | FileCheck --check-prefix=STRIP %s
 
+# RUN: llc -passes=mir-debugify -o - %s | FileCheck --check-prefixes=ALL,VALUE %s
+# RUN: llc -passes=mir-debugify -debugify-level=locations -o - %s | FileCheck --check-prefixes=ALL --implicit-check-not=dbg_value %s
+# RUN: llc -passes=mir-debugify,mir-strip-debug,mir-debugify -o - %s | FileCheck --check-prefixes=ALL,VALUE %s
+# RUN: llc -passes=mir-debugify,mir-strip-debug -o - %s | FileCheck --check-prefix=STRIP %s
+
 --- |
   ; ModuleID = 'loc-only.ll'
   source_filename = "loc-only.ll"

diff  --git a/llvm/test/CodeGen/Generic/MIRStripDebug/all.mir b/llvm/test/CodeGen/Generic/MIRStripDebug/all.mir
index 0404b991779a5..760f7c3655a68 100644
--- a/llvm/test/CodeGen/Generic/MIRStripDebug/all.mir
+++ b/llvm/test/CodeGen/Generic/MIRStripDebug/all.mir
@@ -1,5 +1,8 @@
 # RUN: llc -run-pass=mir-strip-debug -o - %s | FileCheck %s
 # RUN: llc -run-pass=mir-strip-debug,mir-debugify,mir-strip-debug -o - %s | FileCheck %s
+
+# RUN: llc -passes=mir-strip-debug -o - %s | FileCheck %s
+# RUN: llc -passes=mir-strip-debug,mir-debugify,mir-strip-debug -o - %s | FileCheck %s
 --- |
   ; ModuleID = 'loc-only.ll'
   source_filename = "loc-only.ll"

diff  --git a/llvm/test/CodeGen/Generic/MIRStripDebug/bundles.mir b/llvm/test/CodeGen/Generic/MIRStripDebug/bundles.mir
index 111c886f585cf..917fa54e18cdf 100644
--- a/llvm/test/CodeGen/Generic/MIRStripDebug/bundles.mir
+++ b/llvm/test/CodeGen/Generic/MIRStripDebug/bundles.mir
@@ -1,6 +1,9 @@
 # RUN: llc -run-pass=mir-strip-debug -o - %s | FileCheck %s
 # RUN: llc -run-pass=mir-strip-debug,mir-debugify,mir-strip-debug -o - %s | FileCheck %s
 
+# RUN: llc -passes=mir-strip-debug -o - %s | FileCheck %s
+# RUN: llc -passes=mir-strip-debug,mir-debugify,mir-strip-debug -o - %s | FileCheck %s
+
 --- |
   source_filename = "loc-only.ll"
   

diff  --git a/llvm/test/CodeGen/Generic/MIRStripDebug/dont-strip-real-debug-info.mir b/llvm/test/CodeGen/Generic/MIRStripDebug/dont-strip-real-debug-info.mir
index a085462b8e8f9..f241396648f2b 100644
--- a/llvm/test/CodeGen/Generic/MIRStripDebug/dont-strip-real-debug-info.mir
+++ b/llvm/test/CodeGen/Generic/MIRStripDebug/dont-strip-real-debug-info.mir
@@ -1,5 +1,8 @@
 # RUN: llc -run-pass=mir-strip-debug -o - %s | FileCheck %s
 # RUN: llc -run-pass=mir-strip-debug,mir-debugify,mir-strip-debug -o - %s | FileCheck %s
+
+# RUN: llc -passes=mir-strip-debug -o - %s | FileCheck %s
+# RUN: llc -passes=mir-strip-debug,mir-debugify,mir-strip-debug -o - %s | FileCheck %s
 --- |
   ; ModuleID = 'loc-only.ll'
   source_filename = "loc-only.ll"

diff  --git a/llvm/test/CodeGen/Generic/MIRStripDebug/multiple-moduleflags.mir b/llvm/test/CodeGen/Generic/MIRStripDebug/multiple-moduleflags.mir
index 9240c1a56eb9b..408616e546d6e 100644
--- a/llvm/test/CodeGen/Generic/MIRStripDebug/multiple-moduleflags.mir
+++ b/llvm/test/CodeGen/Generic/MIRStripDebug/multiple-moduleflags.mir
@@ -1,5 +1,8 @@
 # RUN: llc -run-pass=mir-strip-debug -o - %s | FileCheck %s
 # RUN: llc -run-pass=mir-strip-debug,mir-debugify,mir-strip-debug -o - %s | FileCheck %s
+
+# RUN: llc -passes=mir-strip-debug -o - %s | FileCheck %s
+# RUN: llc -passes=mir-strip-debug,mir-debugify,mir-strip-debug -o - %s | FileCheck %s
 --- |
   ; ModuleID = 'loc-only.ll'
   source_filename = "loc-only.ll"

diff  --git a/llvm/test/CodeGen/X86/machinesink-debug-inv-0.mir b/llvm/test/CodeGen/X86/machinesink-debug-inv-0.mir
index 20207311e66cd..242fbfb11785d 100644
--- a/llvm/test/CodeGen/X86/machinesink-debug-inv-0.mir
+++ b/llvm/test/CodeGen/X86/machinesink-debug-inv-0.mir
@@ -3,7 +3,7 @@
 # RUN: llc -mtriple=x86_64 -machine-sink-load-instrs-threshold=2 -run-pass=mir-debugify,machine-sink,mir-strip-debug %s -o - | FileCheck %s
 
 # RUN: llc -mtriple=x86_64 -machine-sink-load-instrs-threshold=2 -passes=machine-sink %s -o - | FileCheck %s
-# TODO: add test with mir-debugify once it is ported
+# RUN: llc -mtriple=x86_64 -machine-sink-load-instrs-threshold=2 -passes=mir-debugify,function(machine-function(machine-sink)),mir-strip-debug %s -o - | FileCheck %s
 
 # Verify that machine-sink pass is debug invariant wrt to given input. Since
 # the pass examines MemOperands the IR is required for the original bug to


        


More information about the llvm-commits mailing list