[llvm] [CodeGen][NPM] Port ResetMachineFunction to NPM (PR #222886)
Vikram Hegde via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 11 01:58:16 PDT 2026
https://github.com/vikramRH updated https://github.com/llvm/llvm-project/pull/222886
>From 462dbc5528cd914d8450611d73f39fddd3cb0880 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] [CodeGen][NPM] Port ResetMachineFunction to NPM
---
llvm/include/llvm/CodeGen/Passes.h | 3 +-
.../llvm/CodeGen/ResetMachineFunctionPass.h | 36 +++++
llvm/include/llvm/InitializePasses.h | 2 +-
.../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 +
8 files changed, 123 insertions(+), 71 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 8c2b923aeb807..b1d81010f9075 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 1ba2f67d27e5d..fc8aabb498c59 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -290,7 +290,7 @@ LLVM_ABI void initializeRemoveLoadsIntoFakeUsesLegacyPass(PassRegistry &);
LLVM_ABI void initializeRemoveRedundantDebugValuesLegacyPass(PassRegistry &);
LLVM_ABI void initializeRenameIndependentSubregsLegacyPass(PassRegistry &);
LLVM_ABI void initializeReplaceWithVeclibLegacyPass(PassRegistry &);
-LLVM_ABI void initializeResetMachineFunctionPass(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 14c3cc2ebe354..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,77 +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;
-INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
+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 e390db16aa63d..1f105f604e3aa 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -1071,7 +1071,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 63e161965820e..06b43a5d84b96 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 725ce2d589a31..3766e3ea2ad88 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"
More information about the llvm-commits
mailing list