1cc5e6a72SClement Courbet //===--------------------- Support.cpp --------------------------*- C++ -*-===//
2cc5e6a72SClement Courbet //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cc5e6a72SClement Courbet //
7cc5e6a72SClement Courbet //===----------------------------------------------------------------------===//
8cc5e6a72SClement Courbet /// \file
9cc5e6a72SClement Courbet ///
10cc5e6a72SClement Courbet /// This file implements a few helper functions used by various pipeline
11cc5e6a72SClement Courbet /// components.
12cc5e6a72SClement Courbet ///
13cc5e6a72SClement Courbet //===----------------------------------------------------------------------===//
14cc5e6a72SClement Courbet 
15cc5e6a72SClement Courbet #include "llvm/MCA/Support.h"
16cc5e6a72SClement Courbet #include "llvm/MC/MCSchedule.h"
17cc5e6a72SClement Courbet 
18cc5e6a72SClement Courbet namespace llvm {
19cc5e6a72SClement Courbet namespace mca {
20cc5e6a72SClement Courbet 
2197ed076dSAndrea Di Biagio #define DEBUG_TYPE "llvm-mca"
2297ed076dSAndrea Di Biagio 
operator +=(const ResourceCycles & RHS)23*d30fff9aSAndrea Di Biagio ResourceCycles &ResourceCycles::operator+=(const ResourceCycles &RHS) {
24*d30fff9aSAndrea Di Biagio   if (Denominator == RHS.Denominator)
25*d30fff9aSAndrea Di Biagio     Numerator += RHS.Numerator;
26*d30fff9aSAndrea Di Biagio   else {
27*d30fff9aSAndrea Di Biagio     // Create a common denominator for LHS and RHS by calculating the least
28*d30fff9aSAndrea Di Biagio     // common multiple from the GCD.
29*d30fff9aSAndrea Di Biagio     unsigned GCD = GreatestCommonDivisor64(Denominator, RHS.Denominator);
30*d30fff9aSAndrea Di Biagio     unsigned LCM = (Denominator * RHS.Denominator) / GCD;
31*d30fff9aSAndrea Di Biagio     unsigned LHSNumerator = Numerator * (LCM / Denominator);
32*d30fff9aSAndrea Di Biagio     unsigned RHSNumerator = RHS.Numerator * (LCM / RHS.Denominator);
33*d30fff9aSAndrea Di Biagio     Numerator = LHSNumerator + RHSNumerator;
34*d30fff9aSAndrea Di Biagio     Denominator = LCM;
35*d30fff9aSAndrea Di Biagio   }
36*d30fff9aSAndrea Di Biagio   return *this;
37*d30fff9aSAndrea Di Biagio }
38*d30fff9aSAndrea Di Biagio 
computeProcResourceMasks(const MCSchedModel & SM,MutableArrayRef<uint64_t> Masks)39cc5e6a72SClement Courbet void computeProcResourceMasks(const MCSchedModel &SM,
4097ed076dSAndrea Di Biagio                               MutableArrayRef<uint64_t> Masks) {
41cc5e6a72SClement Courbet   unsigned ProcResourceID = 0;
42cc5e6a72SClement Courbet 
4397ed076dSAndrea Di Biagio   assert(Masks.size() == SM.getNumProcResourceKinds() &&
4497ed076dSAndrea Di Biagio          "Invalid number of elements");
4597ed076dSAndrea Di Biagio   // Resource at index 0 is the 'InvalidUnit'. Set an invalid mask for it.
4697ed076dSAndrea Di Biagio   Masks[0] = 0;
4797ed076dSAndrea Di Biagio 
48cc5e6a72SClement Courbet   // Create a unique bitmask for every processor resource unit.
49cc5e6a72SClement Courbet   for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
50cc5e6a72SClement Courbet     const MCProcResourceDesc &Desc = *SM.getProcResource(I);
51cc5e6a72SClement Courbet     if (Desc.SubUnitsIdxBegin)
52cc5e6a72SClement Courbet       continue;
53cc5e6a72SClement Courbet     Masks[I] = 1ULL << ProcResourceID;
54cc5e6a72SClement Courbet     ProcResourceID++;
55cc5e6a72SClement Courbet   }
56cc5e6a72SClement Courbet 
57cc5e6a72SClement Courbet   // Create a unique bitmask for every processor resource group.
58cc5e6a72SClement Courbet   for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
59cc5e6a72SClement Courbet     const MCProcResourceDesc &Desc = *SM.getProcResource(I);
60cc5e6a72SClement Courbet     if (!Desc.SubUnitsIdxBegin)
61cc5e6a72SClement Courbet       continue;
62cc5e6a72SClement Courbet     Masks[I] = 1ULL << ProcResourceID;
63cc5e6a72SClement Courbet     for (unsigned U = 0; U < Desc.NumUnits; ++U) {
64cc5e6a72SClement Courbet       uint64_t OtherMask = Masks[Desc.SubUnitsIdxBegin[U]];
65cc5e6a72SClement Courbet       Masks[I] |= OtherMask;
66cc5e6a72SClement Courbet     }
67cc5e6a72SClement Courbet     ProcResourceID++;
68cc5e6a72SClement Courbet   }
6997ed076dSAndrea Di Biagio 
7097ed076dSAndrea Di Biagio #ifndef NDEBUG
7197ed076dSAndrea Di Biagio   LLVM_DEBUG(dbgs() << "\nProcessor resource masks:"
7297ed076dSAndrea Di Biagio                     << "\n");
7397ed076dSAndrea Di Biagio   for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
7497ed076dSAndrea Di Biagio     const MCProcResourceDesc &Desc = *SM.getProcResource(I);
75*d30fff9aSAndrea Di Biagio     LLVM_DEBUG(dbgs() << '[' << format_decimal(I,2) << "] " << " - "
76*d30fff9aSAndrea Di Biagio                       << format_hex(Masks[I],16) << " - "
77*d30fff9aSAndrea Di Biagio                       << Desc.Name << '\n');
7897ed076dSAndrea Di Biagio   }
7997ed076dSAndrea Di Biagio #endif
80cc5e6a72SClement Courbet }
81cc5e6a72SClement Courbet 
computeBlockRThroughput(const MCSchedModel & SM,unsigned DispatchWidth,unsigned NumMicroOps,ArrayRef<unsigned> ProcResourceUsage)82cc5e6a72SClement Courbet double computeBlockRThroughput(const MCSchedModel &SM, unsigned DispatchWidth,
83cc5e6a72SClement Courbet                                unsigned NumMicroOps,
84cc5e6a72SClement Courbet                                ArrayRef<unsigned> ProcResourceUsage) {
85cc5e6a72SClement Courbet   // The block throughput is bounded from above by the hardware dispatch
86cc5e6a72SClement Courbet   // throughput. That is because the DispatchWidth is an upper bound on the
87cc5e6a72SClement Courbet   // number of opcodes that can be part of a single dispatch group.
88cc5e6a72SClement Courbet   double Max = static_cast<double>(NumMicroOps) / DispatchWidth;
89cc5e6a72SClement Courbet 
90cc5e6a72SClement Courbet   // The block throughput is also limited by the amount of hardware parallelism.
91cc5e6a72SClement Courbet   // The number of available resource units affects the resource pressure
92cc5e6a72SClement Courbet   // distribution, as well as how many blocks can be executed every cycle.
93cc5e6a72SClement Courbet   for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
94cc5e6a72SClement Courbet     unsigned ResourceCycles = ProcResourceUsage[I];
95cc5e6a72SClement Courbet     if (!ResourceCycles)
96cc5e6a72SClement Courbet       continue;
97cc5e6a72SClement Courbet 
98cc5e6a72SClement Courbet     const MCProcResourceDesc &MCDesc = *SM.getProcResource(I);
99cc5e6a72SClement Courbet     double Throughput = static_cast<double>(ResourceCycles) / MCDesc.NumUnits;
100cc5e6a72SClement Courbet     Max = std::max(Max, Throughput);
101cc5e6a72SClement Courbet   }
102cc5e6a72SClement Courbet 
103cc5e6a72SClement Courbet   // The block reciprocal throughput is computed as the MAX of:
104cc5e6a72SClement Courbet   //  - (NumMicroOps / DispatchWidth)
105cc5e6a72SClement Courbet   //  - (NumUnits / ResourceCycles)   for every consumed processor resource.
106cc5e6a72SClement Courbet   return Max;
107cc5e6a72SClement Courbet }
108cc5e6a72SClement Courbet 
109cc5e6a72SClement Courbet } // namespace mca
110cc5e6a72SClement Courbet } // namespace llvm
111