| 1 | //===--------------------- Support.cpp --------------------------*- 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 | /// This file implements a few helper functions used by various pipeline |
| 11 | /// components. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/MCA/Support.h" |
| 16 | #include "llvm/MC/MCSchedule.h" |
| 17 | #include "llvm/Support/Debug.h" |
| 18 | #include <numeric> |
| 19 | |
| 20 | namespace llvm { |
| 21 | namespace mca { |
| 22 | |
| 23 | #define DEBUG_TYPE "llvm-mca" |
| 24 | |
| 25 | ReleaseAtCycles &ReleaseAtCycles::operator+=(const ReleaseAtCycles &RHS) { |
| 26 | if (Denominator == RHS.Denominator) |
| 27 | Numerator += RHS.Numerator; |
| 28 | else { |
| 29 | // Create a common denominator for LHS and RHS by calculating the least |
| 30 | // common multiple from the GCD. |
| 31 | unsigned GCD = std::gcd(m: Denominator, n: RHS.Denominator); |
| 32 | unsigned LCM = (Denominator * RHS.Denominator) / GCD; |
| 33 | unsigned LHSNumerator = Numerator * (LCM / Denominator); |
| 34 | unsigned RHSNumerator = RHS.Numerator * (LCM / RHS.Denominator); |
| 35 | Numerator = LHSNumerator + RHSNumerator; |
| 36 | Denominator = LCM; |
| 37 | } |
| 38 | return *this; |
| 39 | } |
| 40 | |
| 41 | void computeProcResourceMasks(const MCSchedModel &SM, |
| 42 | MutableArrayRef<uint64_t> Masks) { |
| 43 | unsigned ProcResourceID = 0; |
| 44 | |
| 45 | assert(Masks.size() == SM.getNumProcResourceKinds() && |
| 46 | "Invalid number of elements" ); |
| 47 | // Resource at index 0 is the 'InvalidUnit'. Set an invalid mask for it. |
| 48 | Masks[0] = 0; |
| 49 | |
| 50 | // Create a unique bitmask for every processor resource unit. |
| 51 | for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) { |
| 52 | const MCProcResourceDesc &Desc = *SM.getProcResource(ProcResourceIdx: I); |
| 53 | if (Desc.SubUnitsIdxBegin) |
| 54 | continue; |
| 55 | Masks[I] = 1ULL << ProcResourceID; |
| 56 | ProcResourceID++; |
| 57 | } |
| 58 | |
| 59 | // Create a unique bitmask for every processor resource group. |
| 60 | for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) { |
| 61 | const MCProcResourceDesc &Desc = *SM.getProcResource(ProcResourceIdx: I); |
| 62 | if (!Desc.SubUnitsIdxBegin) |
| 63 | continue; |
| 64 | Masks[I] = 1ULL << ProcResourceID; |
| 65 | for (unsigned U = 0; U < Desc.NumUnits; ++U) { |
| 66 | uint64_t OtherMask = Masks[Desc.SubUnitsIdxBegin[U]]; |
| 67 | Masks[I] |= OtherMask; |
| 68 | } |
| 69 | ProcResourceID++; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #ifndef NDEBUG |
| 74 | void dumpProcResourceMasks(const MCSchedModel &SM, ArrayRef<uint64_t> Masks) { |
| 75 | dbgs() << "\nProcessor resource masks:\n" ; |
| 76 | for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) { |
| 77 | const MCProcResourceDesc &Desc = *SM.getProcResource(I); |
| 78 | dbgs() << '[' << format_decimal(I, 2) << "] " << " - " |
| 79 | << format_hex(Masks[I], 16) << " - " << Desc.Name << '\n'; |
| 80 | } |
| 81 | } |
| 82 | #endif |
| 83 | |
| 84 | double computeBlockRThroughput(const MCSchedModel &SM, unsigned DispatchWidth, |
| 85 | unsigned NumMicroOps, |
| 86 | ArrayRef<unsigned> ProcResourceUsage) { |
| 87 | // The block throughput is bounded from above by the hardware dispatch |
| 88 | // throughput. That is because the DispatchWidth is an upper bound on the |
| 89 | // number of opcodes that can be part of a single dispatch group. |
| 90 | double Max = static_cast<double>(NumMicroOps) / DispatchWidth; |
| 91 | |
| 92 | // The block throughput is also limited by the amount of hardware parallelism. |
| 93 | // The number of available resource units affects the resource pressure |
| 94 | // distribution, as well as how many blocks can be executed every cycle. |
| 95 | for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) { |
| 96 | unsigned ReleaseAtCycles = ProcResourceUsage[I]; |
| 97 | if (!ReleaseAtCycles) |
| 98 | continue; |
| 99 | |
| 100 | const MCProcResourceDesc &MCDesc = *SM.getProcResource(ProcResourceIdx: I); |
| 101 | double Throughput = static_cast<double>(ReleaseAtCycles) / MCDesc.NumUnits; |
| 102 | Max = std::max(a: Max, b: Throughput); |
| 103 | } |
| 104 | |
| 105 | // The block reciprocal throughput is computed as the MAX of: |
| 106 | // - (NumMicroOps / DispatchWidth) |
| 107 | // - (NumUnits / ReleaseAtCycles) for every consumed processor resource. |
| 108 | return Max; |
| 109 | } |
| 110 | |
| 111 | } // namespace mca |
| 112 | } // namespace llvm |
| 113 | |