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" |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 16 | #include "FetchStage.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 17 | #include "HWEventListener.h" |
| 18 | #include "llvm/CodeGen/TargetSchedule.h" |
| 19 | #include "llvm/Support/Debug.h" |
| 20 | |
| 21 | namespace mca { |
| 22 | |
| 23 | #define DEBUG_TYPE "llvm-mca" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | void Backend::addEventListener(HWEventListener *Listener) { |
| 28 | if (Listener) |
| 29 | Listeners.insert(Listener); |
| 30 | } |
| 31 | |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 32 | void Backend::run() { |
Matt Davis | 679083e | 2018-05-17 19:22:29 +0000 | [diff] [blame] | 33 | while (Fetch->isReady() || !Dispatch->isReady()) |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 34 | runCycle(Cycles++); |
| 35 | } |
| 36 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 37 | void Backend::runCycle(unsigned Cycle) { |
| 38 | notifyCycleBegin(Cycle); |
| 39 | |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 40 | InstRef IR; |
Matt Davis | bd12532 | 2018-05-22 20:51:58 +0000 | [diff] [blame] | 41 | Dispatch->preExecute(IR); |
| 42 | HWS->cycleEvent(); // TODO: This will eventually be stage-ified. |
| 43 | |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 44 | while (Fetch->execute(IR)) { |
Matt Davis | 679083e | 2018-05-17 19:22:29 +0000 | [diff] [blame] | 45 | if (!Dispatch->execute(IR)) |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 46 | break; |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 47 | Fetch->postExecute(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | notifyCycleEnd(Cycle); |
| 51 | } |
| 52 | |
| 53 | void Backend::notifyCycleBegin(unsigned Cycle) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 54 | LLVM_DEBUG(dbgs() << "[E] Cycle begin: " << Cycle << '\n'); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 55 | for (HWEventListener *Listener : Listeners) |
Andrea Di Biagio | 3e64644 | 2018-04-12 10:49:40 +0000 | [diff] [blame] | 56 | Listener->onCycleBegin(); |
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. |