Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@fabianschuiki
Copy link
Contributor

The StateType in the Arc dialect currently just defers to hw::getBitWidth to compute the number of bits that need to be allocated in the simulator state. This is problematic, since types like structs and arrays have field and element alignment requirements, making LLVM require more bytes for these types than the summed-up bit width would suggest. As a result, the AllocateState pass would allocate 2 bytes for struct<tag: i5, sign_ext: i1, offset: i3, size: i2>, even though LLVM would consider the individual fields to be aligned to the nearest byte, making the struct cover 4 bytes in memory.

To fix this issue, make StateType more carefully compute the actual number of bits required in simulator storage by requiring all elements of a array or struct to be at least byte-aligned, and to more carefully track the alignmenmt of individual fields. This allows us to predict how many bytes more accurately.

This fixes a few nasty simulation mismatches between Arcilator and Verilator due to the memory corruption that would result from not allocating sufficient storage for arrays and structs.

@fabianschuiki fabianschuiki requested a review from TaoBi22 August 19, 2025 00:48
@fabianschuiki fabianschuiki added the Arc Involving the `arc` dialect label Aug 19, 2025
Copy link
Contributor

@TaoBi22 TaoBi22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!! Might be worth adding a regression test for AllocateState too?

@fabianschuiki fabianschuiki force-pushed the fschuiki/i0-llvm-consts branch from 60ecc21 to 254c7bf Compare August 19, 2025 20:58
@fabianschuiki fabianschuiki force-pushed the fschuiki/fix-arc-state-type-alignment branch from cec2d0e to 2afcf79 Compare August 19, 2025 20:58
@fabianschuiki fabianschuiki force-pushed the fschuiki/i0-llvm-consts branch from 254c7bf to cb523ee Compare August 19, 2025 21:21
@fabianschuiki fabianschuiki force-pushed the fschuiki/fix-arc-state-type-alignment branch from 2afcf79 to ea7d3b0 Compare August 19, 2025 21:21
Base automatically changed from fschuiki/i0-llvm-consts to main August 19, 2025 22:17
The `StateType` in the Arc dialect currently just defers to
`hw::getBitWidth` to compute the number of bits that need to be
allocated in the simulator state. This is problematic, since types like
structs and arrays have field and element alignment requirements, making
LLVM require more bytes for these types than the summed-up bit width
would suggest. As a result, the AllocateState pass would allocate 2
bytes for `struct<tag: i5, sign_ext: i1, offset: i3, size: i2>`, even
though LLVM would consider the individual fields to be aligned to the
nearest byte, making the struct cover 4 bytes in memory.

To fix this issue, make `StateType` more carefully compute the actual
number of bits required in simulator storage by requiring all elements
of a array or struct to be at least byte-aligned, and to more carefully
track the alignmenmt of individual fields. This allows us to predict how
many bytes more accurately.

This fixes a few nasty simulation mismatches between Arcilator and
Verilator due to the memory corruption that would result from not
allocating sufficient storage for arrays and structs.
@fabianschuiki fabianschuiki force-pushed the fschuiki/fix-arc-state-type-alignment branch from ea7d3b0 to cf148e4 Compare August 19, 2025 22:18
@fabianschuiki fabianschuiki merged commit b869f9e into main Aug 19, 2025
3 checks passed
@fabianschuiki fabianschuiki deleted the fschuiki/fix-arc-state-type-alignment branch August 19, 2025 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arc Involving the `arc` dialect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants