InstructionInfoView.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 (PrintEncodings) {
  30. TempStream << "[7]: Encoding Size\n";
  31. TempStream << "\n[1] [2] [3] [4] [5] [6] [7] "
  32. << "Encodings: Instructions:\n";
  33. } else {
  34. TempStream << "\n[1] [2] [3] [4] [5] [6] Instructions:\n";
  35. }
  36. for (const auto &I : enumerate(zip(IIVD, Source))) {
  37. const InstructionInfoViewData &IIVDEntry = std::get<0>(I.value());
  38. TempStream << ' ' << IIVDEntry.NumMicroOpcodes << " ";
  39. if (IIVDEntry.NumMicroOpcodes < 10)
  40. TempStream << " ";
  41. else if (IIVDEntry.NumMicroOpcodes < 100)
  42. TempStream << ' ';
  43. TempStream << IIVDEntry.Latency << " ";
  44. if (IIVDEntry.Latency < 10)
  45. TempStream << " ";
  46. else if (IIVDEntry.Latency < 100)
  47. TempStream << ' ';
  48. if (IIVDEntry.RThroughput.hasValue()) {
  49. double RT = IIVDEntry.RThroughput.getValue();
  50. TempStream << format("%.2f", RT) << ' ';
  51. if (RT < 10.0)
  52. TempStream << " ";
  53. else if (RT < 100.0)
  54. TempStream << ' ';
  55. } else {
  56. TempStream << " - ";
  57. }
  58. TempStream << (IIVDEntry.mayLoad ? " * " : " ");
  59. TempStream << (IIVDEntry.mayStore ? " * " : " ");
  60. TempStream << (IIVDEntry.hasUnmodeledSideEffects ? " U " : " ");
  61. if (PrintEncodings) {
  62. StringRef Encoding(CE.getEncoding(I.index()));
  63. unsigned EncodingSize = Encoding.size();
  64. TempStream << " " << EncodingSize
  65. << (EncodingSize < 10 ? " " : " ");
  66. TempStream.flush();
  67. formatted_raw_ostream FOS(TempStream);
  68. for (unsigned i = 0, e = Encoding.size(); i != e; ++i)
  69. FOS << format("%02x ", (uint8_t)Encoding[i]);
  70. FOS.PadToColumn(30);
  71. FOS.flush();
  72. }
  73. const MCInst &Inst = std::get<1>(I.value());
  74. TempStream << printInstructionString(Inst) << '\n';
  75. }
  76. TempStream.flush();
  77. OS << Buffer;
  78. }
  79. void InstructionInfoView::collectData(
  80. MutableArrayRef<InstructionInfoViewData> IIVD) const {
  81. const llvm::MCSubtargetInfo &STI = getSubTargetInfo();
  82. const MCSchedModel &SM = STI.getSchedModel();
  83. for (const auto &I : zip(getSource(), IIVD)) {
  84. const MCInst &Inst = std::get<0>(I);
  85. InstructionInfoViewData &IIVDEntry = std::get<1>(I);
  86. const MCInstrDesc &MCDesc = MCII.get(Inst.getOpcode());
  87. // Obtain the scheduling class information from the instruction.
  88. unsigned SchedClassID = MCDesc.getSchedClass();
  89. unsigned CPUID = SM.getProcessorID();
  90. // Try to solve variant scheduling classes.
  91. while (SchedClassID && SM.getSchedClassDesc(SchedClassID)->isVariant())
  92. SchedClassID =
  93. STI.resolveVariantSchedClass(SchedClassID, &Inst, &MCII, CPUID);
  94. const MCSchedClassDesc &SCDesc = *SM.getSchedClassDesc(SchedClassID);
  95. IIVDEntry.NumMicroOpcodes = SCDesc.NumMicroOps;
  96. IIVDEntry.Latency = MCSchedModel::computeInstrLatency(STI, SCDesc);
  97. // Add extra latency due to delays in the forwarding data paths.
  98. IIVDEntry.Latency += MCSchedModel::getForwardingDelayCycles(
  99. STI.getReadAdvanceEntries(SCDesc));
  100. IIVDEntry.RThroughput = MCSchedModel::getReciprocalThroughput(STI, SCDesc);
  101. IIVDEntry.mayLoad = MCDesc.mayLoad();
  102. IIVDEntry.mayStore = MCDesc.mayStore();
  103. IIVDEntry.hasUnmodeledSideEffects = MCDesc.hasUnmodeledSideEffects();
  104. }
  105. }
  106. // Construct a JSON object from a single InstructionInfoViewData object.
  107. json::Object
  108. InstructionInfoView::toJSON(const InstructionInfoViewData &IIVD) const {
  109. json::Object JO({{"NumMicroOpcodes", IIVD.NumMicroOpcodes},
  110. {"Latency", IIVD.Latency},
  111. {"mayLoad", IIVD.mayLoad},
  112. {"mayStore", IIVD.mayStore},
  113. {"hasUnmodeledSideEffects", IIVD.hasUnmodeledSideEffects}});
  114. JO.try_emplace("RThroughput", IIVD.RThroughput.getValueOr(0.0));
  115. return JO;
  116. }
  117. json::Value InstructionInfoView::toJSON() const {
  118. ArrayRef<llvm::MCInst> Source = getSource();
  119. if (!Source.size())
  120. return json::Value(0);
  121. IIVDVec IIVD(Source.size());
  122. collectData(IIVD);
  123. json::Array InstInfo;
  124. for (const auto &I : enumerate(IIVD)) {
  125. const InstructionInfoViewData &IIVDEntry = I.value();
  126. json::Object JO = toJSON(IIVDEntry);
  127. JO.try_emplace("Instruction", (unsigned)I.index());
  128. InstInfo.push_back(std::move(JO));
  129. }
  130. return json::Value(std::move(InstInfo));
  131. }
  132. } // namespace mca.
  133. } // namespace llvm