LLVM  10.0.0svn
Support.cpp
Go to the documentation of this file.
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 
18 namespace llvm {
19 namespace mca {
20 
21 #define DEBUG_TYPE "llvm-mca"
22 
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 
41  unsigned ProcResourceID = 0;
42 
43  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 
48  // Create a unique bitmask for every processor resource unit.
49  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  }
69 
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);
75  LLVM_DEBUG(dbgs() << '[' << format_decimal(I,2) << "] " << " - "
76  << format_hex(Masks[I],16) << " - "
77  << Desc.Name << '\n');
78  }
79 #endif
80 }
81 
82 double 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 
109 } // namespace mca
110 } // namespace llvm
uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B)
Definition: MathExtras.h:609
This class represents lattice values for constants.
Definition: AllocatorList.h:23
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:186
const MCProcResourceDesc * getProcResource(unsigned ProcResourceIdx) const
Definition: MCSchedule.h:339
double computeBlockRThroughput(const MCSchedModel &SM, unsigned DispatchWidth, unsigned NumMicroOps, ArrayRef< unsigned > ProcResourceUsage)
Compute the reciprocal block throughput from a set of processor resource cycles.
Definition: Support.cpp:82
void computeProcResourceMasks(const MCSchedModel &SM, MutableArrayRef< uint64_t > Masks)
Populates vector Masks with processor resource masks.
Definition: Support.cpp:39
ResourceCycles & operator+=(const ResourceCycles &RHS)
Definition: Support.cpp:23
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:290
const unsigned * SubUnitsIdxBegin
Definition: MCSchedule.h:54
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Helper functions used by various pipeline components.
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:336
Define a kind of processor resource that will be modeled by the scheduler.
Definition: MCSchedule.h:32
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
This class represents the number of cycles per resource (fractions of cycles).
Definition: Support.h:50
FormattedNumber format_decimal(int64_t N, unsigned Width)
format_decimal - Output N as a right justified, fixed-width decimal.
Definition: Format.h:211
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#define LLVM_DEBUG(X)
Definition: Debug.h:122
Machine model for scheduling, bundling, and heuristics.
Definition: MCSchedule.h:244
unsigned getNumProcResourceKinds() const
Definition: MCSchedule.h:335