Skip to content

[TTI][AMDGPU] Allow targets to adjust LastCallToStaticBonus via getInliningLastCallToStaticBonus #111311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion llvm/include/llvm/Analysis/InlineAdvisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ getDevelopmentModeAdvisor(Module &M, ModuleAnalysisManager &MAM,
/// using that cost, so we won't do so from this function. Return std::nullopt
/// if inlining should not be attempted.
std::optional<InlineCost>
shouldInline(CallBase &CB, function_ref<InlineCost(CallBase &CB)> GetInlineCost,
shouldInline(CallBase &CB, TargetTransformInfo &CalleeTTI,
function_ref<InlineCost(CallBase &CB)> GetInlineCost,
OptimizationRemarkEmitter &ORE, bool EnableDeferral = true);

/// Emit ORE message.
Expand Down
1 change: 0 additions & 1 deletion llvm/include/llvm/Analysis/InlineCost.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const int OptAggressiveThreshold = 250;
int getInstrCost();
const int IndirectCallThreshold = 100;
const int LoopPenalty = 25;
const int LastCallToStaticBonus = 15000;
const int ColdccPenalty = 2000;
/// Do not inline functions which allocate this many bytes on the stack
/// when the caller is recursive.
Expand Down
7 changes: 7 additions & 0 deletions llvm/include/llvm/Analysis/TargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ class TargetTransformInfo {
unsigned getInliningCostBenefitAnalysisSavingsMultiplier() const;
unsigned getInliningCostBenefitAnalysisProfitableMultiplier() const;

/// \returns The bonus of inlining the last call to a static function.
int getInliningLastCallToStaticBonus() const;

/// \returns A value to be added to the inlining threshold.
unsigned adjustInliningThreshold(const CallBase *CB) const;

Expand Down Expand Up @@ -1840,6 +1843,7 @@ class TargetTransformInfo::Concept {
virtual unsigned getInliningCostBenefitAnalysisSavingsMultiplier() const = 0;
virtual unsigned
getInliningCostBenefitAnalysisProfitableMultiplier() const = 0;
virtual int getInliningLastCallToStaticBonus() const = 0;
virtual unsigned adjustInliningThreshold(const CallBase *CB) = 0;
virtual int getInlinerVectorBonusPercent() const = 0;
virtual unsigned getCallerAllocaCost(const CallBase *CB,
Expand Down Expand Up @@ -2250,6 +2254,9 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
unsigned getInliningCostBenefitAnalysisProfitableMultiplier() const override {
return Impl.getInliningCostBenefitAnalysisProfitableMultiplier();
}
int getInliningLastCallToStaticBonus() const override {
return Impl.getInliningLastCallToStaticBonus();
}
int getInlinerVectorBonusPercent() const override {
return Impl.getInlinerVectorBonusPercent();
}
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class TargetTransformInfoImplBase {
unsigned getInliningCostBenefitAnalysisProfitableMultiplier() const {
return 8;
}
int getInliningLastCallToStaticBonus() const {
// This is the value of InlineConstants::LastCallToStaticBonus before it was
// removed along with the introduction of this function.
return 15000;
}
unsigned adjustInliningThreshold(const CallBase *CB) const { return 0; }
unsigned getCallerAllocaCost(const CallBase *CB, const AllocaInst *AI) const {
return 0;
Expand Down
17 changes: 9 additions & 8 deletions llvm/lib/Analysis/InlineAdvisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ std::optional<llvm::InlineCost> static getDefaultInlineAdvice(
return FAM.getResult<TargetLibraryAnalysis>(F);
};

Function &Callee = *CB.getCalledFunction();
auto &CalleeTTI = FAM.getResult<TargetIRAnalysis>(Callee);
auto GetInlineCost = [&](CallBase &CB) {
Function &Callee = *CB.getCalledFunction();
auto &CalleeTTI = FAM.getResult<TargetIRAnalysis>(Callee);
bool RemarksEnabled =
Callee.getContext().getDiagHandlerPtr()->isMissedOptRemarkEnabled(
DEBUG_TYPE);
return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, GetTLI,
GetBFI, PSI, RemarksEnabled ? &ORE : nullptr);
};
return llvm::shouldInline(
CB, GetInlineCost, ORE,
CB, CalleeTTI, GetInlineCost, ORE,
Params.EnableDeferral.value_or(EnableInlineDeferral));
}

Expand Down Expand Up @@ -247,7 +247,8 @@ bool InlineAdvisorAnalysis::Result::tryCreate(
/// \p TotalSecondaryCost will be set to the estimated cost of inlining the
/// caller if \p CB is suppressed for inlining.
static bool
shouldBeDeferred(Function *Caller, InlineCost IC, int &TotalSecondaryCost,
shouldBeDeferred(Function *Caller, TargetTransformInfo &CalleeTTI,
InlineCost IC, int &TotalSecondaryCost,
function_ref<InlineCost(CallBase &CB)> GetInlineCost) {
// For now we only handle local or inline functions.
if (!Caller->hasLocalLinkage() && !Caller->hasLinkOnceODRLinkage())
Expand Down Expand Up @@ -320,7 +321,7 @@ shouldBeDeferred(Function *Caller, InlineCost IC, int &TotalSecondaryCost,
// be removed entirely. We did not account for this above unless there
// is only one caller of Caller.
if (ApplyLastCallBonus)
TotalSecondaryCost -= InlineConstants::LastCallToStaticBonus;
TotalSecondaryCost -= CalleeTTI.getInliningLastCallToStaticBonus();

// If InlineDeferralScale is negative, then ignore the cost of primary
// inlining -- IC.getCost() multiplied by the number of callers to Caller.
Expand Down Expand Up @@ -374,7 +375,7 @@ void llvm::setInlineRemark(CallBase &CB, StringRef Message) {
/// using that cost, so we won't do so from this function. Return std::nullopt
/// if inlining should not be attempted.
std::optional<InlineCost>
llvm::shouldInline(CallBase &CB,
llvm::shouldInline(CallBase &CB, TargetTransformInfo &CalleeTTI,
function_ref<InlineCost(CallBase &CB)> GetInlineCost,
OptimizationRemarkEmitter &ORE, bool EnableDeferral) {
using namespace ore;
Expand Down Expand Up @@ -413,8 +414,8 @@ llvm::shouldInline(CallBase &CB,
}

int TotalSecondaryCost = 0;
if (EnableDeferral &&
shouldBeDeferred(Caller, IC, TotalSecondaryCost, GetInlineCost)) {
if (EnableDeferral && shouldBeDeferred(Caller, CalleeTTI, IC,
TotalSecondaryCost, GetInlineCost)) {
LLVM_DEBUG(dbgs() << " NOT Inlining: " << CB
<< " Cost = " << IC.getCost()
<< ", outer Cost = " << TotalSecondaryCost << '\n');
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/InlineCost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ void InlineCostCallAnalyzer::updateThreshold(CallBase &Call, Function &Callee) {
// and the callsite.
int SingleBBBonusPercent = 50;
int VectorBonusPercent = TTI.getInlinerVectorBonusPercent();
int LastCallToStaticBonus = InlineConstants::LastCallToStaticBonus;
int LastCallToStaticBonus = TTI.getInliningLastCallToStaticBonus();

// Lambda to set all the above bonus and bonus percentages to 0.
auto DisallowAllBonuses = [&]() {
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Analysis/TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ TargetTransformInfo::getInliningCostBenefitAnalysisProfitableMultiplier()
return TTIImpl->getInliningCostBenefitAnalysisProfitableMultiplier();
}

int TargetTransformInfo::getInliningLastCallToStaticBonus() const {
return TTIImpl->getInliningLastCallToStaticBonus();
}

unsigned
TargetTransformInfo::adjustInliningThreshold(const CallBase *CB) const {
return TTIImpl->adjustInliningThreshold(CB);
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,11 @@ static unsigned getCallArgsTotalAllocaSize(const CallBase *CB,
return AllocaSize;
}

int GCNTTIImpl::getInliningLastCallToStaticBonus() const {
return BaseT::getInliningLastCallToStaticBonus() *
getInliningThresholdMultiplier();
}

unsigned GCNTTIImpl::adjustInliningThreshold(const CallBase *CB) const {
unsigned Threshold = adjustInliningThresholdUsingCallee(CB, TLI, this);

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class GCNTTIImpl final : public BasicTTIImplBase<GCNTTIImpl> {
bool areInlineCompatible(const Function *Caller,
const Function *Callee) const;

int getInliningLastCallToStaticBonus() const;
unsigned getInliningThresholdMultiplier() const { return 11; }
unsigned adjustInliningThreshold(const CallBase *CB) const;
unsigned getCallerAllocaCost(const CallBase *CB, const AllocaInst *AI) const;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; RUN: opt -mtriple=amdgcn-amd-amdhsa -S -passes=inline -inline-threshold=0 -debug-only=inline-cost %s -o - 2>&1 | FileCheck %s
; REQUIRES: asserts

; CHECK: Analyzing call of callee_not_only_one_live_use... (caller:caller)
; CHECK: Cost: -30
; CHECK: Analyzing call of callee_only_one_live_use... (caller:caller)
; CHECK: Cost: -165030

define internal void @callee_not_only_one_live_use() {
ret void
}

define internal void @callee_only_one_live_use() {
ret void
}

define void @caller() {
call void @callee_not_only_one_live_use()
call void @callee_not_only_one_live_use()
call void @callee_only_one_live_use()
ret void
}
Loading