blob: 79e5bba7f8daae8c0097fa81e925e38489e49e60 [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===--------------------- 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
Matt Davis679083e2018-05-17 19:22:29 +000018#include "DispatchStage.h"
Matt Davis488ac4c2018-06-14 01:20:18 +000019#include "ExecuteStage.h"
Matt Davis5d1cda12018-05-15 20:21:04 +000020#include "FetchStage.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000021#include "InstrBuilder.h"
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000022#include "RegisterFile.h"
23#include "RetireControlUnit.h"
24#include "RetireStage.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000025#include "Scheduler.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000026
27namespace mca {
28
Andrea Di Biagio3db1fd92018-03-08 16:34:19 +000029class HWEventListener;
Clement Courbet844f22d2018-03-13 13:11:01 +000030class HWInstructionEvent;
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +000031class HWStallEvent;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000032
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000033/// An out of order backend for a specific subtarget.
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000034///
35/// It emulates an out-of-order execution of instructions. Instructions are
Matt Davis5d1cda12018-05-15 20:21:04 +000036/// fetched from a MCInst sequence managed by an initial 'Fetch' stage.
37/// Instructions are firstly fetched, then dispatched to the schedulers, and
38/// then executed.
39///
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000040/// This class tracks the lifetime of an instruction from the moment where
41/// it gets dispatched to the schedulers, to the moment where it finishes
42/// executing and register writes are architecturally committed.
43/// In particular, it monitors changes in the state of every instruction
44/// in flight.
Matt Davis5d1cda12018-05-15 20:21:04 +000045///
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000046/// Instructions are executed in a loop of iterations. The number of iterations
Matt Davis5d1cda12018-05-15 20:21:04 +000047/// is defined by the SourceMgr object, which is managed by the initial stage
48/// of the instruction pipeline.
49///
50/// The Backend entry point is method 'run()' which executes cycles in a loop
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000051/// until there are new instructions to dispatch, and not every instruction
52/// has been retired.
Matt Davis5d1cda12018-05-15 20:21:04 +000053///
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000054/// Internally, the Backend collects statistical information in the form of
55/// histograms. For example, it tracks how the dispatch group size changes
56/// over time.
57class Backend {
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000058 // The following are the simulated hardware components of the backend.
59 RetireControlUnit RCU;
60 RegisterFile PRF;
Matt Davis488ac4c2018-06-14 01:20:18 +000061 Scheduler HWS;
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000062
Matt Davis5d1cda12018-05-15 20:21:04 +000063 /// TODO: Eventually this will become a list of unique Stage* that this
64 /// backend pipeline executes.
65 std::unique_ptr<FetchStage> Fetch;
Matt Davis679083e2018-05-17 19:22:29 +000066 std::unique_ptr<DispatchStage> Dispatch;
Matt Davis488ac4c2018-06-14 01:20:18 +000067 std::unique_ptr<ExecuteStage> Execute;
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000068 std::unique_ptr<RetireStage> Retire;
69
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000070 std::set<HWEventListener *> Listeners;
Matt Davis5d1cda12018-05-15 20:21:04 +000071 unsigned Cycles;
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000072
73 void runCycle(unsigned Cycle);
74
75public:
Andrea Di Biagiob5088da2018-03-23 11:50:43 +000076 Backend(const llvm::MCSubtargetInfo &Subtarget,
Matt Davis5d1cda12018-05-15 20:21:04 +000077 const llvm::MCRegisterInfo &MRI,
78 std::unique_ptr<FetchStage> InitialStage, unsigned DispatchWidth = 0,
79 unsigned RegisterFileSize = 0, unsigned LoadQueueSize = 0,
80 unsigned StoreQueueSize = 0, bool AssumeNoAlias = false)
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000081 : RCU(Subtarget.getSchedModel()),
82 PRF(Subtarget.getSchedModel(), MRI, RegisterFileSize),
Matt Davis488ac4c2018-06-14 01:20:18 +000083 HWS(Subtarget.getSchedModel(), LoadQueueSize, StoreQueueSize,
84 AssumeNoAlias),
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000085 Fetch(std::move(InitialStage)),
Matt Davis679083e2018-05-17 19:22:29 +000086 Dispatch(llvm::make_unique<DispatchStage>(
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000087 this, Subtarget, MRI, RegisterFileSize, DispatchWidth, RCU, PRF,
Matt Davis488ac4c2018-06-14 01:20:18 +000088 HWS)),
89 Execute(llvm::make_unique<ExecuteStage>(this, RCU, HWS)),
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000090 Retire(llvm::make_unique<RetireStage>(this, RCU, PRF)), Cycles(0) {}
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000091
Matt Davis5d1cda12018-05-15 20:21:04 +000092 void run();
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000093 void addEventListener(HWEventListener *Listener);
94 void notifyCycleBegin(unsigned Cycle);
Clement Courbet844f22d2018-03-13 13:11:01 +000095 void notifyInstructionEvent(const HWInstructionEvent &Event);
Andrea Di Biagio91ab2ee2018-03-19 13:23:07 +000096 void notifyStallEvent(const HWStallEvent &Event);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000097 void notifyResourceAvailable(const ResourceRef &RR);
Andrea Di Biagioa3f2e482018-03-20 18:20:39 +000098 void notifyReservedBuffers(llvm::ArrayRef<unsigned> Buffers);
99 void notifyReleasedBuffers(llvm::ArrayRef<unsigned> Buffers);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000100 void notifyCycleEnd(unsigned Cycle);
101};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000102} // namespace mca
103
104#endif