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 198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 205844fcf0SLinJiaweiimport chisel3._ 215844fcf0SLinJiaweiimport chisel3.util._ 223c02ee8fSwakafaimport utility._ 233b739f49SXuan Huimport utils._ 243b739f49SXuan Huimport xiangshan._ 2589cc69c1STang Haojinimport xiangshan.backend.Bundles.{DecodedInst, DynInst} 26765e58c6Ssinsanctionimport xiangshan.backend.decode.{FusionDecodeInfo, ImmUnion, Imm_I, Imm_LUI_LOAD, Imm_U} 27730cfbc0SXuan Huimport xiangshan.backend.fu.FuType 2870224bf6SYinan Xuimport xiangshan.backend.rename.freelist._ 29c3f16425Sxiaofeibao-xjtuimport xiangshan.backend.rob.{RobEnqIO, RobPtr} 30980c1bc3SWilliam Wangimport xiangshan.mem.mdp._ 318daac0bfSxiaofeibao-xjtuimport xiangshan.ExceptionNO._ 3299b8dc2cSYinan Xu 33ccfddc82SHaojin Tangclass Rename(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper with HasPerfEvents { 34d6f9198fSXuan Hu 35d6f9198fSXuan Hu // params alias 3698639abbSXuan Hu private val numRegSrc = backendParams.numRegSrc 37d6f9198fSXuan Hu private val numVecRegSrc = backendParams.numVecRegSrc 385718c384SHaojin Tang private val numVecRatPorts = numVecRegSrc 3998639abbSXuan Hu 4098639abbSXuan Hu println(s"[Rename] numRegSrc: $numRegSrc") 4198639abbSXuan Hu 425844fcf0SLinJiawei val io = IO(new Bundle() { 435844fcf0SLinJiawei val redirect = Flipped(ValidIO(new Redirect)) 446b102a39SHaojin Tang val rabCommits = Input(new RabCommitIO) 457fa2c198SYinan Xu // from decode 463b739f49SXuan Hu val in = Vec(RenameWidth, Flipped(DecoupledIO(new DecodedInst))) 47a0db5a4bSYinan Xu val fusionInfo = Vec(DecodeWidth - 1, Flipped(new FusionDecodeInfo)) 48980c1bc3SWilliam Wang // ssit read result 49980c1bc3SWilliam Wang val ssit = Flipped(Vec(RenameWidth, Output(new SSITEntry))) 50980c1bc3SWilliam Wang // waittable read result 51980c1bc3SWilliam Wang val waittable = Flipped(Vec(RenameWidth, Output(Bool()))) 527fa2c198SYinan Xu // to rename table 535718c384SHaojin Tang val intReadPorts = Vec(RenameWidth, Vec(2, Input(UInt(PhyRegIdxWidth.W)))) 545718c384SHaojin Tang val fpReadPorts = Vec(RenameWidth, Vec(3, Input(UInt(PhyRegIdxWidth.W)))) 55d6f9198fSXuan Hu val vecReadPorts = Vec(RenameWidth, Vec(numVecRatPorts, Input(UInt(PhyRegIdxWidth.W)))) 56*368cbcecSxiaofeibao val v0ReadPorts = Vec(RenameWidth, Vec(1, Input(UInt(PhyRegIdxWidth.W)))) 57*368cbcecSxiaofeibao val vlReadPorts = Vec(RenameWidth, Vec(1, Input(UInt(PhyRegIdxWidth.W)))) 587fa2c198SYinan Xu val intRenamePorts = Vec(RenameWidth, Output(new RatWritePort)) 597fa2c198SYinan Xu val fpRenamePorts = Vec(RenameWidth, Output(new RatWritePort)) 60deb6421eSHaojin Tang val vecRenamePorts = Vec(RenameWidth, Output(new RatWritePort)) 61*368cbcecSxiaofeibao val v0RenamePorts = Vec(RenameWidth, Output(new RatWritePort)) 62*368cbcecSxiaofeibao val vlRenamePorts = Vec(RenameWidth, Output(new RatWritePort)) 63dcf3a679STang Haojin // from rename table 64780712aaSxiaofeibao-xjtu val int_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W))) 65780712aaSxiaofeibao-xjtu val fp_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W))) 66780712aaSxiaofeibao-xjtu val vec_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W))) 67*368cbcecSxiaofeibao val v0_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W))) 68*368cbcecSxiaofeibao val vl_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W))) 69780712aaSxiaofeibao-xjtu val int_need_free = Vec(RabCommitWidth, Input(Bool())) 7057c4f8d6SLinJiawei // to dispatch1 713b739f49SXuan Hu val out = Vec(RenameWidth, DecoupledIO(new DynInst)) 72fa7f2c26STang Haojin // for snapshots 73fa7f2c26STang Haojin val snpt = Input(new SnapshotPort) 74c4b56310SHaojin Tang val snptLastEnq = Flipped(ValidIO(new RobPtr)) 75bb7e6e3aSxiaofeibao-xjtu val snptIsFull= Input(Bool()) 76ccfddc82SHaojin Tang // debug arch ports 77b7d9e8d5Sxiaofeibao-xjtu val debug_int_rat = if (backendParams.debugEn) Some(Vec(32, Input(UInt(PhyRegIdxWidth.W)))) else None 78b7d9e8d5Sxiaofeibao-xjtu val debug_fp_rat = if (backendParams.debugEn) Some(Vec(32, Input(UInt(PhyRegIdxWidth.W)))) else None 79*368cbcecSxiaofeibao val debug_vec_rat = if (backendParams.debugEn) Some(Vec(31, Input(UInt(PhyRegIdxWidth.W)))) else None 80*368cbcecSxiaofeibao val debug_v0_rat = if (backendParams.debugEn) Some(Input(UInt(PhyRegIdxWidth.W))) else None 81*368cbcecSxiaofeibao val debug_vl_rat = if (backendParams.debugEn) Some(Input(UInt(PhyRegIdxWidth.W))) else None 82d2b20d1aSTang Haojin // perf only 83d2b20d1aSTang Haojin val stallReason = new Bundle { 84d2b20d1aSTang Haojin val in = Flipped(new StallReasonIO(RenameWidth)) 85d2b20d1aSTang Haojin val out = new StallReasonIO(RenameWidth) 86d2b20d1aSTang Haojin } 875844fcf0SLinJiawei }) 88b034d3b9SLinJiawei 896374b1d6SXuan Hu // io alias 906374b1d6SXuan Hu private val dispatchCanAcc = io.out.head.ready 916374b1d6SXuan Hu 9289cc69c1STang Haojin val compressUnit = Module(new CompressUnit()) 938b8e745dSYikeZhou // create free list and rat 9439c59369SXuan Hu val intFreeList = Module(new MEFreeList(IntPhyRegs)) 954eebf274Ssinsanction val fpFreeList = Module(new StdFreeList(FpPhyRegs - FpLogicRegs, FpLogicRegs, Reg_F)) 964eebf274Ssinsanction val vecFreeList = Module(new StdFreeList(VfPhyRegs - VecLogicRegs, VecLogicRegs, Reg_V)) 97*368cbcecSxiaofeibao// val v0FreeList = Module(new StdFreeList(V0PhyRegs - V0LogicRegs, V0LogicRegs, Reg_V0)) 98*368cbcecSxiaofeibao// val vlFreeList = Module(new StdFreeList(VlPhyRegs - VlLogicRegs, VlLogicRegs, Reg_Vl)) 99*368cbcecSxiaofeibao val v0FreeList = Module(new StdFreeList(21, 1, Reg_V0)) 100*368cbcecSxiaofeibao val vlFreeList = Module(new StdFreeList(31, 2, Reg_Vl)) 101*368cbcecSxiaofeibao 1028b8e745dSYikeZhou 1036b102a39SHaojin Tang intFreeList.io.commit <> io.rabCommits 104b7d9e8d5Sxiaofeibao-xjtu intFreeList.io.debug_rat.foreach(_ <> io.debug_int_rat.get) 1056b102a39SHaojin Tang fpFreeList.io.commit <> io.rabCommits 106b7d9e8d5Sxiaofeibao-xjtu fpFreeList.io.debug_rat.foreach(_ <> io.debug_fp_rat.get) 1074eebf274Ssinsanction vecFreeList.io.commit <> io.rabCommits 1084eebf274Ssinsanction vecFreeList.io.debug_rat.foreach(_ <> io.debug_vec_rat.get) 109*368cbcecSxiaofeibao v0FreeList.io.commit <> io.rabCommits 110*368cbcecSxiaofeibao v0FreeList.io.debug_rat.foreach(_ <> io.debug_v0_rat.get) 111*368cbcecSxiaofeibao vlFreeList.io.commit <> io.rabCommits 112*368cbcecSxiaofeibao vlFreeList.io.debug_rat.foreach(_ <> io.debug_vl_rat.get) 113ccfddc82SHaojin Tang 1149aca92b9SYinan Xu // decide if given instruction needs allocating a new physical register (CfCtrl: from decode; RobCommitInfo: from rob) 1153b739f49SXuan Hu def needDestReg[T <: DecodedInst](reg_t: RegType, x: T): Bool = reg_t match { 1163b739f49SXuan Hu case Reg_I => x.rfWen && x.ldest =/= 0.U 1173b739f49SXuan Hu case Reg_F => x.fpWen 1183b739f49SXuan Hu case Reg_V => x.vecWen 119*368cbcecSxiaofeibao case Reg_V0 => x.v0Wen 120*368cbcecSxiaofeibao case Reg_Vl => x.vlWen 121b034d3b9SLinJiawei } 1226b102a39SHaojin Tang def needDestRegCommit[T <: RabCommitInfo](reg_t: RegType, x: T): Bool = { 1233b739f49SXuan Hu reg_t match { 1243b739f49SXuan Hu case Reg_I => x.rfWen 1253b739f49SXuan Hu case Reg_F => x.fpWen 1263b739f49SXuan Hu case Reg_V => x.vecWen 127*368cbcecSxiaofeibao case Reg_V0 => x.v0Wen 128*368cbcecSxiaofeibao case Reg_Vl => x.vlWen 129fe6452fcSYinan Xu } 130deb6421eSHaojin Tang } 1316b102a39SHaojin Tang def needDestRegWalk[T <: RabCommitInfo](reg_t: RegType, x: T): Bool = { 1323b739f49SXuan Hu reg_t match { 1333b739f49SXuan Hu case Reg_I => x.rfWen && x.ldest =/= 0.U 1343b739f49SXuan Hu case Reg_F => x.fpWen 1353b739f49SXuan Hu case Reg_V => x.vecWen 136*368cbcecSxiaofeibao case Reg_V0 => x.v0Wen 137*368cbcecSxiaofeibao case Reg_Vl => x.vlWen 1383b739f49SXuan Hu } 139ccfddc82SHaojin Tang } 1408b8e745dSYikeZhou 1414eebf274Ssinsanction // connect [redirect + walk] ports for fp & vec & int free list 142*368cbcecSxiaofeibao Seq(fpFreeList, vecFreeList, intFreeList, v0FreeList, vlFreeList).foreach { case fl => 14370224bf6SYinan Xu fl.io.redirect := io.redirect.valid 1446b102a39SHaojin Tang fl.io.walk := io.rabCommits.isWalk 1454efb89cbSYikeZhou } 1464eebf274Ssinsanction // only when all free list and dispatch1 has enough space can we do allocation 147ccfddc82SHaojin Tang // when isWalk, freelist can definitely allocate 148*368cbcecSxiaofeibao intFreeList.io.doAllocate := fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk 149*368cbcecSxiaofeibao fpFreeList.io.doAllocate := intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk 150*368cbcecSxiaofeibao vecFreeList.io.doAllocate := intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk 151*368cbcecSxiaofeibao v0FreeList.io.doAllocate := intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk 152*368cbcecSxiaofeibao vlFreeList.io.doAllocate := intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk 1535eb4af5bSYikeZhou 1544eebf274Ssinsanction // dispatch1 ready ++ float point free list ready ++ int free list ready ++ vec free list ready ++ not walk 155*368cbcecSxiaofeibao val canOut = dispatchCanAcc && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !io.rabCommits.isWalk 1565eb4af5bSYikeZhou 15789cc69c1STang Haojin compressUnit.io.in.zip(io.in).foreach{ case(sink, source) => 15889cc69c1STang Haojin sink.valid := source.valid 15989cc69c1STang Haojin sink.bits := source.bits 16089cc69c1STang Haojin } 16189cc69c1STang Haojin val needRobFlags = compressUnit.io.out.needRobFlags 16289cc69c1STang Haojin val instrSizesVec = compressUnit.io.out.instrSizes 16389cc69c1STang Haojin val compressMasksVec = compressUnit.io.out.masks 164b034d3b9SLinJiawei 1659aca92b9SYinan Xu // speculatively assign the instruction with an robIdx 16689cc69c1STang Haojin val validCount = PopCount(io.in.zip(needRobFlags).map{ case(in, needRobFlag) => in.valid && in.bits.lastUop && needRobFlag}) // number of instructions waiting to enter rob (from decode) 1679aca92b9SYinan Xu val robIdxHead = RegInit(0.U.asTypeOf(new RobPtr)) 1685f8b6c9eSsinceforYy val lastCycleMisprediction = GatedValidRegNext(io.redirect.valid && !io.redirect.bits.flushItself()) 169f4b2089aSYinan Xu val robIdxHeadNext = Mux(io.redirect.valid, io.redirect.bits.robIdx, // redirect: move ptr to given rob index 1709aca92b9SYinan Xu Mux(lastCycleMisprediction, robIdxHead + 1.U, // mis-predict: not flush robIdx itself 171ac78003fSzhanglyGit Mux(canOut, robIdxHead + validCount, // instructions successfully entered next stage: increase robIdx 172f4b2089aSYinan Xu /* default */ robIdxHead))) // no instructions passed by this cycle: stick to old value 1739aca92b9SYinan Xu robIdxHead := robIdxHeadNext 174588ceab5SYinan Xu 17500ad41d0SYinan Xu /** 17600ad41d0SYinan Xu * Rename: allocate free physical register and update rename table 17700ad41d0SYinan Xu */ 1783b739f49SXuan Hu val uops = Wire(Vec(RenameWidth, new DynInst)) 179b034d3b9SLinJiawei uops.foreach( uop => { 180a7a8a6ccSHaojin Tang uop.srcState := DontCare 1817cef916fSYinan Xu uop.debugInfo := DontCare 182bc86598fSWilliam Wang uop.lqIdx := DontCare 183bc86598fSWilliam Wang uop.sqIdx := DontCare 1843b739f49SXuan Hu uop.waitForRobIdx := DontCare 1853b739f49SXuan Hu uop.singleStep := DontCare 186fa7f2c26STang Haojin uop.snapshot := DontCare 18713551487SzhanglyGit uop.srcLoadDependency := DontCare 188f3a9fb05SAnzo uop.numLsElem := DontCare 1898daac0bfSxiaofeibao-xjtu uop.hasException := DontCare 190b034d3b9SLinJiawei }) 191b034d3b9SLinJiawei 192deb6421eSHaojin Tang val needVecDest = Wire(Vec(RenameWidth, Bool())) 19399b8dc2cSYinan Xu val needFpDest = Wire(Vec(RenameWidth, Bool())) 19499b8dc2cSYinan Xu val needIntDest = Wire(Vec(RenameWidth, Bool())) 195*368cbcecSxiaofeibao val needV0Dest = Wire(Vec(RenameWidth, Bool())) 196*368cbcecSxiaofeibao val needVlDest = Wire(Vec(RenameWidth, Bool())) 197b424051cSYinan Xu val hasValid = Cat(io.in.map(_.valid)).orR 198a63155a6SXuan Hu private val inHeadValid = io.in.head.valid 1998b8e745dSYikeZhou 200c58c2872STang Haojin val isMove = Wire(Vec(RenameWidth, Bool())) 201c58c2872STang Haojin isMove zip io.in.map(_.bits) foreach { 202c58c2872STang Haojin case (move, in) => move := Mux(in.exceptionVec.asUInt.orR, false.B, in.isMove) 203c58c2872STang Haojin } 2048b8e745dSYikeZhou 205ccfddc82SHaojin Tang val walkNeedIntDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 2063b739f49SXuan Hu val walkNeedFpDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 2073b739f49SXuan Hu val walkNeedVecDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 208*368cbcecSxiaofeibao val walkNeedV0Dest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 209*368cbcecSxiaofeibao val walkNeedVlDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 210ccfddc82SHaojin Tang val walkIsMove = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 211ccfddc82SHaojin Tang 2128b8e745dSYikeZhou val intSpecWen = Wire(Vec(RenameWidth, Bool())) 2138b8e745dSYikeZhou val fpSpecWen = Wire(Vec(RenameWidth, Bool())) 214deb6421eSHaojin Tang val vecSpecWen = Wire(Vec(RenameWidth, Bool())) 215*368cbcecSxiaofeibao val v0SpecWen = Wire(Vec(RenameWidth, Bool())) 216*368cbcecSxiaofeibao val vlSpecWen = Wire(Vec(RenameWidth, Bool())) 2178b8e745dSYikeZhou 218ccfddc82SHaojin Tang val walkIntSpecWen = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 219ccfddc82SHaojin Tang 220ccfddc82SHaojin Tang val walkPdest = Wire(Vec(RenameWidth, UInt(PhyRegIdxWidth.W))) 221ccfddc82SHaojin Tang 2228b8e745dSYikeZhou // uop calculation 223b034d3b9SLinJiawei for (i <- 0 until RenameWidth) { 2240c01a27aSHaojin Tang (uops(i): Data).waiveAll :<= (io.in(i).bits: Data).waiveAll 225b034d3b9SLinJiawei 226980c1bc3SWilliam Wang // update cf according to ssit result 2273b739f49SXuan Hu uops(i).storeSetHit := io.ssit(i).valid 2283b739f49SXuan Hu uops(i).loadWaitStrict := io.ssit(i).strict && io.ssit(i).valid 2293b739f49SXuan Hu uops(i).ssid := io.ssit(i).ssid 230980c1bc3SWilliam Wang 231980c1bc3SWilliam Wang // update cf according to waittable result 2323b739f49SXuan Hu uops(i).loadWaitBit := io.waittable(i) 233980c1bc3SWilliam Wang 2343b739f49SXuan Hu uops(i).replayInst := false.B // set by IQ or MemQ 2354eebf274Ssinsanction // alloc a new phy reg 236*368cbcecSxiaofeibao needV0Dest(i) := io.in(i).valid && needDestReg(Reg_V0, io.in(i).bits) 237*368cbcecSxiaofeibao needVlDest(i) := io.in(i).valid && needDestReg(Reg_Vl, io.in(i).bits) 238ac78003fSzhanglyGit needVecDest(i) := io.in(i).valid && needDestReg(Reg_V, io.in(i).bits) 239ac78003fSzhanglyGit needFpDest(i) := io.in(i).valid && needDestReg(Reg_F, io.in(i).bits) 240ac78003fSzhanglyGit needIntDest(i) := io.in(i).valid && needDestReg(Reg_I, io.in(i).bits) 241780712aaSxiaofeibao-xjtu if (i < RabCommitWidth) { 2426b102a39SHaojin Tang walkNeedIntDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_I, io.rabCommits.info(i)) 2436b102a39SHaojin Tang walkNeedFpDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_F, io.rabCommits.info(i)) 2446b102a39SHaojin Tang walkNeedVecDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_V, io.rabCommits.info(i)) 245*368cbcecSxiaofeibao walkNeedV0Dest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_V0, io.rabCommits.info(i)) 246*368cbcecSxiaofeibao walkNeedVlDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_Vl, io.rabCommits.info(i)) 2476b102a39SHaojin Tang walkIsMove(i) := io.rabCommits.info(i).isMove 248ccfddc82SHaojin Tang } 2494eebf274Ssinsanction fpFreeList.io.allocateReq(i) := needFpDest(i) 2504eebf274Ssinsanction fpFreeList.io.walkReq(i) := walkNeedFpDest(i) 2514eebf274Ssinsanction vecFreeList.io.allocateReq(i) := needVecDest(i) 2524eebf274Ssinsanction vecFreeList.io.walkReq(i) := walkNeedVecDest(i) 253*368cbcecSxiaofeibao v0FreeList.io.allocateReq(i) := needV0Dest(i) 254*368cbcecSxiaofeibao v0FreeList.io.walkReq(i) := walkNeedV0Dest(i) 255*368cbcecSxiaofeibao vlFreeList.io.allocateReq(i) := needVlDest(i) 256*368cbcecSxiaofeibao vlFreeList.io.walkReq(i) := walkNeedVlDest(i) 257dcf3a679STang Haojin intFreeList.io.allocateReq(i) := needIntDest(i) && !isMove(i) 258dcf3a679STang Haojin intFreeList.io.walkReq(i) := walkNeedIntDest(i) && !walkIsMove(i) 2592438f9ebSYinan Xu 2608b8e745dSYikeZhou // no valid instruction from decode stage || all resources (dispatch1 + both free lists) ready 261ac78003fSzhanglyGit io.in(i).ready := !hasValid || canOut 26258e06390SLinJiawei 26389cc69c1STang Haojin uops(i).robIdx := robIdxHead + PopCount(io.in.zip(needRobFlags).take(i).map{ case(in, needRobFlag) => in.valid && in.bits.lastUop && needRobFlag}) 26489cc69c1STang Haojin uops(i).instrSize := instrSizesVec(i) 26589cc69c1STang Haojin when(isMove(i)) { 26689cc69c1STang Haojin uops(i).numUops := 0.U 2673235a9d8SZiyue-Zhang uops(i).numWB := 0.U 26889cc69c1STang Haojin } 26989cc69c1STang Haojin if (i > 0) { 27089cc69c1STang Haojin when(!needRobFlags(i - 1)) { 27189cc69c1STang Haojin uops(i).firstUop := false.B 27289cc69c1STang Haojin uops(i).ftqPtr := uops(i - 1).ftqPtr 27389cc69c1STang Haojin uops(i).ftqOffset := uops(i - 1).ftqOffset 27489cc69c1STang Haojin uops(i).numUops := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse)) 2753235a9d8SZiyue-Zhang uops(i).numWB := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse)) 27689cc69c1STang Haojin } 27789cc69c1STang Haojin } 27889cc69c1STang Haojin when(!needRobFlags(i)) { 27989cc69c1STang Haojin uops(i).lastUop := false.B 28089cc69c1STang Haojin uops(i).numUops := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse)) 2813235a9d8SZiyue-Zhang uops(i).numWB := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse)) 28289cc69c1STang Haojin } 283f1ba628bSHaojin Tang uops(i).wfflags := (compressMasksVec(i) & Cat(io.in.map(_.bits.wfflags).reverse)).orR 284f1ba628bSHaojin Tang uops(i).dirtyFs := (compressMasksVec(i) & Cat(io.in.map(_.bits.fpWen).reverse)).orR 2853af3539fSZiyue Zhang // vector instructions' uopSplitType cannot be UopSplitType.SCA_SIM 2863af3539fSZiyue Zhang uops(i).dirtyVs := (compressMasksVec(i) & Cat(io.in.map(_.bits.uopSplitType =/= UopSplitType.SCA_SIM).reverse)).orR 287*368cbcecSxiaofeibao // psrc0,psrc1,psrc2 don't require v0ReadPorts because their srcType can distinguish whether they are V0 or not 288*368cbcecSxiaofeibao uops(i).psrc(0) := Mux1H(uops(i).srcType(0)(2, 0), Seq(io.intReadPorts(i)(0), io.fpReadPorts(i)(0), io.vecReadPorts(i)(0))) 289*368cbcecSxiaofeibao uops(i).psrc(1) := Mux1H(uops(i).srcType(1)(2, 0), Seq(io.intReadPorts(i)(1), io.fpReadPorts(i)(1), io.vecReadPorts(i)(1))) 2903b739f49SXuan Hu uops(i).psrc(2) := Mux1H(uops(i).srcType(2)(2, 1), Seq(io.fpReadPorts(i)(2), io.vecReadPorts(i)(2))) 291*368cbcecSxiaofeibao uops(i).psrc(3) := io.v0ReadPorts(i)(0) 292*368cbcecSxiaofeibao uops(i).psrc(4) := io.vlReadPorts(i)(0) 293f5710817SXuan Hu 294a0db5a4bSYinan Xu // int psrc2 should be bypassed from next instruction if it is fused 295a0db5a4bSYinan Xu if (i < RenameWidth - 1) { 296a0db5a4bSYinan Xu when (io.fusionInfo(i).rs2FromRs2 || io.fusionInfo(i).rs2FromRs1) { 297a0db5a4bSYinan Xu uops(i).psrc(1) := Mux(io.fusionInfo(i).rs2FromRs2, io.intReadPorts(i + 1)(1), io.intReadPorts(i + 1)(0)) 298a0db5a4bSYinan Xu }.elsewhen(io.fusionInfo(i).rs2FromZero) { 299a0db5a4bSYinan Xu uops(i).psrc(1) := 0.U 300a0db5a4bSYinan Xu } 301a0db5a4bSYinan Xu } 30270224bf6SYinan Xu uops(i).eliminatedMove := isMove(i) 3038b8e745dSYikeZhou 3048b8e745dSYikeZhou // update pdest 305ac78003fSzhanglyGit uops(i).pdest := MuxCase(0.U, Seq( 306ac78003fSzhanglyGit needIntDest(i) -> intFreeList.io.allocatePhyReg(i), 3074eebf274Ssinsanction needFpDest(i) -> fpFreeList.io.allocatePhyReg(i), 3084eebf274Ssinsanction needVecDest(i) -> vecFreeList.io.allocatePhyReg(i), 309*368cbcecSxiaofeibao needV0Dest(i) -> v0FreeList.io.allocatePhyReg(i), 310*368cbcecSxiaofeibao needVlDest(i) -> vlFreeList.io.allocatePhyReg(i), 3113b739f49SXuan Hu )) 3128b8e745dSYikeZhou 313ebb8ebf8SYinan Xu // Assign performance counters 314ebb8ebf8SYinan Xu uops(i).debugInfo.renameTime := GTimer() 315ebb8ebf8SYinan Xu 316*368cbcecSxiaofeibao io.out(i).valid := io.in(i).valid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !io.rabCommits.isWalk 317ebb8ebf8SYinan Xu io.out(i).bits := uops(i) 3183b739f49SXuan Hu // Todo: move these shit in decode stage 319f025d715SYinan Xu // dirty code for fence. The lsrc is passed by imm. 3203b739f49SXuan Hu when (io.out(i).bits.fuType === FuType.fence.U) { 3213b739f49SXuan Hu io.out(i).bits.imm := Cat(io.in(i).bits.lsrc(1), io.in(i).bits.lsrc(0)) 322a020ce37SYinan Xu } 323d91483a6Sfdy 324f025d715SYinan Xu // dirty code for SoftPrefetch (prefetch.r/prefetch.w) 325621007d9SXuan Hu// when (io.in(i).bits.isSoftPrefetch) { 326621007d9SXuan Hu// io.out(i).bits.fuType := FuType.ldu.U 327621007d9SXuan Hu// io.out(i).bits.fuOpType := Mux(io.in(i).bits.lsrc(1) === 1.U, LSUOpType.prefetch_r, LSUOpType.prefetch_w) 328621007d9SXuan Hu// io.out(i).bits.selImm := SelImm.IMM_S 329621007d9SXuan Hu// io.out(i).bits.imm := Cat(io.in(i).bits.imm(io.in(i).bits.imm.getWidth - 1, 5), 0.U(5.W)) 330621007d9SXuan Hu// } 331ebb8ebf8SYinan Xu 332765e58c6Ssinsanction // dirty code for lui+addi(w) fusion 333765e58c6Ssinsanction if (i < RenameWidth - 1) { 334765e58c6Ssinsanction val fused_lui32 = io.in(i).bits.selImm === SelImm.IMM_LUI32 && io.in(i).bits.fuType === FuType.alu.U 335765e58c6Ssinsanction when (fused_lui32) { 336765e58c6Ssinsanction val lui_imm = io.in(i).bits.imm(19, 0) 337765e58c6Ssinsanction val add_imm = io.in(i + 1).bits.imm(11, 0) 33849f433deSXuan Hu require(io.out(i).bits.imm.getWidth >= lui_imm.getWidth + add_imm.getWidth) 33949f433deSXuan Hu io.out(i).bits.imm := Cat(lui_imm, add_imm) 340765e58c6Ssinsanction } 341765e58c6Ssinsanction } 342765e58c6Ssinsanction 3438b8e745dSYikeZhou // write speculative rename table 34439d3280eSYikeZhou // we update rat later inside commit code 3456b102a39SHaojin Tang intSpecWen(i) := needIntDest(i) && intFreeList.io.canAllocate && intFreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid 3466b102a39SHaojin Tang fpSpecWen(i) := needFpDest(i) && fpFreeList.io.canAllocate && fpFreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid 3474eebf274Ssinsanction vecSpecWen(i) := needVecDest(i) && vecFreeList.io.canAllocate && vecFreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid 348*368cbcecSxiaofeibao v0SpecWen(i) := needV0Dest(i) && v0FreeList.io.canAllocate && v0FreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid 349*368cbcecSxiaofeibao vlSpecWen(i) := needVlDest(i) && vlFreeList.io.canAllocate && vlFreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid 350ac78003fSzhanglyGit 35170224bf6SYinan Xu 352780712aaSxiaofeibao-xjtu if (i < RabCommitWidth) { 353ccfddc82SHaojin Tang walkIntSpecWen(i) := walkNeedIntDest(i) && !io.redirect.valid 3546b102a39SHaojin Tang walkPdest(i) := io.rabCommits.info(i).pdest 355ccfddc82SHaojin Tang } else { 356ccfddc82SHaojin Tang walkPdest(i) := io.out(i).bits.pdest 357ccfddc82SHaojin Tang } 358b034d3b9SLinJiawei } 359b034d3b9SLinJiawei 36070224bf6SYinan Xu /** 36170224bf6SYinan Xu * How to set psrc: 36270224bf6SYinan Xu * - bypass the pdest to psrc if previous instructions write to the same ldest as lsrc 36370224bf6SYinan Xu * - default: psrc from RAT 36470224bf6SYinan Xu * How to set pdest: 36570224bf6SYinan Xu * - Mux(isMove, psrc, pdest_from_freelist). 36670224bf6SYinan Xu * 36770224bf6SYinan Xu * The critical path of rename lies here: 36870224bf6SYinan Xu * When move elimination is enabled, we need to update the rat with psrc. 36970224bf6SYinan Xu * However, psrc maybe comes from previous instructions' pdest, which comes from freelist. 37070224bf6SYinan Xu * 37170224bf6SYinan Xu * If we expand these logic for pdest(N): 37270224bf6SYinan Xu * pdest(N) = Mux(isMove(N), psrc(N), freelist_out(N)) 37370224bf6SYinan Xu * = Mux(isMove(N), Mux(bypass(N, N - 1), pdest(N - 1), 37470224bf6SYinan Xu * Mux(bypass(N, N - 2), pdest(N - 2), 37570224bf6SYinan Xu * ... 37670224bf6SYinan Xu * Mux(bypass(N, 0), pdest(0), 37770224bf6SYinan Xu * rat_out(N))...)), 37870224bf6SYinan Xu * freelist_out(N)) 37970224bf6SYinan Xu */ 38070224bf6SYinan Xu // a simple functional model for now 38170224bf6SYinan Xu io.out(0).bits.pdest := Mux(isMove(0), uops(0).psrc.head, uops(0).pdest) 3823b739f49SXuan Hu 3833b739f49SXuan Hu // psrc(n) + pdest(1) 38498639abbSXuan Hu val bypassCond: Vec[MixedVec[UInt]] = Wire(Vec(numRegSrc + 1, MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))))) 38598639abbSXuan Hu require(io.in(0).bits.srcType.size == io.in(0).bits.numSrc) 38698639abbSXuan Hu private val pdestLoc = io.in.head.bits.srcType.size // 2 vector src: v0, vl&vtype 3873b739f49SXuan Hu println(s"[Rename] idx of pdest in bypassCond $pdestLoc") 38899b8dc2cSYinan Xu for (i <- 1 until RenameWidth) { 389*368cbcecSxiaofeibao val v0Cond = io.in(i).bits.srcType.zipWithIndex.map{ case (s, i) => 390*368cbcecSxiaofeibao if (i == 3) (s === SrcType.vp) || (s === SrcType.v0) 391*368cbcecSxiaofeibao else false.B 392*368cbcecSxiaofeibao } :+ needV0Dest(i) 393*368cbcecSxiaofeibao val vlCond = io.in(i).bits.srcType.zipWithIndex.map{ case (s, i) => 394*368cbcecSxiaofeibao if (i == 4) s === SrcType.vp 395*368cbcecSxiaofeibao else false.B 396*368cbcecSxiaofeibao } :+ needVlDest(i) 39798639abbSXuan Hu val vecCond = io.in(i).bits.srcType.map(_ === SrcType.vp) :+ needVecDest(i) 39898639abbSXuan Hu val fpCond = io.in(i).bits.srcType.map(_ === SrcType.fp) :+ needFpDest(i) 39998639abbSXuan Hu val intCond = io.in(i).bits.srcType.map(_ === SrcType.xp) :+ needIntDest(i) 40098639abbSXuan Hu val target = io.in(i).bits.lsrc :+ io.in(i).bits.ldest 401*368cbcecSxiaofeibao for ((((((cond1, (condV0, condVl)), cond2), cond3), t), j) <- vecCond.zip(v0Cond.zip(vlCond)).zip(fpCond).zip(intCond).zip(target).zipWithIndex) { 40270224bf6SYinan Xu val destToSrc = io.in.take(i).zipWithIndex.map { case (in, j) => 4033b739f49SXuan Hu val indexMatch = in.bits.ldest === t 404deb6421eSHaojin Tang val writeMatch = cond3 && needIntDest(j) || cond2 && needFpDest(j) || cond1 && needVecDest(j) 405*368cbcecSxiaofeibao val v0vlMatch = condV0 && needV0Dest(j) || condVl && needVlDest(j) 406*368cbcecSxiaofeibao indexMatch && writeMatch || v0vlMatch 40770224bf6SYinan Xu } 40870224bf6SYinan Xu bypassCond(j)(i - 1) := VecInit(destToSrc).asUInt 40970224bf6SYinan Xu } 41070224bf6SYinan Xu io.out(i).bits.psrc(0) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(0)(i-1).asBools).foldLeft(uops(i).psrc(0)) { 41170224bf6SYinan Xu (z, next) => Mux(next._2, next._1, z) 41270224bf6SYinan Xu } 41370224bf6SYinan Xu io.out(i).bits.psrc(1) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(1)(i-1).asBools).foldLeft(uops(i).psrc(1)) { 41470224bf6SYinan Xu (z, next) => Mux(next._2, next._1, z) 41570224bf6SYinan Xu } 41670224bf6SYinan Xu io.out(i).bits.psrc(2) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(2)(i-1).asBools).foldLeft(uops(i).psrc(2)) { 41770224bf6SYinan Xu (z, next) => Mux(next._2, next._1, z) 41870224bf6SYinan Xu } 419a7a8a6ccSHaojin Tang io.out(i).bits.psrc(3) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(3)(i-1).asBools).foldLeft(uops(i).psrc(3)) { 420a7a8a6ccSHaojin Tang (z, next) => Mux(next._2, next._1, z) 421a7a8a6ccSHaojin Tang } 422996aacc9SXuan Hu io.out(i).bits.psrc(4) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(4)(i-1).asBools).foldLeft(uops(i).psrc(4)) { 4233b739f49SXuan Hu (z, next) => Mux(next._2, next._1, z) 4243b739f49SXuan Hu } 42570224bf6SYinan Xu io.out(i).bits.pdest := Mux(isMove(i), io.out(i).bits.psrc(0), uops(i).pdest) 426fd7603d9SYinan Xu 4273b739f49SXuan Hu // Todo: better implementation for fields reuse 428fd7603d9SYinan Xu // For fused-lui-load, load.src(0) is replaced by the imm. 4293b739f49SXuan Hu val last_is_lui = io.in(i - 1).bits.selImm === SelImm.IMM_U && io.in(i - 1).bits.srcType(0) =/= SrcType.pc 4303b739f49SXuan Hu val this_is_load = io.in(i).bits.fuType === FuType.ldu.U 4313b739f49SXuan Hu val lui_to_load = io.in(i - 1).valid && io.in(i - 1).bits.ldest === io.in(i).bits.lsrc(0) 432f4dcd9fcSsinsanction val fused_lui_load = last_is_lui && this_is_load && lui_to_load 433fd7603d9SYinan Xu when (fused_lui_load) { 43449f433deSXuan Hu // The first LOAD operand (base address) is replaced by LUI-imm and stored in imm 43549f433deSXuan Hu val lui_imm = io.in(i - 1).bits.imm(ImmUnion.U.len - 1, 0) 43649f433deSXuan Hu val ld_imm = io.in(i).bits.imm(ImmUnion.I.len - 1, 0) 43749f433deSXuan Hu require(io.out(i).bits.imm.getWidth >= lui_imm.getWidth + ld_imm.getWidth) 4383b739f49SXuan Hu io.out(i).bits.srcType(0) := SrcType.imm 43949f433deSXuan Hu io.out(i).bits.imm := Cat(lui_imm, ld_imm) 440fd7603d9SYinan Xu } 441fd7603d9SYinan Xu 442b034d3b9SLinJiawei } 44300ad41d0SYinan Xu 444c4b56310SHaojin Tang val genSnapshot = Cat(io.out.map(out => out.fire && out.bits.snapshot)).orR 445bb7e6e3aSxiaofeibao-xjtu val lastCycleCreateSnpt = RegInit(false.B) 446bb7e6e3aSxiaofeibao-xjtu lastCycleCreateSnpt := genSnapshot && !io.snptIsFull 447bb7e6e3aSxiaofeibao-xjtu val sameSnptDistance = (RobCommitWidth * 4).U 448bb7e6e3aSxiaofeibao-xjtu // notInSameSnpt: 1.robidxHead - snapLastEnq >= sameSnptDistance 2.no snap 449bb7e6e3aSxiaofeibao-xjtu val notInSameSnpt = GatedValidRegNext(distanceBetween(robIdxHeadNext, io.snptLastEnq.bits) >= sameSnptDistance || !io.snptLastEnq.valid) 450bb7e6e3aSxiaofeibao-xjtu val allowSnpt = if (EnableRenameSnapshot) notInSameSnpt && !lastCycleCreateSnpt && io.in.head.bits.firstUop else false.B 451c4b56310SHaojin Tang io.out.zip(io.in).foreach{ case (out, in) => out.bits.snapshot := allowSnpt && (!in.bits.preDecodeInfo.notCFI || FuType.isJump(in.bits.fuType)) && in.fire } 4528daac0bfSxiaofeibao-xjtu io.out.map{ x => 4538daac0bfSxiaofeibao-xjtu x.bits.hasException := Cat(selectFrontend(x.bits.exceptionVec) :+ x.bits.exceptionVec(illegalInstr) :+ x.bits.exceptionVec(virtualInstr)).orR || x.bits.trigger.getFrontendCanFire 4548daac0bfSxiaofeibao-xjtu } 455780712aaSxiaofeibao-xjtu if(backendParams.debugEn){ 456780712aaSxiaofeibao-xjtu dontTouch(robIdxHeadNext) 457780712aaSxiaofeibao-xjtu dontTouch(notInSameSnpt) 458780712aaSxiaofeibao-xjtu dontTouch(genSnapshot) 459fa7f2c26STang Haojin } 460fa7f2c26STang Haojin intFreeList.io.snpt := io.snpt 461fa7f2c26STang Haojin fpFreeList.io.snpt := io.snpt 4624eebf274Ssinsanction vecFreeList.io.snpt := io.snpt 463*368cbcecSxiaofeibao v0FreeList.io.snpt := io.snpt 464*368cbcecSxiaofeibao vlFreeList.io.snpt := io.snpt 465c4b56310SHaojin Tang intFreeList.io.snpt.snptEnq := genSnapshot 466c4b56310SHaojin Tang fpFreeList.io.snpt.snptEnq := genSnapshot 4674eebf274Ssinsanction vecFreeList.io.snpt.snptEnq := genSnapshot 468*368cbcecSxiaofeibao v0FreeList.io.snpt.snptEnq := genSnapshot 469*368cbcecSxiaofeibao vlFreeList.io.snpt.snptEnq := genSnapshot 470fa7f2c26STang Haojin 47100ad41d0SYinan Xu /** 47200ad41d0SYinan Xu * Instructions commit: update freelist and rename table 47300ad41d0SYinan Xu */ 474780712aaSxiaofeibao-xjtu for (i <- 0 until RabCommitWidth) { 4756b102a39SHaojin Tang val commitValid = io.rabCommits.isCommit && io.rabCommits.commitValid(i) 4766b102a39SHaojin Tang val walkValid = io.rabCommits.isWalk && io.rabCommits.walkValid(i) 47700ad41d0SYinan Xu 478deb6421eSHaojin Tang // I. RAT Update 4797fa2c198SYinan Xu // When redirect happens (mis-prediction), don't update the rename table 480deb6421eSHaojin Tang io.intRenamePorts(i).wen := intSpecWen(i) 4813b739f49SXuan Hu io.intRenamePorts(i).addr := uops(i).ldest 482deb6421eSHaojin Tang io.intRenamePorts(i).data := io.out(i).bits.pdest 4838b8e745dSYikeZhou 484deb6421eSHaojin Tang io.fpRenamePorts(i).wen := fpSpecWen(i) 4853b739f49SXuan Hu io.fpRenamePorts(i).addr := uops(i).ldest 486deb6421eSHaojin Tang io.fpRenamePorts(i).data := fpFreeList.io.allocatePhyReg(i) 487deb6421eSHaojin Tang 488deb6421eSHaojin Tang io.vecRenamePorts(i).wen := vecSpecWen(i) 4893b739f49SXuan Hu io.vecRenamePorts(i).addr := uops(i).ldest 4904eebf274Ssinsanction io.vecRenamePorts(i).data := vecFreeList.io.allocatePhyReg(i) 491deb6421eSHaojin Tang 492*368cbcecSxiaofeibao io.v0RenamePorts(i).wen := v0SpecWen(i) 493*368cbcecSxiaofeibao io.v0RenamePorts(i).addr := uops(i).ldest 494*368cbcecSxiaofeibao io.v0RenamePorts(i).data := v0FreeList.io.allocatePhyReg(i) 495*368cbcecSxiaofeibao 496*368cbcecSxiaofeibao io.vlRenamePorts(i).wen := vlSpecWen(i) 497*368cbcecSxiaofeibao io.vlRenamePorts(i).addr := uops(i).ldest 498*368cbcecSxiaofeibao io.vlRenamePorts(i).data := vlFreeList.io.allocatePhyReg(i) 499*368cbcecSxiaofeibao 500deb6421eSHaojin Tang // II. Free List Update 501dcf3a679STang Haojin intFreeList.io.freeReq(i) := io.int_need_free(i) 502dcf3a679STang Haojin intFreeList.io.freePhyReg(i) := RegNext(io.int_old_pdest(i)) 5034eebf274Ssinsanction fpFreeList.io.freeReq(i) := GatedValidRegNext(commitValid && needDestRegCommit(Reg_F, io.rabCommits.info(i))) 5047042bac3Ssinsanction fpFreeList.io.freePhyReg(i) := io.fp_old_pdest(i) 5054eebf274Ssinsanction vecFreeList.io.freeReq(i) := GatedValidRegNext(commitValid && needDestRegCommit(Reg_V, io.rabCommits.info(i))) 5067042bac3Ssinsanction vecFreeList.io.freePhyReg(i) := io.vec_old_pdest(i) 507*368cbcecSxiaofeibao v0FreeList.io.freeReq(i) := GatedValidRegNext(commitValid && needDestRegCommit(Reg_V0, io.rabCommits.info(i))) 508*368cbcecSxiaofeibao v0FreeList.io.freePhyReg(i) := io.vec_old_pdest(i) 509*368cbcecSxiaofeibao vlFreeList.io.freeReq(i) := GatedValidRegNext(commitValid && needDestRegCommit(Reg_Vl, io.rabCommits.info(i))) 510*368cbcecSxiaofeibao vlFreeList.io.freePhyReg(i) := io.vec_old_pdest(i) 5118b8e745dSYikeZhou } 5128b8e745dSYikeZhou 5138b8e745dSYikeZhou /* 51470224bf6SYinan Xu Debug and performance counters 5158b8e745dSYikeZhou */ 5163b739f49SXuan Hu def printRenameInfo(in: DecoupledIO[DecodedInst], out: DecoupledIO[DynInst]) = { 5173b739f49SXuan Hu XSInfo(out.fire, p"pc:${Hexadecimal(in.bits.pc)} in(${in.valid},${in.ready}) " + 5183b739f49SXuan Hu p"lsrc(0):${in.bits.lsrc(0)} -> psrc(0):${out.bits.psrc(0)} " + 5193b739f49SXuan Hu p"lsrc(1):${in.bits.lsrc(1)} -> psrc(1):${out.bits.psrc(1)} " + 5203b739f49SXuan Hu p"lsrc(2):${in.bits.lsrc(2)} -> psrc(2):${out.bits.psrc(2)} " + 521c61abc0cSXuan Hu p"ldest:${in.bits.ldest} -> pdest:${out.bits.pdest}\n" 5228b8e745dSYikeZhou ) 5238b8e745dSYikeZhou } 5248b8e745dSYikeZhou 5258b8e745dSYikeZhou for ((x,y) <- io.in.zip(io.out)) { 5268b8e745dSYikeZhou printRenameInfo(x, y) 5278b8e745dSYikeZhou } 5288b8e745dSYikeZhou 52942bcc716Sxiaofeibao-xjtu io.out.map { case x => 53042bcc716Sxiaofeibao-xjtu when(x.valid && x.bits.rfWen){ 53142bcc716Sxiaofeibao-xjtu assert(x.bits.ldest =/= 0.U, "rfWen cannot be 1 when Int regfile ldest is 0") 53242bcc716Sxiaofeibao-xjtu } 53342bcc716Sxiaofeibao-xjtu } 534d2b20d1aSTang Haojin val debugRedirect = RegEnable(io.redirect.bits, io.redirect.valid) 535d2b20d1aSTang Haojin // bad speculation 5366b102a39SHaojin Tang val recStall = io.redirect.valid || io.rabCommits.isWalk 5376b102a39SHaojin Tang val ctrlRecStall = Mux(io.redirect.valid, io.redirect.bits.debugIsCtrl, io.rabCommits.isWalk && debugRedirect.debugIsCtrl) 5386b102a39SHaojin Tang val mvioRecStall = Mux(io.redirect.valid, io.redirect.bits.debugIsMemVio, io.rabCommits.isWalk && debugRedirect.debugIsMemVio) 539d2b20d1aSTang Haojin val otherRecStall = recStall && !(ctrlRecStall || mvioRecStall) 540d2b20d1aSTang Haojin XSPerfAccumulate("recovery_stall", recStall) 541d2b20d1aSTang Haojin XSPerfAccumulate("control_recovery_stall", ctrlRecStall) 542d2b20d1aSTang Haojin XSPerfAccumulate("mem_violation_recovery_stall", mvioRecStall) 543d2b20d1aSTang Haojin XSPerfAccumulate("other_recovery_stall", otherRecStall) 544d2b20d1aSTang Haojin // freelist stall 545d2b20d1aSTang Haojin val notRecStall = !io.out.head.valid && !recStall 546*368cbcecSxiaofeibao val intFlStall = notRecStall && inHeadValid && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !intFreeList.io.canAllocate 547*368cbcecSxiaofeibao val fpFlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !fpFreeList.io.canAllocate 548*368cbcecSxiaofeibao val vecFlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !vecFreeList.io.canAllocate 549*368cbcecSxiaofeibao val v0FlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && !v0FreeList.io.canAllocate 550*368cbcecSxiaofeibao val vlFlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && !vlFreeList.io.canAllocate 551*368cbcecSxiaofeibao val multiFlStall = notRecStall && inHeadValid && (PopCount(Cat( 552*368cbcecSxiaofeibao !intFreeList.io.canAllocate, 553*368cbcecSxiaofeibao !fpFreeList.io.canAllocate, 554*368cbcecSxiaofeibao !vecFreeList.io.canAllocate, 555*368cbcecSxiaofeibao !v0FreeList.io.canAllocate, 556*368cbcecSxiaofeibao !vlFreeList.io.canAllocate, 557*368cbcecSxiaofeibao )) > 1.U) 558d2b20d1aSTang Haojin // other stall 559*368cbcecSxiaofeibao val otherStall = notRecStall && !intFlStall && !fpFlStall && !vecFlStall && !v0FlStall && !vlFlStall && !multiFlStall 560d2b20d1aSTang Haojin 561d2b20d1aSTang Haojin io.stallReason.in.backReason.valid := io.stallReason.out.backReason.valid || !io.in.head.ready 562d2b20d1aSTang Haojin io.stallReason.in.backReason.bits := Mux(io.stallReason.out.backReason.valid, io.stallReason.out.backReason.bits, 563d2b20d1aSTang Haojin MuxCase(TopDownCounters.OtherCoreStall.id.U, Seq( 564d2b20d1aSTang Haojin ctrlRecStall -> TopDownCounters.ControlRecoveryStall.id.U, 565d2b20d1aSTang Haojin mvioRecStall -> TopDownCounters.MemVioRecoveryStall.id.U, 566d2b20d1aSTang Haojin otherRecStall -> TopDownCounters.OtherRecoveryStall.id.U, 567d2b20d1aSTang Haojin intFlStall -> TopDownCounters.IntFlStall.id.U, 5684eebf274Ssinsanction fpFlStall -> TopDownCounters.FpFlStall.id.U, 5694eebf274Ssinsanction vecFlStall -> TopDownCounters.VecFlStall.id.U, 570*368cbcecSxiaofeibao v0FlStall -> TopDownCounters.V0FlStall.id.U, 571*368cbcecSxiaofeibao vlFlStall -> TopDownCounters.VlFlStall.id.U, 572*368cbcecSxiaofeibao multiFlStall -> TopDownCounters.MultiFlStall.id.U, 573d2b20d1aSTang Haojin ) 574d2b20d1aSTang Haojin )) 575d2b20d1aSTang Haojin io.stallReason.out.reason.zip(io.stallReason.in.reason).zip(io.in.map(_.valid)).foreach { case ((out, in), valid) => 5760adf86dcSHaojin Tang out := Mux(io.stallReason.in.backReason.valid, io.stallReason.in.backReason.bits, in) 577d2b20d1aSTang Haojin } 578d2b20d1aSTang Haojin 5796b102a39SHaojin Tang XSDebug(io.rabCommits.isWalk, p"Walk Recovery Enabled\n") 5806b102a39SHaojin Tang XSDebug(io.rabCommits.isWalk, p"validVec:${Binary(io.rabCommits.walkValid.asUInt)}\n") 581780712aaSxiaofeibao-xjtu for (i <- 0 until RabCommitWidth) { 5826b102a39SHaojin Tang val info = io.rabCommits.info(i) 5836b102a39SHaojin Tang XSDebug(io.rabCommits.isWalk && io.rabCommits.walkValid(i), p"[#$i walk info] " + 584*368cbcecSxiaofeibao p"ldest:${info.ldest} rfWen:${info.rfWen} fpWen:${info.fpWen} vecWen:${info.vecWen} v0Wen:${info.v0Wen} vlWen:${info.vlWen}") 5858b8e745dSYikeZhou } 5868b8e745dSYikeZhou 5878b8e745dSYikeZhou XSDebug(p"inValidVec: ${Binary(Cat(io.in.map(_.valid)))}\n") 5888b8e745dSYikeZhou 589a63155a6SXuan Hu XSPerfAccumulate("in_valid_count", PopCount(io.in.map(_.valid))) 590a63155a6SXuan Hu XSPerfAccumulate("in_fire_count", PopCount(io.in.map(_.fire))) 591a63155a6SXuan Hu XSPerfAccumulate("in_valid_not_ready_count", PopCount(io.in.map(x => x.valid && !x.ready))) 5926374b1d6SXuan Hu XSPerfAccumulate("wait_cycle", !io.in.head.valid && dispatchCanAcc) 5935eb4af5bSYikeZhou 594a63155a6SXuan Hu // These stall reasons could overlap each other, but we configure the priority as fellows. 595a63155a6SXuan Hu // walk stall > dispatch stall > int freelist stall > fp freelist stall 596a63155a6SXuan Hu private val inHeadStall = io.in.head match { case x => x.valid && !x.ready } 5976b102a39SHaojin Tang private val stallForWalk = inHeadValid && io.rabCommits.isWalk 5986374b1d6SXuan Hu private val stallForDispatch = inHeadValid && !io.rabCommits.isWalk && !dispatchCanAcc 599*368cbcecSxiaofeibao private val stallForIntFL = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !intFreeList.io.canAllocate 600*368cbcecSxiaofeibao private val stallForFpFL = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !fpFreeList.io.canAllocate 601*368cbcecSxiaofeibao private val stallForVecFL = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !vecFreeList.io.canAllocate 602*368cbcecSxiaofeibao private val stallForV0FL = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && !v0FreeList.io.canAllocate 603*368cbcecSxiaofeibao private val stallForVlFL = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && !vlFreeList.io.canAllocate 604a63155a6SXuan Hu XSPerfAccumulate("stall_cycle", inHeadStall) 605a63155a6SXuan Hu XSPerfAccumulate("stall_cycle_walk", stallForWalk) 606a63155a6SXuan Hu XSPerfAccumulate("stall_cycle_dispatch", stallForDispatch) 607a63155a6SXuan Hu XSPerfAccumulate("stall_cycle_int", stallForIntFL) 608a63155a6SXuan Hu XSPerfAccumulate("stall_cycle_fp", stallForFpFL) 6094eebf274Ssinsanction XSPerfAccumulate("stall_cycle_vec", stallForVecFL) 610*368cbcecSxiaofeibao XSPerfAccumulate("stall_cycle_vec", stallForV0FL) 611*368cbcecSxiaofeibao XSPerfAccumulate("stall_cycle_vec", stallForVlFL) 612a63155a6SXuan Hu 613a63155a6SXuan Hu XSPerfHistogram("in_valid_range", PopCount(io.in.map(_.valid)), true.B, 0, DecodeWidth + 1, 1) 614a63155a6SXuan Hu XSPerfHistogram("in_fire_range", PopCount(io.in.map(_.fire)), true.B, 0, DecodeWidth + 1, 1) 615a63155a6SXuan Hu XSPerfHistogram("out_valid_range", PopCount(io.out.map(_.valid)), true.B, 0, DecodeWidth + 1, 1) 616a63155a6SXuan Hu XSPerfHistogram("out_fire_range", PopCount(io.out.map(_.fire)), true.B, 0, DecodeWidth + 1, 1) 617d8aa3d57SbugGenerator 6183b739f49SXuan Hu XSPerfAccumulate("move_instr_count", PopCount(io.out.map(out => out.fire && out.bits.isMove))) 6193b739f49SXuan Hu val is_fused_lui_load = io.out.map(o => o.fire && o.bits.fuType === FuType.ldu.U && o.bits.srcType(0) === SrcType.imm) 620fd7603d9SYinan Xu XSPerfAccumulate("fused_lui_load_instr_count", PopCount(is_fused_lui_load)) 621cd365d4cSrvcoresjw 6221ca0e4f3SYinan Xu val renamePerf = Seq( 623cd365d4cSrvcoresjw ("rename_in ", PopCount(io.in.map(_.valid & io.in(0).ready )) ), 624cd365d4cSrvcoresjw ("rename_waitinstr ", PopCount((0 until RenameWidth).map(i => io.in(i).valid && !io.in(i).ready)) ), 625a63155a6SXuan Hu ("rename_stall ", inHeadStall), 6266b102a39SHaojin Tang ("rename_stall_cycle_walk ", inHeadValid && io.rabCommits.isWalk), 6276374b1d6SXuan Hu ("rename_stall_cycle_dispatch", inHeadValid && !io.rabCommits.isWalk && !dispatchCanAcc), 628*368cbcecSxiaofeibao ("rename_stall_cycle_int ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !intFreeList.io.canAllocate), 629*368cbcecSxiaofeibao ("rename_stall_cycle_fp ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !fpFreeList.io.canAllocate), 630*368cbcecSxiaofeibao ("rename_stall_cycle_vec ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !vecFreeList.io.canAllocate), 631*368cbcecSxiaofeibao ("rename_stall_cycle_v0 ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && !v0FreeList.io.canAllocate), 632*368cbcecSxiaofeibao ("rename_stall_cycle_vl ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && !vlFreeList.io.canAllocate), 633cd365d4cSrvcoresjw ) 6341ca0e4f3SYinan Xu val intFlPerf = intFreeList.getPerfEvents 6351ca0e4f3SYinan Xu val fpFlPerf = fpFreeList.getPerfEvents 6364eebf274Ssinsanction val vecFlPerf = vecFreeList.getPerfEvents 637*368cbcecSxiaofeibao val v0FlPerf = v0FreeList.getPerfEvents 638*368cbcecSxiaofeibao val vlFlPerf = vlFreeList.getPerfEvents 639*368cbcecSxiaofeibao val perfEvents = renamePerf ++ intFlPerf ++ fpFlPerf ++ vecFlPerf ++ v0FlPerf ++ vlFlPerf 6401ca0e4f3SYinan Xu generatePerfEvent() 6415eb4af5bSYikeZhou} 642