[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
Thu Jan 22 03:04:34 PST 2026


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

As part of https://github.com/llvm/llvm-project/pull/174746 I encountered a compile time regression due to ResetMachineFunction performing full resets on empty functions. In normal operation, this behaviour is not likely to impact compile time, as the pass is only inserted when using GlobalISel.

https://github.com/llvm/llvm-project/pull/174746 includes GlobalISel passes in the SDAG pipeline (only on AArch64) and skips them if a given function is not optnone surfacing this. By checking if the MachineFunction is empty we can perform a more lightweight reset that just sets the required flags, reducing the impact of this change.

>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] [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;
     }
 
   };



More information about the llvm-commits mailing list