Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:10

0001 //===- PseudoProbe.h - Pseudo Probe IR Helpers ------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Pseudo probe IR intrinsic and dwarf discriminator manipulation routines.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_IR_PSEUDOPROBE_H
0014 #define LLVM_IR_PSEUDOPROBE_H
0015 
0016 #include <cassert>
0017 #include <cstdint>
0018 #include <limits>
0019 #include <optional>
0020 
0021 namespace llvm {
0022 
0023 class Instruction;
0024 
0025 constexpr const char *PseudoProbeDescMetadataName = "llvm.pseudo_probe_desc";
0026 
0027 enum class PseudoProbeReservedId { Invalid = 0, Last = Invalid };
0028 
0029 enum class PseudoProbeType { Block = 0, IndirectCall, DirectCall };
0030 
0031 enum class PseudoProbeAttributes {
0032   Reserved = 0x1,
0033   Sentinel = 0x2,         // A place holder for split function entry address.
0034   HasDiscriminator = 0x4, // for probes with a discriminator
0035 };
0036 
0037 // The saturated distrution factor representing 100% for block probes.
0038 constexpr static uint64_t PseudoProbeFullDistributionFactor =
0039     std::numeric_limits<uint64_t>::max();
0040 
0041 struct PseudoProbeDwarfDiscriminator {
0042 public:
0043   // The following APIs encodes/decodes per-probe information to/from a
0044   // 32-bit integer which is organized as:
0045   //  [2:0] - 0x7, this is reserved for regular discriminator,
0046   //          see DWARF discriminator encoding rule
0047   //  if the [28:28] bit is zero:
0048   //    [18:3] for probe id.
0049   //  else:
0050   //    [15:3] for probe id, [18:16] for dwarf base discriminator.
0051   //  [25:19] - probe distribution factor
0052   //  [27:26] - probe type, see PseudoProbeType
0053   //  [28:28] - indicates whether dwarf base discriminator is encoded.
0054   //  [30:29] - reserved for probe attributes
0055   static uint32_t
0056   packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags, uint32_t Factor,
0057                 std::optional<uint32_t> DwarfBaseDiscriminator) {
0058     assert(Index <= 0xFFFF && "Probe index too big to encode, exceeding 2^16");
0059     assert(Type <= 0x3 && "Probe type too big to encode, exceeding 3");
0060     assert(Flags <= 0x7);
0061     assert(Factor <= 100 &&
0062            "Probe distribution factor too big to encode, exceeding 100");
0063     uint32_t V = (Index << 3) | (Factor << 19) | (Type << 26) | 0x7;
0064     // If both the probe id and dwarf base discriminator is small, the probe id
0065     // space is shared with the dwarf base discriminator, this is to make the
0066     // probe-based build compatible with the dwarf-based profile.
0067     // Pack the dwarf base discriminator into [18:16] and set the [28:28] bit.
0068     if (Index <= 0x1FFF && DwarfBaseDiscriminator &&
0069         *DwarfBaseDiscriminator <= 0x7)
0070       V |= (1 << 28) | (*DwarfBaseDiscriminator << 16);
0071     return V;
0072   }
0073 
0074   static uint32_t extractProbeIndex(uint32_t Value) {
0075     if (isDwarfBaseDiscriminatorEncoded(Value))
0076       return (Value >> 3) & 0x1FFF;
0077     return (Value >> 3) & 0xFFFF;
0078   }
0079 
0080   static std::optional<uint32_t> extractDwarfBaseDiscriminator(uint32_t Value) {
0081     if (isDwarfBaseDiscriminatorEncoded(Value))
0082       return (Value >> 16) & 0x7;
0083     return std::nullopt;
0084   }
0085 
0086   static bool isDwarfBaseDiscriminatorEncoded(uint32_t Value) {
0087     return Value & 0x10000000;
0088   }
0089 
0090   static uint32_t extractProbeType(uint32_t Value) {
0091     return (Value >> 26) & 0x3;
0092   }
0093 
0094   static uint32_t extractProbeAttributes(uint32_t Value) {
0095     return (Value >> 29) & 0x7;
0096   }
0097 
0098   static uint32_t extractProbeFactor(uint32_t Value) {
0099     return (Value >> 19) & 0x7F;
0100   }
0101 
0102   // The saturated distrution factor representing 100% for callsites.
0103   constexpr static uint8_t FullDistributionFactor = 100;
0104 };
0105 
0106 class PseudoProbeDescriptor {
0107   uint64_t FunctionGUID;
0108   uint64_t FunctionHash;
0109 
0110 public:
0111   PseudoProbeDescriptor(uint64_t GUID, uint64_t Hash)
0112       : FunctionGUID(GUID), FunctionHash(Hash) {}
0113   uint64_t getFunctionGUID() const { return FunctionGUID; }
0114   uint64_t getFunctionHash() const { return FunctionHash; }
0115 };
0116 
0117 struct PseudoProbe {
0118   uint32_t Id;
0119   uint32_t Type;
0120   uint32_t Attr;
0121   uint32_t Discriminator;
0122   // Distribution factor that estimates the portion of the real execution count.
0123   // A saturated distribution factor stands for 1.0 or 100%. A pesudo probe has
0124   // a factor with the value ranged from 0.0 to 1.0.
0125   float Factor;
0126 };
0127 
0128 static inline bool isSentinelProbe(uint32_t Flags) {
0129   return Flags & (uint32_t)PseudoProbeAttributes::Sentinel;
0130 }
0131 
0132 static inline bool hasDiscriminator(uint32_t Flags) {
0133   return Flags & (uint32_t)PseudoProbeAttributes::HasDiscriminator;
0134 }
0135 
0136 std::optional<PseudoProbe> extractProbe(const Instruction &Inst);
0137 
0138 void setProbeDistributionFactor(Instruction &Inst, float Factor);
0139 } // end namespace llvm
0140 
0141 #endif // LLVM_IR_PSEUDOPROBE_H