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 | /// |
Matt Davis | b7972f8 | 2018-05-16 17:07:08 +0000 | [diff] [blame] | 11 | /// This file implements methods declared by the DispatchUnit class. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Dispatch.h" |
| 16 | #include "Backend.h" |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 17 | #include "HWEventListener.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 18 | #include "Scheduler.h" |
| 19 | #include "llvm/Support/Debug.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | #define DEBUG_TYPE "llvm-mca" |
| 24 | |
| 25 | namespace mca { |
| 26 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 27 | void DispatchUnit::notifyInstructionDispatched(const InstRef &IR, |
Andrea Di Biagio | 94fafdf | 2018-03-24 16:05:36 +0000 | [diff] [blame] | 28 | ArrayRef<unsigned> UsedRegs) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 29 | LLVM_DEBUG(dbgs() << "[E] Instruction Dispatched: " << IR << '\n'); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 30 | Owner->notifyInstructionEvent(HWInstructionDispatchedEvent(IR, UsedRegs)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 33 | void DispatchUnit::notifyInstructionRetired(const InstRef &IR) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 34 | LLVM_DEBUG(dbgs() << "[E] Instruction Retired: " << IR << '\n'); |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 35 | SmallVector<unsigned, 4> FreedRegs(RAT->getNumRegisterFiles()); |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 36 | const InstrDesc &Desc = IR.getInstruction()->getDesc(); |
| 37 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 38 | for (const std::unique_ptr<WriteState> &WS : IR.getInstruction()->getDefs()) |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 39 | RAT->removeRegisterWrite(*WS.get(), FreedRegs, !Desc.isZeroLatency()); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 40 | Owner->notifyInstructionEvent(HWInstructionRetiredEvent(IR, FreedRegs)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 43 | bool DispatchUnit::checkRAT(const InstRef &IR) { |
Andrea Di Biagio | 9ecb401 | 2018-03-27 15:23:41 +0000 | [diff] [blame] | 44 | SmallVector<unsigned, 4> RegDefs; |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 45 | for (const std::unique_ptr<WriteState> &RegDef : |
| 46 | IR.getInstruction()->getDefs()) |
Andrea Di Biagio | 9ecb401 | 2018-03-27 15:23:41 +0000 | [diff] [blame] | 47 | RegDefs.emplace_back(RegDef->getRegisterID()); |
| 48 | |
| 49 | unsigned RegisterMask = RAT->isAvailable(RegDefs); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 50 | // A mask with all zeroes means: register files are available. |
| 51 | if (RegisterMask) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 52 | Owner->notifyStallEvent(HWStallEvent(HWStallEvent::RegisterFileStall, IR)); |
Andrea Di Biagio | e64f3b1 | 2018-03-18 15:33:27 +0000 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | |
| 56 | return true; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 59 | bool DispatchUnit::checkRCU(const InstRef &IR) { |
| 60 | const unsigned NumMicroOps = IR.getInstruction()->getDesc().NumMicroOps; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 61 | if (RCU->isAvailable(NumMicroOps)) |
| 62 | return true; |
Andrea Di Biagio | 91ab2ee | 2018-03-19 13:23:07 +0000 | [diff] [blame] | 63 | Owner->notifyStallEvent( |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 64 | HWStallEvent(HWStallEvent::RetireControlUnitStall, IR)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 68 | bool DispatchUnit::checkScheduler(const InstRef &IR) { |
| 69 | return SC->canBeDispatched(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 72 | void DispatchUnit::updateRAWDependencies(ReadState &RS, |
| 73 | const MCSubtargetInfo &STI) { |
| 74 | SmallVector<WriteState *, 4> DependentWrites; |
| 75 | |
| 76 | collectWrites(DependentWrites, RS.getRegisterID()); |
| 77 | RS.setDependentWrites(DependentWrites.size()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 78 | LLVM_DEBUG(dbgs() << "Found " << DependentWrites.size() |
| 79 | << " dependent writes\n"); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 80 | // We know that this read depends on all the writes in DependentWrites. |
| 81 | // For each write, check if we have ReadAdvance information, and use it |
| 82 | // to figure out in how many cycles this read becomes available. |
| 83 | const ReadDescriptor &RD = RS.getDescriptor(); |
| 84 | if (!RD.HasReadAdvanceEntries) { |
| 85 | for (WriteState *WS : DependentWrites) |
| 86 | WS->addUser(&RS, /* ReadAdvance */ 0); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | const MCSchedModel &SM = STI.getSchedModel(); |
| 91 | const MCSchedClassDesc *SC = SM.getSchedClassDesc(RD.SchedClassID); |
| 92 | for (WriteState *WS : DependentWrites) { |
| 93 | unsigned WriteResID = WS->getWriteResourceID(); |
Andrea Di Biagio | 0a837ef | 2018-03-29 14:26:56 +0000 | [diff] [blame] | 94 | int ReadAdvance = STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 95 | WS->addUser(&RS, ReadAdvance); |
| 96 | } |
| 97 | // Prepare the set for another round. |
| 98 | DependentWrites.clear(); |
| 99 | } |
| 100 | |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 101 | void DispatchUnit::dispatch(InstRef IR, const MCSubtargetInfo &STI) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 102 | assert(!CarryOver && "Cannot dispatch another instruction!"); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 103 | Instruction &IS = *IR.getInstruction(); |
| 104 | const InstrDesc &Desc = IS.getDesc(); |
| 105 | const unsigned NumMicroOps = Desc.NumMicroOps; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 106 | if (NumMicroOps > DispatchWidth) { |
| 107 | assert(AvailableEntries == DispatchWidth); |
| 108 | AvailableEntries = 0; |
| 109 | CarryOver = NumMicroOps - DispatchWidth; |
| 110 | } else { |
| 111 | assert(AvailableEntries >= NumMicroOps); |
| 112 | AvailableEntries -= NumMicroOps; |
| 113 | } |
| 114 | |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 115 | // A dependency-breaking instruction doesn't have to wait on the register |
| 116 | // input operands, and it is often optimized at register renaming stage. |
| 117 | // Update RAW dependencies if this instruction is not a dependency-breaking |
| 118 | // instruction. A dependency-breaking instruction is a zero-latency |
| 119 | // instruction that doesn't consume hardware resources. |
| 120 | // An example of dependency-breaking instruction on X86 is a zero-idiom XOR. |
| 121 | if (!Desc.isZeroLatency()) |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 122 | for (std::unique_ptr<ReadState> &RS : IS.getUses()) |
Andrea Di Biagio | db66efc | 2018-04-25 09:38:58 +0000 | [diff] [blame] | 123 | updateRAWDependencies(*RS, STI); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 124 | |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 125 | // By default, a dependency-breaking zero-latency instruction is expected to |
| 126 | // be optimized at register renaming stage. That means, no physical register |
| 127 | // is allocated to the instruction. |
Andrea Di Biagio | 12ef526 | 2018-03-21 18:11:05 +0000 | [diff] [blame] | 128 | SmallVector<unsigned, 4> RegisterFiles(RAT->getNumRegisterFiles()); |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 129 | for (std::unique_ptr<WriteState> &WS : IS.getDefs()) |
Andrea Di Biagio | 8ea3a34 | 2018-05-14 15:08:22 +0000 | [diff] [blame] | 130 | RAT->addRegisterWrite(*WS, RegisterFiles, !Desc.isZeroLatency()); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 131 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 132 | // Reserve slots in the RCU, and notify the instruction that it has been |
| 133 | // dispatched to the schedulers for execution. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 134 | IS.dispatch(RCU->reserveSlot(IR, NumMicroOps)); |
Andrea Di Biagio | 4732d43ca | 2018-03-14 14:57:23 +0000 | [diff] [blame] | 135 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 136 | // Notify listeners of the "instruction dispatched" event. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 137 | notifyInstructionDispatched(IR, RegisterFiles); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 138 | |
Andrea Di Biagio | 09ea09e | 2018-03-22 11:39:34 +0000 | [diff] [blame] | 139 | // Now move the instruction into the scheduler's queue. |
| 140 | // The scheduler is responsible for checking if this is a zero-latency |
| 141 | // instruction that doesn't consume pipeline/scheduler resources. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 142 | SC->scheduleInstruction(IR); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | #ifndef NDEBUG |
| 146 | void DispatchUnit::dump() const { |
| 147 | RAT->dump(); |
| 148 | RCU->dump(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 149 | } |
| 150 | #endif |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 151 | } // namespace mca |