MCSchedule.cpp 6.2 KB

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