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.frontend 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import utils._ 24import chisel3.experimental.chiselName 25 26import scala.math.min 27 28trait HasSCParameter extends TageParams { 29} 30 31class SCReq(implicit p: Parameters) extends TageReq 32 33abstract class SCBundle(implicit p: Parameters) extends TageBundle with HasSCParameter {} 34abstract class SCModule(implicit p: Parameters) extends TageModule with HasSCParameter {} 35 36 37class SCMeta(val ntables: Int)(implicit p: Parameters) extends XSBundle with HasSCParameter { 38 val tageTakens = Vec(numBr, Bool()) 39 val scUsed = Bool() 40 val scPreds = Vec(numBr, Bool()) 41 // Suppose ctrbits of all tables are identical 42 val ctrs = Vec(numBr, Vec(ntables, SInt(SCCtrBits.W))) 43} 44 45 46class SCResp(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle { 47 val ctrs = Vec(numBr, Vec(2, SInt(ctrBits.W))) 48} 49 50class SCUpdate(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle { 51 val pc = UInt(VAddrBits.W) 52 val folded_hist = new AllFoldedHistories(foldedGHistInfos) 53 val mask = Vec(numBr, Bool()) 54 val oldCtrs = Vec(numBr, SInt(ctrBits.W)) 55 val tagePreds = Vec(numBr, Bool()) 56 val takens = Vec(numBr, Bool()) 57} 58 59class SCTableIO(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle { 60 val req = Input(Valid(new SCReq)) 61 val resp = Output(new SCResp(ctrBits)) 62 val update = Input(new SCUpdate(ctrBits)) 63} 64 65@chiselName 66class SCTable(val nRows: Int, val ctrBits: Int, val histLen: Int)(implicit p: Parameters) 67 extends SCModule with HasFoldedHistory { 68 val io = IO(new SCTableIO(ctrBits)) 69 70 // val table = Module(new SRAMTemplate(SInt(ctrBits.W), set=nRows, way=2*TageBanks, shouldReset=true, holdRead=true, singlePort=false)) 71 val table = Module(new SRAMTemplate(SInt(ctrBits.W), set=nRows, way=2*TageBanks, shouldReset=true, holdRead=true, singlePort=false)) 72 73 // def getIdx(hist: UInt, pc: UInt) = { 74 // (compute_folded_ghist(hist, log2Ceil(nRows)) ^ (pc >> instOffsetBits))(log2Ceil(nRows)-1,0) 75 // } 76 77 78 val idxFhInfo = (histLen, min(log2Ceil(nRows), histLen)) 79 80 def getFoldedHistoryInfo = Set(idxFhInfo).filter(_._1 > 0) 81 82 def getIdx(pc: UInt, allFh: AllFoldedHistories) = { 83 if (histLen > 0) { 84 val idx_fh = allFh.getHistWithInfo(idxFhInfo).folded_hist 85 // require(idx_fh.getWidth == log2Ceil(nRows)) 86 ((pc >> instOffsetBits) ^ idx_fh)(log2Ceil(nRows)-1,0) 87 } 88 else { 89 (pc >> instOffsetBits)(log2Ceil(nRows)-1,0) 90 } 91 } 92 93 def ctrUpdate(ctr: SInt, cond: Bool): SInt = signedSatUpdate(ctr, ctrBits, cond) 94 95 val s0_idx = getIdx(io.req.bits.pc, io.req.bits.folded_hist) 96 val s1_idx = RegEnable(s0_idx, enable=io.req.valid) 97 98 table.io.r.req.valid := io.req.valid 99 table.io.r.req.bits.setIdx := s0_idx 100 101 for (i <- 0 until numBr) { 102 io.resp.ctrs(i)(0) := table.io.r.resp.data(2*i) 103 io.resp.ctrs(i)(1) := table.io.r.resp.data(2*i+1) 104 } 105 106 val update_wdata = Wire(Vec(numBr, SInt(ctrBits.W))) 107 val update_wdata_packed = VecInit(update_wdata.map(Seq.fill(2)(_)).reduce(_++_)) 108 val updateWayMask = Wire(Vec(2*numBr, Bool())) 109 110 for (i <- 0 until numBr) { 111 updateWayMask(2*i) := io.update.mask(i) && !io.update.tagePreds(i) 112 updateWayMask(2*i+1) := io.update.mask(i) && io.update.tagePreds(i) 113 } 114 115 val update_idx = getIdx(io.update.pc, io.update.folded_hist) 116 117 table.io.w.apply( 118 valid = io.update.mask.reduce(_||_), 119 data = update_wdata_packed, 120 setIdx = update_idx, 121 waymask = updateWayMask.asUInt 122 ) 123 124 val wrBypassEntries = 16 125 126 val wrbypasses = Seq.fill(numBr)(Module(new WrBypass(SInt(ctrBits.W), wrBypassEntries, log2Ceil(nRows), numWays=2))) 127 128 for (i <- 0 until numBr) { 129 val wrbypass = wrbypasses(i) 130 val ctrPos = io.update.tagePreds(i) 131 val altPos = !io.update.tagePreds(i) 132 val bypass_ctr = wrbypass.io.hit_data(ctrPos) 133 val hit_and_valid = wrbypass.io.hit && bypass_ctr.valid 134 val oldCtr = Mux(hit_and_valid, bypass_ctr.bits, io.update.oldCtrs(i)) 135 update_wdata(i) := ctrUpdate(oldCtr, io.update.takens(i)) 136 137 wrbypass.io.wen := io.update.mask(i) 138 wrbypass.io.write_data := update_wdata_packed.slice(2*i, 2*i+2) 139 wrbypass.io.write_idx := update_idx 140 wrbypass.io.write_way_mask.map(_ := updateWayMask.slice(2*i, 2*i+2)) 141 } 142 143 144 val u = io.update 145 XSDebug(io.req.valid, 146 p"scTableReq: pc=0x${Hexadecimal(io.req.bits.pc)}, " + 147 p"s0_idx=${s0_idx}\n") 148 XSDebug(RegNext(io.req.valid), 149 p"scTableResp: s1_idx=${s1_idx}," + 150 p"ctr:${io.resp.ctrs}\n") 151 XSDebug(io.update.mask.reduce(_||_), 152 p"update Table: pc:${Hexadecimal(u.pc)}, " + 153 p"tageTakens:${u.tagePreds}, taken:${u.takens}, oldCtr:${u.oldCtrs}\n") 154} 155 156class SCThreshold(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle { 157 val ctr = UInt(ctrBits.W) 158 def satPos(ctr: UInt = this.ctr) = ctr === ((1.U << ctrBits) - 1.U) 159 def satNeg(ctr: UInt = this.ctr) = ctr === 0.U 160 def neutralVal = (1.U << (ctrBits - 1)) 161 val thres = UInt(8.W) 162 def initVal = 6.U 163 def minThres = 6.U 164 def maxThres = 31.U 165 def update(cause: Bool): SCThreshold = { 166 val res = Wire(new SCThreshold(this.ctrBits)) 167 val newCtr = satUpdate(this.ctr, this.ctrBits, cause) 168 val newThres = Mux(res.satPos(newCtr) && this.thres <= maxThres, this.thres + 2.U, 169 Mux(res.satNeg(newCtr) && this.thres >= minThres, this.thres - 2.U, 170 this.thres)) 171 res.thres := newThres 172 res.ctr := Mux(res.satPos(newCtr) || res.satNeg(newCtr), res.neutralVal, newCtr) 173 // XSDebug(true.B, p"scThres Update: cause${cause} newCtr ${newCtr} newThres ${newThres}\n") 174 res 175 } 176} 177 178object SCThreshold { 179 def apply(bits: Int)(implicit p: Parameters) = { 180 val t = Wire(new SCThreshold(ctrBits=bits)) 181 t.ctr := t.neutralVal 182 t.thres := t.initVal 183 t 184 } 185} 186 187 188trait HasSC extends HasSCParameter with HasPerfEvents { this: Tage => 189 val update_on_mispred, update_on_unconf = WireInit(0.U.asTypeOf(Vec(TageBanks, Bool()))) 190 var sc_fh_info = Set[FoldedHistoryInfo]() 191 if (EnableSC) { 192 val scTables = SCTableInfos.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.s0_fire 197 req.bits.pc := s0_pc 198 req.bits.folded_hist := io.in.bits.folded_hist 199 req.bits.ghist := DontCare 200 if (!EnableSC) {t.io.update := DontCare} 201 t 202 } 203 } 204 sc_fh_info = scTables.map(_.getFoldedHistoryInfo).reduce(_++_).toSet 205 206 val scThresholds = List.fill(TageBanks)(RegInit(SCThreshold(5))) 207 val useThresholds = VecInit(scThresholds map (_.thres)) 208 209 def sign(x: SInt) = x(x.getWidth-1) 210 def pos(x: SInt) = !sign(x) 211 def neg(x: SInt) = sign(x) 212 213 def aboveThreshold(scSum: SInt, tagePvdr: SInt, threshold: UInt): Bool = { 214 val signedThres = threshold.zext 215 val totalSum = scSum +& tagePvdr 216 (scSum > signedThres - tagePvdr) && pos(totalSum) || 217 (scSum < -signedThres - tagePvdr) && neg(totalSum) 218 } 219 val updateThresholds = VecInit(useThresholds map (t => (t << 3) +& 21.U)) 220 221 val s1_scResps = VecInit(scTables.map(t => t.io.resp)) 222 223 val scUpdateMask = WireInit(0.U.asTypeOf(Vec(numBr, Vec(SCNTables, Bool())))) 224 val scUpdateTagePreds = Wire(Vec(TageBanks, Bool())) 225 val scUpdateTakens = Wire(Vec(TageBanks, Bool())) 226 val scUpdateOldCtrs = Wire(Vec(numBr, Vec(SCNTables, SInt(SCCtrBits.W)))) 227 scUpdateTagePreds := DontCare 228 scUpdateTakens := DontCare 229 scUpdateOldCtrs := DontCare 230 231 val updateSCMeta = updateMeta.scMeta.get 232 233 val s2_sc_used, s2_conf, s2_unconf, s2_agree, s2_disagree = 234 0.U.asTypeOf(Vec(TageBanks, Bool())) 235 val update_sc_used, update_conf, update_unconf, update_agree, update_disagree = 236 0.U.asTypeOf(Vec(TageBanks, Bool())) 237 val sc_misp_tage_corr, sc_corr_tage_misp = 238 0.U.asTypeOf(Vec(TageBanks, Bool())) 239 240 // for sc ctrs 241 def getCentered(ctr: SInt): SInt = Cat(ctr, 1.U(1.W)).asSInt 242 // for tage ctrs, (2*(ctr-4)+1)*8 243 def getPvdrCentered(ctr: UInt): SInt = Cat(ctr ^ (1 << (TageCtrBits-1)).U, 1.U(1.W), 0.U(3.W)).asSInt 244 245 val scMeta = resp_meta.scMeta.get 246 scMeta := DontCare 247 for (w <- 0 until TageBanks) { 248 // do summation in s2 249 val s1_scTableSums = VecInit( 250 (0 to 1) map { i => 251 ParallelSingedExpandingAdd(s1_scResps map (r => getCentered(r.ctrs(w)(i)))) // TODO: rewrite with wallace tree 252 } 253 ) 254 val s2_scTableSums = RegEnable(s1_scTableSums, io.s1_fire) 255 val s2_tagePrvdCtrCentered = getPvdrCentered(RegEnable(s1_providerResp.ctrs(w), io.s1_fire)) 256 val s2_totalSums = s2_scTableSums.map(_ +& s2_tagePrvdCtrCentered) 257 val s2_sumAboveThresholds = aboveThreshold(s2_scTableSums(w), s2_tagePrvdCtrCentered, useThresholds(w)) 258 val s2_scPreds = VecInit(s2_totalSums.map(_ >= 0.S)) 259 260 val s2_scResps = VecInit(RegEnable(s1_scResps, io.s1_fire).map(_.ctrs(w))) 261 val s2_scCtrs = VecInit(s2_scResps.map(_(s2_tageTakens(w).asUInt))) 262 val s2_chooseBit = s2_tageTakens(w) 263 264 val s2_pred = 265 Mux(s2_provided && s2_sumAboveThresholds(s2_chooseBit), 266 s2_scPreds(s2_chooseBit), 267 s2_tageTakens(w) 268 ) 269 270 scMeta.tageTakens(w) := RegEnable(s2_tageTakens(w), io.s2_fire) 271 scMeta.scUsed := RegEnable(s2_provided, io.s2_fire) 272 scMeta.scPreds(w) := RegEnable(s2_scPreds(s2_chooseBit), io.s2_fire) 273 scMeta.ctrs(w) := RegEnable(s2_scCtrs, io.s2_fire) 274 275 when (s2_provided) { 276 s2_sc_used(w) := true.B 277 s2_unconf(w) := !s2_sumAboveThresholds(s2_chooseBit) 278 s2_conf(w) := s2_sumAboveThresholds(s2_chooseBit) 279 // Use prediction from Statistical Corrector 280 XSDebug(p"---------tage_bank_${w} provided so that sc used---------\n") 281 when (s2_sumAboveThresholds(s2_chooseBit)) { 282 val pred = s2_scPreds(s2_chooseBit) 283 val debug_pc = Cat(debug_pc_s2, w.U, 0.U(instOffsetBits.W)) 284 s2_agree(w) := s2_tageTakens(w) === pred 285 s2_disagree(w) := s2_tageTakens(w) =/= pred 286 // fit to always-taken condition 287 // io.out.resp.s2.full_pred.br_taken_mask(w) := pred 288 XSDebug(p"pc(${Hexadecimal(debug_pc)}) SC(${w.U}) overriden pred to ${pred}\n") 289 } 290 } 291 292 io.out.resp.s3.full_pred.br_taken_mask(w) := RegEnable(s2_pred, io.s2_fire) 293 294 val updateTageMeta = updateMeta 295 when (updateValids(w) && updateSCMeta.scUsed.asBool) { 296 val scPred = updateSCMeta.scPreds(w) 297 val tagePred = updateSCMeta.tageTakens(w) 298 val taken = update.full_pred.br_taken_mask(w) 299 val scOldCtrs = updateSCMeta.ctrs(w) 300 val pvdrCtr = updateTageMeta.providerResp.ctrs(w) 301 val sum = ParallelSingedExpandingAdd(scOldCtrs.map(getCentered)) +& getPvdrCentered(pvdrCtr) 302 val sumAbs = sum.abs.asUInt 303 val sumAboveThreshold = aboveThreshold(sum, getPvdrCentered(pvdrCtr), useThresholds(w)) 304 scUpdateTagePreds(w) := tagePred 305 scUpdateTakens(w) := taken 306 (scUpdateOldCtrs(w) zip scOldCtrs).foreach{case (t, c) => t := c} 307 308 update_sc_used(w) := true.B 309 update_unconf(w) := !sumAboveThreshold 310 update_conf(w) := sumAboveThreshold 311 update_agree(w) := scPred === tagePred 312 update_disagree(w) := scPred =/= tagePred 313 sc_corr_tage_misp(w) := scPred === taken && tagePred =/= taken && update_conf(w) 314 sc_misp_tage_corr(w) := scPred =/= taken && tagePred === taken && update_conf(w) 315 316 val thres = useThresholds(w) 317 when (scPred =/= tagePred && sumAbs >= thres - 4.U && sumAbs <= thres - 2.U) { 318 val newThres = scThresholds(w).update(scPred =/= taken) 319 scThresholds(w) := newThres 320 XSDebug(p"scThres $w update: old ${useThresholds(w)} --> new ${newThres.thres}\n") 321 } 322 323 val updateThres = updateThresholds(w) 324 when (scPred =/= taken || !sumAboveThreshold) { 325 scUpdateMask(w).foreach(_ := true.B) 326 XSDebug(sum < 0.S, 327 p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " + 328 p"scSum(-$sumAbs), mispred: sc(${scPred =/= taken}), tage(${updateMisPreds(w)})\n" 329 ) 330 XSDebug(sum >= 0.S, 331 p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " + 332 p"scSum(+$sumAbs), mispred: sc(${scPred =/= taken}), tage(${updateMisPreds(w)})\n" 333 ) 334 XSDebug(p"bank(${w}), update: sc: ${updateSCMeta}\n") 335 update_on_mispred(w) := scPred =/= taken 336 update_on_unconf(w) := scPred === taken 337 } 338 } 339 } 340 341 342 for (b <- 0 until TageBanks) { 343 for (i <- 0 until SCNTables) { 344 scTables(i).io.update.mask(b) := RegNext(scUpdateMask(b)(i)) 345 scTables(i).io.update.tagePreds(b) := RegNext(scUpdateTagePreds(b)) 346 scTables(i).io.update.takens(b) := RegNext(scUpdateTakens(b)) 347 scTables(i).io.update.oldCtrs(b) := RegNext(scUpdateOldCtrs(b)(i)) 348 scTables(i).io.update.pc := RegNext(update.pc) 349 scTables(i).io.update.folded_hist := RegNext(updateFHist) 350 } 351 } 352 353 tage_perf("sc_conf", PopCount(s2_conf), PopCount(update_conf)) 354 tage_perf("sc_unconf", PopCount(s2_unconf), PopCount(update_unconf)) 355 tage_perf("sc_agree", PopCount(s2_agree), PopCount(update_agree)) 356 tage_perf("sc_disagree", PopCount(s2_disagree), PopCount(update_disagree)) 357 tage_perf("sc_used", PopCount(s2_sc_used), PopCount(update_sc_used)) 358 XSPerfAccumulate("sc_update_on_mispred", PopCount(update_on_mispred)) 359 XSPerfAccumulate("sc_update_on_unconf", PopCount(update_on_unconf)) 360 XSPerfAccumulate("sc_mispred_but_tage_correct", PopCount(sc_misp_tage_corr)) 361 XSPerfAccumulate("sc_correct_and_tage_wrong", PopCount(sc_corr_tage_misp)) 362 363 } 364 365 override def getFoldedHistoryInfo = Some(tage_fh_info ++ sc_fh_info) 366 367 val perfEvents = Seq( 368 ("tage_tht_hit ", updateMeta.provider.valid), 369 ("sc_update_on_mispred ", PopCount(update_on_mispred) ), 370 ("sc_update_on_unconf ", PopCount(update_on_unconf) ), 371 ) 372 generatePerfEvent() 373} 374