| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===--------------------- Backend.h ----------------------------*- C++ -*-===// | 
|  | 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 | /// \file | 
|  | 10 | /// | 
|  | 11 | /// This file implements an OoO backend for the llvm-mca tool. | 
|  | 12 | /// | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
|  | 15 | #ifndef LLVM_TOOLS_LLVM_MCA_BACKEND_H | 
|  | 16 | #define LLVM_TOOLS_LLVM_MCA_BACKEND_H | 
|  | 17 |  | 
| Matt Davis | 679083e | 2018-05-17 19:22:29 +0000 | [diff] [blame] | 18 | #include "DispatchStage.h" | 
| Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 19 | #include "ExecuteStage.h" | 
| Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 20 | #include "FetchStage.h" | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 21 | #include "InstrBuilder.h" | 
| Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 22 | #include "RegisterFile.h" | 
|  | 23 | #include "RetireControlUnit.h" | 
|  | 24 | #include "RetireStage.h" | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 25 | #include "Scheduler.h" | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 26 |  | 
|  | 27 | namespace mca { | 
|  | 28 |  | 
| Andrea Di Biagio | 3db1fd9 | 2018-03-08 16:34:19 +0000 | [diff] [blame] | 29 | class HWEventListener; | 
| Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 30 | class HWInstructionEvent; | 
| Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 31 | class HWStallEvent; | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 32 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 33 | /// An out of order backend for a specific subtarget. | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 34 | /// | 
|  | 35 | /// It emulates an out-of-order execution of instructions. Instructions are | 
| Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 36 | /// fetched from a MCInst sequence managed by an initial 'Fetch' stage. | 
|  | 37 | /// Instructions are firstly fetched, then dispatched to the schedulers, and | 
|  | 38 | /// then executed. | 
|  | 39 | /// | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 40 | /// This class tracks the lifetime of an instruction from the moment where | 
|  | 41 | /// it gets dispatched to the schedulers, to the moment where it finishes | 
|  | 42 | /// executing and register writes are architecturally committed. | 
|  | 43 | /// In particular, it monitors changes in the state of every instruction | 
|  | 44 | /// in flight. | 
| Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 45 | /// | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 46 | /// Instructions are executed in a loop of iterations. The number of iterations | 
| Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 47 | /// is defined by the SourceMgr object, which is managed by the initial stage | 
|  | 48 | /// of the instruction pipeline. | 
|  | 49 | /// | 
|  | 50 | /// The Backend entry point is method 'run()' which executes cycles in a loop | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 51 | /// until there are new instructions to dispatch, and not every instruction | 
|  | 52 | /// has been retired. | 
| Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 53 | /// | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 54 | /// Internally, the Backend collects statistical information in the form of | 
|  | 55 | /// histograms. For example, it tracks how the dispatch group size changes | 
|  | 56 | /// over time. | 
|  | 57 | class Backend { | 
| Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 58 | // The following are the simulated hardware components of the backend. | 
|  | 59 | RetireControlUnit RCU; | 
|  | 60 | RegisterFile PRF; | 
| Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 61 | Scheduler HWS; | 
| Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 62 |  | 
| Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 63 | /// TODO: Eventually this will become a list of unique Stage* that this | 
|  | 64 | /// backend pipeline executes. | 
|  | 65 | std::unique_ptr<FetchStage> Fetch; | 
| Matt Davis | 679083e | 2018-05-17 19:22:29 +0000 | [diff] [blame] | 66 | std::unique_ptr<DispatchStage> Dispatch; | 
| Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 67 | std::unique_ptr<ExecuteStage> Execute; | 
| Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 68 | std::unique_ptr<RetireStage> Retire; | 
|  | 69 |  | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 70 | std::set<HWEventListener *> Listeners; | 
| Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 71 | unsigned Cycles; | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 72 |  | 
|  | 73 | void runCycle(unsigned Cycle); | 
|  | 74 |  | 
|  | 75 | public: | 
| Andrea Di Biagio | b5088da | 2018-03-23 11:50:43 +0000 | [diff] [blame] | 76 | Backend(const llvm::MCSubtargetInfo &Subtarget, | 
| Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 77 | const llvm::MCRegisterInfo &MRI, | 
|  | 78 | std::unique_ptr<FetchStage> InitialStage, unsigned DispatchWidth = 0, | 
|  | 79 | unsigned RegisterFileSize = 0, unsigned LoadQueueSize = 0, | 
|  | 80 | unsigned StoreQueueSize = 0, bool AssumeNoAlias = false) | 
| Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 81 | : RCU(Subtarget.getSchedModel()), | 
|  | 82 | PRF(Subtarget.getSchedModel(), MRI, RegisterFileSize), | 
| Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 83 | HWS(Subtarget.getSchedModel(), LoadQueueSize, StoreQueueSize, | 
|  | 84 | AssumeNoAlias), | 
| Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 85 | Fetch(std::move(InitialStage)), | 
| Matt Davis | 679083e | 2018-05-17 19:22:29 +0000 | [diff] [blame] | 86 | Dispatch(llvm::make_unique<DispatchStage>( | 
| Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 87 | this, Subtarget, MRI, RegisterFileSize, DispatchWidth, RCU, PRF, | 
| Matt Davis | 488ac4c | 2018-06-14 01:20:18 +0000 | [diff] [blame] | 88 | HWS)), | 
|  | 89 | Execute(llvm::make_unique<ExecuteStage>(this, RCU, HWS)), | 
| Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 90 | Retire(llvm::make_unique<RetireStage>(this, RCU, PRF)), Cycles(0) {} | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 91 |  | 
| Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 92 | void run(); | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 93 | void addEventListener(HWEventListener *Listener); | 
|  | 94 | void notifyCycleBegin(unsigned Cycle); | 
| Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 95 | void notifyInstructionEvent(const HWInstructionEvent &Event); | 
| Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 96 | void notifyStallEvent(const HWStallEvent &Event); | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 97 | void notifyResourceAvailable(const ResourceRef &RR); | 
| Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 98 | void notifyReservedBuffers(llvm::ArrayRef<unsigned> Buffers); | 
|  | 99 | void notifyReleasedBuffers(llvm::ArrayRef<unsigned> Buffers); | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 100 | void notifyCycleEnd(unsigned Cycle); | 
|  | 101 | }; | 
| Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 102 | } // namespace mca | 
|  | 103 |  | 
|  | 104 | #endif |