Skip to content

Commit e586a68

Browse files
committed
IR: More tight inline cost model.
1 parent 84842a6 commit e586a68

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

include/config/config.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ constexpr auto GVNPRE_SKIP_BLOCK_THRESHOLD = 1000;
3838
constexpr auto GVNPRE_SKIP_NESTED_EXPR_THRESHOLD = 128;
3939

4040
// Function Inline
41-
constexpr auto FUNCTION_INLINE_INST_THRESHOLD = 1000;
41+
constexpr auto FUNCTION_INLINE_RECURSIVE_EXPAND_THRESHOLD = 100;
42+
constexpr auto FUNCTION_INLINE_INST_THRESHOLD = 200;
4243

4344
// Loop Elimination
4445
// LoopElim attempts to expand SCEVExpr to make loops trivially eliminable.

lib/ir/passes/transforms/inline.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,36 @@ struct InlineCandidate {
1515
pFunc callee;
1616
};
1717

18-
bool isProfitableToInline(const Function& caller, const InlineCandidate &candidate) {
19-
auto& callee = *candidate.callee;
20-
auto& call_points = candidate.call_points;
18+
bool isProfitableToInline(const Function &caller, const InlineCandidate &candidate) {
19+
auto &callee = *candidate.callee;
20+
auto &call_points = candidate.call_points;
2121

2222
// Do not inline function that can be memoized
2323
if (isProfitableToMemoize(callee))
2424
return false;
2525

2626
// Expand recursive call once can have better performance
27-
if (callee.isRecursive() && &caller != &callee)
28-
return false;
27+
if (callee.isRecursive()) {
28+
if (&caller != &callee)
29+
return false;
30+
31+
if (callee.getInstCount() * call_points.size() > Config::IR::FUNCTION_INLINE_RECURSIVE_EXPAND_THRESHOLD) {
32+
Logger::logDebug("[Inline]: Canceled expanding recursive function '", callee.getName(),
33+
"', due to too many instructions.(", call_points.size(), " calls, with each ",
34+
callee.getInstCount(), " instructions)");
35+
return false;
36+
}
37+
38+
return true;
39+
}
40+
41+
if (call_points.size() == 1)
42+
return true;
2943

3044
if (callee.getInstCount() * call_points.size() > Config::IR::FUNCTION_INLINE_INST_THRESHOLD) {
3145
Logger::logDebug("[Inline]: Canceled inlining '", callee.getName(), "' into '", caller.getName(),
32-
"', due to too many instructions");
46+
"', due to too many instructions. (", call_points.size(), " calls, with each ",
47+
callee.getInstCount(), " instructions)");
3348
return false;
3449
}
3550
return true;
@@ -43,6 +58,8 @@ void doInline(Function &caller, const pCall &call) {
4358

4459
if (candidate->isRecursive())
4560
Logger::logDebug("[Inline]: Expanding recursive function '", candidate->getName(), "'.");
61+
else
62+
Logger::logDebug("[Inline]: Inlining function '", candidate->getName(), "' into '", caller.getName(), "'.");
4663

4764
auto cloned = makeClone(candidate);
4865

@@ -150,7 +167,7 @@ PM::PreservedAnalyses InlinePass::run(Function &function, FAM &fam) {
150167
if (auto call = inst->as<CALLInst>()) {
151168
auto callee_def = call->getFunc()->as<Function>();
152169
if (callee_def != nullptr) {
153-
auto& candidate = candidates[callee_def];
170+
auto &candidate = candidates[callee_def];
154171
candidate.call_points.emplace_back(call);
155172
candidate.callee = callee_def;
156173
}

0 commit comments

Comments
 (0)