[llvm] [CodeGen][NPM] Port ResetMachineFunction to NPM (PR #222886)
Vikram Hegde via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 16 19:35:57 PDT 2026
https://github.com/vikramRH updated https://github.com/llvm/llvm-project/pull/222886
>From df4c21b054e67fe58b0212e10fefc98a96066838 Mon Sep 17 00:00:00 2001
From: vikhegde <vikram.hegde at amd.com>
Date: Fri, 11 Sep 2026 13:37:05 +0530
Subject: [PATCH 1/2] [CodeGen][NPM] Port ResetMachineFunction to NPM
---
llvm/include/llvm/CodeGen/Passes.h | 3 +-
.../llvm/CodeGen/ResetMachineFunctionPass.h | 36 +++++
llvm/include/llvm/InitializePasses.h | 1 +
.../llvm/Passes/MachinePassRegistry.def | 2 +-
llvm/lib/CodeGen/ResetMachineFunctionPass.cpp | 147 ++++++++++--------
llvm/lib/CodeGen/TargetPassConfig.cpp | 2 +-
llvm/lib/Passes/CodeGenPassBuilder.cpp | 1 +
llvm/lib/Passes/PassBuilder.cpp | 1 +
llvm/test/CodeGen/AMDGPU/llc-pipeline-npm.ll | 6 +-
9 files changed, 127 insertions(+), 72 deletions(-)
create mode 100644 llvm/include/llvm/CodeGen/ResetMachineFunctionPass.h
diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index 9b41b6b56600a..b63c0712b0003 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -122,7 +122,8 @@ LLVM_ABI MachineFunctionPass *createPrintMIRPass(raw_ostream &OS);
/// DiagnosticInfoISelFallback for every MachineFunction it resets.
/// If AbortOnFailedISel is true, abort compilation instead of resetting.
LLVM_ABI MachineFunctionPass *
-createResetMachineFunctionPass(bool EmitFallbackDiag, bool AbortOnFailedISel);
+createResetMachineFunctionLegacyPass(bool EmitFallbackDiag,
+ bool AbortOnFailedISel);
/// createCodeGenPrepareLegacyPass - Transform the code to expose more pattern
/// matching during instruction selection.
diff --git a/llvm/include/llvm/CodeGen/ResetMachineFunctionPass.h b/llvm/include/llvm/CodeGen/ResetMachineFunctionPass.h
new file mode 100644
index 0000000000000..e9b223093fe88
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/ResetMachineFunctionPass.h
@@ -0,0 +1,36 @@
+//===-- llvm/CodeGen/ResetMachineFunctionPass.h -----------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_RESETMACHINEFUNCTIONPASS_H
+#define LLVM_CODEGEN_RESETMACHINEFUNCTIONPASS_H
+
+#include "llvm/CodeGen/MachinePassManager.h"
+
+namespace llvm {
+
+class ResetMachineFunctionPass
+ : public RequiredPassInfoMixin<ResetMachineFunctionPass> {
+ /// Tells whether or not this pass should emit a fallback
+ /// diagnostic when it resets a function.
+ bool EmitFallbackDiag;
+ /// Whether we should abort immediately instead of resetting the function.
+ bool AbortOnFailedISel;
+
+public:
+ ResetMachineFunctionPass(bool EmitFallbackDiag = false,
+ bool AbortOnFailedISel = false)
+ : EmitFallbackDiag(EmitFallbackDiag),
+ AbortOnFailedISel(AbortOnFailedISel) {}
+
+ LLVM_ABI PreservedAnalyses run(MachineFunction &MF,
+ MachineFunctionAnalysisManager &);
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_RESETMACHINEFUNCTIONPASS_H
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 925b2aef4b983..d3f7f50673336 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -287,6 +287,7 @@ LLVM_ABI void initializeRemoveLoadsIntoFakeUsesLegacyPass(PassRegistry &);
LLVM_ABI void initializeRemoveRedundantDebugValuesLegacyPass(PassRegistry &);
LLVM_ABI void initializeRenameIndependentSubregsLegacyPass(PassRegistry &);
LLVM_ABI void initializeReplaceWithVeclibLegacyPass(PassRegistry &);
+LLVM_ABI void initializeResetMachineFunctionLegacyPass(PassRegistry &);
LLVM_ABI void initializeRuntimeLibraryInfoWrapperPass(PassRegistry &);
LLVM_ABI void initializeSCEVAAWrapperPassPass(PassRegistry &);
LLVM_ABI void initializeSROALegacyPassPass(PassRegistry &);
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 78253532a8570..7747bce185cbe 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -144,6 +144,7 @@ MACHINE_FUNCTION_PASS("register-coalescer", RegisterCoalescerPass())
MACHINE_FUNCTION_PASS("rename-independent-subregs", RenameIndependentSubregsPass())
MACHINE_FUNCTION_PASS("remove-loads-into-fake-uses", RemoveLoadsIntoFakeUsesPass())
MACHINE_FUNCTION_PASS("remove-redundant-debug-values", RemoveRedundantDebugValuesPass())
+MACHINE_FUNCTION_PASS("reset-machine-function", ResetMachineFunctionPass())
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
RequireAllMachineFunctionPropertiesPass())
MACHINE_FUNCTION_PASS("shrink-wrap", ShrinkWrapPass())
@@ -260,6 +261,5 @@ DUMMY_MACHINE_FUNCTION_PASS("ra-basic", RABasicPass)
DUMMY_MACHINE_FUNCTION_PASS("ra-pbqp", RAPBQPPass)
DUMMY_MACHINE_FUNCTION_PASS("regalloc", RegAllocPass)
DUMMY_MACHINE_FUNCTION_PASS("regallocscoringpass", RegAllocScoringPass)
-DUMMY_MACHINE_FUNCTION_PASS("reset-machine-function", ResetMachineFunctionPass)
DUMMY_MACHINE_FUNCTION_PASS("stackmap-liveness", StackMapLivenessPass)
#undef DUMMY_MACHINE_FUNCTION_PASS
diff --git a/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp b/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
index 7bcda2028a8de..6a560be5df5a1 100644
--- a/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
+++ b/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
@@ -12,6 +12,7 @@
/// happen is that the MachineFunction has the FailedISel property.
//===----------------------------------------------------------------------===//
+#include "llvm/CodeGen/ResetMachineFunctionPass.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFunction.h"
@@ -30,75 +31,89 @@ using namespace llvm;
STATISTIC(NumFunctionsReset, "Number of functions reset");
STATISTIC(NumFunctionsVisited, "Number of functions visited");
+static bool runImpl(MachineFunction &MF, bool EmitFallbackDiag,
+ bool AbortOnFailedISel) {
+ ++NumFunctionsVisited;
+ // No matter what happened, whether we successfully selected the function
+ // or not, nothing is going to use the vreg types after us. Make sure they
+ // disappear.
+ llvm::scope_exit ClearVRegTypesOnReturn(
+ [&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
+
+ if (!MF.getProperties().hasFailedISel())
+ return false;
+
+ if (AbortOnFailedISel)
+ report_fatal_error("Instruction selection failed");
+
+ LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
+ ++NumFunctionsReset;
+
+ if (MF.empty()) {
+ // Nothing was materialized in the MachineFunction, so avoid the cost of
+ // tearing down and rebuilding all of the per-function state. Just clear
+ // the FailedISel bit so the SelectionDAG pipeline can proceed.
+ auto &Props = MF.getProperties();
+ Props.resetToInitial();
+ } else {
+ MF.reset();
+ MF.initTargetMachineFunctionInfo(MF.getSubtarget());
+
+ const TargetMachine &TM = MF.getTarget();
+ // MRI callback for target specific initializations.
+ TM.registerMachineRegisterInfoCallback(MF);
+ }
+
+ if (EmitFallbackDiag) {
+ const Function &F = MF.getFunction();
+ DiagnosticInfoISelFallback DiagFallback(F);
+ F.getContext().diagnose(DiagFallback);
+ }
+ return true;
+}
+
namespace {
- class ResetMachineFunction : public MachineFunctionPass {
- /// Tells whether or not this pass should emit a fallback
- /// diagnostic when it resets a function.
- bool EmitFallbackDiag;
- /// Whether we should abort immediately instead of resetting the function.
- bool AbortOnFailedISel;
-
- public:
- static char ID; // Pass identification, replacement for typeid
- ResetMachineFunction(bool EmitFallbackDiag = false,
- bool AbortOnFailedISel = false)
- : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
- AbortOnFailedISel(AbortOnFailedISel) {}
-
- StringRef getPassName() const override { return "ResetMachineFunction"; }
-
- void getAnalysisUsage(AnalysisUsage &AU) const override {
- AU.addPreserved<StackProtector>();
- MachineFunctionPass::getAnalysisUsage(AU);
- }
-
- bool runOnMachineFunction(MachineFunction &MF) override {
- ++NumFunctionsVisited;
- // No matter what happened, whether we successfully selected the function
- // or not, nothing is going to use the vreg types after us. Make sure they
- // disappear.
- llvm::scope_exit ClearVRegTypesOnReturn(
- [&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
-
- if (!MF.getProperties().hasFailedISel())
- return false;
-
- if (AbortOnFailedISel)
- report_fatal_error("Instruction selection failed");
-
- LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
- ++NumFunctionsReset;
-
- if (MF.empty()) {
- // Nothing was materialized in the MachineFunction, so avoid the cost of
- // tearing down and rebuilding all of the per-function state. Just clear
- // the FailedISel bit so the SelectionDAG pipeline can proceed.
- auto &Props = MF.getProperties();
- Props.resetToInitial();
- } else {
- MF.reset();
- MF.initTargetMachineFunctionInfo(MF.getSubtarget());
-
- const TargetMachine &TM = MF.getTarget();
- // MRI callback for target specific initializations.
- TM.registerMachineRegisterInfoCallback(MF);
- }
-
- if (EmitFallbackDiag) {
- const Function &F = MF.getFunction();
- DiagnosticInfoISelFallback DiagFallback(F);
- F.getContext().diagnose(DiagFallback);
- }
- return true;
- }
-
- };
+class ResetMachineFunctionLegacy : public MachineFunctionPass {
+ /// Tells whether or not this pass should emit a fallback
+ /// diagnostic when it resets a function.
+ bool EmitFallbackDiag;
+ /// Whether we should abort immediately instead of resetting the function.
+ bool AbortOnFailedISel;
+
+public:
+ static char ID; // Pass identification, replacement for typeid
+ ResetMachineFunctionLegacy(bool EmitFallbackDiag = false,
+ bool AbortOnFailedISel = false)
+ : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
+ AbortOnFailedISel(AbortOnFailedISel) {}
+
+ StringRef getPassName() const override { return "ResetMachineFunction"; }
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ AU.addPreserved<StackProtector>();
+ MachineFunctionPass::getAnalysisUsage(AU);
+ }
+
+ bool runOnMachineFunction(MachineFunction &MF) override {
+ return runImpl(MF, EmitFallbackDiag, AbortOnFailedISel);
+ }
+};
} // end anonymous namespace
-char ResetMachineFunction::ID = 0;
+char ResetMachineFunctionLegacy::ID = 0;
+INITIALIZE_PASS(ResetMachineFunctionLegacy, DEBUG_TYPE,
+ "Reset machine function if ISel failed", false, false)
MachineFunctionPass *
-llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
- bool AbortOnFailedISel = false) {
- return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
+llvm::createResetMachineFunctionLegacyPass(bool EmitFallbackDiag = false,
+ bool AbortOnFailedISel = false) {
+ return new ResetMachineFunctionLegacy(EmitFallbackDiag, AbortOnFailedISel);
+}
+
+PreservedAnalyses
+ResetMachineFunctionPass::run(MachineFunction &MF,
+ MachineFunctionAnalysisManager &) {
+ if (runImpl(MF, EmitFallbackDiag, AbortOnFailedISel))
+ return PreservedAnalyses::none();
+ return PreservedAnalyses::all();
}
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index e6c6cf542da35..bda74e0d6461b 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -1075,7 +1075,7 @@ bool TargetPassConfig::addCoreISelPasses() {
// Pass to reset the MachineFunction if the ISel failed. Outside of the above
// if so that the verifier is not added to it.
if (Selector == SelectorType::GlobalISel)
- addPass(createResetMachineFunctionPass(
+ addPass(createResetMachineFunctionLegacyPass(
reportDiagnosticWhenGlobalISelFallback(), isGlobalISelAbortEnabled()));
// Run the SDAG InstSelector, providing a fallback path when we do not want to
diff --git a/llvm/lib/Passes/CodeGenPassBuilder.cpp b/llvm/lib/Passes/CodeGenPassBuilder.cpp
index eb550cc276087..c68de7efa1842 100644
--- a/llvm/lib/Passes/CodeGenPassBuilder.cpp
+++ b/llvm/lib/Passes/CodeGenPassBuilder.cpp
@@ -78,6 +78,7 @@
#include "llvm/CodeGen/RemoveRedundantDebugValues.h"
#include "llvm/CodeGen/RenameIndependentSubregs.h"
#include "llvm/CodeGen/ReplaceWithVeclib.h"
+#include "llvm/CodeGen/ResetMachineFunctionPass.h"
#include "llvm/CodeGen/SafeStack.h"
#include "llvm/CodeGen/SanitizerBinaryMetadata.h"
#include "llvm/CodeGen/SelectOptimize.h"
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 6368a9012a87b..858c76706427e 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -185,6 +185,7 @@
#include "llvm/CodeGen/RemoveRedundantDebugValues.h"
#include "llvm/CodeGen/RenameIndependentSubregs.h"
#include "llvm/CodeGen/ReplaceWithVeclib.h"
+#include "llvm/CodeGen/ResetMachineFunctionPass.h"
#include "llvm/CodeGen/SafeStack.h"
#include "llvm/CodeGen/SanitizerBinaryMetadata.h"
#include "llvm/CodeGen/SelectOptimize.h"
diff --git a/llvm/test/CodeGen/AMDGPU/llc-pipeline-npm.ll b/llvm/test/CodeGen/AMDGPU/llc-pipeline-npm.ll
index e41b8ec122b5b..97f6b7537658f 100644
--- a/llvm/test/CodeGen/AMDGPU/llc-pipeline-npm.ll
+++ b/llvm/test/CodeGen/AMDGPU/llc-pipeline-npm.ll
@@ -175,7 +175,7 @@
; GCN-O0-GISEL-NEXT: amdgpu-reg-bank-legalize
; GCN-O0-GISEL-NEXT: amdgpu-regbank-combiner
; GCN-O0-GISEL-NEXT: instruction-select
-; GCN-O0-GISEL-NEXT: ResetMachineFunctionPass
+; GCN-O0-GISEL-NEXT: reset-machine-function
; GCN-O0-GISEL-NEXT: finalize-isel
; GCN-O0-GISEL-NEXT: localstackalloc
; GCN-O0-GISEL-NEXT: require<reg-usage>
@@ -498,7 +498,7 @@
; GCN-O2-GISEL-NEXT: amdgpu-reg-bank-legalize
; GCN-O2-GISEL-NEXT: amdgpu-regbank-combiner
; GCN-O2-GISEL-NEXT: instruction-select
-; GCN-O2-GISEL-NEXT: ResetMachineFunctionPass
+; GCN-O2-GISEL-NEXT: reset-machine-function
; GCN-O2-GISEL-NEXT: finalize-isel
; GCN-O2-GISEL-NEXT: early-tailduplication
; GCN-O2-GISEL-NEXT: opt-phis
@@ -879,7 +879,7 @@
; GCN-O3-GISEL-NEXT: amdgpu-reg-bank-legalize
; GCN-O3-GISEL-NEXT: amdgpu-regbank-combiner
; GCN-O3-GISEL-NEXT: instruction-select
-; GCN-O3-GISEL-NEXT: ResetMachineFunctionPass
+; GCN-O3-GISEL-NEXT: reset-machine-function
; GCN-O3-GISEL-NEXT: finalize-isel
; GCN-O3-GISEL-NEXT: early-tailduplication
; GCN-O3-GISEL-NEXT: opt-phis
>From 91b1408da40a2bc4f8c18041149ac312c6b38d78 Mon Sep 17 00:00:00 2001
From: vikhegde <vikram.hegde at amd.com>
Date: Thu, 17 Sep 2026 08:04:40 +0530
Subject: [PATCH 2/2] comments
---
llvm/include/llvm/CodeGen/ResetMachineFunctionPass.h | 11 ++++++++---
llvm/lib/CodeGen/ResetMachineFunctionPass.cpp | 2 +-
2 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/ResetMachineFunctionPass.h b/llvm/include/llvm/CodeGen/ResetMachineFunctionPass.h
index e9b223093fe88..9c854c825a10a 100644
--- a/llvm/include/llvm/CodeGen/ResetMachineFunctionPass.h
+++ b/llvm/include/llvm/CodeGen/ResetMachineFunctionPass.h
@@ -1,10 +1,15 @@
-//===-- llvm/CodeGen/ResetMachineFunctionPass.h -----------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
//
// 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 ResetMachineFunctionPass class.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_CODEGEN_RESETMACHINEFUNCTIONPASS_H
#define LLVM_CODEGEN_RESETMACHINEFUNCTIONPASS_H
@@ -17,9 +22,9 @@ class ResetMachineFunctionPass
: public RequiredPassInfoMixin<ResetMachineFunctionPass> {
/// Tells whether or not this pass should emit a fallback
/// diagnostic when it resets a function.
- bool EmitFallbackDiag;
+ bool EmitFallbackDiag = false;
/// Whether we should abort immediately instead of resetting the function.
- bool AbortOnFailedISel;
+ bool AbortOnFailedISel = false;
public:
ResetMachineFunctionPass(bool EmitFallbackDiag = false,
diff --git a/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp b/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
index 6a560be5df5a1..eb467ddad7fe0 100644
--- a/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
+++ b/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
@@ -114,6 +114,6 @@ PreservedAnalyses
ResetMachineFunctionPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &) {
if (runImpl(MF, EmitFallbackDiag, AbortOnFailedISel))
- return PreservedAnalyses::none();
+ return getMachineFunctionPassPreservedAnalyses();
return PreservedAnalyses::all();
}
More information about the llvm-commits
mailing list