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 ReadState::writeStartEvent(unsigned Cycles
) {
22 assert(DependentWrites
);
23 assert(CyclesLeft
== UNKNOWN_CYCLES
);
25 // This read may be dependent on more than one write. This typically occurs
26 // when a definition is the result of multiple writes where at least one
27 // write does a partial register update.
28 // The HW is forced to do some extra bookkeeping to track of all the
29 // dependent writes, and implement a merging scheme for the partial writes.
31 TotalCycles
= std::max(TotalCycles
, Cycles
);
33 if (!DependentWrites
) {
34 CyclesLeft
= TotalCycles
;
35 IsReady
= !CyclesLeft
;
39 void WriteState::onInstructionIssued() {
40 assert(CyclesLeft
== UNKNOWN_CYCLES
);
41 // Update the number of cycles left based on the WriteDescriptor info.
42 CyclesLeft
= getLatency();
44 // Now that the time left before write-back is known, notify
46 for (const std::pair
<ReadState
*, int> &User
: Users
) {
47 ReadState
*RS
= User
.first
;
48 unsigned ReadCycles
= std::max(0, CyclesLeft
- User
.second
);
49 RS
->writeStartEvent(ReadCycles
);
52 // Notify any writes that are in a false dependency with this write.
54 PartialWrite
->writeStartEvent(CyclesLeft
);
57 void WriteState::addUser(ReadState
*User
, int ReadAdvance
) {
58 // If CyclesLeft is different than -1, then we don't need to
59 // update the list of users. We can just notify the user with
60 // the actual number of cycles left (which may be zero).
61 if (CyclesLeft
!= UNKNOWN_CYCLES
) {
62 unsigned ReadCycles
= std::max(0, CyclesLeft
- ReadAdvance
);
63 User
->writeStartEvent(ReadCycles
);
67 Users
.emplace_back(User
, ReadAdvance
);
70 void WriteState::addUser(WriteState
*User
) {
71 if (CyclesLeft
!= UNKNOWN_CYCLES
) {
72 User
->writeStartEvent(std::max(0, CyclesLeft
));
76 assert(!PartialWrite
&& "PartialWrite already set!");
78 User
->setDependentWrite(this);
81 void WriteState::cycleEvent() {
82 // Note: CyclesLeft can be a negative number. It is an error to
83 // make it an unsigned quantity because users of this write may
84 // specify a negative ReadAdvance.
85 if (CyclesLeft
!= UNKNOWN_CYCLES
)
88 if (DependentWriteCyclesLeft
)
89 DependentWriteCyclesLeft
--;
92 void ReadState::cycleEvent() {
93 // Update the total number of cycles.
94 if (DependentWrites
&& TotalCycles
) {
99 // Bail out immediately if we don't know how many cycles are left.
100 if (CyclesLeft
== UNKNOWN_CYCLES
)
105 IsReady
= !CyclesLeft
;
110 void WriteState::dump() const {
111 dbgs() << "{ OpIdx=" << WD
->OpIndex
<< ", Lat=" << getLatency() << ", RegID "
112 << getRegisterID() << ", Cycles Left=" << getCyclesLeft() << " }";
115 void WriteRef::dump() const {
116 dbgs() << "IID=" << getSourceIndex() << ' ';
118 getWriteState()->dump();
124 void Instruction::dispatch(unsigned RCUToken
) {
125 assert(Stage
== IS_INVALID
);
126 Stage
= IS_DISPATCHED
;
127 RCUTokenID
= RCUToken
;
129 // Check if input operands are already available.
130 if (updateDispatched())
134 void Instruction::execute() {
135 assert(Stage
== IS_READY
);
136 Stage
= IS_EXECUTING
;
138 // Set the cycles left before the write-back stage.
139 CyclesLeft
= getLatency();
141 for (WriteState
&WS
: getDefs())
142 WS
.onInstructionIssued();
144 // Transition to the "executed" stage if this is a zero-latency instruction.
149 void Instruction::forceExecuted() {
150 assert(Stage
== IS_READY
&& "Invalid internal state!");
155 bool Instruction::updatePending() {
156 assert(isPending() && "Unexpected instruction stage found!");
158 if (!all_of(getUses(), [](const ReadState
&Use
) { return Use
.isReady(); }))
161 // A partial register write cannot complete before a dependent write.
162 if (!all_of(getDefs(), [](const WriteState
&Def
) { return Def
.isReady(); }))
169 bool Instruction::updateDispatched() {
170 assert(isDispatched() && "Unexpected instruction stage found!");
172 if (!all_of(getUses(), [](const ReadState
&Use
) {
173 return Use
.isPending() || Use
.isReady();
177 // A partial register write cannot complete before a dependent write.
178 if (!all_of(getDefs(),
179 [](const WriteState
&Def
) { return !Def
.getDependentWrite(); }))
186 void Instruction::update() {
193 void Instruction::cycleEvent() {
197 if (isDispatched() || isPending()) {
198 for (ReadState
&Use
: getUses())
201 for (WriteState
&Def
: getDefs())
208 assert(isExecuting() && "Instruction not in-flight?");
209 assert(CyclesLeft
&& "Instruction already executed?");
210 for (WriteState
&Def
: getDefs())
217 const unsigned WriteRef::INVALID_IID
= std::numeric_limits
<unsigned>::max();