PseudoProbeInserter.cpp 5.8 KB

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