[llvm] [AMDGPU][Scheduler] Scoring system for rematerialization candidates (PR #153092)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 11 22:51:35 PDT 2025


================
@@ -1940,65 +2152,80 @@ void PreRARematStage::rematerialize() {
             assert((SR.LaneMask & UncoveredLanes).none());
         }
       }
+    }
 #endif
 
-      // The register is no longer a live-in in all regions but the one that
-      // contains the single use. In live-through regions, maximum register
-      // pressure decreases predictably so we can directly update it. In the
-      // using region, maximum RP may or may not decrease, so we will mark it
-      // for re-computation after all materializations have taken place.
-      LaneBitmask PrevMask = RegionLiveIns[Reg];
-      RegionLiveIns.erase(Reg);
-      RegMasks.insert({{I, Remat.RematMI->getOperand(0).getReg()}, PrevMask});
-      if (Remat.UseMI->getParent() != DAG.Regions[I].first->getParent())
-        DAG.Pressure[I].inc(Reg, PrevMask, LaneBitmask::getNone(), DAG.MRI);
-      else
-        RecomputeRP.insert(I);
+    // This save is exact in beneficial regions but optimistic in all other
+    // regions where the register is live.
+    RPTargets[I].saveReg(Reg, Remat.Mask, DAG.MRI);
+    DAG.LiveIns[I].erase(Reg);
+    DAG.RegionLiveOuts.getLiveRegsForRegionIdx(I).erase(Reg);
+    if (!Remat.isBeneficialRegion(I))
+      RecomputeRP.set(I);
+  }
+
+  DAG.deleteMI(MIRegion.at(&DefMI), &DefMI);
+  RescheduleRegions |= Remat.Live;
+  return NewMI;
+}
+
+void PreRARematStage::rollback(const RollbackReg &Rollback) const {
+  const SIInstrInfo *TII = MF.getSubtarget<GCNSubtarget>().getInstrInfo();
+  auto &[RematMI, Remat] = Rollback;
+
+  // Recreate the original MI from the first rematerialization. Any
+  // rematerialization could do, this is just a simple way to do this.
+  unsigned DefRegion = MIRegion.at(Remat->DefMI);
+  MachineBasicBlock *MBB = RegionBB[DefRegion];
+  Register Reg = RematMI->getOperand(0).getReg();
+  const TargetRegisterClass *RC = DAG.MRI.getRegClass(Reg);
+  Register NewReg = DAG.MRI.createVirtualRegister(RC);
+
+  // Re-rematerialize MI in its original region. Note that it may not be
+  // rematerialized exactly in the same position as originally within the
+  // region, but it should not matter much.
+  MachineBasicBlock::iterator InsertPos(DAG.Regions[DefRegion].second);
+  TII->reMaterialize(*MBB, InsertPos, NewReg, 0, *RematMI, *DAG.TRI);
+  REMAT_DEBUG(dbgs() << "[" << DefRegion << "] Re-rematerialized as "
----------------
arsenm wrote:

```suggestion
  REMAT_DEBUG(dbgs() << '[' << DefRegion << "] Re-rematerialized as "
```

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


More information about the llvm-commits mailing list