ProfDataUtils.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //===- ProfDataUtils.cpp - Utility functions for MD_prof Metadata ---------===//
  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. // This file implements utilities for working with Profiling Metadata.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/IR/ProfDataUtils.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/Twine.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/Instructions.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/IR/Metadata.h"
  20. #include "llvm/Support/BranchProbability.h"
  21. #include "llvm/Support/CommandLine.h"
  22. using namespace llvm;
  23. namespace {
  24. // MD_prof nodes have the following layout
  25. //
  26. // In general:
  27. // { String name, Array of i32 }
  28. //
  29. // In terms of Types:
  30. // { MDString, [i32, i32, ...]}
  31. //
  32. // Concretely for Branch Weights
  33. // { "branch_weights", [i32 1, i32 10000]}
  34. //
  35. // We maintain some constants here to ensure that we access the branch weights
  36. // correctly, and can change the behavior in the future if the layout changes
  37. // The index at which the weights vector starts
  38. constexpr unsigned WeightsIdx = 1;
  39. // the minimum number of operands for MD_prof nodes with branch weights
  40. constexpr unsigned MinBWOps = 3;
  41. bool extractWeights(const MDNode *ProfileData,
  42. SmallVectorImpl<uint32_t> &Weights) {
  43. // Assume preconditions are already met (i.e. this is valid metadata)
  44. assert(ProfileData && "ProfileData was nullptr in extractWeights");
  45. unsigned NOps = ProfileData->getNumOperands();
  46. assert(WeightsIdx < NOps && "Weights Index must be less than NOps.");
  47. Weights.resize(NOps - WeightsIdx);
  48. for (unsigned Idx = WeightsIdx, E = NOps; Idx != E; ++Idx) {
  49. ConstantInt *Weight =
  50. mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(Idx));
  51. assert(Weight && "Malformed branch_weight in MD_prof node");
  52. assert(Weight->getValue().getActiveBits() <= 32 &&
  53. "Too many bits for uint32_t");
  54. Weights[Idx - WeightsIdx] = Weight->getZExtValue();
  55. }
  56. return true;
  57. }
  58. // We may want to add support for other MD_prof types, so provide an abstraction
  59. // for checking the metadata type.
  60. bool isTargetMD(const MDNode *ProfData, const char *Name, unsigned MinOps) {
  61. // TODO: This routine may be simplified if MD_prof used an enum instead of a
  62. // string to differentiate the types of MD_prof nodes.
  63. if (!ProfData || !Name || MinOps < 2)
  64. return false;
  65. unsigned NOps = ProfData->getNumOperands();
  66. if (NOps < MinOps)
  67. return false;
  68. auto *ProfDataName = dyn_cast<MDString>(ProfData->getOperand(0));
  69. if (!ProfDataName)
  70. return false;
  71. return ProfDataName->getString().equals(Name);
  72. }
  73. } // namespace
  74. namespace llvm {
  75. bool hasProfMD(const Instruction &I) {
  76. return nullptr != I.getMetadata(LLVMContext::MD_prof);
  77. }
  78. bool isBranchWeightMD(const MDNode *ProfileData) {
  79. return isTargetMD(ProfileData, "branch_weights", MinBWOps);
  80. }
  81. bool hasBranchWeightMD(const Instruction &I) {
  82. auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
  83. return isBranchWeightMD(ProfileData);
  84. }
  85. bool hasValidBranchWeightMD(const Instruction &I) {
  86. return getValidBranchWeightMDNode(I);
  87. }
  88. MDNode *getBranchWeightMDNode(const Instruction &I) {
  89. auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
  90. if (!isBranchWeightMD(ProfileData))
  91. return nullptr;
  92. return ProfileData;
  93. }
  94. MDNode *getValidBranchWeightMDNode(const Instruction &I) {
  95. auto *ProfileData = getBranchWeightMDNode(I);
  96. if (ProfileData && ProfileData->getNumOperands() == 1 + I.getNumSuccessors())
  97. return ProfileData;
  98. return nullptr;
  99. }
  100. bool extractBranchWeights(const MDNode *ProfileData,
  101. SmallVectorImpl<uint32_t> &Weights) {
  102. if (!isBranchWeightMD(ProfileData))
  103. return false;
  104. return extractWeights(ProfileData, Weights);
  105. }
  106. bool extractBranchWeights(const Instruction &I,
  107. SmallVectorImpl<uint32_t> &Weights) {
  108. auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
  109. return extractBranchWeights(ProfileData, Weights);
  110. }
  111. bool extractBranchWeights(const Instruction &I, uint64_t &TrueVal,
  112. uint64_t &FalseVal) {
  113. assert((I.getOpcode() == Instruction::Br ||
  114. I.getOpcode() == Instruction::Select) &&
  115. "Looking for branch weights on something besides branch, select, or "
  116. "switch");
  117. SmallVector<uint32_t, 2> Weights;
  118. auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
  119. if (!extractBranchWeights(ProfileData, Weights))
  120. return false;
  121. if (Weights.size() > 2)
  122. return false;
  123. TrueVal = Weights[0];
  124. FalseVal = Weights[1];
  125. return true;
  126. }
  127. bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
  128. TotalVal = 0;
  129. if (!ProfileData)
  130. return false;
  131. auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
  132. if (!ProfDataName)
  133. return false;
  134. if (ProfDataName->getString().equals("branch_weights")) {
  135. for (unsigned Idx = 1; Idx < ProfileData->getNumOperands(); Idx++) {
  136. auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(Idx));
  137. assert(V && "Malformed branch_weight in MD_prof node");
  138. TotalVal += V->getValue().getZExtValue();
  139. }
  140. return true;
  141. }
  142. if (ProfDataName->getString().equals("VP") &&
  143. ProfileData->getNumOperands() > 3) {
  144. TotalVal = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2))
  145. ->getValue()
  146. .getZExtValue();
  147. return true;
  148. }
  149. return false;
  150. }
  151. bool extractProfTotalWeight(const Instruction &I, uint64_t &TotalVal) {
  152. return extractProfTotalWeight(I.getMetadata(LLVMContext::MD_prof), TotalVal);
  153. }
  154. } // namespace llvm