LLVM 19.0.0git
PseudoProbe.cpp
Go to the documentation of this file.
1//===- PseudoProbe.cpp - Pseudo Probe Helpers -----------------------------===//
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 the helpers to manipulate pseudo probe IR intrinsic
10// calls.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/PseudoProbe.h"
16#include "llvm/IR/IRBuilder.h"
17#include "llvm/IR/Instruction.h"
19
20using namespace llvm;
21
22namespace llvm {
23
24std::optional<PseudoProbe>
26 if (DIL) {
27 auto Discriminator = DIL->getDiscriminator();
28 if (DILocation::isPseudoProbeDiscriminator(Discriminator)) {
29 PseudoProbe Probe;
30 Probe.Id =
32 Probe.Type =
34 Probe.Attr =
36 Probe.Factor =
39 Probe.Discriminator = 0;
40 return Probe;
41 }
42 }
43 return std::nullopt;
44}
45
46std::optional<PseudoProbe>
48 assert(isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst) &&
49 "Only call instructions should have pseudo probe encodes as their "
50 "Dwarf discriminators");
51 if (const DebugLoc &DLoc = Inst.getDebugLoc())
53 return std::nullopt;
54}
55
56std::optional<PseudoProbe> extractProbe(const Instruction &Inst) {
57 if (const auto *II = dyn_cast<PseudoProbeInst>(&Inst)) {
58 PseudoProbe Probe;
59 Probe.Id = II->getIndex()->getZExtValue();
61 Probe.Attr = II->getAttributes()->getZExtValue();
62 Probe.Factor = II->getFactor()->getZExtValue() /
64 Probe.Discriminator = 0;
65 if (const DebugLoc &DLoc = Inst.getDebugLoc())
66 Probe.Discriminator = DLoc->getDiscriminator();
67 return Probe;
68 }
69
70 if (isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst))
72
73 return std::nullopt;
74}
75
76void setProbeDistributionFactor(Instruction &Inst, float Factor) {
77 assert(Factor >= 0 && Factor <= 1 &&
78 "Distribution factor must be in [0, 1.0]");
79 if (auto *II = dyn_cast<PseudoProbeInst>(&Inst)) {
80 IRBuilder<> Builder(&Inst);
82 if (Factor < 1)
83 IntFactor *= Factor;
84 auto OrigFactor = II->getFactor()->getZExtValue();
85 if (IntFactor != OrigFactor)
86 II->replaceUsesOfWith(II->getFactor(), Builder.getInt64(IntFactor));
87 } else if (isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst)) {
88 if (const DebugLoc &DLoc = Inst.getDebugLoc()) {
89 const DILocation *DIL = DLoc;
90 auto Discriminator = DIL->getDiscriminator();
91 if (DILocation::isPseudoProbeDiscriminator(Discriminator)) {
92 auto Index =
94 auto Type =
97 Discriminator);
98 // Round small factors to 0 to avoid over-counting.
99 uint32_t IntFactor =
101 if (Factor < 1)
102 IntFactor *= Factor;
104 Index, Type, Attr, IntFactor);
105 DIL = DIL->cloneWithDiscriminator(V);
106 Inst.setDebugLoc(DIL);
107 }
108 }
109 }
110}
111
112} // namespace llvm
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Debug location.
static bool isPseudoProbeDiscriminator(unsigned Discriminator)
const DILocation * cloneWithDiscriminator(unsigned Discriminator) const
Returns a new DILocation with updated Discriminator.
A debug info location.
Definition: DebugLoc.h:33
ConstantInt * getInt64(uint64_t C)
Get a constant 64-bit value.
Definition: IRBuilder.h:485
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2644
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:454
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:451
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
static constexpr uint64_t PseudoProbeFullDistributionFactor
Definition: PseudoProbe.h:38
std::optional< PseudoProbe > extractProbeFromDiscriminator(const DILocation *DIL)
Definition: PseudoProbe.cpp:25
void setProbeDistributionFactor(Instruction &Inst, float Factor)
Definition: PseudoProbe.cpp:76
std::optional< PseudoProbe > extractProbe(const Instruction &Inst)
Definition: PseudoProbe.cpp:56
static uint32_t extractProbeFactor(uint32_t Value)
Definition: PseudoProbe.h:73
static constexpr uint8_t FullDistributionFactor
Definition: PseudoProbe.h:78
static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags, uint32_t Factor)
Definition: PseudoProbe.h:51
static uint32_t extractProbeIndex(uint32_t Value)
Definition: PseudoProbe.h:61
static uint32_t extractProbeAttributes(uint32_t Value)
Definition: PseudoProbe.h:69
static uint32_t extractProbeType(uint32_t Value)
Definition: PseudoProbe.h:65
uint32_t Discriminator
Definition: PseudoProbe.h:96