xref: /XiangShan/src/main/scala/xiangshan/backend/rename/Rename.scala (revision 53c7ab8b2e79de0fa70909ea81a51720f666aaeb)
1package xiangshan.backend.rename
2
3import chisel3._
4import chisel3.util._
5import xiangshan._
6import utils._
7import xiangshan.backend.roq.RoqPtr
8
9class RenameBypassInfo extends XSBundle {
10  val lsrc1_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
11  val lsrc2_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
12  val lsrc3_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
13  val ldest_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
14}
15
16class Rename extends XSModule with HasCircularQueuePtrHelper {
17  val io = IO(new Bundle() {
18    val redirect = Flipped(ValidIO(new Redirect))
19    val flush = Input(Bool())
20    val roqCommits = Flipped(new RoqCommitIO)
21    // from decode buffer
22    val in = Vec(RenameWidth, Flipped(DecoupledIO(new CfCtrl)))
23    // to dispatch1
24    val out = Vec(RenameWidth, DecoupledIO(new MicroOp))
25    val renameBypass = Output(new RenameBypassInfo)
26  })
27
28  def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = {
29    XSInfo(
30      in.valid && in.ready,
31      p"pc:${Hexadecimal(in.bits.cf.pc)} in v:${in.valid} in rdy:${in.ready} " +
32        p"lsrc1:${in.bits.ctrl.lsrc1} -> psrc1:${out.bits.psrc1} " +
33        p"lsrc2:${in.bits.ctrl.lsrc2} -> psrc2:${out.bits.psrc2} " +
34        p"lsrc3:${in.bits.ctrl.lsrc3} -> psrc3:${out.bits.psrc3} " +
35        p"ldest:${in.bits.ctrl.ldest} -> pdest:${out.bits.pdest} " +
36        p"old_pdest:${out.bits.old_pdest} " +
37        p"out v:${out.valid} r:${out.ready}\n"
38    )
39  }
40
41  for((x,y) <- io.in.zip(io.out)){
42    printRenameInfo(x, y)
43  }
44
45  val intFreeList, fpFreeList = Module(new FreeList).io
46  val intRat = Module(new RenameTable(float = false)).io
47  val fpRat = Module(new RenameTable(float = true)).io
48  val allPhyResource = Seq((intRat, intFreeList, false), (fpRat, fpFreeList, true))
49
50  allPhyResource.map{ case (rat, freelist, _) =>
51    rat.redirect := io.redirect.valid
52    rat.flush := io.flush
53    rat.walkWen := io.roqCommits.isWalk
54    freelist.redirect := io.redirect.valid
55    freelist.flush := io.flush
56    freelist.walk.valid := io.roqCommits.isWalk
57  }
58  val canOut = io.out(0).ready && fpFreeList.req.canAlloc && intFreeList.req.canAlloc && !io.roqCommits.isWalk
59
60  def needDestReg[T <: CfCtrl](fp: Boolean, x: T): Bool = {
61    {if(fp) x.ctrl.fpWen else x.ctrl.rfWen && (x.ctrl.ldest =/= 0.U)}
62  }
63  def needDestRegCommit[T <: RoqCommitInfo](fp: Boolean, x: T): Bool = {
64    {if(fp) x.fpWen else x.rfWen && (x.ldest =/= 0.U)}
65  }
66  fpFreeList.walk.bits := PopCount(io.roqCommits.valid.zip(io.roqCommits.info).map{case (v, i) => v && needDestRegCommit(true, i)})
67  intFreeList.walk.bits := PopCount(io.roqCommits.valid.zip(io.roqCommits.info).map{case (v, i) => v && needDestRegCommit(false, i)})
68  // walk has higher priority than allocation and thus we don't use isWalk here
69  fpFreeList.req.doAlloc := intFreeList.req.canAlloc && io.out(0).ready
70  intFreeList.req.doAlloc := fpFreeList.req.canAlloc && io.out(0).ready
71
72  // speculatively assign the instruction with an roqIdx
73  val validCount = PopCount(io.in.map(_.valid))
74  val roqIdxHead = RegInit(0.U.asTypeOf(new RoqPtr))
75  val lastCycleMisprediction = RegNext(io.redirect.valid && !io.redirect.bits.flushItself())
76  val roqIdxHeadNext = Mux(io.flush,
77    0.U.asTypeOf(new RoqPtr),
78    Mux(io.redirect.valid,
79      io.redirect.bits.roqIdx,
80      Mux(lastCycleMisprediction,
81        roqIdxHead + 1.U,
82        Mux(canOut, roqIdxHead + validCount, roqIdxHead))
83    )
84  )
85  roqIdxHead := roqIdxHeadNext
86
87  /**
88    * Rename: allocate free physical register and update rename table
89    */
90  val uops = Wire(Vec(RenameWidth, new MicroOp))
91
92  uops.foreach( uop => {
93//    uop.brMask := DontCare
94//    uop.brTag := DontCare
95    uop.src1State := DontCare
96    uop.src2State := DontCare
97    uop.src3State := DontCare
98    uop.roqIdx := DontCare
99    uop.diffTestDebugLrScValid := DontCare
100    uop.debugInfo := DontCare
101    uop.lqIdx := DontCare
102    uop.sqIdx := DontCare
103  })
104
105  val needFpDest = Wire(Vec(RenameWidth, Bool()))
106  val needIntDest = Wire(Vec(RenameWidth, Bool()))
107  val hasValid = Cat(io.in.map(_.valid)).orR
108  for (i <- 0 until RenameWidth) {
109    uops(i).cf := io.in(i).bits.cf
110    uops(i).ctrl := io.in(i).bits.ctrl
111
112    val inValid = io.in(i).valid
113
114    // alloc a new phy reg
115    needFpDest(i) := inValid && needDestReg(fp = true, io.in(i).bits)
116    needIntDest(i) := inValid && needDestReg(fp = false, io.in(i).bits)
117    fpFreeList.req.allocReqs(i) := needFpDest(i)
118    intFreeList.req.allocReqs(i) := needIntDest(i)
119
120    io.in(i).ready := !hasValid || canOut
121
122    // do checkpoints when a branch inst come
123    // for(fl <- Seq(fpFreeList, intFreeList)){
124    //   fl.cpReqs(i).valid := inValid
125    //   fl.cpReqs(i).bits := io.in(i).bits.brTag
126    // }
127
128    uops(i).pdest := Mux(needIntDest(i),
129      intFreeList.req.pdests(i),
130      Mux(
131        uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen,
132        0.U, fpFreeList.req.pdests(i)
133      )
134    )
135
136    uops(i).roqIdx := roqIdxHead + i.U
137
138    io.out(i).valid := io.in(i).valid && intFreeList.req.canAlloc && fpFreeList.req.canAlloc && !io.roqCommits.isWalk
139    io.out(i).bits := uops(i)
140
141    // write speculative rename table
142    allPhyResource.map{ case (rat, freelist, _) =>
143      val specWen = freelist.req.allocReqs(i) && freelist.req.canAlloc && freelist.req.doAlloc && !io.roqCommits.isWalk
144
145      rat.specWritePorts(i).wen := specWen
146      rat.specWritePorts(i).addr := uops(i).ctrl.ldest
147      rat.specWritePorts(i).wdata := freelist.req.pdests(i)
148
149      freelist.deallocReqs(i) := specWen
150    }
151
152    // read rename table
153    def readRat(lsrcList: List[UInt], ldest: UInt, fp: Boolean) = {
154      val rat = if(fp) fpRat else intRat
155      val srcCnt = lsrcList.size
156      val psrcVec = Wire(Vec(srcCnt, UInt(PhyRegIdxWidth.W)))
157      val old_pdest = Wire(UInt(PhyRegIdxWidth.W))
158      for(k <- 0 until srcCnt+1){
159        val rportIdx = i * (srcCnt+1) + k
160        if(k != srcCnt){
161          rat.readPorts(rportIdx).addr := lsrcList(k)
162          psrcVec(k) := rat.readPorts(rportIdx).rdata
163        } else {
164          rat.readPorts(rportIdx).addr := ldest
165          old_pdest := rat.readPorts(rportIdx).rdata
166        }
167      }
168      (psrcVec, old_pdest)
169    }
170    val lsrcList = List(uops(i).ctrl.lsrc1, uops(i).ctrl.lsrc2, uops(i).ctrl.lsrc3)
171    val ldest = uops(i).ctrl.ldest
172    val (intPhySrcVec, intOldPdest) = readRat(lsrcList.take(2), ldest, fp = false)
173    val (fpPhySrcVec, fpOldPdest) = readRat(lsrcList, ldest, fp = true)
174    uops(i).psrc1 := Mux(uops(i).ctrl.src1Type === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0))
175    uops(i).psrc2 := Mux(uops(i).ctrl.src2Type === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1))
176    uops(i).psrc3 := fpPhySrcVec(2)
177    uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest)
178  }
179
180  // We don't bypass the old_pdest from valid instructions with the same ldest currently in rename stage.
181  // Instead, we determine whether there're some dependences between the valid instructions.
182  for (i <- 1 until RenameWidth) {
183    io.renameBypass.lsrc1_bypass(i-1) := Cat((0 until i).map(j => {
184      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.src1Type === SrcType.fp
185      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src1Type === SrcType.reg
186      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc1
187    }).reverse)
188    io.renameBypass.lsrc2_bypass(i-1) := Cat((0 until i).map(j => {
189      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.src2Type === SrcType.fp
190      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src2Type === SrcType.reg
191      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc2
192    }).reverse)
193    io.renameBypass.lsrc3_bypass(i-1) := Cat((0 until i).map(j => {
194      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.src3Type === SrcType.fp
195      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src3Type === SrcType.reg
196      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc3
197    }).reverse)
198    io.renameBypass.ldest_bypass(i-1) := Cat((0 until i).map(j => {
199      val fpMatch  = needFpDest(j) && needFpDest(i)
200      val intMatch = needIntDest(j) && needIntDest(i)
201      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.ldest
202    }).reverse)
203  }
204
205  /**
206    * Instructions commit: update freelist and rename table
207    */
208  for (i <- 0 until CommitWidth) {
209    if (i >= RenameWidth) {
210      allPhyResource.map{ case (rat, _, _) =>
211        rat.specWritePorts(i).wen   := false.B
212        rat.specWritePorts(i).addr  := DontCare
213        rat.specWritePorts(i).wdata := DontCare
214      }
215    }
216
217    allPhyResource.map{ case (rat, freelist, fp) =>
218      // walk back write
219      val commitDestValid = io.roqCommits.valid(i) && needDestRegCommit(fp, io.roqCommits.info(i))
220
221      when (commitDestValid && io.roqCommits.isWalk) {
222        rat.specWritePorts(i).wen := true.B
223        rat.specWritePorts(i).addr := io.roqCommits.info(i).ldest
224        rat.specWritePorts(i).wdata := io.roqCommits.info(i).old_pdest
225        XSInfo({if(fp) p"fp" else p"int "} + p"walk: " +
226          p" ldest:${rat.specWritePorts(i).addr} old_pdest:${rat.specWritePorts(i).wdata}\n")
227      }
228
229      rat.archWritePorts(i).wen := commitDestValid && !io.roqCommits.isWalk
230      rat.archWritePorts(i).addr := io.roqCommits.info(i).ldest
231      rat.archWritePorts(i).wdata := io.roqCommits.info(i).pdest
232
233      XSInfo(rat.archWritePorts(i).wen,
234        {if(fp) p"fp" else p"int "} + p" rat arch: ldest:${rat.archWritePorts(i).addr}" +
235          p" pdest:${rat.archWritePorts(i).wdata}\n"
236      )
237
238      freelist.deallocReqs(i) := rat.archWritePorts(i).wen
239      freelist.deallocPregs(i) := io.roqCommits.info(i).old_pdest
240    }
241  }
242}
243