MCSchedule.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===- MCSchedule.cpp - Scheduling ------------------------------*- 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. //
  9. // This file defines the default scheduling model.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/MC/MCSchedule.h"
  13. #include "llvm/MC/MCInst.h"
  14. #include "llvm/MC/MCInstrDesc.h"
  15. #include "llvm/MC/MCInstrInfo.h"
  16. #include "llvm/MC/MCSubtargetInfo.h"
  17. #include <optional>
  18. #include <type_traits>
  19. using namespace llvm;
  20. static_assert(std::is_pod<MCSchedModel>::value,
  21. "We shouldn't have a static constructor here");
  22. const MCSchedModel MCSchedModel::Default = {DefaultIssueWidth,
  23. DefaultMicroOpBufferSize,
  24. DefaultLoopMicroOpBufferSize,
  25. DefaultLoadLatency,
  26. DefaultHighLatency,
  27. DefaultMispredictPenalty,
  28. false,
  29. true,
  30. 0,
  31. nullptr,
  32. nullptr,
  33. 0,
  34. 0,
  35. nullptr,
  36. nullptr};
  37. int MCSchedModel::computeInstrLatency(const MCSubtargetInfo &STI,
  38. const MCSchedClassDesc &SCDesc) {
  39. int Latency = 0;
  40. for (unsigned DefIdx = 0, DefEnd = SCDesc.NumWriteLatencyEntries;
  41. DefIdx != DefEnd; ++DefIdx) {
  42. // Lookup the definition's write latency in SubtargetInfo.
  43. const MCWriteLatencyEntry *WLEntry =
  44. STI.getWriteLatencyEntry(&SCDesc, DefIdx);
  45. // Early exit if we found an invalid latency.
  46. if (WLEntry->Cycles < 0)
  47. return WLEntry->Cycles;
  48. Latency = std::max(Latency, static_cast<int>(WLEntry->Cycles));
  49. }
  50. return Latency;
  51. }
  52. int MCSchedModel::computeInstrLatency(const MCSubtargetInfo &STI,
  53. unsigned SchedClass) const {
  54. const MCSchedClassDesc &SCDesc = *getSchedClassDesc(SchedClass);
  55. if (!SCDesc.isValid())
  56. return 0;
  57. if (!SCDesc.isVariant())
  58. return MCSchedModel::computeInstrLatency(STI, SCDesc);
  59. llvm_unreachable("unsupported variant scheduling class");
  60. }
  61. int MCSchedModel::computeInstrLatency(const MCSubtargetInfo &STI,
  62. const MCInstrInfo &MCII,
  63. const MCInst &Inst) const {
  64. unsigned SchedClass = MCII.get(Inst.getOpcode()).getSchedClass();
  65. const MCSchedClassDesc *SCDesc = getSchedClassDesc(SchedClass);
  66. if (!SCDesc->isValid())
  67. return 0;
  68. unsigned CPUID = getProcessorID();
  69. while (SCDesc->isVariant()) {
  70. SchedClass = STI.resolveVariantSchedClass(SchedClass, &Inst, &MCII, CPUID);
  71. SCDesc = getSchedClassDesc(SchedClass);
  72. }
  73. if (SchedClass)
  74. return MCSchedModel::computeInstrLatency(STI, *SCDesc);
  75. llvm_unreachable("unsupported variant scheduling class");
  76. }
  77. double
  78. MCSchedModel::getReciprocalThroughput(const MCSubtargetInfo &STI,
  79. const MCSchedClassDesc &SCDesc) {
  80. std::optional<double> Throughput;
  81. const MCSchedModel &SM = STI.getSchedModel();
  82. const MCWriteProcResEntry *I = STI.getWriteProcResBegin(&SCDesc);
  83. const MCWriteProcResEntry *E = STI.getWriteProcResEnd(&SCDesc);
  84. for (; I != E; ++I) {
  85. if (!I->Cycles)
  86. continue;
  87. unsigned NumUnits = SM.getProcResource(I->ProcResourceIdx)->NumUnits;
  88. double Temp = NumUnits * 1.0 / I->Cycles;
  89. Throughput = Throughput ? std::min(*Throughput, Temp) : Temp;
  90. }
  91. if (Throughput)
  92. return 1.0 / *Throughput;
  93. // If no throughput value was calculated, assume that we can execute at the
  94. // maximum issue width scaled by number of micro-ops for the schedule class.
  95. return ((double)SCDesc.NumMicroOps) / SM.IssueWidth;
  96. }
  97. double
  98. MCSchedModel::getReciprocalThroughput(const MCSubtargetInfo &STI,
  99. const MCInstrInfo &MCII,
  100. const MCInst &Inst) const {
  101. unsigned SchedClass = MCII.get(Inst.getOpcode()).getSchedClass();
  102. const MCSchedClassDesc *SCDesc = getSchedClassDesc(SchedClass);
  103. // If there's no valid class, assume that the instruction executes/completes
  104. // at the maximum issue width.
  105. if (!SCDesc->isValid())
  106. return 1.0 / IssueWidth;
  107. unsigned CPUID = getProcessorID();
  108. while (SCDesc->isVariant()) {
  109. SchedClass = STI.resolveVariantSchedClass(SchedClass, &Inst, &MCII, CPUID);
  110. SCDesc = getSchedClassDesc(SchedClass);
  111. }
  112. if (SchedClass)
  113. return MCSchedModel::getReciprocalThroughput(STI, *SCDesc);
  114. llvm_unreachable("unsupported variant scheduling class");
  115. }
  116. double
  117. MCSchedModel::getReciprocalThroughput(unsigned SchedClass,
  118. const InstrItineraryData &IID) {
  119. std::optional<double> Throughput;
  120. const InstrStage *I = IID.beginStage(SchedClass);
  121. const InstrStage *E = IID.endStage(SchedClass);
  122. for (; I != E; ++I) {
  123. if (!I->getCycles())
  124. continue;
  125. double Temp = llvm::popcount(I->getUnits()) * 1.0 / I->getCycles();
  126. Throughput = Throughput ? std::min(*Throughput, Temp) : Temp;
  127. }
  128. if (Throughput)
  129. return 1.0 / *Throughput;
  130. // If there are no execution resources specified for this class, then assume
  131. // that it can execute at the maximum default issue width.
  132. return 1.0 / DefaultIssueWidth;
  133. }
  134. unsigned
  135. MCSchedModel::getForwardingDelayCycles(ArrayRef<MCReadAdvanceEntry> Entries,
  136. unsigned WriteResourceID) {
  137. if (Entries.empty())
  138. return 0;
  139. int DelayCycles = 0;
  140. for (const MCReadAdvanceEntry &E : Entries) {
  141. if (E.WriteResourceID != WriteResourceID)
  142. continue;
  143. DelayCycles = std::min(DelayCycles, E.Cycles);
  144. }
  145. return std::abs(DelayCycles);
  146. }