1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.backend.rename 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utility._ 23import utils._ 24import xiangshan._ 25import xiangshan.backend.Bundles.{DecodedInst, DynInst} 26import xiangshan.backend.decode.{FusionDecodeInfo, Imm_I, Imm_LUI_LOAD, Imm_U} 27import xiangshan.backend.fu.FuType 28import xiangshan.backend.rename.freelist._ 29import xiangshan.backend.rob.RobPtr 30import xiangshan.mem.mdp._ 31 32class Rename(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper with HasPerfEvents { 33 34 // params alias 35 private val numRegSrc = backendParams.numRegSrc 36 private val numVecRegSrc = backendParams.numVecRegSrc 37 private val numVecRatPorts = numVecRegSrc + 1 // +1 dst 38 39 println(s"[Rename] numRegSrc: $numRegSrc") 40 41 val io = IO(new Bundle() { 42 val redirect = Flipped(ValidIO(new Redirect)) 43 val robCommits = Input(new RobCommitIO) 44 // from decode 45 val in = Vec(RenameWidth, Flipped(DecoupledIO(new DecodedInst))) 46 val fusionInfo = Vec(DecodeWidth - 1, Flipped(new FusionDecodeInfo)) 47 // ssit read result 48 val ssit = Flipped(Vec(RenameWidth, Output(new SSITEntry))) 49 // waittable read result 50 val waittable = Flipped(Vec(RenameWidth, Output(Bool()))) 51 // to rename table 52 val intReadPorts = Vec(RenameWidth, Vec(3, Input(UInt(PhyRegIdxWidth.W)))) 53 val fpReadPorts = Vec(RenameWidth, Vec(4, Input(UInt(PhyRegIdxWidth.W)))) 54 val vecReadPorts = Vec(RenameWidth, Vec(numVecRatPorts, Input(UInt(PhyRegIdxWidth.W)))) 55 val intRenamePorts = Vec(RenameWidth, Output(new RatWritePort)) 56 val fpRenamePorts = Vec(RenameWidth, Output(new RatWritePort)) 57 val vecRenamePorts = Vec(RenameWidth, Output(new RatWritePort)) 58 // from rename table 59 val int_old_pdest = Vec(CommitWidth, Input(UInt(PhyRegIdxWidth.W))) 60 val fp_old_pdest = Vec(CommitWidth, Input(UInt(PhyRegIdxWidth.W))) 61 val vec_old_pdest = Vec(CommitWidth, Input(UInt(PhyRegIdxWidth.W))) 62 val int_need_free = Vec(CommitWidth, Input(Bool())) 63 // to dispatch1 64 val out = Vec(RenameWidth, DecoupledIO(new DynInst)) 65 // for snapshots 66 val snpt = Input(new SnapshotPort) 67 // debug arch ports 68 val debug_int_rat = Vec(32, Input(UInt(PhyRegIdxWidth.W))) 69 val debug_vconfig_rat = Input(UInt(PhyRegIdxWidth.W)) 70 val debug_fp_rat = Vec(32, Input(UInt(PhyRegIdxWidth.W))) 71 val debug_vec_rat = Vec(32, Input(UInt(PhyRegIdxWidth.W))) 72 // perf only 73 val stallReason = new Bundle { 74 val in = Flipped(new StallReasonIO(RenameWidth)) 75 val out = new StallReasonIO(RenameWidth) 76 } 77 }) 78 79 val compressUnit = Module(new CompressUnit()) 80 // create free list and rat 81 val intFreeList = Module(new MEFreeList(IntPhyRegs)) 82 val fpFreeList = Module(new StdFreeList(VfPhyRegs - FpLogicRegs - VecLogicRegs)) 83 84 intFreeList.io.commit <> io.robCommits 85 intFreeList.io.debug_rat <> io.debug_int_rat 86 fpFreeList.io.commit <> io.robCommits 87 fpFreeList.io.debug_rat <> io.debug_fp_rat 88 89 // decide if given instruction needs allocating a new physical register (CfCtrl: from decode; RobCommitInfo: from rob) 90 // fp and vec share `fpFreeList` 91 def needDestReg[T <: DecodedInst](reg_t: RegType, x: T): Bool = reg_t match { 92 case Reg_I => x.rfWen && x.ldest =/= 0.U 93 case Reg_F => x.fpWen 94 case Reg_V => x.vecWen 95 } 96 def needDestRegCommit[T <: RobCommitInfo](reg_t: RegType, x: T): Bool = { 97 reg_t match { 98 case Reg_I => x.rfWen 99 case Reg_F => x.fpWen 100 case Reg_V => x.vecWen 101 } 102 } 103 def needDestRegWalk[T <: RobCommitInfo](reg_t: RegType, x: T): Bool = { 104 reg_t match { 105 case Reg_I => x.rfWen && x.ldest =/= 0.U 106 case Reg_F => x.fpWen 107 case Reg_V => x.vecWen 108 } 109 } 110 111 // connect [redirect + walk] ports for __float point__ & __integer__ free list 112 Seq(fpFreeList, intFreeList).foreach { case fl => 113 fl.io.redirect := io.redirect.valid 114 fl.io.walk := io.robCommits.isWalk 115 } 116 // only when both fp and int free list and dispatch1 has enough space can we do allocation 117 // when isWalk, freelist can definitely allocate 118 intFreeList.io.doAllocate := fpFreeList.io.canAllocate && io.out(0).ready || io.robCommits.isWalk 119 fpFreeList.io.doAllocate := intFreeList.io.canAllocate && io.out(0).ready || io.robCommits.isWalk 120 121 // dispatch1 ready ++ float point free list ready ++ int free list ready ++ not walk 122 val canOut = io.out(0).ready && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && !io.robCommits.isWalk 123 124 compressUnit.io.in.zip(io.in).foreach{ case(sink, source) => 125 sink.valid := source.valid 126 sink.bits := source.bits 127 } 128 val needRobFlags = compressUnit.io.out.needRobFlags 129 val instrSizesVec = compressUnit.io.out.instrSizes 130 val compressMasksVec = compressUnit.io.out.masks 131 132 // speculatively assign the instruction with an robIdx 133 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) 134 val robIdxHead = RegInit(0.U.asTypeOf(new RobPtr)) 135 val lastCycleMisprediction = RegNext(io.redirect.valid && !io.redirect.bits.flushItself()) 136 val robIdxHeadNext = Mux(io.redirect.valid, io.redirect.bits.robIdx, // redirect: move ptr to given rob index 137 Mux(lastCycleMisprediction, robIdxHead + 1.U, // mis-predict: not flush robIdx itself 138 Mux(canOut, robIdxHead + validCount, // instructions successfully entered next stage: increase robIdx 139 /* default */ robIdxHead))) // no instructions passed by this cycle: stick to old value 140 robIdxHead := robIdxHeadNext 141 142 /** 143 * Rename: allocate free physical register and update rename table 144 */ 145 val uops = Wire(Vec(RenameWidth, new DynInst)) 146 uops.foreach( uop => { 147 uop.srcState := DontCare 148 uop.debugInfo := DontCare 149 uop.lqIdx := DontCare 150 uop.sqIdx := DontCare 151 uop.waitForRobIdx := DontCare 152 uop.singleStep := DontCare 153 uop.snapshot := DontCare 154 }) 155 156 require(RenameWidth >= CommitWidth) 157 val needVecDest = Wire(Vec(RenameWidth, Bool())) 158 val needFpDest = Wire(Vec(RenameWidth, Bool())) 159 val needIntDest = Wire(Vec(RenameWidth, Bool())) 160 val hasValid = Cat(io.in.map(_.valid)).orR 161 162 val isMove = io.in.map(_.bits.isMove) 163 164 val walkNeedIntDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 165 val walkNeedFpDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 166 val walkNeedVecDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 167 val walkIsMove = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 168 169 val intSpecWen = Wire(Vec(RenameWidth, Bool())) 170 val fpSpecWen = Wire(Vec(RenameWidth, Bool())) 171 val vecSpecWen = Wire(Vec(RenameWidth, Bool())) 172 173 val walkIntSpecWen = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B))) 174 175 val walkPdest = Wire(Vec(RenameWidth, UInt(PhyRegIdxWidth.W))) 176 177 // uop calculation 178 for (i <- 0 until RenameWidth) { 179 for ((name, data) <- uops(i).elements) { 180 if (io.in(i).bits.elements.contains(name)) { 181 data := io.in(i).bits.elements(name) 182 } 183 } 184 185 // update cf according to ssit result 186 uops(i).storeSetHit := io.ssit(i).valid 187 uops(i).loadWaitStrict := io.ssit(i).strict && io.ssit(i).valid 188 uops(i).ssid := io.ssit(i).ssid 189 190 // update cf according to waittable result 191 uops(i).loadWaitBit := io.waittable(i) 192 193 uops(i).replayInst := false.B // set by IQ or MemQ 194 // alloc a new phy reg, fp and vec share the `fpFreeList` 195 needVecDest (i) := io.in(i).valid && needDestReg(Reg_V, io.in(i).bits) 196 needFpDest (i) := io.in(i).valid && needDestReg(Reg_F, io.in(i).bits) 197 needIntDest (i) := io.in(i).valid && needDestReg(Reg_I, io.in(i).bits) 198 if (i < CommitWidth) { 199 walkNeedIntDest(i) := io.robCommits.walkValid(i) && needDestRegWalk(Reg_I, io.robCommits.info(i)) 200 walkNeedFpDest(i) := io.robCommits.walkValid(i) && needDestRegWalk(Reg_F, io.robCommits.info(i)) 201 walkNeedVecDest(i) := io.robCommits.walkValid(i) && needDestRegWalk(Reg_V, io.robCommits.info(i)) 202 walkIsMove(i) := io.robCommits.info(i).isMove 203 } 204 fpFreeList.io.allocateReq(i) := needFpDest(i) || needVecDest(i) 205 fpFreeList.io.walkReq(i) := walkNeedFpDest(i) || walkNeedVecDest(i) 206 intFreeList.io.allocateReq(i) := needIntDest(i) && !isMove(i) 207 intFreeList.io.walkReq(i) := walkNeedIntDest(i) && !walkIsMove(i) 208 209 // no valid instruction from decode stage || all resources (dispatch1 + both free lists) ready 210 io.in(i).ready := !hasValid || canOut 211 212 uops(i).robIdx := robIdxHead + PopCount(io.in.zip(needRobFlags).take(i).map{ case(in, needRobFlag) => in.valid && in.bits.lastUop && needRobFlag}) 213 uops(i).instrSize := instrSizesVec(i) 214 when(isMove(i)) { 215 uops(i).numUops := 0.U 216 } 217 if (i > 0) { 218 when(!needRobFlags(i - 1)) { 219 uops(i).firstUop := false.B 220 uops(i).ftqPtr := uops(i - 1).ftqPtr 221 uops(i).ftqOffset := uops(i - 1).ftqOffset 222 uops(i).numUops := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse)) 223 } 224 } 225 when(!needRobFlags(i)) { 226 uops(i).lastUop := false.B 227 uops(i).numUops := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse)) 228 } 229 230 uops(i).psrc(0) := Mux1H(uops(i).srcType(0), Seq(io.intReadPorts(i)(0), io.fpReadPorts(i)(0), io.vecReadPorts(i)(0))) 231 uops(i).psrc(1) := Mux1H(uops(i).srcType(1), Seq(io.intReadPorts(i)(1), io.fpReadPorts(i)(1), io.vecReadPorts(i)(1))) 232 uops(i).psrc(2) := Mux1H(uops(i).srcType(2)(2, 1), Seq(io.fpReadPorts(i)(2), io.vecReadPorts(i)(2))) 233 uops(i).psrc(3) := io.vecReadPorts(i)(3) 234 uops(i).psrc(4) := io.vecReadPorts(i)(4) // Todo: vl read port 235 236 // int psrc2 should be bypassed from next instruction if it is fused 237 if (i < RenameWidth - 1) { 238 when (io.fusionInfo(i).rs2FromRs2 || io.fusionInfo(i).rs2FromRs1) { 239 uops(i).psrc(1) := Mux(io.fusionInfo(i).rs2FromRs2, io.intReadPorts(i + 1)(1), io.intReadPorts(i + 1)(0)) 240 }.elsewhen(io.fusionInfo(i).rs2FromZero) { 241 uops(i).psrc(1) := 0.U 242 } 243 } 244 uops(i).psrc(2) := io.fpReadPorts(i)(2) 245 // Todo 246 // uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, io.intReadPorts(i).last, io.fpReadPorts(i).last) 247 uops(i).eliminatedMove := isMove(i) 248 249 // update pdest 250 uops(i).pdest := MuxCase(0.U, Seq( 251 needIntDest(i) -> intFreeList.io.allocatePhyReg(i), 252 (needFpDest(i) || needVecDest(i)) -> fpFreeList.io.allocatePhyReg(i), 253 )) 254 255 // Assign performance counters 256 uops(i).debugInfo.renameTime := GTimer() 257 258 io.out(i).valid := io.in(i).valid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && !io.robCommits.isWalk 259 io.out(i).bits := uops(i) 260 // Todo: move these shit in decode stage 261 // dirty code for fence. The lsrc is passed by imm. 262 when (io.out(i).bits.fuType === FuType.fence.U) { 263 io.out(i).bits.imm := Cat(io.in(i).bits.lsrc(1), io.in(i).bits.lsrc(0)) 264 } 265 266 // dirty code for SoftPrefetch (prefetch.r/prefetch.w) 267// when (io.in(i).bits.isSoftPrefetch) { 268// io.out(i).bits.fuType := FuType.ldu.U 269// io.out(i).bits.fuOpType := Mux(io.in(i).bits.lsrc(1) === 1.U, LSUOpType.prefetch_r, LSUOpType.prefetch_w) 270// io.out(i).bits.selImm := SelImm.IMM_S 271// io.out(i).bits.imm := Cat(io.in(i).bits.imm(io.in(i).bits.imm.getWidth - 1, 5), 0.U(5.W)) 272// } 273 274 // write speculative rename table 275 // we update rat later inside commit code 276 intSpecWen(i) := needIntDest(i) && intFreeList.io.canAllocate && intFreeList.io.doAllocate && !io.robCommits.isWalk && !io.redirect.valid 277 fpSpecWen(i) := needFpDest(i) && fpFreeList.io.canAllocate && fpFreeList.io.doAllocate && !io.robCommits.isWalk && !io.redirect.valid 278 vecSpecWen(i) := needVecDest(i) && fpFreeList.io.canAllocate && fpFreeList.io.doAllocate && !io.robCommits.isWalk && !io.redirect.valid 279 280 if (i < CommitWidth) { 281 walkIntSpecWen(i) := walkNeedIntDest(i) && !io.redirect.valid 282 walkPdest(i) := io.robCommits.info(i).pdest 283 } else { 284 walkPdest(i) := io.out(i).bits.pdest 285 } 286 } 287 288 /** 289 * How to set psrc: 290 * - bypass the pdest to psrc if previous instructions write to the same ldest as lsrc 291 * - default: psrc from RAT 292 * How to set pdest: 293 * - Mux(isMove, psrc, pdest_from_freelist). 294 * 295 * The critical path of rename lies here: 296 * When move elimination is enabled, we need to update the rat with psrc. 297 * However, psrc maybe comes from previous instructions' pdest, which comes from freelist. 298 * 299 * If we expand these logic for pdest(N): 300 * pdest(N) = Mux(isMove(N), psrc(N), freelist_out(N)) 301 * = Mux(isMove(N), Mux(bypass(N, N - 1), pdest(N - 1), 302 * Mux(bypass(N, N - 2), pdest(N - 2), 303 * ... 304 * Mux(bypass(N, 0), pdest(0), 305 * rat_out(N))...)), 306 * freelist_out(N)) 307 */ 308 // a simple functional model for now 309 io.out(0).bits.pdest := Mux(isMove(0), uops(0).psrc.head, uops(0).pdest) 310 311 // psrc(n) + pdest(1) 312 val bypassCond: Vec[MixedVec[UInt]] = Wire(Vec(numRegSrc + 1, MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))))) 313 require(io.in(0).bits.srcType.size == io.in(0).bits.numSrc) 314 private val pdestLoc = io.in.head.bits.srcType.size // 2 vector src: v0, vl&vtype 315 println(s"[Rename] idx of pdest in bypassCond $pdestLoc") 316 for (i <- 1 until RenameWidth) { 317 val vecCond = io.in(i).bits.srcType.map(_ === SrcType.vp) :+ needVecDest(i) 318 val fpCond = io.in(i).bits.srcType.map(_ === SrcType.fp) :+ needFpDest(i) 319 val intCond = io.in(i).bits.srcType.map(_ === SrcType.xp) :+ needIntDest(i) 320 val target = io.in(i).bits.lsrc :+ io.in(i).bits.ldest 321 for (((((cond1, cond2), cond3), t), j) <- vecCond.zip(fpCond).zip(intCond).zip(target).zipWithIndex) { 322 val destToSrc = io.in.take(i).zipWithIndex.map { case (in, j) => 323 val indexMatch = in.bits.ldest === t 324 val writeMatch = cond3 && needIntDest(j) || cond2 && needFpDest(j) || cond1 && needVecDest(j) 325 indexMatch && writeMatch 326 } 327 bypassCond(j)(i - 1) := VecInit(destToSrc).asUInt 328 } 329 io.out(i).bits.psrc(0) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(0)(i-1).asBools).foldLeft(uops(i).psrc(0)) { 330 (z, next) => Mux(next._2, next._1, z) 331 } 332 io.out(i).bits.psrc(1) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(1)(i-1).asBools).foldLeft(uops(i).psrc(1)) { 333 (z, next) => Mux(next._2, next._1, z) 334 } 335 io.out(i).bits.psrc(2) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(2)(i-1).asBools).foldLeft(uops(i).psrc(2)) { 336 (z, next) => Mux(next._2, next._1, z) 337 } 338 io.out(i).bits.psrc(3) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(3)(i-1).asBools).foldLeft(uops(i).psrc(3)) { 339 (z, next) => Mux(next._2, next._1, z) 340 } 341 io.out(i).bits.psrc(4) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(4)(i-1).asBools).foldLeft(uops(i).psrc(4)) { 342 (z, next) => Mux(next._2, next._1, z) 343 } 344 io.out(i).bits.pdest := Mux(isMove(i), io.out(i).bits.psrc(0), uops(i).pdest) 345 346 // Todo: better implementation for fields reuse 347 // For fused-lui-load, load.src(0) is replaced by the imm. 348 val last_is_lui = io.in(i - 1).bits.selImm === SelImm.IMM_U && io.in(i - 1).bits.srcType(0) =/= SrcType.pc 349 val this_is_load = io.in(i).bits.fuType === FuType.ldu.U 350 val lui_to_load = io.in(i - 1).valid && io.in(i - 1).bits.ldest === io.in(i).bits.lsrc(0) 351 val fused_lui_load = last_is_lui && this_is_load && lui_to_load && false.B // Todo: enable it 352 when (fused_lui_load) { 353 // The first LOAD operand (base address) is replaced by LUI-imm and stored in {psrc, imm} 354 val lui_imm = io.in(i - 1).bits.imm(19, 0) 355 val ld_imm = io.in(i).bits.imm 356 io.out(i).bits.srcType(0) := SrcType.imm 357 io.out(i).bits.imm := Imm_LUI_LOAD().immFromLuiLoad(lui_imm, ld_imm) 358 val psrcWidth = uops(i).psrc.head.getWidth 359 val lui_imm_in_imm = 20/*Todo: uops(i).imm.getWidth*/ - Imm_I().len 360 val left_lui_imm = Imm_U().len - lui_imm_in_imm 361 require(2 * psrcWidth >= left_lui_imm, "cannot fused lui and load with psrc") 362 io.out(i).bits.psrc(0) := lui_imm(lui_imm_in_imm + psrcWidth - 1, lui_imm_in_imm) 363 io.out(i).bits.psrc(1) := lui_imm(lui_imm.getWidth - 1, lui_imm_in_imm + psrcWidth) 364 } 365 366 } 367 368 val hasCFI = VecInit(io.in.map(in => (!in.bits.preDecodeInfo.notCFI || FuType.isJump(in.bits.fuType)) && in.fire)).asUInt.orR 369 val snapshotCtr = RegInit((4 * CommitWidth).U) 370 val allowSnpt = if (EnableRenameSnapshot) !snapshotCtr.orR else false.B 371 io.out.head.bits.snapshot := hasCFI && allowSnpt 372 when(io.out.head.fire && io.out.head.bits.snapshot) { 373 snapshotCtr := (4 * CommitWidth).U - PopCount(io.out.map(_.fire)) 374 }.elsewhen(io.out.head.fire) { 375 snapshotCtr := Mux(snapshotCtr < PopCount(io.out.map(_.fire)), 0.U, snapshotCtr - PopCount(io.out.map(_.fire))) 376 } 377 378 intFreeList.io.snpt := io.snpt 379 fpFreeList.io.snpt := io.snpt 380 intFreeList.io.snpt.snptEnq := io.out.head.fire && io.out.head.bits.snapshot 381 fpFreeList.io.snpt.snptEnq := io.out.head.fire && io.out.head.bits.snapshot 382 383 /** 384 * Instructions commit: update freelist and rename table 385 */ 386 for (i <- 0 until CommitWidth) { 387 val commitValid = io.robCommits.isCommit && io.robCommits.commitValid(i) 388 val walkValid = io.robCommits.isWalk && io.robCommits.walkValid(i) 389 390 // I. RAT Update 391 // When redirect happens (mis-prediction), don't update the rename table 392 io.intRenamePorts(i).wen := intSpecWen(i) 393 io.intRenamePorts(i).addr := uops(i).ldest 394 io.intRenamePorts(i).data := io.out(i).bits.pdest 395 396 io.fpRenamePorts(i).wen := fpSpecWen(i) 397 io.fpRenamePorts(i).addr := uops(i).ldest 398 io.fpRenamePorts(i).data := fpFreeList.io.allocatePhyReg(i) 399 400 io.vecRenamePorts(i).wen := vecSpecWen(i) 401 io.vecRenamePorts(i).addr := uops(i).ldest 402 io.vecRenamePorts(i).data := fpFreeList.io.allocatePhyReg(i) 403 404 // II. Free List Update 405 intFreeList.io.freeReq(i) := io.int_need_free(i) 406 intFreeList.io.freePhyReg(i) := RegNext(io.int_old_pdest(i)) 407 fpFreeList.io.freeReq(i) := RegNext(commitValid && (needDestRegCommit(Reg_F, io.robCommits.info(i)) || needDestRegCommit(Reg_V, io.robCommits.info(i)))) 408 fpFreeList.io.freePhyReg(i) := Mux(RegNext(needDestRegCommit(Reg_F, io.robCommits.info(i))), io.fp_old_pdest(i), io.vec_old_pdest(i)) 409 } 410 411 /* 412 Debug and performance counters 413 */ 414 def printRenameInfo(in: DecoupledIO[DecodedInst], out: DecoupledIO[DynInst]) = { 415 XSInfo(out.fire, p"pc:${Hexadecimal(in.bits.pc)} in(${in.valid},${in.ready}) " + 416 p"lsrc(0):${in.bits.lsrc(0)} -> psrc(0):${out.bits.psrc(0)} " + 417 p"lsrc(1):${in.bits.lsrc(1)} -> psrc(1):${out.bits.psrc(1)} " + 418 p"lsrc(2):${in.bits.lsrc(2)} -> psrc(2):${out.bits.psrc(2)} " + 419 p"ldest:${in.bits.ldest} -> pdest:${out.bits.pdest}\n" 420 ) 421 } 422 423 for ((x,y) <- io.in.zip(io.out)) { 424 printRenameInfo(x, y) 425 } 426 427 val debugRedirect = RegEnable(io.redirect.bits, io.redirect.valid) 428 // bad speculation 429 val recStall = io.redirect.valid || io.robCommits.isWalk 430 val ctrlRecStall = Mux(io.redirect.valid, io.redirect.bits.debugIsCtrl, io.robCommits.isWalk && debugRedirect.debugIsCtrl) 431 val mvioRecStall = Mux(io.redirect.valid, io.redirect.bits.debugIsMemVio, io.robCommits.isWalk && debugRedirect.debugIsMemVio) 432 val otherRecStall = recStall && !(ctrlRecStall || mvioRecStall) 433 XSPerfAccumulate("recovery_stall", recStall) 434 XSPerfAccumulate("control_recovery_stall", ctrlRecStall) 435 XSPerfAccumulate("mem_violation_recovery_stall", mvioRecStall) 436 XSPerfAccumulate("other_recovery_stall", otherRecStall) 437 // freelist stall 438 val notRecStall = !io.out.head.valid && !recStall 439 val intFlStall = notRecStall && hasValid && !intFreeList.io.canAllocate 440 val fpFlStall = notRecStall && hasValid && !fpFreeList.io.canAllocate 441 // other stall 442 val otherStall = notRecStall && !intFlStall && !fpFlStall 443 444 io.stallReason.in.backReason.valid := io.stallReason.out.backReason.valid || !io.in.head.ready 445 io.stallReason.in.backReason.bits := Mux(io.stallReason.out.backReason.valid, io.stallReason.out.backReason.bits, 446 MuxCase(TopDownCounters.OtherCoreStall.id.U, Seq( 447 ctrlRecStall -> TopDownCounters.ControlRecoveryStall.id.U, 448 mvioRecStall -> TopDownCounters.MemVioRecoveryStall.id.U, 449 otherRecStall -> TopDownCounters.OtherRecoveryStall.id.U, 450 intFlStall -> TopDownCounters.IntFlStall.id.U, 451 fpFlStall -> TopDownCounters.FpFlStall.id.U 452 ) 453 )) 454 io.stallReason.out.reason.zip(io.stallReason.in.reason).zip(io.in.map(_.valid)).foreach { case ((out, in), valid) => 455 out := Mux(io.stallReason.in.backReason.valid, 456 io.stallReason.in.backReason.bits, 457 Mux(valid, TopDownCounters.NoStall.id.U, in)) 458 } 459 460 XSDebug(io.robCommits.isWalk, p"Walk Recovery Enabled\n") 461 XSDebug(io.robCommits.isWalk, p"validVec:${Binary(io.robCommits.walkValid.asUInt)}\n") 462 for (i <- 0 until CommitWidth) { 463 val info = io.robCommits.info(i) 464 XSDebug(io.robCommits.isWalk && io.robCommits.walkValid(i), p"[#$i walk info] pc:${Hexadecimal(info.pc)} " + 465 p"ldest:${info.ldest} rfWen:${info.rfWen} fpWen:${info.fpWen} vecWen:${info.vecWen}") 466 } 467 468 XSDebug(p"inValidVec: ${Binary(Cat(io.in.map(_.valid)))}\n") 469 470 XSPerfAccumulate("in", Mux(RegNext(io.in(0).ready), PopCount(io.in.map(_.valid)), 0.U)) 471 XSPerfAccumulate("utilization", PopCount(io.in.map(_.valid))) 472 XSPerfAccumulate("waitInstr", PopCount((0 until RenameWidth).map(i => io.in(i).valid && !io.in(i).ready))) 473 XSPerfAccumulate("stall_cycle_dispatch", hasValid && !io.out(0).ready && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && !io.robCommits.isWalk) 474 XSPerfAccumulate("stall_cycle_fp", hasValid && io.out(0).ready && !fpFreeList.io.canAllocate && intFreeList.io.canAllocate && !io.robCommits.isWalk) 475 XSPerfAccumulate("stall_cycle_int", hasValid && io.out(0).ready && fpFreeList.io.canAllocate && !intFreeList.io.canAllocate && !io.robCommits.isWalk) 476 XSPerfAccumulate("stall_cycle_walk", hasValid && io.out(0).ready && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && io.robCommits.isWalk) 477 478 XSPerfHistogram("slots_fire", PopCount(io.out.map(_.fire)), true.B, 0, RenameWidth+1, 1) 479 // Explaination: when out(0) not fire, PopCount(valid) is not meaningfull 480 XSPerfHistogram("slots_valid_pure", PopCount(io.in.map(_.valid)), io.out(0).fire, 0, RenameWidth+1, 1) 481 XSPerfHistogram("slots_valid_rough", PopCount(io.in.map(_.valid)), true.B, 0, RenameWidth+1, 1) 482 483 XSPerfAccumulate("move_instr_count", PopCount(io.out.map(out => out.fire && out.bits.isMove))) 484 val is_fused_lui_load = io.out.map(o => o.fire && o.bits.fuType === FuType.ldu.U && o.bits.srcType(0) === SrcType.imm) 485 XSPerfAccumulate("fused_lui_load_instr_count", PopCount(is_fused_lui_load)) 486 487 488 val renamePerf = Seq( 489 ("rename_in ", PopCount(io.in.map(_.valid & io.in(0).ready )) ), 490 ("rename_waitinstr ", PopCount((0 until RenameWidth).map(i => io.in(i).valid && !io.in(i).ready)) ), 491 ("rename_stall_cycle_dispatch", hasValid && !io.out(0).ready && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && !io.robCommits.isWalk), 492 ("rename_stall_cycle_fp ", hasValid && io.out(0).ready && !fpFreeList.io.canAllocate && intFreeList.io.canAllocate && !io.robCommits.isWalk), 493 ("rename_stall_cycle_int ", hasValid && io.out(0).ready && fpFreeList.io.canAllocate && !intFreeList.io.canAllocate && !io.robCommits.isWalk), 494 ("rename_stall_cycle_walk ", hasValid && io.out(0).ready && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && io.robCommits.isWalk) 495 ) 496 val intFlPerf = intFreeList.getPerfEvents 497 val fpFlPerf = fpFreeList.getPerfEvents 498 val perfEvents = renamePerf ++ intFlPerf ++ fpFlPerf 499 generatePerfEvent() 500} 501