multiperspective_perceptron_tage.hh revision 14081:f99ed78e5263
1/*
2 * Copyright 2019 Texas A&M University
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 *    this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 *    this list of conditions and the following disclaimer in the documentation
12 *    and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 *    contributors may be used to endorse or promote products derived from this
16 *    software without specific prior written permission.
17 *
18 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 *  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 *  Author: Daniel A. Jiménez
31 *  Adapted to gem5 by: Javier Bueno Hedo
32 *
33 */
34
35/*
36 * Multiperspective Perceptron Predictor with TAGE (by Daniel A. Jiménez)
37 */
38
39#ifndef __CPU_PRED_MULTIPERSPECTIVE_PERCEPTRON_TAGE_HH__
40#define __CPU_PRED_MULTIPERSPECTIVE_PERCEPTRON_TAGE_HH__
41
42#include "cpu/pred/loop_predictor.hh"
43#include "cpu/pred/multiperspective_perceptron.hh"
44#include "cpu/pred/statistical_corrector.hh"
45#include "cpu/pred/tage_base.hh"
46#include "params/MPP_LoopPredictor.hh"
47#include "params/MPP_StatisticalCorrector.hh"
48#include "params/MPP_TAGE.hh"
49#include "params/MultiperspectivePerceptronTAGE.hh"
50
51class MPP_TAGE : public TAGEBase {
52    std::vector<unsigned int> tunedHistoryLengths;
53  public:
54    struct BranchInfo : public TAGEBase::BranchInfo {
55        BranchInfo(TAGEBase &tage) : TAGEBase::BranchInfo(tage)
56        {}
57        virtual ~BranchInfo()
58        {}
59    };
60
61    MPP_TAGE(const MPP_TAGEParams *p) : TAGEBase(p),
62        tunedHistoryLengths(p->tunedHistoryLengths)
63    {}
64
65    void calculateParameters() override;
66    void handleTAGEUpdate(Addr branch_pc, bool taken, TAGEBase::BranchInfo* bi)
67        override;
68    void handleAllocAndUReset(bool alloc, bool taken, TAGEBase::BranchInfo* bi,
69                              int nrand) override;
70    void handleUReset() override;
71    void resetUctr(uint8_t &u) override;
72    int bindex(Addr pc_in) const override;
73    bool isHighConfidence(TAGEBase::BranchInfo *bi) const override;
74
75    unsigned getUseAltIdx(TAGEBase::BranchInfo* bi, Addr branch_pc) override;
76    void adjustAlloc(bool & alloc, bool taken, bool pred_taken) override;
77    void updateHistories(ThreadID tid, Addr branch_pc, bool taken,
78                         TAGEBase::BranchInfo* b, bool speculative,
79                         const StaticInstPtr &inst, Addr target) override;
80
81    void updatePathAndGlobalHistory(ThreadHistory& tHist, int brtype,
82                                    bool taken, Addr branch_pc, Addr target);
83};
84
85class MPP_LoopPredictor : public LoopPredictor {
86  public:
87    MPP_LoopPredictor(MPP_LoopPredictorParams *p) : LoopPredictor(p)
88    {}
89
90    bool calcConf(int index) const override;
91    bool optionalAgeInc() const override;
92};
93
94class MPP_StatisticalCorrector : public StatisticalCorrector {
95  protected:
96    int8_t thirdH;
97    // global branch history variation GEHL
98    const unsigned pnb;
99    const unsigned logPnb;
100    std::vector<int> pm;
101    std::vector<int8_t> * pgehl;
102    std::vector<int8_t> wp;
103
104    // global branch history GEHL
105    const unsigned gnb;
106    const unsigned logGnb;
107    std::vector<int> gm;
108    std::vector<int8_t> * ggehl;
109    std::vector<int8_t> wg;
110
111    struct MPP_SCThreadHistory : public StatisticalCorrector::SCThreadHistory
112    {
113        MPP_SCThreadHistory() : globalHist(0), historyStack(16, 0),
114            historyStackPointer(0) {}
115        int64_t globalHist; // global history
116        std::vector<int64_t> historyStack;
117        unsigned int historyStackPointer;
118
119        int64_t getHistoryStackEntry() const
120        {
121            return historyStack[historyStackPointer];
122        }
123
124        void updateHistoryStack(Addr target, bool taken, bool is_call,
125                                bool is_return)
126        {
127            unsigned int truncated_target = target;
128            historyStack[historyStackPointer] =
129                (historyStack[historyStackPointer] << 1) ^ (truncated_target ^
130                (truncated_target >> 5) ^ taken);
131            if (is_return) {
132                historyStackPointer = (historyStackPointer - 1) %
133                    historyStack.size();
134            }
135            if (is_call) {
136                int index = (historyStackPointer + 1) % historyStack.size();
137                historyStack[index] = historyStack[historyStackPointer];
138                historyStackPointer = index;
139            }
140        }
141        unsigned int getPointer() const { return historyStackPointer; }
142    };
143
144  public:
145    struct BranchInfo : public StatisticalCorrector::BranchInfo {
146        virtual ~BranchInfo()
147        {}
148    };
149    MPP_StatisticalCorrector(const MPP_StatisticalCorrectorParams *p);
150
151    void initBias() override;
152    unsigned getIndBias(Addr branch_pc, StatisticalCorrector::BranchInfo* bi,
153                        bool bias) const override;
154    unsigned getIndBiasSK(Addr branch_pc, StatisticalCorrector::BranchInfo* bi)
155            const override;
156    unsigned getIndBiasBank(Addr branch_pc,
157                            StatisticalCorrector::BranchInfo* bi, int hitBank,
158                            int altBank) const override;
159    unsigned getIndUpd(Addr branch_pc) const override;
160    int gIndexLogsSubstr(int nbr, int i) override;
161
162    bool scPredict(ThreadID tid, Addr branch_pc, bool cond_branch,
163                   StatisticalCorrector::BranchInfo* bi, bool prev_pred_taken,
164                   bool bias_bit, bool use_conf_ctr, int8_t conf_ctr,
165                   unsigned conf_bits, int hitBank, int altBank, int64_t phist,
166                   int init_lsum) override;
167
168    void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken,
169                          StatisticalCorrector::BranchInfo *bi,
170                          Addr corrTarget, bool b, int hitBank, int altBank,
171                          int64_t phist) override;
172
173    virtual void getBiasLSUM(Addr branch_pc,
174            StatisticalCorrector::BranchInfo *bi, int &lsum) const = 0;
175
176    void gUpdate(
177        Addr branch_pc, bool taken, int64_t hist, std::vector<int> & length,
178        std::vector<int8_t> * tab, int nbr, int logs,
179        std::vector<int8_t> &w, StatisticalCorrector::BranchInfo* bi) override;
180};
181
182class MultiperspectivePerceptronTAGE : public MultiperspectivePerceptron
183{
184    TAGEBase *tage;
185    LoopPredictor *loopPredictor;
186    StatisticalCorrector *statisticalCorrector;
187
188    /**
189     * Branch information data type
190     */
191    struct MPPTAGEBranchInfo : public MPPBranchInfo
192    {
193        TAGEBase::BranchInfo *tageBranchInfo;
194        LoopPredictor::BranchInfo *lpBranchInfo;
195        StatisticalCorrector::BranchInfo *scBranchInfo;
196        bool predictedTaken;
197        MPPTAGEBranchInfo(Addr pc, int pcshift, bool cond, TAGEBase &tage,
198                          LoopPredictor &loopPredictor,
199                          StatisticalCorrector &statisticalCorrector)
200          : MPPBranchInfo(pc, pcshift, cond),
201            tageBranchInfo(tage.makeBranchInfo()),
202            lpBranchInfo(loopPredictor.makeBranchInfo()),
203            scBranchInfo(statisticalCorrector.makeBranchInfo()),
204            predictedTaken(false)
205        {}
206        virtual ~MPPTAGEBranchInfo()
207        {
208            delete tageBranchInfo;
209            delete lpBranchInfo;
210            delete scBranchInfo;
211        }
212    };
213
214    unsigned int getIndex(ThreadID tid, MPPTAGEBranchInfo &bi,
215                          const HistorySpec &spec, int index) const;
216    int computePartialSum(ThreadID tid, MPPTAGEBranchInfo &bi) const;
217    void updatePartial(ThreadID tid, MPPTAGEBranchInfo &bi, bool taken);
218    void updateHistories(ThreadID tid, MPPTAGEBranchInfo &bi, bool taken);
219
220  public:
221    MultiperspectivePerceptronTAGE(
222        const MultiperspectivePerceptronTAGEParams *p);
223
224    void init() override;
225
226    bool lookup(ThreadID tid, Addr instPC, void * &bp_history) override;
227
228    void update(ThreadID tid, Addr instPC, bool taken,
229            void *bp_history, bool squashed,
230            const StaticInstPtr & inst,
231            Addr corrTarget = MaxAddr) override;
232    void uncondBranch(ThreadID tid, Addr pc, void * &bp_history) override;
233    void squash(ThreadID tid, void *bp_history) override;
234
235};
236#endif//__CPU_PRED_MULTIPERSPECTIVE_PERCEPTRON_TAGE_HH__
237