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