Skip to content

Commit d97f776

Browse files
committed
Revert "[NPM][Inliner] Factor ImportedFunctionStats in the InlineAdvisor"
This reverts commit e8aec76.
1 parent ff3b51b commit d97f776

File tree

12 files changed

+56
-77
lines changed

12 files changed

+56
-77
lines changed

llvm/include/llvm/Analysis/InlineAdvisor.h

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

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

7173
/// Call after inlining succeeded, and resulted in deleting the callee.
7274
void recordInliningWithCalleeDeleted();
@@ -112,7 +114,6 @@ class InlineAdvice {
112114
assert(!Recorded && "Recording should happen exactly once");
113115
Recorded = true;
114116
}
115-
void recordInlineStatsIfNeeded();
116117

117118
bool Recorded = false;
118119
};
@@ -140,7 +141,7 @@ class DefaultInlineAdvice : public InlineAdvice {
140141
class InlineAdvisor {
141142
public:
142143
InlineAdvisor(InlineAdvisor &&) = delete;
143-
virtual ~InlineAdvisor();
144+
virtual ~InlineAdvisor() { freeDeletedFunctions(); }
144145

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

164165
protected:
165-
InlineAdvisor(Module &M, FunctionAnalysisManager &FAM);
166+
InlineAdvisor(FunctionAnalysisManager &FAM) : FAM(FAM) {}
166167
virtual std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) = 0;
167168
virtual std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
168169
bool Advice);
169170

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

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

210208
private:
211209
std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;

llvm/include/llvm/Analysis/MLInlineAdvisor.h

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

53+
Module &M;
5354
std::unique_ptr<MLModelRunner> ModelRunner;
5455

5556
private:

llvm/include/llvm/Analysis/ReplayInlineAdvisor.h

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

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

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

102103
PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM,
@@ -105,6 +106,7 @@ class InlinerPass : public PassInfoMixin<InlinerPass> {
105106
private:
106107
InlineAdvisor &getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
107108
FunctionAnalysisManager &FAM, Module &M);
109+
std::unique_ptr<ImportedFunctionsInliningStatistics> ImportedFunctionsStats;
108110
std::unique_ptr<DefaultInlineAdvisor> OwnedDefaultAdvisor;
109111
const bool OnlyMandatory;
110112
};

llvm/include/llvm/Transforms/Utils/ImportedFunctionsInliningStatistics.h

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

104-
enum class InlinerFunctionImportStatsOpts {
105-
No = 0,
106-
Basic = 1,
107-
Verbose = 2,
108-
};
109-
110104
} // llvm
111105

112106
#endif // LLVM_TRANSFORMS_UTILS_IMPORTEDFUNCTIONSINLININGSTATISTICS_H

llvm/lib/Analysis/InlineAdvisor.cpp

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ 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-
5351
void DefaultInlineAdvice::recordUnsuccessfulInliningImpl(
5452
const InlineResult &Result) {
5553
using namespace ore;
@@ -132,20 +130,8 @@ void InlineAdvisor::freeDeletedFunctions() {
132130
DeletedFunctions.clear();
133131
}
134132

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-
146133
void InlineAdvice::recordInliningWithCalleeDeleted() {
147134
markRecorded();
148-
recordInlineStatsIfNeeded();
149135
Advisor->markFunctionAsDeleted(Callee);
150136
recordInliningWithCalleeDeletedImpl();
151137
}
@@ -157,7 +143,7 @@ bool InlineAdvisorAnalysis::Result::tryCreate(InlineParams Params,
157143
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
158144
switch (Mode) {
159145
case InliningAdvisorMode::Default:
160-
Advisor.reset(new DefaultInlineAdvisor(M, FAM, Params));
146+
Advisor.reset(new DefaultInlineAdvisor(FAM, Params));
161147
break;
162148
case InliningAdvisorMode::Development:
163149
#ifdef LLVM_HAVE_TF_API
@@ -442,25 +428,6 @@ void llvm::emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
442428
});
443429
}
444430

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-
464431
std::unique_ptr<InlineAdvice> InlineAdvisor::getMandatoryAdvice(CallBase &CB,
465432
bool Advice) {
466433
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-
M, MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()),
70-
ModelRunner(std::move(Runner)), CG(new CallGraph(M)),
69+
MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()),
70+
M(M), ModelRunner(std::move(Runner)), CG(new CallGraph(M)),
7171
InitialIRSize(getModuleIRSize()), CurrentIRSize(InitialIRSize) {
7272
assert(ModelRunner);
7373

llvm/lib/Analysis/ReplayInlineAdvisor.cpp

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

2323
#define DEBUG_TYPE "inline-replay"
2424

25-
ReplayInlineAdvisor::ReplayInlineAdvisor(Module &M,
26-
FunctionAnalysisManager &FAM,
25+
ReplayInlineAdvisor::ReplayInlineAdvisor(FunctionAnalysisManager &FAM,
2726
LLVMContext &Context,
2827
StringRef RemarksFile,
2928
bool EmitRemarks)
30-
: InlineAdvisor(M, FAM), HasReplayRemarks(false), EmitRemarks(EmitRemarks) {
29+
: InlineAdvisor(FAM), HasReplayRemarks(false), EmitRemarks(EmitRemarks) {
3130
auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(RemarksFile);
3231
std::error_code EC = BufferOrErr.getError();
3332
if (EC) {

llvm/lib/Transforms/IPO/Inliner.cpp

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

93-
extern cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats;
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"));
94111

95112
LegacyInlinerBase::LegacyInlinerBase(char &ID) : CallGraphSCCPass(ID) {}
96113

@@ -630,12 +647,17 @@ bool LegacyInlinerBase::removeDeadFunctions(CallGraph &CG,
630647
return true;
631648
}
632649

650+
InlinerPass::~InlinerPass() {
651+
if (ImportedFunctionsStats) {
652+
assert(InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No);
653+
ImportedFunctionsStats->dump(InlinerFunctionImportStats ==
654+
InlinerFunctionImportStatsOpts::Verbose);
655+
}
656+
}
657+
633658
InlineAdvisor &
634659
InlinerPass::getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
635660
FunctionAnalysisManager &FAM, Module &M) {
636-
if (OwnedDefaultAdvisor)
637-
return *OwnedDefaultAdvisor;
638-
639661
auto *IAA = MAM.getCachedResult<InlineAdvisorAnalysis>(M);
640662
if (!IAA) {
641663
// It should still be possible to run the inliner as a stand-alone SCC pass,
@@ -647,7 +669,7 @@ InlinerPass::getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
647669
// The one we would get from the MAM can be invalidated as a result of the
648670
// inliner's activity.
649671
OwnedDefaultAdvisor =
650-
std::make_unique<DefaultInlineAdvisor>(M, FAM, getInlineParams());
672+
std::make_unique<DefaultInlineAdvisor>(FAM, getInlineParams());
651673
return *OwnedDefaultAdvisor;
652674
}
653675
assert(IAA->getAdvisor() &&
@@ -676,6 +698,13 @@ PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
676698

677699
auto AdvisorOnExit = make_scope_exit([&] { Advisor.onPassExit(); });
678700

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

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

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-
M, *FAM, Ctx, ProfileInlineReplayFile, /*EmitRemarks=*/false);
1965+
*FAM, Ctx, ProfileInlineReplayFile, /*EmitRemarks=*/false);
19661966
if (!ExternalInlineAdvisor->areReplayRemarksLoaded())
19671967
ExternalInlineAdvisor.reset();
19681968
}

llvm/lib/Transforms/Utils/ImportedFunctionsInliningStatistics.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,13 @@
1313
#include "llvm/ADT/STLExtras.h"
1414
#include "llvm/IR/Function.h"
1515
#include "llvm/IR/Module.h"
16-
#include "llvm/Support/CommandLine.h"
1716
#include "llvm/Support/Debug.h"
1817
#include "llvm/Support/raw_ostream.h"
1918
#include <algorithm>
2019
#include <iomanip>
2120
#include <sstream>
2221
using namespace llvm;
2322

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-
3323
ImportedFunctionsInliningStatistics::InlineGraphNode &
3424
ImportedFunctionsInliningStatistics::createInlineGraphNode(const Function &F) {
3525

llvm/test/Transforms/Inline/inline_stats.ll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
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-
1512
; CHECK: ------- Dumping inliner stats for [<stdin>] -------
1613
; CHECK-BASIC-NOT: -- List of inlined functions:
1714
; CHECK-BASIC-NOT: -- Inlined not imported function

0 commit comments

Comments
 (0)