[llvm] [LoopStrengthReduce] Mitigation of issues introduced by compilation time optimization in SolveRecurse. (PR #147588)
Sergey Shcherbinin via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 9 03:21:16 PDT 2025
================
@@ -5398,54 +5443,69 @@ void LSRInstance::SolveRecurse(SmallVectorImpl<const Formula *> &Solution,
SmallPtrSet<const SCEV *, 16> NewRegs;
Cost NewCost(L, SE, TTI, AMK);
- for (const Formula &F : LU.Formulae) {
- // Ignore formulae which may not be ideal in terms of register reuse of
- // ReqRegs. The formula should use all required registers before
- // introducing new ones.
- // This can sometimes (notably when trying to favour postinc) lead to
- // sub-optimial decisions. There it is best left to the cost modelling to
- // get correct.
- if (AMK != TTI::AMK_PostIndexed || LU.Kind != LSRUse::Address) {
- int NumReqRegsToFind = std::min(F.getNumRegs(), ReqRegs.size());
- for (const SCEV *Reg : ReqRegs) {
- if ((F.ScaledReg && F.ScaledReg == Reg) ||
- is_contained(F.BaseRegs, Reg)) {
- --NumReqRegsToFind;
- if (NumReqRegsToFind == 0)
- break;
+ bool FormulaeTested = false;
+ unsigned NumReqRegsToIgnore = 0;
+
+ while (!FormulaeTested) {
+ assert(
+ !NumReqRegsToIgnore ||
+ NumReqRegsToIgnore < ReqRegs.size() &&
+ "at least one formulae should have at least one required register");
+
+ for (const Formula &F : LU.Formulae) {
+ // ReqRegs. The formula should use required registers before
+ // introducing new ones. Firstly try the most aggressive option
+ // (when maximum of required registers are used) and then gradually make
+ // it weaker if all formulae don't satisfy this requirement.
+ //
+ // This can sometimes (notably when trying to favour postinc) lead to
+ // sub-optimal decisions. There it is best left to the cost modeling to
+ // get correct.
+ if (ReqRegs.size() &&
+ (AMK != TTI::AMK_PostIndexed || LU.Kind != LSRUse::Address)) {
+ unsigned NumReqRegsToFind = std::min(F.getNumRegs(), ReqRegs.size());
+ bool ReqRegsFound = false;
+ for (const SCEV *Reg : ReqRegs) {
+ if ((F.ScaledReg && F.ScaledReg == Reg) ||
+ is_contained(F.BaseRegs, Reg)) {
+ ReqRegsFound = true;
+ if (--NumReqRegsToFind == NumReqRegsToIgnore)
+ break;
+ }
+ }
+ if (!ReqRegsFound || NumReqRegsToFind != NumReqRegsToIgnore) {
+ continue;
}
}
- if (NumReqRegsToFind != 0) {
- // If none of the formulae satisfied the required registers, then we could
- // clear ReqRegs and try again. Currently, we simply give up in this case.
- continue;
- }
- }
- // Evaluate the cost of the current formula. If it's already worse than
- // the current best, prune the search at that point.
- NewCost = CurCost;
- NewRegs = CurRegs;
- NewCost.RateFormula(F, NewRegs, VisitedRegs, LU);
- if (NewCost.isLess(SolutionCost)) {
- Workspace.push_back(&F);
- if (Workspace.size() != Uses.size()) {
- SolveRecurse(Solution, SolutionCost, Workspace, NewCost,
- NewRegs, VisitedRegs);
- if (F.getNumRegs() == 1 && Workspace.size() == 1)
- VisitedRegs.insert(F.ScaledReg ? F.ScaledReg : F.BaseRegs[0]);
- } else {
- LLVM_DEBUG(dbgs() << "New best at "; NewCost.print(dbgs());
- dbgs() << ".\nRegs:\n";
- for (const SCEV *S : NewRegs) dbgs()
- << "- " << *S << "\n";
- dbgs() << '\n');
-
- SolutionCost = NewCost;
- Solution = Workspace;
+ // Evaluate the cost of the current formula. If it's already worse than
+ // the current best, prune the search at that point.
+ FormulaeTested = true;
+ NewCost = CurCost;
+ NewRegs = CurRegs;
+ NewCost.RateFormula(F, NewRegs, VisitedRegs, LU);
+ if (NewCost.isLess(SolutionCost)) {
+ Workspace.push_back(&F);
+ if (Workspace.size() != Uses.size()) {
+ SolveRecurse(Solution, SolutionCost, Workspace, NewCost, NewRegs,
+ VisitedRegs);
+ if (F.getNumRegs() == 1 && Workspace.size() == 1)
+ VisitedRegs.insert(F.ScaledReg ? F.ScaledReg : F.BaseRegs[0]);
+ } else {
+ LLVM_DEBUG(dbgs() << "New best at "; NewCost.print(dbgs());
+ dbgs() << ".\nRegs:\n";
+ for (const SCEV *S : NewRegs) dbgs() << "- " << *S << "\n";
+ dbgs() << '\n');
----------------
SergeyShch01 wrote:
Ok, done
https://github.com/llvm/llvm-project/pull/147588
More information about the llvm-commits
mailing list