xref: /XiangShan/src/main/scala/xiangshan/mem/prefetch/FDP.scala (revision bb2f3f51dd67f6e16e0cc1ffe43368c9fc7e4aef)
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.mem.prefetch
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import freechips.rocketchip.tilelink.ClientStates._
23import freechips.rocketchip.tilelink.MemoryOpCategories._
24import freechips.rocketchip.tilelink.TLPermissions._
25import freechips.rocketchip.tilelink.{ClientMetadata, ClientStates, TLPermissions}
26import xiangshan.backend.rob.RobDebugRollingIO
27import utils._
28import utility._
29import xiangshan.{L1CacheErrorInfo, XSCoreParamsKey}
30import xiangshan.mem.HasL1PrefetchSourceParameter
31import utility.{CircularQueuePtr}
32import xiangshan.cache._
33import xiangshan.{XSBundle, XSModule}
34
35//----------------------------------------
36// Feedback Direct Prefetching
37class CounterFilterDataBundle(implicit p: Parameters) extends DCacheBundle {
38  val idx = UInt(idxBits.W)
39  val way = UInt(wayBits.W)
40}
41
42class CounterFilterQueryBundle(implicit p: Parameters) extends DCacheBundle {
43  val req = ValidIO(new CounterFilterDataBundle())
44  val resp = Input(Bool())
45}
46
47// no Set Blocking in LoadPipe, so when counting useful prefetch counter, duplicate result occurs
48// s0    s1     s2     s3
49// r                   w
50// if 3 load insts is accessing the same cache line(set0, way0) in s0, s1, s2
51// they think they all prefetch hit, increment useful prefetch counter 3 times
52// so when load arrives at s3, save it's set&way to an FIFO, all loads will search this FIFO to avoid this case
53class CounterFilter()(implicit p: Parameters) extends DCacheModule {
54  private val LduCnt = backendParams.LduCnt
55  private val HyuCnt = backendParams.HyuCnt
56
57  val io = IO(new Bundle() {
58    // input, only from load for now
59    val ld_in = Flipped(Vec(LoadPipelineWidth, ValidIO(new CounterFilterDataBundle())))
60    val query = Flipped(Vec(LoadPipelineWidth, new CounterFilterQueryBundle()))
61  })
62
63  val LduStages = 4
64  val SIZE = (LduStages) * LduCnt
65  class Ptr(implicit p: Parameters) extends CircularQueuePtr[Ptr]( p => SIZE ){}
66  object Ptr {
67    def apply(f: Bool, v: UInt)(implicit p: Parameters): Ptr = {
68      val ptr = Wire(new Ptr)
69      ptr.flag := f
70      ptr.value := v
71      ptr
72    }
73  }
74
75  val entries = RegInit(VecInit(Seq.fill(SIZE){ (0.U.asTypeOf(new CounterFilterDataBundle())) }))
76  val valids = RegInit(VecInit(Seq.fill(SIZE){ (false.B) }))
77
78  // enq
79  val enqLen = LduCnt
80  val deqLen = LduCnt
81  val enqPtrExt = RegInit(VecInit((0 until enqLen).map(_.U.asTypeOf(new Ptr))))
82  val deqPtrExt = RegInit(VecInit((0 until deqLen).map(_.U.asTypeOf(new Ptr))))
83
84  val deqPtr = WireInit(deqPtrExt(0).value)
85
86  val reqs_l = io.ld_in.map(_.bits)
87  val reqs_vl = io.ld_in.map(_.valid)
88  val needAlloc = Wire(Vec(enqLen, Bool()))
89  val canAlloc = Wire(Vec(enqLen, Bool()))
90  val last3CycleAlloc = RegInit(0.U(log2Ceil(LduCnt + 1).W))
91
92  for(i <- (0 until enqLen)) {
93    val req = reqs_l(i)
94    val req_v = reqs_vl(i)
95    val index = PopCount(needAlloc.take(i))
96    val allocPtr = enqPtrExt(index)
97
98    needAlloc(i) := req_v
99    canAlloc(i) := needAlloc(i) && allocPtr >= deqPtrExt(0)
100
101    when(canAlloc(i)) {
102      valids(allocPtr.value) := true.B
103      entries(allocPtr.value) := req
104    }
105
106    assert(!needAlloc(i) || canAlloc(i), s"port${i} can not accept CounterFilter enq request, check if SIZE >= (Ldu stages - 2) * LduCnt")
107  }
108  val allocNum = PopCount(canAlloc)
109
110  enqPtrExt.foreach{case x =>
111    when(canAlloc.asUInt.orR){
112      x := x + allocNum
113    }
114  }
115  last3CycleAlloc := RegNext(RegNext(allocNum))
116
117  // deq
118  for(i <- (0 until deqLen)) {
119    when(i.U < last3CycleAlloc) {
120      valids(deqPtrExt(i).value) := false.B
121    }
122  }
123
124  deqPtrExt.foreach{case x => x := x + last3CycleAlloc}
125
126  // query
127  val querys_l = io.query.map(_.req.bits)
128  val querys_vl = io.query.map(_.req.valid)
129  for(i <- (0 until LduCnt + HyuCnt)) {
130    val q = querys_l(i)
131    val q_v = querys_vl(i)
132
133    val entry_match = Cat(entries.zip(valids).map {
134      case(e, v) => v && (q.idx === e.idx) && (q.way === e.way)
135    }).orR
136
137    io.query(i).resp := q_v && entry_match
138  }
139
140  XSPerfAccumulate("req_nums", PopCount(io.query.map(_.req.valid)))
141  XSPerfAccumulate("req_set_way_match", PopCount(io.query.map(_.resp)))
142}
143
144class BloomQueryBundle(n: Int)(implicit p: Parameters) extends DCacheBundle {
145  val addr = UInt(BLOOMADDRWIDTH.W)
146
147  def BLOOMADDRWIDTH = log2Ceil(n)
148
149  def get_addr(paddr: UInt): UInt = {
150    assert(paddr.getWidth == PAddrBits)
151    assert(paddr.getWidth >= (blockOffBits + 2 * BLOOMADDRWIDTH))
152    val block_paddr = paddr(paddr.getWidth - 1, blockOffBits)
153    val low_part = block_paddr(BLOOMADDRWIDTH - 1, 0)
154    val high_part = block_paddr(2 * BLOOMADDRWIDTH - 1, BLOOMADDRWIDTH)
155    low_part ^ high_part
156  }
157}
158
159class BloomRespBundle(implicit p: Parameters) extends DCacheBundle {
160  val res = Bool()
161}
162class BloomFilter(n: Int, bypass: Boolean = true)(implicit p: Parameters) extends DCacheModule {
163  val io = IO(new DCacheBundle {
164    val set = Flipped(ValidIO(new BloomQueryBundle(n)))
165    val clr = Flipped(ValidIO(new BloomQueryBundle(n)))
166    val query = Vec(LoadPipelineWidth, Flipped(ValidIO(new BloomQueryBundle(n))))
167    val resp = Vec(LoadPipelineWidth, ValidIO(new BloomRespBundle))
168  })
169
170  val data = RegInit(0.U(n.W))
171  val data_next = Wire(Vec(n, Bool()))
172
173  for (i <- 0 until n) {
174    when(io.clr.valid && i.U === io.clr.bits.addr) {
175      data_next(i) := false.B
176    }.elsewhen(io.set.valid && i.U === io.set.bits.addr) {
177      data_next(i) := true.B
178    }.otherwise {
179      data_next(i) := data(i).asBool
180    }
181  }
182
183  // resp will valid in next cycle
184  for(i <- 0 until LoadPipelineWidth) {
185    io.resp(i).valid := GatedValidRegNext(io.query(i).valid)
186    if(bypass) {
187      io.resp(i).bits.res := RegEnable(data_next(io.query(i).bits.addr), io.query(i).valid)
188    }else {
189      io.resp(i).bits.res := RegEnable(data(io.query(i).bits.addr), io.query(i).valid)
190    }
191  }
192
193  data := data_next.asUInt
194
195  assert(PopCount(data ^ data_next.asUInt) <= 2.U)
196
197  XSPerfHistogram("valid_nums", PopCount(data), true.B, 0, n + 1, 20)
198}
199
200class FDPrefetcherMonitorBundle()(implicit p: Parameters) extends XSBundle {
201  val refill = Input(Bool()) // from refill pipe, fire
202  val accuracy = new XSBundle {
203    val total_prefetch = Input(Bool()) // from mshr enq, fire, alloc, prefetch
204    val useful_prefetch = Vec(LoadPipelineWidth, Input(Bool())) // from load pipeline, prefetch hit
205  }
206
207  val timely = new XSBundle {
208    val late_prefetch = Input(Bool()) // from mshr enq, a load matches a mshr caused by prefetch
209  }
210
211  val pollution = new XSBundle {
212    val demand_miss = Vec(LoadPipelineWidth, Input(Bool())) // from load pipeline, first miss
213    val cache_pollution = Vec(LoadPipelineWidth, Input(Bool())) // from load pipeline, first miss and pollution caused
214  }
215
216  val pf_ctrl = Output(new PrefetchControlBundle)
217  val debugRolling = Flipped(new RobDebugRollingIO)
218}
219
220class FDPrefetcherMonitor()(implicit p: Parameters) extends XSModule {
221  val io = IO(new FDPrefetcherMonitorBundle)
222
223  val INTERVAL = 8192
224  val CNTWIDTH = log2Up(INTERVAL) + 1
225
226  io.pf_ctrl := DontCare
227
228  val refill_cnt = RegInit(0.U(CNTWIDTH.W))
229
230  val total_prefetch_prev_cnt = RegInit(0.U(CNTWIDTH.W))
231  val useful_prefetch_prev_cnt = RegInit(0.U(CNTWIDTH.W))
232  val late_prefetch_prev_cnt = RegInit(0.U(CNTWIDTH.W))
233  val demand_miss_prev_cnt = RegInit(0.U(CNTWIDTH.W))
234  val pollution_prev_cnt = RegInit(0.U(CNTWIDTH.W))
235  val prev_cnts = Seq(total_prefetch_prev_cnt, useful_prefetch_prev_cnt, late_prefetch_prev_cnt, demand_miss_prev_cnt, pollution_prev_cnt)
236
237  val total_prefetch_interval_cnt = RegInit(0.U(CNTWIDTH.W))
238  val useful_prefetch_interval_cnt = RegInit(0.U(CNTWIDTH.W))
239  val late_prefetch_interval_cnt = RegInit(0.U(CNTWIDTH.W))
240  val demand_miss_interval_cnt = RegInit(0.U(CNTWIDTH.W))
241  val pollution_interval_cnt = RegInit(0.U(CNTWIDTH.W))
242  val interval_cnts = Seq(total_prefetch_interval_cnt, useful_prefetch_interval_cnt, late_prefetch_interval_cnt, demand_miss_interval_cnt, pollution_interval_cnt)
243
244  val interval_trigger = refill_cnt === INTERVAL.U
245
246  val io_ens = Seq(io.accuracy.total_prefetch, io.accuracy.useful_prefetch, io.timely.late_prefetch, io.pollution.demand_miss, io.pollution.cache_pollution)
247
248  for((interval, en) <- interval_cnts.zip(io_ens)) {
249    interval := interval + PopCount(en.asUInt)
250  }
251
252  when(io.refill) {
253    refill_cnt := refill_cnt + 1.U
254  }
255
256  when(interval_trigger) {
257    refill_cnt := 0.U
258    for((prev, interval) <- prev_cnts.zip(interval_cnts)) {
259      prev := Cat(0.U(1.W), prev(prev.getWidth - 1, 1)) + Cat(0.U(1.W), interval(interval.getWidth - 1, 1))
260      interval := 0.U
261    }
262  }
263
264  // rolling by instr
265  XSPerfRolling(
266    "L1PrefetchAccuracyIns",
267    PopCount(io.accuracy.useful_prefetch), PopCount(io.accuracy.total_prefetch),
268    1000, io.debugRolling.robTrueCommit, clock, reset
269  )
270
271  XSPerfRolling(
272    "L1PrefetchLatenessIns",
273    PopCount(io.timely.late_prefetch), PopCount(io.accuracy.total_prefetch),
274    1000, io.debugRolling.robTrueCommit, clock, reset
275  )
276
277  XSPerfRolling(
278    "L1PrefetchPollutionIns",
279    PopCount(io.pollution.cache_pollution), PopCount(io.pollution.demand_miss),
280    1000, io.debugRolling.robTrueCommit, clock, reset
281  )
282
283  XSPerfRolling(
284    "IPCIns",
285    io.debugRolling.robTrueCommit, 1.U,
286    1000, io.debugRolling.robTrueCommit, clock, reset
287  )
288
289  XSPerfAccumulate("io_refill", io.refill)
290  XSPerfAccumulate("total_prefetch_en", io.accuracy.total_prefetch)
291  XSPerfAccumulate("useful_prefetch_en", PopCount(io.accuracy.useful_prefetch) + io.timely.late_prefetch)
292  XSPerfAccumulate("late_prefetch_en", io.timely.late_prefetch)
293  XSPerfAccumulate("demand_miss_en", PopCount(io.pollution.demand_miss))
294  XSPerfAccumulate("cache_pollution_en", PopCount(io.pollution.cache_pollution))
295}