[llvm] [CodeGen] Port mir-check-debugify to new pass manager (PR #199280)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 28 08:24:35 PDT 2026


https://github.com/juanvazquez updated https://github.com/llvm/llvm-project/pull/199280

>From 496495f77c940995271414b2dca2b43152425595 Mon Sep 17 00:00:00 2001
From: Juan Vazquez <juvazq at google.com>
Date: Fri, 22 May 2026 22:18:48 +0200
Subject: [PATCH] [CodeGen] Port mir-check-debugify to new pass manager

---
 .../llvm/CodeGen/MachineCheckDebugify.h       |  33 ++++
 llvm/include/llvm/CodeGen/Passes.h            |   2 +-
 llvm/include/llvm/InitializePasses.h          |   2 +-
 .../llvm/Passes/MachinePassRegistry.def       |   1 -
 llvm/lib/CodeGen/CodeGen.cpp                  |   2 +-
 llvm/lib/CodeGen/MachineCheckDebugify.cpp     | 165 ++++++++++--------
 llvm/lib/CodeGen/TargetPassConfig.cpp         |   2 +-
 llvm/lib/Passes/PassBuilder.cpp               |   1 +
 llvm/lib/Passes/PassRegistry.def              |   1 +
 .../check-line-and-variables-x.mir            |   2 +
 .../MIRDebugify/check-line-and-variables.mir  |   3 +
 .../MIRDebugify/multifunction-module.mir      |   2 +
 12 files changed, 139 insertions(+), 77 deletions(-)
 create mode 100644 llvm/include/llvm/CodeGen/MachineCheckDebugify.h

diff --git a/llvm/include/llvm/CodeGen/MachineCheckDebugify.h b/llvm/include/llvm/CodeGen/MachineCheckDebugify.h
new file mode 100644
index 0000000000000..df0aeeb9dda8c
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/MachineCheckDebugify.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the declaration of the CheckDebugMachineModulePass class,
+/// used by the new pass manager to check debug info after mir-debugify.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_MACHINECHECKDEBUGIFY_H_
+#define LLVM_CODEGEN_MACHINECHECKDEBUGIFY_H_
+
+#include "llvm/IR/Analysis.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/Support/Compiler.h"
+
+namespace llvm {
+
+class CheckDebugMachineModulePass
+    : public PassInfoMixin<CheckDebugMachineModulePass> {
+public:
+  LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_MACHINECHECKDEBUGIFY_H_
diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index b2a16dd74c304..4ae41f7427c51 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -606,7 +606,7 @@ LLVM_ABI ModulePass *
 createStripDebugMachineModuleLegacyPass(bool OnlyDebugified);
 
 /// Creates MIR Check Debug pass. \see MachineCheckDebugify.cpp
-LLVM_ABI ModulePass *createCheckDebugMachineModulePass();
+LLVM_ABI ModulePass *createCheckDebugMachineModuleLegacyPass();
 
 /// The pass fixups statepoint machine instruction to replace usage of
 /// caller saved registers with stack slots.
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 2c9a848bb6fba..2f02a7ede167f 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -79,7 +79,7 @@ LLVM_ABI void initializeCFIInstrInserterPass(PassRegistry &);
 LLVM_ABI void initializeCallGraphDOTPrinterPass(PassRegistry &);
 LLVM_ABI void initializeCallGraphViewerPass(PassRegistry &);
 LLVM_ABI void initializeCallGraphWrapperPassPass(PassRegistry &);
-LLVM_ABI void initializeCheckDebugMachineModulePass(PassRegistry &);
+LLVM_ABI void initializeCheckDebugMachineModuleLegacyPass(PassRegistry &);
 LLVM_ABI void initializeCodeGenPrepareLegacyPassPass(PassRegistry &);
 LLVM_ABI void initializeComplexDeinterleavingLegacyPassPass(PassRegistry &);
 LLVM_ABI void initializeConstantHoistingLegacyPassPass(PassRegistry &);
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index bd411b7331ed0..cb3aa9c013c43 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -226,7 +226,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-check-debugify", CheckDebugMachineModulePass)
 #undef DUMMY_MACHINE_MODULE_PASS
 
 #ifndef DUMMY_MACHINE_FUNCTION_PASS
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index b473190783027..870e12f1b308e 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -30,7 +30,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
   initializeCFGuardLongjmpPass(Registry);
   initializeCFIFixupLegacyPass(Registry);
   initializeCFIInstrInserterPass(Registry);
-  initializeCheckDebugMachineModulePass(Registry);
+  initializeCheckDebugMachineModuleLegacyPass(Registry);
   initializeCodeGenPrepareLegacyPassPass(Registry);
   initializeComplexDeinterleavingLegacyPassPass(Registry);
   initializeDeadMachineInstructionElimPass(Registry);
diff --git a/llvm/lib/CodeGen/MachineCheckDebugify.cpp b/llvm/lib/CodeGen/MachineCheckDebugify.cpp
index 9b703d5401cb9..ac4e5823cd705 100644
--- a/llvm/lib/CodeGen/MachineCheckDebugify.cpp
+++ b/llvm/lib/CodeGen/MachineCheckDebugify.cpp
@@ -11,9 +11,11 @@
 /// DILocalVariable which mir-debugifiy generated before.
 //===----------------------------------------------------------------------===//
 
+#include "llvm/CodeGen/MachineCheckDebugify.h"
 #include "llvm/ADT/StringExtras.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/Constants.h"
@@ -28,84 +30,91 @@ using namespace llvm;
 
 namespace {
 
-struct CheckDebugMachineModule : public ModulePass {
-  bool runOnModule(Module &M) override {
-    NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
-    if (!NMD) {
-      errs() << "WARNING: Please run mir-debugify to generate "
-                "llvm.mir.debugify metadata first.\n";
-      return false;
-    }
-
-    MachineModuleInfo &MMI =
-        getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
+bool checkDebugMachineModuleImpl(
+    Module &M, llvm::function_ref<MachineFunction *(Function &)> GetMF) {
+  NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
+  if (!NMD) {
+    errs() << "WARNING: Please run mir-debugify to generate "
+              "llvm.mir.debugify metadata first.\n";
+    return false;
+  }
 
-    auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
-      return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
-          ->getZExtValue();
-    };
-    assert(NMD->getNumOperands() == 2 &&
-           "llvm.mir.debugify should have exactly 2 operands!");
-    unsigned NumLines = getDebugifyOperand(0);
-    unsigned NumVars = getDebugifyOperand(1);
-    BitVector MissingLines{NumLines, true};
-    BitVector MissingVars{NumVars, true};
-
-    for (Function &F : M.functions()) {
-      MachineFunction *MF = MMI.getMachineFunction(F);
-      if (!MF)
-        continue;
-      for (MachineBasicBlock &MBB : *MF) {
-        // Find missing lines.
-        // TODO: Avoid meta instructions other than dbg_val.
-        for (MachineInstr &MI : MBB) {
-          if (MI.isDebugValue())
-            continue;
-          const DebugLoc DL = MI.getDebugLoc();
-          if (DL && DL.getLine() != 0) {
-            MissingLines.reset(DL.getLine() - 1);
-            continue;
-          }
-
-          if (!DL) {
-            errs() << "WARNING: Instruction with empty DebugLoc in function ";
-            errs() << F.getName() << " --";
-            MI.print(errs());
-          }
+  auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
+    return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
+        ->getZExtValue();
+  };
+  assert(NMD->getNumOperands() == 2 &&
+         "llvm.mir.debugify should have exactly 2 operands!");
+  unsigned NumLines = getDebugifyOperand(0);
+  unsigned NumVars = getDebugifyOperand(1);
+  BitVector MissingLines{NumLines, true};
+  BitVector MissingVars{NumVars, true};
+
+  for (Function &F : M.functions()) {
+    MachineFunction *MF = GetMF(F);
+    if (!MF)
+      continue;
+    for (MachineBasicBlock &MBB : *MF) {
+      // Find missing lines.
+      // TODO: Avoid meta instructions other than dbg_val.
+      for (MachineInstr &MI : MBB) {
+        if (MI.isDebugValue())
+          continue;
+        const DebugLoc DL = MI.getDebugLoc();
+        if (DL && DL.getLine() != 0) {
+          MissingLines.reset(DL.getLine() - 1);
+          continue;
         }
 
-        // Find missing variables.
-        // TODO: Handle DBG_INSTR_REF which is under an experimental option now.
-        for (MachineInstr &MI : MBB) {
-          if (!MI.isDebugValue())
-            continue;
-          const DILocalVariable *LocalVar = MI.getDebugVariable();
-          unsigned Var = ~0U;
-
-          (void)to_integer(LocalVar->getName(), Var, 10);
-          assert(Var <= NumVars && "Unexpected name for DILocalVariable");
-          MissingVars.reset(Var - 1);
+        if (!DL) {
+          errs() << "WARNING: Instruction with empty DebugLoc in function ";
+          errs() << F.getName() << " --";
+          MI.print(errs());
         }
       }
-    }
 
-    bool Fail = false;
-    for (unsigned Idx : MissingLines.set_bits()) {
-      errs() << "WARNING: Missing line " << Idx + 1 << "\n";
-      Fail = true;
+      // Find missing variables.
+      // TODO: Handle DBG_INSTR_REF which is under an experimental option now.
+      for (MachineInstr &MI : MBB) {
+        if (!MI.isDebugValue())
+          continue;
+        const DILocalVariable *LocalVar = MI.getDebugVariable();
+        unsigned Var = ~0U;
+
+        (void)to_integer(LocalVar->getName(), Var, 10);
+        assert(Var <= NumVars && "Unexpected name for DILocalVariable");
+        MissingVars.reset(Var - 1);
+      }
     }
+  }
 
-    for (unsigned Idx : MissingVars.set_bits()) {
-      errs() << "WARNING: Missing variable " << Idx + 1 << "\n";
-      Fail = true;
-    }
-    errs() << "Machine IR debug info check: ";
-    errs() << (Fail ? "FAIL" : "PASS") << "\n";
+  bool Fail = false;
+  for (unsigned Idx : MissingLines.set_bits()) {
+    errs() << "WARNING: Missing line " << Idx + 1 << "\n";
+    Fail = true;
+  }
 
-    return false;
+  for (unsigned Idx : MissingVars.set_bits()) {
+    errs() << "WARNING: Missing variable " << Idx + 1 << "\n";
+    Fail = true;
   }
+  errs() << "Machine IR debug info check: ";
+  errs() << (Fail ? "FAIL" : "PASS") << "\n";
 
-  CheckDebugMachineModule() : ModulePass(ID) {}
+  return false;
+}
+
+struct CheckDebugMachineModuleLegacy : public ModulePass {
+  bool runOnModule(Module &M) override {
+    MachineModuleInfo &MMI =
+        getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
+    return checkDebugMachineModuleImpl(
+        M, [&MMI](Function &F) -> MachineFunction * {
+          return MMI.getMachineFunction(F);
+        });
+  }
+
+  CheckDebugMachineModuleLegacy() : ModulePass(ID) {}
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.addRequired<MachineModuleInfoWrapperPass>();
@@ -114,15 +123,27 @@ struct CheckDebugMachineModule : public ModulePass {
 
   static char ID; // Pass identification.
 };
-char CheckDebugMachineModule::ID = 0;
+char CheckDebugMachineModuleLegacy::ID = 0;
 
 } // end anonymous namespace
 
-INITIALIZE_PASS_BEGIN(CheckDebugMachineModule, DEBUG_TYPE,
+INITIALIZE_PASS_BEGIN(CheckDebugMachineModuleLegacy, DEBUG_TYPE,
                       "Machine Check Debug Module", false, false)
-INITIALIZE_PASS_END(CheckDebugMachineModule, DEBUG_TYPE,
+INITIALIZE_PASS_END(CheckDebugMachineModuleLegacy, DEBUG_TYPE,
                     "Machine Check Debug Module", false, false)
 
-ModulePass *llvm::createCheckDebugMachineModulePass() {
-  return new CheckDebugMachineModule();
+ModulePass *llvm::createCheckDebugMachineModuleLegacyPass() {
+  return new CheckDebugMachineModuleLegacy();
+}
+
+PreservedAnalyses CheckDebugMachineModulePass::run(Module &M,
+                                                   ModuleAnalysisManager &AM) {
+  FunctionAnalysisManager &FAM =
+      AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
+  checkDebugMachineModuleImpl(M, [&FAM](Function &F) -> MachineFunction * {
+    MachineFunctionAnalysis::Result *MFA =
+        FAM.getCachedResult<MachineFunctionAnalysis>(F);
+    return MFA ? &MFA->getMF() : nullptr;
+  });
+  return PreservedAnalyses::all();
 }
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index 04f6a6d7e775e..614c280a2110c 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -820,7 +820,7 @@ void TargetPassConfig::addStripDebugPass() {
 }
 
 void TargetPassConfig::addCheckDebugPass() {
-  PM->add(createCheckDebugMachineModulePass());
+  PM->add(createCheckDebugMachineModuleLegacyPass());
 }
 
 void TargetPassConfig::addMachinePrePasses(bool AllowDebugify) {
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 68ea19332ec33..6dd9ab0eec054 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -128,6 +128,7 @@
 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
 #include "llvm/CodeGen/MachineCFGPrinter.h"
 #include "llvm/CodeGen/MachineCSE.h"
+#include "llvm/CodeGen/MachineCheckDebugify.h"
 #include "llvm/CodeGen/MachineCopyPropagation.h"
 #include "llvm/CodeGen/MachineDebugify.h"
 #include "llvm/CodeGen/MachineDominanceFrontier.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 84804c3e9d2a8..64331e04c155b 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -122,6 +122,7 @@ MODULE_PASS("memprof-module", ModuleMemProfilerPass())
 MODULE_PASS("mergefunc", MergeFunctionsPass())
 MODULE_PASS("metarenamer", MetaRenamerPass())
 MODULE_PASS("mir-debugify", NewPMDebugifyPass(applyMIRDebugify))
+MODULE_PASS("mir-check-debugify", CheckDebugMachineModulePass())
 MODULE_PASS("mir-strip-debug", StripDebugMachineModulePass())
 MODULE_PASS("module-inline", ModuleInlinerPass())
 MODULE_PASS("name-anon-globals", NameAnonGlobalPass())
diff --git a/llvm/test/CodeGen/Generic/MIRDebugify/check-line-and-variables-x.mir b/llvm/test/CodeGen/Generic/MIRDebugify/check-line-and-variables-x.mir
index eaa627966347f..0846dd15728f3 100644
--- a/llvm/test/CodeGen/Generic/MIRDebugify/check-line-and-variables-x.mir
+++ b/llvm/test/CodeGen/Generic/MIRDebugify/check-line-and-variables-x.mir
@@ -1,5 +1,7 @@
 # REQUIRES: x86-registered-target
 # RUN: llc -mtriple=x86_64-unknown-linux-gnu -run-pass=mir-check-debugify -o - %s 2>&1 | FileCheck %s
+
+# RUN: llc -mtriple=x86_64-unknown-linux-gnu -passes=mir-check-debugify -o - %s 2>&1 | FileCheck %s
 --- |
   ; ModuleID = 'check-line-and-variables.mir'
   source_filename = "check-line-and-variables.c"
diff --git a/llvm/test/CodeGen/Generic/MIRDebugify/check-line-and-variables.mir b/llvm/test/CodeGen/Generic/MIRDebugify/check-line-and-variables.mir
index 9eb722258b703..ead0470d985a5 100644
--- a/llvm/test/CodeGen/Generic/MIRDebugify/check-line-and-variables.mir
+++ b/llvm/test/CodeGen/Generic/MIRDebugify/check-line-and-variables.mir
@@ -1,6 +1,9 @@
 # REQUIRES: x86-registered-target
 # RUN: llc -mtriple=x86_64-unknown-linux-gnu -run-pass=mir-debugify,dead-mi-elimination,mir-check-debugify -o - %s 2>&1 | FileCheck %s
 # RUN: llc -mtriple=x86_64-unknown-linux-gnu -run-pass=mir-debugify,mir-check-debugify -o - %s 2>&1 | FileCheck %s --check-prefix=CHECK-PASS
+
+# RUN: llc -mtriple=x86_64-unknown-linux-gnu -passes=mir-debugify,function(machine-function(dead-mi-elimination)),mir-check-debugify -o - %s 2>&1 | FileCheck %s
+# RUN: llc -mtriple=x86_64-unknown-linux-gnu -passes=mir-debugify,mir-check-debugify -o - %s 2>&1 | FileCheck %s --check-prefix=CHECK-PASS
 --- |
   ; ModuleID = 'check-line-and-variables.mir'
   source_filename = "check-line-and-variables.ll"
diff --git a/llvm/test/CodeGen/Generic/MIRDebugify/multifunction-module.mir b/llvm/test/CodeGen/Generic/MIRDebugify/multifunction-module.mir
index 27f942468a8dd..327bf74661982 100644
--- a/llvm/test/CodeGen/Generic/MIRDebugify/multifunction-module.mir
+++ b/llvm/test/CodeGen/Generic/MIRDebugify/multifunction-module.mir
@@ -1,5 +1,7 @@
 # RUN: llc -run-pass=mir-debugify,mir-check-debugify -o - %s 2>&1 | FileCheck %s
 
+# RUN: llc -passes=mir-debugify,mir-check-debugify -o - %s 2>&1 | FileCheck %s
+
 # CHECK: Machine IR debug info check: PASS
 # CHECK-NOT: Assertion `Var <= NumVars && "Unexpected name for DILocalVariable"'
 



More information about the llvm-commits mailing list