Coverage Report

Created: 2024-01-17 10:31

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