42
42
#include < array>
43
43
#include < bitset>
44
44
#include < memory>
45
+ #include < unordered_map>
45
46
46
47
using namespace llvm ;
47
48
@@ -63,11 +64,11 @@ static cl::opt<std::string> InteractiveChannelBaseName(
63
64
" outgoing name should be "
64
65
" <regalloc-evict-interactive-channel-base>.out" ));
65
66
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 ));
71
72
72
73
// Options that only make sense in development mode
73
74
#ifdef LLVM_HAVE_TFLITE
@@ -364,6 +365,22 @@ class MLEvictAdvisor : public RegAllocEvictionAdvisor {
364
365
365
366
using RegID = unsigned ;
366
367
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
+ }
367
384
};
368
385
369
386
#define _DECL_FEATURES (type, name, shape, _ ) \
@@ -657,7 +674,7 @@ bool MLEvictAdvisor::loadInterferenceFeatures(
657
674
// threshold, prevent the range from being evicted. We still let the
658
675
// range through if it is urgent as we are required to produce an
659
676
// eviction if the candidate is not spillable.
660
- if (IntfCascade >= MaxCascade && !Urgent)
677
+ if (getEvictionCount (Intf-> reg ()) > MaxEvictionCount && !Urgent)
661
678
return false ;
662
679
663
680
// Only evict older cascades or live ranges without a cascade.
@@ -803,6 +820,22 @@ MCRegister MLEvictAdvisor::tryFindEvictionCandidate(
803
820
}
804
821
assert (CandidatePos < ValidPosLimit);
805
822
(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
+
806
839
return Regs[CandidatePos].first ;
807
840
}
808
841
0 commit comments