Skip to content

Commit a36e1a9

Browse files
authored
Merge pull request #41821 from nate-chandler/performance_inliner/added-simple-printing-helpers
[SILOpt] Print functions during inlining.
2 parents 52e0534 + 5b5ed56 commit a36e1a9

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static llvm::cl::opt<DebugOnlyPassNumberOpt, true,
167167
llvm::cl::location(DebugOnlyPassNumberOptLoc),
168168
llvm::cl::ValueRequired);
169169

170-
static bool isFunctionSelectedForPrinting(SILFunction *F) {
170+
bool isFunctionSelectedForPrinting(SILFunction *F) {
171171
if (!SILPrintFunction.empty() && SILPrintFunction.end() ==
172172
std::find(SILPrintFunction.begin(), SILPrintFunction.end(), F->getName()))
173173
return false;

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,59 @@ llvm::cl::opt<bool> EnableVerifyAfterInlining(
4949
llvm::cl::desc("Run sil verification after inlining all found callee apply "
5050
"sites into a caller."));
5151

52+
llvm::cl::opt<bool> SILPrintInliningCallee(
53+
"sil-print-inlining-callee", llvm::cl::init(false),
54+
llvm::cl::desc("Print functions that are inlined into other functions."));
55+
56+
llvm::cl::opt<bool> SILPrintInliningCallerBefore(
57+
"sil-print-inlining-caller-before", llvm::cl::init(false),
58+
llvm::cl::desc(
59+
"Print functions into which another function is about to be inlined."));
60+
61+
llvm::cl::opt<bool> SILPrintInliningCallerAfter(
62+
"sil-print-inlining-caller-after", llvm::cl::init(false),
63+
llvm::cl::desc(
64+
"Print functions into which another function has been inlined."));
65+
66+
//===----------------------------------------------------------------------===//
67+
// Printing Helpers
68+
//===----------------------------------------------------------------------===//
69+
70+
extern bool isFunctionSelectedForPrinting(SILFunction *F);
71+
72+
static void printInliningDetails(StringRef passName, SILFunction *caller,
73+
SILFunction *callee, bool isCaller,
74+
bool alreadyInlined) {
75+
if (!isFunctionSelectedForPrinting(caller))
76+
return;
77+
llvm::dbgs() << " " << passName
78+
<< (alreadyInlined ? " has inlined " : " will inline ")
79+
<< callee->getName() << " into " << caller->getName() << ".\n";
80+
auto *printee = isCaller ? caller : callee;
81+
printee->dump(caller->getModule().getOptions().EmitVerboseSIL);
82+
llvm::dbgs() << '\n';
83+
}
84+
85+
static void printInliningDetailsCallee(StringRef passName, SILFunction *caller,
86+
SILFunction *callee) {
87+
printInliningDetails(passName, caller, callee, /*isCaller=*/false,
88+
/*alreadyInlined=*/false);
89+
}
90+
91+
static void printInliningDetailsCallerBefore(StringRef passName,
92+
SILFunction *caller,
93+
SILFunction *callee) {
94+
printInliningDetails(passName, caller, callee, /*isCaller=*/true,
95+
/*alreadyInlined=*/false);
96+
}
97+
98+
static void printInliningDetailsCallerAfter(StringRef passName,
99+
SILFunction *caller,
100+
SILFunction *callee) {
101+
printInliningDetails(passName, caller, callee, /*isCaller=*/true,
102+
/*alreadyInlined=*/true);
103+
}
104+
52105
//===----------------------------------------------------------------------===//
53106
// Performance Inliner
54107
//===----------------------------------------------------------------------===//
@@ -58,6 +111,7 @@ namespace {
58111
using Weight = ShortestPathAnalysis::Weight;
59112

60113
class SILPerformanceInliner {
114+
StringRef PassName;
61115
SILOptFunctionBuilder &FuncBuilder;
62116

63117
/// Specifies which functions not to inline, based on @_semantics and
@@ -190,12 +244,13 @@ class SILPerformanceInliner {
190244
SmallVectorImpl<FullApplySite> &Applies);
191245

192246
public:
193-
SILPerformanceInliner(SILOptFunctionBuilder &FuncBuilder,
194-
InlineSelection WhatToInline, DominanceAnalysis *DA,
247+
SILPerformanceInliner(StringRef PassName, SILOptFunctionBuilder &FuncBuilder,
248+
InlineSelection WhatToInline, DominanceAnalysis *DA,
195249
SILLoopAnalysis *LA, SideEffectAnalysis *SEA,
196250
OptimizationMode OptMode, OptRemark::Emitter &ORE)
197-
: FuncBuilder(FuncBuilder), WhatToInline(WhatToInline), DA(DA), LA(LA),
198-
SEA(SEA), CBI(DA), ORE(ORE), OptMode(OptMode) {}
251+
: PassName(PassName), FuncBuilder(FuncBuilder),
252+
WhatToInline(WhatToInline), DA(DA), LA(LA), SEA(SEA), CBI(DA), ORE(ORE),
253+
OptMode(OptMode) {}
199254

200255
bool inlineCallsIntoFunction(SILFunction *F);
201256
};
@@ -975,6 +1030,12 @@ bool SILPerformanceInliner::inlineCallsIntoFunction(SILFunction *Caller) {
9751030
// will be deleted after inlining.
9761031
invalidatedStackNesting |= SILInliner::invalidatesStackNesting(AI);
9771032

1033+
if (SILPrintInliningCallee) {
1034+
printInliningDetailsCallee(PassName, Caller, Callee);
1035+
}
1036+
if (SILPrintInliningCallerBefore) {
1037+
printInliningDetailsCallerBefore(PassName, Caller, Callee);
1038+
}
9781039
// We've already determined we should be able to inline this, so
9791040
// unconditionally inline the function.
9801041
//
@@ -983,6 +1044,9 @@ bool SILPerformanceInliner::inlineCallsIntoFunction(SILFunction *Caller) {
9831044
SILInliner::inlineFullApply(AI, SILInliner::InlineKind::PerformanceInline,
9841045
FuncBuilder, deleter);
9851046
++NumFunctionsInlined;
1047+
if (SILPrintInliningCallerAfter) {
1048+
printInliningDetailsCallerAfter(PassName, Caller, Callee);
1049+
}
9861050
}
9871051
deleter.cleanupDeadInstructions();
9881052

@@ -1056,8 +1120,9 @@ class SILPerformanceInlinerPass : public SILFunctionTransform {
10561120
auto OptMode = getFunction()->getEffectiveOptimizationMode();
10571121

10581122
SILOptFunctionBuilder FuncBuilder(*this);
1059-
SILPerformanceInliner Inliner(FuncBuilder, WhatToInline, DA, LA, SEA,
1060-
OptMode, ORE);
1123+
1124+
SILPerformanceInliner Inliner(getID(), FuncBuilder, WhatToInline, DA, LA,
1125+
SEA, OptMode, ORE);
10611126

10621127
assert(getFunction()->isDefinition() &&
10631128
"Expected only functions with bodies!");

0 commit comments

Comments
 (0)