Skip to content

Optimize away zero-length MCP/MCPI instructions#7629

Merged
ironcev merged 5 commits into
FuelLabs:masterfrom
Dnreikronos:Dnreikronos/optimize-mcp-zero-length
Jun 1, 2026
Merged

Optimize away zero-length MCP/MCPI instructions#7629
ironcev merged 5 commits into
FuelLabs:masterfrom
Dnreikronos:Dnreikronos/optimize-mcp-zero-length

Conversation

@Dnreikronos
Copy link
Copy Markdown
Contributor

@Dnreikronos Dnreikronos commented May 23, 2026

Description

MCP with length=0 does nothing at runtime in FuelVM: no panic, no memory change. It resets $err to 0, but every instruction does that anyway. So we can safely strip these out.

This adds two match arms to remove_redundant_ops():

  • MCP(_, _, len) where len is $zero (set by constant propagation)
  • MCPI(_, _, imm) where the immediate is 0

Since this runs after constant_propagate() and dce(), any register known to be zero is already $zero by the time we get here. Leftover setup instructions (like MOVI len_reg, 0) become dead code, and DCE cleans them up on the next round.

The IR-level path (compile_mem_copy_bytes) already skips zero-length copies at compile time. This catches the cases that only resolve to zero after constant propagation at the ASM level (inline ASM paths, Vec push of empty slices, etc.).

Closes #7624

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 (Forc, Sway book, etc.).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a) parsing test in core/trybuild, if needed.
  • I have added the necessary Breaking* or New Feature labels, if 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.

Test plan

  • mcp_zero_len.sw IR generation test: checks that mcp shows up in the IR but is gone from final ASM (not: mcp filecheck directive)
  • All 78 IR generation tests still pass
  • Manually confirmed with forc build --asm final on a script using asm { mcp dst src zero; }, no mcp in output

FuelVM's MCP with length=0 is a no-op at runtime: no panic, no memory
change. Remove zero-length MCP (when length register is $zero) and
MCPI (when immediate is 0) in remove_redundant_ops(), saving bytecode
size and execution cost.

Closes FuelLabs#7624
Verify that an inline asm mcp with zero length register is present in
the IR but optimized away in the final assembly output.
@Dnreikronos Dnreikronos requested a review from a team as a code owner May 23, 2026 19:21
@cursor
Copy link
Copy Markdown

cursor Bot commented May 23, 2026

PR Summary

Low Risk
Localized compiler peephole change for documented no-op copies; behavior preserved with tests and lower gas on affected paths.

Overview
Extends Fuel VM remove_redundant_ops so zero-length memory copies are dropped from generated assembly: MCP when the length register is $zero (after constant propagation) and MCPI when the immediate length is 0. These are no-ops at runtime, so removing them shrinks code and saves gas on paths that only become zero-length at the ASM layer (inline asm, empty Vec copies, etc.).

Adds mcp_zero_len.sw to assert copies still appear in IR but not in final ASM, and refreshes vec_encoding_decoding snapshot gas/artifact sizes slightly lower.

Reviewed by Cursor Bugbot for commit 8d92e1a. Bugbot is set up for automated code reviews on this repo. Configure here.

xunilrj
xunilrj previously approved these changes May 24, 2026
@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. compiler: optimization IR Optimization Passes labels May 25, 2026
Comment thread test/src/ir_generation/tests/mcp_zero_len.sw Outdated
Comment thread test/src/ir_generation/tests/mcp_zero_len.sw
@Dnreikronos Dnreikronos force-pushed the Dnreikronos/optimize-mcp-zero-length branch from 2323409 to 8cd8513 Compare May 25, 2026 12:29
@Dnreikronos Dnreikronos requested review from ironcev and xunilrj May 25, 2026 12:31
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 26, 2026

Merging this PR will not alter performance

✅ 25 untouched benchmarks


Comparing Dnreikronos:Dnreikronos/optimize-mcp-zero-length (8d92e1a) with master (b556c08)

Open in CodSpeed

@xunilrj
Copy link
Copy Markdown
Contributor

xunilrj commented May 26, 2026

@Dnreikronos You may need to run cargo fmt and update snapshot tests cargo r -p test -r -- -k snapshot; cargo insta accept

@Dnreikronos
Copy link
Copy Markdown
Contributor Author

@Dnreikronos You may need to run cargo fmt and update snapshot tests cargo r -p test -r -- -k snapshot; cargo insta accept

Adjusted!

@fuel-cla-bot
Copy link
Copy Markdown

fuel-cla-bot Bot commented May 26, 2026

Thanks for the contribution! Before we can merge this, we need @Dnreikronos to sign the Fuel Labs Contributor License Agreement.

@Dnreikronos
Copy link
Copy Markdown
Contributor Author

@xunilrj @ironcev
image
I'm just trying to sign the "Fuel Labs Contributor License Agreement"

@ironcev
Copy link
Copy Markdown
Member

ironcev commented May 26, 2026

@Dnreikronos We currently have an issue with the CLA signing. I hope it will be solved soon. Thank you for your contribution and please bear with us until the verification step is fixed.

@Voxelot
Copy link
Copy Markdown
Member

Voxelot commented May 26, 2026

@Dnreikronos the CLA bot should be back in working order. Appreciate your patience and your contribution!

@ironcev
Copy link
Copy Markdown
Member

ironcev commented May 30, 2026

👍

@ironcev
Copy link
Copy Markdown
Member

ironcev commented May 31, 2026

@Dnreikronos Did you try to sign the CLA?

@Dnreikronos
Copy link
Copy Markdown
Contributor Author

@Dnreikronos Did you try to sign the CLA?

Yes, but I still get the same error:
image

@fuel-cla-bot fuel-cla-bot Bot removed the cla:missing label Jun 1, 2026
@Dnreikronos
Copy link
Copy Markdown
Contributor Author

Now worked, @ironcev :)

@ironcev ironcev enabled auto-merge (squash) June 1, 2026 20:30
@ironcev ironcev temporarily deployed to fuel-sway-bot June 1, 2026 20:32 — with GitHub Actions Inactive
@ironcev ironcev merged commit cf225be into FuelLabs:master Jun 1, 2026
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla:signed compiler: codegen Everything to do with IR->ASM, register allocation, etc. compiler: optimization IR Optimization Passes compiler General compiler. Should eventually become more specific as the issue is triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider optimizing mcp dest src $zero away

4 participants