1 //===--------------------- Instruction.cpp ----------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines abstractions used by the Pipeline to model register reads,
10 // register writes and instructions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/MCA/Instruction.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/raw_ostream.h"
21 void WriteState::writeStartEvent(unsigned IID
, MCPhysReg RegID
,
26 DependentWriteCyclesLeft
= Cycles
;
27 DependentWrite
= nullptr;
30 void ReadState::writeStartEvent(unsigned IID
, MCPhysReg RegID
, unsigned Cycles
) {
31 assert(DependentWrites
);
32 assert(CyclesLeft
== UNKNOWN_CYCLES
);
34 // This read may be dependent on more than one write. This typically occurs
35 // when a definition is the result of multiple writes where at least one
36 // write does a partial register update.
37 // The HW is forced to do some extra bookkeeping to track of all the
38 // dependent writes, and implement a merging scheme for the partial writes.
40 if (TotalCycles
< Cycles
) {
47 if (!DependentWrites
) {
48 CyclesLeft
= TotalCycles
;
49 IsReady
= !CyclesLeft
;
53 void WriteState::onInstructionIssued(unsigned IID
) {
54 assert(CyclesLeft
== UNKNOWN_CYCLES
);
55 // Update the number of cycles left based on the WriteDescriptor info.
56 CyclesLeft
= getLatency();
58 // Now that the time left before write-back is known, notify
60 for (const std::pair
<ReadState
*, int> &User
: Users
) {
61 ReadState
*RS
= User
.first
;
62 unsigned ReadCycles
= std::max(0, CyclesLeft
- User
.second
);
63 RS
->writeStartEvent(IID
, RegisterID
, ReadCycles
);
66 // Notify any writes that are in a false dependency with this write.
68 PartialWrite
->writeStartEvent(IID
, RegisterID
, CyclesLeft
);
71 void WriteState::addUser(unsigned IID
, ReadState
*User
, int ReadAdvance
) {
72 // If CyclesLeft is different than -1, then we don't need to
73 // update the list of users. We can just notify the user with
74 // the actual number of cycles left (which may be zero).
75 if (CyclesLeft
!= UNKNOWN_CYCLES
) {
76 unsigned ReadCycles
= std::max(0, CyclesLeft
- ReadAdvance
);
77 User
->writeStartEvent(IID
, RegisterID
, ReadCycles
);
81 Users
.emplace_back(User
, ReadAdvance
);
84 void WriteState::addUser(unsigned IID
, WriteState
*User
) {
85 if (CyclesLeft
!= UNKNOWN_CYCLES
) {
86 User
->writeStartEvent(IID
, RegisterID
, std::max(0, CyclesLeft
));
90 assert(!PartialWrite
&& "PartialWrite already set!");
92 User
->setDependentWrite(this);
95 void WriteState::cycleEvent() {
96 // Note: CyclesLeft can be a negative number. It is an error to
97 // make it an unsigned quantity because users of this write may
98 // specify a negative ReadAdvance.
99 if (CyclesLeft
!= UNKNOWN_CYCLES
)
102 if (DependentWriteCyclesLeft
)
103 DependentWriteCyclesLeft
--;
106 void ReadState::cycleEvent() {
107 // Update the total number of cycles.
108 if (DependentWrites
&& TotalCycles
) {
113 // Bail out immediately if we don't know how many cycles are left.
114 if (CyclesLeft
== UNKNOWN_CYCLES
)
119 IsReady
= !CyclesLeft
;
124 void WriteState::dump() const {
125 dbgs() << "{ OpIdx=" << WD
->OpIndex
<< ", Lat=" << getLatency() << ", RegID "
126 << getRegisterID() << ", Cycles Left=" << getCyclesLeft() << " }";
129 void WriteRef::dump() const {
130 dbgs() << "IID=" << getSourceIndex() << ' ';
132 getWriteState()->dump();
138 const CriticalDependency
&Instruction::computeCriticalRegDep() {
139 if (CriticalRegDep
.Cycles
)
140 return CriticalRegDep
;
142 unsigned MaxLatency
= 0;
143 for (const WriteState
&WS
: getDefs()) {
144 const CriticalDependency
&WriteCRD
= WS
.getCriticalRegDep();
145 if (WriteCRD
.Cycles
> MaxLatency
)
146 CriticalRegDep
= WriteCRD
;
149 for (const ReadState
&RS
: getUses()) {
150 const CriticalDependency
&ReadCRD
= RS
.getCriticalRegDep();
151 if (ReadCRD
.Cycles
> MaxLatency
)
152 CriticalRegDep
= ReadCRD
;
155 return CriticalRegDep
;
158 void Instruction::dispatch(unsigned RCUToken
) {
159 assert(Stage
== IS_INVALID
);
160 Stage
= IS_DISPATCHED
;
161 RCUTokenID
= RCUToken
;
163 // Check if input operands are already available.
164 if (updateDispatched())
168 void Instruction::execute(unsigned IID
) {
169 assert(Stage
== IS_READY
);
170 Stage
= IS_EXECUTING
;
172 // Set the cycles left before the write-back stage.
173 CyclesLeft
= getLatency();
175 for (WriteState
&WS
: getDefs())
176 WS
.onInstructionIssued(IID
);
178 // Transition to the "executed" stage if this is a zero-latency instruction.
183 void Instruction::forceExecuted() {
184 assert(Stage
== IS_READY
&& "Invalid internal state!");
189 bool Instruction::updatePending() {
190 assert(isPending() && "Unexpected instruction stage found!");
192 if (!all_of(getUses(), [](const ReadState
&Use
) { return Use
.isReady(); }))
195 // A partial register write cannot complete before a dependent write.
196 if (!all_of(getDefs(), [](const WriteState
&Def
) { return Def
.isReady(); }))
203 bool Instruction::updateDispatched() {
204 assert(isDispatched() && "Unexpected instruction stage found!");
206 if (!all_of(getUses(), [](const ReadState
&Use
) {
207 return Use
.isPending() || Use
.isReady();
211 // A partial register write cannot complete before a dependent write.
212 if (!all_of(getDefs(),
213 [](const WriteState
&Def
) { return !Def
.getDependentWrite(); }))
220 void Instruction::update() {
227 void Instruction::cycleEvent() {
231 if (isDispatched() || isPending()) {
232 for (ReadState
&Use
: getUses())
235 for (WriteState
&Def
: getDefs())
242 assert(isExecuting() && "Instruction not in-flight?");
243 assert(CyclesLeft
&& "Instruction already executed?");
244 for (WriteState
&Def
: getDefs())
251 const unsigned WriteRef::INVALID_IID
= std::numeric_limits
<unsigned>::max();