]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-mca/Views/SummaryView.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-mca / Views / SummaryView.cpp
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       ProcResourceMasks(Model.getNumProcResourceKinds()) {
32   computeProcResourceMasks(SM, ProcResourceMasks);
33 }
34
35 void SummaryView::onEvent(const HWInstructionEvent &Event) {
36   if (Event.Type == HWInstructionEvent::Dispatched)
37     LastInstructionIdx = Event.IR.getSourceIndex();
38
39   // We are only interested in the "instruction retired" events generated by
40   // the retire stage for instructions that are part of iteration #0.
41   if (Event.Type != HWInstructionEvent::Retired ||
42       Event.IR.getSourceIndex() >= Source.size())
43     return;
44
45   // Update the cumulative number of resource cycles based on the processor
46   // resource usage information available from the instruction descriptor. We
47   // need to compute the cumulative number of resource cycles for every
48   // processor resource which is consumed by an instruction of the block.
49   const Instruction &Inst = *Event.IR.getInstruction();
50   const InstrDesc &Desc = Inst.getDesc();
51   NumMicroOps += Desc.NumMicroOps;
52   for (const std::pair<uint64_t, const ResourceUsage> &RU : Desc.Resources) {
53     if (RU.second.size()) {
54       const auto It = find(ProcResourceMasks, RU.first);
55       assert(It != ProcResourceMasks.end() &&
56              "Invalid processor resource mask!");
57       ProcResourceUsage[std::distance(ProcResourceMasks.begin(), It)] +=
58           RU.second.size();
59     }
60   }
61 }
62
63 void SummaryView::printView(raw_ostream &OS) const {
64   unsigned Instructions = Source.size();
65   unsigned Iterations = (LastInstructionIdx / Instructions) + 1;
66   unsigned TotalInstructions = Instructions * Iterations;
67   unsigned TotalUOps = NumMicroOps * Iterations;
68   double IPC = (double)TotalInstructions / TotalCycles;
69   double UOpsPerCycle = (double)TotalUOps / TotalCycles;
70   double BlockRThroughput = computeBlockRThroughput(
71       SM, DispatchWidth, NumMicroOps, ProcResourceUsage);
72
73   std::string Buffer;
74   raw_string_ostream TempStream(Buffer);
75   TempStream << "Iterations:        " << Iterations;
76   TempStream << "\nInstructions:      " << TotalInstructions;
77   TempStream << "\nTotal Cycles:      " << TotalCycles;
78   TempStream << "\nTotal uOps:        " << TotalUOps << '\n';
79   TempStream << "\nDispatch Width:    " << DispatchWidth;
80   TempStream << "\nuOps Per Cycle:    "
81              << format("%.2f", floor((UOpsPerCycle * 100) + 0.5) / 100);
82   TempStream << "\nIPC:               "
83              << format("%.2f", floor((IPC * 100) + 0.5) / 100);
84   TempStream << "\nBlock RThroughput: "
85              << format("%.1f", floor((BlockRThroughput * 10) + 0.5) / 10)
86              << '\n';
87   TempStream.flush();
88   OS << Buffer;
89 }
90 } // namespace mca.
91 } // namespace llvm