blob: 9077b6e6f6f52cf2aca212946d5d9e4a33c0192a [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===--------------------- 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 Davis5d1cda12018-05-15 20:21:04 +000016#include "FetchStage.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000017#include "HWEventListener.h"
18#include "llvm/CodeGen/TargetSchedule.h"
19#include "llvm/Support/Debug.h"
20
21namespace mca {
22
23#define DEBUG_TYPE "llvm-mca"
24
25using namespace llvm;
26
27void Backend::addEventListener(HWEventListener *Listener) {
28 if (Listener)
29 Listeners.insert(Listener);
30}
31
Matt Davis5d1cda12018-05-15 20:21:04 +000032void Backend::run() {
Matt Davis679083e2018-05-17 19:22:29 +000033 while (Fetch->isReady() || !Dispatch->isReady())
Matt Davis5d1cda12018-05-15 20:21:04 +000034 runCycle(Cycles++);
35}
36
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000037void Backend::runCycle(unsigned Cycle) {
38 notifyCycleBegin(Cycle);
39
Matt Davis5d1cda12018-05-15 20:21:04 +000040 InstRef IR;
Matt Davisbd125322018-05-22 20:51:58 +000041 Dispatch->preExecute(IR);
42 HWS->cycleEvent(); // TODO: This will eventually be stage-ified.
43
Matt Davis5d1cda12018-05-15 20:21:04 +000044 while (Fetch->execute(IR)) {
Matt Davis679083e2018-05-17 19:22:29 +000045 if (!Dispatch->execute(IR))
Matt Davis21a8d322018-05-07 18:29:15 +000046 break;
Matt Davis5d1cda12018-05-15 20:21:04 +000047 Fetch->postExecute(IR);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000048 }
49
50 notifyCycleEnd(Cycle);
51}
52
53void Backend::notifyCycleBegin(unsigned Cycle) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000054 LLVM_DEBUG(dbgs() << "[E] Cycle begin: " << Cycle << '\n');
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000055 for (HWEventListener *Listener : Listeners)
Andrea Di Biagio3e646442018-04-12 10:49:40 +000056 Listener->onCycleBegin();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000057}
58
Clement Courbet844f22d2018-03-13 13:11:01 +000059void Backend::notifyInstructionEvent(const HWInstructionEvent &Event) {
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000060 for (HWEventListener *Listener : Listeners)
Clement Courbet844f22d2018-03-13 13:11:01 +000061 Listener->onInstructionEvent(Event);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000062}
63
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +000064void Backend::notifyStallEvent(const HWStallEvent &Event) {
65 for (HWEventListener *Listener : Listeners)
66 Listener->onStallEvent(Event);
67}
68
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000069void Backend::notifyResourceAvailable(const ResourceRef &RR) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000070 LLVM_DEBUG(dbgs() << "[E] Resource Available: [" << RR.first << '.'
71 << RR.second << "]\n");
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000072 for (HWEventListener *Listener : Listeners)
73 Listener->onResourceAvailable(RR);
74}
75
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +000076void Backend::notifyReservedBuffers(ArrayRef<unsigned> Buffers) {
77 for (HWEventListener *Listener : Listeners)
78 Listener->onReservedBuffers(Buffers);
79}
80
81void Backend::notifyReleasedBuffers(ArrayRef<unsigned> Buffers) {
82 for (HWEventListener *Listener : Listeners)
83 Listener->onReleasedBuffers(Buffers);
84}
85
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000086void Backend::notifyCycleEnd(unsigned Cycle) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000087 LLVM_DEBUG(dbgs() << "[E] Cycle end: " << Cycle << "\n\n");
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000088 for (HWEventListener *Listener : Listeners)
Andrea Di Biagio3e646442018-04-12 10:49:40 +000089 Listener->onCycleEnd();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000090}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000091} // namespace mca.