[Arc] Consider array/struct field alignment when computing state size #8872
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
StateTypein the Arc dialect currently just defers tohw::getBitWidthto 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 forstruct<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
StateTypemore 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.