Skip to content

Improve asm const propagation#7550

Open
xunilrj wants to merge 21 commits intomasterfrom
xunilrj/asm-const-prop-improv
Open

Improve asm const propagation#7550
xunilrj wants to merge 21 commits intomasterfrom
xunilrj/asm-const-prop-improv

Conversation

@xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Feb 9, 2026

Description

This PR optimises ASM const propagation. That means that it tries to use as few registers as possible, hoping that their initialisation can be removed later with DCE and others...

This is particularly useful on function calls, and indexing:

1 - On function calls we normally have something like

movi $r0 i0
move $$arg0 $r0
jal ...

We now replace this with:

movi $r0 i0
movi $$arg0 i0
jal ...

and let DCE kill the movi $r0 i0 because is no longer neeed;

2 - Constant indexing like something[0] normally generates addi and mul, now these will be coalesced into a simple movi

Gain on testing is huge because we tend to have tons of constants on tests... on real code like O2, we see more humble gains.

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).
  • 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.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@xunilrj xunilrj changed the title Xunilrj/asm const prop improv Replave move by movi when possible Feb 9, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 9, 2026

Merging this PR will not alter performance

✅ 25 untouched benchmarks


Comparing xunilrj/asm-const-prop-improv (f81f077) with master (0d8fbc8)

Open in CodSpeed

@xunilrj
Copy link
Contributor Author

xunilrj commented Feb 9, 2026

Comparing 'e2e-gas-usages':

Improvements Regressions
Count 134 1
Average 3.03% -0.08%
Median 0.77% -0.08%
Max 9.96% -0.08%
Min 0.01% -0.08%

| should_pass/stdlib/u128_log_test (test.toml) | 11100 | 11109 | -0.08% |

Comparing 'e2e-bytecode-sizes':

Improvements Regressions
Count 123 1
Average 3.61% -0.18%
Median 2.04% -0.18%
Max 9.09% -0.18%
Min 0.01% -0.18%

| should_pass/stdlib/u128_log_test (test.toml) | 4512 | 4520 | -0.18% |

Comparing 'in-language-gas-usages':

Improvements Regressions
Count 341 2
Average 0.69% -0.07%
Median 0.64% -0.07%
Max 3.50% -0.08%
Min 0.01% -0.06%

| u128_inline_tests::parity_u128_log_with_ruint | 1756499 | 1757515 | -0.06% |
| u128_inline_tests::u128_log | 22656 | 22674 | -0.08% |

Comparing 'bytecode-sizes' for project 'o2':

Improvements Regressions
Count 11
Average 0.09%
Median 0.10%
Max 0.11%
Min 0.01%

Comparing 'gas-usages' for project 'o2':

Improvements Regressions
Count 65
Average 0.03%
Median 0.02%
Max 0.42%
Min 0.01%

@xunilrj xunilrj changed the title Replave move by movi when possible Replace move by movi when possible Feb 9, 2026
@xunilrj xunilrj force-pushed the xunilrj/asm-const-prop-improv branch from 48770b2 to 5929427 Compare February 14, 2026 20:03
@xunilrj
Copy link
Contributor Author

xunilrj commented Feb 14, 2026

e2e gas usage

Improvements Regressions
Count 271
Average 7.36%
Median 2.32%
Max 76.22%
Min 0.04%

e2e bytecode

Improvements Regressions
Count 304
Average 11.12%
Median 4.44%
Max 94.15%
Min 0.31%

in lang gas usage

Improvements Regressions
Count 599
Average 6.73%
Median 1.39%
Max 84.77%
Min 0.01%

Comparing 'gas-usages' for project 'o2':

Improvements Regressions
Count 65
Average 0.32%
Median 0.29%
Max 1.51%
Min 0.21%

Comparing 'bytecode-sizes' for project 'o2':

Improvements Regressions
Count 13
Average 1.55%
Median 1.67%
Max 2.13%
Min 0.06%

@ironcev ironcev added compiler General compiler. Should eventually become more specific as the issue is triaged compiler: codegen Everything to do with IR->ASM, register allocation, etc. performance Everything related to performance, speed wise or memory wise. labels Feb 16, 2026
@xunilrj xunilrj changed the title Replace move by movi when possible Improve asm const propagation Feb 16, 2026
@cursor
Copy link

cursor bot commented Feb 18, 2026

PR Summary

Medium Risk
Changes core ASM optimization logic and emitted bytecode, which can affect correctness and any tests/tools that depend on exact instruction sequences or offsets.

Overview
Improves sway-core’s ASM optimization by significantly expanding constant_propagate to fold constants and apply algebraic/bitwise simplifications (e.g., turning ops into movi/addi/noop, simplifying jnz when the condition is known, and better invalidating dependent known-values). The pass now accepts a logging callback (no-op in normal builds) and adds expect-test coverage for many transform cases.

To support these transforms, the ASM layer adds small ergonomics and representation tweaks (e.g., use_registers_mut now returns a Vec, VirtualOp convenience constructors, From<&str> for VirtualRegister, immediate conversions, and NOOP now renders as noop), plus AbstractInstructionSet display no longer prints blank lines.

Generated output changes ripple into updated integration/snapshot expectations: forc raw log PC/ptr values, multiple e2e stdout.snap files (bytecode/offsets/hashes/sizes), and ABI configurable offsets now reflect the new optimized bytecode.

Written by Cursor Bugbot for commit f81f077. This will update automatically on new commits. Configure here.

@xunilrj xunilrj force-pushed the xunilrj/asm-const-prop-improv branch from 126f1ce to 91861d1 Compare February 18, 2026 13:28
@xunilrj
Copy link
Contributor Author

xunilrj commented Feb 18, 2026

e2e gas usage

Improvements Regressions
Count 272
Average 7.48%
Median 2.38%
Max 76.22%
Min 0.04%

e2e bytecode

Improvements Regressions
Count 305
Average 11.21%
Median 4.61%
Max 94.15%
Min 0.31%

inlang gas usage

Improvements Regressions
Count 606
Average 6.95%
Median 1.50%
Max 84.77%
Min 0.01%

Comparing 'bytecode-sizes' for project 'o2':

Improvements Regressions
Count 13
Average 1.56%
Median 1.67%
Max 2.13%
Min 0.07%

Comparing 'gas-usages' for project 'o2':

Improvements Regressions
Count 65
Average 0.32%
Median 0.29%
Max 1.51%
Min 0.21%

@xunilrj xunilrj deployed to fuel-sway-bot February 18, 2026 14:51 — with GitHub Actions Active
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@xunilrj xunilrj marked this pull request as ready for review February 18, 2026 17:54
@xunilrj xunilrj requested review from a team as code owners February 18, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler: codegen Everything to do with IR->ASM, register allocation, etc. compiler General compiler. Should eventually become more specific as the issue is triaged performance Everything related to performance, speed wise or memory wise.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments