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