PseudoProbe.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "llvm/ADT/Optional.h"
  20. #include <cassert>
  21. #include <cstdint>
  22. #include <limits>
  23. namespace llvm {
  24. class Instruction;
  25. constexpr const char *PseudoProbeDescMetadataName = "llvm.pseudo_probe_desc";
  26. enum class PseudoProbeType { Block = 0, IndirectCall, DirectCall };
  27. // The saturated distrution factor representing 100% for block probes.
  28. constexpr static uint64_t PseudoProbeFullDistributionFactor =
  29. std::numeric_limits<uint64_t>::max();
  30. struct PseudoProbeDwarfDiscriminator {
  31. public:
  32. // The following APIs encodes/decodes per-probe information to/from a
  33. // 32-bit integer which is organized as:
  34. // [2:0] - 0x7, this is reserved for regular discriminator,
  35. // see DWARF discriminator encoding rule
  36. // [18:3] - probe id
  37. // [25:19] - probe distribution factor
  38. // [28:26] - probe type, see PseudoProbeType
  39. // [31:29] - reserved for probe attributes
  40. static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags,
  41. uint32_t Factor) {
  42. assert(Index <= 0xFFFF && "Probe index too big to encode, exceeding 2^16");
  43. assert(Type <= 0x7 && "Probe type too big to encode, exceeding 7");
  44. assert(Flags <= 0x7);
  45. assert(Factor <= 100 &&
  46. "Probe distribution factor too big to encode, exceeding 100");
  47. return (Index << 3) | (Factor << 19) | (Type << 26) | 0x7;
  48. }
  49. static uint32_t extractProbeIndex(uint32_t Value) {
  50. return (Value >> 3) & 0xFFFF;
  51. }
  52. static uint32_t extractProbeType(uint32_t Value) {
  53. return (Value >> 26) & 0x7;
  54. }
  55. static uint32_t extractProbeAttributes(uint32_t Value) {
  56. return (Value >> 29) & 0x7;
  57. }
  58. static uint32_t extractProbeFactor(uint32_t Value) {
  59. return (Value >> 19) & 0x7F;
  60. }
  61. // The saturated distrution factor representing 100% for callsites.
  62. constexpr static uint8_t FullDistributionFactor = 100;
  63. };
  64. struct PseudoProbe {
  65. uint32_t Id;
  66. uint32_t Type;
  67. uint32_t Attr;
  68. // Distribution factor that estimates the portion of the real execution count.
  69. // A saturated distribution factor stands for 1.0 or 100%. A pesudo probe has
  70. // a factor with the value ranged from 0.0 to 1.0.
  71. float Factor;
  72. };
  73. Optional<PseudoProbe> extractProbe(const Instruction &Inst);
  74. void setProbeDistributionFactor(Instruction &Inst, float Factor);
  75. } // end namespace llvm
  76. #endif // LLVM_IR_PSEUDOPROBE_H
  77. #ifdef __GNUC__
  78. #pragma GCC diagnostic pop
  79. #endif