Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 1 | //===--------------------- Pipeline.cpp -------------------------*- C++ -*-===// |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 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 | /// |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 11 | /// This file implements an ordered container of stages that simulate the |
| 12 | /// pipeline of a hardware backend. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 16 | #include "Pipeline.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 | |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 27 | void Pipeline::addEventListener(HWEventListener *Listener) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 28 | if (Listener) |
| 29 | Listeners.insert(Listener); |
Matt Davis | 7b5a36e | 2018-06-27 16:09:33 +0000 | [diff] [blame] | 30 | for (auto &S : Stages) |
| 31 | S->addListener(Listener); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 34 | bool Pipeline::hasWorkToProcess() { |
Matt Davis | 43de6db | 2018-06-22 16:17:26 +0000 | [diff] [blame] | 35 | const auto It = llvm::find_if(Stages, [](const std::unique_ptr<Stage> &S) { |
| 36 | return S->hasWorkToComplete(); |
| 37 | }); |
| 38 | return It != Stages.end(); |
| 39 | } |
| 40 | |
| 41 | // This routine returns early if any stage returns 'false' after execute() is |
| 42 | // called on it. |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 43 | bool Pipeline::executeStages(InstRef &IR) { |
Matt Davis | 43de6db | 2018-06-22 16:17:26 +0000 | [diff] [blame] | 44 | for (const std::unique_ptr<Stage> &S : Stages) |
| 45 | if (!S->execute(IR)) |
| 46 | return false; |
| 47 | return true; |
| 48 | } |
| 49 | |
Matt Davis | 3250899 | 2018-07-12 22:59:53 +0000 | [diff] [blame] | 50 | void Pipeline::preExecuteStages(const InstRef &IR) { |
| 51 | for (const std::unique_ptr<Stage> &S : Stages) |
| 52 | S->preExecute(IR); |
| 53 | } |
| 54 | |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 55 | void Pipeline::postExecuteStages(const InstRef &IR) { |
Matt Davis | 43de6db | 2018-06-22 16:17:26 +0000 | [diff] [blame] | 56 | for (const std::unique_ptr<Stage> &S : Stages) |
| 57 | S->postExecute(IR); |
| 58 | } |
| 59 | |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 60 | void Pipeline::run() { |
Matt Davis | 43de6db | 2018-06-22 16:17:26 +0000 | [diff] [blame] | 61 | while (hasWorkToProcess()) |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 62 | runCycle(Cycles++); |
| 63 | } |
| 64 | |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 65 | void Pipeline::runCycle(unsigned Cycle) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 66 | notifyCycleBegin(Cycle); |
| 67 | |
Matt Davis | 5b79ffc5b | 2018-05-25 18:00:25 +0000 | [diff] [blame] | 68 | // Update the stages before we do any processing for this cycle. |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 69 | InstRef IR; |
Matt Davis | 43de6db | 2018-06-22 16:17:26 +0000 | [diff] [blame] | 70 | for (auto &S : Stages) |
Matt Davis | 3250899 | 2018-07-12 22:59:53 +0000 | [diff] [blame] | 71 | S->cycleStart(); |
Matt Davis | bd12532 | 2018-05-22 20:51:58 +0000 | [diff] [blame] | 72 | |
Matt Davis | 43de6db | 2018-06-22 16:17:26 +0000 | [diff] [blame] | 73 | // Continue executing this cycle until any stage claims it cannot make |
| 74 | // progress. |
Matt Davis | 3250899 | 2018-07-12 22:59:53 +0000 | [diff] [blame] | 75 | while (true) { |
| 76 | preExecuteStages(IR); |
| 77 | if (!executeStages(IR)) |
| 78 | break; |
Matt Davis | 43de6db | 2018-06-22 16:17:26 +0000 | [diff] [blame] | 79 | postExecuteStages(IR); |
Matt Davis | 3250899 | 2018-07-12 22:59:53 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | for (auto &S : Stages) |
| 83 | S->cycleEnd(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 84 | |
| 85 | notifyCycleEnd(Cycle); |
| 86 | } |
| 87 | |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 88 | void Pipeline::notifyCycleBegin(unsigned Cycle) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 89 | LLVM_DEBUG(dbgs() << "[E] Cycle begin: " << Cycle << '\n'); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 90 | for (HWEventListener *Listener : Listeners) |
Andrea Di Biagio | 3e64644 | 2018-04-12 10:49:40 +0000 | [diff] [blame] | 91 | Listener->onCycleBegin(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 94 | void Pipeline::notifyCycleEnd(unsigned Cycle) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 95 | LLVM_DEBUG(dbgs() << "[E] Cycle end: " << Cycle << "\n\n"); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 96 | for (HWEventListener *Listener : Listeners) |
Andrea Di Biagio | 3e64644 | 2018-04-12 10:49:40 +0000 | [diff] [blame] | 97 | Listener->onCycleEnd(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 98 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 99 | } // namespace mca. |