summaryrefslogtreecommitdiff
path: root/src/mem/cache/prefetch/delta_correlating_prediction_tables.cc
blob: 4dbd596a2b949cea67f2f755a36fce96d156ed98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
 * Copyright (c) 2018 Metempsy Technology Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Javier Bueno
 */

#include "mem/cache/prefetch/delta_correlating_prediction_tables.hh"

#include "debug/HWPrefetch.hh"
#include "mem/cache/prefetch/associative_set_impl.hh"
#include "params/DCPTPrefetcher.hh"
#include "params/DeltaCorrelatingPredictionTables.hh"

DeltaCorrelatingPredictionTables::DeltaCorrelatingPredictionTables(
   DeltaCorrelatingPredictionTablesParams *p) : SimObject(p),
   deltaBits(p->delta_bits), deltaMaskBits(p->delta_mask_bits),
   table(p->table_assoc, p->table_entries, p->table_indexing_policy,
         p->table_replacement_policy, DCPTEntry(p->deltas_per_entry))
{
}

void
DeltaCorrelatingPredictionTables::DCPTEntry::reset()
{
    for (auto &delta : deltas) {
        delta = 0;
    }
    lastAddress = 0;
    deltaPointer = 0;
}

void
DeltaCorrelatingPredictionTables::DCPTEntry::addAddress(Addr address,
    unsigned int delta_bits)
{
    if ((address - lastAddress) != 0) {
        Addr delta = address - lastAddress;
        // Account for the sign bit
        Addr max_positive_delta = (1 << (delta_bits-1)) - 1;
        if (address > lastAddress) {
            // check positive delta overflow
            if (delta > max_positive_delta) {
                delta = 0;
            }
        } else {
            // check negative delta overflow
            if (lastAddress - address > (max_positive_delta + 1)) {
                delta = 0;
            }
        }
        deltas[deltaPointer] = delta;
        deltaPointer = (deltaPointer + 1) % deltas.size();
        lastAddress = address;
    }
}

void
DeltaCorrelatingPredictionTables::DCPTEntry::getCandidates(
    std::vector<QueuedPrefetcher::AddrPriority> &pfs, unsigned int mask) const
{
    // most recent index
    unsigned int last = (deltaPointer - 1) % deltas.size();
    // second most recent index
    unsigned int last_prev = (deltaPointer - 2) % deltas.size();
    int delta_0 = deltas[last_prev];
    int delta_1 = deltas[last];

    // a delta 0 means that it overflowed, we can not match it
    if (delta_0 == 0 || delta_1 == 0) {
        return;
    }

    // Try to find the two most recent deltas in a previous position on the
    // delta circular array, if found, start issuing prefetches using the
    // remaining deltas (adding each delta to the last Addr to generate the
    // prefetched address.

    // oldest index
    int idx_0 = deltaPointer + 1;
    // second oldest index
    int idx_1 = deltaPointer + 2;
    for (int i = 0; i < deltas.size() - 2; i += 1) {
        int this_delta_0 = deltas[(idx_0 + i) % deltas.size()];
        int this_delta_1 = deltas[(idx_1 + i) % deltas.size()];
        if ((this_delta_0 >> mask) == (delta_0 >> mask) &&
            (this_delta_1 >> mask) == (delta_1 >> mask)) {
            Addr addr = lastAddress;
            // Pattern found, issue prefetches with the remaining deltas after
            // this pair
            i += 2; // skip the matching pair
            do {
                int pf_delta = deltas[(idx_0 + i) % deltas.size()];
                addr += pf_delta;
                pfs.push_back(QueuedPrefetcher::AddrPriority(addr, 0));
                i += 1;
            } while (i < deltas.size() - 2);
        }
    }
}

void
DeltaCorrelatingPredictionTables::calculatePrefetch(
    const BasePrefetcher::PrefetchInfo &pfi,
    std::vector<QueuedPrefetcher::AddrPriority> &addresses)
{
    if (!pfi.hasPC()) {
        DPRINTF(HWPrefetch, "Ignoring request with no PC.\n");
        return;
    }
    Addr address = pfi.getAddr();
    Addr pc = pfi.getPC();
    // Look up table entry, is_secure is unused in findEntry because we
    // index using the pc
    DCPTEntry *entry = table.findEntry(pc, false /* unused */);
    if (entry != nullptr) {
        entry->addAddress(address, deltaBits);
        //Delta correlating
        entry->getCandidates(addresses, deltaMaskBits);
    } else {
        entry = table.findVictim(pc);

        table.insertEntry(pc, false /* unused */, entry);

        entry->lastAddress = address;
    }
}

DeltaCorrelatingPredictionTables *
DeltaCorrelatingPredictionTablesParams::create()
{
   return new DeltaCorrelatingPredictionTables(this);
}

DCPTPrefetcher::DCPTPrefetcher(const DCPTPrefetcherParams *p)
  : QueuedPrefetcher(p), dcpt(*p->dcpt)
{
}

void
DCPTPrefetcher::calculatePrefetch(const PrefetchInfo &pfi,
    std::vector<AddrPriority> &addresses)
{
    dcpt.calculatePrefetch(pfi, addresses);
}

DCPTPrefetcher*
DCPTPrefetcherParams::create()
{
    return new DCPTPrefetcher(this);
}