LLVM  10.0.0svn
Support.h
Go to the documentation of this file.
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.
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.
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.
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
std::string & operator+=(std::string &buffer, StringRef string)
Definition: StringRef.h:902
unsigned getNumerator() const
Definition: Support.h:63
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Support.h:37
InstructionError(std::string M, const T &MCI)
Definition: Support.h:32
unsigned getDenominator() const
Definition: Support.h:64
Definition: BitVector.h:937
double computeBlockRThroughput(const MCSchedModel &SM, unsigned DispatchWidth, unsigned NumMicroOps, ArrayRef< unsigned > ProcResourceUsage)
Compute the reciprocal block throughput from a set of processor resource cycles.
Definition: Support.cpp:82
void computeProcResourceMasks(const MCSchedModel &SM, MutableArrayRef< uint64_t > Masks)
Populates vector Masks with processor resource masks.
Definition: Support.cpp:39
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:290
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: Support.h:35
unsigned countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0&#39;s from the most significant bit to the least stopping at the first 1...
Definition: MathExtras.h:225
This class represents the number of cycles per resource (fractions of cycles).
Definition: Support.h:50
Base class for user error types.
Definition: Error.h:344
ResourceCycles(unsigned Cycles, unsigned ResourceUnits=1)
Definition: Support.h:55
unsigned getResourceStateIndex(uint64_t Mask)
Definition: Support.h:99
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Machine model for scheduling, bundling, and heuristics.
Definition: MCSchedule.h:244
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77