xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-mca/Views/TimelineView.h (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===--------------------- TimelineView.h -----------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric /// \brief
9*0b57cec5SDimitry Andric ///
10*0b57cec5SDimitry Andric /// This file implements a timeline view for the llvm-mca tool.
11*0b57cec5SDimitry Andric ///
12*0b57cec5SDimitry Andric /// Class TimelineView observes events generated by the pipeline. For every
13*0b57cec5SDimitry Andric /// instruction executed by the pipeline, it stores information related to
14*0b57cec5SDimitry Andric /// state transition. It then plots that information in the form of a table
15*0b57cec5SDimitry Andric /// as reported by the example below:
16*0b57cec5SDimitry Andric ///
17*0b57cec5SDimitry Andric /// Timeline view:
18*0b57cec5SDimitry Andric ///     	          0123456
19*0b57cec5SDimitry Andric /// Index	0123456789
20*0b57cec5SDimitry Andric ///
21*0b57cec5SDimitry Andric /// [0,0]	DeER .    .    ..	vmovshdup  %xmm0, %xmm1
22*0b57cec5SDimitry Andric /// [0,1]	DeER .    .    ..	vpermilpd  $1, %xmm0, %xmm2
23*0b57cec5SDimitry Andric /// [0,2]	.DeER.    .    ..	vpermilps  $231, %xmm0, %xmm5
24*0b57cec5SDimitry Andric /// [0,3]	.DeeeER   .    ..	vaddss  %xmm1, %xmm0, %xmm3
25*0b57cec5SDimitry Andric /// [0,4]	. D==eeeER.    ..	vaddss  %xmm3, %xmm2, %xmm4
26*0b57cec5SDimitry Andric /// [0,5]	. D=====eeeER  ..	vaddss  %xmm4, %xmm5, %xmm6
27*0b57cec5SDimitry Andric ///
28*0b57cec5SDimitry Andric /// [1,0]	.  DeE------R  ..	vmovshdup  %xmm0, %xmm1
29*0b57cec5SDimitry Andric /// [1,1]	.  DeE------R  ..	vpermilpd  $1, %xmm0, %xmm2
30*0b57cec5SDimitry Andric /// [1,2]	.   DeE-----R  ..	vpermilps  $231, %xmm0, %xmm5
31*0b57cec5SDimitry Andric /// [1,3]	.   D=eeeE--R  ..	vaddss  %xmm1, %xmm0, %xmm3
32*0b57cec5SDimitry Andric /// [1,4]	.    D===eeeER ..	vaddss  %xmm3, %xmm2, %xmm4
33*0b57cec5SDimitry Andric /// [1,5]	.    D======eeeER	vaddss  %xmm4, %xmm5, %xmm6
34*0b57cec5SDimitry Andric ///
35*0b57cec5SDimitry Andric /// There is an entry for every instruction in the input assembly sequence.
36*0b57cec5SDimitry Andric /// The first field is a pair of numbers obtained from the instruction index.
37*0b57cec5SDimitry Andric /// The first element of the pair is the iteration index, while the second
38*0b57cec5SDimitry Andric /// element of the pair is a sequence number (i.e. a position in the assembly
39*0b57cec5SDimitry Andric /// sequence).
40*0b57cec5SDimitry Andric /// The second field of the table is the actual timeline information; each
41*0b57cec5SDimitry Andric /// column is the information related to a specific cycle of execution.
42*0b57cec5SDimitry Andric /// The timeline of an instruction is described by a sequence of character
43*0b57cec5SDimitry Andric /// where each character represents the instruction state at a specific cycle.
44*0b57cec5SDimitry Andric ///
45*0b57cec5SDimitry Andric /// Possible instruction states are:
46*0b57cec5SDimitry Andric ///  D: Instruction Dispatched
47*0b57cec5SDimitry Andric ///  e: Instruction Executing
48*0b57cec5SDimitry Andric ///  E: Instruction Executed (write-back stage)
49*0b57cec5SDimitry Andric ///  R: Instruction retired
50*0b57cec5SDimitry Andric ///  =: Instruction waiting in the Scheduler's queue
51*0b57cec5SDimitry Andric ///  -: Instruction executed, waiting to retire in order.
52*0b57cec5SDimitry Andric ///
53*0b57cec5SDimitry Andric /// dots ('.') and empty spaces are cycles where the instruction is not
54*0b57cec5SDimitry Andric /// in-flight.
55*0b57cec5SDimitry Andric ///
56*0b57cec5SDimitry Andric /// The last column is the assembly instruction associated to the entry.
57*0b57cec5SDimitry Andric ///
58*0b57cec5SDimitry Andric /// Based on the timeline view information from the example, instruction 0
59*0b57cec5SDimitry Andric /// at iteration 0 was dispatched at cycle 0, and was retired at cycle 3.
60*0b57cec5SDimitry Andric /// Instruction [0,1] was also dispatched at cycle 0, and it retired at
61*0b57cec5SDimitry Andric /// the same cycle than instruction [0,0].
62*0b57cec5SDimitry Andric /// Instruction [0,4] has been dispatched at cycle 2. However, it had to
63*0b57cec5SDimitry Andric /// wait for two cycles before being issued. That is because operands
64*0b57cec5SDimitry Andric /// became ready only at cycle 5.
65*0b57cec5SDimitry Andric ///
66*0b57cec5SDimitry Andric /// This view helps further understanding bottlenecks and the impact of
67*0b57cec5SDimitry Andric /// resource pressure on the code.
68*0b57cec5SDimitry Andric ///
69*0b57cec5SDimitry Andric /// To better understand why instructions had to wait for multiple cycles in
70*0b57cec5SDimitry Andric /// the scheduler's queue, class TimelineView also reports extra timing info
71*0b57cec5SDimitry Andric /// in another table named "Average Wait times" (see example below).
72*0b57cec5SDimitry Andric ///
73*0b57cec5SDimitry Andric ///
74*0b57cec5SDimitry Andric /// Average Wait times (based on the timeline view):
75*0b57cec5SDimitry Andric /// [0]: Executions
76*0b57cec5SDimitry Andric /// [1]: Average time spent waiting in a scheduler's queue
77*0b57cec5SDimitry Andric /// [2]: Average time spent waiting in a scheduler's queue while ready
78*0b57cec5SDimitry Andric /// [3]: Average time elapsed from WB until retire stage
79*0b57cec5SDimitry Andric ///
80*0b57cec5SDimitry Andric ///	[0]	[1]	[2]	[3]
81*0b57cec5SDimitry Andric /// 0.	 2	1.0	1.0	3.0	vmovshdup  %xmm0, %xmm1
82*0b57cec5SDimitry Andric /// 1.	 2	1.0	1.0	3.0	vpermilpd  $1, %xmm0, %xmm2
83*0b57cec5SDimitry Andric /// 2.	 2	1.0	1.0	2.5	vpermilps  $231, %xmm0, %xmm5
84*0b57cec5SDimitry Andric /// 3.	 2	1.5	0.5	1.0	vaddss  %xmm1, %xmm0, %xmm3
85*0b57cec5SDimitry Andric /// 4.	 2	3.5	0.0	0.0	vaddss  %xmm3, %xmm2, %xmm4
86*0b57cec5SDimitry Andric /// 5.	 2	6.5	0.0	0.0	vaddss  %xmm4, %xmm5, %xmm6
87*0b57cec5SDimitry Andric ///
88*0b57cec5SDimitry Andric /// By comparing column [2] with column [1], we get an idea about how many
89*0b57cec5SDimitry Andric /// cycles were spent in the scheduler's queue due to data dependencies.
90*0b57cec5SDimitry Andric ///
91*0b57cec5SDimitry Andric /// In this example, instruction 5 spent an average of ~6 cycles in the
92*0b57cec5SDimitry Andric /// scheduler's queue. As soon as operands became ready, the instruction
93*0b57cec5SDimitry Andric /// was immediately issued to the pipeline(s).
94*0b57cec5SDimitry Andric /// That is expected because instruction 5 cannot transition to the "ready"
95*0b57cec5SDimitry Andric /// state until %xmm4 is written by instruction 4.
96*0b57cec5SDimitry Andric ///
97*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
98*0b57cec5SDimitry Andric 
99*0b57cec5SDimitry Andric #ifndef LLVM_TOOLS_LLVM_MCA_TIMELINEVIEW_H
100*0b57cec5SDimitry Andric #define LLVM_TOOLS_LLVM_MCA_TIMELINEVIEW_H
101*0b57cec5SDimitry Andric 
102*0b57cec5SDimitry Andric #include "Views/View.h"
103*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
104*0b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
105*0b57cec5SDimitry Andric #include "llvm/MC/MCInstPrinter.h"
106*0b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
107*0b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h"
108*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
109*0b57cec5SDimitry Andric 
110*0b57cec5SDimitry Andric namespace llvm {
111*0b57cec5SDimitry Andric namespace mca {
112*0b57cec5SDimitry Andric 
113*0b57cec5SDimitry Andric /// This class listens to instruction state transition events
114*0b57cec5SDimitry Andric /// in order to construct a timeline information.
115*0b57cec5SDimitry Andric ///
116*0b57cec5SDimitry Andric /// For every instruction executed by the Pipeline, this class constructs
117*0b57cec5SDimitry Andric /// a TimelineViewEntry object. TimelineViewEntry objects are then used
118*0b57cec5SDimitry Andric /// to print the timeline information, as well as the "average wait times"
119*0b57cec5SDimitry Andric /// for every instruction in the input assembly sequence.
120*0b57cec5SDimitry Andric class TimelineView : public View {
121*0b57cec5SDimitry Andric   const llvm::MCSubtargetInfo &STI;
122*0b57cec5SDimitry Andric   llvm::MCInstPrinter &MCIP;
123*0b57cec5SDimitry Andric   llvm::ArrayRef<llvm::MCInst> Source;
124*0b57cec5SDimitry Andric 
125*0b57cec5SDimitry Andric   unsigned CurrentCycle;
126*0b57cec5SDimitry Andric   unsigned MaxCycle;
127*0b57cec5SDimitry Andric   unsigned LastCycle;
128*0b57cec5SDimitry Andric 
129*0b57cec5SDimitry Andric   struct TimelineViewEntry {
130*0b57cec5SDimitry Andric     int CycleDispatched;  // A negative value is an "invalid cycle".
131*0b57cec5SDimitry Andric     unsigned CycleReady;
132*0b57cec5SDimitry Andric     unsigned CycleIssued;
133*0b57cec5SDimitry Andric     unsigned CycleExecuted;
134*0b57cec5SDimitry Andric     unsigned CycleRetired;
135*0b57cec5SDimitry Andric   };
136*0b57cec5SDimitry Andric   std::vector<TimelineViewEntry> Timeline;
137*0b57cec5SDimitry Andric 
138*0b57cec5SDimitry Andric   struct WaitTimeEntry {
139*0b57cec5SDimitry Andric     unsigned CyclesSpentInSchedulerQueue;
140*0b57cec5SDimitry Andric     unsigned CyclesSpentInSQWhileReady;
141*0b57cec5SDimitry Andric     unsigned CyclesSpentAfterWBAndBeforeRetire;
142*0b57cec5SDimitry Andric   };
143*0b57cec5SDimitry Andric   std::vector<WaitTimeEntry> WaitTime;
144*0b57cec5SDimitry Andric 
145*0b57cec5SDimitry Andric   // This field is used to map instructions to buffered resources.
146*0b57cec5SDimitry Andric   // Elements of this vector are <resourceID, BufferSizer> pairs.
147*0b57cec5SDimitry Andric   std::vector<std::pair<unsigned, int>> UsedBuffer;
148*0b57cec5SDimitry Andric 
149*0b57cec5SDimitry Andric   void printTimelineViewEntry(llvm::formatted_raw_ostream &OS,
150*0b57cec5SDimitry Andric                               const TimelineViewEntry &E, unsigned Iteration,
151*0b57cec5SDimitry Andric                               unsigned SourceIndex) const;
152*0b57cec5SDimitry Andric   void printWaitTimeEntry(llvm::formatted_raw_ostream &OS,
153*0b57cec5SDimitry Andric                           const WaitTimeEntry &E, unsigned Index,
154*0b57cec5SDimitry Andric                           unsigned Executions) const;
155*0b57cec5SDimitry Andric 
156*0b57cec5SDimitry Andric   // Display characters for the TimelineView report output.
157*0b57cec5SDimitry Andric   struct DisplayChar {
158*0b57cec5SDimitry Andric     static const char Dispatched = 'D';
159*0b57cec5SDimitry Andric     static const char Executed = 'E';
160*0b57cec5SDimitry Andric     static const char Retired = 'R';
161*0b57cec5SDimitry Andric     static const char Waiting = '='; // Instruction is waiting in the scheduler.
162*0b57cec5SDimitry Andric     static const char Executing = 'e';
163*0b57cec5SDimitry Andric     static const char RetireLag = '-'; // The instruction is waiting to retire.
164*0b57cec5SDimitry Andric   };
165*0b57cec5SDimitry Andric 
166*0b57cec5SDimitry Andric public:
167*0b57cec5SDimitry Andric   TimelineView(const llvm::MCSubtargetInfo &sti, llvm::MCInstPrinter &Printer,
168*0b57cec5SDimitry Andric                llvm::ArrayRef<llvm::MCInst> S, unsigned Iterations,
169*0b57cec5SDimitry Andric                unsigned Cycles);
170*0b57cec5SDimitry Andric 
171*0b57cec5SDimitry Andric   // Event handlers.
172*0b57cec5SDimitry Andric   void onCycleEnd() override { ++CurrentCycle; }
173*0b57cec5SDimitry Andric   void onEvent(const HWInstructionEvent &Event) override;
174*0b57cec5SDimitry Andric   void onReservedBuffers(const InstRef &IR,
175*0b57cec5SDimitry Andric                          llvm::ArrayRef<unsigned> Buffers) override;
176*0b57cec5SDimitry Andric 
177*0b57cec5SDimitry Andric   // print functionalities.
178*0b57cec5SDimitry Andric   void printTimeline(llvm::raw_ostream &OS) const;
179*0b57cec5SDimitry Andric   void printAverageWaitTimes(llvm::raw_ostream &OS) const;
180*0b57cec5SDimitry Andric   void printView(llvm::raw_ostream &OS) const override {
181*0b57cec5SDimitry Andric     printTimeline(OS);
182*0b57cec5SDimitry Andric     printAverageWaitTimes(OS);
183*0b57cec5SDimitry Andric   }
184*0b57cec5SDimitry Andric };
185*0b57cec5SDimitry Andric } // namespace mca
186*0b57cec5SDimitry Andric } // namespace llvm
187*0b57cec5SDimitry Andric 
188*0b57cec5SDimitry Andric #endif
189