4242#include < array>
4343#include < bitset>
4444#include < memory>
45+ #include < unordered_map>
4546
4647using namespace llvm ;
4748
@@ -63,11 +64,11 @@ static cl::opt<std::string> InteractiveChannelBaseName(
6364 " outgoing name should be "
6465 " <regalloc-evict-interactive-channel-base>.out" ));
6566
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 ));
67+ static cl::opt<unsigned > MaxEvictionCount (
68+ " mlregalloc-max-eviction-count " , cl::Hidden,
69+ cl::desc (" The maximum number of times a live range can be "
70+ " evicted before preventing it from being evicted" ),
71+ cl::init(100 ));
7172
7273// Options that only make sense in development mode
7374#ifdef LLVM_HAVE_TFLITE
@@ -364,6 +365,22 @@ class MLEvictAdvisor : public RegAllocEvictionAdvisor {
364365
365366 using RegID = unsigned ;
366367 mutable DenseMap<RegID, LIFeatureComponents> CachedFeatures;
368+
369+ mutable std::unordered_map<unsigned , unsigned > VirtRegEvictionCounts;
370+
371+ void onEviction (Register RegBeingEvicted) const {
372+ // If we cannot find the virtual register in the map, we just assume it has
373+ // not been evicted before and thus has a value of zero (which is what the
374+ // subscript operator returns by default).
375+ ++VirtRegEvictionCounts[RegBeingEvicted.id ()];
376+ }
377+
378+ unsigned getEvictionCount (Register Reg) const {
379+ auto EvictionCountIt = VirtRegEvictionCounts.find (Reg.id ());
380+ if (EvictionCountIt != VirtRegEvictionCounts.end ())
381+ return EvictionCountIt->second ;
382+ return 0 ;
383+ }
367384};
368385
369386#define _DECL_FEATURES (type, name, shape, _ ) \
@@ -657,7 +674,7 @@ bool MLEvictAdvisor::loadInterferenceFeatures(
657674 // threshold, prevent the range from being evicted. We still let the
658675 // range through if it is urgent as we are required to produce an
659676 // eviction if the candidate is not spillable.
660- if (IntfCascade >= MaxCascade && !Urgent)
677+ if (getEvictionCount (Intf-> reg ()) > MaxEvictionCount && !Urgent)
661678 return false ;
662679
663680 // Only evict older cascades or live ranges without a cascade.
@@ -803,6 +820,22 @@ MCRegister MLEvictAdvisor::tryFindEvictionCandidate(
803820 }
804821 assert (CandidatePos < ValidPosLimit);
805822 (void )ValidPosLimit;
823+
824+ // Update information about how many times the virtual registers being
825+ // evicted have been evicted so that we can prevent the model from evicting
826+ // the same ranges continually and eating compile time.
827+ if (CandidatePos == CandidateVirtRegPos) {
828+ onEviction (VirtReg.reg ());
829+ } else {
830+ for (MCRegUnit Unit : TRI->regunits (Regs[CandidatePos].first )) {
831+ LiveIntervalUnion::Query &Q = Matrix->query (VirtReg, Unit);
832+ const auto &IFIntervals = Q.interferingVRegs (EvictInterferenceCutoff);
833+ for (const LiveInterval *Intf : reverse (IFIntervals)) {
834+ onEviction (Intf->reg ());
835+ }
836+ }
837+ }
838+
806839 return Regs[CandidatePos].first ;
807840}
808841
0 commit comments