xref: /XiangShan/src/main/scala/xiangshan/mem/prefetch/FDP.scala (revision f7063a43ab34da917ba6c670d21871314340c550)
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 => x := x + allocNum}
111  last3CycleAlloc := RegNext(RegNext(allocNum))
112
113  // deq
114  for(i <- (0 until deqLen)) {
115    when(i.U < last3CycleAlloc) {
116      valids(deqPtrExt(i).value) := false.B
117    }
118  }
119
120  deqPtrExt.foreach{case x => x := x + last3CycleAlloc}
121
122  // query
123  val querys_l = io.query.map(_.req.bits)
124  val querys_vl = io.query.map(_.req.valid)
125  for(i <- (0 until LduCnt + HyuCnt)) {
126    val q = querys_l(i)
127    val q_v = querys_vl(i)
128
129    val entry_match = Cat(entries.zip(valids).map {
130      case(e, v) => v && (q.idx === e.idx) && (q.way === e.way)
131    }).orR
132
133    io.query(i).resp := q_v && entry_match
134  }
135
136  XSPerfAccumulate("req_nums", PopCount(io.query.map(_.req.valid)))
137  XSPerfAccumulate("req_set_way_match", PopCount(io.query.map(_.resp)))
138}
139
140class BloomQueryBundle(n: Int)(implicit p: Parameters) extends DCacheBundle {
141  val addr = UInt(BLOOMADDRWIDTH.W)
142
143  def BLOOMADDRWIDTH = log2Ceil(n)
144
145  def get_addr(paddr: UInt): UInt = {
146    assert(paddr.getWidth == PAddrBits)
147    assert(paddr.getWidth >= (blockOffBits + 2 * BLOOMADDRWIDTH))
148    val block_paddr = paddr(paddr.getWidth - 1, blockOffBits)
149    val low_part = block_paddr(BLOOMADDRWIDTH - 1, 0)
150    val high_part = block_paddr(2 * BLOOMADDRWIDTH - 1, BLOOMADDRWIDTH)
151    low_part ^ high_part
152  }
153}
154
155class BloomRespBundle(implicit p: Parameters) extends DCacheBundle {
156  val res = Bool()
157}
158class BloomFilter(n: Int, bypass: Boolean = true)(implicit p: Parameters) extends DCacheModule {
159  val io = IO(new DCacheBundle {
160    val set = Flipped(ValidIO(new BloomQueryBundle(n)))
161    val clr = Flipped(ValidIO(new BloomQueryBundle(n)))
162    val query = Vec(LoadPipelineWidth, Flipped(ValidIO(new BloomQueryBundle(n))))
163    val resp = Vec(LoadPipelineWidth, ValidIO(new BloomRespBundle))
164  })
165
166  val data = RegInit(0.U(n.W))
167  val data_next = Wire(Vec(n, Bool()))
168
169  for (i <- 0 until n) {
170    when(io.clr.valid && i.U === io.clr.bits.addr) {
171      data_next(i) := false.B
172    }.elsewhen(io.set.valid && i.U === io.set.bits.addr) {
173      data_next(i) := true.B
174    }.otherwise {
175      data_next(i) := data(i).asBool
176    }
177  }
178
179  // resp will valid in next cycle
180  for(i <- 0 until LoadPipelineWidth) {
181    io.resp(i).valid := RegNext(io.query(i).valid)
182    if(bypass) {
183      io.resp(i).bits.res := RegEnable(data_next(io.query(i).bits.addr), io.query(i).valid)
184    }else {
185      io.resp(i).bits.res := RegEnable(data(io.query(i).bits.addr), io.query(i).valid)
186    }
187  }
188
189  data := data_next.asUInt
190
191  assert(PopCount(data ^ data_next.asUInt) <= 2.U)
192
193  XSPerfHistogram("valid_nums", PopCount(data), true.B, 0, n + 1, 20)
194}
195
196class FDPrefetcherMonitorBundle()(implicit p: Parameters) extends XSBundle {
197  val refill = Input(Bool()) // from refill pipe, fire
198  val accuracy = new XSBundle {
199    val total_prefetch = Input(Bool()) // from mshr enq, fire, alloc, prefetch
200    val useful_prefetch = Vec(LoadPipelineWidth, Input(Bool())) // from load pipeline, prefetch hit
201  }
202
203  val timely = new XSBundle {
204    val late_prefetch = Input(Bool()) // from mshr enq, a load matches a mshr caused by prefetch
205  }
206
207  val pollution = new XSBundle {
208    val demand_miss = Vec(LoadPipelineWidth, Input(Bool())) // from load pipeline, fisrt miss
209    val cache_pollution = Vec(LoadPipelineWidth, Input(Bool())) // from load pipeline, fisrt miss and pollution caused
210  }
211
212  val pf_ctrl = Output(new PrefetchControlBundle)
213  val debugRolling = Flipped(new RobDebugRollingIO)
214}
215
216class FDPrefetcherMonitor()(implicit p: Parameters) extends XSModule {
217  val io = IO(new FDPrefetcherMonitorBundle)
218
219  val INTERVAL = 8192
220  val CNTWIDTH = log2Up(INTERVAL) + 1
221
222  io.pf_ctrl := DontCare
223
224  val refill_cnt = RegInit(0.U(CNTWIDTH.W))
225
226  val total_prefetch_prev_cnt = RegInit(0.U(CNTWIDTH.W))
227  val useful_prefetch_prev_cnt = RegInit(0.U(CNTWIDTH.W))
228  val late_prefetch_prev_cnt = RegInit(0.U(CNTWIDTH.W))
229  val demand_miss_prev_cnt = RegInit(0.U(CNTWIDTH.W))
230  val pollution_prev_cnt = RegInit(0.U(CNTWIDTH.W))
231  val prev_cnts = Seq(total_prefetch_prev_cnt, useful_prefetch_prev_cnt, late_prefetch_prev_cnt, demand_miss_prev_cnt, pollution_prev_cnt)
232
233  val total_prefetch_interval_cnt = RegInit(0.U(CNTWIDTH.W))
234  val useful_prefetch_interval_cnt = RegInit(0.U(CNTWIDTH.W))
235  val late_prefetch_interval_cnt = RegInit(0.U(CNTWIDTH.W))
236  val demand_miss_interval_cnt = RegInit(0.U(CNTWIDTH.W))
237  val pollution_interval_cnt = RegInit(0.U(CNTWIDTH.W))
238  val interval_cnts = Seq(total_prefetch_interval_cnt, useful_prefetch_interval_cnt, late_prefetch_interval_cnt, demand_miss_interval_cnt, pollution_interval_cnt)
239
240  val interval_trigger = refill_cnt === INTERVAL.U
241
242  val io_ens = Seq(io.accuracy.total_prefetch, io.accuracy.useful_prefetch, io.timely.late_prefetch, io.pollution.demand_miss, io.pollution.cache_pollution)
243
244  for((interval, en) <- interval_cnts.zip(io_ens)) {
245    interval := interval + PopCount(en.asUInt)
246  }
247
248  when(io.refill) {
249    refill_cnt := refill_cnt + 1.U
250  }
251
252  when(interval_trigger) {
253    refill_cnt := 0.U
254    for((prev, interval) <- prev_cnts.zip(interval_cnts)) {
255      prev := Cat(0.U(1.W), prev(prev.getWidth - 1, 1)) + Cat(0.U(1.W), interval(interval.getWidth - 1, 1))
256      interval := 0.U
257    }
258  }
259
260  // rolling by instr
261  XSPerfRolling(
262    "L1PrefetchAccuracyIns",
263    PopCount(io.accuracy.useful_prefetch), PopCount(io.accuracy.total_prefetch),
264    1000, io.debugRolling.robTrueCommit, clock, reset
265  )
266
267  XSPerfRolling(
268    "L1PrefetchLatenessIns",
269    PopCount(io.timely.late_prefetch), PopCount(io.accuracy.total_prefetch),
270    1000, io.debugRolling.robTrueCommit, clock, reset
271  )
272
273  XSPerfRolling(
274    "L1PrefetchPollutionIns",
275    PopCount(io.pollution.cache_pollution), PopCount(io.pollution.demand_miss),
276    1000, io.debugRolling.robTrueCommit, clock, reset
277  )
278
279  XSPerfRolling(
280    "IPCIns",
281    io.debugRolling.robTrueCommit, 1.U,
282    1000, io.debugRolling.robTrueCommit, clock, reset
283  )
284
285  XSPerfAccumulate("io_refill", io.refill)
286  XSPerfAccumulate("total_prefetch_en", io.accuracy.total_prefetch)
287  XSPerfAccumulate("useful_prefetch_en", PopCount(io.accuracy.useful_prefetch) + io.timely.late_prefetch)
288  XSPerfAccumulate("late_prefetch_en", io.timely.late_prefetch)
289  XSPerfAccumulate("demand_miss_en", PopCount(io.pollution.demand_miss))
290  XSPerfAccumulate("cache_pollution_en", PopCount(io.pollution.cache_pollution))
291}