blob: ce1f0f6f211b510f57d4482dec41b68e3d38ad7d [file] [log] [blame]
Andrea Di Biagio35622482018-03-22 10:19:20 +00001//===--------------------- Support.cpp --------------------------*- C++ -*-===//
Andrea Di Biagio4704f032018-03-20 12:25:54 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Andrea Di Biagio4704f032018-03-20 12:25:54 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
9///
Matt Davisdea343d2018-06-25 16:53:00 +000010/// This file implements a few helper functions used by various pipeline
Andrea Di Biagio4704f032018-03-20 12:25:54 +000011/// components.
12///
13//===----------------------------------------------------------------------===//
14
Clement Courbetcc5e6a72018-12-17 08:08:31 +000015#include "llvm/MCA/Support.h"
Andrea Di Biagio4704f032018-03-20 12:25:54 +000016#include "llvm/MC/MCSchedule.h"
17
Fangrui Song5a8fd652018-10-30 15:56:08 +000018namespace llvm {
Andrea Di Biagio4704f032018-03-20 12:25:54 +000019namespace mca {
20
Andrea Di Biagio97ed0762019-01-10 13:59:13 +000021#define DEBUG_TYPE "llvm-mca"
22
Andrea Di Biagiod30fff92019-02-12 16:18:57 +000023ResourceCycles &ResourceCycles::operator+=(const ResourceCycles &RHS) {
24 if (Denominator == RHS.Denominator)
25 Numerator += RHS.Numerator;
26 else {
27 // Create a common denominator for LHS and RHS by calculating the least
28 // common multiple from the GCD.
29 unsigned GCD = GreatestCommonDivisor64(Denominator, RHS.Denominator);
30 unsigned LCM = (Denominator * RHS.Denominator) / GCD;
31 unsigned LHSNumerator = Numerator * (LCM / Denominator);
32 unsigned RHSNumerator = RHS.Numerator * (LCM / RHS.Denominator);
33 Numerator = LHSNumerator + RHSNumerator;
34 Denominator = LCM;
35 }
36 return *this;
37}
38
Andrea Di Biagio4704f032018-03-20 12:25:54 +000039void computeProcResourceMasks(const MCSchedModel &SM,
Andrea Di Biagio97ed0762019-01-10 13:59:13 +000040 MutableArrayRef<uint64_t> Masks) {
Andrea Di Biagio4704f032018-03-20 12:25:54 +000041 unsigned ProcResourceID = 0;
42
Andrea Di Biagio97ed0762019-01-10 13:59:13 +000043 assert(Masks.size() == SM.getNumProcResourceKinds() &&
44 "Invalid number of elements");
45 // Resource at index 0 is the 'InvalidUnit'. Set an invalid mask for it.
46 Masks[0] = 0;
47
Andrea Di Biagio4704f032018-03-20 12:25:54 +000048 // Create a unique bitmask for every processor resource unit.
Andrea Di Biagio4704f032018-03-20 12:25:54 +000049 for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
50 const MCProcResourceDesc &Desc = *SM.getProcResource(I);
51 if (Desc.SubUnitsIdxBegin)
52 continue;
53 Masks[I] = 1ULL << ProcResourceID;
54 ProcResourceID++;
55 }
56
57 // Create a unique bitmask for every processor resource group.
58 for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
59 const MCProcResourceDesc &Desc = *SM.getProcResource(I);
60 if (!Desc.SubUnitsIdxBegin)
61 continue;
62 Masks[I] = 1ULL << ProcResourceID;
63 for (unsigned U = 0; U < Desc.NumUnits; ++U) {
64 uint64_t OtherMask = Masks[Desc.SubUnitsIdxBegin[U]];
65 Masks[I] |= OtherMask;
66 }
67 ProcResourceID++;
68 }
Andrea Di Biagio97ed0762019-01-10 13:59:13 +000069
70#ifndef NDEBUG
71 LLVM_DEBUG(dbgs() << "\nProcessor resource masks:"
72 << "\n");
73 for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
74 const MCProcResourceDesc &Desc = *SM.getProcResource(I);
Andrea Di Biagiod30fff92019-02-12 16:18:57 +000075 LLVM_DEBUG(dbgs() << '[' << format_decimal(I,2) << "] " << " - "
76 << format_hex(Masks[I],16) << " - "
77 << Desc.Name << '\n');
Andrea Di Biagio97ed0762019-01-10 13:59:13 +000078 }
79#endif
Andrea Di Biagio4704f032018-03-20 12:25:54 +000080}
Andrea Di Biagiobdc67062018-06-01 14:35:21 +000081
82double computeBlockRThroughput(const MCSchedModel &SM, unsigned DispatchWidth,
83 unsigned NumMicroOps,
84 ArrayRef<unsigned> ProcResourceUsage) {
85 // The block throughput is bounded from above by the hardware dispatch
86 // throughput. That is because the DispatchWidth is an upper bound on the
87 // number of opcodes that can be part of a single dispatch group.
88 double Max = static_cast<double>(NumMicroOps) / DispatchWidth;
89
90 // The block throughput is also limited by the amount of hardware parallelism.
91 // The number of available resource units affects the resource pressure
92 // distribution, as well as how many blocks can be executed every cycle.
93 for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
94 unsigned ResourceCycles = ProcResourceUsage[I];
95 if (!ResourceCycles)
96 continue;
97
98 const MCProcResourceDesc &MCDesc = *SM.getProcResource(I);
99 double Throughput = static_cast<double>(ResourceCycles) / MCDesc.NumUnits;
100 Max = std::max(Max, Throughput);
101 }
102
103 // The block reciprocal throughput is computed as the MAX of:
104 // - (NumMicroOps / DispatchWidth)
105 // - (NumUnits / ResourceCycles) for every consumed processor resource.
106 return Max;
107}
108
Andrea Di Biagio4704f032018-03-20 12:25:54 +0000109} // namespace mca
Fangrui Song5a8fd652018-10-30 15:56:08 +0000110} // namespace llvm