Skip to content

Commit

Permalink
Properly treat FuelVMInstructions in the memcpyopt (#6650)
Browse files Browse the repository at this point in the history
## Description

This PR fixes the issue of `FuelVMInstruction`s not being considered in
the `memcpyopt` as storing into the destination. E.g., in this example:
```
script {
    entry fn main() -> u256 {
        local u256 x
        local u256 y
        local u256 z

        entry():
        v0 = get_local ptr u256, x
        v1 = get_local ptr u256, y
        v2 = get_local ptr u256, z

        mem_copy_val v2, v0
        wide add v0, v1 to v2
        v3 = load v2

        ret u256 v3
    }
}
```
after the `memcpyopt` the `v3 = load v2` got turned into `v3 = load v0`
because the `v2` is considered as not being written to.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
ironcev authored Oct 16, 2024
1 parent e216061 commit b7dde28
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
27 changes: 24 additions & 3 deletions sway-ir/src/optimize/memcpyopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use sway_types::{FxIndexMap, FxIndexSet};

use crate::{
get_gep_symbol, get_referred_symbol, get_referred_symbols, get_stored_symbols, memory_utils,
AnalysisResults, Block, Context, EscapedSymbols, Function, InstOp, Instruction,
InstructionInserter, IrError, LocalVar, Pass, PassMutability, ReferredSymbols, ScopedPass,
Symbol, Type, Value, ValueDatum, ESCAPED_SYMBOLS_NAME,
AnalysisResults, Block, Context, EscapedSymbols, FuelVmInstruction, Function, InstOp,
Instruction, InstructionInserter, IrError, LocalVar, Pass, PassMutability, ReferredSymbols,
ScopedPass, Symbol, Type, Value, ValueDatum, ESCAPED_SYMBOLS_NAME,
};

pub const MEMCPYOPT_NAME: &str = "memcpyopt";
Expand Down Expand Up @@ -649,6 +649,27 @@ fn local_copy_prop(
&mut dest_to_copies,
);
}
Instruction {
op:
InstOp::FuelVm(
FuelVmInstruction::WideBinaryOp { result, .. }
| FuelVmInstruction::WideUnaryOp { result, .. }
| FuelVmInstruction::WideModularOp { result, .. }
| FuelVmInstruction::StateLoadQuadWord {
load_val: result, ..
},
),
..
} => {
kill_defined_symbol(
context,
*result,
memory_utils::pointee_size(context, *result),
&mut available_copies,
&mut src_to_copies,
&mut dest_to_copies,
);
}
_ => (),
}
}
Expand Down
20 changes: 20 additions & 0 deletions sway-ir/tests/memcpyopt/no_memcpyopt_wide_binary_operator.ir
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
script {
entry fn main() -> u256 {
local u256 x
local u256 y
local u256 z

entry():
v0 = get_local ptr u256, x
v1 = get_local ptr u256, y
v2 = get_local ptr u256, z

mem_copy_val v2, v0
wide add v0, v1 to v2
v3 = load v2

ret u256 v3
}
}

// check: v3 = load v2
20 changes: 20 additions & 0 deletions sway-ir/tests/memcpyopt/no_memcpyopt_wide_unary_operator.ir
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
script {
entry fn main() -> u256 {
local u256 x
local u256 y
local u256 z

entry():
v0 = get_local ptr u256, x
v1 = get_local ptr u256, y
v2 = get_local ptr u256, z

mem_copy_val v2, v0
wide not v1 to v2
v3 = load v2

ret u256 v3
}
}

// check: v3 = load v2

0 comments on commit b7dde28

Please sign in to comment.