Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===--------------------- Backend.cpp --------------------------*- 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 | /// Implementation of class Backend which emulates an hardware OoO backend. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Backend.h" |
| 16 | #include "HWEventListener.h" |
| 17 | #include "llvm/CodeGen/TargetSchedule.h" |
| 18 | #include "llvm/Support/Debug.h" |
| 19 | |
| 20 | namespace mca { |
| 21 | |
| 22 | #define DEBUG_TYPE "llvm-mca" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | void Backend::addEventListener(HWEventListener *Listener) { |
| 27 | if (Listener) |
| 28 | Listeners.insert(Listener); |
| 29 | } |
| 30 | |
| 31 | void Backend::runCycle(unsigned Cycle) { |
| 32 | notifyCycleBegin(Cycle); |
| 33 | |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 34 | while (SM.hasNext()) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 35 | SourceRef SR = SM.peekNext(); |
| 36 | std::unique_ptr<Instruction> NewIS = IB.createInstruction(*SR.second); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 37 | const InstrDesc &Desc = NewIS->getDesc(); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 38 | Instruction *IS = NewIS.get(); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 39 | InstRef IR(SR.first, IS); |
| 40 | if (!DU->isAvailable(Desc.NumMicroOps) || !DU->canDispatch(IR)) |
| 41 | break; |
| 42 | Instructions[SR.first] = std::move(NewIS); |
| 43 | DU->dispatch(IR, STI); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 44 | SM.updateNext(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | notifyCycleEnd(Cycle); |
| 48 | } |
| 49 | |
| 50 | void Backend::notifyCycleBegin(unsigned Cycle) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 51 | LLVM_DEBUG(dbgs() << "[E] Cycle begin: " << Cycle << '\n'); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 52 | for (HWEventListener *Listener : Listeners) |
Andrea Di Biagio | 3e64644 | 2018-04-12 10:49:40 +0000 | [diff] [blame] | 53 | Listener->onCycleBegin(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 54 | |
Andrea Di Biagio | 3e64644 | 2018-04-12 10:49:40 +0000 | [diff] [blame] | 55 | DU->cycleEvent(); |
| 56 | HWS->cycleEvent(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 59 | void Backend::notifyInstructionEvent(const HWInstructionEvent &Event) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 60 | for (HWEventListener *Listener : Listeners) |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 61 | Listener->onInstructionEvent(Event); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 64 | void Backend::notifyStallEvent(const HWStallEvent &Event) { |
| 65 | for (HWEventListener *Listener : Listeners) |
| 66 | Listener->onStallEvent(Event); |
| 67 | } |
| 68 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 69 | void Backend::notifyResourceAvailable(const ResourceRef &RR) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 70 | LLVM_DEBUG(dbgs() << "[E] Resource Available: [" << RR.first << '.' |
| 71 | << RR.second << "]\n"); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 72 | for (HWEventListener *Listener : Listeners) |
| 73 | Listener->onResourceAvailable(RR); |
| 74 | } |
| 75 | |
Andrea Di Biagio | a3f2e48 | 2018-03-20 18:20:39 +0000 | [diff] [blame] | 76 | void Backend::notifyReservedBuffers(ArrayRef<unsigned> Buffers) { |
| 77 | for (HWEventListener *Listener : Listeners) |
| 78 | Listener->onReservedBuffers(Buffers); |
| 79 | } |
| 80 | |
| 81 | void Backend::notifyReleasedBuffers(ArrayRef<unsigned> Buffers) { |
| 82 | for (HWEventListener *Listener : Listeners) |
| 83 | Listener->onReleasedBuffers(Buffers); |
| 84 | } |
| 85 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 86 | void Backend::notifyCycleEnd(unsigned Cycle) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 87 | LLVM_DEBUG(dbgs() << "[E] Cycle end: " << Cycle << "\n\n"); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 88 | for (HWEventListener *Listener : Listeners) |
Andrea Di Biagio | 3e64644 | 2018-04-12 10:49:40 +0000 | [diff] [blame] | 89 | Listener->onCycleEnd(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 90 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 91 | } // namespace mca. |