xref: /XiangShan/src/main/scala/xiangshan/backend/rename/RenameTable.scala (revision c4b56310b9f6edacd8ee65bfdd7dd13b260a316c)
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 utils.XSError
25import xiangshan._
26
27abstract class RegType
28case object Reg_I extends RegType
29case object Reg_F extends RegType
30case object Reg_V extends RegType
31
32class RatReadPort(implicit p: Parameters) extends XSBundle {
33  val hold = Input(Bool())
34  val addr = Input(UInt(6.W))
35  val data = Output(UInt(PhyRegIdxWidth.W))
36}
37
38class RatWritePort(implicit p: Parameters) extends XSBundle {
39  val wen = Bool()
40  val addr = UInt(6.W)
41  val data = UInt(PhyRegIdxWidth.W)
42}
43
44class RenameTable(reg_t: RegType)(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper {
45
46  // params alias
47  private val numVecRegSrc = backendParams.numVecRegSrc
48  private val numVecRatPorts = numVecRegSrc + 1 // +1 dst
49
50  val readPortsNum = reg_t match {
51    case Reg_I => 3
52    case Reg_F => 4
53    case Reg_V => numVecRatPorts // +1 ldest
54  }
55  val io = IO(new Bundle {
56    val redirect = Input(Bool())
57    val readPorts = Vec(readPortsNum * RenameWidth, new RatReadPort)
58    val specWritePorts = Vec(CommitWidth, Input(new RatWritePort))
59    val archWritePorts = Vec(CommitWidth, Input(new RatWritePort))
60    val old_pdest = Vec(CommitWidth, Output(UInt(PhyRegIdxWidth.W)))
61    val need_free = Vec(CommitWidth, Output(Bool()))
62    val snpt = Input(new SnapshotPort)
63    val diffWritePorts = Vec(CommitWidth * MaxUopSize, Input(new RatWritePort))
64    val debug_rdata = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
65    val debug_vconfig = if (backendParams.debugEn) reg_t match { // vconfig is implemented as int reg[32]
66      case Reg_V => Some(Output(UInt(PhyRegIdxWidth.W)))
67      case _ => None
68    } else None
69    val diff_rdata = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
70    val diff_vconfig = if (backendParams.debugEn) reg_t match {
71      case Reg_V => Some(Output(UInt(PhyRegIdxWidth.W)))
72      case _ => None
73    } else None
74  })
75
76  // speculative rename table
77  // fp and vec share the same free list, so the first init value of vecRAT is 32
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)(x => (x + FpLogicRegs).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(CommitWidth)(0.U(PhyRegIdxWidth.W)))
90  val need_free = RegInit(VecInit.fill(CommitWidth)(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 = RegNext(io.redirect, false.B)
98  val t1_rdata = io.readPorts.map(p => RegNext(Mux(p.hold, p.data, spec_table_next(p.addr))))
99  val t1_raddr = io.readPorts.map(p => RegEnable(p.addr, !p.hold))
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    // We use two comparisons here because r.hold has bad timing but addrs have better timing.
123    val t0_bypass = io.specWritePorts.map(w => w.wen && Mux(r.hold, w.addr === t1_raddr(i), w.addr === r.addr))
124    val t1_bypass = RegNext(Mux(io.redirect, 0.U.asTypeOf(VecInit(t0_bypass)), VecInit(t0_bypass)))
125    val bypass_data = ParallelPriorityMux(t1_bypass.reverse, t1_wSpec.map(_.data).reverse)
126    r.data := Mux(t1_bypass.asUInt.orR, bypass_data, t1_rdata(i))
127  }
128
129  for ((w, i) <- io.archWritePorts.zipWithIndex) {
130    when (w.wen) {
131      arch_table_next(w.addr) := w.data
132    }
133    val arch_mask = VecInit.fill(PhyRegIdxWidth)(w.wen).asUInt
134    old_pdest(i) :=
135      MuxCase(arch_table(w.addr) & arch_mask,
136              io.archWritePorts.take(i).reverse.map(x => (x.wen && x.addr === w.addr, x.data & arch_mask)))
137  }
138  arch_table := arch_table_next
139
140  for (((old, free), i) <- (old_pdest zip need_free).zipWithIndex) {
141    val hasDuplicate = old_pdest.take(i).map(_ === old)
142    val blockedByDup = if (i == 0) false.B else VecInit(hasDuplicate).asUInt.orR
143    free := VecInit(arch_table.map(_ =/= old)).asUInt.andR && !blockedByDup
144  }
145
146  io.old_pdest := old_pdest
147  io.need_free := need_free
148  io.debug_rdata.foreach(_ := arch_table.take(32))
149  io.debug_vconfig match {
150    case None =>
151    case x => x.get := arch_table.last
152  }
153  if (env.EnableDifftest || env.AlwaysBasicDiff) {
154    val difftest_table = RegInit(rename_table_init)
155    val difftest_table_next = WireDefault(difftest_table)
156
157    for (w <- io.diffWritePorts) {
158      when(w.wen) {
159        difftest_table_next(w.addr) := w.data
160      }
161    }
162    difftest_table := difftest_table_next
163
164    io.diff_rdata.foreach(_ := difftest_table.take(32))
165    io.diff_vconfig match {
166      case None =>
167      case x => x.get := difftest_table(VCONFIG_IDX)
168    }
169  }
170  else {
171    io.diff_rdata.foreach(_ := 0.U.asTypeOf(io.debug_rdata.get))
172    io.diff_vconfig match {
173      case None =>
174      case x => x.get := 0.U
175    }
176  }
177}
178
179class RenameTableWrapper(implicit p: Parameters) extends XSModule {
180
181  // params alias
182  private val numVecRegSrc = backendParams.numVecRegSrc
183  private val numVecRatPorts = numVecRegSrc + 1 // +1 dst
184
185  val io = IO(new Bundle() {
186    val redirect = Input(Bool())
187    val robCommits = Input(new RobCommitIO)
188    val diffCommits = Input(new DiffCommitIO)
189    val intReadPorts = Vec(RenameWidth, Vec(3, new RatReadPort))
190    val intRenamePorts = Vec(RenameWidth, Input(new RatWritePort))
191    val fpReadPorts = Vec(RenameWidth, Vec(4, new RatReadPort))
192    val fpRenamePorts = Vec(RenameWidth, Input(new RatWritePort))
193    val vecReadPorts = Vec(RenameWidth, Vec(numVecRatPorts, new RatReadPort))
194    val vecRenamePorts = Vec(RenameWidth, Input(new RatWritePort))
195
196    val int_old_pdest = Vec(CommitWidth, Output(UInt(PhyRegIdxWidth.W)))
197    val fp_old_pdest = Vec(CommitWidth, Output(UInt(PhyRegIdxWidth.W)))
198    val vec_old_pdest = Vec(CommitWidth, Output(UInt(PhyRegIdxWidth.W)))
199    val int_need_free = Vec(CommitWidth, Output(Bool()))
200    val snpt = Input(new SnapshotPort)
201
202    // for debug printing
203    val debug_int_rat     = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
204    val debug_fp_rat      = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
205    val debug_vec_rat     = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
206    val debug_vconfig_rat = if (backendParams.debugEn) Some(Output(UInt(PhyRegIdxWidth.W))) else None
207
208    val diff_int_rat     = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
209    val diff_fp_rat      = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
210    val diff_vec_rat     = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
211    val diff_vconfig_rat = if (backendParams.debugEn) Some(Output(UInt(PhyRegIdxWidth.W))) else None
212  })
213
214  val intRat = Module(new RenameTable(Reg_I))
215  val fpRat  = Module(new RenameTable(Reg_F))
216  val vecRat = Module(new RenameTable(Reg_V))
217
218  io.debug_int_rat .foreach(_ := intRat.io.debug_rdata.get)
219  io.diff_int_rat  .foreach(_ := intRat.io.diff_rdata.get)
220  intRat.io.readPorts <> io.intReadPorts.flatten
221  intRat.io.redirect := io.redirect
222  intRat.io.snpt := io.snpt
223  io.int_old_pdest := intRat.io.old_pdest
224  io.int_need_free := intRat.io.need_free
225  val intDestValid = io.robCommits.info.map(_.rfWen)
226  for ((arch, i) <- intRat.io.archWritePorts.zipWithIndex) {
227    arch.wen  := io.robCommits.isCommit && io.robCommits.commitValid(i) && intDestValid(i)
228    arch.addr := io.robCommits.info(i).ldest
229    arch.data := io.robCommits.info(i).pdest
230    XSError(arch.wen && arch.addr === 0.U && arch.data =/= 0.U, "pdest for $0 should be 0\n")
231  }
232  for ((spec, i) <- intRat.io.specWritePorts.zipWithIndex) {
233    spec.wen  := io.robCommits.isWalk && io.robCommits.walkValid(i) && intDestValid(i)
234    spec.addr := io.robCommits.info(i).ldest
235    spec.data := io.robCommits.info(i).pdest
236    XSError(spec.wen && spec.addr === 0.U && spec.data =/= 0.U, "pdest for $0 should be 0\n")
237  }
238  for ((spec, rename) <- intRat.io.specWritePorts.zip(io.intRenamePorts)) {
239    when (rename.wen) {
240      spec.wen  := true.B
241      spec.addr := rename.addr
242      spec.data := rename.data
243    }
244  }
245  for ((diff, i) <- intRat.io.diffWritePorts.zipWithIndex) {
246    diff.wen := io.diffCommits.isCommit && io.diffCommits.commitValid(i) && io.diffCommits.info(i).rfWen
247    diff.addr := io.diffCommits.info(i).ldest
248    diff.data := io.diffCommits.info(i).pdest
249  }
250
251  // debug read ports for difftest
252  io.debug_fp_rat.foreach(_ := fpRat.io.debug_rdata.get)
253  io.diff_fp_rat .foreach(_ := fpRat.io.diff_rdata.get)
254  fpRat.io.readPorts <> io.fpReadPorts.flatten
255  fpRat.io.redirect := io.redirect
256  fpRat.io.snpt := io.snpt
257  io.fp_old_pdest := fpRat.io.old_pdest
258
259  for ((arch, i) <- fpRat.io.archWritePorts.zipWithIndex) {
260    arch.wen  := io.robCommits.isCommit && io.robCommits.commitValid(i) && io.robCommits.info(i).fpWen
261    arch.addr := io.robCommits.info(i).ldest
262    arch.data := io.robCommits.info(i).pdest
263  }
264  for ((spec, i) <- fpRat.io.specWritePorts.zipWithIndex) {
265    spec.wen  := io.robCommits.isWalk && io.robCommits.walkValid(i) && io.robCommits.info(i).fpWen
266    spec.addr := io.robCommits.info(i).ldest
267    spec.data := io.robCommits.info(i).pdest
268  }
269  for ((spec, rename) <- fpRat.io.specWritePorts.zip(io.fpRenamePorts)) {
270    when (rename.wen) {
271      spec.wen  := true.B
272      spec.addr := rename.addr
273      spec.data := rename.data
274    }
275  }
276  for ((diff, i) <- fpRat.io.diffWritePorts.zipWithIndex) {
277    diff.wen := io.diffCommits.isCommit && io.diffCommits.commitValid(i) && io.diffCommits.info(i).fpWen
278    diff.addr := io.diffCommits.info(i).ldest
279    diff.data := io.diffCommits.info(i).pdest
280  }
281
282  // debug read ports for difftest
283  io.debug_vec_rat    .foreach(_ := vecRat.io.debug_rdata.get)
284  io.debug_vconfig_rat.foreach(_ := vecRat.io.debug_vconfig.get)
285  io.diff_vec_rat     .foreach(_ := vecRat.io.diff_rdata.get)
286  io.diff_vconfig_rat .foreach(_ := vecRat.io.diff_vconfig.get)
287  vecRat.io.readPorts <> io.vecReadPorts.flatten
288  vecRat.io.redirect := io.redirect
289  vecRat.io.snpt := io.snpt
290  io.vec_old_pdest := vecRat.io.old_pdest
291
292  //TODO: RM the donTouch
293  dontTouch(vecRat.io)
294  for ((arch, i) <- vecRat.io.archWritePorts.zipWithIndex) {
295    arch.wen  := io.robCommits.isCommit && io.robCommits.commitValid(i) && io.robCommits.info(i).vecWen
296    arch.addr := io.robCommits.info(i).ldest
297    arch.data := io.robCommits.info(i).pdest
298  }
299  for ((spec, i) <- vecRat.io.specWritePorts.zipWithIndex) {
300    spec.wen  := io.robCommits.isWalk && io.robCommits.walkValid(i) && io.robCommits.info(i).vecWen
301    spec.addr := io.robCommits.info(i).ldest
302    spec.data := io.robCommits.info(i).pdest
303  }
304  for ((spec, rename) <- vecRat.io.specWritePorts.zip(io.vecRenamePorts)) {
305    when (rename.wen) {
306      spec.wen  := true.B
307      spec.addr := rename.addr
308      spec.data := rename.data
309    }
310  }
311  for ((diff, i) <- vecRat.io.diffWritePorts.zipWithIndex) {
312    diff.wen := io.diffCommits.isCommit && io.diffCommits.commitValid(i) && io.diffCommits.info(i).vecWen
313    diff.addr := io.diffCommits.info(i).ldest
314    diff.data := io.diffCommits.info(i).pdest
315  }
316
317}
318