TimelineView.h 7.5 KB

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