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