Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:16

0001 //===--------------------- Support.h ----------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// Helper functions used by various pipeline components.
0011 ///
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_MCA_SUPPORT_H
0015 #define LLVM_MCA_SUPPORT_H
0016 
0017 #include "llvm/ADT/ArrayRef.h"
0018 #include "llvm/ADT/SmallVector.h"
0019 #include "llvm/MC/MCSchedule.h"
0020 #include "llvm/Support/Error.h"
0021 #include "llvm/Support/MathExtras.h"
0022 
0023 namespace llvm {
0024 namespace mca {
0025 
0026 template <typename T>
0027 class InstructionError : public ErrorInfo<InstructionError<T>> {
0028 public:
0029   static char ID;
0030   std::string Message;
0031   const T &Inst;
0032 
0033   InstructionError(std::string M, const T &MCI)
0034       : Message(std::move(M)), Inst(MCI) {}
0035 
0036   void log(raw_ostream &OS) const override { OS << Message; }
0037 
0038   std::error_code convertToErrorCode() const override {
0039     return inconvertibleErrorCode();
0040   }
0041 };
0042 
0043 template <typename T> char InstructionError<T>::ID;
0044 
0045 /// This class represents the number of cycles per resource (fractions of
0046 /// cycles).  That quantity is managed here as a ratio, and accessed via the
0047 /// double cast-operator below.  The two quantities, number of cycles and
0048 /// number of resources, are kept separate.  This is used by the
0049 /// ResourcePressureView to calculate the average resource cycles
0050 /// per instruction/iteration.
0051 class ReleaseAtCycles {
0052   unsigned Numerator, Denominator;
0053 
0054 public:
0055   ReleaseAtCycles() : Numerator(0), Denominator(1) {}
0056   ReleaseAtCycles(unsigned Cycles, unsigned ResourceUnits = 1)
0057       : Numerator(Cycles), Denominator(ResourceUnits) {}
0058 
0059   operator double() const {
0060     assert(Denominator && "Invalid denominator (must be non-zero).");
0061     return (Denominator == 1) ? Numerator : (double)Numerator / Denominator;
0062   }
0063 
0064   unsigned getNumerator() const { return Numerator; }
0065   unsigned getDenominator() const { return Denominator; }
0066 
0067   // Add the components of RHS to this instance.  Instead of calculating
0068   // the final value here, we keep track of the numerator and denominator
0069   // separately, to reduce floating point error.
0070   ReleaseAtCycles &operator+=(const ReleaseAtCycles &RHS);
0071 };
0072 
0073 /// Populates vector Masks with processor resource masks.
0074 ///
0075 /// The number of bits set in a mask depends on the processor resource type.
0076 /// Each processor resource mask has at least one bit set. For groups, the
0077 /// number of bits set in the mask is equal to the cardinality of the group plus
0078 /// one. Excluding the most significant bit, the remaining bits in the mask
0079 /// identify processor resources that are part of the group.
0080 ///
0081 /// Example:
0082 ///
0083 ///  ResourceA  -- Mask: 0b001
0084 ///  ResourceB  -- Mask: 0b010
0085 ///  ResourceAB -- Mask: 0b100 U (ResourceA::Mask | ResourceB::Mask) == 0b111
0086 ///
0087 /// ResourceAB is a processor resource group containing ResourceA and ResourceB.
0088 /// Each resource mask uniquely identifies a resource; both ResourceA and
0089 /// ResourceB only have one bit set.
0090 /// ResourceAB is a group; excluding the most significant bit in the mask, the
0091 /// remaining bits identify the composition of the group.
0092 ///
0093 /// Resource masks are used by the ResourceManager to solve set membership
0094 /// problems with simple bit manipulation operations.
0095 void computeProcResourceMasks(const MCSchedModel &SM,
0096                               MutableArrayRef<uint64_t> Masks);
0097 
0098 // Returns the index of the highest bit set. For resource masks, the position of
0099 // the highest bit set can be used to construct a resource mask identifier.
0100 inline unsigned getResourceStateIndex(uint64_t Mask) {
0101   assert(Mask && "Processor Resource Mask cannot be zero!");
0102   return llvm::Log2_64(Mask);
0103 }
0104 
0105 /// Compute the reciprocal block throughput from a set of processor resource
0106 /// cycles. The reciprocal block throughput is computed as the MAX between:
0107 ///  - NumMicroOps / DispatchWidth
0108 ///  - ProcReleaseAtCycles / #ProcResourceUnits  (for every consumed resource).
0109 double computeBlockRThroughput(const MCSchedModel &SM, unsigned DispatchWidth,
0110                                unsigned NumMicroOps,
0111                                ArrayRef<unsigned> ProcResourceUsage);
0112 } // namespace mca
0113 } // namespace llvm
0114 
0115 #endif // LLVM_MCA_SUPPORT_H