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