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
21 namespace llvm {
22
23 class Instruction;
24
25 constexpr const char *PseudoProbeDescMetadataName = "llvm.pseudo_probe_desc";
26
27 enum class PseudoProbeReservedId { Invalid = 0, Last = Invalid };
28
29 enum class PseudoProbeType { Block = 0, IndirectCall, DirectCall };
30
31 enum class PseudoProbeAttributes {
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.
38 constexpr static uint64_t PseudoProbeFullDistributionFactor =
39 std::numeric_limits<uint64_t>::max();
40
41 struct PseudoProbeDwarfDiscriminator {
42 public:
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 // [18:3] - probe id
48 // [25:19] - probe distribution factor
49 // [28:26] - probe type, see PseudoProbeType
50 // [31:29] - reserved for probe attributes
packProbeDataPseudoProbeDwarfDiscriminator51 static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags,
52 uint32_t Factor) {
53 assert(Index <= 0xFFFF && "Probe index too big to encode, exceeding 2^16");
54 assert(Type <= 0x7 && "Probe type too big to encode, exceeding 7");
55 assert(Flags <= 0x7);
56 assert(Factor <= 100 &&
57 "Probe distribution factor too big to encode, exceeding 100");
58 return (Index << 3) | (Factor << 19) | (Type << 26) | 0x7;
59 }
60
extractProbeIndexPseudoProbeDwarfDiscriminator61 static uint32_t extractProbeIndex(uint32_t Value) {
62 return (Value >> 3) & 0xFFFF;
63 }
64
extractProbeTypePseudoProbeDwarfDiscriminator65 static uint32_t extractProbeType(uint32_t Value) {
66 return (Value >> 26) & 0x7;
67 }
68
extractProbeAttributesPseudoProbeDwarfDiscriminator69 static uint32_t extractProbeAttributes(uint32_t Value) {
70 return (Value >> 29) & 0x7;
71 }
72
extractProbeFactorPseudoProbeDwarfDiscriminator73 static uint32_t extractProbeFactor(uint32_t Value) {
74 return (Value >> 19) & 0x7F;
75 }
76
77 // The saturated distrution factor representing 100% for callsites.
78 constexpr static uint8_t FullDistributionFactor = 100;
79 };
80
81 class PseudoProbeDescriptor {
82 uint64_t FunctionGUID;
83 uint64_t FunctionHash;
84
85 public:
PseudoProbeDescriptor(uint64_t GUID,uint64_t Hash)86 PseudoProbeDescriptor(uint64_t GUID, uint64_t Hash)
87 : FunctionGUID(GUID), FunctionHash(Hash) {}
getFunctionGUID()88 uint64_t getFunctionGUID() const { return FunctionGUID; }
getFunctionHash()89 uint64_t getFunctionHash() const { return FunctionHash; }
90 };
91
92 struct PseudoProbe {
93 uint32_t Id;
94 uint32_t Type;
95 uint32_t Attr;
96 uint32_t Discriminator;
97 // Distribution factor that estimates the portion of the real execution count.
98 // A saturated distribution factor stands for 1.0 or 100%. A pesudo probe has
99 // a factor with the value ranged from 0.0 to 1.0.
100 float Factor;
101 };
102
isSentinelProbe(uint32_t Flags)103 static inline bool isSentinelProbe(uint32_t Flags) {
104 return Flags & (uint32_t)PseudoProbeAttributes::Sentinel;
105 }
106
hasDiscriminator(uint32_t Flags)107 static inline bool hasDiscriminator(uint32_t Flags) {
108 return Flags & (uint32_t)PseudoProbeAttributes::HasDiscriminator;
109 }
110
111 std::optional<PseudoProbe> extractProbe(const Instruction &Inst);
112
113 void setProbeDistributionFactor(Instruction &Inst, float Factor);
114 } // end namespace llvm
115
116 #endif // LLVM_IR_PSEUDOPROBE_H
117