• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SummaryView.h"
17 #include "Support.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/Format.h"
20 
21 namespace mca {
22 
23 #define DEBUG_TYPE "llvm-mca"
24 
25 using namespace llvm;
26 
SummaryView(const llvm::MCSchedModel & Model,const SourceMgr & S,unsigned Width)27 SummaryView::SummaryView(const llvm::MCSchedModel &Model, const SourceMgr &S,
28                          unsigned Width)
29     : SM(Model), Source(S), DispatchWidth(Width), TotalCycles(0),
30       NumMicroOps(0), ProcResourceUsage(Model.getNumProcResourceKinds(), 0),
31       ProcResourceMasks(Model.getNumProcResourceKinds(), 0) {
32   computeProcResourceMasks(SM, ProcResourceMasks);
33 }
34 
onEvent(const HWInstructionEvent & Event)35 void SummaryView::onEvent(const HWInstructionEvent &Event) {
36   // We are only interested in the "instruction dispatched" events generated by
37   // the dispatch stage for instructions that are part of iteration #0.
38   if (Event.Type != HWInstructionEvent::Dispatched)
39     return;
40 
41   if (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 
printView(raw_ostream & OS) const62 void SummaryView::printView(raw_ostream &OS) const {
63   unsigned Iterations = Source.getNumIterations();
64   unsigned Instructions = Source.size();
65   unsigned TotalInstructions = Instructions * Iterations;
66   double IPC = (double)TotalInstructions / TotalCycles;
67   double BlockRThroughput = computeBlockRThroughput(
68       SM, DispatchWidth, NumMicroOps, ProcResourceUsage);
69 
70   std::string Buffer;
71   raw_string_ostream TempStream(Buffer);
72   TempStream << "Iterations:        " << Iterations;
73   TempStream << "\nInstructions:      " << TotalInstructions;
74   TempStream << "\nTotal Cycles:      " << TotalCycles;
75   TempStream << "\nDispatch Width:    " << DispatchWidth;
76   TempStream << "\nIPC:               " << format("%.2f", IPC);
77 
78   // Round to the block reciprocal throughput to the nearest tenth.
79   TempStream << "\nBlock RThroughput: "
80              << format("%.1f", floor((BlockRThroughput * 10) + 0.5) / 10)
81              << '\n';
82   TempStream.flush();
83   OS << Buffer;
84 }
85 } // namespace mca.
86