[llvm] [AMDGPU] Optimize PreRARematStage candidate initialization (PR #220924)

Frederik Harwath via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 15 01:54:15 PDT 2026


================
@@ -1592,12 +1597,42 @@ bool PreRARematStage::initGCNSchedStage() {
                      }))
       continue;
 
-    MarkedRegs.insert(CandReg.getDefReg());
-    ScoredRemat &Cand = Candidates.emplace_back();
-    Cand.init(RegIdx, FreqInfo, Remater, DAG);
+    Register DefReg = CandReg.getDefReg();
+    MarkedRegs.insert(DefReg);
+    DefRegToCandIdx.grow(DefReg);
+    DefRegToCandIdx[DefReg] = Candidates.size();
+    Candidates.emplace_back(RegIdx, NumRegions);
+  }
+
+  // Initialize the LiveIn and LiveOut sets of all candidates.
+  // Iterating all regions and their live regs once is considerably
+  // more efficient than querying those structures for each candidate
+  // separately in ScoredRemat::init.
+  for (unsigned I = 0; I < NumRegions; ++I) {
+    for (const auto &[Reg, Mask] : DAG.LiveIns[I]) {
+      if (DefRegToCandIdx.inBounds(Reg)) {
----------------
frederik-h wrote:

> I think this should just be a Reg.isVirtual() check since VirtReg2IndexFunctor will assert on a physical register passed to the IndexedMap. Same in the live-out loop below.
Yes, but only If I `resize ``DefRegToCandIdx` to `DAG.MRI.getNumVirtRegs()` initially instead of only calling `reserve`. I changed the PR in this way.

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


More information about the llvm-commits mailing list