Path: blob/main/contrib/llvm-project/llvm/tools/llvm-mca/Views/TimelineView.h
35290 views
//===--------------------- TimelineView.h -----------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7/// \brief8///9/// This file implements a timeline view for the llvm-mca tool.10///11/// Class TimelineView observes events generated by the pipeline. For every12/// instruction executed by the pipeline, it stores information related to13/// state transition. It then plots that information in the form of a table14/// as reported by the example below:15///16/// Timeline view:17/// 012345618/// Index 012345678919///20/// [0,0] DeER . . .. vmovshdup %xmm0, %xmm121/// [0,1] DeER . . .. vpermilpd $1, %xmm0, %xmm222/// [0,2] .DeER. . .. vpermilps $231, %xmm0, %xmm523/// [0,3] .DeeeER . .. vaddss %xmm1, %xmm0, %xmm324/// [0,4] . D==eeeER. .. vaddss %xmm3, %xmm2, %xmm425/// [0,5] . D=====eeeER .. vaddss %xmm4, %xmm5, %xmm626///27/// [1,0] . DeE------R .. vmovshdup %xmm0, %xmm128/// [1,1] . DeE------R .. vpermilpd $1, %xmm0, %xmm229/// [1,2] . DeE-----R .. vpermilps $231, %xmm0, %xmm530/// [1,3] . D=eeeE--R .. vaddss %xmm1, %xmm0, %xmm331/// [1,4] . D===eeeER .. vaddss %xmm3, %xmm2, %xmm432/// [1,5] . D======eeeER vaddss %xmm4, %xmm5, %xmm633///34/// There is an entry for every instruction in the input assembly sequence.35/// The first field is a pair of numbers obtained from the instruction index.36/// The first element of the pair is the iteration index, while the second37/// element of the pair is a sequence number (i.e. a position in the assembly38/// sequence).39/// The second field of the table is the actual timeline information; each40/// column is the information related to a specific cycle of execution.41/// The timeline of an instruction is described by a sequence of character42/// where each character represents the instruction state at a specific cycle.43///44/// Possible instruction states are:45/// D: Instruction Dispatched46/// e: Instruction Executing47/// E: Instruction Executed (write-back stage)48/// R: Instruction retired49/// =: Instruction waiting in the Scheduler's queue50/// -: Instruction executed, waiting to retire in order.51///52/// dots ('.') and empty spaces are cycles where the instruction is not53/// in-flight.54///55/// The last column is the assembly instruction associated to the entry.56///57/// Based on the timeline view information from the example, instruction 058/// at iteration 0 was dispatched at cycle 0, and was retired at cycle 3.59/// Instruction [0,1] was also dispatched at cycle 0, and it retired at60/// the same cycle than instruction [0,0].61/// Instruction [0,4] has been dispatched at cycle 2. However, it had to62/// wait for two cycles before being issued. That is because operands63/// became ready only at cycle 5.64///65/// This view helps further understanding bottlenecks and the impact of66/// resource pressure on the code.67///68/// To better understand why instructions had to wait for multiple cycles in69/// the scheduler's queue, class TimelineView also reports extra timing info70/// in another table named "Average Wait times" (see example below).71///72///73/// Average Wait times (based on the timeline view):74/// [0]: Executions75/// [1]: Average time spent waiting in a scheduler's queue76/// [2]: Average time spent waiting in a scheduler's queue while ready77/// [3]: Average time elapsed from WB until retire stage78///79/// [0] [1] [2] [3]80/// 0. 2 1.0 1.0 3.0 vmovshdup %xmm0, %xmm181/// 1. 2 1.0 1.0 3.0 vpermilpd $1, %xmm0, %xmm282/// 2. 2 1.0 1.0 2.5 vpermilps $231, %xmm0, %xmm583/// 3. 2 1.5 0.5 1.0 vaddss %xmm1, %xmm0, %xmm384/// 4. 2 3.5 0.0 0.0 vaddss %xmm3, %xmm2, %xmm485/// 5. 2 6.5 0.0 0.0 vaddss %xmm4, %xmm5, %xmm686/// 2 2.4 0.6 1.6 <total>87///88/// By comparing column [2] with column [1], we get an idea about how many89/// cycles were spent in the scheduler's queue due to data dependencies.90///91/// In this example, instruction 5 spent an average of ~6 cycles in the92/// scheduler's queue. As soon as operands became ready, the instruction93/// was immediately issued to the pipeline(s).94/// That is expected because instruction 5 cannot transition to the "ready"95/// state until %xmm4 is written by instruction 4.96///97//===----------------------------------------------------------------------===//9899#ifndef LLVM_TOOLS_LLVM_MCA_TIMELINEVIEW_H100#define LLVM_TOOLS_LLVM_MCA_TIMELINEVIEW_H101102#include "Views/InstructionView.h"103#include "llvm/ADT/ArrayRef.h"104#include "llvm/MC/MCInst.h"105#include "llvm/MC/MCInstPrinter.h"106#include "llvm/MC/MCSubtargetInfo.h"107#include "llvm/Support/FormattedStream.h"108#include "llvm/Support/JSON.h"109#include "llvm/Support/raw_ostream.h"110111namespace llvm {112namespace mca {113114/// This class listens to instruction state transition events115/// in order to construct a timeline information.116///117/// For every instruction executed by the Pipeline, this class constructs118/// a TimelineViewEntry object. TimelineViewEntry objects are then used119/// to print the timeline information, as well as the "average wait times"120/// for every instruction in the input assembly sequence.121class TimelineView : public InstructionView {122unsigned CurrentCycle;123unsigned MaxCycle;124unsigned LastCycle;125126struct TimelineViewEntry {127int CycleDispatched; // A negative value is an "invalid cycle".128unsigned CycleReady;129unsigned CycleIssued;130unsigned CycleExecuted;131unsigned CycleRetired;132};133std::vector<TimelineViewEntry> Timeline;134135struct WaitTimeEntry {136unsigned CyclesSpentInSchedulerQueue;137unsigned CyclesSpentInSQWhileReady;138unsigned CyclesSpentAfterWBAndBeforeRetire;139};140std::vector<WaitTimeEntry> WaitTime;141142// This field is used to map instructions to buffered resources.143// Elements of this vector are <resourceID, BufferSizer> pairs.144std::vector<std::pair<unsigned, int>> UsedBuffer;145146void printTimelineViewEntry(llvm::formatted_raw_ostream &OS,147const TimelineViewEntry &E, unsigned Iteration,148unsigned SourceIndex) const;149void printWaitTimeEntry(llvm::formatted_raw_ostream &OS,150const WaitTimeEntry &E, unsigned Index,151unsigned Executions) const;152153// Display characters for the TimelineView report output.154struct DisplayChar {155static const char Dispatched = 'D';156static const char Executed = 'E';157static const char Retired = 'R';158static const char Waiting = '='; // Instruction is waiting in the scheduler.159static const char Executing = 'e';160static const char RetireLag = '-'; // The instruction is waiting to retire.161};162163public:164TimelineView(const llvm::MCSubtargetInfo &sti, llvm::MCInstPrinter &Printer,165llvm::ArrayRef<llvm::MCInst> S, unsigned Iterations,166unsigned Cycles);167168// Event handlers.169void onCycleEnd() override { ++CurrentCycle; }170void onEvent(const HWInstructionEvent &Event) override;171void onReservedBuffers(const InstRef &IR,172llvm::ArrayRef<unsigned> Buffers) override;173174// print functionalities.175void printTimeline(llvm::raw_ostream &OS) const;176void printAverageWaitTimes(llvm::raw_ostream &OS) const;177void printView(llvm::raw_ostream &OS) const override {178printTimeline(OS);179printAverageWaitTimes(OS);180}181StringRef getNameAsString() const override { return "TimelineView"; }182json::Value toJSON() const override;183};184} // namespace mca185} // namespace llvm186187#endif188189190