blob: c3cec95f8dfad3e639b0438dee7cb024f28fe624 [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===--------------------- BackendStatistics.h ------------------*- 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 a printer class for printing generic Backend
12/// statistics related to the dispatch logic, scheduler and retire unit.
13///
14/// Example:
15/// ========
16///
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000017/// Dynamic Dispatch Stall Cycles:
18/// RAT - Register unavailable: 0
19/// RCU - Retire tokens unavailable: 0
20/// SCHEDQ - Scheduler full: 42
21/// LQ - Load queue full: 0
22/// SQ - Store queue full: 0
23/// GROUP - Static restrictions on the dispatch group: 0
24///
25///
26/// Register Alias Table:
27/// Total number of mappings created: 210
28/// Max number of mappings used: 35
29///
30///
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000031/// Dispatch Logic - number of cycles where we saw N instructions dispatched:
32/// [# dispatched], [# cycles]
33/// 0, 15 (11.5%)
34/// 5, 4 (3.1%)
35///
36/// Schedulers - number of cycles where we saw N instructions issued:
37/// [# issued], [# cycles]
38/// 0, 7 (5.4%)
39/// 1, 4 (3.1%)
40/// 2, 8 (6.2%)
41///
42/// Retire Control Unit - number of cycles where we saw N instructions retired:
43/// [# retired], [# cycles]
44/// 0, 9 (6.9%)
45/// 1, 6 (4.6%)
46/// 2, 1 (0.8%)
47/// 4, 3 (2.3%)
48///
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000049///
50/// Scheduler's queue usage:
51/// JALU01, 0/20
52/// JFPU01, 18/18
53/// JLSAGU, 0/12
54///
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000055//===----------------------------------------------------------------------===//
56
57#ifndef LLVM_TOOLS_LLVM_MCA_BACKENDSTATISTICS_H
58#define LLVM_TOOLS_LLVM_MCA_BACKENDSTATISTICS_H
59
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000060#include "View.h"
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +000061#include "llvm/ADT/SmallVector.h"
Andrea Di Biagiofbf37cc2018-04-03 15:36:15 +000062#include "llvm/ADT/DenseMap.h"
Andrea Di Biagio09771ad2018-03-16 22:21:52 +000063#include "llvm/MC/MCSubtargetInfo.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000064#include "llvm/Support/raw_ostream.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000065
66namespace mca {
67
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000068class BackendStatistics : public View {
Andrea Di Biagio09771ad2018-03-16 22:21:52 +000069 const llvm::MCSubtargetInfo &STI;
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +000070
Andrea Di Biagiofbf37cc2018-04-03 15:36:15 +000071 using Histogram = llvm::DenseMap<unsigned, unsigned>;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000072 Histogram DispatchGroupSizePerCycle;
73 Histogram RetiredPerCycle;
74 Histogram IssuedPerCycle;
75
76 unsigned NumDispatched;
77 unsigned NumIssued;
78 unsigned NumRetired;
79 unsigned NumCycles;
80
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +000081 // Counts dispatch stall events caused by unavailability of resources. There
82 // is one counter for every generic stall kind (see class HWStallEvent).
83 llvm::SmallVector<unsigned, 8> HWStalls;
84
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +000085 // Tracks the usage of a scheduler's queue.
86 struct BufferUsage {
87 unsigned SlotsInUse;
88 unsigned MaxUsedSlots;
89 };
90
91 // There is a map entry for each buffered resource in the scheduling model.
92 // Every time a buffer is consumed/freed, this view updates the corresponding
93 // entry.
94 llvm::DenseMap<unsigned, BufferUsage> BufferedResources;
95
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000096 void updateHistograms() {
97 DispatchGroupSizePerCycle[NumDispatched]++;
98 IssuedPerCycle[NumIssued]++;
99 RetiredPerCycle[NumRetired]++;
100 NumDispatched = 0;
101 NumIssued = 0;
102 NumRetired = 0;
103 }
104
Andrea Di Biagio12ef5262018-03-21 18:11:05 +0000105 // Used to track the number of physical registers used in a register file.
106 struct RegisterFileUsage {
107 unsigned TotalMappings;
108 unsigned MaxUsedMappings;
109 unsigned CurrentlyUsedMappings;
110 };
111
112 // There is one entry for each register file implemented by the processor.
113 llvm::SmallVector<RegisterFileUsage, 4> RegisterFiles;
114
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000115 void initializeRegisterFileInfo();
116
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000117 void printRetireUnitStatistics(llvm::raw_ostream &OS) const;
118 void printDispatchUnitStatistics(llvm::raw_ostream &OS) const;
119 void printSchedulerStatistics(llvm::raw_ostream &OS) const;
120
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +0000121 void printDispatchStalls(llvm::raw_ostream &OS) const;
Andrea Di Biagio12ef5262018-03-21 18:11:05 +0000122 void printRATStatistics(llvm::raw_ostream &OS) const;
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000123 void printRCUStatistics(llvm::raw_ostream &OS, const Histogram &Histogram,
124 unsigned Cycles) const;
125 void printDispatchUnitUsage(llvm::raw_ostream &OS, const Histogram &Stats,
126 unsigned Cycles) const;
127 void printIssuePerCycle(const Histogram &IssuePerCycle,
128 unsigned TotalCycles) const;
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000129 void printSchedulerUsage(llvm::raw_ostream &OS,
130 const llvm::MCSchedModel &SM) const;
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000131
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000132public:
Andrea Di Biagio12ef5262018-03-21 18:11:05 +0000133 BackendStatistics(const llvm::MCSubtargetInfo &sti)
Andrea Di Biagio94fafdf2018-03-24 16:05:36 +0000134 : STI(sti), NumDispatched(0), NumIssued(0), NumRetired(0), NumCycles(0),
Andrea Di Biagio9da4d6d2018-04-03 13:36:24 +0000135 HWStalls(HWStallEvent::LastGenericEvent) {
136 initializeRegisterFileInfo();
137 }
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000138
Clement Courbet844f22d2018-03-13 13:11:01 +0000139 void onInstructionEvent(const HWInstructionEvent &Event) override;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000140
141 void onCycleBegin(unsigned Cycle) override { NumCycles++; }
142
143 void onCycleEnd(unsigned Cycle) override { updateHistograms(); }
144
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +0000145 void onStallEvent(const HWStallEvent &Event) override {
146 if (Event.Type < HWStallEvent::LastGenericEvent)
147 HWStalls[Event.Type]++;
148 }
149
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000150 // Increases the number of used scheduler queue slots of every buffered
151 // resource in the Buffers set.
Andrea Di Biagio04de0b42018-03-20 20:18:36 +0000152 void onReservedBuffers(llvm::ArrayRef<unsigned> Buffers) override;
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000153
154 // Decreases by one the number of used scheduler queue slots of every
155 // buffered resource in the Buffers set.
Andrea Di Biagio04de0b42018-03-20 20:18:36 +0000156 void onReleasedBuffers(llvm::ArrayRef<unsigned> Buffers) override;
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000157
Andrea Di Biagio8af3fe82018-03-08 16:08:43 +0000158 void printView(llvm::raw_ostream &OS) const override {
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +0000159 printDispatchStalls(OS);
Andrea Di Biagio12ef5262018-03-21 18:11:05 +0000160 printRATStatistics(OS);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000161 printDispatchUnitStatistics(OS);
162 printSchedulerStatistics(OS);
163 printRetireUnitStatistics(OS);
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +0000164 printSchedulerUsage(OS, STI.getSchedModel());
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000165 }
166};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000167} // namespace mca
168
169#endif