PseudoProbe.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- PseudoProbe.h - Pseudo Probe IR Helpers ------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Pseudo probe IR intrinsic and dwarf discriminator manipulation routines.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_IR_PSEUDOPROBE_H
  18. #define LLVM_IR_PSEUDOPROBE_H
  19. #include <cassert>
  20. #include <cstdint>
  21. #include <limits>
  22. #include <optional>
  23. namespace llvm {
  24. class Instruction;
  25. constexpr const char *PseudoProbeDescMetadataName = "llvm.pseudo_probe_desc";
  26. enum class PseudoProbeReservedId { Invalid = 0, Last = Invalid };
  27. enum class PseudoProbeType { Block = 0, IndirectCall, DirectCall };
  28. enum class PseudoProbeAttributes {
  29. Reserved = 0x1,
  30. Sentinel = 0x2, // A place holder for split function entry address.
  31. };
  32. // The saturated distrution factor representing 100% for block probes.
  33. constexpr static uint64_t PseudoProbeFullDistributionFactor =
  34. std::numeric_limits<uint64_t>::max();
  35. struct PseudoProbeDwarfDiscriminator {
  36. public:
  37. // The following APIs encodes/decodes per-probe information to/from a
  38. // 32-bit integer which is organized as:
  39. // [2:0] - 0x7, this is reserved for regular discriminator,
  40. // see DWARF discriminator encoding rule
  41. // [18:3] - probe id
  42. // [25:19] - probe distribution factor
  43. // [28:26] - probe type, see PseudoProbeType
  44. // [31:29] - reserved for probe attributes
  45. static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags,
  46. uint32_t Factor) {
  47. assert(Index <= 0xFFFF && "Probe index too big to encode, exceeding 2^16");
  48. assert(Type <= 0x7 && "Probe type too big to encode, exceeding 7");
  49. assert(Flags <= 0x7);
  50. assert(Factor <= 100 &&
  51. "Probe distribution factor too big to encode, exceeding 100");
  52. return (Index << 3) | (Factor << 19) | (Type << 26) | 0x7;
  53. }
  54. static uint32_t extractProbeIndex(uint32_t Value) {
  55. return (Value >> 3) & 0xFFFF;
  56. }
  57. static uint32_t extractProbeType(uint32_t Value) {
  58. return (Value >> 26) & 0x7;
  59. }
  60. static uint32_t extractProbeAttributes(uint32_t Value) {
  61. return (Value >> 29) & 0x7;
  62. }
  63. static uint32_t extractProbeFactor(uint32_t Value) {
  64. return (Value >> 19) & 0x7F;
  65. }
  66. // The saturated distrution factor representing 100% for callsites.
  67. constexpr static uint8_t FullDistributionFactor = 100;
  68. };
  69. struct PseudoProbe {
  70. uint32_t Id;
  71. uint32_t Type;
  72. uint32_t Attr;
  73. // Distribution factor that estimates the portion of the real execution count.
  74. // A saturated distribution factor stands for 1.0 or 100%. A pesudo probe has
  75. // a factor with the value ranged from 0.0 to 1.0.
  76. float Factor;
  77. };
  78. static inline bool isSentinelProbe(uint32_t Flags) {
  79. return Flags & (uint32_t)PseudoProbeAttributes::Sentinel;
  80. }
  81. std::optional<PseudoProbe> extractProbe(const Instruction &Inst);
  82. void setProbeDistributionFactor(Instruction &Inst, float Factor);
  83. } // end namespace llvm
  84. #endif // LLVM_IR_PSEUDOPROBE_H
  85. #ifdef __GNUC__
  86. #pragma GCC diagnostic pop
  87. #endif