PseudoProbeInserter.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===- PseudoProbeInserter.cpp - Insert annotation for callsite profiling -===//
  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 PseudoProbeInserter pass, which inserts pseudo probe
  10. // annotations for call instructions with a pseudo-probe-specific dwarf
  11. // discriminator. such discriminator indicates that the call instruction comes
  12. // with a pseudo probe, and the discriminator value holds information to
  13. // identify the corresponding counter.
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/CodeGen/MachineBasicBlock.h"
  16. #include "llvm/CodeGen/MachineFunctionPass.h"
  17. #include "llvm/CodeGen/MachineInstr.h"
  18. #include "llvm/CodeGen/TargetInstrInfo.h"
  19. #include "llvm/IR/DebugInfoMetadata.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IR/PseudoProbe.h"
  22. #include "llvm/InitializePasses.h"
  23. #define DEBUG_TYPE "pseudo-probe-inserter"
  24. using namespace llvm;
  25. namespace {
  26. class PseudoProbeInserter : public MachineFunctionPass {
  27. public:
  28. static char ID;
  29. PseudoProbeInserter() : MachineFunctionPass(ID) {
  30. initializePseudoProbeInserterPass(*PassRegistry::getPassRegistry());
  31. }
  32. StringRef getPassName() const override { return "Pseudo Probe Inserter"; }
  33. void getAnalysisUsage(AnalysisUsage &AU) const override {
  34. AU.setPreservesAll();
  35. MachineFunctionPass::getAnalysisUsage(AU);
  36. }
  37. bool doInitialization(Module &M) override {
  38. ShouldRun = M.getNamedMetadata(PseudoProbeDescMetadataName);
  39. return false;
  40. }
  41. bool runOnMachineFunction(MachineFunction &MF) override {
  42. if (!ShouldRun)
  43. return false;
  44. const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
  45. bool Changed = false;
  46. for (MachineBasicBlock &MBB : MF) {
  47. MachineInstr *FirstInstr = nullptr;
  48. for (MachineInstr &MI : MBB) {
  49. if (!MI.isPseudo())
  50. FirstInstr = &MI;
  51. if (MI.isCall()) {
  52. if (DILocation *DL = MI.getDebugLoc()) {
  53. auto Value = DL->getDiscriminator();
  54. if (DILocation::isPseudoProbeDiscriminator(Value)) {
  55. BuildMI(MBB, MI, DL, TII->get(TargetOpcode::PSEUDO_PROBE))
  56. .addImm(getFuncGUID(MF.getFunction().getParent(), DL))
  57. .addImm(
  58. PseudoProbeDwarfDiscriminator::extractProbeIndex(Value))
  59. .addImm(
  60. PseudoProbeDwarfDiscriminator::extractProbeType(Value))
  61. .addImm(PseudoProbeDwarfDiscriminator::extractProbeAttributes(
  62. Value));
  63. Changed = true;
  64. }
  65. }
  66. }
  67. }
  68. // Walk the block backwards, move PSEUDO_PROBE before the first real
  69. // instruction to fix out-of-order probes. There is a problem with probes
  70. // as the terminator of the block. During the offline counts processing,
  71. // the samples collected on the first physical instruction following a
  72. // probe will be counted towards the probe. This logically equals to
  73. // treating the instruction next to a probe as if it is from the same
  74. // block of the probe. This is accurate most of the time unless the
  75. // instruction can be reached from multiple flows, which means it actually
  76. // starts a new block. Samples collected on such probes may cause
  77. // imprecision with the counts inference algorithm. Fortunately, if
  78. // there are still other native instructions preceding the probe we can
  79. // use them as a place holder to collect samples for the probe.
  80. if (FirstInstr) {
  81. auto MII = MBB.rbegin();
  82. while (MII != MBB.rend()) {
  83. // Skip all pseudo probes followed by a real instruction since they
  84. // are not dangling.
  85. if (!MII->isPseudo())
  86. break;
  87. auto Cur = MII++;
  88. if (Cur->getOpcode() != TargetOpcode::PSEUDO_PROBE)
  89. continue;
  90. // Move the dangling probe before FirstInstr.
  91. auto *ProbeInstr = &*Cur;
  92. MBB.remove(ProbeInstr);
  93. MBB.insert(FirstInstr, ProbeInstr);
  94. Changed = true;
  95. }
  96. } else {
  97. // Probes not surrounded by any real instructions in the same block are
  98. // called dangling probes. Since there's no good way to pick up a sample
  99. // collection point for dangling probes at compile time, they are being
  100. // removed so that the profile correlation tool will not report any
  101. // samples collected for them and it's up to the counts inference tool
  102. // to get them a reasonable count.
  103. SmallVector<MachineInstr *, 4> ToBeRemoved;
  104. for (MachineInstr &MI : MBB) {
  105. if (MI.isPseudoProbe())
  106. ToBeRemoved.push_back(&MI);
  107. }
  108. for (auto *MI : ToBeRemoved)
  109. MI->eraseFromParent();
  110. Changed |= !ToBeRemoved.empty();
  111. }
  112. }
  113. return Changed;
  114. }
  115. private:
  116. uint64_t getFuncGUID(Module *M, DILocation *DL) {
  117. auto *SP = DL->getScope()->getSubprogram();
  118. auto Name = SP->getLinkageName();
  119. if (Name.empty())
  120. Name = SP->getName();
  121. return Function::getGUID(Name);
  122. }
  123. bool ShouldRun = false;
  124. };
  125. } // namespace
  126. char PseudoProbeInserter::ID = 0;
  127. INITIALIZE_PASS_BEGIN(PseudoProbeInserter, DEBUG_TYPE,
  128. "Insert pseudo probe annotations for value profiling",
  129. false, false)
  130. INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
  131. INITIALIZE_PASS_END(PseudoProbeInserter, DEBUG_TYPE,
  132. "Insert pseudo probe annotations for value profiling",
  133. false, false)
  134. FunctionPass *llvm::createPseudoProbeInserter() {
  135. return new PseudoProbeInserter();
  136. }