@@ -40,7 +40,7 @@ static cl::opt<bool>
4040 cl::desc(" Track reg pressure more accurately and "
4141 " delay some instructions to avoid spills." ));
4242static cl::opt<unsigned > NumCriticalFreeRegs (
43- " aie-premisched-near-critical-regs" , cl::init(4 ),
43+ " aie-premisched-near-critical-regs" , cl::init(2 ),
4444 cl::desc(" Number of free registers below which premisched should actively "
4545 " try to reduce the pressure." ));
4646
@@ -761,6 +761,33 @@ bool AIEPostRASchedStrategy::tryCandidate(SchedCandidate &Cand,
761761 return false ;
762762}
763763
764+ void AIEPreRASchedStrategy::initialize (ScheduleDAGMI *DAG) {
765+ GenericScheduler::initialize (DAG);
766+
767+ // Cache the threshold for each pressure set.
768+ const std::vector<unsigned > &RegionMaxPressure =
769+ static_cast <ScheduleDAGMILive *>(DAG)->getRegPressure ().MaxSetPressure ;
770+ PSetThresholds.clear ();
771+ for (unsigned PSet = 0 , EndPSet = RegionMaxPressure.size (); PSet < EndPSet;
772+ ++PSet) {
773+ unsigned MaxPressure = RegionMaxPressure[PSet];
774+ unsigned Limit = Context->RegClassInfo ->getRegPressureSetLimit (PSet);
775+
776+ // If the region has a maximum pressure that exceeds the target threshold,
777+ // artificially reduce that threshold to force more conservative scheduling.
778+ if (MaxPressure > Limit) {
779+ unsigned ExtraPressure = MaxPressure - Limit;
780+ if (Limit > ExtraPressure)
781+ Limit -= ExtraPressure;
782+ else
783+ Limit = 0 ;
784+ LLVM_DEBUG (dbgs () << TRI->getRegPressureSetName (PSet)
785+ << " Decreased Threshold to " << Limit << " \n " );
786+ }
787+ PSetThresholds.push_back (Limit);
788+ }
789+ }
790+
764791void AIEPreRASchedStrategy::enterRegion (MachineBasicBlock *BB,
765792 MachineBasicBlock::iterator Begin,
766793 MachineBasicBlock::iterator End,
@@ -874,8 +901,9 @@ bool AIEPreRASchedStrategy::isAvailableNode(SUnit &SU, SchedBoundary &Zone,
874901 }
875902
876903 unsigned CurrPressure = BotRPT.getRegSetPressureAtPos ()[WorstPC.getPSet ()];
877- if (CurrPressure + WorstPC.getUnitInc () <
878- TRI->getRegPressureSetLimit (*CurMBB->getParent (), WorstPC.getPSet ())) {
904+ if (CurrPressure + WorstPC.getUnitInc () +
905+ (NumCriticalFreeRegs * WorstPC.getUnitInc ()) <
906+ PSetThresholds[WorstPC.getPSet ()]) {
879907 // Worsening pressure, but still within limits, keep node as available
880908 return true ;
881909 }
@@ -960,10 +988,11 @@ bool AIEPreRASchedStrategy::tryCandidate(SchedCandidate &Cand,
960988 if (!PC.isValid ())
961989 return false ;
962990 unsigned CurrPressure = BotRPT.getRegSetPressureAtPos ()[PC.getPSet ()];
963- unsigned Threshold =
964- TRI->getRegPressureSetLimit (*CurMBB->getParent (), PC.getPSet ());
965- return Threshold <= NumCriticalFreeRegs ||
966- CurrPressure >= Threshold - NumCriticalFreeRegs;
991+ unsigned Threshold = PSetThresholds[PC.getPSet ()];
992+ unsigned NumCriticalFreeUnits =
993+ NumCriticalFreeRegs * std::abs (PC.getUnitInc ());
994+ return Threshold <= NumCriticalFreeUnits ||
995+ CurrPressure >= Threshold - NumCriticalFreeUnits;
967996 };
968997 PressureChange TryCandPC =
969998 getPressureChange (estimatePressureDiff (*TryCand.SU , BotRPT));
@@ -972,13 +1001,12 @@ bool AIEPreRASchedStrategy::tryCandidate(SchedCandidate &Cand,
9721001 if ((IsNearCritical (TryCandPC) || IsNearCritical (CandPC)) &&
9731002 tryPressure (TryCandPC, CandPC, TryCand, Cand, RegMax, TRI, DAG->MF ))
9741003 return TryCand.Reason != NoCand;
975- }
9761004
977- // Avoid increasing the max pressure of the entire region.
978- if (DAG-> isTrackingPressure () &&
979- tryPressure ( TryCand. RPDelta . CurrentMax , Cand. RPDelta . CurrentMax , TryCand,
980- Cand, RegMax, TRI, DAG-> MF ))
981- return TryCand. Reason != NoCand;
1005+ // Avoid increasing the max pressure of the entire region.
1006+ if (tryPressure (TryCand. RPDelta . CurrentMax , Cand. RPDelta . CurrentMax ,
1007+ TryCand, Cand, RegMax, TRI, DAG-> MF ))
1008+ return TryCand. Reason != NoCand;
1009+ }
9821010
9831011 // Fall through to original instruction order.
9841012 if ((Zone->isTop () && TryCand.SU ->NodeNum < Cand.SU ->NodeNum ) ||
0 commit comments