mem-cache: Set compression bit with its size
authorDaniel R. Carvalho <odanrc@yahoo.com.br>
Thu, 22 Aug 2019 09:50:10 +0000 (11:50 +0200)
committerDaniel Carvalho <odanrc@yahoo.com.br>
Thu, 12 Nov 2020 21:46:43 +0000 (21:46 +0000)
When setting the size of a compressed block, its compressibility
needs to be recalculated based on that, so move such functionality
to be done after the block has been inserted, within setSizeBits.

As a side effect, insertBlock does not need to be overridden
anymore.

Change-Id: I608f876cd2110ac5e394ffad5b29941ba458ba91
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/36580
Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Maintainer: Nikos Nikoleris <nikos.nikoleris@arm.com>
Tested-by: kokoro <noreply+kokoro@google.com>
src/mem/cache/base.cc
src/mem/cache/tags/compressed_tags.cc
src/mem/cache/tags/compressed_tags.hh
src/mem/cache/tags/super_blk.cc

index 09c810a7e71aa466391eca20a7075bd26f677b31..98467ab98cc34d38801511af53d2a91562506b36 100644 (file)
@@ -853,12 +853,6 @@ BaseCache::updateCompressionData(CacheBlk *&blk, const uint64_t* data,
     CompressionBlk* compression_blk = static_cast<CompressionBlk*>(blk);
     M5_VAR_USED const std::size_t prev_size = compression_blk->getSizeBits();
 
-    // Check if new data is co-allocatable
-    const SuperBlk* superblock =
-        static_cast<const SuperBlk*>(compression_blk->getSectorBlock());
-    const bool is_co_allocatable = superblock->isCompressed(compression_blk) &&
-        superblock->canCoAllocate(compression_size);
-
     // If compressed size didn't change enough to modify its co-allocatability
     // there is nothing to do. Otherwise we may be facing a data expansion
     // (block passing from more compressed to less compressed state), or a
@@ -909,7 +903,7 @@ BaseCache::updateCompressionData(CacheBlk *&blk, const uint64_t* data,
         } else {
             // If we do not move the expanded block, we must make room for
             // the expansion to happen, so evict every co-allocated block
-            superblock = static_cast<const SuperBlk*>(
+            const SuperBlk* superblock = static_cast<const SuperBlk*>(
                 compression_blk->getSectorBlock());
             for (auto& sub_blk : superblock->blks) {
                 if (sub_blk->isValid() && (blk != sub_blk)) {
@@ -945,17 +939,6 @@ BaseCache::updateCompressionData(CacheBlk *&blk, const uint64_t* data,
     compression_blk->setSizeBits(compression_size);
     compression_blk->setDecompressionLatency(decompression_lat);
 
-    if (is_data_expansion || is_data_contraction) {
-        // If contracting data, for sure data is compressed. If expanding,
-        // both situations can arise. When no contraction or expansion happens
-        // block keeps its old state
-        if (is_co_allocatable) {
-            compression_blk->setCompressed();
-        } else {
-            compression_blk->setUncompressed();
-        }
-    }
-
     return true;
 }
 
@@ -1503,16 +1486,16 @@ BaseCache::allocateBlock(const PacketPtr pkt, PacketList &writebacks)
         return nullptr;
     }
 
-    // If using a compressor, set compression data. This must be done before
-    // block insertion, as compressed tags use this information.
+    // Insert new block at victimized entry
+    tags->insertBlock(pkt, victim);
+
+    // If using a compressor, set compression data. This must be done after
+    // insertion, as the compression bit may be set.
     if (compressor) {
         compressor->setSizeBits(victim, blk_size_bits);
         compressor->setDecompressionLatency(victim, decompression_lat);
     }
 
-    // Insert new block at victimized entry
-    tags->insertBlock(pkt, victim);
-
     return victim;
 }
 
index 780c738f968c3470a1214ad5bef20e9e821c8a58..f71dedacd730cbf6734aecff41530878bf5cbd25 100644 (file)
@@ -164,28 +164,6 @@ CompressedTags::findVictim(Addr addr, const bool is_secure,
     return victim;
 }
 
-void
-CompressedTags::insertBlock(const PacketPtr pkt, CacheBlk *blk)
-{
-    // We check if block can co-allocate before inserting, because this check
-    // assumes the block is still invalid
-    CompressionBlk* compression_blk = static_cast<CompressionBlk*>(blk);
-    const SuperBlk* superblock = static_cast<const SuperBlk*>(
-        compression_blk->getSectorBlock());
-    const bool is_co_allocatable = superblock->isCompressed() &&
-        superblock->canCoAllocate(compression_blk->getSizeBits());
-
-    // Insert block
-    SectorTags::insertBlock(pkt, blk);
-
-    // We always store compressed blocks when possible
-    if (is_co_allocatable) {
-        compression_blk->setCompressed();
-    } else {
-        compression_blk->setUncompressed();
-    }
-}
-
 void
 CompressedTags::forEachBlk(std::function<void(CacheBlk &)> visitor)
 {
index 73231f2f5665499f049f7acba91954121282680a..306acd5b756344de8c0f5c2772391b762cfe7603 100644 (file)
@@ -109,14 +109,6 @@ class CompressedTags : public SectorTags
                          const std::size_t compressed_size,
                          std::vector<CacheBlk*>& evict_blks) override;
 
-    /**
-     * Insert the new block into the cache and update replacement data.
-     *
-     * @param pkt Packet holding the address to update
-     * @param blk The block to update.
-     */
-    void insertBlock(const PacketPtr pkt, CacheBlk *blk) override;
-
     /**
      * Visit each sub-block in the tags and apply a visitor.
      *
index 9f7ce6552b827057194e48b7ac038b1577cb81c2..09bb2073f9118a4286ce0a3e3312b5158c68e1ba 100644 (file)
@@ -93,6 +93,27 @@ void
 CompressionBlk::setSizeBits(const std::size_t size)
 {
     _size = size;
+
+    SuperBlk* superblock = static_cast<SuperBlk*>(getSectorBlock());
+
+    // Either this function is called after an insertion, or an update.
+    // If somebody else is present in the block, keep the superblock's
+    // compressibility. Otherwise, check if it can co-allocate
+    const uint8_t num_valid = superblock->getNumValid();
+    assert(num_valid >= 1);
+    if (num_valid == 1) {
+        if (superblock->canCoAllocate(size)) {
+            setCompressed();
+        } else {
+            setUncompressed();
+        }
+    } else {
+        if (superblock->isCompressed(this)) {
+            setCompressed();
+        } else {
+            setUncompressed();
+        }
+    }
 }
 
 Cycles