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 <array>
14 #include <string>
15 #include <vector>
16 
17 namespace llvm {
18 
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 
isHeuristicInlineCostFeature(InlineCostFeatureIndex Feature)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 
74 // List of features. Each feature is defined through a triple:
75 // - the name of an enum member, which will be the feature index
76 // - a textual name, used for Tensorflow model binding (so it needs to match the
77 // names used by the Tensorflow model)
78 // - a documentation description. Currently, that is not used anywhere
79 // programmatically, and serves as workaround to inability of inserting comments
80 // in macros.
81 #define INLINE_FEATURE_ITERATOR(M)                                             \
82   M(CalleeBasicBlockCount, "callee_basic_block_count",                         \
83     "number of basic blocks of the callee")                                    \
84   M(CallSiteHeight, "callsite_height",                                         \
85     "position of the call site in the original call graph - measured from "    \
86     "the farthest SCC")                                                        \
87   M(NodeCount, "node_count",                                                   \
88     "total current number of defined functions in the module")                 \
89   M(NrCtantParams, "nr_ctant_params",                                          \
90     "number of parameters in the call site that are constants")                \
91   M(CostEstimate, "cost_estimate", "total cost estimate (threshold - free)")   \
92   M(EdgeCount, "edge_count", "total number of calls in the module")            \
93   M(CallerUsers, "caller_users",                                               \
94     "number of module-internal users of the caller, +1 if the caller is "      \
95     "exposed externally")                                                      \
96   M(CallerConditionallyExecutedBlocks, "caller_conditionally_executed_blocks", \
97     "number of blocks reached from a conditional instruction, in the caller")  \
98   M(CallerBasicBlockCount, "caller_basic_block_count",                         \
99     "number of basic blocks in the caller")                                    \
100   M(CalleeConditionallyExecutedBlocks, "callee_conditionally_executed_blocks", \
101     "number of blocks reached from a conditional instruction, in the callee")  \
102   M(CalleeUsers, "callee_users",                                               \
103     "number of module-internal users of the callee, +1 if the callee is "      \
104     "exposed externally")
105 
106 // clang-format off
107 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
114 #define POPULATE_INDICES(INDEX_NAME, NAME, COMMENT) INDEX_NAME,
115   INLINE_FEATURE_ITERATOR(POPULATE_INDICES)
116 #undef POPULATE_INDICES
117 
118   NumberOfFeatures
119 };
120 // clang-format on
121 
122 constexpr FeatureIndex
inlineCostFeatureToMlFeature(InlineCostFeatureIndex Feature)123 inlineCostFeatureToMlFeature(InlineCostFeatureIndex Feature) {
124   return static_cast<FeatureIndex>(static_cast<size_t>(Feature));
125 }
126 
127 constexpr size_t NumberOfFeatures =
128     static_cast<size_t>(FeatureIndex::NumberOfFeatures);
129 
130 extern const std::array<std::string, NumberOfFeatures> FeatureNameMap;
131 
132 extern const char *const DecisionName;
133 extern const char *const DefaultDecisionName;
134 extern const char *const RewardName;
135 
136 using InlineFeatures = std::vector<int64_t>;
137 
138 } // namespace llvm
139 #endif // LLVM_ANALYSIS_INLINEMODELFEATUREMAPS_H
140