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 };
35
36 // The saturated distrution factor representing 100% for block probes.
37 constexpr static uint64_t PseudoProbeFullDistributionFactor =
38 std::numeric_limits<uint64_t>::max();
39
40 struct PseudoProbeDwarfDiscriminator {
41 public:
42 // The following APIs encodes/decodes per-probe information to/from a
43 // 32-bit integer which is organized as:
44 // [2:0] - 0x7, this is reserved for regular discriminator,
45 // see DWARF discriminator encoding rule
46 // [18:3] - probe id
47 // [25:19] - probe distribution factor
48 // [28:26] - probe type, see PseudoProbeType
49 // [31:29] - reserved for probe attributes
packProbeDataPseudoProbeDwarfDiscriminator50 static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags,
51 uint32_t Factor) {
52 assert(Index <= 0xFFFF && "Probe index too big to encode, exceeding 2^16");
53 assert(Type <= 0x7 && "Probe type too big to encode, exceeding 7");
54 assert(Flags <= 0x7);
55 assert(Factor <= 100 &&
56 "Probe distribution factor too big to encode, exceeding 100");
57 return (Index << 3) | (Factor << 19) | (Type << 26) | 0x7;
58 }
59
extractProbeIndexPseudoProbeDwarfDiscriminator60 static uint32_t extractProbeIndex(uint32_t Value) {
61 return (Value >> 3) & 0xFFFF;
62 }
63
extractProbeTypePseudoProbeDwarfDiscriminator64 static uint32_t extractProbeType(uint32_t Value) {
65 return (Value >> 26) & 0x7;
66 }
67
extractProbeAttributesPseudoProbeDwarfDiscriminator68 static uint32_t extractProbeAttributes(uint32_t Value) {
69 return (Value >> 29) & 0x7;
70 }
71
extractProbeFactorPseudoProbeDwarfDiscriminator72 static uint32_t extractProbeFactor(uint32_t Value) {
73 return (Value >> 19) & 0x7F;
74 }
75
76 // The saturated distrution factor representing 100% for callsites.
77 constexpr static uint8_t FullDistributionFactor = 100;
78 };
79
80 struct PseudoProbe {
81 uint32_t Id;
82 uint32_t Type;
83 uint32_t Attr;
84 // Distribution factor that estimates the portion of the real execution count.
85 // A saturated distribution factor stands for 1.0 or 100%. A pesudo probe has
86 // a factor with the value ranged from 0.0 to 1.0.
87 float Factor;
88 };
89
isSentinelProbe(uint32_t Flags)90 static inline bool isSentinelProbe(uint32_t Flags) {
91 return Flags & (uint32_t)PseudoProbeAttributes::Sentinel;
92 }
93
94 std::optional<PseudoProbe> extractProbe(const Instruction &Inst);
95
96 void setProbeDistributionFactor(Instruction &Inst, float Factor);
97 } // end namespace llvm
98
99 #endif // LLVM_IR_PSEUDOPROBE_H
100