summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-mca/Backend.h
blob: 13e65bb208dec817d5a0ecbdba597aaf4c431a7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//===--------------------- Backend.h ----------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
/// \file
///
/// This file implements an OoO backend for the llvm-mca tool.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_TOOLS_LLVM_MCA_BACKEND_H
#define LLVM_TOOLS_LLVM_MCA_BACKEND_H

#include "Dispatch.h"
#include "InstrBuilder.h"
#include "Scheduler.h"
#include "SourceMgr.h"

namespace mca {

class HWEventListener;
class HWInstructionEvent;

/// \brief An out of order backend for a specific subtarget.
///
/// It emulates an out-of-order execution of instructions. Instructions are
/// fetched from a MCInst sequence managed by an object of class SourceMgr.
/// Instructions are firstly dispatched to the schedulers and then executed.
/// This class tracks the lifetime of an instruction from the moment where
/// it gets dispatched to the schedulers, to the moment where it finishes
/// executing and register writes are architecturally committed.
/// In particular, it monitors changes in the state of every instruction
/// in flight.
/// Instructions are executed in a loop of iterations. The number of iterations
/// is defined by the SourceMgr object.
/// The Backend entrypoint is method 'Run()' which execute cycles in a loop
/// until there are new instructions to dispatch, and not every instruction
/// has been retired.
/// Internally, the Backend collects statistical information in the form of
/// histograms. For example, it tracks how the dispatch group size changes
/// over time.
class Backend {
  const llvm::MCSubtargetInfo &STI;

  std::unique_ptr<InstrBuilder> IB;
  std::unique_ptr<Scheduler> HWS;
  std::unique_ptr<DispatchUnit> DU;
  SourceMgr &SM;
  unsigned Cycles;

  llvm::DenseMap<unsigned, std::unique_ptr<Instruction>> Instructions;
  std::set<HWEventListener *> Listeners;

  void runCycle(unsigned Cycle);

public:
  Backend(const llvm::MCSubtargetInfo &Subtarget, const llvm::MCInstrInfo &MCII,
          const llvm::MCRegisterInfo &MRI, SourceMgr &Source,
          unsigned DispatchWidth = 0, unsigned RegisterFileSize = 0,
          unsigned MaxRetirePerCycle = 0, unsigned LoadQueueSize = 0,
          unsigned StoreQueueSize = 0, bool AssumeNoAlias = false)
      : STI(Subtarget),
        HWS(llvm::make_unique<Scheduler>(this, Subtarget.getSchedModel(),
                                         LoadQueueSize, StoreQueueSize,
                                         AssumeNoAlias)),
        DU(llvm::make_unique<DispatchUnit>(
            this, MRI, Subtarget.getSchedModel().MicroOpBufferSize,
            RegisterFileSize, MaxRetirePerCycle, DispatchWidth, HWS.get())),
        SM(Source), Cycles(0) {
    IB = llvm::make_unique<InstrBuilder>(MCII, HWS->getProcResourceMasks());
    HWS->setDispatchUnit(DU.get());
  }

  void run() {
    while (SM.hasNext() || !DU->isRCUEmpty())
      runCycle(Cycles++);
  }

  const Instruction &getInstruction(unsigned Index) const {
    const auto It = Instructions.find(Index);
    assert(It != Instructions.end() && "no running instructions with index");
    assert(It->second);
    return *It->second;
  }
  void eraseInstruction(unsigned Index) { Instructions.erase(Index); }
  unsigned getTotalRegisterMappingsCreated() const {
    return DU->getTotalRegisterMappingsCreated();
  }
  unsigned getMaxUsedRegisterMappings() const {
    return DU->getMaxUsedRegisterMappings();
  }
  const llvm::MCSchedModel &getSchedModel() const {
    return STI.getSchedModel();
  }
  void getBuffersUsage(std::vector<BufferUsageEntry> &Usage) const {
    return HWS->getBuffersUsage(Usage);
  }

  unsigned getNumRATStalls() const { return DU->getNumRATStalls(); }
  unsigned getNumRCUStalls() const { return DU->getNumRCUStalls(); }
  unsigned getNumSQStalls() const { return DU->getNumSQStalls(); }
  unsigned getNumLDQStalls() const { return DU->getNumLDQStalls(); }
  unsigned getNumSTQStalls() const { return DU->getNumSTQStalls(); }
  unsigned getNumDispatchGroupStalls() const {
    return DU->getNumDispatchGroupStalls();
  }

  void addEventListener(HWEventListener *Listener);
  void notifyCycleBegin(unsigned Cycle);
  void notifyInstructionEvent(const HWInstructionEvent &Event);
  void notifyResourceAvailable(const ResourceRef &RR);
  void notifyCycleEnd(unsigned Cycle);
};

} // namespace mca

#endif
OpenPOWER on IntegriCloud