aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
diff options
context:
space:
mode:
authorAlexandros Lamprineas <alexandros.lamprineas@arm.com>2023-05-11 13:44:40 +0100
committerAlexandros Lamprineas <alexandros.lamprineas@arm.com>2023-05-22 17:49:52 +0100
commit1b1232047e83b69561fd64b9547cb0a0d374473a (patch)
treecf666cdb9166f9e8bdeb76ca875e269936ac64b8 /llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
parentc067c6e55dcf6cd936d4b74aed254abaa6f398fc (diff)
downloadllvm-1b1232047e83b69561fd64b9547cb0a0d374473a.zip
llvm-1b1232047e83b69561fd64b9547cb0a0d374473a.tar.gz
llvm-1b1232047e83b69561fd64b9547cb0a0d374473a.tar.bz2
[FuncSpec] Replace LoopInfo with BlockFrequencyInfo.
Using AvgLoopIters on any loop is too imprecise making the cost model favor users inside loop nests regardless of the actual tripcount. Differential Revision: https://reviews.llvm.org/D150375
Diffstat (limited to 'llvm/lib/Transforms/IPO/FunctionSpecialization.cpp')
-rw-r--r--llvm/lib/Transforms/IPO/FunctionSpecialization.cpp31
1 files changed, 13 insertions, 18 deletions
diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
index 51f1319a..87cc0f6 100644
--- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -49,7 +49,6 @@
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/CodeMetrics.h"
#include "llvm/Analysis/InlineCost.h"
-#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueLattice.h"
#include "llvm/Analysis/ValueLatticeUtils.h"
@@ -82,10 +81,6 @@ static cl::opt<unsigned> MinFunctionSize(
"Don't specialize functions that have less than this number of "
"instructions"));
-static cl::opt<unsigned> AvgLoopIters(
- "funcspec-avg-loop-iters", cl::init(10), cl::Hidden, cl::desc(
- "Average loop iteration count"));
-
static cl::opt<bool> SpecializeOnAddress(
"funcspec-on-address", cl::init(false), cl::Hidden, cl::desc(
"Enable function specialization on the address of global values"));
@@ -502,8 +497,7 @@ bool FunctionSpecializer::findSpecializations(Function *F, Cost SpecCost,
// Calculate the specialisation gain.
Cost Score = 0 - SpecCost;
for (ArgInfo &A : S.Args)
- Score +=
- getSpecializationBonus(A.Formal, A.Actual, Solver.getLoopInfo(*F));
+ Score += getSpecializationBonus(A.Formal, A.Actual);
// Discard unprofitable specialisations.
if (!ForceSpecialization && Score <= 0)
@@ -594,41 +588,42 @@ Cost FunctionSpecializer::getSpecializationCost(Function *F) {
}
static Cost getUserBonus(User *U, TargetTransformInfo &TTI,
- const LoopInfo &LI) {
+ BlockFrequencyInfo &BFI) {
auto *I = dyn_cast_or_null<Instruction>(U);
// If not an instruction we do not know how to evaluate.
// Keep minimum possible cost for now so that it doesnt affect
// specialization.
if (!I)
- return std::numeric_limits<unsigned>::min();
+ return 0;
- Cost Bonus =
- TTI.getInstructionCost(U, TargetTransformInfo::TCK_SizeAndLatency);
+ uint64_t Weight = BFI.getBlockFreq(I->getParent()).getFrequency() /
+ BFI.getEntryFreq();
+ if (!Weight)
+ return 0;
- // Increase the cost if it is inside the loop.
- unsigned LoopDepth = LI.getLoopDepth(I->getParent());
- Bonus *= std::pow((double)AvgLoopIters, LoopDepth);
+ Cost Bonus = Weight *
+ TTI.getInstructionCost(U, TargetTransformInfo::TCK_SizeAndLatency);
// Traverse recursively if there are more uses.
// TODO: Any other instructions to be added here?
if (I->mayReadFromMemory() || I->isCast())
for (auto *User : I->users())
- Bonus += getUserBonus(User, TTI, LI);
+ Bonus += getUserBonus(User, TTI, BFI);
return Bonus;
}
/// Compute a bonus for replacing argument \p A with constant \p C.
-Cost FunctionSpecializer::getSpecializationBonus(Argument *A, Constant *C,
- const LoopInfo &LI) {
+Cost FunctionSpecializer::getSpecializationBonus(Argument *A, Constant *C) {
Function *F = A->getParent();
auto &TTI = (GetTTI)(*F);
+ auto &BFI = (GetBFI)(*F);
LLVM_DEBUG(dbgs() << "FnSpecialization: Analysing bonus for constant: "
<< C->getNameOrAsOperand() << "\n");
Cost TotalCost = 0;
for (auto *U : A->users()) {
- TotalCost += getUserBonus(U, TTI, LI);
+ TotalCost += getUserBonus(U, TTI, BFI);
LLVM_DEBUG(dbgs() << "FnSpecialization: User cost ";
TotalCost.print(dbgs()); dbgs() << " for: " << *U << "\n");
}