Skip to content

Commit 8cc83b6

Browse files
[MLGO] Count LR Evictions Rather than Relying on Cascade (#124440)
This patch adjusts the mlregalloc-max-cascade flag (renaming it to mlregalloc-max-eviction-count) to actually count evictions rather than just looking at the cascade number. The cascade number is not very representative of how many times a LR has been evicted, which can lead to some problems in certain cases, where we might end up with many eviction problems where we have now masked off all the interferences and are forced to evict the candidate. This is probably what I should've done in the first place. No test case as this only shows up in quite large functions post ThinLTO and it would be hard to construct something that would serve as a nice regression test without being super brittle. I've tested this on the pathological cases that we have come across so far and it works. Fixes #122829
1 parent 7fd5833 commit 8cc83b6

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ static cl::opt<std::string> InteractiveChannelBaseName(
6363
"outgoing name should be "
6464
"<regalloc-evict-interactive-channel-base>.out"));
6565

66-
static cl::opt<unsigned>
67-
MaxCascade("mlregalloc-max-cascade", cl::Hidden,
68-
cl::desc("The maximum number of times a live range can be "
69-
"evicted before preventing it from being evicted"),
70-
cl::init(20));
66+
static cl::opt<unsigned> MaxEvictionCount(
67+
"mlregalloc-max-eviction-count", cl::Hidden,
68+
cl::desc("The maximum number of times a live range can be "
69+
"evicted before preventing it from being evicted"),
70+
cl::init(100));
7171

7272
// Options that only make sense in development mode
7373
#ifdef LLVM_HAVE_TFLITE
@@ -364,6 +364,22 @@ class MLEvictAdvisor : public RegAllocEvictionAdvisor {
364364

365365
using RegID = unsigned;
366366
mutable DenseMap<RegID, LIFeatureComponents> CachedFeatures;
367+
368+
mutable std::unordered_map<unsigned, unsigned> VirtRegEvictionCounts;
369+
370+
void onEviction(Register RegBeingEvicted) const {
371+
// If we cannot find the virtual register in the map, we just assume it has
372+
// not been evicted before and thus has a value of zero (which is what the
373+
// subscript operator returns by default).
374+
++VirtRegEvictionCounts[RegBeingEvicted.id()];
375+
}
376+
377+
unsigned getEvictionCount(Register Reg) const {
378+
auto EvictionCountIt = VirtRegEvictionCounts.find(Reg.id());
379+
if (EvictionCountIt != VirtRegEvictionCounts.end())
380+
return EvictionCountIt->second;
381+
return 0;
382+
}
367383
};
368384

369385
#define _DECL_FEATURES(type, name, shape, _) \
@@ -657,7 +673,7 @@ bool MLEvictAdvisor::loadInterferenceFeatures(
657673
// threshold, prevent the range from being evicted. We still let the
658674
// range through if it is urgent as we are required to produce an
659675
// eviction if the candidate is not spillable.
660-
if (IntfCascade >= MaxCascade && !Urgent)
676+
if (getEvictionCount(Intf->reg()) > MaxEvictionCount && !Urgent)
661677
return false;
662678

663679
// Only evict older cascades or live ranges without a cascade.
@@ -803,6 +819,22 @@ MCRegister MLEvictAdvisor::tryFindEvictionCandidate(
803819
}
804820
assert(CandidatePos < ValidPosLimit);
805821
(void)ValidPosLimit;
822+
823+
// Update information about how many times the virtual registers being
824+
// evicted have been evicted so that we can prevent the model from evicting
825+
// the same ranges continually and eating compile time.
826+
if (CandidatePos == CandidateVirtRegPos) {
827+
onEviction(VirtReg.reg());
828+
} else {
829+
for (MCRegUnit Unit : TRI->regunits(Regs[CandidatePos].first)) {
830+
LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, Unit);
831+
const auto &IFIntervals = Q.interferingVRegs(EvictInterferenceCutoff);
832+
for (const LiveInterval *Intf : reverse(IFIntervals)) {
833+
onEviction(Intf->reg());
834+
}
835+
}
836+
}
837+
806838
return Regs[CandidatePos].first;
807839
}
808840

0 commit comments

Comments
 (0)