xref: /XiangShan/src/main/scala/xiangshan/backend/rename/RenameTable.scala (revision 4daa5bf3c3f27e7fd090866d52405b21e107eb8d)
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 org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import utility.HasCircularQueuePtrHelper
23import utility.ParallelPriorityMux
24import utility.GatedValidRegNext
25import utils.XSError
26import xiangshan._
27
28abstract class RegType
29case object Reg_I extends RegType
30case object Reg_F extends RegType
31case object Reg_V extends RegType
32
33class RatReadPort(implicit p: Parameters) extends XSBundle {
34  val hold = Input(Bool())
35  val addr = Input(UInt(6.W))
36  val data = Output(UInt(PhyRegIdxWidth.W))
37}
38
39class RatWritePort(implicit p: Parameters) extends XSBundle {
40  val wen = Bool()
41  val addr = UInt(6.W)
42  val data = UInt(PhyRegIdxWidth.W)
43}
44
45class RenameTable(reg_t: RegType)(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper {
46
47  // params alias
48  private val numVecRegSrc = backendParams.numVecRegSrc
49  private val numVecRatPorts = numVecRegSrc
50
51  val readPortsNum = reg_t match {
52    case Reg_I => 2
53    case Reg_F => 3
54    case Reg_V => numVecRatPorts // +1 ldest
55  }
56  val io = IO(new Bundle {
57    val redirect = Input(Bool())
58    val readPorts = Vec(readPortsNum * RenameWidth, new RatReadPort)
59    val specWritePorts = Vec(RabCommitWidth, Input(new RatWritePort))
60    val archWritePorts = Vec(RabCommitWidth, Input(new RatWritePort))
61    val old_pdest = Vec(RabCommitWidth, Output(UInt(PhyRegIdxWidth.W)))
62    val need_free = Vec(RabCommitWidth, Output(Bool()))
63    val snpt = Input(new SnapshotPort)
64    val diffWritePorts = if (backendParams.debugEn) Some(Vec(RabCommitWidth * MaxUopSize, Input(new RatWritePort))) else None
65    val debug_rdata = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
66    val debug_vconfig = if (backendParams.debugEn) reg_t match { // vconfig is implemented as int reg[32]
67      case Reg_V => Some(Output(UInt(PhyRegIdxWidth.W)))
68      case _ => None
69    } else None
70    val diff_rdata = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
71    val diff_vconfig = if (backendParams.debugEn) reg_t match {
72      case Reg_V => Some(Output(UInt(PhyRegIdxWidth.W)))
73      case _ => None
74    } else None
75  })
76
77  // speculative rename table
78  val rename_table_init = reg_t match {
79    case Reg_I => VecInit.fill    (IntLogicRegs)(0.U(PhyRegIdxWidth.W))
80    case Reg_F => VecInit.tabulate(FpLogicRegs)(_.U(PhyRegIdxWidth.W))
81    case Reg_V => VecInit.tabulate(VecLogicRegs)(_.U(PhyRegIdxWidth.W))
82  }
83  val spec_table = RegInit(rename_table_init)
84  val spec_table_next = WireInit(spec_table)
85  // arch state rename table
86  val arch_table = RegInit(rename_table_init)
87  val arch_table_next = WireDefault(arch_table)
88  // old_pdest
89  val old_pdest = RegInit(VecInit.fill(RabCommitWidth)(0.U(PhyRegIdxWidth.W)))
90  val need_free = RegInit(VecInit.fill(RabCommitWidth)(false.B))
91
92  // For better timing, we optimize reading and writing to RenameTable as follows:
93  // (1) Writing at T0 will be actually processed at T1.
94  // (2) Reading is synchronous now.
95  // (3) RAddr at T0 will be used to access the table and get data at T0.
96  // (4) WData at T0 is bypassed to RData at T1.
97  val t1_redirect = GatedValidRegNext(io.redirect, false.B)
98  val t1_raddr = io.readPorts.map(p => RegEnable(p.addr, !p.hold))
99  val t1_rdata_use_t1_raddr = VecInit(t1_raddr.map(spec_table(_)))
100  val t1_wSpec = RegNext(Mux(io.redirect, 0.U.asTypeOf(io.specWritePorts), io.specWritePorts))
101
102  val t1_snpt = RegNext(io.snpt, 0.U.asTypeOf(io.snpt))
103
104  val snapshots = SnapshotGenerator(spec_table, t1_snpt.snptEnq, t1_snpt.snptDeq, t1_redirect, t1_snpt.flushVec)
105
106  // WRITE: when instruction commits or walking
107  val t1_wSpec_addr = t1_wSpec.map(w => Mux(w.wen, UIntToOH(w.addr), 0.U))
108  for ((next, i) <- spec_table_next.zipWithIndex) {
109    val matchVec = t1_wSpec_addr.map(w => w(i))
110    val wMatch = ParallelPriorityMux(matchVec.reverse, t1_wSpec.map(_.data).reverse)
111    // When there's a flush, we use arch_table to update spec_table.
112    next := Mux(
113      t1_redirect,
114      Mux(t1_snpt.useSnpt, snapshots(t1_snpt.snptSelect)(i), arch_table(i)),
115      Mux(VecInit(matchVec).asUInt.orR, wMatch, spec_table(i))
116    )
117  }
118  spec_table := spec_table_next
119
120  // READ: decode-rename stage
121  for ((r, i) <- io.readPorts.zipWithIndex) {
122    val t0_bypass = io.specWritePorts.map(w => w.wen && Mux(r.hold, w.addr === t1_raddr(i), w.addr === r.addr))
123    val t1_bypass = RegNext(Mux(io.redirect, 0.U.asTypeOf(VecInit(t0_bypass)), VecInit(t0_bypass)))
124    val bypass_data = ParallelPriorityMux(t1_bypass.reverse, t1_wSpec.map(_.data).reverse)
125    r.data := Mux(t1_bypass.asUInt.orR, bypass_data, t1_rdata_use_t1_raddr(i))
126  }
127
128  for ((w, i) <- io.archWritePorts.zipWithIndex) {
129    when (w.wen) {
130      arch_table_next(w.addr) := w.data
131    }
132    val arch_mask = VecInit.fill(PhyRegIdxWidth)(w.wen).asUInt
133    old_pdest(i) :=
134      MuxCase(arch_table(w.addr) & arch_mask,
135              io.archWritePorts.take(i).reverse.map(x => (x.wen && x.addr === w.addr, x.data & arch_mask)))
136  }
137  arch_table := arch_table_next
138
139  for (((old, free), i) <- (old_pdest zip need_free).zipWithIndex) {
140    val hasDuplicate = old_pdest.take(i).map(_ === old)
141    val blockedByDup = if (i == 0) false.B else VecInit(hasDuplicate).asUInt.orR
142    free := VecInit(arch_table.map(_ =/= old)).asUInt.andR && !blockedByDup
143  }
144
145  io.old_pdest := old_pdest
146  io.need_free := need_free
147  io.debug_rdata.foreach(_ := arch_table.take(32))
148  io.debug_vconfig match {
149    case None =>
150    case x => x.get := arch_table.last
151  }
152  if (env.EnableDifftest || env.AlwaysBasicDiff) {
153    val difftest_table = RegInit(rename_table_init)
154    val difftest_table_next = WireDefault(difftest_table)
155
156    for (w <- io.diffWritePorts.get) {
157      when(w.wen) {
158        difftest_table_next(w.addr) := w.data
159      }
160    }
161    difftest_table := difftest_table_next
162
163    io.diff_rdata.foreach(_ := difftest_table.take(32))
164    io.diff_vconfig match {
165      case None =>
166      case x => x.get := difftest_table(VCONFIG_IDX)
167    }
168  }
169  else {
170    io.diff_rdata.foreach(_ := 0.U.asTypeOf(io.debug_rdata.get))
171    io.diff_vconfig match {
172      case None =>
173      case x => x.get := 0.U
174    }
175  }
176}
177
178class RenameTableWrapper(implicit p: Parameters) extends XSModule {
179
180  // params alias
181  private val numVecRegSrc = backendParams.numVecRegSrc
182  private val numVecRatPorts = numVecRegSrc
183
184  val io = IO(new Bundle() {
185    val redirect = Input(Bool())
186    val rabCommits = Input(new RabCommitIO)
187    val diffCommits = if (backendParams.debugEn) Some(Input(new DiffCommitIO)) else None
188    val intReadPorts = Vec(RenameWidth, Vec(2, new RatReadPort))
189    val intRenamePorts = Vec(RenameWidth, Input(new RatWritePort))
190    val fpReadPorts = Vec(RenameWidth, Vec(3, new RatReadPort))
191    val fpRenamePorts = Vec(RenameWidth, Input(new RatWritePort))
192    val vecReadPorts = Vec(RenameWidth, Vec(numVecRatPorts, new RatReadPort))
193    val vecRenamePorts = Vec(RenameWidth, Input(new RatWritePort))
194
195    val int_old_pdest = Vec(RabCommitWidth, Output(UInt(PhyRegIdxWidth.W)))
196    val fp_old_pdest = Vec(RabCommitWidth, Output(UInt(PhyRegIdxWidth.W)))
197    val vec_old_pdest = Vec(RabCommitWidth, Output(UInt(PhyRegIdxWidth.W)))
198    val int_need_free = Vec(RabCommitWidth, Output(Bool()))
199    val snpt = Input(new SnapshotPort)
200
201    // for debug printing
202    val debug_int_rat     = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
203    val debug_fp_rat      = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
204    val debug_vec_rat     = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
205    val debug_vconfig_rat = if (backendParams.debugEn) Some(Output(UInt(PhyRegIdxWidth.W))) else None
206
207    val diff_int_rat     = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
208    val diff_fp_rat      = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
209    val diff_vec_rat     = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
210    val diff_vconfig_rat = if (backendParams.debugEn) Some(Output(UInt(PhyRegIdxWidth.W))) else None
211  })
212
213  val intRat = Module(new RenameTable(Reg_I))
214  val fpRat  = Module(new RenameTable(Reg_F))
215  val vecRat = Module(new RenameTable(Reg_V))
216
217  io.debug_int_rat .foreach(_ := intRat.io.debug_rdata.get)
218  io.diff_int_rat  .foreach(_ := intRat.io.diff_rdata.get)
219  intRat.io.readPorts <> io.intReadPorts.flatten
220  intRat.io.redirect := io.redirect
221  intRat.io.snpt := io.snpt
222  io.int_old_pdest := intRat.io.old_pdest
223  io.int_need_free := intRat.io.need_free
224  val intDestValid = io.rabCommits.info.map(_.rfWen)
225  for ((arch, i) <- intRat.io.archWritePorts.zipWithIndex) {
226    arch.wen  := io.rabCommits.isCommit && io.rabCommits.commitValid(i) && intDestValid(i)
227    arch.addr := io.rabCommits.info(i).ldest
228    arch.data := io.rabCommits.info(i).pdest
229    XSError(arch.wen && arch.addr === 0.U && arch.data =/= 0.U, "pdest for $0 should be 0\n")
230  }
231  for ((spec, i) <- intRat.io.specWritePorts.zipWithIndex) {
232    spec.wen  := io.rabCommits.isWalk && io.rabCommits.walkValid(i) && intDestValid(i)
233    spec.addr := io.rabCommits.info(i).ldest
234    spec.data := io.rabCommits.info(i).pdest
235    XSError(spec.wen && spec.addr === 0.U && spec.data =/= 0.U, "pdest for $0 should be 0\n")
236  }
237  for ((spec, rename) <- intRat.io.specWritePorts.zip(io.intRenamePorts)) {
238    when (rename.wen) {
239      spec.wen  := true.B
240      spec.addr := rename.addr
241      spec.data := rename.data
242    }
243  }
244  if (backendParams.debugEn) {
245    for ((diff, i) <- intRat.io.diffWritePorts.get.zipWithIndex) {
246      diff.wen := io.diffCommits.get.isCommit && io.diffCommits.get.commitValid(i) && io.diffCommits.get.info(i).rfWen
247      diff.addr := io.diffCommits.get.info(i).ldest
248      diff.data := io.diffCommits.get.info(i).pdest
249    }
250  }
251
252  // debug read ports for difftest
253  io.debug_fp_rat.foreach(_ := fpRat.io.debug_rdata.get)
254  io.diff_fp_rat .foreach(_ := fpRat.io.diff_rdata.get)
255  fpRat.io.readPorts <> io.fpReadPorts.flatten
256  fpRat.io.redirect := io.redirect
257  fpRat.io.snpt := io.snpt
258  io.fp_old_pdest := fpRat.io.old_pdest
259
260  for ((arch, i) <- fpRat.io.archWritePorts.zipWithIndex) {
261    arch.wen  := io.rabCommits.isCommit && io.rabCommits.commitValid(i) && io.rabCommits.info(i).fpWen
262    arch.addr := io.rabCommits.info(i).ldest
263    arch.data := io.rabCommits.info(i).pdest
264  }
265  for ((spec, i) <- fpRat.io.specWritePorts.zipWithIndex) {
266    spec.wen  := io.rabCommits.isWalk && io.rabCommits.walkValid(i) && io.rabCommits.info(i).fpWen
267    spec.addr := io.rabCommits.info(i).ldest
268    spec.data := io.rabCommits.info(i).pdest
269  }
270  for ((spec, rename) <- fpRat.io.specWritePorts.zip(io.fpRenamePorts)) {
271    when (rename.wen) {
272      spec.wen  := true.B
273      spec.addr := rename.addr
274      spec.data := rename.data
275    }
276  }
277  if (backendParams.debugEn) {
278    for ((diff, i) <- fpRat.io.diffWritePorts.get.zipWithIndex) {
279      diff.wen := io.diffCommits.get.isCommit && io.diffCommits.get.commitValid(i) && io.diffCommits.get.info(i).fpWen
280      diff.addr := io.diffCommits.get.info(i).ldest
281      diff.data := io.diffCommits.get.info(i).pdest
282    }
283  }
284  // debug read ports for difftest
285  io.debug_vec_rat    .foreach(_ := vecRat.io.debug_rdata.get)
286  io.debug_vconfig_rat.foreach(_ := vecRat.io.debug_vconfig.get)
287  io.diff_vec_rat     .foreach(_ := vecRat.io.diff_rdata.get)
288  io.diff_vconfig_rat .foreach(_ := vecRat.io.diff_vconfig.get)
289  vecRat.io.readPorts <> io.vecReadPorts.flatten
290  vecRat.io.redirect := io.redirect
291  vecRat.io.snpt := io.snpt
292  io.vec_old_pdest := vecRat.io.old_pdest
293
294  //TODO: RM the donTouch
295  if(backendParams.debugEn) {
296    dontTouch(vecRat.io)
297  }
298  for ((arch, i) <- vecRat.io.archWritePorts.zipWithIndex) {
299    arch.wen  := io.rabCommits.isCommit && io.rabCommits.commitValid(i) && io.rabCommits.info(i).vecWen
300    arch.addr := io.rabCommits.info(i).ldest
301    arch.data := io.rabCommits.info(i).pdest
302  }
303  for ((spec, i) <- vecRat.io.specWritePorts.zipWithIndex) {
304    spec.wen  := io.rabCommits.isWalk && io.rabCommits.walkValid(i) && io.rabCommits.info(i).vecWen
305    spec.addr := io.rabCommits.info(i).ldest
306    spec.data := io.rabCommits.info(i).pdest
307  }
308  for ((spec, rename) <- vecRat.io.specWritePorts.zip(io.vecRenamePorts)) {
309    when (rename.wen) {
310      spec.wen  := true.B
311      spec.addr := rename.addr
312      spec.data := rename.data
313    }
314  }
315  if (backendParams.debugEn) {
316    for ((diff, i) <- vecRat.io.diffWritePorts.get.zipWithIndex) {
317      diff.wen := io.diffCommits.get.isCommit && io.diffCommits.get.commitValid(i) && io.diffCommits.get.info(i).vecWen
318      diff.addr := io.diffCommits.get.info(i).ldest
319      diff.data := io.diffCommits.get.info(i).pdest
320    }
321  }
322}
323