blob: 1da097c909227e66abe283cde0974f2ee1f7ccde [file] [log] [blame]
Andrew Walbran16937d02019-10-22 13:54:20 +01001//===--------------------- 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
22namespace llvm {
23namespace mca {
24
25template <typename T>
26class InstructionError : public ErrorInfo<InstructionError<T>> {
27public:
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
42template <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.
50class ResourceCycles {
51 unsigned Numerator, Denominator;
52
53public:
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.
94void computeProcResourceMasks(const MCSchedModel &SM,
95 MutableArrayRef<uint64_t> Masks);
96
Andrew Walbran3d2c1972020-04-07 12:24:26 +010097// 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.
99inline 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
Andrew Walbran16937d02019-10-22 13:54:20 +0100104/// 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).
108double 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