mem-cache: Added the Slim AMPM Prefetcher

Reference:
    Towards Bandwidth-Efficient Prefetching with Slim AMPM.
    Young, V., & Krishna, A. (2015). The 2nd Data Prefetching Championship.

Slim AMPM is composed of two prefetchers, the DPCT and the AMPM (both already
in gem5).

Change-Id: I6e868faf216e3e75231cf181d59884ed6f0d382a
Reviewed-on: https://gem5-review.googlesource.com/c/16383
Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Maintainer: Nikos Nikoleris <nikos.nikoleris@arm.com>
diff --git a/src/mem/cache/prefetch/Prefetcher.py b/src/mem/cache/prefetch/Prefetcher.py
index 2c31de9..36e2541 100644
--- a/src/mem/cache/prefetch/Prefetcher.py
+++ b/src/mem/cache/prefetch/Prefetcher.py
@@ -204,11 +204,16 @@
     global_history_register_replacement_policy = Param.BaseReplacementPolicy(
         LRURP(), "Replacement policy of the global history register")
 
-class AccessMapPatternMatchingPrefetcher(QueuedPrefetcher):
-    type = 'AccessMapPatternMatchingPrefetcher'
-    cxx_class = 'AccessMapPatternMatchingPrefetcher'
+class AccessMapPatternMatching(ClockedObject):
+    type = 'AccessMapPatternMatching'
+    cxx_class = 'AccessMapPatternMatching'
     cxx_header = "mem/cache/prefetch/access_map_pattern_matching.hh"
 
+    block_size = Param.Unsigned(Parent.block_size,
+        "Cacheline size used by the prefetcher using this object")
+
+    limit_stride = Param.Unsigned(0,
+        "Limit the strides checked up to -X/X, if 0, disable the limit")
     start_degree = Param.Unsigned(4,
         "Initial degree (Maximum number of prefetches generated")
     hot_zone_size = Param.MemorySize("2kB", "Memory covered by a hot zone")
@@ -238,6 +243,13 @@
     offchip_memory_latency = Param.Latency("30ns",
         "Memory latency used to compute the required memory bandwidth")
 
+class AMPMPrefetcher(QueuedPrefetcher):
+    type = 'AMPMPrefetcher'
+    cxx_class = 'AMPMPrefetcher'
+    cxx_header = "mem/cache/prefetch/access_map_pattern_matching.hh"
+    ampm = Param.AccessMapPatternMatching( AccessMapPatternMatching(),
+        "Access Map Pattern Matching object")
+
 class DeltaCorrelatingPredictionTables(SimObject):
     type = 'DeltaCorrelatingPredictionTables'
     cxx_class = 'DeltaCorrelatingPredictionTables'
@@ -309,3 +321,23 @@
     sp_address_map_cache_replacement_policy = Param.BaseReplacementPolicy(
         LRURP(),
         "Replacement policy of the Structural-to-Physical Address Map Cache")
+
+class SlimAccessMapPatternMatching(AccessMapPatternMatching):
+    start_degree = 2
+    limit_stride = 4
+
+class SlimDeltaCorrelatingPredictionTables(DeltaCorrelatingPredictionTables):
+    table_entries = "256"
+    table_assoc = 256
+    deltas_per_entry = 9
+
+class SlimAMPMPrefetcher(QueuedPrefetcher):
+    type = 'SlimAMPMPrefetcher'
+    cxx_class = 'SlimAMPMPrefetcher'
+    cxx_header = "mem/cache/prefetch/slim_ampm.hh"
+
+    ampm = Param.AccessMapPatternMatching(SlimAccessMapPatternMatching(),
+        "Access Map Pattern Matching object")
+    dcpt = Param.DeltaCorrelatingPredictionTables(
+        SlimDeltaCorrelatingPredictionTables(),
+        "Delta Correlating Prediction Tables object")
diff --git a/src/mem/cache/prefetch/SConscript b/src/mem/cache/prefetch/SConscript
index 0a209ff..48ee218 100644
--- a/src/mem/cache/prefetch/SConscript
+++ b/src/mem/cache/prefetch/SConscript
@@ -39,5 +39,6 @@
 Source('queued.cc')
 Source('signature_path.cc')
 Source('signature_path_v2.cc')
+Source('slim_ampm.cc')
 Source('stride.cc')
 Source('tagged.cc')
diff --git a/src/mem/cache/prefetch/access_map_pattern_matching.cc b/src/mem/cache/prefetch/access_map_pattern_matching.cc
index 0f46eff..df2a9f7 100644
--- a/src/mem/cache/prefetch/access_map_pattern_matching.cc
+++ b/src/mem/cache/prefetch/access_map_pattern_matching.cc
@@ -32,11 +32,12 @@
 
 #include "debug/HWPrefetch.hh"
 #include "mem/cache/prefetch/associative_set_impl.hh"
-#include "params/AccessMapPatternMatchingPrefetcher.hh"
+#include "params/AMPMPrefetcher.hh"
+#include "params/AccessMapPatternMatching.hh"
 
-AccessMapPatternMatchingPrefetcher::AccessMapPatternMatchingPrefetcher(
-    const AccessMapPatternMatchingPrefetcherParams *p)
-    : QueuedPrefetcher(p),
+AccessMapPatternMatching::AccessMapPatternMatching(
+    const AccessMapPatternMatchingParams *p)
+    : ClockedObject(p), blkSize(p->block_size), limitStride(p->limit_stride),
       startDegree(p->start_degree), hotZoneSize(p->hot_zone_size),
       highCoverageThreshold(p->high_coverage_threshold),
       lowCoverageThreshold(p->low_coverage_threshold),
@@ -62,7 +63,7 @@
 }
 
 void
-AccessMapPatternMatchingPrefetcher::processEpochEvent()
+AccessMapPatternMatching::processEpochEvent()
 {
     schedule(epochEvent, clockEdge(epochCycles));
     double prefetch_accuracy =
@@ -95,8 +96,8 @@
     numRawCacheHits = 0.0;
 }
 
-AccessMapPatternMatchingPrefetcher::AccessMapEntry *
-AccessMapPatternMatchingPrefetcher::getAccessMapEntry(Addr am_addr,
+AccessMapPatternMatching::AccessMapEntry *
+AccessMapPatternMatching::getAccessMapEntry(Addr am_addr,
                 bool is_secure)
 {
     AccessMapEntry *am_entry = accessMapTable.findEntry(am_addr, is_secure);
@@ -112,7 +113,7 @@
 }
 
 void
-AccessMapPatternMatchingPrefetcher::setEntryState(AccessMapEntry &entry,
+AccessMapPatternMatching::setEntryState(AccessMapEntry &entry,
     Addr block, enum AccessMapState state)
 {
     enum AccessMapState old = entry.states[block];
@@ -147,8 +148,9 @@
 }
 
 void
-AccessMapPatternMatchingPrefetcher::calculatePrefetch(const PrefetchInfo &pfi,
-    std::vector<AddrPriority> &addresses)
+AccessMapPatternMatching::calculatePrefetch(
+    const BasePrefetcher::PrefetchInfo &pfi,
+    std::vector<QueuedPrefetcher::AddrPriority> &addresses)
 {
     assert(addresses.empty());
     bool is_secure = pfi.isSecure();
@@ -194,7 +196,8 @@
     // index of the current_block in the new vector
     Addr states_current_block = current_block + lines_per_zone;
     // consider strides 1..lines_per_zone/2
-    for (int stride = 1; stride < lines_per_zone/2; stride += 1) {
+    int max_stride = limitStride == 0 ? lines_per_zone / 2 : limitStride + 1;
+    for (int stride = 1; stride < max_stride; stride += 1) {
         // Test accessed positive strides
         if (checkCandidate(states, states_current_block, stride)) {
             // candidate found, current_block - stride
@@ -213,7 +216,7 @@
                 pf_addr = am_addr * hotZoneSize + blk * blkSize;
                 setEntryState(*am_entry_curr, blk, AM_PREFETCH);
             }
-            addresses.push_back(AddrPriority(pf_addr, 0));
+            addresses.push_back(QueuedPrefetcher::AddrPriority(pf_addr, 0));
             if (addresses.size() == degree) {
                 break;
             }
@@ -237,7 +240,7 @@
                 pf_addr = am_addr * hotZoneSize + blk * blkSize;
                 setEntryState(*am_entry_curr, blk, AM_PREFETCH);
             }
-            addresses.push_back(AddrPriority(pf_addr, 0));
+            addresses.push_back(QueuedPrefetcher::AddrPriority(pf_addr, 0));
             if (addresses.size() == degree) {
                 break;
             }
@@ -245,8 +248,26 @@
     }
 }
 
-AccessMapPatternMatchingPrefetcher*
-AccessMapPatternMatchingPrefetcherParams::create()
+AccessMapPatternMatching*
+AccessMapPatternMatchingParams::create()
 {
-    return new AccessMapPatternMatchingPrefetcher(this);
+    return new AccessMapPatternMatching(this);
+}
+
+AMPMPrefetcher::AMPMPrefetcher(const AMPMPrefetcherParams *p)
+  : QueuedPrefetcher(p), ampm(*p->ampm)
+{
+}
+
+void
+AMPMPrefetcher::calculatePrefetch(const PrefetchInfo &pfi,
+    std::vector<AddrPriority> &addresses)
+{
+    ampm.calculatePrefetch(pfi, addresses);
+}
+
+AMPMPrefetcher*
+AMPMPrefetcherParams::create()
+{
+    return new AMPMPrefetcher(this);
 }
diff --git a/src/mem/cache/prefetch/access_map_pattern_matching.hh b/src/mem/cache/prefetch/access_map_pattern_matching.hh
index 33a20a2..5561cf5 100644
--- a/src/mem/cache/prefetch/access_map_pattern_matching.hh
+++ b/src/mem/cache/prefetch/access_map_pattern_matching.hh
@@ -44,11 +44,16 @@
 #include "mem/cache/prefetch/associative_set.hh"
 #include "mem/cache/prefetch/queued.hh"
 #include "mem/packet.hh"
+#include "sim/clocked_object.hh"
 
-struct AccessMapPatternMatchingPrefetcherParams;
+struct AccessMapPatternMatchingParams;
 
-class AccessMapPatternMatchingPrefetcher : public QueuedPrefetcher
+class AccessMapPatternMatching : public ClockedObject
 {
+    /** Cacheline size used by the prefetcher using this object */
+    const unsigned blkSize;
+    /** Limit the stride checking to -limitStride/+limitStride */
+    const unsigned limitStride;
     /** Maximum number of prefetch generated */
     const unsigned startDegree;
     /** Amount of memory covered by a hot zone */
@@ -173,9 +178,22 @@
     EventFunctionWrapper epochEvent;
 
   public:
-    AccessMapPatternMatchingPrefetcher(
-        const AccessMapPatternMatchingPrefetcherParams* p);
-    ~AccessMapPatternMatchingPrefetcher() {}
+    AccessMapPatternMatching(const AccessMapPatternMatchingParams* p);
+    ~AccessMapPatternMatching()
+    {}
+    void calculatePrefetch(const BasePrefetcher::PrefetchInfo &pfi,
+        std::vector<QueuedPrefetcher::AddrPriority> &addresses);
+};
+
+struct AMPMPrefetcherParams;
+
+class AMPMPrefetcher : public QueuedPrefetcher
+{
+    AccessMapPatternMatching &ampm;
+  public:
+    AMPMPrefetcher(const AMPMPrefetcherParams* p);
+    ~AMPMPrefetcher()
+    {}
     void calculatePrefetch(const PrefetchInfo &pfi,
                            std::vector<AddrPriority> &addresses) override;
 };
diff --git a/src/mem/cache/prefetch/slim_ampm.cc b/src/mem/cache/prefetch/slim_ampm.cc
new file mode 100644
index 0000000..432d00f
--- /dev/null
+++ b/src/mem/cache/prefetch/slim_ampm.cc
@@ -0,0 +1,54 @@
+/**
+ * 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/slim_ampm.hh"
+
+#include "params/SlimAMPMPrefetcher.hh"
+
+SlimAMPMPrefetcher::SlimAMPMPrefetcher(const SlimAMPMPrefetcherParams* p)
+  : QueuedPrefetcher(p), ampm(*p->ampm), dcpt(*p->dcpt)
+{
+}
+
+void
+SlimAMPMPrefetcher::calculatePrefetch(const PrefetchInfo &pfi,
+                  std::vector<AddrPriority> &addresses)
+{
+    dcpt.calculatePrefetch(pfi, addresses);
+    if (addresses.empty()) {
+        ampm.calculatePrefetch(pfi, addresses);
+    }
+}
+
+SlimAMPMPrefetcher*
+SlimAMPMPrefetcherParams::create()
+{
+    return new SlimAMPMPrefetcher(this);
+}
diff --git a/src/mem/cache/prefetch/slim_ampm.hh b/src/mem/cache/prefetch/slim_ampm.hh
new file mode 100644
index 0000000..310a165
--- /dev/null
+++ b/src/mem/cache/prefetch/slim_ampm.hh
@@ -0,0 +1,64 @@
+/**
+ * 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
+ */
+
+#ifndef __MEM_CACHE_PREFETCH_SLIM_AMPM_HH__
+#define __MEM_CACHE_PREFETCH_SLIM_AMPM_HH__
+
+#include "mem/cache/prefetch/access_map_pattern_matching.hh"
+#include "mem/cache/prefetch/delta_correlating_prediction_tables.hh"
+#include "mem/cache/prefetch/queued.hh"
+
+/**
+ * The SlimAMPM Prefetcher
+ * Reference:
+ *    Towards Bandwidth-Efficient Prefetching with Slim AMPM.
+ *    Young, Vinson, and A. Krishna.
+ *    The 2nd Data Prefetching Championship (2015).
+ *
+ * This prefetcher uses two other prefetchers, the AMPM and the DCPT.
+ */
+
+struct SlimAMPMPrefetcherParams;
+
+class SlimAMPMPrefetcher : public QueuedPrefetcher
+{
+   /** AMPM prefetcher object */
+   AccessMapPatternMatching &ampm;
+   /** DCPT prefetcher object */
+   DeltaCorrelatingPredictionTables &dcpt;
+ public:
+   SlimAMPMPrefetcher(const SlimAMPMPrefetcherParams *p);
+   ~SlimAMPMPrefetcher()
+   {}
+
+   void calculatePrefetch(const PrefetchInfo &pfi,
+                          std::vector<AddrPriority> &addresses) override;
+};
+#endif//__MEM_CACHE_PREFETCH_SLIM_AMPM_HH__