-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix fn dedup miscompilation leading to MemoryOverflow
on Vec
s
#6195
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xunilrj
force-pushed
the
xunilrj/fix-vec-memoverflow
branch
from
June 27, 2024 12:20
2660a01
to
fa75266
Compare
Benchmark for 8f8f743Click to view benchmark
|
Benchmark for 53fa851Click to view benchmark
|
xunilrj
changed the title
Xunilrj/fix vec memoverflow
Fix fn dedup miscompilation leading to Jun 27, 2024
MemoryOverflow
on Vec
s
tritao
approved these changes
Jun 27, 2024
IGI-111
approved these changes
Jun 27, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch!
Benchmark for 559e04bClick to view benchmark
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR fixes a miscompilation bug caused by our dedup cache just before IR generation, we started to notice this bug with some
MemoryOverflow
reverts onVec
tests using 0.61.These reverts happen because
Vec::with_capacity
allocates 16 bytes per item, but later oneVec::push
pushes 24 bytes per item; causing some writes outside the allocated buffer.This is happening because, before we start IR generation, we try to coalesce identical functions using the following hash key: https://github.com/FuelLabs/sway/blob/master/sway-core/src/ir_generation.rs#L75
The example below shows that two different bodies end up colliding at the same key.
The difference here is that in one case
RawVec
is inferred to beRawVec<Vec<[u64; 2]>>
and in the otherRawVec<Vec<SomeEnum<u32>>
. The crux of the issue is that this difference does not appear in the key.The question is why this broke
with_capacity
and notpush
. And the answer in thatpush
has the self and the generic parameters to differentiate them, whilstwith_capacity
does not.Funnily enough, we also have the inverse problem. Sometimes functions with the same body do not fall at the same key, because their
TypeId
are different, andHashWithEngines
actually look atTypeInfo
variants. This is probably increasing code size and/or throwing more stuff at the optimizer.My solution for this problem is to use the body hash as the cache key. We can try to be less risky and include the body hash and keep the other items.
Below there are two graphs showing that compilation time and binary size are not affected too much by this change.
Compilation Time
Binary Size
Checklist
Breaking*
orNew Feature
labels where relevant.