1 //===--------------------- Pipeline.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 /// \file
9 ///
10 /// This file implements an ordered container of stages that simulate the
11 /// pipeline of a hardware backend.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_MCA_PIPELINE_H
16 #define LLVM_MCA_PIPELINE_H
17 
18 #include "llvm/MCA/Stages/Stage.h"
19 #include "llvm/Support/Error.h"
20 
21 namespace llvm {
22 namespace mca {
23 
24 class HWEventListener;
25 
26 /// A pipeline for a specific subtarget.
27 ///
28 /// It emulates an out-of-order execution of instructions. Instructions are
29 /// fetched from a MCInst sequence managed by an initial 'Fetch' stage.
30 /// Instructions are firstly fetched, then dispatched to the schedulers, and
31 /// then executed.
32 ///
33 /// This class tracks the lifetime of an instruction from the moment where
34 /// it gets dispatched to the schedulers, to the moment where it finishes
35 /// executing and register writes are architecturally committed.
36 /// In particular, it monitors changes in the state of every instruction
37 /// in flight.
38 ///
39 /// Instructions are executed in a loop of iterations. The number of iterations
40 /// is defined by the SourceMgr object, which is managed by the initial stage
41 /// of the instruction pipeline.
42 ///
43 /// The Pipeline entry point is method 'run()' which executes cycles in a loop
44 /// until there are new instructions to dispatch, and not every instruction
45 /// has been retired.
46 ///
47 /// Internally, the Pipeline collects statistical information in the form of
48 /// histograms. For example, it tracks how the dispatch group size changes
49 /// over time.
50 class Pipeline {
51   Pipeline(const Pipeline &P) = delete;
52   Pipeline &operator=(const Pipeline &P) = delete;
53 
54   /// An ordered list of stages that define this instruction pipeline.
55   SmallVector<std::unique_ptr<Stage>, 8> Stages;
56   std::set<HWEventListener *> Listeners;
57   unsigned Cycles;
58 
59   Error runCycle();
60   bool hasWorkToProcess();
61   void notifyCycleBegin();
62   void notifyCycleEnd();
63 
64 public:
65   Pipeline() : Cycles(0) {}
66   void appendStage(std::unique_ptr<Stage> S);
67 
68   /// Returns the total number of simulated cycles.
69   Expected<unsigned> run();
70 
71   void addEventListener(HWEventListener *Listener);
72 };
73 } // namespace mca
74 } // namespace llvm
75 
76 #endif // LLVM_MCA_PIPELINE_H
77