1c6d43980SLemover/*************************************************************************************** 2c6d43980SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory 4c6d43980SLemover* 5c6d43980SLemover* XiangShan is licensed under Mulan PSL v2. 6c6d43980SLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 7c6d43980SLemover* You may obtain a copy of Mulan PSL v2 at: 8c6d43980SLemover* http://license.coscl.org.cn/MulanPSL2 9c6d43980SLemover* 10c6d43980SLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11c6d43980SLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12c6d43980SLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13c6d43980SLemover* 14c6d43980SLemover* See the Mulan PSL v2 for more details. 15c6d43980SLemover***************************************************************************************/ 16c6d43980SLemover 175844fcf0SLinJiaweipackage xiangshan.backend.rename 185844fcf0SLinJiawei 192225d46eSJiawei Linimport chipsalliance.rocketchip.config.Parameters 205844fcf0SLinJiaweiimport chisel3._ 215844fcf0SLinJiaweiimport chisel3.util._ 225844fcf0SLinJiaweiimport xiangshan._ 237cef916fSYinan Xuimport utils._ 24588ceab5SYinan Xuimport xiangshan.backend.roq.RoqPtr 25049559e7SYinan Xuimport xiangshan.backend.dispatch.PreDispatchInfo 265844fcf0SLinJiawei 272225d46eSJiawei Linclass RenameBypassInfo(implicit p: Parameters) extends XSBundle { 2899b8dc2cSYinan Xu val lsrc1_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))) 2999b8dc2cSYinan Xu val lsrc2_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))) 3099b8dc2cSYinan Xu val lsrc3_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))) 3199b8dc2cSYinan Xu val ldest_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))) 3299b8dc2cSYinan Xu} 3399b8dc2cSYinan Xu 3439d3280eSYikeZhouclass Rename(implicit p: Parameters) extends XSModule { 355844fcf0SLinJiawei val io = IO(new Bundle() { 365844fcf0SLinJiawei val redirect = Flipped(ValidIO(new Redirect)) 372d7c7105SYinan Xu val flush = Input(Bool()) 3821e7a6c5SYinan Xu val roqCommits = Flipped(new RoqCommitIO) 3957c4f8d6SLinJiawei // from decode buffer 409a2e6b8aSLinJiawei val in = Vec(RenameWidth, Flipped(DecoupledIO(new CfCtrl))) 4157c4f8d6SLinJiawei // to dispatch1 429a2e6b8aSLinJiawei val out = Vec(RenameWidth, DecoupledIO(new MicroOp)) 4399b8dc2cSYinan Xu val renameBypass = Output(new RenameBypassInfo) 44049559e7SYinan Xu val dispatchInfo = Output(new PreDispatchInfo) 458b8e745dSYikeZhou // for debug printing 462225d46eSJiawei Lin val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 472225d46eSJiawei Lin val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 485844fcf0SLinJiawei }) 49b034d3b9SLinJiawei 508b8e745dSYikeZhou // create free list and rat 5139d3280eSYikeZhou val intFreeList = Module(if (EnableIntMoveElim) new freelist.MEFreeList else new freelist.StdFreeList) 5239d3280eSYikeZhou val fpFreeList = Module(new freelist.StdFreeList) 538b8e745dSYikeZhou 548b8e745dSYikeZhou val intRat = Module(new RenameTable(float = false)) 558b8e745dSYikeZhou val fpRat = Module(new RenameTable(float = true)) 568b8e745dSYikeZhou 578b8e745dSYikeZhou // connect flush and redirect ports for rat 588b8e745dSYikeZhou Seq(intRat, fpRat) foreach { case rat => 598b8e745dSYikeZhou rat.io.redirect := io.redirect.valid 608b8e745dSYikeZhou rat.io.flush := io.flush 618b8e745dSYikeZhou rat.io.walkWen := io.roqCommits.isWalk 622e9d39e0SLinJiawei } 632e9d39e0SLinJiawei 648b8e745dSYikeZhou // decide if given instruction needs allocating a new physical register (CfCtrl: from decode; RoqCommitInfo: from roq) 65b034d3b9SLinJiawei def needDestReg[T <: CfCtrl](fp: Boolean, x: T): Bool = { 66b034d3b9SLinJiawei {if(fp) x.ctrl.fpWen else x.ctrl.rfWen && (x.ctrl.ldest =/= 0.U)} 67b034d3b9SLinJiawei } 68fe6452fcSYinan Xu def needDestRegCommit[T <: RoqCommitInfo](fp: Boolean, x: T): Bool = { 69fe6452fcSYinan Xu {if(fp) x.fpWen else x.rfWen && (x.ldest =/= 0.U)} 70fe6452fcSYinan Xu } 718b8e745dSYikeZhou 725eb4af5bSYikeZhou // connect [flush + redirect + walk] ports for __float point__ & __integer__ free list 735eb4af5bSYikeZhou Seq((fpFreeList, true), (intFreeList, false)).foreach{ case (fl, isFp) => 745eb4af5bSYikeZhou fl.flush := io.flush 755eb4af5bSYikeZhou fl.redirect := io.redirect.valid 765eb4af5bSYikeZhou fl.walk := io.roqCommits.isWalk 775eb4af5bSYikeZhou // when isWalk, use stepBack to restore head pointer of free list 785eb4af5bSYikeZhou // (if ME enabled, stepBack of intFreeList should be useless thus optimized out) 795eb4af5bSYikeZhou fl.stepBack := PopCount(io.roqCommits.valid.zip(io.roqCommits.info).map{case (v, i) => v && needDestRegCommit(isFp, i)}) 804efb89cbSYikeZhou } 815eb4af5bSYikeZhou // walk has higher priority than allocation and thus we don't use isWalk here 825eb4af5bSYikeZhou // only when both fp and int free list and dispatch1 has enough space can we do allocation 834efb89cbSYikeZhou intFreeList.doAllocate := fpFreeList.canAllocate && io.out(0).ready 844efb89cbSYikeZhou fpFreeList.doAllocate := intFreeList.canAllocate && io.out(0).ready 855eb4af5bSYikeZhou 865eb4af5bSYikeZhou // dispatch1 ready ++ float point free list ready ++ int free list ready ++ not walk 875eb4af5bSYikeZhou val canOut = io.out(0).ready && fpFreeList.canAllocate && intFreeList.canAllocate && !io.roqCommits.isWalk 885eb4af5bSYikeZhou 89b034d3b9SLinJiawei 90588ceab5SYinan Xu // speculatively assign the instruction with an roqIdx 918b8e745dSYikeZhou val validCount = PopCount(io.in.map(_.valid)) // number of instructions waiting to enter roq (from decode) 92588ceab5SYinan Xu val roqIdxHead = RegInit(0.U.asTypeOf(new RoqPtr)) 938f77f081SYinan Xu val lastCycleMisprediction = RegNext(io.redirect.valid && !io.redirect.bits.flushItself()) 948b8e745dSYikeZhou val roqIdxHeadNext = Mux(io.flush, 0.U.asTypeOf(new RoqPtr), // flush: clear roq 958b8e745dSYikeZhou Mux(io.redirect.valid, io.redirect.bits.roqIdx, // redirect: move ptr to given roq index (flush itself) 968b8e745dSYikeZhou Mux(lastCycleMisprediction, roqIdxHead + 1.U, // mis-predict: not flush roqIdx itself 978b8e745dSYikeZhou Mux(canOut, roqIdxHead + validCount, // instructions successfully entered next stage: increase roqIdx 988b8e745dSYikeZhou /* default */ roqIdxHead)))) // no instructions passed by this cycle: stick to old value 99588ceab5SYinan Xu roqIdxHead := roqIdxHeadNext 100588ceab5SYinan Xu 1018b8e745dSYikeZhou 10200ad41d0SYinan Xu /** 10300ad41d0SYinan Xu * Rename: allocate free physical register and update rename table 10400ad41d0SYinan Xu */ 105b034d3b9SLinJiawei val uops = Wire(Vec(RenameWidth, new MicroOp)) 106b034d3b9SLinJiawei uops.foreach( uop => { 10720e31bd1SYinan Xu uop.srcState(0) := DontCare 10820e31bd1SYinan Xu uop.srcState(1) := DontCare 10920e31bd1SYinan Xu uop.srcState(2) := DontCare 110b034d3b9SLinJiawei uop.roqIdx := DontCare 1116ae7ac7cSAllen uop.diffTestDebugLrScValid := DontCare 1127cef916fSYinan Xu uop.debugInfo := DontCare 113bc86598fSWilliam Wang uop.lqIdx := DontCare 114bc86598fSWilliam Wang uop.sqIdx := DontCare 115b034d3b9SLinJiawei }) 116b034d3b9SLinJiawei 11799b8dc2cSYinan Xu val needFpDest = Wire(Vec(RenameWidth, Bool())) 11899b8dc2cSYinan Xu val needIntDest = Wire(Vec(RenameWidth, Bool())) 119b424051cSYinan Xu val hasValid = Cat(io.in.map(_.valid)).orR 1208b8e745dSYikeZhou 1218b8e745dSYikeZhou val isMove = io.in.map(_.bits.ctrl.isMove) 12239d3280eSYikeZhou val isMax = if (EnableIntMoveElim) Some(intFreeList.asInstanceOf[freelist.MEFreeList].maxVec) else None 1238b8e745dSYikeZhou val meEnable = WireInit(VecInit(Seq.fill(RenameWidth)(false.B))) 1248b8e745dSYikeZhou val psrc_cmp = Wire(MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))) 1258b8e745dSYikeZhou 1268b8e745dSYikeZhou val intSpecWen = Wire(Vec(RenameWidth, Bool())) 1278b8e745dSYikeZhou val fpSpecWen = Wire(Vec(RenameWidth, Bool())) 1288b8e745dSYikeZhou 1298b8e745dSYikeZhou // uop calculation 130b034d3b9SLinJiawei for (i <- 0 until RenameWidth) { 131b034d3b9SLinJiawei uops(i).cf := io.in(i).bits.cf 132b034d3b9SLinJiawei uops(i).ctrl := io.in(i).bits.ctrl 133b034d3b9SLinJiawei 134567096a6Slinjiawei val inValid = io.in(i).valid 1352dcb2daaSLinJiawei 136b034d3b9SLinJiawei // alloc a new phy reg 13799b8dc2cSYinan Xu needFpDest(i) := inValid && needDestReg(fp = true, io.in(i).bits) 13899b8dc2cSYinan Xu needIntDest(i) := inValid && needDestReg(fp = false, io.in(i).bits) 13939d3280eSYikeZhou fpFreeList.allocateReq(i) := needFpDest(i) 14039d3280eSYikeZhou intFreeList.allocateReq(i) := needIntDest(i) 1412438f9ebSYinan Xu 1428b8e745dSYikeZhou // no valid instruction from decode stage || all resources (dispatch1 + both free lists) ready 143b424051cSYinan Xu io.in(i).ready := !hasValid || canOut 14458e06390SLinJiawei 145c7054babSLinJiawei // do checkpoints when a branch inst come 1464f787118SYinan Xu // for(fl <- Seq(fpFreeList, intFreeList)){ 1474f787118SYinan Xu // fl.cpReqs(i).valid := inValid 1484f787118SYinan Xu // fl.cpReqs(i).bits := io.in(i).bits.brTag 1494f787118SYinan Xu // } 15058e06390SLinJiawei 15188825c5cSYinan Xu uops(i).roqIdx := roqIdxHead + PopCount(io.in.take(i).map(_.valid)) 152588ceab5SYinan Xu 153b034d3b9SLinJiawei // read rename table 154b034d3b9SLinJiawei def readRat(lsrcList: List[UInt], ldest: UInt, fp: Boolean) = { 155b034d3b9SLinJiawei val rat = if(fp) fpRat else intRat 156b034d3b9SLinJiawei val srcCnt = lsrcList.size 157b034d3b9SLinJiawei val psrcVec = Wire(Vec(srcCnt, UInt(PhyRegIdxWidth.W))) 158b034d3b9SLinJiawei val old_pdest = Wire(UInt(PhyRegIdxWidth.W)) 159b034d3b9SLinJiawei for(k <- 0 until srcCnt+1){ 160b034d3b9SLinJiawei val rportIdx = i * (srcCnt+1) + k 161b034d3b9SLinJiawei if(k != srcCnt){ 1628b8e745dSYikeZhou rat.io.readPorts(rportIdx).addr := lsrcList(k) 1638b8e745dSYikeZhou psrcVec(k) := rat.io.readPorts(rportIdx).rdata 164b034d3b9SLinJiawei } else { 1658b8e745dSYikeZhou rat.io.readPorts(rportIdx).addr := ldest 1668b8e745dSYikeZhou old_pdest := rat.io.readPorts(rportIdx).rdata 167b034d3b9SLinJiawei } 168b034d3b9SLinJiawei } 169b034d3b9SLinJiawei (psrcVec, old_pdest) 170b034d3b9SLinJiawei } 17120e31bd1SYinan Xu val lsrcList = List(uops(i).ctrl.lsrc(0), uops(i).ctrl.lsrc(1), uops(i).ctrl.lsrc(2)) 172b034d3b9SLinJiawei val ldest = uops(i).ctrl.ldest 173b034d3b9SLinJiawei val (intPhySrcVec, intOldPdest) = readRat(lsrcList.take(2), ldest, fp = false) 174b034d3b9SLinJiawei val (fpPhySrcVec, fpOldPdest) = readRat(lsrcList, ldest, fp = true) 17520e31bd1SYinan Xu uops(i).psrc(0) := Mux(uops(i).ctrl.srcType(0) === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0)) 17620e31bd1SYinan Xu uops(i).psrc(1) := Mux(uops(i).ctrl.srcType(1) === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1)) 17720e31bd1SYinan Xu uops(i).psrc(2) := fpPhySrcVec(2) 178b034d3b9SLinJiawei uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest) 1798b8e745dSYikeZhou 1805eb4af5bSYikeZhou if (EnableIntMoveElim) { 1815eb4af5bSYikeZhou 1828b8e745dSYikeZhou if (i == 0) { 1838b8e745dSYikeZhou // calculate meEnable 18431ebfb1dSYikeZhou meEnable(i) := isMove(i) && (!isMax.get(uops(i).psrc(0)) || uops(i).ctrl.lsrc(0) === 0.U) 1858b8e745dSYikeZhou } else { 1868b8e745dSYikeZhou // compare psrc0 1878b8e745dSYikeZhou psrc_cmp(i-1) := Cat((0 until i).map(j => { 188d3975becSYikeZhou uops(i).psrc(0) === uops(j).psrc(0) && io.in(i).bits.ctrl.isMove && io.in(j).bits.ctrl.isMove 1898b8e745dSYikeZhou }) /* reverse is not necessary here */) 1908b8e745dSYikeZhou 1918b8e745dSYikeZhou // calculate meEnable 19231ebfb1dSYikeZhou meEnable(i) := isMove(i) && (!(io.renameBypass.lsrc1_bypass(i-1).orR | psrc_cmp(i-1).orR | isMax.get(uops(i).psrc(0))) || uops(i).ctrl.lsrc(0) === 0.U) 1938b8e745dSYikeZhou } 19473c4359eSYikeZhou uops(i).eliminatedMove := meEnable(i) || (uops(i).ctrl.isMove && uops(i).ctrl.ldest === 0.U) 1958b8e745dSYikeZhou 1968b8e745dSYikeZhou // send psrc of eliminated move instructions to free list and label them as eliminated 1978b8e745dSYikeZhou when (meEnable(i)) { 19839d3280eSYikeZhou intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).valid := true.B 19939d3280eSYikeZhou intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).bits := uops(i).psrc(0) 2008b8e745dSYikeZhou XSInfo(io.in(i).valid && io.out(i).valid, p"Move instruction ${Hexadecimal(io.in(i).bits.cf.pc)} eliminated successfully! psrc:${uops(i).psrc(0)}\n") 2018b8e745dSYikeZhou } .otherwise { 20239d3280eSYikeZhou intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).valid := false.B 20339d3280eSYikeZhou intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).bits := DontCare 2048b8e745dSYikeZhou XSInfo(io.in(i).valid && io.out(i).valid && isMove(i), p"Move instruction ${Hexadecimal(io.in(i).bits.cf.pc)} failed to be eliminated! psrc:${uops(i).psrc(0)}\n") 2058b8e745dSYikeZhou } 2068b8e745dSYikeZhou 2078b8e745dSYikeZhou // update pdest 2088b8e745dSYikeZhou uops(i).pdest := Mux(meEnable(i), uops(i).psrc(0), // move eliminated 20939d3280eSYikeZhou Mux(needIntDest(i), intFreeList.allocatePhyReg(i), // normal int inst 2108b8e745dSYikeZhou Mux(uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen, 0.U // int inst with dst=r0 21139d3280eSYikeZhou /* default */, fpFreeList.allocatePhyReg(i)))) // normal fp inst 2125eb4af5bSYikeZhou } else { 2135eb4af5bSYikeZhou uops(i).eliminatedMove := DontCare 2145eb4af5bSYikeZhou psrc_cmp.foreach(_ := DontCare) 2155eb4af5bSYikeZhou // update pdest 2165eb4af5bSYikeZhou uops(i).pdest := Mux(needIntDest(i), intFreeList.allocatePhyReg(i), // normal int inst 2175eb4af5bSYikeZhou Mux(uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen, 0.U // int inst with dst=r0 2185eb4af5bSYikeZhou /* default */, fpFreeList.allocatePhyReg(i))) // normal fp inst 2195eb4af5bSYikeZhou } 2208b8e745dSYikeZhou 221*ebb8ebf8SYinan Xu // Assign performance counters 222*ebb8ebf8SYinan Xu uops(i).debugInfo.renameTime := GTimer() 223*ebb8ebf8SYinan Xu 224*ebb8ebf8SYinan Xu io.out(i).valid := io.in(i).valid && intFreeList.canAllocate && fpFreeList.canAllocate && !io.roqCommits.isWalk 225*ebb8ebf8SYinan Xu io.out(i).bits := uops(i) 226*ebb8ebf8SYinan Xu 2278b8e745dSYikeZhou // write speculative rename table 22839d3280eSYikeZhou // we update rat later inside commit code 22939d3280eSYikeZhou intSpecWen(i) := intFreeList.allocateReq(i) && intFreeList.canAllocate && intFreeList.doAllocate && !io.roqCommits.isWalk 23039d3280eSYikeZhou fpSpecWen(i) := fpFreeList.allocateReq(i) && fpFreeList.canAllocate && fpFreeList.doAllocate && !io.roqCommits.isWalk 231b034d3b9SLinJiawei } 232b034d3b9SLinJiawei 23399b8dc2cSYinan Xu // We don't bypass the old_pdest from valid instructions with the same ldest currently in rename stage. 2348b8e745dSYikeZhou // Instead, we determine whether there're some dependencies between the valid instructions. 23599b8dc2cSYinan Xu for (i <- 1 until RenameWidth) { 23699b8dc2cSYinan Xu io.renameBypass.lsrc1_bypass(i-1) := Cat((0 until i).map(j => { 23720e31bd1SYinan Xu val fpMatch = needFpDest(j) && io.in(i).bits.ctrl.srcType(0) === SrcType.fp 23820e31bd1SYinan Xu val intMatch = needIntDest(j) && io.in(i).bits.ctrl.srcType(0) === SrcType.reg 23920e31bd1SYinan Xu (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc(0) 24099b8dc2cSYinan Xu }).reverse) 24199b8dc2cSYinan Xu io.renameBypass.lsrc2_bypass(i-1) := Cat((0 until i).map(j => { 24220e31bd1SYinan Xu val fpMatch = needFpDest(j) && io.in(i).bits.ctrl.srcType(1) === SrcType.fp 24320e31bd1SYinan Xu val intMatch = needIntDest(j) && io.in(i).bits.ctrl.srcType(1) === SrcType.reg 24420e31bd1SYinan Xu (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc(1) 24599b8dc2cSYinan Xu }).reverse) 24699b8dc2cSYinan Xu io.renameBypass.lsrc3_bypass(i-1) := Cat((0 until i).map(j => { 24720e31bd1SYinan Xu val fpMatch = needFpDest(j) && io.in(i).bits.ctrl.srcType(2) === SrcType.fp 24820e31bd1SYinan Xu val intMatch = needIntDest(j) && io.in(i).bits.ctrl.srcType(2) === SrcType.reg 24920e31bd1SYinan Xu (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc(2) 25099b8dc2cSYinan Xu }).reverse) 25199b8dc2cSYinan Xu io.renameBypass.ldest_bypass(i-1) := Cat((0 until i).map(j => { 25299b8dc2cSYinan Xu val fpMatch = needFpDest(j) && needFpDest(i) 25399b8dc2cSYinan Xu val intMatch = needIntDest(j) && needIntDest(i) 25499b8dc2cSYinan Xu (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.ldest 25599b8dc2cSYinan Xu }).reverse) 256b034d3b9SLinJiawei } 25700ad41d0SYinan Xu 2588b8e745dSYikeZhou // calculate lsq space requirement 259049559e7SYinan Xu val isLs = VecInit(uops.map(uop => FuType.isLoadStore(uop.ctrl.fuType))) 260049559e7SYinan Xu val isStore = VecInit(uops.map(uop => FuType.isStoreExu(uop.ctrl.fuType))) 261049559e7SYinan Xu val isAMO = VecInit(uops.map(uop => FuType.isAMO(uop.ctrl.fuType))) 262049559e7SYinan Xu io.dispatchInfo.lsqNeedAlloc := VecInit((0 until RenameWidth).map(i => 263049559e7SYinan Xu Mux(isLs(i), Mux(isStore(i) && !isAMO(i), 2.U, 1.U), 0.U))) 264049559e7SYinan Xu 26500ad41d0SYinan Xu /** 26600ad41d0SYinan Xu * Instructions commit: update freelist and rename table 26700ad41d0SYinan Xu */ 26800ad41d0SYinan Xu for (i <- 0 until CommitWidth) { 26900ad41d0SYinan Xu 2708b8e745dSYikeZhou Seq((intRat, false), (fpRat, true)) foreach { case (rat, fp) => 2718b8e745dSYikeZhou // is valid commit req and given instruction has destination register 27200ad41d0SYinan Xu val commitDestValid = io.roqCommits.valid(i) && needDestRegCommit(fp, io.roqCommits.info(i)) 2738b8e745dSYikeZhou XSDebug(p"isFp[${fp}]index[$i]-commitDestValid:$commitDestValid,isWalk:${io.roqCommits.isWalk}\n") 2748b8e745dSYikeZhou 2758b8e745dSYikeZhou /* 2768b8e745dSYikeZhou I. RAT Update 2778b8e745dSYikeZhou */ 2788b8e745dSYikeZhou 2798b8e745dSYikeZhou // walk back write - restore spec state : ldest => old_pdest 2808b8e745dSYikeZhou if (fp && i < RenameWidth) { 2818b8e745dSYikeZhou rat.io.specWritePorts(i).wen := (commitDestValid && io.roqCommits.isWalk) || fpSpecWen(i) 2828b8e745dSYikeZhou rat.io.specWritePorts(i).addr := Mux(fpSpecWen(i), uops(i).ctrl.ldest, io.roqCommits.info(i).ldest) 28339d3280eSYikeZhou rat.io.specWritePorts(i).wdata := Mux(fpSpecWen(i), fpFreeList.allocatePhyReg(i), io.roqCommits.info(i).old_pdest) 2848b8e745dSYikeZhou } else if (!fp && i < RenameWidth) { 2858b8e745dSYikeZhou rat.io.specWritePorts(i).wen := (commitDestValid && io.roqCommits.isWalk) || intSpecWen(i) 2868b8e745dSYikeZhou rat.io.specWritePorts(i).addr := Mux(intSpecWen(i), uops(i).ctrl.ldest, io.roqCommits.info(i).ldest) 28739d3280eSYikeZhou if (EnableIntMoveElim) { 28839d3280eSYikeZhou rat.io.specWritePorts(i).wdata := 28939d3280eSYikeZhou Mux(intSpecWen(i), Mux(meEnable(i), uops(i).psrc(0), intFreeList.allocatePhyReg(i)), io.roqCommits.info(i).old_pdest) 29039d3280eSYikeZhou } else { 29139d3280eSYikeZhou rat.io.specWritePorts(i).wdata := 29239d3280eSYikeZhou Mux(intSpecWen(i), intFreeList.allocatePhyReg(i), io.roqCommits.info(i).old_pdest) 29339d3280eSYikeZhou } 29439d3280eSYikeZhou // when i >= RenameWidth, this write must happens during WALK process 29539d3280eSYikeZhou } else if (i >= RenameWidth) { 2968b8e745dSYikeZhou rat.io.specWritePorts(i).wen := commitDestValid && io.roqCommits.isWalk 2978b8e745dSYikeZhou rat.io.specWritePorts(i).addr := io.roqCommits.info(i).ldest 2988b8e745dSYikeZhou rat.io.specWritePorts(i).wdata := io.roqCommits.info(i).old_pdest 2998b8e745dSYikeZhou } 30000ad41d0SYinan Xu 30100ad41d0SYinan Xu when (commitDestValid && io.roqCommits.isWalk) { 3028b8e745dSYikeZhou XSInfo({if(fp) p"[fp" else p"[int"} + p" walk] " + 3038b8e745dSYikeZhou p"ldest:${rat.io.specWritePorts(i).addr} -> old_pdest:${rat.io.specWritePorts(i).wdata}\n") 30400ad41d0SYinan Xu } 30500ad41d0SYinan Xu 3068b8e745dSYikeZhou // normal write - update arch state (serve as initialization) 3078b8e745dSYikeZhou rat.io.archWritePorts(i).wen := commitDestValid && !io.roqCommits.isWalk 3088b8e745dSYikeZhou rat.io.archWritePorts(i).addr := io.roqCommits.info(i).ldest 3098b8e745dSYikeZhou rat.io.archWritePorts(i).wdata := io.roqCommits.info(i).pdest 31000ad41d0SYinan Xu 3118b8e745dSYikeZhou XSInfo(rat.io.archWritePorts(i).wen, 3128b8e745dSYikeZhou {if(fp) p"[fp" else p"[int"} + p" arch rat update] ldest:${rat.io.archWritePorts(i).addr} ->" + 3138b8e745dSYikeZhou p" pdest:${rat.io.archWritePorts(i).wdata}\n" 31400ad41d0SYinan Xu ) 31500ad41d0SYinan Xu 3168b8e745dSYikeZhou 3178b8e745dSYikeZhou /* 3188b8e745dSYikeZhou II. Free List Update 3198b8e745dSYikeZhou */ 3208b8e745dSYikeZhou 3218b8e745dSYikeZhou if (fp) { // Float Point free list 32239d3280eSYikeZhou fpFreeList.freeReq(i) := commitDestValid && !io.roqCommits.isWalk 32339d3280eSYikeZhou fpFreeList.freePhyReg(i) := io.roqCommits.info(i).old_pdest 32439d3280eSYikeZhou } else if (EnableIntMoveElim) { // Integer free list 3258b8e745dSYikeZhou 3268b8e745dSYikeZhou // during walk process: 3278b8e745dSYikeZhou // 1. for normal inst, free pdest + revert rat from ldest->pdest to ldest->old_pdest 3288b8e745dSYikeZhou // 2. for ME inst, free pdest(commit counter++) + revert rat 3298b8e745dSYikeZhou 3308b8e745dSYikeZhou // conclusion: 3318b8e745dSYikeZhou // a. rat recovery has nothing to do with ME or not 3328b8e745dSYikeZhou // b. treat walk as normal commit except replace old_pdests with pdests and set io.walk to true 3338b8e745dSYikeZhou // c. ignore pdests port when walking 3348b8e745dSYikeZhou 33539d3280eSYikeZhou intFreeList.freeReq(i) := commitDestValid // walk or not walk 33639d3280eSYikeZhou intFreeList.freePhyReg(i) := Mux(io.roqCommits.isWalk, io.roqCommits.info(i).pdest, io.roqCommits.info(i).old_pdest) 33739d3280eSYikeZhou intFreeList.asInstanceOf[freelist.MEFreeList].eliminatedMove(i) := io.roqCommits.info(i).eliminatedMove 33839d3280eSYikeZhou intFreeList.asInstanceOf[freelist.MEFreeList].multiRefPhyReg(i) := io.roqCommits.info(i).pdest 33939d3280eSYikeZhou } else { 34039d3280eSYikeZhou intFreeList.freeReq(i) := commitDestValid && !io.roqCommits.isWalk 34139d3280eSYikeZhou intFreeList.freePhyReg(i) := io.roqCommits.info(i).old_pdest 34200ad41d0SYinan Xu } 34300ad41d0SYinan Xu } 3448b8e745dSYikeZhou } 3458b8e745dSYikeZhou 3468b8e745dSYikeZhou 3478b8e745dSYikeZhou /* 3488b8e745dSYikeZhou Debug and performance counter 3498b8e745dSYikeZhou */ 3508b8e745dSYikeZhou 3518b8e745dSYikeZhou def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = { 3528b8e745dSYikeZhou XSInfo( 3538b8e745dSYikeZhou in.valid && in.ready, 3548b8e745dSYikeZhou p"pc:${Hexadecimal(in.bits.cf.pc)} in v:${in.valid} in rdy:${in.ready} " + 3558b8e745dSYikeZhou p"lsrc(0):${in.bits.ctrl.lsrc(0)} -> psrc(0):${out.bits.psrc(0)} " + 3568b8e745dSYikeZhou p"lsrc(1):${in.bits.ctrl.lsrc(1)} -> psrc(1):${out.bits.psrc(1)} " + 3578b8e745dSYikeZhou p"lsrc(2):${in.bits.ctrl.lsrc(2)} -> psrc(2):${out.bits.psrc(2)} " + 3588b8e745dSYikeZhou p"ldest:${in.bits.ctrl.ldest} -> pdest:${out.bits.pdest} " + 3598b8e745dSYikeZhou p"old_pdest:${out.bits.old_pdest} " + 3608b8e745dSYikeZhou p"out v:${out.valid} r:${out.ready}\n" 3618b8e745dSYikeZhou ) 3628b8e745dSYikeZhou } 3638b8e745dSYikeZhou 3648b8e745dSYikeZhou for((x,y) <- io.in.zip(io.out)){ 3658b8e745dSYikeZhou printRenameInfo(x, y) 3668b8e745dSYikeZhou } 3678b8e745dSYikeZhou 3688b8e745dSYikeZhou XSDebug(io.roqCommits.isWalk, p"Walk Recovery Enabled\n") 3698b8e745dSYikeZhou XSDebug(io.roqCommits.isWalk, p"validVec:${Binary(io.roqCommits.valid.asUInt)}\n") 3708b8e745dSYikeZhou for (i <- 0 until CommitWidth) { 3718b8e745dSYikeZhou val info = io.roqCommits.info(i) 3728b8e745dSYikeZhou XSDebug(io.roqCommits.isWalk && io.roqCommits.valid(i), p"[#$i walk info] pc:${Hexadecimal(info.pc)} " + 3735eb4af5bSYikeZhou p"ldest:${info.ldest} rfWen:${info.rfWen} fpWen:${info.fpWen} " + { if (EnableIntMoveElim) p"eliminatedMove:${info.eliminatedMove} " else p"" } + 3748b8e745dSYikeZhou p"pdest:${info.pdest} old_pdest:${info.old_pdest}\n") 3758b8e745dSYikeZhou } 3768b8e745dSYikeZhou 3778b8e745dSYikeZhou XSDebug(p"inValidVec: ${Binary(Cat(io.in.map(_.valid)))}\n") 37839d3280eSYikeZhou XSInfo(!canOut, p"stall at rename, hasValid:${hasValid}, fpCanAlloc:${fpFreeList.canAllocate}, intCanAlloc:${intFreeList.canAllocate} dispatch1ready:${io.out(0).ready}, isWalk:${io.roqCommits.isWalk}\n") 3798b8e745dSYikeZhou 3808b8e745dSYikeZhou intRat.io.debug_rdata <> io.debug_int_rat 3818b8e745dSYikeZhou fpRat.io.debug_rdata <> io.debug_fp_rat 3828b8e745dSYikeZhou 3838b8e745dSYikeZhou XSDebug(p"Arch Int RAT:" + io.debug_int_rat.zipWithIndex.map{ case (r, i) => p"#$i:$r " }.reduceLeft(_ + _) + p"\n") 384d479a3a8SYinan Xu 385408a32b7SAllen XSPerfAccumulate("in", Mux(RegNext(io.in(0).ready), PopCount(io.in.map(_.valid)), 0.U)) 386408a32b7SAllen XSPerfAccumulate("utilization", PopCount(io.in.map(_.valid))) 387408a32b7SAllen XSPerfAccumulate("waitInstr", PopCount((0 until RenameWidth).map(i => io.in(i).valid && !io.in(i).ready))) 38839d3280eSYikeZhou XSPerfAccumulate("stall_cycle_dispatch", hasValid && !io.out(0).ready && fpFreeList.canAllocate && intFreeList.canAllocate && !io.roqCommits.isWalk) 38939d3280eSYikeZhou XSPerfAccumulate("stall_cycle_fp", hasValid && io.out(0).ready && !fpFreeList.canAllocate && intFreeList.canAllocate && !io.roqCommits.isWalk) 39039d3280eSYikeZhou XSPerfAccumulate("stall_cycle_int", hasValid && io.out(0).ready && fpFreeList.canAllocate && !intFreeList.canAllocate && !io.roqCommits.isWalk) 39139d3280eSYikeZhou XSPerfAccumulate("stall_cycle_walk", hasValid && io.out(0).ready && fpFreeList.canAllocate && intFreeList.canAllocate && io.roqCommits.isWalk) 3921a2cf152SYinan Xu if (!env.FPGAPlatform) { 3931a2cf152SYinan Xu ExcitingUtils.addSource(io.roqCommits.isWalk, "TMA_backendiswalk") 3941a2cf152SYinan Xu } 3955eb4af5bSYikeZhou 3965eb4af5bSYikeZhou if (EnableIntMoveElim) { 397d3975becSYikeZhou XSPerfAccumulate("move_instr_count", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove))) 398d3975becSYikeZhou XSPerfAccumulate("move_elim_enabled", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && meEnable(i)))) 399d3975becSYikeZhou XSPerfAccumulate("move_elim_cancelled", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i)))) 400d3975becSYikeZhou XSPerfAccumulate("move_elim_cancelled_psrc_bypass", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && { if (i == 0) false.B else io.renameBypass.lsrc1_bypass(i-1).orR }))) 40139d3280eSYikeZhou XSPerfAccumulate("move_elim_cancelled_cnt_limit", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && isMax.get(io.out(i).bits.psrc(0))))) 402d3975becSYikeZhou XSPerfAccumulate("move_elim_cancelled_inc_more_than_one", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && { if (i == 0) false.B else psrc_cmp(i-1).orR }))) 403d3975becSYikeZhou 404d3975becSYikeZhou // to make sure meEnable functions as expected 405d3975becSYikeZhou for (i <- 0 until RenameWidth) { 40639d3280eSYikeZhou XSDebug(io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && isMax.get(io.out(i).bits.psrc(0)), 407d3975becSYikeZhou p"ME_CANCELLED: ref counter hits max value (pc:0x${Hexadecimal(io.in(i).bits.cf.pc)})\n") 408d3975becSYikeZhou XSDebug(io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && { if (i == 0) false.B else io.renameBypass.lsrc1_bypass(i-1).orR }, 409d3975becSYikeZhou p"ME_CANCELLED: RAW dependency (pc:0x${Hexadecimal(io.in(i).bits.cf.pc)})\n") 410d3975becSYikeZhou XSDebug(io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && { if (i == 0) false.B else psrc_cmp(i-1).orR }, 411d3975becSYikeZhou p"ME_CANCELLED: psrc duplicates with former instruction (pc:0x${Hexadecimal(io.in(i).bits.cf.pc)})\n") 412d3975becSYikeZhou } 413d3975becSYikeZhou XSDebug(VecInit(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i))).asUInt().orR, 414d3975becSYikeZhou p"ME_CANCELLED: pc group [ " + (0 until RenameWidth).map(i => p"fire:${io.out(i).fire()},pc:0x${Hexadecimal(io.in(i).bits.cf.pc)} ").reduceLeft(_ + _) + p"]\n") 4155eb4af5bSYikeZhou XSInfo(meEnable.asUInt().orR(), p"meEnableVec:${Binary(meEnable.asUInt)}\n") 4165eb4af5bSYikeZhou } 417b034d3b9SLinJiawei} 418