1package xiangshan.backend.rename 2 3import chisel3._ 4import chisel3.util._ 5import xiangshan._ 6import utils._ 7import xiangshan.backend.roq.RoqPtr 8 9class RenameBypassInfo extends XSBundle { 10 val lsrc1_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))) 11 val lsrc2_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))) 12 val lsrc3_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))) 13 val ldest_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))) 14} 15 16class Rename extends XSModule with HasCircularQueuePtrHelper { 17 val io = IO(new Bundle() { 18 val redirect = Flipped(ValidIO(new Redirect)) 19 val roqCommits = Flipped(new RoqCommitIO) 20 // from decode buffer 21 val in = Vec(RenameWidth, Flipped(DecoupledIO(new CfCtrl))) 22 // to dispatch1 23 val out = Vec(RenameWidth, DecoupledIO(new MicroOp)) 24 val renameBypass = Output(new RenameBypassInfo) 25 }) 26 27 def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = { 28 XSInfo( 29 in.valid && in.ready, 30 p"pc:${Hexadecimal(in.bits.cf.pc)} in v:${in.valid} in rdy:${in.ready} " + 31 p"lsrc1:${in.bits.ctrl.lsrc1} -> psrc1:${out.bits.psrc1} " + 32 p"lsrc2:${in.bits.ctrl.lsrc2} -> psrc2:${out.bits.psrc2} " + 33 p"lsrc3:${in.bits.ctrl.lsrc3} -> psrc3:${out.bits.psrc3} " + 34 p"ldest:${in.bits.ctrl.ldest} -> pdest:${out.bits.pdest} " + 35 p"old_pdest:${out.bits.old_pdest} " + 36 p"out v:${out.valid} r:${out.ready}\n" 37 ) 38 } 39 40 for((x,y) <- io.in.zip(io.out)){ 41 printRenameInfo(x, y) 42 } 43 44 val intFreeList, fpFreeList = Module(new FreeList).io 45 val intRat = Module(new RenameTable(float = false)).io 46 val fpRat = Module(new RenameTable(float = true)).io 47 val allPhyResource = Seq((intRat, intFreeList, false), (fpRat, fpFreeList, true)) 48 49 allPhyResource.map{ case (rat, freelist, _) => 50 rat.redirect := io.redirect 51 rat.walkWen := io.roqCommits.isWalk 52 freelist.redirect := io.redirect 53 freelist.walk.valid := io.roqCommits.isWalk 54 } 55 val canOut = io.out(0).ready && fpFreeList.req.canAlloc && intFreeList.req.canAlloc && !io.roqCommits.isWalk 56 57 def needDestReg[T <: CfCtrl](fp: Boolean, x: T): Bool = { 58 {if(fp) x.ctrl.fpWen else x.ctrl.rfWen && (x.ctrl.ldest =/= 0.U)} 59 } 60 def needDestRegCommit[T <: RoqCommitInfo](fp: Boolean, x: T): Bool = { 61 {if(fp) x.fpWen else x.rfWen && (x.ldest =/= 0.U)} 62 } 63 fpFreeList.walk.bits := PopCount(io.roqCommits.valid.zip(io.roqCommits.info).map{case (v, i) => v && needDestRegCommit(true, i)}) 64 intFreeList.walk.bits := PopCount(io.roqCommits.valid.zip(io.roqCommits.info).map{case (v, i) => v && needDestRegCommit(false, i)}) 65 // walk has higher priority than allocation and thus we don't use isWalk here 66 fpFreeList.req.doAlloc := intFreeList.req.canAlloc && io.out(0).ready 67 intFreeList.req.doAlloc := fpFreeList.req.canAlloc && io.out(0).ready 68 69 // speculatively assign the instruction with an roqIdx 70 val validCount = PopCount(io.in.map(_.valid)) 71 val roqIdxHead = RegInit(0.U.asTypeOf(new RoqPtr)) 72 val lastCycleMisprediction = RegNext(io.redirect.valid && !io.redirect.bits.isUnconditional() && !io.redirect.bits.flushItself()) 73 val roqIdxHeadNext = Mux(io.redirect.valid, 74 Mux(io.redirect.bits.isUnconditional(), 0.U.asTypeOf(new RoqPtr), io.redirect.bits.roqIdx), 75 Mux(lastCycleMisprediction, roqIdxHead + 1.U, Mux(canOut, roqIdxHead + validCount, roqIdxHead)) 76 ) 77 roqIdxHead := roqIdxHeadNext 78 79 /** 80 * Rename: allocate free physical register and update rename table 81 */ 82 val uops = Wire(Vec(RenameWidth, new MicroOp)) 83 84 uops.foreach( uop => { 85// uop.brMask := DontCare 86// uop.brTag := DontCare 87 uop.src1State := DontCare 88 uop.src2State := DontCare 89 uop.src3State := DontCare 90 uop.roqIdx := DontCare 91 uop.diffTestDebugLrScValid := DontCare 92 uop.debugInfo := DontCare 93 uop.lqIdx := DontCare 94 uop.sqIdx := DontCare 95 }) 96 97 val needFpDest = Wire(Vec(RenameWidth, Bool())) 98 val needIntDest = Wire(Vec(RenameWidth, Bool())) 99 val hasValid = Cat(io.in.map(_.valid)).orR 100 for (i <- 0 until RenameWidth) { 101 uops(i).cf := io.in(i).bits.cf 102 uops(i).ctrl := io.in(i).bits.ctrl 103 104 val inValid = io.in(i).valid 105 106 // alloc a new phy reg 107 needFpDest(i) := inValid && needDestReg(fp = true, io.in(i).bits) 108 needIntDest(i) := inValid && needDestReg(fp = false, io.in(i).bits) 109 fpFreeList.req.allocReqs(i) := needFpDest(i) 110 intFreeList.req.allocReqs(i) := needIntDest(i) 111 112 io.in(i).ready := !hasValid || canOut 113 114 // do checkpoints when a branch inst come 115 // for(fl <- Seq(fpFreeList, intFreeList)){ 116 // fl.cpReqs(i).valid := inValid 117 // fl.cpReqs(i).bits := io.in(i).bits.brTag 118 // } 119 120 uops(i).pdest := Mux(needIntDest(i), 121 intFreeList.req.pdests(i), 122 Mux( 123 uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen, 124 0.U, fpFreeList.req.pdests(i) 125 ) 126 ) 127 128 uops(i).roqIdx := roqIdxHead + i.U 129 130 io.out(i).valid := io.in(i).valid && intFreeList.req.canAlloc && fpFreeList.req.canAlloc && !io.roqCommits.isWalk 131 io.out(i).bits := uops(i) 132 133 // write speculative rename table 134 allPhyResource.map{ case (rat, freelist, _) => 135 val specWen = freelist.req.allocReqs(i) && freelist.req.canAlloc && freelist.req.doAlloc && !io.roqCommits.isWalk 136 137 rat.specWritePorts(i).wen := specWen 138 rat.specWritePorts(i).addr := uops(i).ctrl.ldest 139 rat.specWritePorts(i).wdata := freelist.req.pdests(i) 140 141 freelist.deallocReqs(i) := specWen 142 } 143 144 // read rename table 145 def readRat(lsrcList: List[UInt], ldest: UInt, fp: Boolean) = { 146 val rat = if(fp) fpRat else intRat 147 val srcCnt = lsrcList.size 148 val psrcVec = Wire(Vec(srcCnt, UInt(PhyRegIdxWidth.W))) 149 val old_pdest = Wire(UInt(PhyRegIdxWidth.W)) 150 for(k <- 0 until srcCnt+1){ 151 val rportIdx = i * (srcCnt+1) + k 152 if(k != srcCnt){ 153 rat.readPorts(rportIdx).addr := lsrcList(k) 154 psrcVec(k) := rat.readPorts(rportIdx).rdata 155 } else { 156 rat.readPorts(rportIdx).addr := ldest 157 old_pdest := rat.readPorts(rportIdx).rdata 158 } 159 } 160 (psrcVec, old_pdest) 161 } 162 val lsrcList = List(uops(i).ctrl.lsrc1, uops(i).ctrl.lsrc2, uops(i).ctrl.lsrc3) 163 val ldest = uops(i).ctrl.ldest 164 val (intPhySrcVec, intOldPdest) = readRat(lsrcList.take(2), ldest, fp = false) 165 val (fpPhySrcVec, fpOldPdest) = readRat(lsrcList, ldest, fp = true) 166 uops(i).psrc1 := Mux(uops(i).ctrl.src1Type === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0)) 167 uops(i).psrc2 := Mux(uops(i).ctrl.src2Type === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1)) 168 uops(i).psrc3 := fpPhySrcVec(2) 169 uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest) 170 } 171 172 // We don't bypass the old_pdest from valid instructions with the same ldest currently in rename stage. 173 // Instead, we determine whether there're some dependences between the valid instructions. 174 for (i <- 1 until RenameWidth) { 175 io.renameBypass.lsrc1_bypass(i-1) := Cat((0 until i).map(j => { 176 val fpMatch = needFpDest(j) && io.in(i).bits.ctrl.src1Type === SrcType.fp 177 val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src1Type === SrcType.reg 178 (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc1 179 }).reverse) 180 io.renameBypass.lsrc2_bypass(i-1) := Cat((0 until i).map(j => { 181 val fpMatch = needFpDest(j) && io.in(i).bits.ctrl.src2Type === SrcType.fp 182 val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src2Type === SrcType.reg 183 (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc2 184 }).reverse) 185 io.renameBypass.lsrc3_bypass(i-1) := Cat((0 until i).map(j => { 186 val fpMatch = needFpDest(j) && io.in(i).bits.ctrl.src3Type === SrcType.fp 187 val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src3Type === SrcType.reg 188 (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc3 189 }).reverse) 190 io.renameBypass.ldest_bypass(i-1) := Cat((0 until i).map(j => { 191 val fpMatch = needFpDest(j) && needFpDest(i) 192 val intMatch = needIntDest(j) && needIntDest(i) 193 (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.ldest 194 }).reverse) 195 } 196 197 /** 198 * Instructions commit: update freelist and rename table 199 */ 200 for (i <- 0 until CommitWidth) { 201 if (i >= RenameWidth) { 202 allPhyResource.map{ case (rat, _, _) => 203 rat.specWritePorts(i).wen := false.B 204 rat.specWritePorts(i).addr := DontCare 205 rat.specWritePorts(i).wdata := DontCare 206 } 207 } 208 209 allPhyResource.map{ case (rat, freelist, fp) => 210 // walk back write 211 val commitDestValid = io.roqCommits.valid(i) && needDestRegCommit(fp, io.roqCommits.info(i)) 212 213 when (commitDestValid && io.roqCommits.isWalk) { 214 rat.specWritePorts(i).wen := true.B 215 rat.specWritePorts(i).addr := io.roqCommits.info(i).ldest 216 rat.specWritePorts(i).wdata := io.roqCommits.info(i).old_pdest 217 XSInfo({if(fp) p"fp" else p"int "} + p"walk: " + 218 p" ldest:${rat.specWritePorts(i).addr} old_pdest:${rat.specWritePorts(i).wdata}\n") 219 } 220 221 rat.archWritePorts(i).wen := commitDestValid && !io.roqCommits.isWalk 222 rat.archWritePorts(i).addr := io.roqCommits.info(i).ldest 223 rat.archWritePorts(i).wdata := io.roqCommits.info(i).pdest 224 225 XSInfo(rat.archWritePorts(i).wen, 226 {if(fp) p"fp" else p"int "} + p" rat arch: ldest:${rat.archWritePorts(i).addr}" + 227 p" pdest:${rat.archWritePorts(i).wdata}\n" 228 ) 229 230 freelist.deallocReqs(i) := rat.archWritePorts(i).wen 231 freelist.deallocPregs(i) := io.roqCommits.info(i).old_pdest 232 } 233 } 234} 235