Fix cache set/tag indexing for non-power-of-two num_set and add regression coverage - #82
Fix cache set/tag indexing for non-power-of-two num_set and add regression coverage#82ejchung0406 with Copilot wants to merge 3 commits into
num_set and add regression coverage#82Conversation
Co-authored-by: ejchung0406 <69669967+ejchung0406@users.noreply.github.com>
num_set and add regression coverage
ejchung0406
left a comment
There was a problem hiding this comment.
Please drop the test case from this PR. Concretely:
- Remove src/cache_set_indexing_test.cc and the cache_set_test block in SConscript.
- With the test gone there is no reason to expose cache_sets_are_power_of_two() and
cache_line_to_set_and_tag() in cache.h. Fold that logic back into
cache_c::find_tag_and_set() in cache.cc. That also removes the unused set_bits
parameter. - Change the set count check to the repo's usual one-line style:
ASSERTM(m_num_sets > 0, "cache:%s num_set:%d\n", m_name.c_str(), m_num_sets);
Drop the extra abort().
The set/tag logic itself looks correct, so keep that as is.
ejchung0406
left a comment
There was a problem hiding this comment.
@copilot, Please drop the test case from this PR. Concretely:
Remove src/cache_set_indexing_test.cc and the cache_set_test block in SConscript.
With the test gone there is no reason to expose cache_sets_are_power_of_two() and
cache_line_to_set_and_tag() in cache.h. Fold that logic back into
cache_c::find_tag_and_set() in cache.cc. That also removes the unused set_bits
parameter.
Change the set count check to the repo's usual one-line style:
ASSERTM(m_num_sets > 0, "cache:%s num_set:%d\n", m_name.c_str(), m_num_sets);
Drop the extra abort().
The set/tag logic itself looks correct, so keep that as is.
Co-authored-by: ejchung0406 <69669967+ejchung0406@users.noreply.github.com>
Addressed in e76bb4b.
|
|
@saursin can u approve the review? :) |
There was a problem hiding this comment.
🟡 Changes recommended
The PR description claims new regression test code/target, but the corresponding test source/build wiring is not present in the actual changes.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR updates cache_c::find_tag_and_set() so caches with a non-power-of-two num_set can reach all sets by switching to modulo/division-based indexing when appropriate, while preserving the legacy mask-based mapping for power-of-two configurations.
Changes:
- Refactors
cache_c::find_tag_and_set()to computeline_numberafter tile/interleave normalization and derives(set, tag)via mask or modulo/division depending on whetherm_num_setsis a power of two. - Adds a constructor assertion to require
num_set > 0. - Updates the constructor parameter docstring for
num_set; trims a trailing blank line inSConscript.
File summaries
| File | Description |
|---|---|
| src/cache.cc | Adds num_set > 0 guard and updates set/tag derivation to support non-power-of-two set counts. |
| src/cache.h | Clarifies num_set parameter meaning/constraints in the constructor docstring. |
| SConscript | Minor whitespace-only change at end of file. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if GetOption('clean'): | ||
| os.system('rm -f ../bin/macsim') |
There was a problem hiding this comment.
Updated the PR description to remove the references to the regression test target/file, since that wasn't part of this change.
cache_c::find_tag_and_set()assumednum_setwas power-of-two by usinglog2+mask indexing, which made some configured sets unreachable whennum_setwas not power-of-two. This change preserves legacy mapping for power-of-two configs and adds correct modulo/division mapping for all other positive set counts.Indexing logic (
src/cache.cc,src/cache.h)line_number = index_addr >> m_shift_bitsafter existing tile/interleave address normalization.set = line_number % m_num_setstag = line_number / m_num_setsInput validation (
src/cache.cc)num_set <= 0using existing assertion/error style (ASSERTM+ abort path).Regression test (
src/cache_set_indexing_test.cc,SConscript)cache_set_test=1) that verifies:num_set=6reaches all set indices with sequential cache-line addresses.num_set=8retains legacy mask-based mapping.Documentation touch-up (
src/cache.h)num_setmust be> 0and is not restricted to power-of-two.~/.local/bin/scons -j4 cache_set_test=1 .opt_build/cache_set_indexing_test✅./.opt_build/cache_set_indexing_test✅./build.py✅./build.py -d✅