xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-mca/Views/TimelineView.h (revision fe6060f10f634930ff71b7c50291ddc610da2475)
10b57cec5SDimitry Andric //===--------------------- TimelineView.h -----------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric /// \brief
90b57cec5SDimitry Andric ///
100b57cec5SDimitry Andric /// This file implements a timeline view for the llvm-mca tool.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric /// Class TimelineView observes events generated by the pipeline. For every
130b57cec5SDimitry Andric /// instruction executed by the pipeline, it stores information related to
140b57cec5SDimitry Andric /// state transition. It then plots that information in the form of a table
150b57cec5SDimitry Andric /// as reported by the example below:
160b57cec5SDimitry Andric ///
170b57cec5SDimitry Andric /// Timeline view:
180b57cec5SDimitry Andric ///     	          0123456
190b57cec5SDimitry Andric /// Index	0123456789
200b57cec5SDimitry Andric ///
210b57cec5SDimitry Andric /// [0,0]	DeER .    .    ..	vmovshdup  %xmm0, %xmm1
220b57cec5SDimitry Andric /// [0,1]	DeER .    .    ..	vpermilpd  $1, %xmm0, %xmm2
230b57cec5SDimitry Andric /// [0,2]	.DeER.    .    ..	vpermilps  $231, %xmm0, %xmm5
240b57cec5SDimitry Andric /// [0,3]	.DeeeER   .    ..	vaddss  %xmm1, %xmm0, %xmm3
250b57cec5SDimitry Andric /// [0,4]	. D==eeeER.    ..	vaddss  %xmm3, %xmm2, %xmm4
260b57cec5SDimitry Andric /// [0,5]	. D=====eeeER  ..	vaddss  %xmm4, %xmm5, %xmm6
270b57cec5SDimitry Andric ///
280b57cec5SDimitry Andric /// [1,0]	.  DeE------R  ..	vmovshdup  %xmm0, %xmm1
290b57cec5SDimitry Andric /// [1,1]	.  DeE------R  ..	vpermilpd  $1, %xmm0, %xmm2
300b57cec5SDimitry Andric /// [1,2]	.   DeE-----R  ..	vpermilps  $231, %xmm0, %xmm5
310b57cec5SDimitry Andric /// [1,3]	.   D=eeeE--R  ..	vaddss  %xmm1, %xmm0, %xmm3
320b57cec5SDimitry Andric /// [1,4]	.    D===eeeER ..	vaddss  %xmm3, %xmm2, %xmm4
330b57cec5SDimitry Andric /// [1,5]	.    D======eeeER	vaddss  %xmm4, %xmm5, %xmm6
340b57cec5SDimitry Andric ///
350b57cec5SDimitry Andric /// There is an entry for every instruction in the input assembly sequence.
360b57cec5SDimitry Andric /// The first field is a pair of numbers obtained from the instruction index.
370b57cec5SDimitry Andric /// The first element of the pair is the iteration index, while the second
380b57cec5SDimitry Andric /// element of the pair is a sequence number (i.e. a position in the assembly
390b57cec5SDimitry Andric /// sequence).
400b57cec5SDimitry Andric /// The second field of the table is the actual timeline information; each
410b57cec5SDimitry Andric /// column is the information related to a specific cycle of execution.
420b57cec5SDimitry Andric /// The timeline of an instruction is described by a sequence of character
430b57cec5SDimitry Andric /// where each character represents the instruction state at a specific cycle.
440b57cec5SDimitry Andric ///
450b57cec5SDimitry Andric /// Possible instruction states are:
460b57cec5SDimitry Andric ///  D: Instruction Dispatched
470b57cec5SDimitry Andric ///  e: Instruction Executing
480b57cec5SDimitry Andric ///  E: Instruction Executed (write-back stage)
490b57cec5SDimitry Andric ///  R: Instruction retired
500b57cec5SDimitry Andric ///  =: Instruction waiting in the Scheduler's queue
510b57cec5SDimitry Andric ///  -: Instruction executed, waiting to retire in order.
520b57cec5SDimitry Andric ///
530b57cec5SDimitry Andric /// dots ('.') and empty spaces are cycles where the instruction is not
540b57cec5SDimitry Andric /// in-flight.
550b57cec5SDimitry Andric ///
560b57cec5SDimitry Andric /// The last column is the assembly instruction associated to the entry.
570b57cec5SDimitry Andric ///
580b57cec5SDimitry Andric /// Based on the timeline view information from the example, instruction 0
590b57cec5SDimitry Andric /// at iteration 0 was dispatched at cycle 0, and was retired at cycle 3.
600b57cec5SDimitry Andric /// Instruction [0,1] was also dispatched at cycle 0, and it retired at
610b57cec5SDimitry Andric /// the same cycle than instruction [0,0].
620b57cec5SDimitry Andric /// Instruction [0,4] has been dispatched at cycle 2. However, it had to
630b57cec5SDimitry Andric /// wait for two cycles before being issued. That is because operands
640b57cec5SDimitry Andric /// became ready only at cycle 5.
650b57cec5SDimitry Andric ///
660b57cec5SDimitry Andric /// This view helps further understanding bottlenecks and the impact of
670b57cec5SDimitry Andric /// resource pressure on the code.
680b57cec5SDimitry Andric ///
690b57cec5SDimitry Andric /// To better understand why instructions had to wait for multiple cycles in
700b57cec5SDimitry Andric /// the scheduler's queue, class TimelineView also reports extra timing info
710b57cec5SDimitry Andric /// in another table named "Average Wait times" (see example below).
720b57cec5SDimitry Andric ///
730b57cec5SDimitry Andric ///
740b57cec5SDimitry Andric /// Average Wait times (based on the timeline view):
750b57cec5SDimitry Andric /// [0]: Executions
760b57cec5SDimitry Andric /// [1]: Average time spent waiting in a scheduler's queue
770b57cec5SDimitry Andric /// [2]: Average time spent waiting in a scheduler's queue while ready
780b57cec5SDimitry Andric /// [3]: Average time elapsed from WB until retire stage
790b57cec5SDimitry Andric ///
800b57cec5SDimitry Andric ///	[0]	[1]	[2]	[3]
810b57cec5SDimitry Andric /// 0.	 2	1.0	1.0	3.0	vmovshdup  %xmm0, %xmm1
820b57cec5SDimitry Andric /// 1.	 2	1.0	1.0	3.0	vpermilpd  $1, %xmm0, %xmm2
830b57cec5SDimitry Andric /// 2.	 2	1.0	1.0	2.5	vpermilps  $231, %xmm0, %xmm5
840b57cec5SDimitry Andric /// 3.	 2	1.5	0.5	1.0	vaddss  %xmm1, %xmm0, %xmm3
850b57cec5SDimitry Andric /// 4.	 2	3.5	0.0	0.0	vaddss  %xmm3, %xmm2, %xmm4
860b57cec5SDimitry Andric /// 5.	 2	6.5	0.0	0.0	vaddss  %xmm4, %xmm5, %xmm6
878bcb0991SDimitry Andric ///      2	2.4	0.6	1.6     <total>
880b57cec5SDimitry Andric ///
890b57cec5SDimitry Andric /// By comparing column [2] with column [1], we get an idea about how many
900b57cec5SDimitry Andric /// cycles were spent in the scheduler's queue due to data dependencies.
910b57cec5SDimitry Andric ///
920b57cec5SDimitry Andric /// In this example, instruction 5 spent an average of ~6 cycles in the
930b57cec5SDimitry Andric /// scheduler's queue. As soon as operands became ready, the instruction
940b57cec5SDimitry Andric /// was immediately issued to the pipeline(s).
950b57cec5SDimitry Andric /// That is expected because instruction 5 cannot transition to the "ready"
960b57cec5SDimitry Andric /// state until %xmm4 is written by instruction 4.
970b57cec5SDimitry Andric ///
980b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric #ifndef LLVM_TOOLS_LLVM_MCA_TIMELINEVIEW_H
1010b57cec5SDimitry Andric #define LLVM_TOOLS_LLVM_MCA_TIMELINEVIEW_H
1020b57cec5SDimitry Andric 
103*e8d8bef9SDimitry Andric #include "Views/InstructionView.h"
1040b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
1050b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
1060b57cec5SDimitry Andric #include "llvm/MC/MCInstPrinter.h"
1070b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
1080b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h"
109*e8d8bef9SDimitry Andric #include "llvm/Support/JSON.h"
1100b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric namespace llvm {
1130b57cec5SDimitry Andric namespace mca {
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric /// This class listens to instruction state transition events
1160b57cec5SDimitry Andric /// in order to construct a timeline information.
1170b57cec5SDimitry Andric ///
1180b57cec5SDimitry Andric /// For every instruction executed by the Pipeline, this class constructs
1190b57cec5SDimitry Andric /// a TimelineViewEntry object. TimelineViewEntry objects are then used
1200b57cec5SDimitry Andric /// to print the timeline information, as well as the "average wait times"
1210b57cec5SDimitry Andric /// for every instruction in the input assembly sequence.
122*e8d8bef9SDimitry Andric class TimelineView : public InstructionView {
1230b57cec5SDimitry Andric   unsigned CurrentCycle;
1240b57cec5SDimitry Andric   unsigned MaxCycle;
1250b57cec5SDimitry Andric   unsigned LastCycle;
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   struct TimelineViewEntry {
1280b57cec5SDimitry Andric     int CycleDispatched; // A negative value is an "invalid cycle".
1290b57cec5SDimitry Andric     unsigned CycleReady;
1300b57cec5SDimitry Andric     unsigned CycleIssued;
1310b57cec5SDimitry Andric     unsigned CycleExecuted;
1320b57cec5SDimitry Andric     unsigned CycleRetired;
1330b57cec5SDimitry Andric   };
1340b57cec5SDimitry Andric   std::vector<TimelineViewEntry> Timeline;
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   struct WaitTimeEntry {
1370b57cec5SDimitry Andric     unsigned CyclesSpentInSchedulerQueue;
1380b57cec5SDimitry Andric     unsigned CyclesSpentInSQWhileReady;
1390b57cec5SDimitry Andric     unsigned CyclesSpentAfterWBAndBeforeRetire;
1400b57cec5SDimitry Andric   };
1410b57cec5SDimitry Andric   std::vector<WaitTimeEntry> WaitTime;
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   // This field is used to map instructions to buffered resources.
1440b57cec5SDimitry Andric   // Elements of this vector are <resourceID, BufferSizer> pairs.
1450b57cec5SDimitry Andric   std::vector<std::pair<unsigned, int>> UsedBuffer;
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   void printTimelineViewEntry(llvm::formatted_raw_ostream &OS,
1480b57cec5SDimitry Andric                               const TimelineViewEntry &E, unsigned Iteration,
1490b57cec5SDimitry Andric                               unsigned SourceIndex) const;
1500b57cec5SDimitry Andric   void printWaitTimeEntry(llvm::formatted_raw_ostream &OS,
1510b57cec5SDimitry Andric                           const WaitTimeEntry &E, unsigned Index,
1520b57cec5SDimitry Andric                           unsigned Executions) const;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   // Display characters for the TimelineView report output.
1550b57cec5SDimitry Andric   struct DisplayChar {
1560b57cec5SDimitry Andric     static const char Dispatched = 'D';
1570b57cec5SDimitry Andric     static const char Executed = 'E';
1580b57cec5SDimitry Andric     static const char Retired = 'R';
1590b57cec5SDimitry Andric     static const char Waiting = '='; // Instruction is waiting in the scheduler.
1600b57cec5SDimitry Andric     static const char Executing = 'e';
1610b57cec5SDimitry Andric     static const char RetireLag = '-'; // The instruction is waiting to retire.
1620b57cec5SDimitry Andric   };
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric public:
1650b57cec5SDimitry Andric   TimelineView(const llvm::MCSubtargetInfo &sti, llvm::MCInstPrinter &Printer,
1660b57cec5SDimitry Andric                llvm::ArrayRef<llvm::MCInst> S, unsigned Iterations,
1670b57cec5SDimitry Andric                unsigned Cycles);
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   // Event handlers.
onCycleEnd()1700b57cec5SDimitry Andric   void onCycleEnd() override { ++CurrentCycle; }
1710b57cec5SDimitry Andric   void onEvent(const HWInstructionEvent &Event) override;
1720b57cec5SDimitry Andric   void onReservedBuffers(const InstRef &IR,
1730b57cec5SDimitry Andric                          llvm::ArrayRef<unsigned> Buffers) override;
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   // print functionalities.
1760b57cec5SDimitry Andric   void printTimeline(llvm::raw_ostream &OS) const;
1770b57cec5SDimitry Andric   void printAverageWaitTimes(llvm::raw_ostream &OS) const;
printView(llvm::raw_ostream & OS)1780b57cec5SDimitry Andric   void printView(llvm::raw_ostream &OS) const override {
1790b57cec5SDimitry Andric     printTimeline(OS);
1800b57cec5SDimitry Andric     printAverageWaitTimes(OS);
1810b57cec5SDimitry Andric   }
getNameAsString()182*e8d8bef9SDimitry Andric   StringRef getNameAsString() const override { return "TimelineView"; }
183*e8d8bef9SDimitry Andric   json::Value toJSON() const override;
1840b57cec5SDimitry Andric };
1850b57cec5SDimitry Andric } // namespace mca
1860b57cec5SDimitry Andric } // namespace llvm
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric #endif
189