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