LLVM 22.0.0git
DispatchStage.cpp
Go to the documentation of this file.
1//===--------------------- DispatchStage.cpp --------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This file models the dispatch component of an instruction pipeline.
11///
12/// The DispatchStage is responsible for updating instruction dependencies
13/// and communicating to the simulated instruction scheduler that an instruction
14/// is ready to be scheduled for execution.
15///
16//===----------------------------------------------------------------------===//
17
20#include "llvm/Support/Debug.h"
21
22#define DEBUG_TYPE "llvm-mca"
23
24namespace llvm {
25namespace mca {
26
28 const MCRegisterInfo &MRI,
29 unsigned MaxDispatchWidth, RetireControlUnit &R,
31 : DispatchWidth(MaxDispatchWidth), AvailableEntries(MaxDispatchWidth),
32 CarryOver(0U), STI(Subtarget), RCU(R), PRF(F) {
33 if (!DispatchWidth)
34 DispatchWidth = Subtarget.getSchedModel().IssueWidth;
35}
36
37void DispatchStage::notifyInstructionDispatched(const InstRef &IR,
38 ArrayRef<unsigned> UsedRegs,
39 unsigned UOps) const {
40 LLVM_DEBUG(dbgs() << "[E] Instruction Dispatched: #" << IR << '\n');
42 HWInstructionDispatchedEvent(IR, UsedRegs, UOps));
43}
44
45bool DispatchStage::checkPRF(const InstRef &IR) const {
47 for (const WriteState &RegDef : IR.getInstruction()->getDefs())
48 RegDefs.emplace_back(RegDef.getRegisterID());
49
50 const unsigned RegisterMask = PRF.isAvailable(RegDefs);
51 // A mask with all zeroes means: register files are available.
52 if (RegisterMask) {
54 HWStallEvent(HWStallEvent::RegisterFileStall, IR));
55 return false;
56 }
57
58 return true;
59}
60
61bool DispatchStage::checkRCU(const InstRef &IR) const {
62 const unsigned NumMicroOps = IR.getInstruction()->getNumMicroOps();
63 if (RCU.isAvailable(NumMicroOps))
64 return true;
67 return false;
68}
69
70bool DispatchStage::canDispatch(const InstRef &IR) const {
71 bool CanDispatch = checkRCU(IR);
72 CanDispatch &= checkPRF(IR);
73 CanDispatch &= checkNextStage(IR);
74 return CanDispatch;
75}
76
77Error DispatchStage::dispatch(InstRef IR) {
78 assert(!CarryOver && "Cannot dispatch another instruction!");
79 Instruction &IS = *IR.getInstruction();
80 const unsigned NumMicroOps = IS.getNumMicroOps();
81 if (NumMicroOps > DispatchWidth) {
82 assert(AvailableEntries == DispatchWidth);
83 AvailableEntries = 0;
84 CarryOver = NumMicroOps - DispatchWidth;
85 CarriedOver = IR;
86 } else {
87 assert(AvailableEntries >= NumMicroOps);
88 AvailableEntries -= NumMicroOps;
89 }
90
91 // Check if this instructions ends the dispatch group.
92 if (IS.getEndGroup())
93 AvailableEntries = 0;
94
95 // Check if this is an optimizable reg-reg move or an XCHG-like instruction.
96 if (IS.isOptimizableMove())
97 if (PRF.tryEliminateMoveOrSwap(IS.getDefs(), IS.getUses()))
98 IS.setEliminated();
99
100 // A dependency-breaking instruction doesn't have to wait on the register
101 // input operands, and it is often optimized at register renaming stage.
102 // Update RAW dependencies if this instruction is not a dependency-breaking
103 // instruction. A dependency-breaking instruction is a zero-latency
104 // instruction that doesn't consume hardware resources.
105 // An example of dependency-breaking instruction on X86 is a zero-idiom XOR.
106 //
107 // We also don't update data dependencies for instructions that have been
108 // eliminated at register renaming stage.
109 if (!IS.isEliminated()) {
110 for (ReadState &RS : IS.getUses())
111 PRF.addRegisterRead(RS, STI);
112 }
113
114 // By default, a dependency-breaking zero-idiom is expected to be optimized
115 // at register renaming stage. That means, no physical register is allocated
116 // to the instruction.
117 SmallVector<unsigned, 4> RegisterFiles(PRF.getNumRegisterFiles());
118 for (WriteState &WS : IS.getDefs())
119 PRF.addRegisterWrite(WriteRef(IR.getSourceIndex(), &WS), RegisterFiles);
120
121 // Reserve entries in the reorder buffer.
122 unsigned RCUTokenID = RCU.dispatch(IR);
123 // Notify the instruction that it has been dispatched.
124 IS.dispatch(RCUTokenID);
125
126 // Notify listeners of the "instruction dispatched" event,
127 // and move IR to the next stage.
128 notifyInstructionDispatched(IR, RegisterFiles,
129 std::min(DispatchWidth, NumMicroOps));
130 return moveToTheNextStage(IR);
131}
132
134 // The retire stage is responsible for calling method `cycleStart`
135 // on the PRF.
136 if (!CarryOver) {
137 AvailableEntries = DispatchWidth;
138 return ErrorSuccess();
139 }
140
141 AvailableEntries = CarryOver >= DispatchWidth ? 0 : DispatchWidth - CarryOver;
142 unsigned DispatchedOpcodes = DispatchWidth - AvailableEntries;
143 CarryOver -= DispatchedOpcodes;
144 assert(CarriedOver && "Invalid dispatched instruction");
145
146 SmallVector<unsigned, 8> RegisterFiles(PRF.getNumRegisterFiles(), 0U);
147 notifyInstructionDispatched(CarriedOver, RegisterFiles, DispatchedOpcodes);
148 if (!CarryOver)
149 CarriedOver = InstRef();
150 return ErrorSuccess();
151}
152
154 // Conservatively bail out if there are no available dispatch entries.
155 if (!AvailableEntries)
156 return false;
157
158 const Instruction &Inst = *IR.getInstruction();
159 unsigned NumMicroOps = Inst.getNumMicroOps();
160 unsigned Required = std::min(NumMicroOps, DispatchWidth);
161 if (Required > AvailableEntries)
162 return false;
163
164 if (Inst.getBeginGroup() && AvailableEntries != DispatchWidth)
165 return false;
166
167 // The dispatch logic doesn't internally buffer instructions. It only accepts
168 // instructions that can be successfully moved to the next stage during this
169 // same cycle.
170 return canDispatch(IR);
171}
172
174 assert(canDispatch(IR) && "Cannot dispatch another instruction!");
175 return dispatch(IR);
176}
177
178#ifndef NDEBUG
180 PRF.dump();
181 RCU.dump();
182}
183#endif
184} // namespace mca
185} // namespace llvm
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file models the dispatch component of an instruction pipeline.
This file defines the main interface for hardware event listeners.
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:80
#define F(x, y, z)
Definition MD5.cpp:55
#define LLVM_DEBUG(...)
Definition Debug.h:114
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Subclass of Error for the sole purpose of identifying the success path in the type system.
Definition Error.h:334
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
const MCSchedModel & getSchedModel() const
Get the machine model for this subtarget's CPU.
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Error cycleStart() override
Called once at the start of each cycle.
bool isAvailable(const InstRef &IR) const override
Returns true if it can execute IR during this cycle.
Error execute(InstRef &IR) override
The primary action that this stage performs on instruction IR.
DispatchStage(const MCSubtargetInfo &Subtarget, const MCRegisterInfo &MRI, unsigned MaxDispatchWidth, RetireControlUnit &R, RegisterFile &F)
An InstRef contains both a SourceMgr index and Instruction pair.
unsigned getNumMicroOps() const
An instruction propagated through the simulated instruction pipeline.
Manages hardware register files, and tracks register definitions for register renaming purposes.
unsigned isAvailable(ArrayRef< MCPhysReg > Regs) const
Error moveToTheNextStage(InstRef &IR)
Called when an instruction is ready to move the next pipeline stage.
Definition Stage.h:74
void notifyEvent(const EventT &Event) const
Notify listeners of a particular hardware event.
Definition Stage.h:83
bool checkNextStage(const InstRef &IR) const
Definition Stage.h:66
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
This class tracks which instructions are in-flight (i.e., dispatched but not retired) in the OoO back...