Skip to content

Commit 99f0063

Browse files
Jacob Hegnajacob-hegna
Jacob Hegna
authored andcommitted
Unpack the CostEstimate feature in ML inlining models.
This change yields an additional 2% size reduction on an internal search binary, and an additional 0.5% size reduction on fuchsia. Differential Revision: https://reviews.llvm.org/D104751
1 parent dba74c6 commit 99f0063

File tree

8 files changed

+458
-25
lines changed

8 files changed

+458
-25
lines changed

llvm/include/llvm/Analysis/InlineCost.h

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "llvm/Analysis/AssumptionCache.h"
1717
#include "llvm/Analysis/CallGraphSCCPass.h"
18+
#include "llvm/Analysis/InlineModelFeatureMaps.h"
1819
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
1920
#include <cassert>
2021
#include <climits>
@@ -270,6 +271,15 @@ Optional<int> getInliningCostEstimate(
270271
ProfileSummaryInfo *PSI = nullptr,
271272
OptimizationRemarkEmitter *ORE = nullptr);
272273

274+
/// Get the expanded cost features. The features are returned unconditionally,
275+
/// even if inlining is impossible.
276+
Optional<InlineCostFeatures> getInliningCostFeatures(
277+
CallBase &Call, TargetTransformInfo &CalleeTTI,
278+
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
279+
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
280+
ProfileSummaryInfo *PSI = nullptr,
281+
OptimizationRemarkEmitter *ORE = nullptr);
282+
273283
/// Minimal filter to detect invalid constructs for inlining.
274284
InlineResult isInlineViable(Function &Callee);
275285

llvm/include/llvm/Analysis/InlineModelFeatureMaps.h

+70-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,61 @@
1616

1717
namespace llvm {
1818

19+
// List of cost features. A "cost" feature is a summand of the heuristic-based
20+
// inline cost, and we define them separately to preserve the original heuristic
21+
// behavior.
22+
#define INLINE_COST_FEATURE_ITERATOR(M) \
23+
M(SROASavings, "sroa_savings") \
24+
M(SROALosses, "sroa_losses") \
25+
M(LoadElimination, "load_elimination") \
26+
M(CallPenalty, "call_penalty") \
27+
M(CallArgumentSetup, "call_argument_setup") \
28+
M(LoadRelativeIntrinsic, "load_relative_intrinsic") \
29+
M(LoweredCallArgSetup, "lowered_call_arg_setup") \
30+
M(IndirectCallPenalty, "indirect_call_penalty") \
31+
M(JumpTablePenalty, "jump_table_penalty") \
32+
M(CaseClusterPenalty, "case_cluster_penalty") \
33+
M(SwitchPenalty, "switch_penalty") \
34+
M(UnsimplifiedCommonInstructions, "unsimplified_common_instructions") \
35+
M(NumLoops, "num_loops") \
36+
M(DeadBlocks, "dead_blocks") \
37+
M(SimplifiedInstructions, "simplified_instructions") \
38+
M(ConstantArgs, "constant_args") \
39+
M(ConstantOffsetPtrArgs, "constant_offset_ptr_args") \
40+
M(CallSiteCost, "callsite_cost") \
41+
M(ColdCcPenalty, "cold_cc_penalty") \
42+
M(LastCallToStaticBonus, "last_call_to_static_bonus") \
43+
M(IsMultipleBlocks, "is_multiple_blocks") \
44+
M(NestedInlines, "nested_inlines") \
45+
M(NestedInlineCostEstimate, "nested_inline_cost_estimate") \
46+
M(Threshold, "threshold")
47+
48+
// clang-format off
49+
enum class InlineCostFeatureIndex : size_t {
50+
#define POPULATE_INDICES(INDEX_NAME, NAME) INDEX_NAME,
51+
INLINE_COST_FEATURE_ITERATOR(POPULATE_INDICES)
52+
#undef POPULATE_INDICES
53+
54+
NumberOfFeatures
55+
};
56+
// clang-format on
57+
58+
using InlineCostFeatures =
59+
std::array<int,
60+
static_cast<size_t>(InlineCostFeatureIndex::NumberOfFeatures)>;
61+
62+
constexpr bool isHeuristicInlineCostFeature(InlineCostFeatureIndex Feature) {
63+
return Feature != InlineCostFeatureIndex::SROASavings &&
64+
Feature != InlineCostFeatureIndex::IsMultipleBlocks &&
65+
Feature != InlineCostFeatureIndex::DeadBlocks &&
66+
Feature != InlineCostFeatureIndex::SimplifiedInstructions &&
67+
Feature != InlineCostFeatureIndex::ConstantArgs &&
68+
Feature != InlineCostFeatureIndex::ConstantOffsetPtrArgs &&
69+
Feature != InlineCostFeatureIndex::NestedInlines &&
70+
Feature != InlineCostFeatureIndex::NestedInlineCostEstimate &&
71+
Feature != InlineCostFeatureIndex::Threshold;
72+
}
73+
1974
// List of features. Each feature is defined through a triple:
2075
// - the name of an enum member, which will be the feature index
2176
// - a textual name, used for Tensorflow model binding (so it needs to match the
@@ -48,12 +103,26 @@ namespace llvm {
48103
"number of module-internal users of the callee, +1 if the callee is " \
49104
"exposed externally")
50105

106+
// clang-format off
51107
enum class FeatureIndex : size_t {
108+
// InlineCost features - these must come first
109+
#define POPULATE_INDICES(INDEX_NAME, NAME) INDEX_NAME,
110+
INLINE_COST_FEATURE_ITERATOR(POPULATE_INDICES)
111+
#undef POPULATE_INDICES
112+
113+
// Non-cost features
52114
#define POPULATE_INDICES(INDEX_NAME, NAME, COMMENT) INDEX_NAME,
53115
INLINE_FEATURE_ITERATOR(POPULATE_INDICES)
54116
#undef POPULATE_INDICES
55-
NumberOfFeatures
117+
118+
NumberOfFeatures
56119
};
120+
// clang-format on
121+
122+
constexpr FeatureIndex
123+
inlineCostFeatureToMlFeature(InlineCostFeatureIndex Feature) {
124+
return static_cast<FeatureIndex>(static_cast<size_t>(Feature));
125+
}
57126

58127
constexpr size_t NumberOfFeatures =
59128
static_cast<size_t>(FeatureIndex::NumberOfFeatures);

llvm/lib/Analysis/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if (DEFINED LLVM_HAVE_TF_AOT OR DEFINED LLVM_HAVE_TF_API)
55
# This url points to the most recent most which is known to be compatible with
66
# LLVM. When better models are published, this url should be updated to aid
77
# discoverability.
8-
set(LLVM_INLINER_MODEL_CURRENT_URL "https://github.com/google/ml-compiler-opt/releases/download/inlining-Oz-v0.1/inlining-Oz-acabaf6-v0.1.tar.gz")
8+
set(LLVM_INLINER_MODEL_CURRENT_URL "TO_BE_UPDATED")
99

1010
if (DEFINED LLVM_HAVE_TF_AOT)
1111
# If the path is empty, autogenerate the model

0 commit comments

Comments
 (0)