Skip to content

Commit 99282a3

Browse files
author
git apple-llvm automerger
committed
Merge commit 'ccec2cf1d9d7' from llvm.org/main into apple/main
2 parents cdc2e29 + ccec2cf commit 99282a3

File tree

12 files changed

+77
-56
lines changed

12 files changed

+77
-56
lines changed

llvm/include/llvm/Analysis/InlineAdvisor.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/Analysis/InlineCost.h"
1313
#include "llvm/Config/llvm-config.h"
1414
#include "llvm/IR/PassManager.h"
15+
#include "llvm/Analysis/Utils/ImportedFunctionsInliningStatistics.h"
1516
#include <memory>
1617
#include <unordered_set>
1718

@@ -65,10 +66,7 @@ class InlineAdvice {
6566
/// behavior by implementing the corresponding record*Impl.
6667
///
6768
/// Call after inlining succeeded, and did not result in deleting the callee.
68-
void recordInlining() {
69-
markRecorded();
70-
recordInliningImpl();
71-
}
69+
void recordInlining();
7270

7371
/// Call after inlining succeeded, and resulted in deleting the callee.
7472
void recordInliningWithCalleeDeleted();
@@ -114,6 +112,7 @@ class InlineAdvice {
114112
assert(!Recorded && "Recording should happen exactly once");
115113
Recorded = true;
116114
}
115+
void recordInlineStatsIfNeeded();
117116

118117
bool Recorded = false;
119118
};
@@ -141,7 +140,7 @@ class DefaultInlineAdvice : public InlineAdvice {
141140
class InlineAdvisor {
142141
public:
143142
InlineAdvisor(InlineAdvisor &&) = delete;
144-
virtual ~InlineAdvisor() { freeDeletedFunctions(); }
143+
virtual ~InlineAdvisor();
145144

146145
/// Get an InlineAdvice containing a recommendation on whether to
147146
/// inline or not. \p CB is assumed to be a direct call. \p FAM is assumed to
@@ -163,12 +162,14 @@ class InlineAdvisor {
163162
virtual void onPassExit() {}
164163

165164
protected:
166-
InlineAdvisor(FunctionAnalysisManager &FAM) : FAM(FAM) {}
165+
InlineAdvisor(Module &M, FunctionAnalysisManager &FAM);
167166
virtual std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) = 0;
168167
virtual std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
169168
bool Advice);
170169

170+
Module &M;
171171
FunctionAnalysisManager &FAM;
172+
std::unique_ptr<ImportedFunctionsInliningStatistics> ImportedFunctionsStats;
172173

173174
/// We may want to defer deleting functions to after the inlining for a whole
174175
/// module has finished. This allows us to reliably use function pointers as
@@ -202,8 +203,9 @@ class InlineAdvisor {
202203
/// reusable as-is for inliner pass test scenarios, as well as for regular use.
203204
class DefaultInlineAdvisor : public InlineAdvisor {
204205
public:
205-
DefaultInlineAdvisor(FunctionAnalysisManager &FAM, InlineParams Params)
206-
: InlineAdvisor(FAM), Params(Params) {}
206+
DefaultInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
207+
InlineParams Params)
208+
: InlineAdvisor(M, FAM), Params(Params) {}
207209

208210
private:
209211
std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;

llvm/include/llvm/Analysis/MLInlineAdvisor.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class MLInlineAdvisor : public InlineAdvisor {
5050
virtual std::unique_ptr<MLInlineAdvice>
5151
getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE);
5252

53-
Module &M;
5453
std::unique_ptr<MLModelRunner> ModelRunner;
5554

5655
private:

llvm/include/llvm/Analysis/ReplayInlineAdvisor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ class OptimizationRemarkEmitter;
2424
/// previous build to guide current inlining. This is useful for inliner tuning.
2525
class ReplayInlineAdvisor : public InlineAdvisor {
2626
public:
27-
ReplayInlineAdvisor(FunctionAnalysisManager &FAM, LLVMContext &Context,
28-
StringRef RemarksFile, bool EmitRemarks);
27+
ReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
28+
LLVMContext &Context, StringRef RemarksFile,
29+
bool EmitRemarks);
2930
std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
3031
bool areReplayRemarksLoaded() const { return HasReplayRemarks; }
3132

llvm/include/llvm/Analysis/Utils/ImportedFunctionsInliningStatistics.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ class ImportedFunctionsInliningStatistics {
101101
StringRef ModuleName;
102102
};
103103

104+
enum class InlinerFunctionImportStatsOpts {
105+
No = 0,
106+
Basic = 1,
107+
Verbose = 2,
108+
};
109+
104110
} // llvm
105111

106112
#endif // LLVM_TRANSFORMS_UTILS_IMPORTEDFUNCTIONSINLININGSTATISTICS_H

llvm/include/llvm/Transforms/IPO/Inliner.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ struct LegacyInlinerBase : public CallGraphSCCPass {
9797
class InlinerPass : public PassInfoMixin<InlinerPass> {
9898
public:
9999
InlinerPass(bool OnlyMandatory = false) : OnlyMandatory(OnlyMandatory) {}
100-
~InlinerPass();
101100
InlinerPass(InlinerPass &&Arg) = default;
102101

103102
PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM,
@@ -106,7 +105,6 @@ class InlinerPass : public PassInfoMixin<InlinerPass> {
106105
private:
107106
InlineAdvisor &getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
108107
FunctionAnalysisManager &FAM, Module &M);
109-
std::unique_ptr<ImportedFunctionsInliningStatistics> ImportedFunctionsStats;
110108
std::unique_ptr<DefaultInlineAdvisor> OwnedDefaultAdvisor;
111109
const bool OnlyMandatory;
112110
};

llvm/lib/Analysis/ImportedFunctionsInliningStatistics.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,23 @@
1313
#include "llvm/ADT/STLExtras.h"
1414
#include "llvm/IR/Function.h"
1515
#include "llvm/IR/Module.h"
16+
#include "llvm/Support/CommandLine.h"
1617
#include "llvm/Support/Debug.h"
1718
#include "llvm/Support/raw_ostream.h"
1819
#include <algorithm>
1920
#include <iomanip>
2021
#include <sstream>
2122
using namespace llvm;
2223

24+
cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats(
25+
"inliner-function-import-stats",
26+
cl::init(InlinerFunctionImportStatsOpts::No),
27+
cl::values(clEnumValN(InlinerFunctionImportStatsOpts::Basic, "basic",
28+
"basic statistics"),
29+
clEnumValN(InlinerFunctionImportStatsOpts::Verbose, "verbose",
30+
"printing of statistics for each inlined function")),
31+
cl::Hidden, cl::desc("Enable inliner stats for imported functions"));
32+
2333
ImportedFunctionsInliningStatistics::InlineGraphNode &
2434
ImportedFunctionsInliningStatistics::createInlineGraphNode(const Function &F) {
2535

llvm/lib/Analysis/InlineAdvisor.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ static cl::opt<int>
4848
cl::desc("Scale to limit the cost of inline deferral"),
4949
cl::init(2), cl::Hidden);
5050

51+
extern cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats;
52+
5153
void DefaultInlineAdvice::recordUnsuccessfulInliningImpl(
5254
const InlineResult &Result) {
5355
using namespace ore;
@@ -130,8 +132,20 @@ void InlineAdvisor::freeDeletedFunctions() {
130132
DeletedFunctions.clear();
131133
}
132134

135+
void InlineAdvice::recordInlineStatsIfNeeded() {
136+
if (Advisor->ImportedFunctionsStats)
137+
Advisor->ImportedFunctionsStats->recordInline(*Caller, *Callee);
138+
}
139+
140+
void InlineAdvice::recordInlining() {
141+
markRecorded();
142+
recordInlineStatsIfNeeded();
143+
recordInliningImpl();
144+
}
145+
133146
void InlineAdvice::recordInliningWithCalleeDeleted() {
134147
markRecorded();
148+
recordInlineStatsIfNeeded();
135149
Advisor->markFunctionAsDeleted(Callee);
136150
recordInliningWithCalleeDeletedImpl();
137151
}
@@ -143,7 +157,7 @@ bool InlineAdvisorAnalysis::Result::tryCreate(InlineParams Params,
143157
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
144158
switch (Mode) {
145159
case InliningAdvisorMode::Default:
146-
Advisor.reset(new DefaultInlineAdvisor(FAM, Params));
160+
Advisor.reset(new DefaultInlineAdvisor(M, FAM, Params));
147161
break;
148162
case InliningAdvisorMode::Development:
149163
#ifdef LLVM_HAVE_TF_API
@@ -428,6 +442,25 @@ void llvm::emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
428442
});
429443
}
430444

445+
InlineAdvisor::InlineAdvisor(Module &M, FunctionAnalysisManager &FAM)
446+
: M(M), FAM(FAM) {
447+
if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No) {
448+
ImportedFunctionsStats =
449+
std::make_unique<ImportedFunctionsInliningStatistics>();
450+
ImportedFunctionsStats->setModuleInfo(M);
451+
}
452+
}
453+
454+
InlineAdvisor::~InlineAdvisor() {
455+
if (ImportedFunctionsStats) {
456+
assert(InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No);
457+
ImportedFunctionsStats->dump(InlinerFunctionImportStats ==
458+
InlinerFunctionImportStatsOpts::Verbose);
459+
}
460+
461+
freeDeletedFunctions();
462+
}
463+
431464
std::unique_ptr<InlineAdvice> InlineAdvisor::getMandatoryAdvice(CallBase &CB,
432465
bool Advice) {
433466
return std::make_unique<InlineAdvice>(this, CB, getCallerORE(CB), Advice);

llvm/lib/Analysis/MLInlineAdvisor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ CallBase *getInlinableCS(Instruction &I) {
6666
MLInlineAdvisor::MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM,
6767
std::unique_ptr<MLModelRunner> Runner)
6868
: InlineAdvisor(
69-
MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()),
70-
M(M), ModelRunner(std::move(Runner)), CG(new CallGraph(M)),
69+
M, MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()),
70+
ModelRunner(std::move(Runner)), CG(new CallGraph(M)),
7171
InitialIRSize(getModuleIRSize()), CurrentIRSize(InitialIRSize) {
7272
assert(ModelRunner);
7373

llvm/lib/Analysis/ReplayInlineAdvisor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ using namespace llvm;
2222

2323
#define DEBUG_TYPE "inline-replay"
2424

25-
ReplayInlineAdvisor::ReplayInlineAdvisor(FunctionAnalysisManager &FAM,
25+
ReplayInlineAdvisor::ReplayInlineAdvisor(Module &M,
26+
FunctionAnalysisManager &FAM,
2627
LLVMContext &Context,
2728
StringRef RemarksFile,
2829
bool EmitRemarks)
29-
: InlineAdvisor(FAM), HasReplayRemarks(false), EmitRemarks(EmitRemarks) {
30+
: InlineAdvisor(M, FAM), HasReplayRemarks(false), EmitRemarks(EmitRemarks) {
3031
auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(RemarksFile);
3132
std::error_code EC = BufferOrErr.getError();
3233
if (EC) {

llvm/lib/Transforms/IPO/Inliner.cpp

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,7 @@ static cl::opt<bool>
9090
DisableInlinedAllocaMerging("disable-inlined-alloca-merging",
9191
cl::init(false), cl::Hidden);
9292

93-
namespace {
94-
95-
enum class InlinerFunctionImportStatsOpts {
96-
No = 0,
97-
Basic = 1,
98-
Verbose = 2,
99-
};
100-
101-
} // end anonymous namespace
102-
103-
static cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats(
104-
"inliner-function-import-stats",
105-
cl::init(InlinerFunctionImportStatsOpts::No),
106-
cl::values(clEnumValN(InlinerFunctionImportStatsOpts::Basic, "basic",
107-
"basic statistics"),
108-
clEnumValN(InlinerFunctionImportStatsOpts::Verbose, "verbose",
109-
"printing of statistics for each inlined function")),
110-
cl::Hidden, cl::desc("Enable inliner stats for imported functions"));
93+
extern cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats;
11194

11295
LegacyInlinerBase::LegacyInlinerBase(char &ID) : CallGraphSCCPass(ID) {}
11396

@@ -647,17 +630,12 @@ bool LegacyInlinerBase::removeDeadFunctions(CallGraph &CG,
647630
return true;
648631
}
649632

650-
InlinerPass::~InlinerPass() {
651-
if (ImportedFunctionsStats) {
652-
assert(InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No);
653-
ImportedFunctionsStats->dump(InlinerFunctionImportStats ==
654-
InlinerFunctionImportStatsOpts::Verbose);
655-
}
656-
}
657-
658633
InlineAdvisor &
659634
InlinerPass::getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
660635
FunctionAnalysisManager &FAM, Module &M) {
636+
if (OwnedDefaultAdvisor)
637+
return *OwnedDefaultAdvisor;
638+
661639
auto *IAA = MAM.getCachedResult<InlineAdvisorAnalysis>(M);
662640
if (!IAA) {
663641
// It should still be possible to run the inliner as a stand-alone SCC pass,
@@ -669,7 +647,7 @@ InlinerPass::getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
669647
// The one we would get from the MAM can be invalidated as a result of the
670648
// inliner's activity.
671649
OwnedDefaultAdvisor =
672-
std::make_unique<DefaultInlineAdvisor>(FAM, getInlineParams());
650+
std::make_unique<DefaultInlineAdvisor>(M, FAM, getInlineParams());
673651
return *OwnedDefaultAdvisor;
674652
}
675653
assert(IAA->getAdvisor() &&
@@ -698,13 +676,6 @@ PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
698676

699677
auto AdvisorOnExit = make_scope_exit([&] { Advisor.onPassExit(); });
700678

701-
if (!ImportedFunctionsStats &&
702-
InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No) {
703-
ImportedFunctionsStats =
704-
std::make_unique<ImportedFunctionsInliningStatistics>();
705-
ImportedFunctionsStats->setModuleInfo(M);
706-
}
707-
708679
// We use a single common worklist for calls across the entire SCC. We
709680
// process these in-order and append new calls introduced during inlining to
710681
// the end.
@@ -879,9 +850,6 @@ PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
879850
}
880851
}
881852

882-
if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No)
883-
ImportedFunctionsStats->recordInline(F, Callee);
884-
885853
// Merge the attributes based on the inlining.
886854
AttributeFuncs::mergeAttributesForInlining(F, Callee);
887855

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1962,7 +1962,7 @@ bool SampleProfileLoader::doInitialization(Module &M,
19621962

19631963
if (FAM && !ProfileInlineReplayFile.empty()) {
19641964
ExternalInlineAdvisor = std::make_unique<ReplayInlineAdvisor>(
1965-
*FAM, Ctx, ProfileInlineReplayFile, /*EmitRemarks=*/false);
1965+
M, *FAM, Ctx, ProfileInlineReplayFile, /*EmitRemarks=*/false);
19661966
if (!ExternalInlineAdvisor->areReplayRemarksLoaded())
19671967
ExternalInlineAdvisor.reset();
19681968
}

llvm/test/Transforms/Inline/inline_stats.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
; RUN: opt -S -passes=inliner-wrapper-no-mandatory-first -inliner-function-import-stats=basic < %s 2>&1 | FileCheck %s --check-prefixes=CHECK-BASIC,CHECK
1010
; RUN: opt -S -passes=inliner-wrapper-no-mandatory-first -inliner-function-import-stats=verbose < %s 2>&1 | FileCheck %s --check-prefixes="CHECK-VERBOSE",CHECK
1111

12+
; RUN: opt -S -passes=inliner-wrapper -inliner-function-import-stats=basic < %s 2>&1 | FileCheck %s --check-prefixes=CHECK-BASIC,CHECK
13+
; RUN: opt -S -passes=inliner-wrapper -inliner-function-import-stats=verbose < %s 2>&1 | FileCheck %s --check-prefixes=CHECK-VERBOSE,CHECK
14+
1215
; CHECK: ------- Dumping inliner stats for [<stdin>] -------
1316
; CHECK-BASIC-NOT: -- List of inlined functions:
1417
; CHECK-BASIC-NOT: -- Inlined not imported function

0 commit comments

Comments
 (0)