Rename.scala 14.9 KB
Newer Older
L
Lemover 已提交
1 2
/***************************************************************************************
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
Y
Yinan Xu 已提交
3
* Copyright (c) 2020-2021 Peng Cheng Laboratory
L
Lemover 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
*
* XiangShan is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

17 18
package xiangshan.backend.rename

19
import chipsalliance.rocketchip.config.Parameters
20 21 22
import chisel3._
import chisel3.util._
import xiangshan._
Y
Yinan Xu 已提交
23
import utils._
Y
Yinan Xu 已提交
24
import xiangshan.backend.rob.RobPtr
25
import xiangshan.backend.rename.freelist._
26
import xiangshan.mem.mdp._
27

28
class Rename(implicit p: Parameters) extends XSModule with HasPerfEvents {
29 30
  val io = IO(new Bundle() {
    val redirect = Flipped(ValidIO(new Redirect))
Y
Yinan Xu 已提交
31
    val robCommits = Flipped(new RobCommitIO)
32
    // from decode
33
    val in = Vec(RenameWidth, Flipped(DecoupledIO(new CfCtrl)))
34 35 36 37
    // ssit read result
    val ssit = Flipped(Vec(RenameWidth, Output(new SSITEntry)))
    // waittable read result
    val waittable = Flipped(Vec(RenameWidth, Output(Bool())))
38 39 40 41 42
    // to rename table
    val intReadPorts = Vec(RenameWidth, Vec(3, Input(UInt(PhyRegIdxWidth.W))))
    val fpReadPorts = Vec(RenameWidth, Vec(4, Input(UInt(PhyRegIdxWidth.W))))
    val intRenamePorts = Vec(RenameWidth, Output(new RatWritePort))
    val fpRenamePorts = Vec(RenameWidth, Output(new RatWritePort))
43
    // to dispatch1
44
    val out = Vec(RenameWidth, DecoupledIO(new MicroOp))
45
  })
46

47
  // create free list and rat
48 49 50
  val intFreeList = Module(new MEFreeList(NRPhyRegs))
  val intRefCounter = Module(new RefCounter(NRPhyRegs))
  val fpFreeList = Module(new StdFreeList(NRPhyRegs - 32))
L
LinJiawei 已提交
51

Y
Yinan Xu 已提交
52
  // decide if given instruction needs allocating a new physical register (CfCtrl: from decode; RobCommitInfo: from rob)
53 54 55
  def needDestReg[T <: CfCtrl](fp: Boolean, x: T): Bool = {
    {if(fp) x.ctrl.fpWen else x.ctrl.rfWen && (x.ctrl.ldest =/= 0.U)}
  }
Y
Yinan Xu 已提交
56
  def needDestRegCommit[T <: RobCommitInfo](fp: Boolean, x: T): Bool = {
57
    if(fp) x.fpWen else x.rfWen
58 59
  }

60
  // connect [redirect + walk] ports for __float point__ & __integer__ free list
61
  Seq((fpFreeList, true), (intFreeList, false)).foreach{ case (fl, isFp) =>
62 63
    fl.io.redirect := io.redirect.valid
    fl.io.walk := io.robCommits.isWalk
64 65
    // when isWalk, use stepBack to restore head pointer of free list
    // (if ME enabled, stepBack of intFreeList should be useless thus optimized out)
66
    fl.io.stepBack := PopCount(io.robCommits.valid.zip(io.robCommits.info).map{case (v, i) => v && needDestRegCommit(isFp, i)})
67
  }
68 69
  // walk has higher priority than allocation and thus we don't use isWalk here
  // only when both fp and int free list and dispatch1 has enough space can we do allocation
70 71
  intFreeList.io.doAllocate := fpFreeList.io.canAllocate && io.out(0).ready
  fpFreeList.io.doAllocate := intFreeList.io.canAllocate && io.out(0).ready
72 73

  //           dispatch1 ready ++ float point free list ready ++ int free list ready      ++ not walk
74
  val canOut = io.out(0).ready && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && !io.robCommits.isWalk
75

76

Y
Yinan Xu 已提交
77 78 79
  // speculatively assign the instruction with an robIdx
  val validCount = PopCount(io.in.map(_.valid)) // number of instructions waiting to enter rob (from decode)
  val robIdxHead = RegInit(0.U.asTypeOf(new RobPtr))
80
  val lastCycleMisprediction = RegNext(io.redirect.valid && !io.redirect.bits.flushItself())
81
  val robIdxHeadNext = Mux(io.redirect.valid, io.redirect.bits.robIdx, // redirect: move ptr to given rob index
Y
Yinan Xu 已提交
82 83
         Mux(lastCycleMisprediction, robIdxHead + 1.U, // mis-predict: not flush robIdx itself
                         Mux(canOut, robIdxHead + validCount, // instructions successfully entered next stage: increase robIdx
84
                      /* default */  robIdxHead))) // no instructions passed by this cycle: stick to old value
Y
Yinan Xu 已提交
85
  robIdxHead := robIdxHeadNext
86

Y
Yinan Xu 已提交
87 88 89
  /**
    * Rename: allocate free physical register and update rename table
    */
90 91
  val uops = Wire(Vec(RenameWidth, new MicroOp))
  uops.foreach( uop => {
92 93 94
    uop.srcState(0) := DontCare
    uop.srcState(1) := DontCare
    uop.srcState(2) := DontCare
Y
Yinan Xu 已提交
95
    uop.robIdx := DontCare
96
    uop.diffTestDebugLrScValid := DontCare
Y
Yinan Xu 已提交
97
    uop.debugInfo := DontCare
98 99
    uop.lqIdx := DontCare
    uop.sqIdx := DontCare
100 101
  })

102 103
  val needFpDest = Wire(Vec(RenameWidth, Bool()))
  val needIntDest = Wire(Vec(RenameWidth, Bool()))
104
  val hasValid = Cat(io.in.map(_.valid)).orR
105 106

  val isMove = io.in.map(_.bits.ctrl.isMove)
107
  val intPsrc = Wire(Vec(RenameWidth, UInt()))
108 109 110 111 112

  val intSpecWen = Wire(Vec(RenameWidth, Bool()))
  val fpSpecWen = Wire(Vec(RenameWidth, Bool()))

  // uop calculation
Y
Yinan Xu 已提交
113
  for (i <- 0 until RenameWidth) {
114 115 116
    uops(i).cf := io.in(i).bits.cf
    uops(i).ctrl := io.in(i).bits.ctrl

117 118 119 120 121 122 123 124
    // update cf according to ssit result
    uops(i).cf.storeSetHit := io.ssit(i).valid
    uops(i).cf.loadWaitStrict := io.ssit(i).strict && io.ssit(i).valid
    uops(i).cf.ssid := io.ssit(i).ssid

    // update cf according to waittable result
    uops(i).cf.loadWaitBit := io.waittable(i)

125
    val inValid = io.in(i).valid
L
LinJiawei 已提交
126

127
    // alloc a new phy reg
128 129
    needFpDest(i) := inValid && needDestReg(fp = true, io.in(i).bits)
    needIntDest(i) := inValid && needDestReg(fp = false, io.in(i).bits)
130 131
    fpFreeList.io.allocateReq(i) := needFpDest(i)
    intFreeList.io.allocateReq(i) := needIntDest(i) && !isMove(i)
L
LinJiawei 已提交
132

133
    // no valid instruction from decode stage || all resources (dispatch1 + both free lists) ready
134
    io.in(i).ready := !hasValid || canOut
L
LinJiawei 已提交
135

Y
Yinan Xu 已提交
136
    uops(i).robIdx := robIdxHead + PopCount(io.in.take(i).map(_.valid))
137

138 139
    val intPhySrcVec = io.intReadPorts(i).take(2)
    val intOldPdest = io.intReadPorts(i).last
140
    intPsrc(i) := intPhySrcVec(0)
141 142
    val fpPhySrcVec = io.fpReadPorts(i).take(3)
    val fpOldPdest = io.fpReadPorts(i).last
143 144 145
    uops(i).psrc(0) := Mux(uops(i).ctrl.srcType(0) === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0))
    uops(i).psrc(1) := Mux(uops(i).ctrl.srcType(1) === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1))
    uops(i).psrc(2) := fpPhySrcVec(2)
146
    uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest)
147
    uops(i).eliminatedMove := isMove(i)
148 149

    // update pdest
150 151 152 153
    uops(i).pdest := Mux(needIntDest(i), intFreeList.io.allocatePhyReg(i), // normal int inst
      // normal fp inst
      Mux(needFpDest(i), fpFreeList.io.allocatePhyReg(i),
        /* default */0.U))
154

155 156 157
    // Assign performance counters
    uops(i).debugInfo.renameTime := GTimer()

158
    io.out(i).valid := io.in(i).valid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && !io.robCommits.isWalk
159
    io.out(i).bits := uops(i)
160 161 162
    when (io.out(i).bits.ctrl.fuType === FuType.fence) {
      io.out(i).bits.ctrl.imm := Cat(io.in(i).bits.ctrl.lsrc(1), io.in(i).bits.ctrl.lsrc(0))
    }
163

164
    // write speculative rename table
165
    // we update rat later inside commit code
166 167 168 169 170
    intSpecWen(i) := needIntDest(i) && intFreeList.io.canAllocate && intFreeList.io.doAllocate && !io.robCommits.isWalk && !io.redirect.valid
    fpSpecWen(i) := needFpDest(i) && fpFreeList.io.canAllocate && fpFreeList.io.doAllocate && !io.robCommits.isWalk && !io.redirect.valid

    intRefCounter.io.allocate(i).valid := intSpecWen(i)
    intRefCounter.io.allocate(i).bits := io.out(i).bits.pdest
171 172
  }

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  /**
    * How to set psrc:
    * - bypass the pdest to psrc if previous instructions write to the same ldest as lsrc
    * - default: psrc from RAT
    * How to set pdest:
    * - Mux(isMove, psrc, pdest_from_freelist).
    *
    * The critical path of rename lies here:
    * When move elimination is enabled, we need to update the rat with psrc.
    * However, psrc maybe comes from previous instructions' pdest, which comes from freelist.
    *
    * If we expand these logic for pdest(N):
    * pdest(N) = Mux(isMove(N), psrc(N), freelist_out(N))
    *          = Mux(isMove(N), Mux(bypass(N, N - 1), pdest(N - 1),
    *                           Mux(bypass(N, N - 2), pdest(N - 2),
    *                           ...
    *                           Mux(bypass(N, 0),     pdest(0),
    *                                                 rat_out(N))...)),
    *                           freelist_out(N))
    */
  // a simple functional model for now
  io.out(0).bits.pdest := Mux(isMove(0), uops(0).psrc.head, uops(0).pdest)
  val bypassCond = Wire(Vec(4, MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))))
196
  for (i <- 1 until RenameWidth) {
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    val fpCond = io.in(i).bits.ctrl.srcType.map(_ === SrcType.fp) :+ needFpDest(i)
    val intCond = io.in(i).bits.ctrl.srcType.map(_ === SrcType.reg) :+ needIntDest(i)
    val target = io.in(i).bits.ctrl.lsrc :+ io.in(i).bits.ctrl.ldest
    for ((((cond1, cond2), t), j) <- fpCond.zip(intCond).zip(target).zipWithIndex) {
      val destToSrc = io.in.take(i).zipWithIndex.map { case (in, j) =>
        val indexMatch = in.bits.ctrl.ldest === t
        val writeMatch =  cond2 && needIntDest(j) || cond1 && needFpDest(j)
        indexMatch && writeMatch
      }
      bypassCond(j)(i - 1) := VecInit(destToSrc).asUInt
    }
    io.out(i).bits.psrc(0) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(0)(i-1).asBools).foldLeft(uops(i).psrc(0)) {
      (z, next) => Mux(next._2, next._1, z)
    }
    io.out(i).bits.psrc(1) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(1)(i-1).asBools).foldLeft(uops(i).psrc(1)) {
      (z, next) => Mux(next._2, next._1, z)
    }
    io.out(i).bits.psrc(2) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(2)(i-1).asBools).foldLeft(uops(i).psrc(2)) {
      (z, next) => Mux(next._2, next._1, z)
    }
    io.out(i).bits.old_pdest := io.out.take(i).map(_.bits.pdest).zip(bypassCond(3)(i-1).asBools).foldLeft(uops(i).old_pdest) {
      (z, next) => Mux(next._2, next._1, z)
    }
    io.out(i).bits.pdest := Mux(isMove(i), io.out(i).bits.psrc(0), uops(i).pdest)
221
  }
Y
Yinan Xu 已提交
222 223 224 225 226 227

  /**
    * Instructions commit: update freelist and rename table
    */
  for (i <- 0 until CommitWidth) {

228
    Seq((io.intRenamePorts, false), (io.fpRenamePorts, true)) foreach { case (rat, fp) =>
229
      // is valid commit req and given instruction has destination register
Y
Yinan Xu 已提交
230 231
      val commitDestValid = io.robCommits.valid(i) && needDestRegCommit(fp, io.robCommits.info(i))
      XSDebug(p"isFp[${fp}]index[$i]-commitDestValid:$commitDestValid,isWalk:${io.robCommits.isWalk}\n")
232 233 234 235 236 237 238

      /*
      I. RAT Update
       */

      // walk back write - restore spec state : ldest => old_pdest
      if (fp && i < RenameWidth) {
239
        // When redirect happens (mis-prediction), don't update the rename table
240
        rat(i).wen := fpSpecWen(i)
241
        rat(i).addr := uops(i).ctrl.ldest
242
        rat(i).data := fpFreeList.io.allocatePhyReg(i)
243
      } else if (!fp && i < RenameWidth) {
244
        rat(i).wen := intSpecWen(i)
245
        rat(i).addr := uops(i).ctrl.ldest
246
        rat(i).data := io.out(i).bits.pdest
247
      }
Y
Yinan Xu 已提交
248

249 250 251 252
      /*
      II. Free List Update
       */
      if (fp) { // Float Point free list
253 254
        fpFreeList.io.freeReq(i)  := commitDestValid && !io.robCommits.isWalk
        fpFreeList.io.freePhyReg(i) := io.robCommits.info(i).old_pdest
255
      } else { // Integer free list
256 257
        intFreeList.io.freeReq(i) := intRefCounter.io.freeRegs(i).valid
        intFreeList.io.freePhyReg(i) := intRefCounter.io.freeRegs(i).bits
258
      }
Y
Yinan Xu 已提交
259
    }
260 261
    intRefCounter.io.deallocate(i).valid := io.robCommits.valid(i) && needDestRegCommit(false, io.robCommits.info(i))
    intRefCounter.io.deallocate(i).bits := Mux(io.robCommits.isWalk, io.robCommits.info(i).pdest, io.robCommits.info(i).old_pdest)
Y
Yinan Xu 已提交
262
  }
Y
Yinan Xu 已提交
263

264
  /*
265
  Debug and performance counters
266 267
   */
  def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = {
268 269 270 271 272 273
    XSInfo(out.fire, p"pc:${Hexadecimal(in.bits.cf.pc)} in(${in.valid},${in.ready}) " +
      p"lsrc(0):${in.bits.ctrl.lsrc(0)} -> psrc(0):${out.bits.psrc(0)} " +
      p"lsrc(1):${in.bits.ctrl.lsrc(1)} -> psrc(1):${out.bits.psrc(1)} " +
      p"lsrc(2):${in.bits.ctrl.lsrc(2)} -> psrc(2):${out.bits.psrc(2)} " +
      p"ldest:${in.bits.ctrl.ldest} -> pdest:${out.bits.pdest} " +
      p"old_pdest:${out.bits.old_pdest}\n"
274 275 276 277 278 279 280
    )
  }

  for((x,y) <- io.in.zip(io.out)){
    printRenameInfo(x, y)
  }

Y
Yinan Xu 已提交
281 282
  XSDebug(io.robCommits.isWalk, p"Walk Recovery Enabled\n")
  XSDebug(io.robCommits.isWalk, p"validVec:${Binary(io.robCommits.valid.asUInt)}\n")
283
  for (i <- 0 until CommitWidth) {
Y
Yinan Xu 已提交
284 285
    val info = io.robCommits.info(i)
    XSDebug(io.robCommits.isWalk && io.robCommits.valid(i), p"[#$i walk info] pc:${Hexadecimal(info.pc)} " +
286
      p"ldest:${info.ldest} rfWen:${info.rfWen} fpWen:${info.fpWen} " +
287 288 289 290 291
      p"pdest:${info.pdest} old_pdest:${info.old_pdest}\n")
  }

  XSDebug(p"inValidVec: ${Binary(Cat(io.in.map(_.valid)))}\n")

292 293 294
  XSPerfAccumulate("in", Mux(RegNext(io.in(0).ready), PopCount(io.in.map(_.valid)), 0.U))
  XSPerfAccumulate("utilization", PopCount(io.in.map(_.valid)))
  XSPerfAccumulate("waitInstr", PopCount((0 until RenameWidth).map(i => io.in(i).valid && !io.in(i).ready)))
295 296 297 298 299 300
  XSPerfAccumulate("stall_cycle_dispatch", hasValid && !io.out(0).ready && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && !io.robCommits.isWalk)
  XSPerfAccumulate("stall_cycle_fp", hasValid && io.out(0).ready && !fpFreeList.io.canAllocate && intFreeList.io.canAllocate && !io.robCommits.isWalk)
  XSPerfAccumulate("stall_cycle_int", hasValid && io.out(0).ready && fpFreeList.io.canAllocate && !intFreeList.io.canAllocate && !io.robCommits.isWalk)
  XSPerfAccumulate("stall_cycle_walk", hasValid && io.out(0).ready && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && io.robCommits.isWalk)

  XSPerfAccumulate("move_instr_count", PopCount(io.out.map(out => out.fire() && out.bits.ctrl.isMove)))
301 302


303 304 305 306 307 308 309 310 311 312 313 314
  val renamePerf = Seq(
    ("rename_in                  ", PopCount(io.in.map(_.valid & io.in(0).ready ))                                                               ),
    ("rename_waitinstr           ", PopCount((0 until RenameWidth).map(i => io.in(i).valid && !io.in(i).ready))                                  ),
    ("rename_stall_cycle_dispatch", hasValid && !io.out(0).ready &&  fpFreeList.io.canAllocate &&  intFreeList.io.canAllocate && !io.robCommits.isWalk),
    ("rename_stall_cycle_fp      ", hasValid &&  io.out(0).ready && !fpFreeList.io.canAllocate &&  intFreeList.io.canAllocate && !io.robCommits.isWalk),
    ("rename_stall_cycle_int     ", hasValid &&  io.out(0).ready &&  fpFreeList.io.canAllocate && !intFreeList.io.canAllocate && !io.robCommits.isWalk),
    ("rename_stall_cycle_walk    ", hasValid &&  io.out(0).ready &&  fpFreeList.io.canAllocate &&  intFreeList.io.canAllocate &&  io.robCommits.isWalk)
  )
  val intFlPerf = intFreeList.getPerfEvents
  val fpFlPerf = fpFreeList.getPerfEvents
  val perfEvents = renamePerf ++ intFlPerf ++ fpFlPerf
  generatePerfEvent()
315
}