Support.cpp 4.0 KB

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