SummaryView.cpp 4.4 KB

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