1package xiangshan.backend.rename 2 3import chisel3._ 4import chisel3.util._ 5import xiangshan._ 6import utils.XSInfo 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 = Vec(CommitWidth, Flipped(ValidIO(new RoqCommit))) 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 fpFreeList, intFreeList = Module(new FreeList).io 44 val fpRat = Module(new RenameTable(float = true)).io 45 val intRat = Module(new RenameTable(float = false)).io 46 47 fpFreeList.redirect := io.redirect 48 intFreeList.redirect := io.redirect 49 50 val flush = io.redirect.valid && (io.redirect.bits.isException || io.redirect.bits.isFlushPipe) // TODO: need check by JiaWei 51 fpRat.flush := flush 52 intRat.flush := flush 53 54 def needDestReg[T <: CfCtrl](fp: Boolean, x: T): Bool = { 55 {if(fp) x.ctrl.fpWen else x.ctrl.rfWen && (x.ctrl.ldest =/= 0.U)} 56 } 57 58 val uops = Wire(Vec(RenameWidth, new MicroOp)) 59 60 uops.foreach( uop => { 61// uop.brMask := DontCare 62// uop.brTag := DontCare 63 uop.src1State := DontCare 64 uop.src2State := DontCare 65 uop.src3State := DontCare 66 uop.roqIdx := DontCare 67 uop.diffTestDebugLrScValid := DontCare 68 uop.lqIdx := DontCare 69 uop.sqIdx := DontCare 70 }) 71 72 val needFpDest = Wire(Vec(RenameWidth, Bool())) 73 val needIntDest = Wire(Vec(RenameWidth, Bool())) 74 var lastReady = WireInit(io.out(0).ready) 75 // debug assert 76 val outRdy = Cat(io.out.map(_.ready)) 77 assert(outRdy===0.U || outRdy.andR()) 78 for(i <- 0 until RenameWidth) { 79 uops(i).cf := io.in(i).bits.cf 80 uops(i).ctrl := io.in(i).bits.ctrl 81 uops(i).brTag := io.in(i).bits.brTag 82 83 val inValid = io.in(i).valid 84 85 // alloc a new phy reg 86 needFpDest(i) := inValid && needDestReg(fp = true, io.in(i).bits) 87 needIntDest(i) := inValid && needDestReg(fp = false, io.in(i).bits) 88 fpFreeList.allocReqs(i) := needFpDest(i) && lastReady 89 intFreeList.allocReqs(i) := needIntDest(i) && lastReady 90 val fpCanAlloc = fpFreeList.canAlloc(i) 91 val intCanAlloc = intFreeList.canAlloc(i) 92 val this_can_alloc = Mux( 93 needIntDest(i), 94 intCanAlloc, 95 Mux( 96 needFpDest(i), 97 fpCanAlloc, 98 true.B 99 ) 100 ) 101 io.in(i).ready := lastReady && this_can_alloc 102 103 // do checkpoints when a branch inst come 104 for(fl <- Seq(fpFreeList, intFreeList)){ 105 fl.cpReqs(i).valid := inValid 106 fl.cpReqs(i).bits := io.in(i).bits.brTag 107 } 108 109 lastReady = io.in(i).ready 110 111 uops(i).pdest := Mux(needIntDest(i), 112 intFreeList.pdests(i), 113 Mux( 114 uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen, 115 0.U, fpFreeList.pdests(i) 116 ) 117 ) 118 119 io.out(i).valid := io.in(i).fire() 120 io.out(i).bits := uops(i) 121 122 // write rename table 123 def writeRat(fp: Boolean) = { 124 val rat = if(fp) fpRat else intRat 125 val freeList = if(fp) fpFreeList else intFreeList 126 // speculative inst write 127 val specWen = freeList.allocReqs(i) && freeList.canAlloc(i) 128 // walk back write 129 val commitDestValid = io.roqCommits(i).valid && needDestReg(fp, io.roqCommits(i).bits.uop) 130 val walkWen = commitDestValid && io.roqCommits(i).bits.isWalk 131 132 rat.specWritePorts(i).wen := specWen || walkWen 133 rat.specWritePorts(i).addr := Mux(specWen, uops(i).ctrl.ldest, io.roqCommits(i).bits.uop.ctrl.ldest) 134 rat.specWritePorts(i).wdata := Mux(specWen, freeList.pdests(i), io.roqCommits(i).bits.uop.old_pdest) 135 136 XSInfo(walkWen, 137 {if(fp) p"fp" else p"int "} + p"walk: pc:${Hexadecimal(io.roqCommits(i).bits.uop.cf.pc)}" + 138 p" ldest:${rat.specWritePorts(i).addr} old_pdest:${rat.specWritePorts(i).wdata}\n" 139 ) 140 141 rat.archWritePorts(i).wen := commitDestValid && !io.roqCommits(i).bits.isWalk 142 rat.archWritePorts(i).addr := io.roqCommits(i).bits.uop.ctrl.ldest 143 rat.archWritePorts(i).wdata := io.roqCommits(i).bits.uop.pdest 144 145 XSInfo(rat.archWritePorts(i).wen, 146 {if(fp) p"fp" else p"int "} + p" rat arch: ldest:${rat.archWritePorts(i).addr}" + 147 p" pdest:${rat.archWritePorts(i).wdata}\n" 148 ) 149 150 freeList.deallocReqs(i) := rat.archWritePorts(i).wen 151 freeList.deallocPregs(i) := io.roqCommits(i).bits.uop.old_pdest 152 153 } 154 155 writeRat(fp = false) 156 writeRat(fp = true) 157 158 // read rename table 159 def readRat(lsrcList: List[UInt], ldest: UInt, fp: Boolean) = { 160 val rat = if(fp) fpRat else intRat 161 val srcCnt = lsrcList.size 162 val psrcVec = Wire(Vec(srcCnt, UInt(PhyRegIdxWidth.W))) 163 val old_pdest = Wire(UInt(PhyRegIdxWidth.W)) 164 for(k <- 0 until srcCnt+1){ 165 val rportIdx = i * (srcCnt+1) + k 166 if(k != srcCnt){ 167 rat.readPorts(rportIdx).addr := lsrcList(k) 168 psrcVec(k) := rat.readPorts(rportIdx).rdata 169 } else { 170 rat.readPorts(rportIdx).addr := ldest 171 old_pdest := rat.readPorts(rportIdx).rdata 172 } 173 } 174 (psrcVec, old_pdest) 175 } 176 val lsrcList = List(uops(i).ctrl.lsrc1, uops(i).ctrl.lsrc2, uops(i).ctrl.lsrc3) 177 val ldest = uops(i).ctrl.ldest 178 val (intPhySrcVec, intOldPdest) = readRat(lsrcList.take(2), ldest, fp = false) 179 val (fpPhySrcVec, fpOldPdest) = readRat(lsrcList, ldest, fp = true) 180 uops(i).psrc1 := Mux(uops(i).ctrl.src1Type === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0)) 181 uops(i).psrc2 := Mux(uops(i).ctrl.src2Type === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1)) 182 uops(i).psrc3 := fpPhySrcVec(2) 183 uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest) 184 } 185 186 // We don't bypass the old_pdest from valid instructions with the same ldest currently in rename stage. 187 // Instead, we determine whether there're some dependences between the valid instructions. 188 for (i <- 1 until RenameWidth) { 189 io.renameBypass.lsrc1_bypass(i-1) := Cat((0 until i).map(j => { 190 val fpMatch = needFpDest(j) && io.in(i).bits.ctrl.src1Type === SrcType.fp 191 val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src1Type === SrcType.reg 192 (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc1 193 }).reverse) 194 io.renameBypass.lsrc2_bypass(i-1) := Cat((0 until i).map(j => { 195 val fpMatch = needFpDest(j) && io.in(i).bits.ctrl.src2Type === SrcType.fp 196 val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src2Type === SrcType.reg 197 (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc2 198 }).reverse) 199 io.renameBypass.lsrc3_bypass(i-1) := Cat((0 until i).map(j => { 200 val fpMatch = needFpDest(j) && io.in(i).bits.ctrl.src3Type === SrcType.fp 201 val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src3Type === SrcType.reg 202 (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc3 203 }).reverse) 204 io.renameBypass.ldest_bypass(i-1) := Cat((0 until i).map(j => { 205 val fpMatch = needFpDest(j) && needFpDest(i) 206 val intMatch = needIntDest(j) && needIntDest(i) 207 (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.ldest 208 }).reverse) 209 } 210} 211