Support.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //===--------------------- Support.h ----------------------------*- 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. /// \file
  9. ///
  10. /// Helper functions used by various pipeline components.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MCA_SUPPORT_H
  14. #define LLVM_MCA_SUPPORT_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/MC/MCSchedule.h"
  18. #include "llvm/Support/Error.h"
  19. namespace llvm {
  20. namespace mca {
  21. template <typename T>
  22. class InstructionError : public ErrorInfo<InstructionError<T>> {
  23. public:
  24. static char ID;
  25. std::string Message;
  26. const T &Inst;
  27. InstructionError(std::string M, const T &MCI)
  28. : Message(std::move(M)), Inst(MCI) {}
  29. void log(raw_ostream &OS) const override { OS << Message; }
  30. std::error_code convertToErrorCode() const override {
  31. return inconvertibleErrorCode();
  32. }
  33. };
  34. template <typename T> char InstructionError<T>::ID;
  35. /// This class represents the number of cycles per resource (fractions of
  36. /// cycles). That quantity is managed here as a ratio, and accessed via the
  37. /// double cast-operator below. The two quantities, number of cycles and
  38. /// number of resources, are kept separate. This is used by the
  39. /// ResourcePressureView to calculate the average resource cycles
  40. /// per instruction/iteration.
  41. class ResourceCycles {
  42. unsigned Numerator, Denominator;
  43. public:
  44. ResourceCycles() : Numerator(0), Denominator(1) {}
  45. ResourceCycles(unsigned Cycles, unsigned ResourceUnits = 1)
  46. : Numerator(Cycles), Denominator(ResourceUnits) {}
  47. operator double() const {
  48. assert(Denominator && "Invalid denominator (must be non-zero).");
  49. return (Denominator == 1) ? Numerator : (double)Numerator / Denominator;
  50. }
  51. unsigned getNumerator() const { return Numerator; }
  52. unsigned getDenominator() const { return Denominator; }
  53. // Add the components of RHS to this instance. Instead of calculating
  54. // the final value here, we keep track of the numerator and denominator
  55. // separately, to reduce floating point error.
  56. ResourceCycles &operator+=(const ResourceCycles &RHS);
  57. };
  58. /// Populates vector Masks with processor resource masks.
  59. ///
  60. /// The number of bits set in a mask depends on the processor resource type.
  61. /// Each processor resource mask has at least one bit set. For groups, the
  62. /// number of bits set in the mask is equal to the cardinality of the group plus
  63. /// one. Excluding the most significant bit, the remaining bits in the mask
  64. /// identify processor resources that are part of the group.
  65. ///
  66. /// Example:
  67. ///
  68. /// ResourceA -- Mask: 0b001
  69. /// ResourceB -- Mask: 0b010
  70. /// ResourceAB -- Mask: 0b100 U (ResourceA::Mask | ResourceB::Mask) == 0b111
  71. ///
  72. /// ResourceAB is a processor resource group containing ResourceA and ResourceB.
  73. /// Each resource mask uniquely identifies a resource; both ResourceA and
  74. /// ResourceB only have one bit set.
  75. /// ResourceAB is a group; excluding the most significant bit in the mask, the
  76. /// remaining bits identify the composition of the group.
  77. ///
  78. /// Resource masks are used by the ResourceManager to solve set membership
  79. /// problems with simple bit manipulation operations.
  80. void computeProcResourceMasks(const MCSchedModel &SM,
  81. MutableArrayRef<uint64_t> Masks);
  82. // Returns the index of the highest bit set. For resource masks, the position of
  83. // the highest bit set can be used to construct a resource mask identifier.
  84. inline unsigned getResourceStateIndex(uint64_t Mask) {
  85. assert(Mask && "Processor Resource Mask cannot be zero!");
  86. return (std::numeric_limits<uint64_t>::digits - countLeadingZeros(Mask)) - 1;
  87. }
  88. /// Compute the reciprocal block throughput from a set of processor resource
  89. /// cycles. The reciprocal block throughput is computed as the MAX between:
  90. /// - NumMicroOps / DispatchWidth
  91. /// - ProcResourceCycles / #ProcResourceUnits (for every consumed resource).
  92. double computeBlockRThroughput(const MCSchedModel &SM, unsigned DispatchWidth,
  93. unsigned NumMicroOps,
  94. ArrayRef<unsigned> ProcResourceUsage);
  95. } // namespace mca
  96. } // namespace llvm
  97. #endif // LLVM_MCA_SUPPORT_H