1 //===- InlineModelFeatureMaps.h - common model runner defs ------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9
10 #ifndef LLVM_ANALYSIS_INLINEMODELFEATUREMAPS_H
11 #define LLVM_ANALYSIS_INLINEMODELFEATUREMAPS_H
12
13 #include "llvm/Analysis/TensorSpec.h"
14 #include "llvm/Support/Compiler.h"
15
16 #include <array>
17 #include <vector>
18
19 namespace llvm {
20
21 // List of cost features. A "cost" feature is a summand of the heuristic-based
22 // inline cost, and we define them separately to preserve the original heuristic
23 // behavior.
24 #define INLINE_COST_FEATURE_ITERATOR(M) \
25 M(int64_t, {1}, sroa_savings, \
26 "Savings from SROA (scalar replacement of aggregates)") \
27 M(int64_t, {1}, sroa_losses, \
28 "Losses from SROA (scalar replacement of aggregates)") \
29 M(int64_t, {1}, load_elimination, "Cost of load elimination in the call") \
30 M(int64_t, {1}, call_penalty, \
31 "Accumulation of penalty applied to call sites when inlining") \
32 M(int64_t, {1}, call_argument_setup, \
33 "Accumulation of call argument setup costs") \
34 M(int64_t, {1}, load_relative_intrinsic, \
35 "Accumulation of costs of loading relative intrinsics") \
36 M(int64_t, {1}, lowered_call_arg_setup, \
37 "Accumulation of cost of lowered call argument setups") \
38 M(int64_t, {1}, indirect_call_penalty, \
39 "Accumulation of costs for indirect calls") \
40 M(int64_t, {1}, jump_table_penalty, "Accumulation of costs for jump tables") \
41 M(int64_t, {1}, case_cluster_penalty, \
42 "Accumulation of costs for case clusters") \
43 M(int64_t, {1}, switch_default_dest_penalty, \
44 "Accumulation of costs for switch default destination") \
45 M(int64_t, {1}, switch_penalty, \
46 "Accumulation of costs for switch statements") \
47 M(int64_t, {1}, unsimplified_common_instructions, \
48 "Costs from unsimplified common instructions") \
49 M(int64_t, {1}, num_loops, "Number of loops in the caller") \
50 M(int64_t, {1}, dead_blocks, "Number of dead blocks in the caller") \
51 M(int64_t, {1}, simplified_instructions, \
52 "Number of simplified instructions") \
53 M(int64_t, {1}, constant_args, \
54 "Number of constant arguments in the call site") \
55 M(int64_t, {1}, constant_offset_ptr_args, \
56 "Number of constant offset pointer args in the call site") \
57 M(int64_t, {1}, callsite_cost, "Estimated cost of the call site") \
58 M(int64_t, {1}, cold_cc_penalty, "Penalty for a cold calling convention") \
59 M(int64_t, {1}, last_call_to_static_bonus, \
60 "Bonus for being the last call to static") \
61 M(int64_t, {1}, is_multiple_blocks, \
62 "Boolean; is the Callee multiple blocks") \
63 M(int64_t, {1}, nested_inlines, \
64 "Would the default inliner perfom nested inlining") \
65 M(int64_t, {1}, nested_inline_cost_estimate, \
66 "Estimate of the accumulated cost of nested inlines") \
67 M(int64_t, {1}, threshold, "Threshold for the heuristic inliner")
68
69 // clang-format off
70 enum class InlineCostFeatureIndex : size_t {
71 #define POPULATE_INDICES(DTYPE, SHAPE, NAME, DOC) NAME,
72 INLINE_COST_FEATURE_ITERATOR(POPULATE_INDICES)
73 #undef POPULATE_INDICES
74
75 NumberOfFeatures
76 };
77 // clang-format on
78
79 using InlineCostFeatures =
80 std::array<int,
81 static_cast<size_t>(InlineCostFeatureIndex::NumberOfFeatures)>;
82
isHeuristicInlineCostFeature(InlineCostFeatureIndex Feature)83 constexpr bool isHeuristicInlineCostFeature(InlineCostFeatureIndex Feature) {
84 return Feature != InlineCostFeatureIndex::sroa_savings &&
85 Feature != InlineCostFeatureIndex::is_multiple_blocks &&
86 Feature != InlineCostFeatureIndex::dead_blocks &&
87 Feature != InlineCostFeatureIndex::simplified_instructions &&
88 Feature != InlineCostFeatureIndex::constant_args &&
89 Feature != InlineCostFeatureIndex::constant_offset_ptr_args &&
90 Feature != InlineCostFeatureIndex::nested_inlines &&
91 Feature != InlineCostFeatureIndex::nested_inline_cost_estimate &&
92 Feature != InlineCostFeatureIndex::threshold;
93 }
94
95 // List of features. Each feature is defined through a triple:
96 // - the name of an enum member, which will be the feature index
97 // - a textual name, used for ML model binding (so it needs to match the
98 // names used by the ML model).
99 // - a documentation description. Currently, that is not used anywhere
100 // programmatically, and serves as workaround to inability of inserting comments
101 // in macros.
102 #define INLINE_FEATURE_ITERATOR(M) \
103 M(int64_t, {1}, callee_basic_block_count, \
104 "number of basic blocks of the callee") \
105 M(int64_t, {1}, callsite_height, \
106 "position of the call site in the original call graph - measured from " \
107 "the farthest SCC") \
108 M(int64_t, {1}, node_count, \
109 "total current number of defined functions in the module") \
110 M(int64_t, {1}, nr_ctant_params, \
111 "number of parameters in the call site that are constants") \
112 M(int64_t, {1}, cost_estimate, "total cost estimate (threshold - free)") \
113 M(int64_t, {1}, edge_count, "total number of calls in the module") \
114 M(int64_t, {1}, caller_users, \
115 "number of module-internal users of the caller, +1 if the caller is " \
116 "exposed externally") \
117 M(int64_t, {1}, caller_conditionally_executed_blocks, \
118 "number of blocks reached from a conditional instruction, in the caller") \
119 M(int64_t, {1}, caller_basic_block_count, \
120 "number of basic blocks in the caller") \
121 M(int64_t, {1}, callee_conditionally_executed_blocks, \
122 "number of blocks reached from a conditional instruction, in the callee") \
123 M(int64_t, {1}, callee_users, \
124 "number of module-internal users of the callee, +1 if the callee is " \
125 "exposed externally") \
126 M(int64_t, {1}, is_callee_avail_external, \
127 "Is callee an available-externally linkage type (i.e. could be DCEd if " \
128 "not " \
129 "fully inlined by ElimAvailExtern)") \
130 M(int64_t, {1}, is_caller_avail_external, \
131 "Is caller an available-externally linkage type (i.e. could be DCEd if " \
132 "not " \
133 "fully inlined by ElimAvailExtern)")
134
135 // Not all features listed in FeatureIndex are used by the ML model.
136 // Specifically, callee_embedding and caller_embedding are used only when the
137 // usage of IR2Vec embeddings is explicitly enabled. Meaning, the size/number of
138 // features is not static. So, we cannot determine number of features based on
139 // the number of elements in this enum.
140 // clang-format off
141 enum class FeatureIndex : size_t {
142 #define POPULATE_INDICES(DTYPE, SHAPE, NAME, COMMENT) NAME,
143 // InlineCost features - these must come first
144 INLINE_COST_FEATURE_ITERATOR(POPULATE_INDICES)
145
146 // Non-cost features
147 INLINE_FEATURE_ITERATOR(POPULATE_INDICES)
148 #undef POPULATE_INDICES
149
150 // IR2Vec embeddings
151 // Dimensions of embeddings are not known in the compile time (until vocab is
152 // read). Hence macros cannot be used here.
153 callee_embedding,
154 caller_embedding
155 };
156 // clang-format on
157
158 constexpr FeatureIndex
inlineCostFeatureToMlFeature(InlineCostFeatureIndex Feature)159 inlineCostFeatureToMlFeature(InlineCostFeatureIndex Feature) {
160 return static_cast<FeatureIndex>(static_cast<size_t>(Feature));
161 }
162
163 LLVM_ABI extern std::vector<TensorSpec> FeatureMap;
164
165 LLVM_ABI extern const char *const DecisionName;
166 LLVM_ABI extern const TensorSpec InlineDecisionSpec;
167 LLVM_ABI extern const char *const DefaultDecisionName;
168 LLVM_ABI extern const TensorSpec DefaultDecisionSpec;
169 LLVM_ABI extern const char *const RewardName;
170
171 using InlineFeatures = std::vector<int64_t>;
172
173 } // namespace llvm
174 #endif // LLVM_ANALYSIS_INLINEMODELFEATUREMAPS_H
175