Skip to content

Commit 5947599

Browse files
[MLGO] Count LR Evictions Rather than Relying on Cascade
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 c1ec5be commit 5947599

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp

Lines changed: 23 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,8 @@ class MLEvictAdvisor : public RegAllocEvictionAdvisor {
364364

365365
using RegID = unsigned;
366366
mutable DenseMap<RegID, LIFeatureComponents> CachedFeatures;
367+
368+
mutable std::unordered_map<unsigned, unsigned> VirtRegEvictionCounts;
367369
};
368370

369371
#define _DECL_FEATURES(type, name, shape, _) \
@@ -657,7 +659,7 @@ bool MLEvictAdvisor::loadInterferenceFeatures(
657659
// threshold, prevent the range from being evicted. We still let the
658660
// range through if it is urgent as we are required to produce an
659661
// eviction if the candidate is not spillable.
660-
if (IntfCascade >= MaxCascade && !Urgent)
662+
if (VirtRegEvictionCounts[Intf->reg().id()] > MaxEvictionCount && !Urgent)
661663
return false;
662664

663665
// Only evict older cascades or live ranges without a cascade.
@@ -803,6 +805,21 @@ MCRegister MLEvictAdvisor::tryFindEvictionCandidate(
803805
}
804806
assert(CandidatePos < ValidPosLimit);
805807
(void)ValidPosLimit;
808+
809+
// Update information about how many times the virtual registers being
810+
// evicted have been evicted.
811+
if (CandidatePos == CandidateVirtRegPos) {
812+
VirtRegEvictionCounts[VirtReg.reg()] += 1;
813+
} else {
814+
for (MCRegUnit Unit : TRI->regunits(Regs[CandidatePos].first)) {
815+
LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, Unit);
816+
const auto &IFIntervals = Q.interferingVRegs(EvictInterferenceCutoff);
817+
for (const LiveInterval *Intf : reverse(IFIntervals)) {
818+
VirtRegEvictionCounts[Intf->reg()] += 1;
819+
}
820+
}
821+
}
822+
806823
return Regs[CandidatePos].first;
807824
}
808825

0 commit comments

Comments
 (0)