You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Eliminate mem_copy_vals when accessing global constants (#7348)
## Description
This PR properly treats `InstOp::GetGlobal` in the
`memory_utils::get_symbols`. Before, when analysis hit `GetGlobal` as a
pointer source, it didn't know that there cannot be a symbol behind that
pointer. That's why it was treated the resulting `ReferredSymbols` as
incomplete. This harmed `memcpyopt` optimizations.
E.g., in this example:
```sway
script;
const ADDRESS: b256 = 0x9999999999999999999999999999999999999999999999999999999999999999;
fn main() {
poke(ADDRESS);
}
#[inline(never)]
fn poke<T>(_t: T) { }
```
the resulting final IR in `release` build was:
```
entry_orig fn main_0() -> (), !13 {
local mut b256 __aggr_memcpy_0 <<<--- Additional local.
local b256 __tmp_arg
entry():
v0 = get_global __ptr b256, storage_all_layouts::ADDRESS, !14
v1 = get_local __ptr b256, __aggr_memcpy_0
mem_copy_val v1, v0 <<<--- Additional `mem_cpy_val`.
v2 = get_local __ptr b256, __tmp_arg
mem_copy_val v2, v1
v3 = call poke_1(v2)
v12 = const unit ()
ret () v12
}
```
After this change, the resulting IR becomes:
```
entry_orig fn main_0() -> (), !13 {
local b256 __tmp_arg
entry():
v0 = get_global __ptr b256, storage_all_layouts::ADDRESS, !14
v1 = get_local __ptr b256, __tmp_arg
mem_copy_val v1, v0
v3 = call poke_1(v1)
v12 = const unit ()
ret () v12
}
```
The change didn't affect the bytecode or gas usage of most of the
`should_pass` tests.
Only the following `should_pass/language` snapshot tests decreased in
bytecode size and gas usage:
| Test | Bytecode before | Bytecode after |
| ---- | --------------- | -------------- |
| const_generics | 9.560 | 9.536 |
| panic_expression/panicking_contract | 8.136 | 8.048 |
| panic_expression/panic_handling_in_unit_tests | 9.552 | 9.392 |
The only test in which the bytecode size and gas usage went up was
`should_pass/language/intrinsics/dbg` test run in `debug` mode. When run
in `release` mode, the usage of `_dbg` intrinsic didn't cause any
increases (the `dbg_release` test).
## Checklist
- [ ] I have linked to any relevant issues.
- [x] 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.
---------
Co-authored-by: Vaivaswatha N <vaivaswatha.nagaraj@fuel.sh>
Copy file name to clipboardExpand all lines: test/src/e2e_vm_tests/test_programs/should_pass/language/panic_expression/panic_handling_in_unit_tests/stdout.snap
Copy file name to clipboardExpand all lines: test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ use basic_storage_abi::{BasicStorage, Quad};
0 commit comments