Support.cpp 4.0 KB

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