SummaryView.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===--------------------- SummaryView.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 functionalities used by the SummaryView to print
  11. /// the report information.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "Views/SummaryView.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/MCA/Support.h"
  17. #include "llvm/Support/Format.h"
  18. namespace llvm {
  19. namespace mca {
  20. #define DEBUG_TYPE "llvm-mca"
  21. SummaryView::SummaryView(const MCSchedModel &Model, ArrayRef<MCInst> S,
  22. unsigned Width)
  23. : SM(Model), Source(S), DispatchWidth(Width ? Width : Model.IssueWidth),
  24. LastInstructionIdx(0), TotalCycles(0), NumMicroOps(0),
  25. ProcResourceUsage(Model.getNumProcResourceKinds(), 0),
  26. ProcResourceMasks(Model.getNumProcResourceKinds()),
  27. ResIdx2ProcResID(Model.getNumProcResourceKinds(), 0) {
  28. computeProcResourceMasks(SM, ProcResourceMasks);
  29. for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
  30. unsigned Index = getResourceStateIndex(ProcResourceMasks[I]);
  31. ResIdx2ProcResID[Index] = I;
  32. }
  33. }
  34. void SummaryView::onEvent(const HWInstructionEvent &Event) {
  35. if (Event.Type == HWInstructionEvent::Dispatched)
  36. LastInstructionIdx = Event.IR.getSourceIndex();
  37. // We are only interested in the "instruction retired" events generated by
  38. // the retire stage for instructions that are part of iteration #0.
  39. if (Event.Type != HWInstructionEvent::Retired ||
  40. Event.IR.getSourceIndex() >= Source.size())
  41. return;
  42. // Update the cumulative number of resource cycles based on the processor
  43. // resource usage information available from the instruction descriptor. We
  44. // need to compute the cumulative number of resource cycles for every
  45. // processor resource which is consumed by an instruction of the block.
  46. const Instruction &Inst = *Event.IR.getInstruction();
  47. const InstrDesc &Desc = Inst.getDesc();
  48. NumMicroOps += Desc.NumMicroOps;
  49. for (const std::pair<uint64_t, ResourceUsage> &RU : Desc.Resources) {
  50. if (RU.second.size()) {
  51. unsigned ProcResID = ResIdx2ProcResID[getResourceStateIndex(RU.first)];
  52. ProcResourceUsage[ProcResID] += RU.second.size();
  53. }
  54. }
  55. }
  56. void SummaryView::printView(raw_ostream &OS) const {
  57. std::string Buffer;
  58. raw_string_ostream TempStream(Buffer);
  59. DisplayValues DV;
  60. collectData(DV);
  61. TempStream << "Iterations: " << DV.Iterations;
  62. TempStream << "\nInstructions: " << DV.TotalInstructions;
  63. TempStream << "\nTotal Cycles: " << DV.TotalCycles;
  64. TempStream << "\nTotal uOps: " << DV.TotalUOps << '\n';
  65. TempStream << "\nDispatch Width: " << DV.DispatchWidth;
  66. TempStream << "\nuOps Per Cycle: "
  67. << format("%.2f", floor((DV.UOpsPerCycle * 100) + 0.5) / 100);
  68. TempStream << "\nIPC: "
  69. << format("%.2f", floor((DV.IPC * 100) + 0.5) / 100);
  70. TempStream << "\nBlock RThroughput: "
  71. << format("%.1f", floor((DV.BlockRThroughput * 10) + 0.5) / 10)
  72. << '\n';
  73. TempStream.flush();
  74. OS << Buffer;
  75. }
  76. void SummaryView::collectData(DisplayValues &DV) const {
  77. DV.Instructions = Source.size();
  78. DV.Iterations = (LastInstructionIdx / DV.Instructions) + 1;
  79. DV.TotalInstructions = DV.Instructions * DV.Iterations;
  80. DV.TotalCycles = TotalCycles;
  81. DV.DispatchWidth = DispatchWidth;
  82. DV.TotalUOps = NumMicroOps * DV.Iterations;
  83. DV.UOpsPerCycle = (double)DV.TotalUOps / TotalCycles;
  84. DV.IPC = (double)DV.TotalInstructions / TotalCycles;
  85. DV.BlockRThroughput = computeBlockRThroughput(SM, DispatchWidth, NumMicroOps,
  86. ProcResourceUsage);
  87. }
  88. json::Value SummaryView::toJSON() const {
  89. DisplayValues DV;
  90. collectData(DV);
  91. json::Object JO({{"Iterations", DV.Iterations},
  92. {"Instructions", DV.TotalInstructions},
  93. {"TotalCycles", DV.TotalCycles},
  94. {"TotaluOps", DV.TotalUOps},
  95. {"DispatchWidth", DV.DispatchWidth},
  96. {"uOpsPerCycle", DV.UOpsPerCycle},
  97. {"IPC", DV.IPC},
  98. {"BlockRThroughput", DV.BlockRThroughput}});
  99. return JO;
  100. }
  101. } // namespace mca.
  102. } // namespace llvm