[llvm] [MachinePipeliner] Use VirtRegOrUnit instead of Register appropriately (PR #177535)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 15 07:55:08 PDT 2026


https://github.com/hjagasiaAMD updated https://github.com/llvm/llvm-project/pull/177535

>From 31d0907305d0fd28c0b465a20949a4d7ccb0c983 Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Fri, 19 Dec 2025 00:33:31 +0900
Subject: [PATCH 1/5] [MachinePipeliner] Use VirtRegOrUnit instead of Register
 appropriately

---
 llvm/lib/CodeGen/MachinePipeliner.cpp | 87 +++++++++++----------------
 1 file changed, 34 insertions(+), 53 deletions(-)

diff --git a/llvm/lib/CodeGen/MachinePipeliner.cpp b/llvm/lib/CodeGen/MachinePipeliner.cpp
index 945a10cd69842..affc40f971eff 100644
--- a/llvm/lib/CodeGen/MachinePipeliner.cpp
+++ b/llvm/lib/CodeGen/MachinePipeliner.cpp
@@ -1530,7 +1530,7 @@ class HighRegisterPressureDetector {
 
   DenseMap<MachineInstr *, RegisterOperands> ROMap;
 
-  using Instr2LastUsesTy = DenseMap<MachineInstr *, SmallDenseSet<Register, 4>>;
+  using Instr2LastUsesTy = DenseMap<MachineInstr *, SmallSet<VirtRegOrUnit, 4>>;
 
 public:
   using OrderedInstsTy = std::vector<MachineInstr *>;
@@ -1550,12 +1550,8 @@ class HighRegisterPressureDetector {
     }
   }
 
-  void dumpPSet(Register Reg) const {
-    dbgs() << "Reg=" << printReg(Reg, TRI, 0, &MRI) << " PSet=";
-    // FIXME: The static_cast is a bug compensating bugs in the callers.
-    VirtRegOrUnit VRegOrUnit =
-        Reg.isVirtual() ? VirtRegOrUnit(Reg)
-                        : VirtRegOrUnit(static_cast<MCRegUnit>(Reg.id()));
+  void dumpPSet(VirtRegOrUnit VRegOrUnit) const {
+    dbgs() << "Reg=" << printVRegOrUnit(VRegOrUnit, TRI) << " PSet=";
     for (auto PSetIter = MRI.getPressureSets(VRegOrUnit); PSetIter.isValid();
          ++PSetIter) {
       dbgs() << *PSetIter << ' ';
@@ -1564,11 +1560,7 @@ class HighRegisterPressureDetector {
   }
 
   void increaseRegisterPressure(std::vector<unsigned> &Pressure,
-                                Register Reg) const {
-    // FIXME: The static_cast is a bug compensating bugs in the callers.
-    VirtRegOrUnit VRegOrUnit =
-        Reg.isVirtual() ? VirtRegOrUnit(Reg)
-                        : VirtRegOrUnit(static_cast<MCRegUnit>(Reg.id()));
+                                VirtRegOrUnit VRegOrUnit) const {
     auto PSetIter = MRI.getPressureSets(VRegOrUnit);
     unsigned Weight = PSetIter.getWeight();
     for (; PSetIter.isValid(); ++PSetIter)
@@ -1576,8 +1568,8 @@ class HighRegisterPressureDetector {
   }
 
   void decreaseRegisterPressure(std::vector<unsigned> &Pressure,
-                                Register Reg) const {
-    auto PSetIter = MRI.getPressureSets(VirtRegOrUnit(Reg));
+                                VirtRegOrUnit VRegOrUnit) const {
+    auto PSetIter = MRI.getPressureSets(VRegOrUnit);
     unsigned Weight = PSetIter.getWeight();
     for (; PSetIter.isValid(); ++PSetIter) {
       auto &P = Pressure[*PSetIter];
@@ -1588,12 +1580,13 @@ class HighRegisterPressureDetector {
   }
 
   // Return true if Reg is reserved one, for example, stack pointer
-  bool isReservedRegister(Register Reg) const {
-    return Reg.isPhysical() && MRI.isReserved(Reg.asMCReg());
+  bool isReservedRegister(VirtRegOrUnit Reg) const {
+    return !Reg.isVirtualReg() && MRI.isReservedRegUnit(Reg.asMCRegUnit());
   }
 
-  bool isDefinedInThisLoop(Register Reg) const {
-    return Reg.isVirtual() && MRI.getVRegDef(Reg)->getParent() == OrigMBB;
+  bool isDefinedInThisLoop(VirtRegOrUnit Reg) const {
+    return Reg.isVirtualReg() &&
+           MRI.getVRegDef(Reg.asVirtualReg())->getParent() == OrigMBB;
   }
 
   // Search for live-in variables. They are factored into the register pressure
@@ -1605,19 +1598,16 @@ class HighRegisterPressureDetector {
   //     a[i] += b[i] + c;
   // \endcode
   void computeLiveIn() {
-    DenseSet<Register> Used;
+    SmallSet<VirtRegOrUnit, 8> Used;
     for (auto &MI : *OrigMBB) {
       if (MI.isDebugInstr())
         continue;
       for (auto &Use : ROMap[&MI].Uses) {
-        // FIXME: The static_cast is a bug.
-        Register Reg =
-            Use.VRegOrUnit.isVirtualReg()
-                ? Use.VRegOrUnit.asVirtualReg()
-                : Register(static_cast<unsigned>(Use.VRegOrUnit.asMCRegUnit()));
+        VirtRegOrUnit Reg = Use.VRegOrUnit;
         // Ignore the variable that appears only on one side of phi instruction
         // because it's used only at the first iteration.
-        if (MI.isPHI() && Reg != getLoopPhiReg(MI, OrigMBB))
+        if (MI.isPHI() && Reg.isVirtualReg() &&
+            Reg.asVirtualReg() != getLoopPhiReg(MI, OrigMBB))
           continue;
         if (isReservedRegister(Reg))
           continue;
@@ -1654,23 +1644,23 @@ class HighRegisterPressureDetector {
     // Following virtual register will be ignored
     //   - live-in one
     //   - defined but not used in the loop (potentially live-out)
-    DenseSet<Register> TargetRegs;
-    const auto UpdateTargetRegs = [this, &TargetRegs](Register Reg) {
+    SmallSet<VirtRegOrUnit, 8> TargetRegs;
+    const auto UpdateTargetRegs = [this, &TargetRegs](VirtRegOrUnit Reg) {
       if (isDefinedInThisLoop(Reg))
         TargetRegs.insert(Reg);
     };
     for (MachineInstr *MI : OrderedInsts) {
       if (MI->isPHI()) {
         Register Reg = getLoopPhiReg(*MI, OrigMBB);
-        UpdateTargetRegs(Reg);
+        if (Reg.isVirtual()) {
+          UpdateTargetRegs(VirtRegOrUnit(Reg));
+        } else if (MRI.isAllocatable(Reg)) {
+          for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg()))
+            UpdateTargetRegs(VirtRegOrUnit(Unit));
+        }
       } else {
         for (auto &Use : ROMap.find(MI)->getSecond().Uses) {
-          // FIXME: The static_cast is a bug.
-          Register Reg = Use.VRegOrUnit.isVirtualReg()
-                             ? Use.VRegOrUnit.asVirtualReg()
-                             : Register(static_cast<unsigned>(
-                                   Use.VRegOrUnit.asMCRegUnit()));
-          UpdateTargetRegs(Reg);
+          UpdateTargetRegs(Use.VRegOrUnit);
         }
       }
     }
@@ -1679,15 +1669,11 @@ class HighRegisterPressureDetector {
       return Stages[MI] + MI->isPHI();
     };
 
-    DenseMap<Register, MachineInstr *> LastUseMI;
+    std::map<VirtRegOrUnit, MachineInstr *> LastUseMI;
     for (MachineInstr *MI : llvm::reverse(OrderedInsts)) {
       for (auto &Use : ROMap.find(MI)->getSecond().Uses) {
-        // FIXME: The static_cast is a bug.
-        Register Reg =
-            Use.VRegOrUnit.isVirtualReg()
-                ? Use.VRegOrUnit.asVirtualReg()
-                : Register(static_cast<unsigned>(Use.VRegOrUnit.asMCRegUnit()));
-        if (!TargetRegs.contains(Reg))
+        VirtRegOrUnit Reg = Use.VRegOrUnit;
+        if (!TargetRegs.contains(Use.VRegOrUnit))
           continue;
         auto [Ite, Inserted] = LastUseMI.try_emplace(Reg, MI);
         if (!Inserted) {
@@ -1721,7 +1707,7 @@ class HighRegisterPressureDetector {
   computeMaxSetPressure(const OrderedInstsTy &OrderedInsts,
                         Instr2StageTy &Stages,
                         const unsigned StageCount) const {
-    using RegSetTy = SmallDenseSet<Register, 16>;
+    using RegSetTy = SmallSet<VirtRegOrUnit, 16>;
 
     // Indexed by #Iter. To treat "local" variables of each stage separately, we
     // manage the liveness of the registers independently by iterations.
@@ -1740,34 +1726,29 @@ class HighRegisterPressureDetector {
     });
 
     const auto InsertReg = [this, &CurSetPressure](RegSetTy &RegSet,
-                                                   VirtRegOrUnit VRegOrUnit) {
-      // FIXME: The static_cast is a bug.
-      Register Reg =
-          VRegOrUnit.isVirtualReg()
-              ? VRegOrUnit.asVirtualReg()
-              : Register(static_cast<unsigned>(VRegOrUnit.asMCRegUnit()));
-      if (!Reg.isValid() || isReservedRegister(Reg))
+                                                   VirtRegOrUnit Reg) {
+      if (isReservedRegister(Reg))
         return;
 
       bool Inserted = RegSet.insert(Reg).second;
       if (!Inserted)
         return;
 
-      LLVM_DEBUG(dbgs() << "insert " << printReg(Reg, TRI, 0, &MRI) << "\n");
+      LLVM_DEBUG(dbgs() << "insert " << printVRegOrUnit(Reg, TRI) << "\n");
       increaseRegisterPressure(CurSetPressure, Reg);
       LLVM_DEBUG(dumpPSet(Reg));
     };
 
     const auto EraseReg = [this, &CurSetPressure](RegSetTy &RegSet,
-                                                  Register Reg) {
-      if (!Reg.isValid() || isReservedRegister(Reg))
+                                                  VirtRegOrUnit Reg) {
+      if (isReservedRegister(Reg))
         return;
 
       // live-in register
       if (!RegSet.contains(Reg))
         return;
 
-      LLVM_DEBUG(dbgs() << "erase " << printReg(Reg, TRI, 0, &MRI) << "\n");
+      LLVM_DEBUG(dbgs() << "erase " << printVRegOrUnit(Reg, TRI) << "\n");
       RegSet.erase(Reg);
       decreaseRegisterPressure(CurSetPressure, Reg);
       LLVM_DEBUG(dumpPSet(Reg));

>From b957cc36503d58a8fcf4310c84e5b19bf2c7da7b Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Fri, 23 Jan 2026 09:51:52 +0000
Subject: [PATCH 2/5] address review comments

---
 llvm/lib/CodeGen/MachinePipeliner.cpp | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/CodeGen/MachinePipeliner.cpp b/llvm/lib/CodeGen/MachinePipeliner.cpp
index affc40f971eff..bb827f76d36d2 100644
--- a/llvm/lib/CodeGen/MachinePipeliner.cpp
+++ b/llvm/lib/CodeGen/MachinePipeliner.cpp
@@ -1579,9 +1579,10 @@ class HighRegisterPressureDetector {
     }
   }
 
-  // Return true if Reg is reserved one, for example, stack pointer
-  bool isReservedRegister(VirtRegOrUnit Reg) const {
-    return !Reg.isVirtualReg() && MRI.isReservedRegUnit(Reg.asMCRegUnit());
+  // Return true if \p VRegOrUnit is reserved one, for example, stack pointer
+  bool isReservedRegUnit(VirtRegOrUnit VRegOrUnit) const {
+    return !VRegOrUnit.isVirtualReg() &&
+           MRI.isReservedRegUnit(VRegOrUnit.asMCRegUnit());
   }
 
   bool isDefinedInThisLoop(VirtRegOrUnit Reg) const {
@@ -1609,7 +1610,7 @@ class HighRegisterPressureDetector {
         if (MI.isPHI() && Reg.isVirtualReg() &&
             Reg.asVirtualReg() != getLoopPhiReg(MI, OrigMBB))
           continue;
-        if (isReservedRegister(Reg))
+        if (isReservedRegUnit(Reg))
           continue;
         if (isDefinedInThisLoop(Reg))
           continue;
@@ -1652,12 +1653,7 @@ class HighRegisterPressureDetector {
     for (MachineInstr *MI : OrderedInsts) {
       if (MI->isPHI()) {
         Register Reg = getLoopPhiReg(*MI, OrigMBB);
-        if (Reg.isVirtual()) {
-          UpdateTargetRegs(VirtRegOrUnit(Reg));
-        } else if (MRI.isAllocatable(Reg)) {
-          for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg()))
-            UpdateTargetRegs(VirtRegOrUnit(Unit));
-        }
+        UpdateTargetRegs(VirtRegOrUnit(Reg));
       } else {
         for (auto &Use : ROMap.find(MI)->getSecond().Uses) {
           UpdateTargetRegs(Use.VRegOrUnit);
@@ -1727,7 +1723,7 @@ class HighRegisterPressureDetector {
 
     const auto InsertReg = [this, &CurSetPressure](RegSetTy &RegSet,
                                                    VirtRegOrUnit Reg) {
-      if (isReservedRegister(Reg))
+      if (isReservedRegUnit(Reg))
         return;
 
       bool Inserted = RegSet.insert(Reg).second;
@@ -1741,7 +1737,7 @@ class HighRegisterPressureDetector {
 
     const auto EraseReg = [this, &CurSetPressure](RegSetTy &RegSet,
                                                   VirtRegOrUnit Reg) {
-      if (isReservedRegister(Reg))
+      if (isReservedRegUnit(Reg))
         return;
 
       // live-in register

>From 07ae81836a17b1db3f092b9ea7c3d5ad01399071 Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Tue, 1 Sep 2026 21:23:25 +0900
Subject: [PATCH 3/5] rename

---
 llvm/lib/CodeGen/MachinePipeliner.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/MachinePipeliner.cpp b/llvm/lib/CodeGen/MachinePipeliner.cpp
index e0721cf1ecfe2..ff0fe6e21387d 100644
--- a/llvm/lib/CodeGen/MachinePipeliner.cpp
+++ b/llvm/lib/CodeGen/MachinePipeliner.cpp
@@ -1644,9 +1644,9 @@ class HighRegisterPressureDetector {
            MRI.isReservedRegUnit(VRegOrUnit.asMCRegUnit());
   }
 
-  bool isDefinedInThisLoop(VirtRegOrUnit Reg) const {
-    return Reg.isVirtualReg() &&
-           MRI.getDefBlock(Reg.asVirtualReg()) == OrigMBB;
+  bool isDefinedInThisLoop(VirtRegOrUnit VRegOrUnit) const {
+    return VRegOrUnit.isVirtualReg() &&
+           MRI.getDefBlock(VRegOrUnit.asVirtualReg()) == OrigMBB;
   }
 
   // Search for live-in variables. They are factored into the register pressure

>From 89385001e7236f012e1ab41231b98130c023ae5e Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Tue, 1 Sep 2026 21:34:47 +0900
Subject: [PATCH 4/5] address review

---
 llvm/lib/CodeGen/MachinePipeliner.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/CodeGen/MachinePipeliner.cpp b/llvm/lib/CodeGen/MachinePipeliner.cpp
index ff0fe6e21387d..96eb5e755d61e 100644
--- a/llvm/lib/CodeGen/MachinePipeliner.cpp
+++ b/llvm/lib/CodeGen/MachinePipeliner.cpp
@@ -1638,7 +1638,7 @@ class HighRegisterPressureDetector {
     }
   }
 
-  // Return true if \p VRegOrUnit is reserved one, for example, stack pointer
+  /// Return true if \p VRegOrUnit is reserved one, for example, stack pointer
   bool isReservedRegUnit(VirtRegOrUnit VRegOrUnit) const {
     return !VRegOrUnit.isVirtualReg() &&
            MRI.isReservedRegUnit(VRegOrUnit.asMCRegUnit());
@@ -1714,9 +1714,8 @@ class HighRegisterPressureDetector {
         Register Reg = getLoopPhiReg(*MI, OrigMBB);
         UpdateTargetRegs(VirtRegOrUnit(Reg));
       } else {
-        for (auto &Use : ROMap.find(MI)->getSecond().Uses) {
+        for (auto &Use : ROMap.find(MI)->getSecond().Uses)
           UpdateTargetRegs(Use.VRegOrUnit);
-        }
       }
     }
 
@@ -1728,7 +1727,7 @@ class HighRegisterPressureDetector {
     for (MachineInstr *MI : llvm::reverse(OrderedInsts)) {
       for (auto &Use : ROMap.find(MI)->getSecond().Uses) {
         VirtRegOrUnit Reg = Use.VRegOrUnit;
-        if (!TargetRegs.contains(Use.VRegOrUnit))
+        if (!TargetRegs.contains(Reg))
           continue;
         auto [Ite, Inserted] = LastUseMI.try_emplace(Reg, MI);
         if (!Inserted) {

>From 7a463428af1c7e2a1e647eb9bceaeeb3d0734e8b Mon Sep 17 00:00:00 2001
From: Harsha Jagasia <harsha.jagasia at amd.com>
Date: Tue, 15 Sep 2026 14:42:28 +0000
Subject: [PATCH 5/5] [MachinePipeliner] Add test for reserved register-unit ID
 collision

PowerPC register unit 19 is F3, but physical register 19 is the reserved
base pointer $bp8. Converting the unit ID to a Register made the register
pressure detector treat F3 as reserved and drop it from pressure accounting
across the 11 pressure sets it belongs to. The under-count caused a schedule
to be accepted at II=26 that exceeds the VSSRC limit; with the fix the
correct II is 30.

Define $f3 in the pipelined loop so the unit is live, and check that it is
inserted into the live set.
---
 llvm/test/CodeGen/PowerPC/sms-regpress.mir | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/llvm/test/CodeGen/PowerPC/sms-regpress.mir b/llvm/test/CodeGen/PowerPC/sms-regpress.mir
index b01115c49fd8d..5e81b9d0b8ee5 100644
--- a/llvm/test/CodeGen/PowerPC/sms-regpress.mir
+++ b/llvm/test/CodeGen/PowerPC/sms-regpress.mir
@@ -5,7 +5,14 @@
 # Check that if the register pressure is too high, the schedule is rejected, II is incremented, and scheduling continues.
 # The specific value of II is not important.
 
+# Also check that a register unit is not mistaken for a physical register when
+# their numeric IDs coincide. Register unit 19 is F3, but physical register 19
+# is the reserved base pointer $bp8, so converting the unit ID to a Register
+# made the detector treat F3 as reserved and drop it from pressure accounting.
+# $f3 is defined in the loop below to keep that unit live.
+
 # CHECK: {{^ *}}Try to schedule with {{[0-9]+$}}
+# CHECK: insert F3
 # CHECK: {{^ *}}Rejected the schedule because of too high register pressure{{$}}
 # CHECK: {{^ *}}Try to schedule with {{[0-9]+$}}
 # CHECK: {{^ *}}Schedule Found? 1 (II={{[0-9]+}}){{$}}
@@ -137,7 +144,7 @@ body:             |
     %5:g8rc_and_g8rc_nox0 = PHI %2, %bb.2, %6, %bb.4
     %18:f8rc, %19:g8rc_and_g8rc_nox0 = LFDU 8, killed %5 :: (load (s64) from %ir.3)
     %6:g8rc = COPY killed %19
-    %20:vsfrc = nofpexcept XSMADDADP %0, %0, %18, implicit $rm
+    %20:vsfrc = nofpexcept XSMADDADP %0, %0, %18, implicit $rm, implicit-def $f3
     %21:vsfrc = nofpexcept XSMADDADP %20, %20, %18, implicit $rm
     %22:vsfrc = nofpexcept XSMADDADP %21, %21, %18, implicit $rm
     %23:vsfrc = nofpexcept XSMADDADP %22, %22, %18, implicit $rm



More information about the llvm-commits mailing list