gem5 [DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
statistical_corrector.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Metempsy Technology Consulting
3 * Copyright (c) 2024 Technical University of Munich
4 * All rights reserved.
5 *
6 * Copyright (c) 2006 INRIA (Institut National de Recherche en
7 * Informatique et en Automatique / French National Research Institute
8 * for Computer Science and Applied Mathematics)
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are
14 * met: redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer;
16 * redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution;
19 * neither the name of the copyright holders nor the names of its
20 * contributors may be used to endorse or promote products derived from
21 * this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * Author: André Seznec, Pau Cabre, Javier Bueno
36 *
37 */
38
39/*
40 * Statistical corrector base class
41 */
42
43#ifndef __CPU_PRED_STATISTICAL_CORRECTOR_HH__
44#define __CPU_PRED_STATISTICAL_CORRECTOR_HH__
45
46#include "base/statistics.hh"
47#include "base/types.hh"
48#include "cpu/static_inst.hh"
49#include "sim/sim_object.hh"
50
51namespace gem5
52{
53
54struct StatisticalCorrectorParams;
55
56namespace branch_prediction
57{
58
60{
61 protected:
62 template<typename T>
63 inline void ctrUpdate(T & ctr, bool taken, int nbits) {
64 assert(nbits <= sizeof(T) << 3);
65 if (nbits > 0) {
66 if (taken) {
67 if (ctr < ((1 << (nbits - 1)) - 1))
68 ctr++;
69 } else {
70 if (ctr > -(1 << (nbits - 1)))
71 ctr--;
72 }
73 }
74 }
75 // histories used for the statistical corrector
77 {
80 {
81 bwHist = 0;
83 imliCount = 0;
84 pHist = 0;
85 }
86 const unsigned instShiftAmt;
87 int64_t bwHist; // backward global history
88 int64_t imliCount;
89 int64_t pHist; // path history from TAGE
90
91 void setNumOrdinalHistories(unsigned num)
92 {
93 assert(num < MaxOrdinalHistories);
95 assert(num > 0);
96 shifts.resize(num);
98 }
99
100 void initLocalHistory(int ordinal, int numHistories, int shift)
101 {
102 assert((ordinal >= 1) && (ordinal <= numOrdinalHistories));
103 shifts[ordinal - 1] = shift;
104 assert(isPowerOf2(numHistories));
105 localHistories[ordinal - 1].resize(numHistories, 0);
106 }
107
108 int64_t getLocalHistory(int ordinal, Addr pc)
109 {
110 assert((ordinal >= 1) && (ordinal <= numOrdinalHistories));
111 unsigned idx = ordinal - 1;
112 return localHistories[idx][getEntry(pc, idx)];
113 }
114
115 void setLocalHistory(int ordinal, Addr pc, int64_t value)
116 {
117 assert((ordinal >= 1) && (ordinal <= numOrdinalHistories));
118 unsigned idx = ordinal - 1;
119 localHistories[idx][getEntry(pc, idx)] = value;
120 }
121
123 int ordinal, Addr branch_pc, bool taken, Addr extraXor = 0)
124 {
125 assert((ordinal >= 1) && (ordinal <= numOrdinalHistories));
126 unsigned idx = ordinal - 1;
127
128 unsigned entry = getEntry(branch_pc, idx);
129 int64_t hist = (localHistories[idx][entry] << 1) + taken;
130
131 if (extraXor) {
132 hist = hist ^ extraXor;
133 }
134
135 localHistories[idx][entry] = hist;
136 }
137
138 private:
142
143 unsigned getEntry(Addr pc, unsigned idx)
144 {
145 Addr shifted_pc = pc >> instShiftAmt;
146 return (shifted_pc ^ (shifted_pc >> shifts[idx]))
147 & (localHistories[idx].size()-1);
148 }
149 };
150
151 // For SC history we use global (i.e. not per thread) non speculative
152 // histories, as some of the strucures needed are quite big and it is not
153 // reasonable to make them per thread and it would be difficult to
154 // rollback on miss-predictions
156
157 const unsigned logBias;
158
159 const unsigned logSizeUp;
160 const unsigned logSizeUps;
161
163
164 // global backward branch history GEHL
165 const unsigned bwnb;
166 const unsigned logBwnb;
170
171 // First local history GEHL
172 const unsigned lnb;
173 const unsigned logLnb;
177
178 // IMLI GEHL
179 const unsigned inb;
180 const unsigned logInb;
184
188
190
193
194 // The two counters used to choose between TAGE ang SC on High Conf
195 // TAGE/Low Conf SC
196 const unsigned chooserConfWidth;
197
198 const unsigned updateThresholdWidth;
199 const unsigned pUpdateThresholdWidth;
200
201 const unsigned extraWeightsWidth;
202
203 const unsigned scCountersWidth;
204
205 const unsigned instShiftAmt;
206
207 // Enable speculative updates of the SC histories
209 // Maximum number of ordinal histories
210 static const int MaxOrdinalHistories = 4;
211
212 int8_t firstH;
213 int8_t secondH;
214
221
222 public:
224 {
225 BranchInfo() : lowConf(false), highConf(false), altConf(false),
226 medConf(false), scPred(false), lsum(0), thres(0),
227 predBeforeSC(false), usedScPred(false),
228 modified(false), pc(0), bwHist(0), imliCount(0), pHist(0),
229 globalHist(0), imHist(0)
230 {}
231
232 // confidences calculated on tage and used on the statistical
233 // correction
238
239 bool scPred;
240 int lsum;
241 int thres;
244
245 // Variables to take a checkpoint of the SC history state
246 // and restore it after a misprediction
247 bool modified; // indicator when the SC history was modified
249 int64_t bwHist; // backward global history
250 int64_t imliCount; // inner most loop interation count
251 int64_t pHist; // path history from TAGE
253 // 8KB SC another global history
254 int64_t globalHist;
255 // 64KB SC uses multiple imliCount
256 int64_t imHist;
257 };
258
259 StatisticalCorrector(const StatisticalCorrectorParams &p);
260
261 virtual BranchInfo *makeBranchInfo();
263
264 virtual void initBias();
265
266 virtual bool scPredict(
267 ThreadID tid, Addr branch_pc, bool cond_branch, BranchInfo *bi,
268 bool prev_pred_taken, bool bias_bit, bool use_conf_ctr,
269 int8_t conf_ctr, unsigned conf_bits, int hitBank, int altBank,
270 int init_lsum = 0);
271
272 virtual unsigned getIndBias(Addr branch_pc, BranchInfo *bi, bool b) const;
273
274 virtual unsigned getIndBiasSK(Addr branch_pc, BranchInfo *bi) const;
275
276 virtual unsigned getIndBiasBank(
277 Addr branch_pc, BranchInfo *bi, int hitBank, int altBank) const = 0;
278
279 virtual unsigned getIndUpd(Addr branch_pc) const;
280 unsigned getIndUpds(Addr branch_pc) const;
281
282 virtual int gPredictions(
283 ThreadID tid, Addr branch_pc, BranchInfo *bi, int &lsum) = 0;
284
285 int calcBias(
286 Addr branch_pc, BranchInfo *bi, bool bias_bit, bool use_conf_ctr,
287 int8_t conf_ctr, unsigned conf_bits, int hitBank, int altBank);
288
289 int64_t gIndex(Addr branch_pc, int64_t bhist, int logs, int nbr, int i);
290
291 virtual int gIndexLogsSubstr(int nbr, int i) = 0;
292
293 int gPredict(
294 Addr branch_pc, int64_t hist, std::vector<int> & length,
295 std::vector<int8_t> * tab, int nbr, int logs,
297
298 virtual void gUpdate(
299 Addr branch_pc, bool taken, int64_t hist, std::vector<int> &length,
300 std::vector<int8_t> *tab, int nbr, int logs,
302
303 void initGEHLTable(
304 unsigned numLenghts, std::vector<int> lengths,
305 std::vector<int8_t> *&table, unsigned logNumEntries,
306 std::vector<int8_t> &w, int8_t wInitValue);
307
308 virtual void scHistoryUpdate(
309 Addr branch_pc, const StaticInstPtr &inst,
310 bool taken, Addr target, int64_t phist);
311
312 void updateHistories(
313 Addr branch_pc, bool speculative,
314 const StaticInstPtr &inst , bool taken,
315 BranchInfo *bi, Addr target, int64_t phist);
316
317 virtual void scRecordHistState(Addr branch_pc, BranchInfo *bi);
318 virtual bool scRestoreHistState(BranchInfo *bi);
319
320 virtual void gUpdates(
321 ThreadID tid, Addr pc, bool taken, BranchInfo *bi) = 0;
322
323 void init() override;
324 void updateStats(bool taken, BranchInfo *bi);
325
326 virtual void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken,
327 BranchInfo *bi, Addr corrTarget, bool bias_bit,
328 int hitBank, int altBank);
329
330 virtual size_t getSizeInBits() const;
331};
332
333} // namespace branch_prediction
334} // namespace gem5
335
336#endif//__CPU_PRED_STATISTICAL_CORRECTOR_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
void initGEHLTable(unsigned numLenghts, std::vector< int > lengths, std::vector< int8_t > *&table, unsigned logNumEntries, std::vector< int8_t > &w, int8_t wInitValue)
virtual int gPredictions(ThreadID tid, Addr branch_pc, BranchInfo *bi, int &lsum)=0
gem5::branch_prediction::StatisticalCorrector::StatisticalCorrectorStats stats
virtual unsigned getIndBiasSK(Addr branch_pc, BranchInfo *bi) const
int gPredict(Addr branch_pc, int64_t hist, std::vector< int > &length, std::vector< int8_t > *tab, int nbr, int logs, std::vector< int8_t > &w)
virtual void scHistoryUpdate(Addr branch_pc, const StaticInstPtr &inst, bool taken, Addr target, int64_t phist)
virtual void scRecordHistState(Addr branch_pc, BranchInfo *bi)
StatisticalCorrector(const StatisticalCorrectorParams &p)
virtual unsigned getIndUpd(Addr branch_pc) const
int calcBias(Addr branch_pc, BranchInfo *bi, bool bias_bit, bool use_conf_ctr, int8_t conf_ctr, unsigned conf_bits, int hitBank, int altBank)
virtual unsigned getIndBias(Addr branch_pc, BranchInfo *bi, bool b) const
void ctrUpdate(T &ctr, bool taken, int nbits)
void updateHistories(Addr branch_pc, bool speculative, const StaticInstPtr &inst, bool taken, BranchInfo *bi, Addr target, int64_t phist)
virtual void gUpdate(Addr branch_pc, bool taken, int64_t hist, std::vector< int > &length, std::vector< int8_t > *tab, int nbr, int logs, std::vector< int8_t > &w, BranchInfo *bi)
void init() override
init() is called after all C++ SimObjects have been created and all ports are connected.
virtual unsigned getIndBiasBank(Addr branch_pc, BranchInfo *bi, int hitBank, int altBank) const =0
virtual void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken, BranchInfo *bi, Addr corrTarget, bool bias_bit, int hitBank, int altBank)
virtual bool scPredict(ThreadID tid, Addr branch_pc, bool cond_branch, BranchInfo *bi, bool prev_pred_taken, bool bias_bit, bool use_conf_ctr, int8_t conf_ctr, unsigned conf_bits, int hitBank, int altBank, int init_lsum=0)
virtual int gIndexLogsSubstr(int nbr, int i)=0
int64_t gIndex(Addr branch_pc, int64_t bhist, int logs, int nbr, int i)
virtual void gUpdates(ThreadID tid, Addr pc, bool taken, BranchInfo *bi)=0
Statistics container.
Definition group.hh:93
This is a simple scalar statistic, like a counter.
STL vector class.
Definition stl.hh:37
static constexpr bool isPowerOf2(const T &n)
Definition intmath.hh:98
SimObject(const Params &p)
Definition sim_object.cc:58
Bitfield< 7 > b
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 6, 5 > shift
Definition types.hh:117
Bitfield< 4 > pc
Bitfield< 0 > p
Bitfield< 0 > w
Bitfield< 20, 16 > bi
Definition types.hh:80
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
int16_t ThreadID
Thread index/ID type.
Definition types.hh:235
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
RefCountingPtr< StaticInst > StaticInstPtr
Declaration of Statistics objects.
void initLocalHistory(int ordinal, int numHistories, int shift)
void updateLocalHistory(int ordinal, Addr branch_pc, bool taken, Addr extraXor=0)

Generated on Mon May 26 2025 09:19:08 for gem5 by doxygen 1.13.2