[llvm] [CodeGen] Add a pass for testing finalizeBundle (PR #149813)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 21 09:22:46 PDT 2025


https://github.com/jayfoad updated https://github.com/llvm/llvm-project/pull/149813

>From 4c9931da2993615e706e353964877355eb03bcf3 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Mon, 21 Jul 2025 14:13:53 +0100
Subject: [PATCH 1/2] [CodeGen] Add a pass for testing finalizeBundle

This allows for unit testing of finalizeBundle with standard MIR tests
using update_mir_test_checks.py.
---
 llvm/include/llvm/CodeGen/Passes.h           |  3 +++
 llvm/include/llvm/InitializePasses.h         |  1 +
 llvm/lib/CodeGen/CodeGen.cpp                 |  1 +
 llvm/lib/CodeGen/MachineInstrBundle.cpp      | 23 ++++++++++++++++++++
 llvm/test/CodeGen/AMDGPU/finalize-bundle.mir | 18 +++++++++++++++
 5 files changed, 46 insertions(+)
 create mode 100644 llvm/test/CodeGen/AMDGPU/finalize-bundle.mir

diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index 714285eb5d5d4..cad8ea5a28dcc 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -623,6 +623,9 @@ LLVM_ABI ModulePass *createWindowsSecureHotPatchingPass();
 
 /// Lowers KCFI operand bundles for indirect calls.
 LLVM_ABI FunctionPass *createKCFIPass();
+
+/// A pass for testing finalizeBundle.
+LLVM_ABI extern char &FinalizeBundleTestID;
 } // namespace llvm
 
 #endif
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 2e231cfba2443..ccb5711e55820 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -340,6 +340,7 @@ LLVM_ABI void initializeWindowsSecureHotPatchingPass(PassRegistry &);
 LLVM_ABI void initializeWinEHPreparePass(PassRegistry &);
 LLVM_ABI void initializeWriteBitcodePassPass(PassRegistry &);
 LLVM_ABI void initializeXRayInstrumentationLegacyPass(PassRegistry &);
+LLVM_ABI void initializeFinalizeBundleTestPass(PassRegistry &);
 
 } // end namespace llvm
 
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index c3b4077b27dd8..bd52edd66c547 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -146,4 +146,5 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
   initializeWasmEHPreparePass(Registry);
   initializeWinEHPreparePass(Registry);
   initializeXRayInstrumentationLegacyPass(Registry);
+  initializeFinalizeBundleTestPass(Registry);
 }
diff --git a/llvm/lib/CodeGen/MachineInstrBundle.cpp b/llvm/lib/CodeGen/MachineInstrBundle.cpp
index 34896c67144bc..741bfb08ef2fb 100644
--- a/llvm/lib/CodeGen/MachineInstrBundle.cpp
+++ b/llvm/lib/CodeGen/MachineInstrBundle.cpp
@@ -359,3 +359,26 @@ PhysRegInfo llvm::AnalyzePhysRegInBundle(const MachineInstr &MI, Register Reg,
 
   return PRI;
 }
+
+namespace {
+class FinalizeBundleTest : public MachineFunctionPass {
+public:
+  static char ID;
+
+  FinalizeBundleTest() : MachineFunctionPass(ID) {
+    initializeFinalizeBundleTestPass(*PassRegistry::getPassRegistry());
+  }
+
+  bool runOnMachineFunction(MachineFunction &MF) override {
+    // For testing purposes, bundle the entire contents of each basic block
+    // except for terminators.
+    for (MachineBasicBlock &MBB : MF)
+      finalizeBundle(MBB, MBB.instr_begin(), MBB.getFirstInstrTerminator());
+    return true;
+  }
+};
+} // namespace
+
+char FinalizeBundleTest::ID = 0;
+INITIALIZE_PASS(FinalizeBundleTest, "finalizebundle-test",
+                "finalizeBundle test", false, false)
diff --git a/llvm/test/CodeGen/AMDGPU/finalize-bundle.mir b/llvm/test/CodeGen/AMDGPU/finalize-bundle.mir
new file mode 100644
index 0000000000000..be25e35765f67
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/finalize-bundle.mir
@@ -0,0 +1,18 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
+# RUN: llc -mtriple=amdgcn -mcpu=gfx1200 -run-pass finalizebundle-test %s -o - | FileCheck %s
+
+---
+name: test_overlap
+body: |
+  bb.0:
+    liveins: $vgpr0_vgpr1
+    ; CHECK-LABEL: name: test_overlap
+    ; CHECK: liveins: $vgpr0_vgpr1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: BUNDLE implicit-def $vgpr2_vgpr3, implicit-def $vgpr2, implicit-def $vgpr2_lo16, implicit-def $vgpr2_hi16, implicit-def $vgpr3, implicit-def $vgpr3_lo16, implicit-def $vgpr3_hi16, implicit-def $vgpr3_vgpr4, implicit-def $vgpr4, implicit-def $vgpr4_lo16, implicit-def $vgpr4_hi16, implicit $vgpr0_vgpr1, implicit $exec, implicit $vgpr1_vgpr2 {
+    ; CHECK-NEXT:   $vgpr2_vgpr3 = V_LSHLREV_B64_pseudo_e32 1, $vgpr0_vgpr1, implicit $exec
+    ; CHECK-NEXT:   $vgpr3_vgpr4 = V_LSHLREV_B64_pseudo_e32 1, $vgpr1_vgpr2, implicit $exec
+    ; CHECK-NEXT: }
+    $vgpr2_vgpr3 = V_LSHLREV_B64_pseudo_e32 1, $vgpr0_vgpr1, implicit $exec
+    $vgpr3_vgpr4 = V_LSHLREV_B64_pseudo_e32 1, $vgpr1_vgpr2, implicit $exec
+...

>From 1399bf6727596078c110e1e12c828a8ed2199046 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Mon, 21 Jul 2025 17:17:40 +0100
Subject: [PATCH 2/2] Switch to NPM

---
 .../include/llvm/CodeGen/MachineInstrBundle.h |  7 +++++
 llvm/include/llvm/CodeGen/Passes.h            |  3 --
 llvm/include/llvm/InitializePasses.h          |  1 -
 .../llvm/Passes/MachinePassRegistry.def       |  1 +
 llvm/lib/CodeGen/CodeGen.cpp                  |  1 -
 llvm/lib/CodeGen/MachineInstrBundle.cpp       | 31 ++++++-------------
 llvm/lib/Passes/PassBuilder.cpp               |  1 +
 ...finalize-bundle.mir => finalizebundle.mir} |  2 +-
 8 files changed, 19 insertions(+), 28 deletions(-)
 rename llvm/test/CodeGen/AMDGPU/{finalize-bundle.mir => finalizebundle.mir} (91%)

diff --git a/llvm/include/llvm/CodeGen/MachineInstrBundle.h b/llvm/include/llvm/CodeGen/MachineInstrBundle.h
index d324236a77348..ebf75347f6b16 100644
--- a/llvm/include/llvm/CodeGen/MachineInstrBundle.h
+++ b/llvm/include/llvm/CodeGen/MachineInstrBundle.h
@@ -15,6 +15,7 @@
 #define LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
 
 #include "llvm/CodeGen/MachineBasicBlock.h"
+#include "llvm/CodeGen/MachinePassManager.h"
 #include "llvm/Support/Compiler.h"
 
 namespace llvm {
@@ -294,6 +295,12 @@ LLVM_ABI PhysRegInfo AnalyzePhysRegInBundle(const MachineInstr &MI,
                                             Register Reg,
                                             const TargetRegisterInfo *TRI);
 
+class FinalizeBundleTestPass : public PassInfoMixin<FinalizeBundleTestPass> {
+public:
+  PreservedAnalyses run(MachineFunction &MF,
+                        MachineFunctionAnalysisManager &MFAM);
+};
+
 } // End llvm namespace
 
 #endif
diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index cad8ea5a28dcc..714285eb5d5d4 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -623,9 +623,6 @@ LLVM_ABI ModulePass *createWindowsSecureHotPatchingPass();
 
 /// Lowers KCFI operand bundles for indirect calls.
 LLVM_ABI FunctionPass *createKCFIPass();
-
-/// A pass for testing finalizeBundle.
-LLVM_ABI extern char &FinalizeBundleTestID;
 } // namespace llvm
 
 #endif
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index ccb5711e55820..2e231cfba2443 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -340,7 +340,6 @@ LLVM_ABI void initializeWindowsSecureHotPatchingPass(PassRegistry &);
 LLVM_ABI void initializeWinEHPreparePass(PassRegistry &);
 LLVM_ABI void initializeWriteBitcodePassPass(PassRegistry &);
 LLVM_ABI void initializeXRayInstrumentationLegacyPass(PassRegistry &);
-LLVM_ABI void initializeFinalizeBundleTestPass(PassRegistry &);
 
 } // end namespace llvm
 
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 732fdc7c9bc1f..bee210678f84f 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -113,6 +113,7 @@ MACHINE_FUNCTION_PASS("early-machinelicm", EarlyMachineLICMPass())
 MACHINE_FUNCTION_PASS("early-tailduplication", EarlyTailDuplicatePass())
 MACHINE_FUNCTION_PASS("fentry-insert", FEntryInserterPass())
 MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
+MACHINE_FUNCTION_PASS("finalizebundle-test", FinalizeBundleTestPass())
 MACHINE_FUNCTION_PASS("fixup-statepoint-caller-saved", FixupStatepointCallerSavedPass())
 MACHINE_FUNCTION_PASS("init-undef", InitUndefPass())
 MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index bd52edd66c547..c3b4077b27dd8 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -146,5 +146,4 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
   initializeWasmEHPreparePass(Registry);
   initializeWinEHPreparePass(Registry);
   initializeXRayInstrumentationLegacyPass(Registry);
-  initializeFinalizeBundleTestPass(Registry);
 }
diff --git a/llvm/lib/CodeGen/MachineInstrBundle.cpp b/llvm/lib/CodeGen/MachineInstrBundle.cpp
index 741bfb08ef2fb..44b648ae65e85 100644
--- a/llvm/lib/CodeGen/MachineInstrBundle.cpp
+++ b/llvm/lib/CodeGen/MachineInstrBundle.cpp
@@ -360,25 +360,12 @@ PhysRegInfo llvm::AnalyzePhysRegInBundle(const MachineInstr &MI, Register Reg,
   return PRI;
 }
 
-namespace {
-class FinalizeBundleTest : public MachineFunctionPass {
-public:
-  static char ID;
-
-  FinalizeBundleTest() : MachineFunctionPass(ID) {
-    initializeFinalizeBundleTestPass(*PassRegistry::getPassRegistry());
-  }
-
-  bool runOnMachineFunction(MachineFunction &MF) override {
-    // For testing purposes, bundle the entire contents of each basic block
-    // except for terminators.
-    for (MachineBasicBlock &MBB : MF)
-      finalizeBundle(MBB, MBB.instr_begin(), MBB.getFirstInstrTerminator());
-    return true;
-  }
-};
-} // namespace
-
-char FinalizeBundleTest::ID = 0;
-INITIALIZE_PASS(FinalizeBundleTest, "finalizebundle-test",
-                "finalizeBundle test", false, false)
+PreservedAnalyses
+llvm::FinalizeBundleTestPass::run(MachineFunction &MF,
+                                  MachineFunctionAnalysisManager &) {
+  // For testing purposes, bundle the entire contents of each basic block
+  // except for terminators.
+  for (MachineBasicBlock &MBB : MF)
+    finalizeBundle(MBB, MBB.instr_begin(), MBB.getFirstInstrTerminator());
+  return PreservedAnalyses::none();
+}
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index e15570c3f600e..cff7ab52f1536 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -124,6 +124,7 @@
 #include "llvm/CodeGen/MachineCopyPropagation.h"
 #include "llvm/CodeGen/MachineDominators.h"
 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
+#include "llvm/CodeGen/MachineInstrBundle.h"
 #include "llvm/CodeGen/MachineLICM.h"
 #include "llvm/CodeGen/MachineLateInstrsCleanup.h"
 #include "llvm/CodeGen/MachinePassManager.h"
diff --git a/llvm/test/CodeGen/AMDGPU/finalize-bundle.mir b/llvm/test/CodeGen/AMDGPU/finalizebundle.mir
similarity index 91%
rename from llvm/test/CodeGen/AMDGPU/finalize-bundle.mir
rename to llvm/test/CodeGen/AMDGPU/finalizebundle.mir
index be25e35765f67..ea1ae04a63259 100644
--- a/llvm/test/CodeGen/AMDGPU/finalize-bundle.mir
+++ b/llvm/test/CodeGen/AMDGPU/finalizebundle.mir
@@ -1,5 +1,5 @@
 # NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
-# RUN: llc -mtriple=amdgcn -mcpu=gfx1200 -run-pass finalizebundle-test %s -o - | FileCheck %s
+# RUN: llc -mtriple=amdgcn -mcpu=gfx1200 -passes=finalizebundle-test %s -o - | FileCheck %s
 
 ---
 name: test_overlap



More information about the llvm-commits mailing list