Skip to content

Commit ab2e766

Browse files
committed
[mlgo][inl] Interactive mode: optionally tell the default decision
This helps training algorithms that may want to sometimes replicate the default decision. The default decision is presented as an extra feature called `inlining_default`. It's not normally exported to save computation time. This is only available in interactive mode. Differential Revision: https://reviews.llvm.org/D147794
1 parent d453af5 commit ab2e766

File tree

7 files changed

+48
-21
lines changed

7 files changed

+48
-21
lines changed

llvm/include/llvm/Analysis/InlineAdvisor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ class InlineAdvisorAnalysisPrinterPass
357357
};
358358

359359
std::unique_ptr<InlineAdvisor>
360-
getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM);
360+
getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM,
361+
std::function<bool(CallBase &)> GetDefaultAdvice);
361362

362363
std::unique_ptr<InlineAdvisor>
363364
getDevelopmentModeAdvisor(Module &M, ModuleAnalysisManager &MAM,

llvm/include/llvm/Analysis/InlineModelFeatureMaps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ extern const std::vector<TensorSpec> FeatureMap;
134134
extern const char *const DecisionName;
135135
extern const TensorSpec InlineDecisionSpec;
136136
extern const char *const DefaultDecisionName;
137+
extern const TensorSpec DefaultDecisionSpec;
137138
extern const char *const RewardName;
138139

139140
using InlineFeatures = std::vector<int64_t>;

llvm/include/llvm/Analysis/MLInlineAdvisor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class MLInlineAdvice;
2828
class MLInlineAdvisor : public InlineAdvisor {
2929
public:
3030
MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM,
31-
std::unique_ptr<MLModelRunner> ModelRunner);
31+
std::unique_ptr<MLModelRunner> ModelRunner,
32+
std::function<bool(CallBase &)> GetDefaultAdvice);
3233

3334
virtual ~MLInlineAdvisor() = default;
3435

@@ -63,6 +64,7 @@ class MLInlineAdvisor : public InlineAdvisor {
6364
unsigned getInitialFunctionLevel(const Function &F) const;
6465

6566
std::unique_ptr<MLModelRunner> ModelRunner;
67+
std::function<bool(CallBase &)> GetDefaultAdvice;
6668

6769
private:
6870
int64_t getModuleIRSize() const;

llvm/lib/Analysis/DevelopmentModeInlineAdvisor.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ class DevelopmentModeMLInlineAdvisor : public MLInlineAdvisor {
165165
bool isLogging() const { return !!Logger; }
166166
std::unique_ptr<MLInlineAdvice> getMandatoryAdviceImpl(CallBase &CB) override;
167167

168-
std::function<bool(CallBase &)> GetDefaultAdvice;
169168
const bool IsDoingInference;
170169
std::unique_ptr<TrainingLogger> Logger;
171170

@@ -280,7 +279,7 @@ TrainingLogger::TrainingLogger(StringRef LogFileName,
280279
append_range(FT, MUTR->extraOutputsForLoggingSpecs());
281280

282281
DefaultDecisionPos = FT.size();
283-
FT.push_back(TensorSpec::createSpec<int64_t>(DefaultDecisionName, {1}));
282+
FT.push_back(DefaultDecisionSpec);
284283

285284
DecisionPos = FT.size();
286285
FT.push_back(InlineDecisionSpec);
@@ -331,8 +330,7 @@ DevelopmentModeMLInlineAdvisor::DevelopmentModeMLInlineAdvisor(
331330
std::unique_ptr<MLModelRunner> ModelRunner,
332331
std::function<bool(CallBase &)> GetDefaultAdvice,
333332
std::unique_ptr<TrainingLogger> Logger)
334-
: MLInlineAdvisor(M, MAM, std::move(ModelRunner)),
335-
GetDefaultAdvice(GetDefaultAdvice),
333+
: MLInlineAdvisor(M, MAM, std::move(ModelRunner), GetDefaultAdvice),
336334
IsDoingInference(isa<ModelUnderTrainingRunner>(getModelRunner())),
337335
Logger(std::move(Logger)),
338336
InitialNativeSize(isLogging() ? getTotalSizeEstimate() : 0),

llvm/lib/Analysis/InlineAdvisor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ bool InlineAdvisorAnalysis::Result::tryCreate(
208208
Advisor.reset(DA.Factory(M, FAM, Params, IC));
209209
return !!Advisor;
210210
}
211+
auto GetDefaultAdvice = [&FAM, Params](CallBase &CB) {
212+
auto OIC = getDefaultInlineAdvice(CB, FAM, Params);
213+
return OIC.has_value();
214+
};
211215
switch (Mode) {
212216
case InliningAdvisorMode::Default:
213217
LLVM_DEBUG(dbgs() << "Using default inliner heuristic.\n");
@@ -223,16 +227,12 @@ bool InlineAdvisorAnalysis::Result::tryCreate(
223227
case InliningAdvisorMode::Development:
224228
#ifdef LLVM_HAVE_TFLITE
225229
LLVM_DEBUG(dbgs() << "Using development-mode inliner policy.\n");
226-
Advisor =
227-
llvm::getDevelopmentModeAdvisor(M, MAM, [&FAM, Params](CallBase &CB) {
228-
auto OIC = getDefaultInlineAdvice(CB, FAM, Params);
229-
return OIC.has_value();
230-
});
230+
Advisor = llvm::getDevelopmentModeAdvisor(M, MAM, GetDefaultAdvice);
231231
#endif
232232
break;
233233
case InliningAdvisorMode::Release:
234234
LLVM_DEBUG(dbgs() << "Using release-mode inliner policy.\n");
235-
Advisor = llvm::getReleaseModeAdvisor(M, MAM);
235+
Advisor = llvm::getReleaseModeAdvisor(M, MAM, GetDefaultAdvice);
236236
break;
237237
}
238238

llvm/lib/Analysis/MLInlineAdvisor.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ static cl::opt<std::string> InteractiveChannelBaseName(
3838
"Base file path for the interactive mode. The incoming filename should "
3939
"have the name <inliner-interactive-channel-base>.in, while the "
4040
"outgoing name should be <inliner-interactive-channel-base>.out"));
41+
static cl::opt<bool> InteractiveIncludeDefault(
42+
"inliner-interactive-include-default", cl::Hidden,
43+
cl::desc(
44+
(Twine("In interactive mode, also send the default policy decision: ") +
45+
DefaultDecisionName + ".")
46+
.str()));
4147

4248
#if defined(LLVM_HAVE_TF_AOT_INLINERSIZEMODEL)
4349
// codegen-ed file
@@ -48,20 +54,26 @@ using CompiledModelType = NoopSavedModelImpl;
4854
#endif
4955

5056
std::unique_ptr<InlineAdvisor>
51-
llvm::getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM) {
57+
llvm::getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM,
58+
std::function<bool(CallBase &)> GetDefaultAdvice) {
5259
if (!llvm::isEmbeddedModelEvaluatorValid<CompiledModelType>() &&
5360
InteractiveChannelBaseName.empty())
5461
return nullptr;
5562
std::unique_ptr<MLModelRunner> AOTRunner;
5663
if (InteractiveChannelBaseName.empty())
5764
AOTRunner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>(
5865
M.getContext(), FeatureMap, DecisionName);
59-
else
66+
else {
67+
auto Features = FeatureMap;
68+
if (InteractiveIncludeDefault)
69+
Features.push_back(DefaultDecisionSpec);
6070
AOTRunner = std::make_unique<InteractiveModelRunner>(
61-
M.getContext(), FeatureMap, InlineDecisionSpec,
71+
M.getContext(), Features, InlineDecisionSpec,
6272
InteractiveChannelBaseName + ".out",
6373
InteractiveChannelBaseName + ".in");
64-
return std::make_unique<MLInlineAdvisor>(M, MAM, std::move(AOTRunner));
74+
}
75+
return std::make_unique<MLInlineAdvisor>(M, MAM, std::move(AOTRunner),
76+
GetDefaultAdvice);
6577
}
6678

6779
#define DEBUG_TYPE "inline-ml"
@@ -96,6 +108,8 @@ const char *const llvm::DecisionName = "inlining_decision";
96108
const TensorSpec llvm::InlineDecisionSpec =
97109
TensorSpec::createSpec<int64_t>(DecisionName, {1});
98110
const char *const llvm::DefaultDecisionName = "inlining_default";
111+
const TensorSpec llvm::DefaultDecisionSpec =
112+
TensorSpec::createSpec<int64_t>(DefaultDecisionName, {1});
99113
const char *const llvm::RewardName = "delta_size";
100114

101115
CallBase *getInlinableCS(Instruction &I) {
@@ -108,11 +122,13 @@ CallBase *getInlinableCS(Instruction &I) {
108122
return nullptr;
109123
}
110124

111-
MLInlineAdvisor::MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM,
112-
std::unique_ptr<MLModelRunner> Runner)
125+
MLInlineAdvisor::MLInlineAdvisor(
126+
Module &M, ModuleAnalysisManager &MAM,
127+
std::unique_ptr<MLModelRunner> Runner,
128+
std::function<bool(CallBase &)> GetDefaultAdvice)
113129
: InlineAdvisor(
114130
M, MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()),
115-
ModelRunner(std::move(Runner)),
131+
ModelRunner(std::move(Runner)), GetDefaultAdvice(GetDefaultAdvice),
116132
CG(MAM.getResult<LazyCallGraphAnalysis>(M)),
117133
InitialIRSize(getModuleIRSize()), CurrentIRSize(InitialIRSize) {
118134
assert(ModelRunner);
@@ -393,7 +409,10 @@ std::unique_ptr<InlineAdvice> MLInlineAdvisor::getAdviceImpl(CallBase &CB) {
393409
*ModelRunner->getTensor<int64_t>(inlineCostFeatureToMlFeature(
394410
static_cast<InlineCostFeatureIndex>(I))) = CostFeatures->at(I);
395411
}
396-
412+
// This one would have been set up to be right at the end.
413+
if (!InteractiveChannelBaseName.empty() && InteractiveIncludeDefault)
414+
*ModelRunner->getTensor<int64_t>(InlineCostFeatureIndex::NumberOfFeatures) =
415+
GetDefaultAdvice(CB);
397416
return getAdviceFromModel(CB, ORE);
398417
}
399418

llvm/test/Transforms/Inline/ML/interactive-mode.ll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
; RUN: cp %S/Inputs/interactive_main.py %t.rundir
88
; RUN: %python %t.rundir/interactive_main.py %t.channel-basename \
99
; RUN: opt -passes=scc-oz-module-inliner -interactive-model-runner-echo-reply \
10-
; RUN: -enable-ml-inliner=release --inliner-interactive-channel-base=%t.channel-basename %S/Inputs/test-module.ll -S -o /dev/null | FileCheck %s
10+
; RUN: -enable-ml-inliner=release -inliner-interactive-channel-base=%t.channel-basename %S/Inputs/test-module.ll -S -o /dev/null | FileCheck %s
11+
; RUN: %python %t.rundir/interactive_main.py %t.channel-basename \
12+
; RUN: opt -passes=scc-oz-module-inliner -interactive-model-runner-echo-reply \
13+
; RUN: -inliner-interactive-include-default -enable-ml-inliner=release \
14+
; RUN: -inliner-interactive-channel-base=%t.channel-basename %S/Inputs/test-module.ll -S -o /dev/null | FileCheck %s -check-prefixes=CHECK,CHECK-DEFAULT
15+
1116

1217
;; It'd be nice if we had stdout and stderr interleaved, but we don't, so
1318
;; let's just check the features have non-zero values, and that we see as many
@@ -17,6 +22,7 @@
1722
; CHECK-NEXT: sroa_savings: 0
1823
; CHECK: unsimplified_common_instructions: 5
1924
; CHECK: callee_users: 3
25+
; CHECK-DEFAULT: inlining_default: 0
2026
; CHECK: observation: 5
2127
; CHECK-NOT: observation: 6
2228

0 commit comments

Comments
 (0)