1 //===--------------------- Support.cpp --------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements a few helper functions used by various pipeline
13 //===----------------------------------------------------------------------===//
15 #include "llvm/MCA/Support.h"
16 #include "llvm/MC/MCSchedule.h"
22 #define DEBUG_TYPE "llvm-mca"
24 ReleaseAtCycles
&ReleaseAtCycles::operator+=(const ReleaseAtCycles
&RHS
) {
25 if (Denominator
== RHS
.Denominator
)
26 Numerator
+= RHS
.Numerator
;
28 // Create a common denominator for LHS and RHS by calculating the least
29 // common multiple from the GCD.
30 unsigned GCD
= std::gcd(Denominator
, RHS
.Denominator
);
31 unsigned LCM
= (Denominator
* RHS
.Denominator
) / GCD
;
32 unsigned LHSNumerator
= Numerator
* (LCM
/ Denominator
);
33 unsigned RHSNumerator
= RHS
.Numerator
* (LCM
/ RHS
.Denominator
);
34 Numerator
= LHSNumerator
+ RHSNumerator
;
40 void computeProcResourceMasks(const MCSchedModel
&SM
,
41 MutableArrayRef
<uint64_t> Masks
) {
42 unsigned ProcResourceID
= 0;
44 assert(Masks
.size() == SM
.getNumProcResourceKinds() &&
45 "Invalid number of elements");
46 // Resource at index 0 is the 'InvalidUnit'. Set an invalid mask for it.
49 // Create a unique bitmask for every processor resource unit.
50 for (unsigned I
= 1, E
= SM
.getNumProcResourceKinds(); I
< E
; ++I
) {
51 const MCProcResourceDesc
&Desc
= *SM
.getProcResource(I
);
52 if (Desc
.SubUnitsIdxBegin
)
54 Masks
[I
] = 1ULL << ProcResourceID
;
58 // Create a unique bitmask for every processor resource group.
59 for (unsigned I
= 1, E
= SM
.getNumProcResourceKinds(); I
< E
; ++I
) {
60 const MCProcResourceDesc
&Desc
= *SM
.getProcResource(I
);
61 if (!Desc
.SubUnitsIdxBegin
)
63 Masks
[I
] = 1ULL << ProcResourceID
;
64 for (unsigned U
= 0; U
< Desc
.NumUnits
; ++U
) {
65 uint64_t OtherMask
= Masks
[Desc
.SubUnitsIdxBegin
[U
]];
66 Masks
[I
] |= OtherMask
;
72 LLVM_DEBUG(dbgs() << "\nProcessor resource masks:"
74 for (unsigned I
= 0, E
= SM
.getNumProcResourceKinds(); I
< E
; ++I
) {
75 const MCProcResourceDesc
&Desc
= *SM
.getProcResource(I
);
76 LLVM_DEBUG(dbgs() << '[' << format_decimal(I
,2) << "] " << " - "
77 << format_hex(Masks
[I
],16) << " - "
78 << Desc
.Name
<< '\n');
83 double computeBlockRThroughput(const MCSchedModel
&SM
, unsigned DispatchWidth
,
85 ArrayRef
<unsigned> ProcResourceUsage
) {
86 // The block throughput is bounded from above by the hardware dispatch
87 // throughput. That is because the DispatchWidth is an upper bound on the
88 // number of opcodes that can be part of a single dispatch group.
89 double Max
= static_cast<double>(NumMicroOps
) / DispatchWidth
;
91 // The block throughput is also limited by the amount of hardware parallelism.
92 // The number of available resource units affects the resource pressure
93 // distribution, as well as how many blocks can be executed every cycle.
94 for (unsigned I
= 0, E
= SM
.getNumProcResourceKinds(); I
< E
; ++I
) {
95 unsigned ReleaseAtCycles
= ProcResourceUsage
[I
];
99 const MCProcResourceDesc
&MCDesc
= *SM
.getProcResource(I
);
100 double Throughput
= static_cast<double>(ReleaseAtCycles
) / MCDesc
.NumUnits
;
101 Max
= std::max(Max
, Throughput
);
104 // The block reciprocal throughput is computed as the MAX of:
105 // - (NumMicroOps / DispatchWidth)
106 // - (NumUnits / ReleaseAtCycles) for every consumed processor resource.