LLVM 22.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
22static std::optional<PseudoProbe>
24 if (DIL) {
25 auto Discriminator = DIL->getDiscriminator();
26 if (DILocation::isPseudoProbeDiscriminator(Discriminator)) {
27 PseudoProbe Probe;
28 Probe.Id =
30 Probe.Type =
32 Probe.Attr =
34 Probe.Factor =
37 Probe.Discriminator = 0;
38 return Probe;
39 }
40 }
41 return std::nullopt;
42}
43
44static std::optional<PseudoProbe>
46 assert(isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst) &&
47 "Only call instructions should have pseudo probe encodes as their "
48 "Dwarf discriminators");
49 if (const DebugLoc &DLoc = Inst.getDebugLoc())
51 return std::nullopt;
52}
53
54std::optional<PseudoProbe> llvm::extractProbe(const Instruction &Inst) {
55 if (const auto *II = dyn_cast<PseudoProbeInst>(&Inst)) {
56 PseudoProbe Probe;
57 Probe.Id = II->getIndex()->getZExtValue();
59 Probe.Attr = II->getAttributes()->getZExtValue();
60 Probe.Factor = II->getFactor()->getZExtValue() /
62 Probe.Discriminator = 0;
63 if (const DebugLoc &DLoc = Inst.getDebugLoc())
64 Probe.Discriminator = DLoc->getDiscriminator();
65 return Probe;
66 }
67
68 if (isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst))
70
71 return std::nullopt;
72}
73
75 assert(Factor >= 0 && Factor <= 1 &&
76 "Distribution factor must be in [0, 1.0]");
77 if (auto *II = dyn_cast<PseudoProbeInst>(&Inst)) {
78 IRBuilder<> Builder(&Inst);
80 if (Factor < 1)
81 IntFactor *= Factor;
82 auto OrigFactor = II->getFactor()->getZExtValue();
83 if (IntFactor != OrigFactor)
84 II->replaceUsesOfWith(II->getFactor(), Builder.getInt64(IntFactor));
85 } else if (isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst)) {
86 if (const DebugLoc &DLoc = Inst.getDebugLoc()) {
87 const DILocation *DIL = DLoc;
88 auto Discriminator = DIL->getDiscriminator();
89 if (DILocation::isPseudoProbeDiscriminator(Discriminator)) {
90 auto Index =
92 auto Type =
95 Discriminator);
96 auto DwarfBaseDiscriminator =
98 Discriminator);
99 // Round small factors to 0 to avoid over-counting.
100 uint32_t IntFactor =
102 if (Factor < 1)
103 IntFactor *= Factor;
105 Index, Type, Attr, IntFactor, DwarfBaseDiscriminator);
106 DIL = DIL->cloneWithDiscriminator(V);
107 Inst.setDebugLoc(DIL);
108 }
109 }
110 }
111}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t IntrinsicInst * II
static std::optional< PseudoProbe > extractProbeFromDiscriminator(const DILocation *DIL)
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:124
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
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.
static constexpr uint64_t PseudoProbeFullDistributionFactor
Definition PseudoProbe.h:39
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void setProbeDistributionFactor(Instruction &Inst, float Factor)
LLVM_ABI std::optional< PseudoProbe > extractProbe(const Instruction &Inst)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
static uint32_t extractProbeFactor(uint32_t Value)
Definition PseudoProbe.h:99
static constexpr uint8_t FullDistributionFactor
static uint32_t extractProbeIndex(uint32_t Value)
Definition PseudoProbe.h:75
static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags, uint32_t Factor, std::optional< uint32_t > DwarfBaseDiscriminator)
Definition PseudoProbe.h:57
static uint32_t extractProbeAttributes(uint32_t Value)
Definition PseudoProbe.h:95
static std::optional< uint32_t > extractDwarfBaseDiscriminator(uint32_t Value)
Definition PseudoProbe.h:81
static uint32_t extractProbeType(uint32_t Value)
Definition PseudoProbe.h:91
uint32_t Discriminator