LLVM 20.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 auto DwarfBaseDiscriminator =
100 Discriminator);
101 // Round small factors to 0 to avoid over-counting.
102 uint32_t IntFactor =
104 if (Factor < 1)
105 IntFactor *= Factor;
107 Index, Type, Attr, IntFactor, DwarfBaseDiscriminator);
108 DIL = DIL->cloneWithDiscriminator(V);
109 Inst.setDebugLoc(DIL);
110 }
111 }
112 }
113}
114
115} // namespace llvm
uint64_t IntrinsicInst * II
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:488
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2686
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:466
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:463
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:98
static constexpr uint8_t FullDistributionFactor
Definition: PseudoProbe.h:103
static uint32_t extractProbeIndex(uint32_t Value)
Definition: PseudoProbe.h:74
static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags, uint32_t Factor, std::optional< uint32_t > DwarfBaseDiscriminator)
Definition: PseudoProbe.h:56
static uint32_t extractProbeAttributes(uint32_t Value)
Definition: PseudoProbe.h:94
static std::optional< uint32_t > extractDwarfBaseDiscriminator(uint32_t Value)
Definition: PseudoProbe.h:80
static uint32_t extractProbeType(uint32_t Value)
Definition: PseudoProbe.h:90
uint32_t Discriminator
Definition: PseudoProbe.h:121