xref: /XiangShan/src/main/scala/xiangshan/backend/rename/Rename.scala (revision 2ea5e87c80e2a8e109391c0e5b25cdc5749c89de)
1package xiangshan.backend.rename
2
3import chisel3._
4import chisel3.util._
5import xiangshan._
6import utils._
7
8class RenameBypassInfo extends XSBundle {
9  val lsrc1_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
10  val lsrc2_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
11  val lsrc3_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
12  val ldest_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
13}
14
15class Rename extends XSModule {
16  val io = IO(new Bundle() {
17    val redirect = Flipped(ValidIO(new Redirect))
18    val roqCommits = Vec(CommitWidth, Flipped(ValidIO(new RoqCommit)))
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
26  def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = {
27    XSInfo(
28      in.valid && in.ready,
29      p"pc:${Hexadecimal(in.bits.cf.pc)} in v:${in.valid} in rdy:${in.ready} " +
30        p"lsrc1:${in.bits.ctrl.lsrc1} -> psrc1:${out.bits.psrc1} " +
31        p"lsrc2:${in.bits.ctrl.lsrc2} -> psrc2:${out.bits.psrc2} " +
32        p"lsrc3:${in.bits.ctrl.lsrc3} -> psrc3:${out.bits.psrc3} " +
33        p"ldest:${in.bits.ctrl.ldest} -> pdest:${out.bits.pdest} " +
34        p"old_pdest:${out.bits.old_pdest} " +
35        p"out v:${out.valid} r:${out.ready}\n"
36    )
37  }
38
39  for((x,y) <- io.in.zip(io.out)){
40    printRenameInfo(x, y)
41  }
42
43  val fpFreeList, intFreeList = Module(new FreeList).io
44  val fpRat = Module(new RenameTable(float = true)).io
45  val intRat = Module(new RenameTable(float = false)).io
46
47  fpFreeList.redirect := io.redirect
48  intFreeList.redirect := io.redirect
49
50  val flush = io.redirect.valid && (io.redirect.bits.isException || io.redirect.bits.isFlushPipe) // TODO: need check by JiaWei
51  fpRat.flush := flush
52  intRat.flush := flush
53
54  def needDestReg[T <: CfCtrl](fp: Boolean, x: T): Bool = {
55    {if(fp) x.ctrl.fpWen else x.ctrl.rfWen && (x.ctrl.ldest =/= 0.U)}
56  }
57  val walkValid = Cat(io.roqCommits.map(_.valid)).orR && io.roqCommits(0).bits.isWalk
58  fpFreeList.walk.valid := walkValid
59  intFreeList.walk.valid := walkValid
60  fpFreeList.walk.bits := PopCount(io.roqCommits.map(c => c.valid && needDestReg(true, c.bits.uop)))
61  intFreeList.walk.bits := PopCount(io.roqCommits.map(c => c.valid && needDestReg(false, c.bits.uop)))
62  fpFreeList.req.doAlloc := intFreeList.req.canAlloc && io.out(0).ready
63  intFreeList.req.doAlloc := fpFreeList.req.canAlloc && io.out(0).ready
64
65  val uops = Wire(Vec(RenameWidth, new MicroOp))
66
67  uops.foreach( uop => {
68//    uop.brMask := DontCare
69//    uop.brTag := DontCare
70    uop.src1State := DontCare
71    uop.src2State := DontCare
72    uop.src3State := DontCare
73    uop.roqIdx := DontCare
74    uop.diffTestDebugLrScValid := DontCare
75    uop.debugInfo := DontCare
76    uop.lqIdx := DontCare
77    uop.sqIdx := DontCare
78  })
79
80  val needFpDest = Wire(Vec(RenameWidth, Bool()))
81  val needIntDest = Wire(Vec(RenameWidth, Bool()))
82  for(i <- 0 until RenameWidth) {
83    uops(i).cf := io.in(i).bits.cf
84    uops(i).ctrl := io.in(i).bits.ctrl
85    uops(i).brTag := io.in(i).bits.brTag
86
87    val inValid = io.in(i).valid
88
89    // alloc a new phy reg
90    needFpDest(i) := inValid && needDestReg(fp = true, io.in(i).bits)
91    needIntDest(i) := inValid && needDestReg(fp = false, io.in(i).bits)
92    fpFreeList.req.allocReqs(i) := needFpDest(i)
93    intFreeList.req.allocReqs(i) := needIntDest(i)
94
95    io.in(i).ready := io.out(i).ready && fpFreeList.req.canAlloc && intFreeList.req.canAlloc
96
97    // do checkpoints when a branch inst come
98    // for(fl <- Seq(fpFreeList, intFreeList)){
99    //   fl.cpReqs(i).valid := inValid
100    //   fl.cpReqs(i).bits := io.in(i).bits.brTag
101    // }
102
103    uops(i).pdest := Mux(needIntDest(i),
104      intFreeList.req.pdests(i),
105      Mux(
106        uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen,
107        0.U, fpFreeList.req.pdests(i)
108      )
109    )
110
111    io.out(i).valid := io.in(i).valid && intFreeList.req.canAlloc && fpFreeList.req.canAlloc
112    io.out(i).bits := uops(i)
113
114    // write rename table
115    def writeRat(fp: Boolean) = {
116      val rat = if(fp) fpRat else intRat
117      val freeList = if(fp) fpFreeList else intFreeList
118      // speculative inst write
119      val specWen = freeList.req.allocReqs(i) && freeList.req.canAlloc && freeList.req.doAlloc
120      // walk back write
121      val commitDestValid = io.roqCommits(i).valid && needDestReg(fp, io.roqCommits(i).bits.uop)
122      val walkWen = commitDestValid && io.roqCommits(i).bits.isWalk
123
124      rat.specWritePorts(i).wen := specWen || walkWen
125      rat.specWritePorts(i).addr := Mux(specWen, uops(i).ctrl.ldest, io.roqCommits(i).bits.uop.ctrl.ldest)
126      rat.specWritePorts(i).wdata := Mux(specWen, freeList.req.pdests(i), io.roqCommits(i).bits.uop.old_pdest)
127
128      XSInfo(walkWen,
129        {if(fp) p"fp" else p"int "} + p"walk: pc:${Hexadecimal(io.roqCommits(i).bits.uop.cf.pc)}" +
130          p" ldest:${rat.specWritePorts(i).addr} old_pdest:${rat.specWritePorts(i).wdata}\n"
131      )
132
133      rat.archWritePorts(i).wen := commitDestValid && !io.roqCommits(i).bits.isWalk
134      rat.archWritePorts(i).addr := io.roqCommits(i).bits.uop.ctrl.ldest
135      rat.archWritePorts(i).wdata := io.roqCommits(i).bits.uop.pdest
136
137      XSInfo(rat.archWritePorts(i).wen,
138        {if(fp) p"fp" else p"int "} + p" rat arch: ldest:${rat.archWritePorts(i).addr}" +
139          p" pdest:${rat.archWritePorts(i).wdata}\n"
140      )
141
142      freeList.deallocReqs(i) := rat.archWritePorts(i).wen
143      freeList.deallocPregs(i) := io.roqCommits(i).bits.uop.old_pdest
144
145    }
146
147    writeRat(fp = false)
148    writeRat(fp = true)
149
150    // read rename table
151    def readRat(lsrcList: List[UInt], ldest: UInt, fp: Boolean) = {
152      val rat = if(fp) fpRat else intRat
153      val srcCnt = lsrcList.size
154      val psrcVec = Wire(Vec(srcCnt, UInt(PhyRegIdxWidth.W)))
155      val old_pdest = Wire(UInt(PhyRegIdxWidth.W))
156      for(k <- 0 until srcCnt+1){
157        val rportIdx = i * (srcCnt+1) + k
158        if(k != srcCnt){
159          rat.readPorts(rportIdx).addr := lsrcList(k)
160          psrcVec(k) := rat.readPorts(rportIdx).rdata
161        } else {
162          rat.readPorts(rportIdx).addr := ldest
163          old_pdest := rat.readPorts(rportIdx).rdata
164        }
165      }
166      (psrcVec, old_pdest)
167    }
168    val lsrcList = List(uops(i).ctrl.lsrc1, uops(i).ctrl.lsrc2, uops(i).ctrl.lsrc3)
169    val ldest = uops(i).ctrl.ldest
170    val (intPhySrcVec, intOldPdest) = readRat(lsrcList.take(2), ldest, fp = false)
171    val (fpPhySrcVec, fpOldPdest) = readRat(lsrcList, ldest, fp = true)
172    uops(i).psrc1 := Mux(uops(i).ctrl.src1Type === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0))
173    uops(i).psrc2 := Mux(uops(i).ctrl.src2Type === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1))
174    uops(i).psrc3 := fpPhySrcVec(2)
175    uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest)
176  }
177
178  // We don't bypass the old_pdest from valid instructions with the same ldest currently in rename stage.
179  // Instead, we determine whether there're some dependences between the valid instructions.
180  for (i <- 1 until RenameWidth) {
181    io.renameBypass.lsrc1_bypass(i-1) := Cat((0 until i).map(j => {
182      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.src1Type === SrcType.fp
183      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src1Type === SrcType.reg
184      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc1
185    }).reverse)
186    io.renameBypass.lsrc2_bypass(i-1) := Cat((0 until i).map(j => {
187      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.src2Type === SrcType.fp
188      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src2Type === SrcType.reg
189      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc2
190    }).reverse)
191    io.renameBypass.lsrc3_bypass(i-1) := Cat((0 until i).map(j => {
192      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.src3Type === SrcType.fp
193      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.src3Type === SrcType.reg
194      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc3
195    }).reverse)
196    io.renameBypass.ldest_bypass(i-1) := Cat((0 until i).map(j => {
197      val fpMatch  = needFpDest(j) && needFpDest(i)
198      val intMatch = needIntDest(j) && needIntDest(i)
199      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.ldest
200    }).reverse)
201  }
202}
203