Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===--------------------- Dispatch.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 | /// \file |
| 10 | /// |
| 11 | /// This file implements methods declared by class RegisterFile, DispatchUnit |
| 12 | /// and RetireControlUnit. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "Dispatch.h" |
| 17 | #include "Backend.h" |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 18 | #include "HWEventListener.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 19 | #include "Scheduler.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | #define DEBUG_TYPE "llvm-mca" |
| 25 | |
| 26 | namespace mca { |
| 27 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 28 | void RegisterFile::initialize(const MCSchedModel &SM, unsigned NumRegs) { |
| 29 | // Create a default register file that "sees" all the machine registers |
| 30 | // declared by the target. The number of physical registers in the default |
| 31 | // register file is set equal to `NumRegs`. A value of zero for `NumRegs` |
| 32 | // means: this register file has an unbounded number of physical registers. |
| 33 | addRegisterFile({} /* all registers */, NumRegs); |
| 34 | if (!SM.hasExtraProcessorInfo()) |
| 35 | return; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 36 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 37 | // For each user defined register file, allocate a RegisterMappingTracker |
| 38 | // object. The size of every register file, as well as the mapping between |
| 39 | // register files and register classes is specified via tablegen. |
| 40 | const MCExtraProcessorInfo &Info = SM.getExtraProcessorInfo(); |
| 41 | for (unsigned I = 0, E = Info.NumRegisterFiles; I < E; ++I) { |
| 42 | const MCRegisterFileDesc &RF = Info.RegisterFiles[I]; |
| 43 | // Skip invalid register files with zero physical registers. |
| 44 | unsigned Length = RF.NumRegisterCostEntries; |
| 45 | if (!RF.NumPhysRegs) |
| 46 | continue; |
| 47 | // The cost of a register definition is equivalent to the number of |
| 48 | // physical registers that are allocated at register renaming stage. |
| 49 | const MCRegisterCostEntry *FirstElt = |
| 50 | &Info.RegisterCostTable[RF.RegisterCostEntryIdx]; |
| 51 | addRegisterFile(ArrayRef<MCRegisterCostEntry>(FirstElt, Length), |
| 52 | RF.NumPhysRegs); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void RegisterFile::addRegisterFile(ArrayRef<MCRegisterCostEntry> Entries, |
| 57 | unsigned NumPhysRegs) { |
| 58 | // A default register file is always allocated at index #0. That register file |
| 59 | // is mainly used to count the total number of mappings created by all |
| 60 | // register files at runtime. Users can limit the number of available physical |
| 61 | // registers in register file #0 through the command line flag |
| 62 | // `-register-file-size`. |
| 63 | unsigned RegisterFileIndex = RegisterFiles.size(); |
| 64 | RegisterFiles.emplace_back(NumPhysRegs); |
| 65 | |
| 66 | // Special case where there is no register class identifier in the set. |
| 67 | // An empty set of register classes means: this register file contains all |
| 68 | // the physical registers specified by the target. |
| 69 | if (Entries.empty()) { |
| 70 | for (std::pair<WriteState *, IndexPlusCostPairTy> &Mapping : RegisterMappings) |
| 71 | Mapping.second = std::make_pair(RegisterFileIndex, 1U); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Now update the cost of individual registers. |
| 76 | for (const MCRegisterCostEntry &RCE : Entries) { |
| 77 | const MCRegisterClass &RC = MRI.getRegClass(RCE.RegisterClassID); |
| 78 | for (const MCPhysReg Reg : RC) { |
| 79 | IndexPlusCostPairTy &Entry = RegisterMappings[Reg].second; |
| 80 | if (Entry.first) { |
| 81 | // The only register file that is allowed to overlap is the default |
| 82 | // register file at index #0. The analysis is inaccurate if register |
| 83 | // files overlap. |
| 84 | errs() << "warning: register " << MRI.getName(Reg) |
| 85 | << " defined in multiple register files."; |
| 86 | } |
| 87 | Entry.first = RegisterFileIndex; |
| 88 | Entry.second = RCE.Cost; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 93 | void RegisterFile::createNewMappings(IndexPlusCostPairTy Entry, |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 94 | MutableArrayRef<unsigned> UsedPhysRegs) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 95 | unsigned RegisterFileIndex = Entry.first; |
| 96 | unsigned Cost = Entry.second; |
| 97 | if (RegisterFileIndex) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 98 | RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 99 | RMT.NumUsedMappings += Cost; |
| 100 | UsedPhysRegs[RegisterFileIndex] += Cost; |
| 101 | } |
| 102 | |
| 103 | // Now update the default register mapping tracker. |
| 104 | RegisterFiles[0].NumUsedMappings += Cost; |
| 105 | UsedPhysRegs[0] += Cost; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 108 | void RegisterFile::removeMappings(IndexPlusCostPairTy Entry, |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 109 | MutableArrayRef<unsigned> FreedPhysRegs) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 110 | unsigned RegisterFileIndex = Entry.first; |
| 111 | unsigned Cost = Entry.second; |
| 112 | if (RegisterFileIndex) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 113 | RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 114 | RMT.NumUsedMappings -= Cost; |
| 115 | FreedPhysRegs[RegisterFileIndex] += Cost; |
| 116 | } |
| 117 | |
| 118 | // Now update the default register mapping tracker. |
| 119 | RegisterFiles[0].NumUsedMappings -= Cost; |
| 120 | FreedPhysRegs[0] += Cost; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 123 | void RegisterFile::addRegisterMapping(WriteState &WS, |
| 124 | MutableArrayRef<unsigned> UsedPhysRegs) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 125 | unsigned RegID = WS.getRegisterID(); |
| 126 | assert(RegID && "Adding an invalid register definition?"); |
| 127 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 128 | RegisterMapping &Mapping = RegisterMappings[RegID]; |
| 129 | Mapping.first = &WS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 130 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 131 | RegisterMappings[*I].first = &WS; |
| 132 | |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 133 | createNewMappings(Mapping.second, UsedPhysRegs); |
| 134 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 135 | // If this is a partial update, then we are done. |
| 136 | if (!WS.fullyUpdatesSuperRegs()) |
| 137 | return; |
| 138 | |
| 139 | for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 140 | RegisterMappings[*I].first = &WS; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 143 | void RegisterFile::invalidateRegisterMapping( |
| 144 | const WriteState &WS, MutableArrayRef<unsigned> FreedPhysRegs) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 145 | unsigned RegID = WS.getRegisterID(); |
| 146 | bool ShouldInvalidateSuperRegs = WS.fullyUpdatesSuperRegs(); |
| 147 | |
| 148 | assert(RegID != 0 && "Invalidating an already invalid register?"); |
| 149 | assert(WS.getCyclesLeft() != -512 && |
| 150 | "Invalidating a write of unknown cycles!"); |
| 151 | assert(WS.getCyclesLeft() <= 0 && "Invalid cycles left for this write!"); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 152 | RegisterMapping &Mapping = RegisterMappings[RegID]; |
| 153 | if (!Mapping.first) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 154 | return; |
| 155 | |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 156 | removeMappings(Mapping.second, FreedPhysRegs); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 157 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 158 | if (Mapping.first == &WS) |
| 159 | Mapping.first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 160 | |
| 161 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 162 | if (RegisterMappings[*I].first == &WS) |
| 163 | RegisterMappings[*I].first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 164 | |
| 165 | if (!ShouldInvalidateSuperRegs) |
| 166 | return; |
| 167 | |
| 168 | for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 169 | if (RegisterMappings[*I].first == &WS) |
| 170 | RegisterMappings[*I].first = nullptr; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 173 | void RegisterFile::collectWrites(SmallVectorImpl<WriteState *> &Writes, |
| 174 | unsigned RegID) const { |
| 175 | assert(RegID && RegID < RegisterMappings.size()); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 176 | WriteState *WS = RegisterMappings[RegID].first; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 177 | if (WS) { |
| 178 | DEBUG(dbgs() << "Found a dependent use of RegID=" << RegID << '\n'); |
| 179 | Writes.push_back(WS); |
| 180 | } |
| 181 | |
| 182 | // Handle potential partial register updates. |
| 183 | for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 184 | WS = RegisterMappings[*I].first; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 185 | if (WS && std::find(Writes.begin(), Writes.end(), WS) == Writes.end()) { |
| 186 | DEBUG(dbgs() << "Found a dependent use of subReg " << *I << " (part of " |
| 187 | << RegID << ")\n"); |
| 188 | Writes.push_back(WS); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Andrea Di Biagio | 847accd | 2018-03-20 19:06:34 +0000 | [diff] [blame] | 193 | unsigned RegisterFile::isAvailable(ArrayRef<unsigned> Regs) const { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 194 | SmallVector<unsigned, 4> NumPhysRegs(getNumRegisterFiles()); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 195 | |
| 196 | // Find how many new mappings must be created for each register file. |
| 197 | for (const unsigned RegID : Regs) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 198 | const IndexPlusCostPairTy &Entry = RegisterMappings[RegID].second; |
| 199 | if (Entry.first) |
| 200 | NumPhysRegs[Entry.first] += Entry.second; |
| 201 | NumPhysRegs[0] += Entry.second; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 204 | unsigned Response = 0; |
| 205 | for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 206 | unsigned NumRegs = NumPhysRegs[I]; |
| 207 | if (!NumRegs) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 208 | continue; |
| 209 | |
| 210 | const RegisterMappingTracker &RMT = RegisterFiles[I]; |
| 211 | if (!RMT.TotalMappings) { |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 212 | // The register file has an unbounded number of microarchitectural |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 213 | // registers. |
| 214 | continue; |
| 215 | } |
| 216 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 217 | if (RMT.TotalMappings < NumRegs) { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 218 | // The current register file is too small. This may occur if the number of |
| 219 | // microarchitectural registers in register file #0 was changed by the |
| 220 | // users via flag -reg-file-size. Alternatively, the scheduling model |
| 221 | // specified a too small number of registers for this register file. |
| 222 | report_fatal_error( |
| 223 | "Not enough microarchitectural registers in the register file"); |
| 224 | } |
| 225 | |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 226 | if (RMT.TotalMappings < (RMT.NumUsedMappings + NumRegs)) |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 227 | Response |= (1U << I); |
| 228 | } |
| 229 | |
| 230 | return Response; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | #ifndef NDEBUG |
| 234 | void RegisterFile::dump() const { |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 235 | for (unsigned I = 0, E = MRI.getNumRegs(); I < E; ++I) { |
| 236 | const RegisterMapping &RM = RegisterMappings[I]; |
Andrea Di Biagio | 9da4d6d | 2018-04-03 13:36:24 +0000 | [diff] [blame] | 237 | dbgs() << MRI.getName(I) << ", " << I << ", Map=" << RM.second.first |
| 238 | << ", "; |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 239 | if (RM.first) |
| 240 | RM.first->dump(); |
| 241 | else |
| 242 | dbgs() << "(null)\n"; |
| 243 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 244 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 245 | for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { |
| 246 | dbgs() << "Register File #" << I; |
| 247 | const RegisterMappingTracker &RMT = RegisterFiles[I]; |
| 248 | dbgs() << "\n TotalMappings: " << RMT.TotalMappings |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 249 | << "\n NumUsedMappings: " << RMT.NumUsedMappings << '\n'; |
| 250 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 251 | } |
| 252 | #endif |
| 253 | |
Andrea Di Biagio | c74ad50 | 2018-04-05 15:41:41 +0000 | [diff] [blame] | 254 | RetireControlUnit::RetireControlUnit(const llvm::MCSchedModel &SM, |
| 255 | DispatchUnit *DU) |
| 256 | : NextAvailableSlotIdx(0), CurrentInstructionSlotIdx(0), |
| 257 | AvailableSlots(SM.MicroOpBufferSize), MaxRetirePerCycle(0), Owner(DU) { |
| 258 | // Check if the scheduling model provides extra information about the machine |
| 259 | // processor. If so, then use that information to set the reorder buffer size |
| 260 | // and the maximum number of instructions retired per cycle. |
| 261 | if (SM.hasExtraProcessorInfo()) { |
| 262 | const MCExtraProcessorInfo &EPI = SM.getExtraProcessorInfo(); |
| 263 | if (EPI.ReorderBufferSize) |
| 264 | AvailableSlots = EPI.ReorderBufferSize; |
| 265 | MaxRetirePerCycle = EPI.MaxRetirePerCycle; |
| 266 | } |
| 267 | |
| 268 | assert(AvailableSlots && "Invalid reorder buffer size!"); |
| 269 | Queue.resize(AvailableSlots); |
| 270 | } |
| 271 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 272 | // Reserves a number of slots, and returns a new token. |
| 273 | unsigned RetireControlUnit::reserveSlot(unsigned Index, unsigned NumMicroOps) { |
| 274 | assert(isAvailable(NumMicroOps)); |
| 275 | unsigned NormalizedQuantity = |
| 276 | std::min(NumMicroOps, static_cast<unsigned>(Queue.size())); |
| 277 | // Zero latency instructions may have zero mOps. Artificially bump this |
| 278 | // value to 1. Although zero latency instructions don't consume scheduler |
| 279 | // resources, they still consume one slot in the retire queue. |
| 280 | NormalizedQuantity = std::max(NormalizedQuantity, 1U); |
| 281 | unsigned TokenID = NextAvailableSlotIdx; |
| 282 | Queue[NextAvailableSlotIdx] = {Index, NormalizedQuantity, false}; |
| 283 | NextAvailableSlotIdx += NormalizedQuantity; |
| 284 | NextAvailableSlotIdx %= Queue.size(); |
| 285 | AvailableSlots -= NormalizedQuantity; |
| 286 | return TokenID; |
| 287 | } |
| 288 | |
Andrea Di Biagio | 94fafdf | 2018-03-24 16:05:36 +0000 | [diff] [blame] | 289 | void DispatchUnit::notifyInstructionDispatched(unsigned Index, |
| 290 | ArrayRef<unsigned> UsedRegs) { |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 291 | DEBUG(dbgs() << "[E] Instruction Dispatched: " << Index << '\n'); |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 292 | Owner->notifyInstructionEvent(HWInstructionDispatchedEvent(Index, UsedRegs)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void DispatchUnit::notifyInstructionRetired(unsigned Index) { |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 296 | DEBUG(dbgs() << "[E] Instruction Retired: " << Index << '\n'); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 297 | const Instruction &IS = Owner->getInstruction(Index); |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 298 | SmallVector<unsigned, 4> FreedRegs(RAT->getNumRegisterFiles()); |
Andrea Di Biagio | 43e8f7d | 2018-03-21 12:49:07 +0000 | [diff] [blame] | 299 | for (const std::unique_ptr<WriteState> &WS : IS.getDefs()) |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 300 | RAT->invalidateRegisterMapping(*WS.get(), FreedRegs); |
Andrea Di Biagio | 43e8f7d | 2018-03-21 12:49:07 +0000 | [diff] [blame] | 301 | |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 302 | Owner->notifyInstructionEvent(HWInstructionRetiredEvent(Index, FreedRegs)); |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 303 | Owner->eraseInstruction(Index); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void RetireControlUnit::cycleEvent() { |
| 307 | if (isEmpty()) |
| 308 | return; |
| 309 | |
| 310 | unsigned NumRetired = 0; |
| 311 | while (!isEmpty()) { |
| 312 | if (MaxRetirePerCycle != 0 && NumRetired == MaxRetirePerCycle) |
| 313 | break; |
| 314 | RUToken &Current = Queue[CurrentInstructionSlotIdx]; |
| 315 | assert(Current.NumSlots && "Reserved zero slots?"); |
| 316 | if (!Current.Executed) |
| 317 | break; |
| 318 | Owner->notifyInstructionRetired(Current.Index); |
| 319 | CurrentInstructionSlotIdx += Current.NumSlots; |
| 320 | CurrentInstructionSlotIdx %= Queue.size(); |
| 321 | AvailableSlots += Current.NumSlots; |
| 322 | NumRetired++; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void RetireControlUnit::onInstructionExecuted(unsigned TokenID) { |
| 327 | assert(Queue.size() > TokenID); |
| 328 | assert(Queue[TokenID].Executed == false && Queue[TokenID].Index != ~0U); |
| 329 | Queue[TokenID].Executed = true; |
| 330 | } |
| 331 | |
| 332 | #ifndef NDEBUG |
| 333 | void RetireControlUnit::dump() const { |
| 334 | dbgs() << "Retire Unit: { Total Slots=" << Queue.size() |
| 335 | << ", Available Slots=" << AvailableSlots << " }\n"; |
| 336 | } |
| 337 | #endif |
| 338 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 339 | bool DispatchUnit::checkRAT(unsigned Index, const Instruction &Instr) { |
Andrea Di Biagio | 9ecb401 | 2018-03-27 15:23:41 +0000 | [diff] [blame] | 340 | SmallVector<unsigned, 4> RegDefs; |
| 341 | for (const std::unique_ptr<WriteState> &RegDef : Instr.getDefs()) |
| 342 | RegDefs.emplace_back(RegDef->getRegisterID()); |
| 343 | |
| 344 | unsigned RegisterMask = RAT->isAvailable(RegDefs); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 345 | // A mask with all zeroes means: register files are available. |
| 346 | if (RegisterMask) { |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 347 | Owner->notifyStallEvent( |
| 348 | HWStallEvent(HWStallEvent::RegisterFileStall, Index)); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | |
| 352 | return true; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 355 | bool DispatchUnit::checkRCU(unsigned Index, const InstrDesc &Desc) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 356 | unsigned NumMicroOps = Desc.NumMicroOps; |
| 357 | if (RCU->isAvailable(NumMicroOps)) |
| 358 | return true; |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 359 | Owner->notifyStallEvent( |
| 360 | HWStallEvent(HWStallEvent::RetireControlUnitStall, Index)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 361 | return false; |
| 362 | } |
| 363 | |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 364 | bool DispatchUnit::checkScheduler(unsigned Index, const InstrDesc &Desc) { |
Andrea Di Biagio | b24953b | 2018-04-11 18:05:23 +0000 | [diff] [blame] | 365 | return SC->canBeDispatched(Index, Desc); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 368 | void DispatchUnit::updateRAWDependencies(ReadState &RS, |
| 369 | const MCSubtargetInfo &STI) { |
| 370 | SmallVector<WriteState *, 4> DependentWrites; |
| 371 | |
| 372 | collectWrites(DependentWrites, RS.getRegisterID()); |
| 373 | RS.setDependentWrites(DependentWrites.size()); |
| 374 | DEBUG(dbgs() << "Found " << DependentWrites.size() << " dependent writes\n"); |
| 375 | // We know that this read depends on all the writes in DependentWrites. |
| 376 | // For each write, check if we have ReadAdvance information, and use it |
| 377 | // to figure out in how many cycles this read becomes available. |
| 378 | const ReadDescriptor &RD = RS.getDescriptor(); |
| 379 | if (!RD.HasReadAdvanceEntries) { |
| 380 | for (WriteState *WS : DependentWrites) |
| 381 | WS->addUser(&RS, /* ReadAdvance */ 0); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | const MCSchedModel &SM = STI.getSchedModel(); |
| 386 | const MCSchedClassDesc *SC = SM.getSchedClassDesc(RD.SchedClassID); |
| 387 | for (WriteState *WS : DependentWrites) { |
| 388 | unsigned WriteResID = WS->getWriteResourceID(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 389 | int ReadAdvance = STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 390 | WS->addUser(&RS, ReadAdvance); |
| 391 | } |
| 392 | // Prepare the set for another round. |
| 393 | DependentWrites.clear(); |
| 394 | } |
| 395 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 396 | void DispatchUnit::dispatch(unsigned IID, Instruction *NewInst, |
| 397 | const MCSubtargetInfo &STI) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 398 | assert(!CarryOver && "Cannot dispatch another instruction!"); |
| 399 | unsigned NumMicroOps = NewInst->getDesc().NumMicroOps; |
| 400 | if (NumMicroOps > DispatchWidth) { |
| 401 | assert(AvailableEntries == DispatchWidth); |
| 402 | AvailableEntries = 0; |
| 403 | CarryOver = NumMicroOps - DispatchWidth; |
| 404 | } else { |
| 405 | assert(AvailableEntries >= NumMicroOps); |
| 406 | AvailableEntries -= NumMicroOps; |
| 407 | } |
| 408 | |
Andrea Di Biagio | db66efc | 2018-04-25 09:38:58 +0000 | [diff] [blame^] | 409 | // Update RAW dependencies if this instruction is not a zero-latency |
| 410 | // instruction. The assumption is that a zero-latency instruction doesn't |
| 411 | // require to be issued to the scheduler for execution. More importantly, it |
| 412 | // doesn't have to wait on the register input operands. |
| 413 | if (NewInst->getDesc().MaxLatency) |
| 414 | for (std::unique_ptr<ReadState> &RS : NewInst->getUses()) |
| 415 | updateRAWDependencies(*RS, STI); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 416 | |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 417 | // Allocate new mappings. |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 418 | SmallVector<unsigned, 4> RegisterFiles(RAT->getNumRegisterFiles()); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 419 | for (std::unique_ptr<WriteState> &WS : NewInst->getDefs()) |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 420 | RAT->addRegisterMapping(*WS, RegisterFiles); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 421 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 422 | // Reserve slots in the RCU, and notify the instruction that it has been |
| 423 | // dispatched to the schedulers for execution. |
| 424 | NewInst->dispatch(RCU->reserveSlot(IID, NumMicroOps)); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 425 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 426 | // Notify listeners of the "instruction dispatched" event. |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 427 | notifyInstructionDispatched(IID, RegisterFiles); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 428 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 429 | // Now move the instruction into the scheduler's queue. |
| 430 | // The scheduler is responsible for checking if this is a zero-latency |
| 431 | // instruction that doesn't consume pipeline/scheduler resources. |
Andrea Di Biagio | 44bfcd2 | 2018-03-19 19:09:38 +0000 | [diff] [blame] | 432 | SC->scheduleInstruction(IID, *NewInst); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | #ifndef NDEBUG |
| 436 | void DispatchUnit::dump() const { |
| 437 | RAT->dump(); |
| 438 | RCU->dump(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 439 | } |
| 440 | #endif |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 441 | } // namespace mca |