[llvm] [FuncSpec] Only compute Latency bonus when necessary (PR #113159)
Alexandros Lamprineas via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 22 05:36:39 PDT 2024
================
@@ -875,48 +909,67 @@ bool FunctionSpecializer::findSpecializations(Function *F, unsigned FuncSize,
AllSpecs[Index].CallSites.push_back(&CS);
} else {
// Calculate the specialisation gain.
- Bonus B;
+ Cost CodeSize;
unsigned Score = 0;
InstCostVisitor Visitor = getInstCostVisitorFor(F);
for (ArgInfo &A : S.Args) {
- B += Visitor.getSpecializationBonus(A.Formal, A.Actual);
+ CodeSize += Visitor.getCodeSizeSavingsForArg(A.Formal, A.Actual);
Score += getInliningBonus(A.Formal, A.Actual);
}
- B += Visitor.getBonusFromPendingPHIs();
-
+ CodeSize += Visitor.getCodeSizeSavingsFromPendingPHIs();
- LLVM_DEBUG(dbgs() << "FnSpecialization: Specialization bonus {CodeSize = "
- << B.CodeSize << ", Latency = " << B.Latency
- << ", Inlining = " << Score << "}\n");
-
- FunctionGrowth[F] += FuncSize - B.CodeSize;
-
- auto IsProfitable = [](Bonus &B, unsigned Score, unsigned FuncSize,
- unsigned FuncGrowth) -> bool {
+ auto IsProfitable = [&]() -> bool {
// No check required.
if (ForceSpecialization)
return true;
+
+ unsigned CodeSizeSavings = getCostValue(CodeSize);
+ // TODO: We should only accumulate codesize increase of specializations
+ // that are actually created.
+ FunctionGrowth[F] += FuncSize - CodeSizeSavings;
----------------
labrinea wrote:
It is true indeed. We only keep `std::min(NumCandidates * MaxClones, unsigned(AllSpecs.size()))` per invocation of `FunctionSpecializer::run()`. It's not trivial to know upfront whether a specialization will be selected even if this lambda says it's profitable. However, if we move the FunctionGrowth accumulation just before the lambda returns true, does it not improve the likelihood of being created? (at least we know that MinCodeSizeSavings and MinLatencySavings requirements are satisfied at that point)
https://github.com/llvm/llvm-project/pull/113159
More information about the llvm-commits
mailing list