1 //===--------------------- SummaryView.cpp -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 ///
11 /// This file implements the functionalities used by the SummaryView to print
12 /// the report information.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "Views/SummaryView.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/MCA/Support.h"
19 #include "llvm/Support/Format.h"
20 
21 namespace llvm {
22 namespace mca {
23 
24 #define DEBUG_TYPE "llvm-mca"
25 
26 SummaryView::SummaryView(const MCSchedModel &Model, ArrayRef<MCInst> S,
27                          unsigned Width)
28     : SM(Model), Source(S), DispatchWidth(Width), LastInstructionIdx(0),
29       TotalCycles(0), NumMicroOps(0),
30       ProcResourceUsage(Model.getNumProcResourceKinds(), 0) {
31   computeProcResourceMasks(SM, ProcResourceMasks);
32 }
33 
34 void SummaryView::onEvent(const HWInstructionEvent &Event) {
35   if (Event.Type == HWInstructionEvent::Dispatched)
36     LastInstructionIdx = Event.IR.getSourceIndex();
37 
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 
44   // Update the cumulative number of resource cycles based on the processor
45   // resource usage information available from the instruction descriptor. We
46   // need to compute the cumulative number of resource cycles for every
47   // processor resource which is consumed by an instruction of the block.
48   const Instruction &Inst = *Event.IR.getInstruction();
49   const InstrDesc &Desc = Inst.getDesc();
50   NumMicroOps += Desc.NumMicroOps;
51   for (const std::pair<uint64_t, const ResourceUsage> &RU : Desc.Resources) {
52     if (RU.second.size()) {
53       const auto It = find(ProcResourceMasks, RU.first);
54       assert(It != ProcResourceMasks.end() &&
55              "Invalid processor resource mask!");
56       ProcResourceUsage[std::distance(ProcResourceMasks.begin(), It)] +=
57           RU.second.size();
58     }
59   }
60 }
61 
62 void SummaryView::printView(raw_ostream &OS) const {
63   unsigned Instructions = Source.size();
64   unsigned Iterations = (LastInstructionIdx / Instructions) + 1;
65   unsigned TotalInstructions = Instructions * Iterations;
66   unsigned TotalUOps = NumMicroOps * Iterations;
67   double IPC = (double)TotalInstructions / TotalCycles;
68   double UOpsPerCycle = (double)TotalUOps / TotalCycles;
69   double BlockRThroughput = computeBlockRThroughput(
70       SM, DispatchWidth, NumMicroOps, ProcResourceUsage);
71 
72   std::string Buffer;
73   raw_string_ostream TempStream(Buffer);
74   TempStream << "Iterations:        " << Iterations;
75   TempStream << "\nInstructions:      " << TotalInstructions;
76   TempStream << "\nTotal Cycles:      " << TotalCycles;
77   TempStream << "\nTotal uOps:        " << TotalUOps << '\n';
78   TempStream << "\nDispatch Width:    " << DispatchWidth;
79   TempStream << "\nuOps Per Cycle:    "
80              << format("%.2f", floor((UOpsPerCycle * 100) + 0.5) / 100);
81   TempStream << "\nIPC:               "
82              << format("%.2f", floor((IPC * 100) + 0.5) / 100);
83   TempStream << "\nBlock RThroughput: "
84              << format("%.1f", floor((BlockRThroughput * 10) + 0.5) / 10)
85              << '\n';
86   TempStream.flush();
87   OS << Buffer;
88 }
89 } // namespace mca.
90 } // namespace llvm
91