Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===--------------------- Instruction.cpp ----------------------*- 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 | // |
Matt Davis | d8c387b | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 10 | // This file defines abstractions used by the Pipeline to model register reads, |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 11 | // register writes and instructions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Clement Courbet | 8178ac8 | 2018-12-17 08:08:31 +0000 | [diff] [blame] | 15 | #include "llvm/MCA/Instruction.h" |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | |
Fangrui Song | 467c307 | 2018-10-30 15:56:08 +0000 | [diff] [blame] | 19 | namespace llvm { |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 20 | namespace mca { |
| 21 | |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 22 | void ReadState::writeStartEvent(unsigned Cycles) { |
| 23 | assert(DependentWrites); |
| 24 | assert(CyclesLeft == UNKNOWN_CYCLES); |
| 25 | |
| 26 | // This read may be dependent on more than one write. This typically occurs |
| 27 | // when a definition is the result of multiple writes where at least one |
| 28 | // write does a partial register update. |
| 29 | // The HW is forced to do some extra bookkeeping to track of all the |
| 30 | // dependent writes, and implement a merging scheme for the partial writes. |
| 31 | --DependentWrites; |
| 32 | TotalCycles = std::max(TotalCycles, Cycles); |
| 33 | |
Andrea Di Biagio | 0093dbb | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 34 | if (!DependentWrites) { |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 35 | CyclesLeft = TotalCycles; |
Andrea Di Biagio | 0093dbb | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 36 | IsReady = !CyclesLeft; |
| 37 | } |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void WriteState::onInstructionIssued() { |
| 41 | assert(CyclesLeft == UNKNOWN_CYCLES); |
| 42 | // Update the number of cycles left based on the WriteDescriptor info. |
Andrea Di Biagio | 2d706a6 | 2018-07-06 13:46:10 +0000 | [diff] [blame] | 43 | CyclesLeft = getLatency(); |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 44 | |
Andrea Di Biagio | 04b507e | 2018-06-05 17:12:02 +0000 | [diff] [blame] | 45 | // Now that the time left before write-back is known, notify |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 46 | // all the users. |
| 47 | for (const std::pair<ReadState *, int> &User : Users) { |
| 48 | ReadState *RS = User.first; |
| 49 | unsigned ReadCycles = std::max(0, CyclesLeft - User.second); |
| 50 | RS->writeStartEvent(ReadCycles); |
| 51 | } |
Andrea Di Biagio | ab518ce | 2018-11-22 12:48:57 +0000 | [diff] [blame] | 52 | |
| 53 | // Notify any writes that are in a false dependency with this write. |
| 54 | if (PartialWrite) |
| 55 | PartialWrite->writeStartEvent(CyclesLeft); |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void WriteState::addUser(ReadState *User, int ReadAdvance) { |
| 59 | // If CyclesLeft is different than -1, then we don't need to |
| 60 | // update the list of users. We can just notify the user with |
| 61 | // the actual number of cycles left (which may be zero). |
| 62 | if (CyclesLeft != UNKNOWN_CYCLES) { |
| 63 | unsigned ReadCycles = std::max(0, CyclesLeft - ReadAdvance); |
| 64 | User->writeStartEvent(ReadCycles); |
| 65 | return; |
| 66 | } |
| 67 | |
Andrea Di Biagio | fb147de | 2018-11-22 14:48:53 +0000 | [diff] [blame] | 68 | if (llvm::find_if(Users, [&User](const std::pair<ReadState *, int> &Use) { |
| 69 | return Use.first == User; |
| 70 | }) == Users.end()) { |
| 71 | Users.emplace_back(User, ReadAdvance); |
| 72 | } |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Andrea Di Biagio | ab518ce | 2018-11-22 12:48:57 +0000 | [diff] [blame] | 75 | void WriteState::addUser(WriteState *User) { |
| 76 | if (CyclesLeft != UNKNOWN_CYCLES) { |
| 77 | User->writeStartEvent(std::max(0, CyclesLeft)); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | assert(!PartialWrite && "PartialWrite already set!"); |
| 82 | PartialWrite = User; |
| 83 | User->setDependentWrite(this); |
| 84 | } |
| 85 | |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 86 | void WriteState::cycleEvent() { |
| 87 | // Note: CyclesLeft can be a negative number. It is an error to |
| 88 | // make it an unsigned quantity because users of this write may |
| 89 | // specify a negative ReadAdvance. |
| 90 | if (CyclesLeft != UNKNOWN_CYCLES) |
| 91 | CyclesLeft--; |
Andrea Di Biagio | ab518ce | 2018-11-22 12:48:57 +0000 | [diff] [blame] | 92 | |
| 93 | if (DependentWriteCyclesLeft) |
| 94 | DependentWriteCyclesLeft--; |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ReadState::cycleEvent() { |
Andrea Di Biagio | 04b507e | 2018-06-05 17:12:02 +0000 | [diff] [blame] | 98 | // Update the total number of cycles. |
| 99 | if (DependentWrites && TotalCycles) { |
| 100 | --TotalCycles; |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | // Bail out immediately if we don't know how many cycles are left. |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 105 | if (CyclesLeft == UNKNOWN_CYCLES) |
| 106 | return; |
| 107 | |
Andrea Di Biagio | 0093dbb | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 108 | if (CyclesLeft) { |
Andrea Di Biagio | 04b507e | 2018-06-05 17:12:02 +0000 | [diff] [blame] | 109 | --CyclesLeft; |
Andrea Di Biagio | 0093dbb | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 110 | IsReady = !CyclesLeft; |
| 111 | } |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | #ifndef NDEBUG |
| 115 | void WriteState::dump() const { |
Andrea Di Biagio | ee5a514 | 2018-10-29 13:29:22 +0000 | [diff] [blame] | 116 | dbgs() << "{ OpIdx=" << WD->OpIndex << ", Lat=" << getLatency() << ", RegID " |
Andrea Di Biagio | 9271265 | 2018-07-05 16:13:49 +0000 | [diff] [blame] | 117 | << getRegisterID() << ", Cycles Left=" << getCyclesLeft() << " }"; |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 118 | } |
Andrea Di Biagio | a2237ff | 2018-06-28 15:50:26 +0000 | [diff] [blame] | 119 | |
| 120 | void WriteRef::dump() const { |
Andrea Di Biagio | 9271265 | 2018-07-05 16:13:49 +0000 | [diff] [blame] | 121 | dbgs() << "IID=" << getSourceIndex() << ' '; |
Andrea Di Biagio | a2237ff | 2018-06-28 15:50:26 +0000 | [diff] [blame] | 122 | if (isValid()) |
| 123 | getWriteState()->dump(); |
| 124 | else |
| 125 | dbgs() << "(null)"; |
| 126 | } |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
Andrea Di Biagio | efac2a8 | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 129 | void Instruction::dispatch(unsigned RCUToken) { |
Andrea Di Biagio | 280f00a | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 130 | assert(Stage == IS_INVALID); |
| 131 | Stage = IS_AVAILABLE; |
Andrea Di Biagio | efac2a8 | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 132 | RCUTokenID = RCUToken; |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 133 | |
Andrea Di Biagio | efac2a8 | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 134 | // Check if input operands are already available. |
Andrea Di Biagio | 8ff27db | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 135 | update(); |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void Instruction::execute() { |
| 139 | assert(Stage == IS_READY); |
| 140 | Stage = IS_EXECUTING; |
Andrea Di Biagio | efac2a8 | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 141 | |
| 142 | // Set the cycles left before the write-back stage. |
Andrea Di Biagio | a64e4ae | 2018-10-25 17:03:51 +0000 | [diff] [blame] | 143 | CyclesLeft = getLatency(); |
Andrea Di Biagio | efac2a8 | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 144 | |
Andrea Di Biagio | a64e4ae | 2018-10-25 17:03:51 +0000 | [diff] [blame] | 145 | for (WriteState &WS : getDefs()) |
| 146 | WS.onInstructionIssued(); |
Andrea Di Biagio | efac2a8 | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 147 | |
| 148 | // Transition to the "executed" stage if this is a zero-latency instruction. |
Andrea Di Biagio | 280f00a | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 149 | if (!CyclesLeft) |
| 150 | Stage = IS_EXECUTED; |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Andrea Di Biagio | 97666c4 | 2018-10-03 15:02:44 +0000 | [diff] [blame] | 153 | void Instruction::forceExecuted() { |
| 154 | assert(Stage == IS_READY && "Invalid internal state!"); |
| 155 | CyclesLeft = 0; |
| 156 | Stage = IS_EXECUTED; |
| 157 | } |
| 158 | |
Andrea Di Biagio | 8ff27db | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 159 | void Instruction::update() { |
Andrea Di Biagio | 0093dbb | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 160 | assert(isDispatched() && "Unexpected instruction stage found!"); |
Andrea Di Biagio | eaabc71 | 2018-07-15 11:01:38 +0000 | [diff] [blame] | 161 | |
Andrea Di Biagio | a64e4ae | 2018-10-25 17:03:51 +0000 | [diff] [blame] | 162 | if (!all_of(getUses(), [](const ReadState &Use) { return Use.isReady(); })) |
Andrea Di Biagio | eaabc71 | 2018-07-15 11:01:38 +0000 | [diff] [blame] | 163 | return; |
| 164 | |
| 165 | // A partial register write cannot complete before a dependent write. |
Andrea Di Biagio | a64e4ae | 2018-10-25 17:03:51 +0000 | [diff] [blame] | 166 | auto IsDefReady = [&](const WriteState &Def) { |
Andrea Di Biagio | ab518ce | 2018-11-22 12:48:57 +0000 | [diff] [blame] | 167 | if (!Def.getDependentWrite()) { |
| 168 | unsigned CyclesLeft = Def.getDependentWriteCyclesLeft(); |
| 169 | return !CyclesLeft || CyclesLeft < getLatency(); |
Andrea Di Biagio | eaabc71 | 2018-07-15 11:01:38 +0000 | [diff] [blame] | 170 | } |
Andrea Di Biagio | ab518ce | 2018-11-22 12:48:57 +0000 | [diff] [blame] | 171 | return false; |
Andrea Di Biagio | eaabc71 | 2018-07-15 11:01:38 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
Andrea Di Biagio | a64e4ae | 2018-10-25 17:03:51 +0000 | [diff] [blame] | 174 | if (all_of(getDefs(), IsDefReady)) |
Andrea Di Biagio | 8ff27db | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 175 | Stage = IS_READY; |
| 176 | } |
| 177 | |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 178 | void Instruction::cycleEvent() { |
Andrea Di Biagio | 280f00a | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 179 | if (isReady()) |
| 180 | return; |
| 181 | |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 182 | if (isDispatched()) { |
Andrea Di Biagio | a64e4ae | 2018-10-25 17:03:51 +0000 | [diff] [blame] | 183 | for (ReadState &Use : getUses()) |
| 184 | Use.cycleEvent(); |
Andrea Di Biagio | 0093dbb | 2018-06-27 11:17:07 +0000 | [diff] [blame] | 185 | |
Andrea Di Biagio | ab518ce | 2018-11-22 12:48:57 +0000 | [diff] [blame] | 186 | for (WriteState &Def : getDefs()) |
| 187 | Def.cycleEvent(); |
| 188 | |
Andrea Di Biagio | eaabc71 | 2018-07-15 11:01:38 +0000 | [diff] [blame] | 189 | update(); |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 190 | return; |
| 191 | } |
Andrea Di Biagio | 280f00a | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 192 | |
| 193 | assert(isExecuting() && "Instruction not in-flight?"); |
| 194 | assert(CyclesLeft && "Instruction already executed?"); |
Andrea Di Biagio | a64e4ae | 2018-10-25 17:03:51 +0000 | [diff] [blame] | 195 | for (WriteState &Def : getDefs()) |
| 196 | Def.cycleEvent(); |
Andrea Di Biagio | 280f00a | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 197 | CyclesLeft--; |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 198 | if (!CyclesLeft) |
| 199 | Stage = IS_EXECUTED; |
| 200 | } |
Andrea Di Biagio | a2237ff | 2018-06-28 15:50:26 +0000 | [diff] [blame] | 201 | |
| 202 | const unsigned WriteRef::INVALID_IID = std::numeric_limits<unsigned>::max(); |
| 203 | |
Andrea Di Biagio | 29b29cc | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 204 | } // namespace mca |
Fangrui Song | 467c307 | 2018-10-30 15:56:08 +0000 | [diff] [blame] | 205 | } // namespace llvm |