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