Skip to content
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: block mload merging when src and dst overlap #3635

Merged
merged 11 commits into from
Oct 3, 2023

Conversation

charles-cooper
Copy link
Member

@charles-cooper charles-cooper commented Oct 1, 2023

What I did

fix an optimization bug introduced in #3483 when the target architecture is cancun. prior to this PR, certain sequences of mload/mstore could incorrectly be merged into mcopy when there is overlap between the source and destination buffers.

How I did it

How to verify it

Commit message

this commit fixes an optimization bug when the target architecture has
the `mcopy` instruction (i.e. `cancun` or later). the bug was introduced
in 5dc3ac7. specifically, the `merge_mload` step can incorrectly merge
`mload`/`mstore` sequences (into `mcopy`) when the source and
destination buffers overlap, and the destination buffer is "ahead of"
(i.e. greater than) the source buffer. this commit fixes the issue by
blocking the optimization in these cases, and adds unit and functional
tests demonstrating the correct behavior.

Description for the changelog

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->



def _merge_load(argz, _LOAD, _COPY):
def _merge_load(argz, _LOAD, _COPY, allow_overlap=True):
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: think generally params should be "safe-by-default", and the "safer" option would be to have no overlap.

Copy link
Member Author

@charles-cooper charles-cooper Oct 1, 2023

Choose a reason for hiding this comment

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

hmm i see, my thinking was that this only affects where the source and destination address spaces are the same, and there is only one case for this, mload / mstore. but i am open to changing the default.

@@ -689,6 +689,11 @@ def _merge_load(argz, _LOAD, _COPY):
initial_dst_offset = dst_offset
initial_src_offset = src_offset
idx = i

if not allow_overlap and initial_dst_offset <= initial_src_offset <= dst_offset:
Copy link
Contributor

Choose a reason for hiding this comment

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

do you have to do do a + 32 somewhere here?

Copy link
Member Author

Choose a reason for hiding this comment

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

i think the <= handles that, see the output of this "partial" overlap:

@external
def partial() -> uint256:
    a: uint256 = 1337
    b: uint256 = 0
    c: uint256 = 0
    d: uint256 = 0
    e: uint256 = 0

    c = a
    d = b
    e = c

    return e

@codecov-commenter
Copy link

codecov-commenter commented Oct 1, 2023

Codecov Report

Merging #3635 (d996a1e) into master (c913b2d) will increase coverage by 0.09%.
Report is 1 commits behind head on master.
The diff coverage is 100.00%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@            Coverage Diff             @@
##           master    #3635      +/-   ##
==========================================
+ Coverage   89.01%   89.10%   +0.09%     
==========================================
  Files          86       86              
  Lines       11460    11463       +3     
  Branches     2606     2606              
==========================================
+ Hits        10201    10214      +13     
+ Misses        836      830       -6     
+ Partials      423      419       -4     
Files Coverage Δ
vyper/ir/optimizer.py 98.31% <100.00%> (+<0.01%) ⬆️

... and 4 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@charles-cooper charles-cooper marked this pull request as ready for review October 2, 2023 01:32
Copy link
Contributor

@chen-robert chen-robert left a comment

Choose a reason for hiding this comment

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

lgtm

@charles-cooper charles-cooper enabled auto-merge (squash) October 3, 2023 22:49
@charles-cooper charles-cooper enabled auto-merge (squash) October 3, 2023 22:49
@charles-cooper charles-cooper changed the title fix: block mload merging when src and dst overlap fix: block mload merging when src and dst overlap Oct 3, 2023
@charles-cooper charles-cooper enabled auto-merge (squash) October 3, 2023 22:58
@charles-cooper charles-cooper merged commit e9c16e4 into vyperlang:master Oct 3, 2023
82 checks passed
@charles-cooper charles-cooper deleted the fix/merge_mload branch October 4, 2023 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants