[llvm] [1/3][RegAlloc][LiveRegMatrix] Fix inconsistency in HoistSpillHelper delegates (PR #197773)

Dhruva Chakrabarti via llvm-commits llvm-commits at lists.llvm.org
Fri May 22 09:06:31 PDT 2026


================
@@ -1844,12 +1874,40 @@ bool HoistSpillHelper::LRE_CanEraseVirtReg(Register VirtReg) {
 /// For VirtReg clone, the \p New register should have the same physreg or
 /// stackslot as the \p old register.
 void HoistSpillHelper::LRE_DidCloneVirtReg(Register New, Register Old) {
-  if (VRM.hasPhys(Old))
-    VRM.assignVirt2Phys(New, VRM.getPhys(Old));
-  else if (VRM.getStackSlot(Old) != VirtRegMap::NO_STACK_SLOT)
+  // New is freshly created by LiveRangeEdit::eliminateDeadDefs and its interval
+  // is guaranteed to exist on every path below.
+  assert(LIS.hasInterval(New) && "Cloned vreg without live interval");
+
+  auto PendingIt = PendingReassignments.find(Old);
+  if (PendingIt != PendingReassignments.end()) {
+    // Old was already unassigned in LRE_WillShrinkVirtReg, which only
+    // enrolls vregs when Matrix is non-null and the interval exists.
+    assert(Matrix && LIS.hasInterval(Old) &&
+           "Pending reassignment without matrix or live interval");
+    MCRegister PhysReg = PendingIt->second;
+    PendingReassignments.erase(PendingIt);
+
+    // Reassign both Old (with its shrunk interval) and New to the matrix.
+    Matrix->assign(LIS.getInterval(Old), PhysReg);
+    Matrix->assign(LIS.getInterval(New), PhysReg);
+  } else if (VRM.hasPhys(Old)) {
+    MCRegister PhysReg = VRM.getPhys(Old);
+    if (Matrix && LIS.hasInterval(Old)) {
+      const LiveInterval &LI = LIS.getInterval(Old);
+      // Drop the stale pre-clone segments before reassigning Old's current LI.
+      Matrix->unassign(LI,
+                       /*ClearAllReferencingSegments=*/true);
+      Matrix->assign(LI, PhysReg);
+    }
+    if (Matrix)
----------------
dhruvachak wrote:

Thanks, made the changes, looks cleaner now.

https://github.com/llvm/llvm-project/pull/197773


More information about the llvm-commits mailing list