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