2020-05-08 05:35:08 +02:00
|
|
|
//===- InlineAdvisor.cpp - analysis pass implementation -------------------===//
|
|
|
|
//
|
2020-06-16 01:46:21 +02:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2020-05-08 05:35:08 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2020-04-28 22:25:15 +02:00
|
|
|
// This file implements InlineAdvisorAnalysis and DefaultInlineAdvisor, and
|
|
|
|
// related types.
|
2020-05-08 05:35:08 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/InlineAdvisor.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/InlineCost.h"
|
|
|
|
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
|
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
2021-01-26 00:25:39 +01:00
|
|
|
#include "llvm/Analysis/ReplayInlineAdvisor.h"
|
2020-05-08 05:35:08 +02:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2020-06-19 19:25:31 +02:00
|
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
2020-05-08 05:35:08 +02:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2020-06-23 13:19:09 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2020-05-08 05:35:08 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
#define DEBUG_TYPE "inline"
|
|
|
|
|
|
|
|
// This weirdly named statistic tracks the number of times that, when attempting
|
|
|
|
// to inline a function A into B, we analyze the callers of B in order to see
|
|
|
|
// if those would be more profitable and blocked inline steps.
|
|
|
|
STATISTIC(NumCallerCallersAnalyzed, "Number of caller-callers analyzed");
|
|
|
|
|
|
|
|
/// Flag to add inline messages as callsite attributes 'inline-remark'.
|
|
|
|
static cl::opt<bool>
|
|
|
|
InlineRemarkAttribute("inline-remark-attribute", cl::init(false),
|
|
|
|
cl::Hidden,
|
|
|
|
cl::desc("Enable adding inline-remark attribute to"
|
|
|
|
" callsites processed by inliner but decided"
|
|
|
|
" to be not inlined"));
|
|
|
|
|
|
|
|
// An integer used to limit the cost of inline deferral. The default negative
|
|
|
|
// number tells shouldBeDeferred to only take the secondary cost into account.
|
|
|
|
static cl::opt<int>
|
|
|
|
InlineDeferralScale("inline-deferral-scale",
|
|
|
|
cl::desc("Scale to limit the cost of inline deferral"),
|
[Inlining] Set inline-deferral-scale to 2.
Summary:
This patch sets inline-deferral-scale to 2.
Both internal and SPEC benchmarking show that 2 is the best number
among -1, 2, 3, and 4.
inline-deferral-scale SPECint2006
------------------------------------------------------------
-1 38.0 (the default without this patch)
2 38.5
3 38.1
4 38.1
With the new default number, shouldBeDeferred returns true if:
TotalCost < IC.getCost() * 2
where
TotalCost is TotalSecondaryCost + IC.getCost() * NumCallerUsers.
If TotalCost >= 0 and NumCallerUsers >= 2, then
TotalCost >= IC.getCost() * 2, so shouldBeDeferred returns true only
when NumCallerUsers is 1.
Now, if TotalSecondaryCost < 0, which can happen if
InlineConstants::LastCallToStaticBonus, a huge number, has been
subtracted from TotalSecondaryCost, then TotalCost may be negative.
In this case, shouldBeDeferred may return true even when
NumCallerUsers >= 2.
Reviewers: davidxl, nikic
Reviewed By: davidxl
Subscribers: xbolva00, hiraditya, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D80229
2020-05-26 00:43:28 +02:00
|
|
|
cl::init(2), cl::Hidden);
|
2020-05-08 05:35:08 +02:00
|
|
|
|
2021-01-20 20:25:43 +01:00
|
|
|
extern cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats;
|
|
|
|
|
2021-01-12 22:19:30 +01:00
|
|
|
void DefaultInlineAdvice::recordUnsuccessfulInliningImpl(
|
|
|
|
const InlineResult &Result) {
|
|
|
|
using namespace ore;
|
|
|
|
llvm::setInlineRemark(*OriginalCB, std::string(Result.getFailureReason()) +
|
|
|
|
"; " + inlineCostStr(*OIC));
|
|
|
|
ORE.emit([&]() {
|
|
|
|
return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block)
|
|
|
|
<< NV("Callee", Callee) << " will not be inlined into "
|
|
|
|
<< NV("Caller", Caller) << ": "
|
|
|
|
<< NV("Reason", Result.getFailureReason());
|
|
|
|
});
|
|
|
|
}
|
2020-04-28 22:25:15 +02:00
|
|
|
|
2021-01-12 22:19:30 +01:00
|
|
|
void DefaultInlineAdvice::recordInliningWithCalleeDeletedImpl() {
|
|
|
|
if (EmitRemarks)
|
2020-04-28 22:25:15 +02:00
|
|
|
emitInlinedInto(ORE, DLoc, Block, *Callee, *Caller, *OIC);
|
2021-01-12 22:19:30 +01:00
|
|
|
}
|
2020-04-28 22:25:15 +02:00
|
|
|
|
2021-01-12 22:19:30 +01:00
|
|
|
void DefaultInlineAdvice::recordInliningImpl() {
|
|
|
|
if (EmitRemarks)
|
2020-04-28 22:25:15 +02:00
|
|
|
emitInlinedInto(ORE, DLoc, Block, *Callee, *Caller, *OIC);
|
2021-01-12 22:19:30 +01:00
|
|
|
}
|
2020-04-28 22:25:15 +02:00
|
|
|
|
2020-07-17 13:49:11 +02:00
|
|
|
llvm::Optional<llvm::InlineCost> static getDefaultInlineAdvice(
|
|
|
|
CallBase &CB, FunctionAnalysisManager &FAM, const InlineParams &Params) {
|
2020-05-15 00:42:26 +02:00
|
|
|
Function &Caller = *CB.getCaller();
|
|
|
|
ProfileSummaryInfo *PSI =
|
|
|
|
FAM.getResult<ModuleAnalysisManagerFunctionProxy>(Caller)
|
|
|
|
.getCachedResult<ProfileSummaryAnalysis>(
|
|
|
|
*CB.getParent()->getParent()->getParent());
|
2020-04-28 22:25:15 +02:00
|
|
|
|
2020-05-15 00:42:26 +02:00
|
|
|
auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
|
2020-05-15 07:38:41 +02:00
|
|
|
auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
|
2020-05-15 00:42:26 +02:00
|
|
|
return FAM.getResult<AssumptionAnalysis>(F);
|
2020-04-28 22:25:15 +02:00
|
|
|
};
|
|
|
|
auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {
|
|
|
|
return FAM.getResult<BlockFrequencyAnalysis>(F);
|
|
|
|
};
|
|
|
|
auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
|
|
|
|
return FAM.getResult<TargetLibraryAnalysis>(F);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto GetInlineCost = [&](CallBase &CB) {
|
|
|
|
Function &Callee = *CB.getCalledFunction();
|
|
|
|
auto &CalleeTTI = FAM.getResult<TargetIRAnalysis>(Callee);
|
|
|
|
bool RemarksEnabled =
|
|
|
|
Callee.getContext().getDiagHandlerPtr()->isMissedOptRemarkEnabled(
|
|
|
|
DEBUG_TYPE);
|
2020-05-15 07:38:41 +02:00
|
|
|
return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, GetTLI,
|
|
|
|
GetBFI, PSI, RemarksEnabled ? &ORE : nullptr);
|
2020-04-28 22:25:15 +02:00
|
|
|
};
|
2020-07-13 21:14:21 +02:00
|
|
|
return llvm::shouldInline(CB, GetInlineCost, ORE,
|
2021-01-13 06:43:50 +01:00
|
|
|
Params.EnableDeferral.getValueOr(false));
|
2020-07-13 21:14:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:56:57 +01:00
|
|
|
std::unique_ptr<InlineAdvice>
|
|
|
|
DefaultInlineAdvisor::getAdviceImpl(CallBase &CB) {
|
2020-07-13 21:14:21 +02:00
|
|
|
auto OIC = getDefaultInlineAdvice(CB, FAM, Params);
|
|
|
|
return std::make_unique<DefaultInlineAdvice>(
|
|
|
|
this, CB, OIC,
|
|
|
|
FAM.getResult<OptimizationRemarkEmitterAnalysis>(*CB.getCaller()));
|
2020-04-28 22:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
InlineAdvice::InlineAdvice(InlineAdvisor *Advisor, CallBase &CB,
|
2020-06-09 23:33:46 +02:00
|
|
|
OptimizationRemarkEmitter &ORE,
|
2020-04-28 22:25:15 +02:00
|
|
|
bool IsInliningRecommended)
|
|
|
|
: Advisor(Advisor), Caller(CB.getCaller()), Callee(CB.getCalledFunction()),
|
2020-06-09 23:33:46 +02:00
|
|
|
DLoc(CB.getDebugLoc()), Block(CB.getParent()), ORE(ORE),
|
2020-04-28 22:25:15 +02:00
|
|
|
IsInliningRecommended(IsInliningRecommended) {}
|
|
|
|
|
|
|
|
void InlineAdvisor::markFunctionAsDeleted(Function *F) {
|
|
|
|
assert((!DeletedFunctions.count(F)) &&
|
|
|
|
"Cannot put cause a function to become dead twice!");
|
|
|
|
DeletedFunctions.insert(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InlineAdvisor::freeDeletedFunctions() {
|
|
|
|
for (auto *F : DeletedFunctions)
|
|
|
|
delete F;
|
|
|
|
DeletedFunctions.clear();
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:25:43 +01:00
|
|
|
void InlineAdvice::recordInlineStatsIfNeeded() {
|
|
|
|
if (Advisor->ImportedFunctionsStats)
|
|
|
|
Advisor->ImportedFunctionsStats->recordInline(*Caller, *Callee);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InlineAdvice::recordInlining() {
|
|
|
|
markRecorded();
|
|
|
|
recordInlineStatsIfNeeded();
|
|
|
|
recordInliningImpl();
|
|
|
|
}
|
|
|
|
|
2020-04-28 22:25:15 +02:00
|
|
|
void InlineAdvice::recordInliningWithCalleeDeleted() {
|
|
|
|
markRecorded();
|
2021-01-20 20:25:43 +01:00
|
|
|
recordInlineStatsIfNeeded();
|
2020-04-28 22:25:15 +02:00
|
|
|
Advisor->markFunctionAsDeleted(Callee);
|
|
|
|
recordInliningWithCalleeDeletedImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalysisKey InlineAdvisorAnalysis::Key;
|
|
|
|
|
|
|
|
bool InlineAdvisorAnalysis::Result::tryCreate(InlineParams Params,
|
2021-01-26 00:25:39 +01:00
|
|
|
InliningAdvisorMode Mode,
|
|
|
|
StringRef ReplayFile) {
|
2020-05-21 17:40:49 +02:00
|
|
|
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
|
2020-04-28 22:25:15 +02:00
|
|
|
switch (Mode) {
|
|
|
|
case InliningAdvisorMode::Default:
|
2021-07-28 00:09:36 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Using default inliner heuristic.\n");
|
2021-01-20 20:25:43 +01:00
|
|
|
Advisor.reset(new DefaultInlineAdvisor(M, FAM, Params));
|
2021-01-26 00:25:39 +01:00
|
|
|
// Restrict replay to default advisor, ML advisors are stateful so
|
|
|
|
// replay will need augmentations to interleave with them correctly.
|
|
|
|
if (!ReplayFile.empty()) {
|
|
|
|
Advisor = std::make_unique<ReplayInlineAdvisor>(
|
|
|
|
M, FAM, M.getContext(), std::move(Advisor), ReplayFile,
|
|
|
|
/* EmitRemarks =*/true);
|
|
|
|
}
|
2020-04-28 22:25:15 +02:00
|
|
|
break;
|
|
|
|
case InliningAdvisorMode::Development:
|
2020-07-09 03:55:36 +02:00
|
|
|
#ifdef LLVM_HAVE_TF_API
|
2021-07-28 00:09:36 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Using development-mode inliner policy.\n");
|
2020-07-09 03:55:36 +02:00
|
|
|
Advisor =
|
|
|
|
llvm::getDevelopmentModeAdvisor(M, MAM, [&FAM, Params](CallBase &CB) {
|
|
|
|
auto OIC = getDefaultInlineAdvice(CB, FAM, Params);
|
|
|
|
return OIC.hasValue();
|
|
|
|
});
|
|
|
|
#endif
|
2020-04-28 22:25:15 +02:00
|
|
|
break;
|
|
|
|
case InliningAdvisorMode::Release:
|
2020-06-09 23:50:50 +02:00
|
|
|
#ifdef LLVM_HAVE_TF_AOT
|
2021-07-28 00:09:36 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Using release-mode inliner policy.\n");
|
2020-06-09 23:50:50 +02:00
|
|
|
Advisor = llvm::getReleaseModeAdvisor(M, MAM);
|
|
|
|
#endif
|
2020-04-28 22:25:15 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-01-26 00:25:39 +01:00
|
|
|
|
2020-04-28 22:25:15 +02:00
|
|
|
return !!Advisor;
|
|
|
|
}
|
|
|
|
|
2020-05-08 05:35:08 +02:00
|
|
|
/// Return true if inlining of CB can block the caller from being
|
|
|
|
/// inlined which is proved to be more beneficial. \p IC is the
|
|
|
|
/// estimated inline cost associated with callsite \p CB.
|
|
|
|
/// \p TotalSecondaryCost will be set to the estimated cost of inlining the
|
|
|
|
/// caller if \p CB is suppressed for inlining.
|
2020-05-11 23:04:10 +02:00
|
|
|
static bool
|
|
|
|
shouldBeDeferred(Function *Caller, InlineCost IC, int &TotalSecondaryCost,
|
|
|
|
function_ref<InlineCost(CallBase &CB)> GetInlineCost) {
|
2020-05-08 05:35:08 +02:00
|
|
|
// For now we only handle local or inline functions.
|
|
|
|
if (!Caller->hasLocalLinkage() && !Caller->hasLinkOnceODRLinkage())
|
|
|
|
return false;
|
|
|
|
// If the cost of inlining CB is non-positive, it is not going to prevent the
|
|
|
|
// caller from being inlined into its callers and hence we don't need to
|
|
|
|
// defer.
|
|
|
|
if (IC.getCost() <= 0)
|
|
|
|
return false;
|
|
|
|
// Try to detect the case where the current inlining candidate caller (call
|
|
|
|
// it B) is a static or linkonce-ODR function and is an inlining candidate
|
|
|
|
// elsewhere, and the current candidate callee (call it C) is large enough
|
|
|
|
// that inlining it into B would make B too big to inline later. In these
|
|
|
|
// circumstances it may be best not to inline C into B, but to inline B into
|
|
|
|
// its callers.
|
|
|
|
//
|
|
|
|
// This only applies to static and linkonce-ODR functions because those are
|
|
|
|
// expected to be available for inlining in the translation units where they
|
|
|
|
// are used. Thus we will always have the opportunity to make local inlining
|
|
|
|
// decisions. Importantly the linkonce-ODR linkage covers inline functions
|
|
|
|
// and templates in C++.
|
|
|
|
//
|
|
|
|
// FIXME: All of this logic should be sunk into getInlineCost. It relies on
|
|
|
|
// the internal implementation of the inline cost metrics rather than
|
|
|
|
// treating them as truly abstract units etc.
|
|
|
|
TotalSecondaryCost = 0;
|
|
|
|
// The candidate cost to be imposed upon the current function.
|
|
|
|
int CandidateCost = IC.getCost() - 1;
|
|
|
|
// If the caller has local linkage and can be inlined to all its callers, we
|
|
|
|
// can apply a huge negative bonus to TotalSecondaryCost.
|
|
|
|
bool ApplyLastCallBonus = Caller->hasLocalLinkage() && !Caller->hasOneUse();
|
|
|
|
// This bool tracks what happens if we DO inline C into B.
|
|
|
|
bool InliningPreventsSomeOuterInline = false;
|
|
|
|
unsigned NumCallerUsers = 0;
|
|
|
|
for (User *U : Caller->users()) {
|
|
|
|
CallBase *CS2 = dyn_cast<CallBase>(U);
|
|
|
|
|
|
|
|
// If this isn't a call to Caller (it could be some other sort
|
|
|
|
// of reference) skip it. Such references will prevent the caller
|
|
|
|
// from being removed.
|
|
|
|
if (!CS2 || CS2->getCalledFunction() != Caller) {
|
|
|
|
ApplyLastCallBonus = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
InlineCost IC2 = GetInlineCost(*CS2);
|
|
|
|
++NumCallerCallersAnalyzed;
|
|
|
|
if (!IC2) {
|
|
|
|
ApplyLastCallBonus = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (IC2.isAlways())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// See if inlining of the original callsite would erase the cost delta of
|
|
|
|
// this callsite. We subtract off the penalty for the call instruction,
|
|
|
|
// which we would be deleting.
|
|
|
|
if (IC2.getCostDelta() <= CandidateCost) {
|
|
|
|
InliningPreventsSomeOuterInline = true;
|
|
|
|
TotalSecondaryCost += IC2.getCost();
|
|
|
|
NumCallerUsers++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!InliningPreventsSomeOuterInline)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If all outer calls to Caller would get inlined, the cost for the last
|
|
|
|
// one is set very low by getInlineCost, in anticipation that Caller will
|
|
|
|
// be removed entirely. We did not account for this above unless there
|
|
|
|
// is only one caller of Caller.
|
|
|
|
if (ApplyLastCallBonus)
|
|
|
|
TotalSecondaryCost -= InlineConstants::LastCallToStaticBonus;
|
|
|
|
|
|
|
|
// If InlineDeferralScale is negative, then ignore the cost of primary
|
|
|
|
// inlining -- IC.getCost() multiplied by the number of callers to Caller.
|
|
|
|
if (InlineDeferralScale < 0)
|
|
|
|
return TotalSecondaryCost < IC.getCost();
|
|
|
|
|
|
|
|
int TotalCost = TotalSecondaryCost + IC.getCost() * NumCallerUsers;
|
|
|
|
int Allowance = IC.getCost() * InlineDeferralScale;
|
|
|
|
return TotalCost < Allowance;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
2021-03-12 18:42:17 +01:00
|
|
|
static raw_ostream &operator<<(raw_ostream &R, const ore::NV &Arg) {
|
2020-05-08 05:35:08 +02:00
|
|
|
return R << Arg.Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class RemarkT>
|
|
|
|
RemarkT &operator<<(RemarkT &&R, const InlineCost &IC) {
|
|
|
|
using namespace ore;
|
|
|
|
if (IC.isAlways()) {
|
|
|
|
R << "(cost=always)";
|
|
|
|
} else if (IC.isNever()) {
|
|
|
|
R << "(cost=never)";
|
|
|
|
} else {
|
|
|
|
R << "(cost=" << ore::NV("Cost", IC.getCost())
|
|
|
|
<< ", threshold=" << ore::NV("Threshold", IC.getThreshold()) << ")";
|
|
|
|
}
|
|
|
|
if (const char *Reason = IC.getReason())
|
|
|
|
R << ": " << ore::NV("Reason", Reason);
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
std::string llvm::inlineCostStr(const InlineCost &IC) {
|
2021-03-12 18:42:17 +01:00
|
|
|
std::string Buffer;
|
|
|
|
raw_string_ostream Remark(Buffer);
|
2020-05-08 05:35:08 +02:00
|
|
|
Remark << IC;
|
|
|
|
return Remark.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::setInlineRemark(CallBase &CB, StringRef Message) {
|
|
|
|
if (!InlineRemarkAttribute)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Attribute Attr = Attribute::get(CB.getContext(), "inline-remark", Message);
|
|
|
|
CB.addAttribute(AttributeList::FunctionIndex, Attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the cost only if the inliner should attempt to inline at the given
|
|
|
|
/// CallSite. If we return the cost, we will emit an optimisation remark later
|
|
|
|
/// using that cost, so we won't do so from this function. Return None if
|
|
|
|
/// inlining should not be attempted.
|
|
|
|
Optional<InlineCost>
|
|
|
|
llvm::shouldInline(CallBase &CB,
|
|
|
|
function_ref<InlineCost(CallBase &CB)> GetInlineCost,
|
2020-06-04 09:40:17 +02:00
|
|
|
OptimizationRemarkEmitter &ORE, bool EnableDeferral) {
|
2020-05-08 05:35:08 +02:00
|
|
|
using namespace ore;
|
|
|
|
|
|
|
|
InlineCost IC = GetInlineCost(CB);
|
|
|
|
Instruction *Call = &CB;
|
|
|
|
Function *Callee = CB.getCalledFunction();
|
|
|
|
Function *Caller = CB.getCaller();
|
|
|
|
|
|
|
|
if (IC.isAlways()) {
|
|
|
|
LLVM_DEBUG(dbgs() << " Inlining " << inlineCostStr(IC)
|
|
|
|
<< ", Call: " << CB << "\n");
|
|
|
|
return IC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IC) {
|
|
|
|
LLVM_DEBUG(dbgs() << " NOT Inlining " << inlineCostStr(IC)
|
|
|
|
<< ", Call: " << CB << "\n");
|
|
|
|
if (IC.isNever()) {
|
|
|
|
ORE.emit([&]() {
|
|
|
|
return OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline", Call)
|
|
|
|
<< NV("Callee", Callee) << " not inlined into "
|
|
|
|
<< NV("Caller", Caller) << " because it should never be inlined "
|
|
|
|
<< IC;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ORE.emit([&]() {
|
|
|
|
return OptimizationRemarkMissed(DEBUG_TYPE, "TooCostly", Call)
|
|
|
|
<< NV("Callee", Callee) << " not inlined into "
|
|
|
|
<< NV("Caller", Caller) << " because too costly to inline "
|
|
|
|
<< IC;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setInlineRemark(CB, inlineCostStr(IC));
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TotalSecondaryCost = 0;
|
2020-06-04 09:40:17 +02:00
|
|
|
if (EnableDeferral &&
|
|
|
|
shouldBeDeferred(Caller, IC, TotalSecondaryCost, GetInlineCost)) {
|
2020-05-08 05:35:08 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " NOT Inlining: " << CB
|
|
|
|
<< " Cost = " << IC.getCost()
|
|
|
|
<< ", outer Cost = " << TotalSecondaryCost << '\n');
|
|
|
|
ORE.emit([&]() {
|
|
|
|
return OptimizationRemarkMissed(DEBUG_TYPE, "IncreaseCostInOtherContexts",
|
|
|
|
Call)
|
|
|
|
<< "Not inlining. Cost of inlining " << NV("Callee", Callee)
|
|
|
|
<< " increases the cost of inlining " << NV("Caller", Caller)
|
|
|
|
<< " in other contexts";
|
|
|
|
});
|
|
|
|
setInlineRemark(CB, "deferred");
|
|
|
|
// IC does not bool() to false, so get an InlineCost that will.
|
|
|
|
// This will not be inspected to make an error message.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << " Inlining " << inlineCostStr(IC) << ", Call: " << CB
|
|
|
|
<< '\n');
|
|
|
|
return IC;
|
|
|
|
}
|
|
|
|
|
2020-08-15 23:52:10 +02:00
|
|
|
std::string llvm::getCallSiteLocation(DebugLoc DLoc) {
|
2021-03-12 18:42:17 +01:00
|
|
|
std::string Buffer;
|
|
|
|
raw_string_ostream CallSiteLoc(Buffer);
|
2020-08-15 23:52:10 +02:00
|
|
|
bool First = true;
|
|
|
|
for (DILocation *DIL = DLoc.get(); DIL; DIL = DIL->getInlinedAt()) {
|
|
|
|
if (!First)
|
|
|
|
CallSiteLoc << " @ ";
|
|
|
|
// Note that negative line offset is actually possible, but we use
|
|
|
|
// unsigned int to match line offset representation in remarks so
|
|
|
|
// it's directly consumable by relay advisor.
|
|
|
|
uint32_t Offset =
|
|
|
|
DIL->getLine() - DIL->getScope()->getSubprogram()->getLine();
|
|
|
|
uint32_t Discriminator = DIL->getBaseDiscriminator();
|
|
|
|
StringRef Name = DIL->getScope()->getSubprogram()->getLinkageName();
|
|
|
|
if (Name.empty())
|
|
|
|
Name = DIL->getScope()->getSubprogram()->getName();
|
2021-01-12 22:19:30 +01:00
|
|
|
CallSiteLoc << Name.str() << ":" << llvm::utostr(Offset) << ":"
|
|
|
|
<< llvm::utostr(DIL->getColumn());
|
|
|
|
if (Discriminator)
|
2020-08-15 23:52:10 +02:00
|
|
|
CallSiteLoc << "." << llvm::utostr(Discriminator);
|
|
|
|
First = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallSiteLoc.str();
|
|
|
|
}
|
|
|
|
|
2020-06-19 19:25:31 +02:00
|
|
|
void llvm::addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc) {
|
2020-08-15 23:52:10 +02:00
|
|
|
if (!DLoc.get()) {
|
2020-06-19 19:25:31 +02:00
|
|
|
return;
|
2020-08-15 23:52:10 +02:00
|
|
|
}
|
2020-06-19 19:25:31 +02:00
|
|
|
|
|
|
|
bool First = true;
|
|
|
|
Remark << " at callsite ";
|
|
|
|
for (DILocation *DIL = DLoc.get(); DIL; DIL = DIL->getInlinedAt()) {
|
|
|
|
if (!First)
|
|
|
|
Remark << " @ ";
|
|
|
|
unsigned int Offset = DIL->getLine();
|
|
|
|
Offset -= DIL->getScope()->getSubprogram()->getLine();
|
|
|
|
unsigned int Discriminator = DIL->getBaseDiscriminator();
|
|
|
|
StringRef Name = DIL->getScope()->getSubprogram()->getLinkageName();
|
|
|
|
if (Name.empty())
|
|
|
|
Name = DIL->getScope()->getSubprogram()->getName();
|
2021-01-12 22:19:30 +01:00
|
|
|
Remark << Name << ":" << ore::NV("Line", Offset) << ":"
|
|
|
|
<< ore::NV("Column", DIL->getColumn());
|
2020-06-19 19:25:31 +02:00
|
|
|
if (Discriminator)
|
|
|
|
Remark << "." << ore::NV("Disc", Discriminator);
|
|
|
|
First = false;
|
|
|
|
}
|
2021-01-12 22:19:30 +01:00
|
|
|
|
|
|
|
Remark << ";";
|
2020-06-19 19:25:31 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 05:35:08 +02:00
|
|
|
void llvm::emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
|
|
|
|
const BasicBlock *Block, const Function &Callee,
|
2020-06-19 19:25:31 +02:00
|
|
|
const Function &Caller, const InlineCost &IC,
|
|
|
|
bool ForProfileContext, const char *PassName) {
|
2020-05-08 05:35:08 +02:00
|
|
|
ORE.emit([&]() {
|
|
|
|
bool AlwaysInline = IC.isAlways();
|
|
|
|
StringRef RemarkName = AlwaysInline ? "AlwaysInline" : "Inlined";
|
2020-06-19 19:25:31 +02:00
|
|
|
OptimizationRemark Remark(PassName ? PassName : DEBUG_TYPE, RemarkName,
|
|
|
|
DLoc, Block);
|
|
|
|
Remark << ore::NV("Callee", &Callee) << " inlined into ";
|
|
|
|
Remark << ore::NV("Caller", &Caller);
|
|
|
|
if (ForProfileContext)
|
|
|
|
Remark << " to match profiling context";
|
|
|
|
Remark << " with " << IC;
|
|
|
|
addLocationToRemarks(Remark, DLoc);
|
|
|
|
return Remark;
|
2020-05-08 05:35:08 +02:00
|
|
|
});
|
|
|
|
}
|
2020-11-16 23:01:53 +01:00
|
|
|
|
2021-01-20 20:25:43 +01:00
|
|
|
InlineAdvisor::InlineAdvisor(Module &M, FunctionAnalysisManager &FAM)
|
|
|
|
: M(M), FAM(FAM) {
|
|
|
|
if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No) {
|
|
|
|
ImportedFunctionsStats =
|
|
|
|
std::make_unique<ImportedFunctionsInliningStatistics>();
|
|
|
|
ImportedFunctionsStats->setModuleInfo(M);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InlineAdvisor::~InlineAdvisor() {
|
|
|
|
if (ImportedFunctionsStats) {
|
|
|
|
assert(InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No);
|
|
|
|
ImportedFunctionsStats->dump(InlinerFunctionImportStats ==
|
|
|
|
InlinerFunctionImportStatsOpts::Verbose);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeDeletedFunctions();
|
|
|
|
}
|
|
|
|
|
2021-01-15 22:56:57 +01:00
|
|
|
std::unique_ptr<InlineAdvice> InlineAdvisor::getMandatoryAdvice(CallBase &CB,
|
|
|
|
bool Advice) {
|
|
|
|
return std::make_unique<InlineAdvice>(this, CB, getCallerORE(CB), Advice);
|
2020-11-16 23:01:53 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:56:57 +01:00
|
|
|
InlineAdvisor::MandatoryInliningKind
|
|
|
|
InlineAdvisor::getMandatoryKind(CallBase &CB, FunctionAnalysisManager &FAM,
|
|
|
|
OptimizationRemarkEmitter &ORE) {
|
2020-11-16 23:01:53 +01:00
|
|
|
auto &Callee = *CB.getCalledFunction();
|
|
|
|
|
|
|
|
auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
|
|
|
|
return FAM.getResult<TargetLibraryAnalysis>(F);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &TIR = FAM.getResult<TargetIRAnalysis>(Callee);
|
|
|
|
|
|
|
|
auto TrivialDecision =
|
|
|
|
llvm::getAttributeBasedInliningDecision(CB, &Callee, TIR, GetTLI);
|
|
|
|
|
|
|
|
if (TrivialDecision.hasValue()) {
|
|
|
|
if (TrivialDecision->isSuccess())
|
|
|
|
return MandatoryInliningKind::Always;
|
|
|
|
else
|
|
|
|
return MandatoryInliningKind::Never;
|
|
|
|
}
|
|
|
|
return MandatoryInliningKind::NotMandatory;
|
|
|
|
}
|
2021-01-15 22:56:57 +01:00
|
|
|
|
|
|
|
std::unique_ptr<InlineAdvice> InlineAdvisor::getAdvice(CallBase &CB,
|
|
|
|
bool MandatoryOnly) {
|
|
|
|
if (!MandatoryOnly)
|
|
|
|
return getAdviceImpl(CB);
|
|
|
|
bool Advice = CB.getCaller() != CB.getCalledFunction() &&
|
|
|
|
MandatoryInliningKind::Always ==
|
|
|
|
getMandatoryKind(CB, FAM, getCallerORE(CB));
|
|
|
|
return getMandatoryAdvice(CB, Advice);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptimizationRemarkEmitter &InlineAdvisor::getCallerORE(CallBase &CB) {
|
|
|
|
return FAM.getResult<OptimizationRemarkEmitterAnalysis>(*CB.getCaller());
|
|
|
|
}
|