Support.h 4.6 KB

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