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