LLVM 20.0.0git
PseudoProbe.h
Go to the documentation of this file.
1//===- PseudoProbe.h - Pseudo Probe IR Helpers ------------------*- C++ -*-===//
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// Pseudo probe IR intrinsic and dwarf discriminator manipulation routines.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_IR_PSEUDOPROBE_H
14#define LLVM_IR_PSEUDOPROBE_H
15
16#include <cassert>
17#include <cstdint>
18#include <limits>
19#include <optional>
20
21namespace llvm {
22
23class Instruction;
24
25constexpr const char *PseudoProbeDescMetadataName = "llvm.pseudo_probe_desc";
26
28
30
32 Reserved = 0x1,
33 Sentinel = 0x2, // A place holder for split function entry address.
34 HasDiscriminator = 0x4, // for probes with a discriminator
35};
36
37// The saturated distrution factor representing 100% for block probes.
39 std::numeric_limits<uint64_t>::max();
40
42public:
43 // The following APIs encodes/decodes per-probe information to/from a
44 // 32-bit integer which is organized as:
45 // [2:0] - 0x7, this is reserved for regular discriminator,
46 // see DWARF discriminator encoding rule
47 // if the [28:28] bit is zero:
48 // [18:3] for probe id.
49 // else:
50 // [15:3] for probe id, [18:16] for dwarf base discriminator.
51 // [25:19] - probe distribution factor
52 // [27:26] - probe type, see PseudoProbeType
53 // [28:28] - indicates whether dwarf base discriminator is encoded.
54 // [30:29] - reserved for probe attributes
55 static uint32_t
57 std::optional<uint32_t> DwarfBaseDiscriminator) {
58 assert(Index <= 0xFFFF && "Probe index too big to encode, exceeding 2^16");
59 assert(Type <= 0x3 && "Probe type too big to encode, exceeding 3");
60 assert(Flags <= 0x7);
61 assert(Factor <= 100 &&
62 "Probe distribution factor too big to encode, exceeding 100");
63 uint32_t V = (Index << 3) | (Factor << 19) | (Type << 26) | 0x7;
64 // If both the probe id and dwarf base discriminator is small, the probe id
65 // space is shared with the dwarf base discriminator, this is to make the
66 // probe-based build compatible with the dwarf-based profile.
67 // Pack the dwarf base discriminator into [18:16] and set the [28:28] bit.
68 if (Index <= 0x1FFF && DwarfBaseDiscriminator &&
69 *DwarfBaseDiscriminator <= 0x7)
70 V |= (1 << 28) | (*DwarfBaseDiscriminator << 16);
71 return V;
72 }
73
76 return (Value >> 3) & 0x1FFF;
77 return (Value >> 3) & 0xFFFF;
78 }
79
80 static std::optional<uint32_t> extractDwarfBaseDiscriminator(uint32_t Value) {
82 return (Value >> 16) & 0x7;
83 return std::nullopt;
84 }
85
87 return Value & 0x10000000;
88 }
89
91 return (Value >> 26) & 0x3;
92 }
93
95 return (Value >> 29) & 0x7;
96 }
97
99 return (Value >> 19) & 0x7F;
100 }
101
102 // The saturated distrution factor representing 100% for callsites.
103 constexpr static uint8_t FullDistributionFactor = 100;
104};
105
107 uint64_t FunctionGUID;
108 uint64_t FunctionHash;
109
110public:
112 : FunctionGUID(GUID), FunctionHash(Hash) {}
113 uint64_t getFunctionGUID() const { return FunctionGUID; }
114 uint64_t getFunctionHash() const { return FunctionHash; }
115};
116
122 // Distribution factor that estimates the portion of the real execution count.
123 // A saturated distribution factor stands for 1.0 or 100%. A pesudo probe has
124 // a factor with the value ranged from 0.0 to 1.0.
125 float Factor;
126};
127
128static inline bool isSentinelProbe(uint32_t Flags) {
130}
131
132static inline bool hasDiscriminator(uint32_t Flags) {
134}
135
136std::optional<PseudoProbe> extractProbe(const Instruction &Inst);
137
138void setProbeDistributionFactor(Instruction &Inst, float Factor);
139} // end namespace llvm
140
141#endif // LLVM_IR_PSEUDOPROBE_H
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
PseudoProbeDescriptor(uint64_t GUID, uint64_t Hash)
Definition: PseudoProbe.h:111
uint64_t getFunctionGUID() const
Definition: PseudoProbe.h:113
uint64_t getFunctionHash() const
Definition: PseudoProbe.h:114
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
PseudoProbeType
Definition: PseudoProbe.h:29
static bool isSentinelProbe(uint32_t Flags)
Definition: PseudoProbe.h:128
static constexpr uint64_t PseudoProbeFullDistributionFactor
Definition: PseudoProbe.h:38
void setProbeDistributionFactor(Instruction &Inst, float Factor)
Definition: PseudoProbe.cpp:76
std::optional< PseudoProbe > extractProbe(const Instruction &Inst)
Definition: PseudoProbe.cpp:56
static bool hasDiscriminator(uint32_t Flags)
Definition: PseudoProbe.h:132
PseudoProbeReservedId
Definition: PseudoProbe.h:27
PseudoProbeAttributes
Definition: PseudoProbe.h:31
constexpr const char * PseudoProbeDescMetadataName
Definition: PseudoProbe.h:25
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 bool isDwarfBaseDiscriminatorEncoded(uint32_t Value)
Definition: PseudoProbe.h:86
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