Rename.scala 8.2 KB
Newer Older
1 2 3 4 5
package xiangshan.backend.rename

import chisel3._
import chisel3.util._
import xiangshan._
6
import utils.XSInfo
7

8 9 10 11 12 13 14
class RenameBypassInfo extends XSBundle {
  val lsrc1_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
  val lsrc2_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
  val lsrc3_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
  val ldest_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
}

15
class Rename extends XSModule {
16 17
  val io = IO(new Bundle() {
    val redirect = Flipped(ValidIO(new Redirect))
Y
Yinan Xu 已提交
18
    val roqCommits = Flipped(new RoqCommitIO)
19
    // from decode buffer
20
    val in = Vec(RenameWidth, Flipped(DecoupledIO(new CfCtrl)))
21
    // to dispatch1
22
    val out = Vec(RenameWidth, DecoupledIO(new MicroOp))
23
    val renameBypass = Output(new RenameBypassInfo)
24
  })
25

L
LinJiawei 已提交
26 27
  def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = {
    XSInfo(
28
      in.valid && in.ready,
L
LinJiawei 已提交
29
      p"pc:${Hexadecimal(in.bits.cf.pc)} in v:${in.valid} in rdy:${in.ready} " +
L
LinJiawei 已提交
30 31 32 33
        p"lsrc1:${in.bits.ctrl.lsrc1} -> psrc1:${out.bits.psrc1} " +
        p"lsrc2:${in.bits.ctrl.lsrc2} -> psrc2:${out.bits.psrc2} " +
        p"lsrc3:${in.bits.ctrl.lsrc3} -> psrc3:${out.bits.psrc3} " +
        p"ldest:${in.bits.ctrl.ldest} -> pdest:${out.bits.pdest} " +
L
LinJiawei 已提交
34
        p"old_pdest:${out.bits.old_pdest} " +
L
LinJiawei 已提交
35
        p"out v:${out.valid} r:${out.ready}\n"
L
LinJiawei 已提交
36 37 38 39 40 41 42
    )
  }

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

43 44 45 46
  val fpFreeList, intFreeList = Module(new FreeList).io
  val fpRat = Module(new RenameTable(float = true)).io
  val intRat = Module(new RenameTable(float = false)).io

L
LinJiawei 已提交
47
  fpFreeList.redirect := io.redirect
48 49
  intFreeList.redirect := io.redirect

50
  val flush = io.redirect.valid && (io.redirect.bits.isException || io.redirect.bits.isFlushPipe) // TODO: need check by JiaWei
51 52 53 54 55 56
  fpRat.flush := flush
  intRat.flush := flush

  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 已提交
57
  val walkValid = io.roqCommits.hasWalkInstr
58 59
  fpFreeList.walk.valid := walkValid
  intFreeList.walk.valid := walkValid
Y
Yinan Xu 已提交
60 61
  fpFreeList.walk.bits := PopCount((0 until CommitWidth).map(i => io.roqCommits.valid(i) && needDestReg(true, io.roqCommits.uop(i))))
  intFreeList.walk.bits := PopCount((0 until CommitWidth).map(i => io.roqCommits.valid(i) && needDestReg(false, io.roqCommits.uop(i))))
62 63
  fpFreeList.req.doAlloc := intFreeList.req.canAlloc && io.out(0).ready
  intFreeList.req.doAlloc := fpFreeList.req.canAlloc && io.out(0).ready
64 65 66 67

  val uops = Wire(Vec(RenameWidth, new MicroOp))

  uops.foreach( uop => {
Y
Yinan Xu 已提交
68 69
//    uop.brMask := DontCare
//    uop.brTag := DontCare
70 71 72 73
    uop.src1State := DontCare
    uop.src2State := DontCare
    uop.src3State := DontCare
    uop.roqIdx := DontCare
74
    uop.diffTestDebugLrScValid := DontCare
75 76
    uop.lqIdx := DontCare
    uop.sqIdx := DontCare
77 78
  })

79 80
  val needFpDest = Wire(Vec(RenameWidth, Bool()))
  val needIntDest = Wire(Vec(RenameWidth, Bool()))
Y
Yinan Xu 已提交
81
  for(i <- 0 until RenameWidth) {
82 83
    uops(i).cf := io.in(i).bits.cf
    uops(i).ctrl := io.in(i).bits.ctrl
Y
Yinan Xu 已提交
84
    uops(i).brTag := io.in(i).bits.brTag
85

86
    val inValid = io.in(i).valid
L
LinJiawei 已提交
87

88
    // alloc a new phy reg
89 90
    needFpDest(i) := inValid && needDestReg(fp = true, io.in(i).bits)
    needIntDest(i) := inValid && needDestReg(fp = false, io.in(i).bits)
91 92 93 94
    fpFreeList.req.allocReqs(i) := needFpDest(i)
    intFreeList.req.allocReqs(i) := needIntDest(i)

    io.in(i).ready := io.out(i).ready && fpFreeList.req.canAlloc && intFreeList.req.canAlloc
L
LinJiawei 已提交
95

L
LinJiawei 已提交
96
    // do checkpoints when a branch inst come
Y
Yinan Xu 已提交
97 98 99 100
    // for(fl <- Seq(fpFreeList, intFreeList)){
    //   fl.cpReqs(i).valid := inValid
    //   fl.cpReqs(i).bits := io.in(i).bits.brTag
    // }
L
LinJiawei 已提交
101

102
    uops(i).pdest := Mux(needIntDest(i),
103
      intFreeList.req.pdests(i),
L
LinJiawei 已提交
104 105
      Mux(
        uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen,
106
        0.U, fpFreeList.req.pdests(i)
L
LinJiawei 已提交
107 108
      )
    )
109

110
    io.out(i).valid := io.in(i).valid && intFreeList.req.canAlloc && fpFreeList.req.canAlloc
111 112 113 114 115 116 117
    io.out(i).bits := uops(i)

    // write rename table
    def writeRat(fp: Boolean) = {
      val rat = if(fp) fpRat else intRat
      val freeList = if(fp) fpFreeList else intFreeList
      // speculative inst write
118
      val specWen = freeList.req.allocReqs(i) && freeList.req.canAlloc && freeList.req.doAlloc
119
      // walk back write
Y
Yinan Xu 已提交
120 121
      val commitDestValid = io.roqCommits.valid(i) && needDestReg(fp, io.roqCommits.uop(i))
      val walkWen = commitDestValid && io.roqCommits.isWalk
122 123

      rat.specWritePorts(i).wen := specWen || walkWen
Y
Yinan Xu 已提交
124 125
      rat.specWritePorts(i).addr := Mux(specWen, uops(i).ctrl.ldest, io.roqCommits.uop(i).ctrl.ldest)
      rat.specWritePorts(i).wdata := Mux(specWen, freeList.req.pdests(i), io.roqCommits.uop(i).old_pdest)
126

L
LinJiawei 已提交
127
      XSInfo(walkWen,
Y
Yinan Xu 已提交
128
        {if(fp) p"fp" else p"int "} + p"walk: pc:${Hexadecimal(io.roqCommits.uop(i).cf.pc)}" +
129
          p" ldest:${rat.specWritePorts(i).addr} old_pdest:${rat.specWritePorts(i).wdata}\n"
L
LinJiawei 已提交
130 131
      )

Y
Yinan Xu 已提交
132 133 134
      rat.archWritePorts(i).wen := commitDestValid && !io.roqCommits.isWalk
      rat.archWritePorts(i).addr := io.roqCommits.uop(i).ctrl.ldest
      rat.archWritePorts(i).wdata := io.roqCommits.uop(i).pdest
135

L
LinJiawei 已提交
136
      XSInfo(rat.archWritePorts(i).wen,
L
LinJiawei 已提交
137
        {if(fp) p"fp" else p"int "} + p" rat arch: ldest:${rat.archWritePorts(i).addr}" +
L
LinJiawei 已提交
138 139 140
          p" pdest:${rat.archWritePorts(i).wdata}\n"
      )

141
      freeList.deallocReqs(i) := rat.archWritePorts(i).wen
Y
Yinan Xu 已提交
142
      freeList.deallocPregs(i) := io.roqCommits.uop(i).old_pdest
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

    }

    writeRat(fp = false)
    writeRat(fp = true)

    // read rename table
    def readRat(lsrcList: List[UInt], ldest: UInt, fp: Boolean) = {
      val rat = if(fp) fpRat else intRat
      val srcCnt = lsrcList.size
      val psrcVec = Wire(Vec(srcCnt, UInt(PhyRegIdxWidth.W)))
      val old_pdest = Wire(UInt(PhyRegIdxWidth.W))
      for(k <- 0 until srcCnt+1){
        val rportIdx = i * (srcCnt+1) + k
        if(k != srcCnt){
          rat.readPorts(rportIdx).addr := lsrcList(k)
          psrcVec(k) := rat.readPorts(rportIdx).rdata
        } else {
          rat.readPorts(rportIdx).addr := ldest
          old_pdest := rat.readPorts(rportIdx).rdata
        }
      }
      (psrcVec, old_pdest)
    }
    val lsrcList = List(uops(i).ctrl.lsrc1, uops(i).ctrl.lsrc2, uops(i).ctrl.lsrc3)
    val ldest = uops(i).ctrl.ldest
    val (intPhySrcVec, intOldPdest) = readRat(lsrcList.take(2), ldest, fp = false)
    val (fpPhySrcVec, fpOldPdest) = readRat(lsrcList, ldest, fp = true)
    uops(i).psrc1 := Mux(uops(i).ctrl.src1Type === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0))
L
LinJiawei 已提交
172
    uops(i).psrc2 := Mux(uops(i).ctrl.src2Type === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1))
173 174 175 176
    uops(i).psrc3 := fpPhySrcVec(2)
    uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest)
  }

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  // We don't bypass the old_pdest from valid instructions with the same ldest currently in rename stage.
  // Instead, we determine whether there're some dependences between the valid instructions.
  for (i <- 1 until RenameWidth) {
    io.renameBypass.lsrc1_bypass(i-1) := Cat((0 until i).map(j => {
      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.src1Type === SrcType.fp
      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src1Type === SrcType.reg
      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc1
    }).reverse)
    io.renameBypass.lsrc2_bypass(i-1) := Cat((0 until i).map(j => {
      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.src2Type === SrcType.fp
      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src2Type === SrcType.reg
      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc2
    }).reverse)
    io.renameBypass.lsrc3_bypass(i-1) := Cat((0 until i).map(j => {
      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.src3Type === SrcType.fp
      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src3Type === SrcType.reg
      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc3
    }).reverse)
    io.renameBypass.ldest_bypass(i-1) := Cat((0 until i).map(j => {
      val fpMatch  = needFpDest(j) && needFpDest(i)
      val intMatch = needIntDest(j) && needIntDest(i)
      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.ldest
    }).reverse)
  }
201
}