[llvm] [GlobalISel] Provide a fast path for ResetMachineFunctionPass when the function is empty (PR #177341)

Ryan Cowan via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 28 03:50:37 PST 2026


https://github.com/HolyMolyCowMan updated https://github.com/llvm/llvm-project/pull/177341

>From aa4fcd959ea4740c19192a7d48a62134a2791223 Mon Sep 17 00:00:00 2001
From: Ryan Cowan <ryan.cowan at arm.com>
Date: Thu, 22 Jan 2026 10:49:03 +0000
Subject: [PATCH 1/2] [GlobalISel] Provide a fast path for
 ResetMachineFunctionPass when the function is empty

---
 llvm/lib/CodeGen/ResetMachineFunctionPass.cpp | 36 ++++++++++++-------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp b/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
index 8739643e25adf..f060e6b940d49 100644
--- a/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
+++ b/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
@@ -60,26 +60,38 @@ namespace {
       llvm::scope_exit ClearVRegTypesOnReturn(
           [&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
 
-      if (MF.getProperties().hasFailedISel()) {
-        if (AbortOnFailedISel)
-          report_fatal_error("Instruction selection failed");
-        LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
-        ++NumFunctionsReset;
+      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.reset();
+        Props.setIsSSA();
+        Props.setTracksLiveness();
+      } 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;
+      if (EmitFallbackDiag) {
+        const Function &F = MF.getFunction();
+        DiagnosticInfoISelFallback DiagFallback(F);
+        F.getContext().diagnose(DiagFallback);
       }
-      return false;
+      return true;
     }
 
   };

>From 4c5bf9952f2a34b39b2b34079df07f266d121eaa Mon Sep 17 00:00:00 2001
From: Ryan Cowan <ryan.cowan at arm.com>
Date: Wed, 28 Jan 2026 11:49:20 +0000
Subject: [PATCH 2/2] Create new reset function in MachineFunctionProperties

---
 llvm/include/llvm/CodeGen/MachineFunction.h   | 8 ++++++++
 llvm/lib/CodeGen/ResetMachineFunctionPass.cpp | 4 +---
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineFunction.h b/llvm/include/llvm/CodeGen/MachineFunction.h
index c22a0101ef478..edb8963ce42b6 100644
--- a/llvm/include/llvm/CodeGen/MachineFunction.h
+++ b/llvm/include/llvm/CodeGen/MachineFunction.h
@@ -239,6 +239,14 @@ class MachineFunctionProperties {
     return *this;
   }
 
+  /// Reset all properties and re-establish baseline invariants.
+  MachineFunctionProperties &resetToInitial() {
+    reset();
+    setIsSSA();
+    setTracksLiveness();
+    return *this;
+  }
+
   MachineFunctionProperties &set(const MachineFunctionProperties &MFP) {
     Properties |= MFP.Properties;
     return *this;
diff --git a/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp b/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
index f060e6b940d49..14c3cc2ebe354 100644
--- a/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
+++ b/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
@@ -74,9 +74,7 @@ namespace {
         // 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.reset();
-        Props.setIsSSA();
-        Props.setTracksLiveness();
+        Props.resetToInitial();
       } else {
         MF.reset();
         MF.initTargetMachineFunctionInfo(MF.getSubtarget());



More information about the llvm-commits mailing list