InstructionInfoView.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //===--------------------- InstructionInfoView.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 the InstructionInfoView API.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "Views/InstructionInfoView.h"
  14. #include "llvm/Support/FormattedStream.h"
  15. #include "llvm/Support/JSON.h"
  16. namespace llvm {
  17. namespace mca {
  18. void InstructionInfoView::printView(raw_ostream &OS) const {
  19. std::string Buffer;
  20. raw_string_ostream TempStream(Buffer);
  21. ArrayRef<llvm::MCInst> Source = getSource();
  22. if (!Source.size())
  23. return;
  24. IIVDVec IIVD(Source.size());
  25. collectData(IIVD);
  26. TempStream << "\n\nInstruction Info:\n";
  27. TempStream << "[1]: #uOps\n[2]: Latency\n[3]: RThroughput\n"
  28. << "[4]: MayLoad\n[5]: MayStore\n[6]: HasSideEffects (U)\n";
  29. if (PrintBarriers) {
  30. TempStream << "[7]: LoadBarrier\n[8]: StoreBarrier\n";
  31. }
  32. if (PrintEncodings) {
  33. if (PrintBarriers) {
  34. TempStream << "[9]: Encoding Size\n";
  35. TempStream << "\n[1] [2] [3] [4] [5] [6] [7] [8] "
  36. << "[9] Encodings: Instructions:\n";
  37. } else {
  38. TempStream << "[7]: Encoding Size\n";
  39. TempStream << "\n[1] [2] [3] [4] [5] [6] [7] "
  40. << "Encodings: Instructions:\n";
  41. }
  42. } else {
  43. if (PrintBarriers) {
  44. TempStream << "\n[1] [2] [3] [4] [5] [6] [7] [8] "
  45. << "Instructions:\n";
  46. } else {
  47. TempStream << "\n[1] [2] [3] [4] [5] [6] "
  48. << "Instructions:\n";
  49. }
  50. }
  51. int Index = 0;
  52. for (const auto &I : enumerate(zip(IIVD, Source))) {
  53. const InstructionInfoViewData &IIVDEntry = std::get<0>(I.value());
  54. TempStream << ' ' << IIVDEntry.NumMicroOpcodes << " ";
  55. if (IIVDEntry.NumMicroOpcodes < 10)
  56. TempStream << " ";
  57. else if (IIVDEntry.NumMicroOpcodes < 100)
  58. TempStream << ' ';
  59. TempStream << IIVDEntry.Latency << " ";
  60. if (IIVDEntry.Latency < 10)
  61. TempStream << " ";
  62. else if (IIVDEntry.Latency < 100)
  63. TempStream << ' ';
  64. if (IIVDEntry.RThroughput) {
  65. double RT = *IIVDEntry.RThroughput;
  66. TempStream << format("%.2f", RT) << ' ';
  67. if (RT < 10.0)
  68. TempStream << " ";
  69. else if (RT < 100.0)
  70. TempStream << ' ';
  71. } else {
  72. TempStream << " - ";
  73. }
  74. TempStream << (IIVDEntry.mayLoad ? " * " : " ");
  75. TempStream << (IIVDEntry.mayStore ? " * " : " ");
  76. TempStream << (IIVDEntry.hasUnmodeledSideEffects ? " U " : " ");
  77. if (PrintBarriers) {
  78. TempStream << (LoweredInsts[Index]->isALoadBarrier() ? " * "
  79. : " ");
  80. TempStream << (LoweredInsts[Index]->isAStoreBarrier() ? " * "
  81. : " ");
  82. }
  83. if (PrintEncodings) {
  84. StringRef Encoding(CE.getEncoding(I.index()));
  85. unsigned EncodingSize = Encoding.size();
  86. TempStream << " " << EncodingSize
  87. << (EncodingSize < 10 ? " " : " ");
  88. TempStream.flush();
  89. formatted_raw_ostream FOS(TempStream);
  90. for (unsigned i = 0, e = Encoding.size(); i != e; ++i)
  91. FOS << format("%02x ", (uint8_t)Encoding[i]);
  92. FOS.PadToColumn(30);
  93. FOS.flush();
  94. }
  95. const MCInst &Inst = std::get<1>(I.value());
  96. TempStream << printInstructionString(Inst) << '\n';
  97. ++Index;
  98. }
  99. TempStream.flush();
  100. OS << Buffer;
  101. }
  102. void InstructionInfoView::collectData(
  103. MutableArrayRef<InstructionInfoViewData> IIVD) const {
  104. const llvm::MCSubtargetInfo &STI = getSubTargetInfo();
  105. const MCSchedModel &SM = STI.getSchedModel();
  106. for (const auto I : zip(getSource(), IIVD)) {
  107. const MCInst &Inst = std::get<0>(I);
  108. InstructionInfoViewData &IIVDEntry = std::get<1>(I);
  109. const MCInstrDesc &MCDesc = MCII.get(Inst.getOpcode());
  110. // Obtain the scheduling class information from the instruction.
  111. unsigned SchedClassID = MCDesc.getSchedClass();
  112. unsigned CPUID = SM.getProcessorID();
  113. // Try to solve variant scheduling classes.
  114. while (SchedClassID && SM.getSchedClassDesc(SchedClassID)->isVariant())
  115. SchedClassID =
  116. STI.resolveVariantSchedClass(SchedClassID, &Inst, &MCII, CPUID);
  117. const MCSchedClassDesc &SCDesc = *SM.getSchedClassDesc(SchedClassID);
  118. IIVDEntry.NumMicroOpcodes = SCDesc.NumMicroOps;
  119. IIVDEntry.Latency = MCSchedModel::computeInstrLatency(STI, SCDesc);
  120. // Add extra latency due to delays in the forwarding data paths.
  121. IIVDEntry.Latency += MCSchedModel::getForwardingDelayCycles(
  122. STI.getReadAdvanceEntries(SCDesc));
  123. IIVDEntry.RThroughput = MCSchedModel::getReciprocalThroughput(STI, SCDesc);
  124. IIVDEntry.mayLoad = MCDesc.mayLoad();
  125. IIVDEntry.mayStore = MCDesc.mayStore();
  126. IIVDEntry.hasUnmodeledSideEffects = MCDesc.hasUnmodeledSideEffects();
  127. }
  128. }
  129. // Construct a JSON object from a single InstructionInfoViewData object.
  130. json::Object
  131. InstructionInfoView::toJSON(const InstructionInfoViewData &IIVD) const {
  132. json::Object JO({{"NumMicroOpcodes", IIVD.NumMicroOpcodes},
  133. {"Latency", IIVD.Latency},
  134. {"mayLoad", IIVD.mayLoad},
  135. {"mayStore", IIVD.mayStore},
  136. {"hasUnmodeledSideEffects", IIVD.hasUnmodeledSideEffects}});
  137. JO.try_emplace("RThroughput", IIVD.RThroughput.value_or(0.0));
  138. return JO;
  139. }
  140. json::Value InstructionInfoView::toJSON() const {
  141. ArrayRef<llvm::MCInst> Source = getSource();
  142. if (!Source.size())
  143. return json::Value(0);
  144. IIVDVec IIVD(Source.size());
  145. collectData(IIVD);
  146. json::Array InstInfo;
  147. for (const auto &I : enumerate(IIVD)) {
  148. const InstructionInfoViewData &IIVDEntry = I.value();
  149. json::Object JO = toJSON(IIVDEntry);
  150. JO.try_emplace("Instruction", (unsigned)I.index());
  151. InstInfo.push_back(std::move(JO));
  152. }
  153. return json::Object({{"InstructionList", json::Value(std::move(InstInfo))}});
  154. }
  155. } // namespace mca.
  156. } // namespace llvm