PseudoProbe.h 2.9 KB

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