InlineModelFeatureMaps.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- InlineModelFeatureMaps.h - common model runner defs ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. #ifndef LLVM_ANALYSIS_INLINEMODELFEATUREMAPS_H
  15. #define LLVM_ANALYSIS_INLINEMODELFEATUREMAPS_H
  16. #include "llvm/Analysis/TensorSpec.h"
  17. #include <array>
  18. #include <string>
  19. #include <vector>
  20. namespace llvm {
  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(SROASavings, "sroa_savings") \
  26. M(SROALosses, "sroa_losses") \
  27. M(LoadElimination, "load_elimination") \
  28. M(CallPenalty, "call_penalty") \
  29. M(CallArgumentSetup, "call_argument_setup") \
  30. M(LoadRelativeIntrinsic, "load_relative_intrinsic") \
  31. M(LoweredCallArgSetup, "lowered_call_arg_setup") \
  32. M(IndirectCallPenalty, "indirect_call_penalty") \
  33. M(JumpTablePenalty, "jump_table_penalty") \
  34. M(CaseClusterPenalty, "case_cluster_penalty") \
  35. M(SwitchPenalty, "switch_penalty") \
  36. M(UnsimplifiedCommonInstructions, "unsimplified_common_instructions") \
  37. M(NumLoops, "num_loops") \
  38. M(DeadBlocks, "dead_blocks") \
  39. M(SimplifiedInstructions, "simplified_instructions") \
  40. M(ConstantArgs, "constant_args") \
  41. M(ConstantOffsetPtrArgs, "constant_offset_ptr_args") \
  42. M(CallSiteCost, "callsite_cost") \
  43. M(ColdCcPenalty, "cold_cc_penalty") \
  44. M(LastCallToStaticBonus, "last_call_to_static_bonus") \
  45. M(IsMultipleBlocks, "is_multiple_blocks") \
  46. M(NestedInlines, "nested_inlines") \
  47. M(NestedInlineCostEstimate, "nested_inline_cost_estimate") \
  48. M(Threshold, "threshold")
  49. // clang-format off
  50. enum class InlineCostFeatureIndex : size_t {
  51. #define POPULATE_INDICES(INDEX_NAME, NAME) INDEX_NAME,
  52. INLINE_COST_FEATURE_ITERATOR(POPULATE_INDICES)
  53. #undef POPULATE_INDICES
  54. NumberOfFeatures
  55. };
  56. // clang-format on
  57. using InlineCostFeatures =
  58. std::array<int,
  59. static_cast<size_t>(InlineCostFeatureIndex::NumberOfFeatures)>;
  60. constexpr bool isHeuristicInlineCostFeature(InlineCostFeatureIndex Feature) {
  61. return Feature != InlineCostFeatureIndex::SROASavings &&
  62. Feature != InlineCostFeatureIndex::IsMultipleBlocks &&
  63. Feature != InlineCostFeatureIndex::DeadBlocks &&
  64. Feature != InlineCostFeatureIndex::SimplifiedInstructions &&
  65. Feature != InlineCostFeatureIndex::ConstantArgs &&
  66. Feature != InlineCostFeatureIndex::ConstantOffsetPtrArgs &&
  67. Feature != InlineCostFeatureIndex::NestedInlines &&
  68. Feature != InlineCostFeatureIndex::NestedInlineCostEstimate &&
  69. Feature != InlineCostFeatureIndex::Threshold;
  70. }
  71. // List of features. Each feature is defined through a triple:
  72. // - the name of an enum member, which will be the feature index
  73. // - a textual name, used for Tensorflow model binding (so it needs to match the
  74. // names used by the Tensorflow model)
  75. // - a documentation description. Currently, that is not used anywhere
  76. // programmatically, and serves as workaround to inability of inserting comments
  77. // in macros.
  78. #define INLINE_FEATURE_ITERATOR(M) \
  79. M(CalleeBasicBlockCount, "callee_basic_block_count", \
  80. "number of basic blocks of the callee") \
  81. M(CallSiteHeight, "callsite_height", \
  82. "position of the call site in the original call graph - measured from " \
  83. "the farthest SCC") \
  84. M(NodeCount, "node_count", \
  85. "total current number of defined functions in the module") \
  86. M(NrCtantParams, "nr_ctant_params", \
  87. "number of parameters in the call site that are constants") \
  88. M(CostEstimate, "cost_estimate", "total cost estimate (threshold - free)") \
  89. M(EdgeCount, "edge_count", "total number of calls in the module") \
  90. M(CallerUsers, "caller_users", \
  91. "number of module-internal users of the caller, +1 if the caller is " \
  92. "exposed externally") \
  93. M(CallerConditionallyExecutedBlocks, "caller_conditionally_executed_blocks", \
  94. "number of blocks reached from a conditional instruction, in the caller") \
  95. M(CallerBasicBlockCount, "caller_basic_block_count", \
  96. "number of basic blocks in the caller") \
  97. M(CalleeConditionallyExecutedBlocks, "callee_conditionally_executed_blocks", \
  98. "number of blocks reached from a conditional instruction, in the callee") \
  99. M(CalleeUsers, "callee_users", \
  100. "number of module-internal users of the callee, +1 if the callee is " \
  101. "exposed externally")
  102. // clang-format off
  103. enum class FeatureIndex : size_t {
  104. // InlineCost features - these must come first
  105. #define POPULATE_INDICES(INDEX_NAME, NAME) INDEX_NAME,
  106. INLINE_COST_FEATURE_ITERATOR(POPULATE_INDICES)
  107. #undef POPULATE_INDICES
  108. // Non-cost features
  109. #define POPULATE_INDICES(INDEX_NAME, NAME, COMMENT) INDEX_NAME,
  110. INLINE_FEATURE_ITERATOR(POPULATE_INDICES)
  111. #undef POPULATE_INDICES
  112. NumberOfFeatures
  113. };
  114. // clang-format on
  115. constexpr FeatureIndex
  116. inlineCostFeatureToMlFeature(InlineCostFeatureIndex Feature) {
  117. return static_cast<FeatureIndex>(static_cast<size_t>(Feature));
  118. }
  119. constexpr size_t NumberOfFeatures =
  120. static_cast<size_t>(FeatureIndex::NumberOfFeatures);
  121. extern const std::array<TensorSpec, NumberOfFeatures> FeatureMap;
  122. extern const char *const DecisionName;
  123. extern const char *const DefaultDecisionName;
  124. extern const char *const RewardName;
  125. using InlineFeatures = std::vector<int64_t>;
  126. } // namespace llvm
  127. #endif // LLVM_ANALYSIS_INLINEMODELFEATUREMAPS_H
  128. #ifdef __GNUC__
  129. #pragma GCC diagnostic pop
  130. #endif