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
15#include <array>
16#include <vector>
17
18namespace llvm {
19
20// List of cost features. A "cost" feature is a summand of the heuristic-based
21// inline cost, and we define them separately to preserve the original heuristic
22// behavior.
23#define INLINE_COST_FEATURE_ITERATOR(M)                                        \
24  M(int64_t, {1}, sroa_savings,                                                \
25    "Savings from SROA (scalar replacement of aggregates)")                    \
26  M(int64_t, {1}, sroa_losses,                                                 \
27    "Losses from SROA (scalar replacement of aggregates)")                     \
28  M(int64_t, {1}, load_elimination, "Cost of load elimination in the call")    \
29  M(int64_t, {1}, call_penalty,                                                \
30    "Accumulation of penalty applied to call sites when inlining")             \
31  M(int64_t, {1}, call_argument_setup,                                         \
32    "Accumulation of call argument setup costs")                               \
33  M(int64_t, {1}, load_relative_intrinsic,                                     \
34    "Accumulation of costs of loading relative intrinsics")                    \
35  M(int64_t, {1}, lowered_call_arg_setup,                                      \
36    "Accumulation of cost of lowered call argument setups")                    \
37  M(int64_t, {1}, indirect_call_penalty,                                       \
38    "Accumulation of costs for indirect calls")                                \
39  M(int64_t, {1}, jump_table_penalty, "Accumulation of costs for jump tables") \
40  M(int64_t, {1}, case_cluster_penalty,                                        \
41    "Accumulation of costs for case clusters")                                 \
42  M(int64_t, {1}, switch_penalty,                                              \
43    "Accumulation of costs for switch statements")                             \
44  M(int64_t, {1}, unsimplified_common_instructions,                            \
45    "Costs from unsimplified common instructions")                             \
46  M(int64_t, {1}, num_loops, "Number of loops in the caller")                  \
47  M(int64_t, {1}, dead_blocks, "Number of dead blocks in the caller")          \
48  M(int64_t, {1}, simplified_instructions,                                     \
49    "Number of simplified instructions")                                       \
50  M(int64_t, {1}, constant_args,                                               \
51    "Number of constant arguments in the call site")                           \
52  M(int64_t, {1}, constant_offset_ptr_args,                                    \
53    "Number of constant offset pointer args in the call site")                 \
54  M(int64_t, {1}, callsite_cost, "Estimated cost of the call site")            \
55  M(int64_t, {1}, cold_cc_penalty, "Penalty for a cold calling convention")    \
56  M(int64_t, {1}, last_call_to_static_bonus,                                   \
57    "Bonus for being the last call to static")                                 \
58  M(int64_t, {1}, is_multiple_blocks,                                          \
59    "Boolean; is the Callee multiple blocks")                                  \
60  M(int64_t, {1}, nested_inlines,                                              \
61    "Would the default inliner perfom nested inlining")                        \
62  M(int64_t, {1}, nested_inline_cost_estimate,                                 \
63    "Estimate of the accumulated cost of nested inlines")                      \
64  M(int64_t, {1}, threshold, "Threshold for the heuristic inliner")
65
66// clang-format off
67enum class InlineCostFeatureIndex : size_t {
68#define POPULATE_INDICES(DTYPE, SHAPE, NAME, DOC) NAME,
69  INLINE_COST_FEATURE_ITERATOR(POPULATE_INDICES)
70#undef POPULATE_INDICES
71
72  NumberOfFeatures
73};
74// clang-format on
75
76using InlineCostFeatures =
77    std::array<int,
78               static_cast<size_t>(InlineCostFeatureIndex::NumberOfFeatures)>;
79
80constexpr bool isHeuristicInlineCostFeature(InlineCostFeatureIndex Feature) {
81  return Feature != InlineCostFeatureIndex::sroa_savings &&
82         Feature != InlineCostFeatureIndex::is_multiple_blocks &&
83         Feature != InlineCostFeatureIndex::dead_blocks &&
84         Feature != InlineCostFeatureIndex::simplified_instructions &&
85         Feature != InlineCostFeatureIndex::constant_args &&
86         Feature != InlineCostFeatureIndex::constant_offset_ptr_args &&
87         Feature != InlineCostFeatureIndex::nested_inlines &&
88         Feature != InlineCostFeatureIndex::nested_inline_cost_estimate &&
89         Feature != InlineCostFeatureIndex::threshold;
90}
91
92// List of features. Each feature is defined through a triple:
93// - the name of an enum member, which will be the feature index
94// - a textual name, used for ML model binding (so it needs to match the
95// names used by the ML model).
96// - a documentation description. Currently, that is not used anywhere
97// programmatically, and serves as workaround to inability of inserting comments
98// in macros.
99#define INLINE_FEATURE_ITERATOR(M)                                             \
100  M(int64_t, {1}, callee_basic_block_count,                                    \
101    "number of basic blocks of the callee")                                    \
102  M(int64_t, {1}, callsite_height,                                             \
103    "position of the call site in the original call graph - measured from "    \
104    "the farthest SCC")                                                        \
105  M(int64_t, {1}, node_count,                                                  \
106    "total current number of defined functions in the module")                 \
107  M(int64_t, {1}, nr_ctant_params,                                             \
108    "number of parameters in the call site that are constants")                \
109  M(int64_t, {1}, cost_estimate, "total cost estimate (threshold - free)")     \
110  M(int64_t, {1}, edge_count, "total number of calls in the module")           \
111  M(int64_t, {1}, caller_users,                                                \
112    "number of module-internal users of the caller, +1 if the caller is "      \
113    "exposed externally")                                                      \
114  M(int64_t, {1}, caller_conditionally_executed_blocks,                        \
115    "number of blocks reached from a conditional instruction, in the caller")  \
116  M(int64_t, {1}, caller_basic_block_count,                                    \
117    "number of basic blocks in the caller")                                    \
118  M(int64_t, {1}, callee_conditionally_executed_blocks,                        \
119    "number of blocks reached from a conditional instruction, in the callee")  \
120  M(int64_t, {1}, callee_users,                                                \
121    "number of module-internal users of the callee, +1 if the callee is "      \
122    "exposed externally")
123
124// clang-format off
125enum class FeatureIndex : size_t {
126#define POPULATE_INDICES(DTYPE, SHAPE, NAME, COMMENT) NAME,
127// InlineCost features - these must come first
128  INLINE_COST_FEATURE_ITERATOR(POPULATE_INDICES)
129
130// Non-cost features
131  INLINE_FEATURE_ITERATOR(POPULATE_INDICES)
132#undef POPULATE_INDICES
133
134  NumberOfFeatures
135};
136// clang-format on
137
138constexpr FeatureIndex
139inlineCostFeatureToMlFeature(InlineCostFeatureIndex Feature) {
140  return static_cast<FeatureIndex>(static_cast<size_t>(Feature));
141}
142
143constexpr size_t NumberOfFeatures =
144    static_cast<size_t>(FeatureIndex::NumberOfFeatures);
145
146extern const std::vector<TensorSpec> FeatureMap;
147
148extern const char *const DecisionName;
149extern const TensorSpec InlineDecisionSpec;
150extern const char *const DefaultDecisionName;
151extern const TensorSpec DefaultDecisionSpec;
152extern const char *const RewardName;
153
154using InlineFeatures = std::vector<int64_t>;
155
156} // namespace llvm
157#endif // LLVM_ANALYSIS_INLINEMODELFEATUREMAPS_H
158