1package xiangshan.frontend 2 3import chisel3._ 4import chisel3.util._ 5import xiangshan._ 6import utils._ 7import chisel3.experimental.chiselName 8 9import scala.math.min 10 11trait HasSCParameter extends HasTageParameter { 12 val SCHistLens = 0 :: TableInfo.map{ case (_,h,_) => h}.toList 13 val SCNTables = 6 14 val SCCtrBits = 6 15 val SCNRows = 1024 16 val SCTableInfo = Seq.fill(SCNTables)((SCNRows, SCCtrBits)) zip SCHistLens map {case ((n, cb), h) => (n, cb, h)} 17} 18 19class SCReq extends TageReq 20 21abstract class SCBundle extends TageBundle with HasSCParameter {} 22abstract class SCModule extends TageModule with HasSCParameter {} 23 24class SCResp(val ctrBits: Int = 6) extends SCBundle { 25 val ctr = Vec(2, SInt(ctrBits.W)) 26} 27 28class SCUpdate(val ctrBits: Int = 6) extends SCBundle { 29 val pc = UInt(VAddrBits.W) 30 val hist = UInt(HistoryLength.W) 31 val mask = Vec(TageBanks, Bool()) 32 val oldCtrs = Vec(TageBanks, SInt(ctrBits.W)) 33 val tagePreds = Vec(TageBanks, Bool()) 34 val takens = Vec(TageBanks, Bool()) 35} 36 37class SCTableIO extends SCBundle { 38 val req = Input(Valid(new SCReq)) 39 val resp = Output(Vec(TageBanks, new SCResp)) 40 val update = Input(new SCUpdate) 41} 42 43@chiselName 44class SCTable(val nRows: Int, val ctrBits: Int, val histLen: Int) 45 extends SCModule with HasFoldedHistory { 46 val io = IO(new SCTableIO) 47 48 val table = Module(new SRAMTemplate(SInt(ctrBits.W), set=nRows, way=2*TageBanks, shouldReset=true, holdRead=true, singlePort=false)) 49 50 def getIdx(hist: UInt, pc: UInt) = { 51 (compute_folded_hist(hist, log2Ceil(nRows)) ^ (pc >> instOffsetBits.U))(log2Ceil(nRows)-1,0) 52 } 53 54 def ctrUpdate(ctr: SInt, cond: Bool): SInt = signedSatUpdate(ctr, ctrBits, cond) 55 56 val if2_idx = getIdx(io.req.bits.hist, io.req.bits.pc) 57 val if3_idx = RegEnable(if2_idx, enable=io.req.valid) 58 59 val table_r = 60 VecInit((0 until TageBanks).map(b => VecInit((0 to 1).map(i => table.io.r.resp.data(b*2+i))))) 61 62 63 val if2_mask = io.req.bits.mask 64 val if3_mask = RegEnable(if2_mask, enable=io.req.valid) 65 66 val update_idx = getIdx(io.update.hist, io.update.pc) 67 val update_wdatas = 68 VecInit((0 until TageBanks).map(w => 69 ctrUpdate(io.update.oldCtrs(w), io.update.takens(w)))) 70 71 table.io.r.req.valid := io.req.valid 72 table.io.r.req.bits.setIdx := if2_idx 73 74 val updateWayMask = 75 VecInit((0 until TageBanks).map(b => 76 VecInit((0 to 1).map(i => 77 (io.update.mask(b) && i.U === io.update.tagePreds(b).asUInt))))).asUInt 78 79 table.io.w.apply( 80 valid = io.update.mask.asUInt.orR, 81 data = VecInit((0 until TageBanks*2).map(i => update_wdatas(i/2))), 82 setIdx = update_idx, 83 waymask = updateWayMask 84 ) 85 86 (0 until TageBanks).map(b => { 87 io.resp(b).ctr := table_r(b) 88 }) 89 90 val wrBypassEntries = 4 91 92 val wrbypass_idxs = RegInit(0.U.asTypeOf(Vec(wrBypassEntries, UInt(log2Ceil(nRows).W)))) 93 val wrbypass_ctrs = RegInit(0.U.asTypeOf(Vec(wrBypassEntries, Vec(2*TageBanks, SInt(ctrBits.W))))) 94 val wrbypass_ctr_valids = RegInit(0.U.asTypeOf(Vec(wrBypassEntries, Vec(2*TageBanks, Bool())))) 95 val wrbypass_enq_idx = RegInit(0.U(log2Ceil(wrBypassEntries).W)) 96 97 when (reset.asBool) { 98 wrbypass_ctr_valids := 0.U.asTypeOf(Vec(wrBypassEntries, Vec(2*TageBanks, Bool()))) 99 } 100 101 val wrbypass_hits = VecInit((0 until wrBypassEntries) map (i => wrbypass_idxs(i) === update_idx)) 102 val wrbypass_hit = wrbypass_hits.asUInt.orR 103 val wrbypass_hit_idx = ParallelPriorityEncoder(wrbypass_hits) 104 105 for (w <- 0 until TageBanks) { 106 val ctrPos = (w << 1).U | io.update.tagePreds(w).asUInt 107 val altPos = (w << 1).U | ~io.update.tagePreds(w).asUInt 108 val bypass_ctr = wrbypass_ctrs(wrbypass_hit_idx)(ctrPos) 109 val hit_and_valid = wrbypass_hit && wrbypass_ctr_valids(wrbypass_hit_idx)(ctrPos) 110 val oldCtr = Mux(hit_and_valid, wrbypass_ctrs(wrbypass_hit_idx)(ctrPos), io.update.oldCtrs(w)) 111 update_wdatas(w) := ctrUpdate(oldCtr, io.update.takens(w)) 112 113 when (io.update.mask.reduce(_||_)) { 114 when (wrbypass_hit) { 115 when (io.update.mask(w)) { 116 wrbypass_ctrs(wrbypass_hit_idx)(ctrPos) := update_wdatas(w) 117 wrbypass_ctr_valids(wrbypass_hit_idx)(ctrPos) := true.B 118 } 119 }.otherwise { 120 // reset valid bit first 121 wrbypass_ctr_valids(wrbypass_enq_idx)(ctrPos) := false.B 122 wrbypass_ctr_valids(wrbypass_enq_idx)(altPos) := false.B 123 when (io.update.mask(w)) { 124 wrbypass_ctr_valids(wrbypass_enq_idx)(ctrPos) := true.B 125 wrbypass_ctrs(wrbypass_enq_idx)(w) := update_wdatas(w) 126 wrbypass_idxs(wrbypass_enq_idx) := update_idx 127 } 128 } 129 } 130 } 131 132 when (io.update.mask.reduce(_||_) && !wrbypass_hit) { 133 wrbypass_enq_idx := (wrbypass_enq_idx + 1.U)(log2Ceil(wrBypassEntries)-1,0) 134 } 135 136 137 if (BPUDebug && debug) { 138 val u = io.update 139 XSDebug(io.req.valid, 140 p"scTableReq: pc=0x${Hexadecimal(io.req.bits.pc)}, " + 141 p"if2_idx=${if2_idx}, hist=${Hexadecimal(io.req.bits.hist)}, " + 142 p"if2_mask=${Binary(if2_mask)}\n") 143 for (i <- 0 until TageBanks) { 144 XSDebug(RegNext(io.req.valid), 145 p"scTableResp[${i.U}]: if3_idx=${if3_idx}," + 146 p"ctr:${io.resp(i).ctr}, if3_mask=${Binary(if3_mask)}\n") 147 XSDebug(io.update.mask(i), 148 p"update Table: pc:${Hexadecimal(u.pc)}, hist:${Hexadecimal(u.hist)}, " + 149 p"bank:${i}, tageTaken:${u.tagePreds(i)}, taken:${u.takens(i)}, oldCtr:${u.oldCtrs(i)}\n") 150 val ctrPos = (i << 1).U | io.update.tagePreds(i).asUInt 151 val hitCtr = wrbypass_ctrs(wrbypass_hit_idx)(ctrPos) 152 XSDebug(wrbypass_hit && wrbypass_ctr_valids(wrbypass_hit_idx)(ctrPos) && io.update.mask(i), 153 p"bank $i wrbypass hit wridx:$wrbypass_hit_idx, idx:$update_idx, ctr:$hitCtr" + 154 p"taken:${io.update.takens(i)} newCtr:${update_wdatas(i)}\n") 155 } 156 } 157 158} 159 160class SCThreshold(val ctrBits: Int = 5) extends SCBundle { 161 val ctr = UInt(ctrBits.W) 162 def satPos(ctr: UInt = this.ctr) = ctr === ((1.U << ctrBits) - 1.U) 163 def satNeg(ctr: UInt = this.ctr) = ctr === 0.U 164 def neutralVal = (1.U << (ctrBits - 1)) 165 val thres = UInt(5.W) 166 def minThres = 5.U 167 def maxThres = 31.U 168 def update(cause: Bool): SCThreshold = { 169 val res = Wire(new SCThreshold(this.ctrBits)) 170 val newCtr = satUpdate(this.ctr, this.ctrBits, cause) 171 val newThres = Mux(res.satPos(newCtr), this.thres + 1.U, 172 Mux(res.satNeg(newCtr), this.thres - 1.U, 173 this.thres)) 174 res.thres := newThres 175 res.ctr := Mux(res.satPos(newCtr) || res.satNeg(newCtr), res.neutralVal, newCtr) 176 // XSDebug(true.B, p"scThres Update: cause${cause} newCtr ${newCtr} newThres ${newThres}\n") 177 res 178 } 179} 180 181object SCThreshold { 182 def apply(bits: Int) = { 183 val t = Wire(new SCThreshold(ctrBits=bits)) 184 t.ctr := t.neutralVal 185 t.thres := t.minThres 186 t 187 } 188} 189 190 191trait HasSC extends HasSCParameter { this: Tage => 192 val scTables = SCTableInfo.map { 193 case (nRows, ctrBits, histLen) => { 194 val t = Module(new SCTable(nRows/TageBanks, ctrBits, histLen)) 195 val req = t.io.req 196 req.valid := io.pc.valid 197 req.bits.pc := io.pc.bits 198 req.bits.hist := io.hist 199 req.bits.mask := io.inMask 200 if (!EnableSC) {t.io.update := DontCare} 201 t 202 } 203 } 204 205 val scThreshold = RegInit(SCThreshold(5)) 206 val useThreshold = WireInit(scThreshold.thres) 207 val updateThreshold = WireInit((useThreshold << 3) + 21.U) 208 209 val if3_scResps = VecInit(scTables.map(t => t.io.resp)) 210 211 val scUpdateMask = WireInit(0.U.asTypeOf(Vec(SCNTables, Vec(TageBanks, Bool())))) 212 val scUpdateTagePreds = Wire(Vec(TageBanks, Bool())) 213 val scUpdateTakens = Wire(Vec(TageBanks, Bool())) 214 val scUpdateOldCtrs = Wire(Vec(TageBanks, Vec(SCNTables, SInt(SCCtrBits.W)))) 215 scUpdateTagePreds := DontCare 216 scUpdateTakens := DontCare 217 scUpdateOldCtrs := DontCare 218 219 val updateSCMetas = VecInit(u.metas.map(_.tageMeta.scMeta)) 220 221 // for sc ctrs 222 def getCentered(ctr: SInt): SInt = (ctr << 1).asSInt + 1.S 223 // for tage ctrs 224 def getPvdrCentered(ctr: UInt): SInt = ((((ctr.zext - 4.S) << 1).asSInt + 1.S) << 3).asSInt 225 226 for (w <- 0 until TageBanks) { 227 val scMeta = io.meta(w).scMeta 228 scMeta := DontCare 229 // do summation in if3 230 val if3_scTableSums = VecInit( 231 (0 to 1) map { i => { 232 (0 until SCNTables) map { j => 233 getCentered(if3_scResps(j)(w).ctr(i)) 234 } reduce (_+_) // TODO: rewrite with adder tree 235 } 236 } 237 ) 238 239 val providerCtr = if3_providerCtrs(w) 240 val if3_pvdrCtrCentered = getPvdrCentered(providerCtr) 241 val if3_totalSums = VecInit(if3_scTableSums.map(_ + if3_pvdrCtrCentered)) 242 val if3_sumAbs = VecInit(if3_totalSums.map(_.abs.asUInt)) 243 val if3_sumBelowThresholds = VecInit(if3_sumAbs.map(_ < useThreshold)) 244 val if3_scPreds = VecInit(if3_totalSums.map (_ >= 0.S)) 245 246 val if4_sumBelowThresholds = RegEnable(if3_sumBelowThresholds, s3_fire) 247 val if4_scPreds = RegEnable(if3_scPreds, s3_fire) 248 val if4_sumAbs = RegEnable(if3_sumAbs, s3_fire) 249 250 val if4_scCtrs = RegEnable(VecInit(if3_scResps.map(r => r(w).ctr(if3_tageTakens(w).asUInt))), s3_fire) 251 val if4_chooseBit = if4_tageTakens(w) 252 scMeta.tageTaken := if4_tageTakens(w) 253 scMeta.scUsed := if4_provideds(w) 254 scMeta.scPred := if4_scPreds(if4_chooseBit) 255 scMeta.ctrs := if4_scCtrs 256 257 when (if4_provideds(w)) { 258 // Use prediction from Statistical Corrector 259 XSDebug(p"---------tage${w} provided so that sc used---------\n") 260 XSDebug(p"scCtrs:$if4_scCtrs, prdrCtr:${if4_providerCtrs(w)}, sumAbs:$if4_sumAbs, tageTaken:${if4_chooseBit}\n") 261 when (!if4_sumBelowThresholds(if4_chooseBit)) { 262 when (ctrl.sc_enable) { 263 val pred = if4_scPreds(if4_chooseBit) 264 val debug_pc = Cat(packetIdx(debug_pc_s3), w.U, 0.U(instOffsetBits.W)) 265 XSDebug(p"pc(${Hexadecimal(debug_pc)}) SC(${w.U}) overriden pred to ${pred}\n") 266 io.resp.takens(w) := pred 267 } 268 } 269 } 270 271 val updateSCMeta = updateSCMetas(w) 272 val updateTageMeta = updateMetas(w) 273 when (updateValids(w) && updateSCMeta.scUsed.asBool && updateBrMask(w)) { 274 val scPred = updateSCMeta.scPred 275 val tagePred = updateSCMeta.tageTaken 276 val taken = u.takens(w) 277 val scOldCtrs = updateSCMeta.ctrs 278 val pvdrCtr = updateTageMeta.providerCtr 279 val sum = scOldCtrs.map(getCentered).reduce(_+_) + getPvdrCentered(pvdrCtr) 280 val sumAbs = sum.abs.asUInt 281 scUpdateTagePreds(w) := tagePred 282 scUpdateTakens(w) := taken 283 (scUpdateOldCtrs(w) zip scOldCtrs).foreach{case (t, c) => t := c} 284 285 when (scPred =/= tagePred && sumAbs < useThreshold - 2.U) { 286 val newThres = scThreshold.update(scPred =/= taken) 287 scThreshold := newThres 288 XSDebug(p"scThres update: old d${useThreshold} --> new ${newThres.thres}\n") 289 } 290 when (scPred =/= taken || sumAbs < updateThreshold) { 291 scUpdateMask.foreach(t => t(w) := true.B) 292 XSDebug(sum < 0.S, 293 p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " + 294 p"scSum(-$sumAbs), mispred: sc(${scPred =/= taken}), tage(${updateTageMisPreds(w)})\n" 295 ) 296 XSDebug(sum >= 0.S, 297 p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " + 298 p"scSum(+$sumAbs), mispred: sc(${scPred =/= taken}), tage(${updateTageMisPreds(w)})\n" 299 ) 300 XSDebug(p"bank(${w}), update: sc: ${updateSCMeta}\n") 301 } 302 } 303 304 for (i <- 0 until SCNTables) { 305 scTables(i).io.update.mask := RegNext(scUpdateMask(i)) 306 scTables(i).io.update.tagePreds := RegNext(scUpdateTagePreds) 307 scTables(i).io.update.takens := RegNext(scUpdateTakens) 308 scTables(i).io.update.oldCtrs := RegNext(VecInit(scUpdateOldCtrs.map(_(i)))) 309 scTables(i).io.update.pc := RegNext(u.ftqPC) 310 scTables(i).io.update.hist := RegNext(updateHist) 311 } 312 } 313}