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 | |
| 18 | #include "Dispatch.h" |
| 19 | #include "InstrBuilder.h" |
| 20 | #include "Scheduler.h" |
| 21 | #include "SourceMgr.h" |
| 22 | |
| 23 | namespace mca { |
| 24 | |
Andrea Di Biagio | 3db1fd9 | 2018-03-08 16:34:19 +0000 | [diff] [blame] | 25 | class HWEventListener; |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 26 | class HWInstructionEvent; |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 27 | class HWStallEvent; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 28 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 29 | /// An out of order backend for a specific subtarget. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 30 | /// |
| 31 | /// It emulates an out-of-order execution of instructions. Instructions are |
| 32 | /// fetched from a MCInst sequence managed by an object of class SourceMgr. |
| 33 | /// Instructions are firstly dispatched to the schedulers and then executed. |
| 34 | /// This class tracks the lifetime of an instruction from the moment where |
| 35 | /// it gets dispatched to the schedulers, to the moment where it finishes |
| 36 | /// executing and register writes are architecturally committed. |
| 37 | /// In particular, it monitors changes in the state of every instruction |
| 38 | /// in flight. |
| 39 | /// Instructions are executed in a loop of iterations. The number of iterations |
| 40 | /// is defined by the SourceMgr object. |
| 41 | /// The Backend entrypoint is method 'Run()' which execute cycles in a loop |
| 42 | /// until there are new instructions to dispatch, and not every instruction |
| 43 | /// has been retired. |
| 44 | /// Internally, the Backend collects statistical information in the form of |
| 45 | /// histograms. For example, it tracks how the dispatch group size changes |
| 46 | /// over time. |
| 47 | class Backend { |
| 48 | const llvm::MCSubtargetInfo &STI; |
| 49 | |
Andrea Di Biagio | b5088da | 2018-03-23 11:50:43 +0000 | [diff] [blame] | 50 | InstrBuilder &IB; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 51 | std::unique_ptr<Scheduler> HWS; |
| 52 | std::unique_ptr<DispatchUnit> DU; |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 53 | SourceMgr &SM; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 54 | unsigned Cycles; |
| 55 | |
| 56 | llvm::DenseMap<unsigned, std::unique_ptr<Instruction>> Instructions; |
| 57 | std::set<HWEventListener *> Listeners; |
| 58 | |
| 59 | void runCycle(unsigned Cycle); |
| 60 | |
| 61 | public: |
Andrea Di Biagio | b5088da | 2018-03-23 11:50:43 +0000 | [diff] [blame] | 62 | Backend(const llvm::MCSubtargetInfo &Subtarget, |
| 63 | const llvm::MCRegisterInfo &MRI, InstrBuilder &B, SourceMgr &Source, |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 64 | unsigned DispatchWidth = 0, unsigned RegisterFileSize = 0, |
Andrea Di Biagio | 020ba25 | 2018-04-05 11:36:50 +0000 | [diff] [blame] | 65 | unsigned LoadQueueSize = 0, unsigned StoreQueueSize = 0, |
| 66 | bool AssumeNoAlias = false) |
Andrea Di Biagio | b5088da | 2018-03-23 11:50:43 +0000 | [diff] [blame] | 67 | : STI(Subtarget), IB(B), |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 68 | HWS(llvm::make_unique<Scheduler>(this, Subtarget.getSchedModel(), |
| 69 | LoadQueueSize, StoreQueueSize, |
| 70 | AssumeNoAlias)), |
Andrea Di Biagio | c74ad50 | 2018-04-05 15:41:41 +0000 | [diff] [blame] | 71 | DU(llvm::make_unique<DispatchUnit>(this, Subtarget.getSchedModel(), MRI, |
| 72 | RegisterFileSize, DispatchWidth, |
| 73 | HWS.get())), |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 74 | SM(Source), Cycles(0) { |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 75 | HWS->setDispatchUnit(DU.get()); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void run() { |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 79 | while (SM.hasNext() || !DU->isRCUEmpty()) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 80 | runCycle(Cycles++); |
| 81 | } |
| 82 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 83 | void eraseInstruction(const InstRef &IR) { |
| 84 | Instructions.erase(IR.getSourceIndex()); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 85 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 86 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 87 | void addEventListener(HWEventListener *Listener); |
| 88 | void notifyCycleBegin(unsigned Cycle); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 89 | void notifyInstructionEvent(const HWInstructionEvent &Event); |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 90 | void notifyStallEvent(const HWStallEvent &Event); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 91 | void notifyResourceAvailable(const ResourceRef &RR); |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 92 | void notifyReservedBuffers(llvm::ArrayRef<unsigned> Buffers); |
| 93 | void notifyReleasedBuffers(llvm::ArrayRef<unsigned> Buffers); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 94 | void notifyCycleEnd(unsigned Cycle); |
| 95 | }; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 96 | } // namespace mca |
| 97 | |
| 98 | #endif |