Skip to content

Implement dynamic storage opcodes in the compiler#7595

Merged
ironcev merged 5 commits intomasterfrom
ironcev/dynamic-storage-add-opcodes
Apr 16, 2026
Merged

Implement dynamic storage opcodes in the compiler#7595
ironcev merged 5 commits intomasterfrom
ironcev/dynamic-storage-add-opcodes

Conversation

@ironcev
Copy link
Copy Markdown
Member

@ironcev ironcev commented Apr 14, 2026

Description

This PR is a step in implementing #7560. It adds compiler support for dynamic storage opcodes by:

  • supporting all opcodes in asm blocks.
  • adding IR instructions for all opcodes.

Note that the FuelVmInstruction::StatePreload IR instruction returns zero if the slot is not used, but also if it is used and contains zero-sized content. The reason is that zero-sized storage types, like, e.g. StorageVec, have special semantics in the storage API and the storage_api does not write zero-sized types into storage, nor reads them. Modeling the IR instruction this way preserves this semantics and allows for more efficient implementation that is actually needed in the API. (Returning both the length and the existence information would require a stack allocation and more gas expensive compilation for something that is actually not used.)

This API semantics might be changed to support zero-sized types like any other types. This would be a separate breaking change and a part of Configurable and composable storage RFC.

Meanwhile, if a distinction between unused and zero-sized used slot is needed while preloading, it can be achieved by using the SPLD opcode in asm block.

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.

@ironcev ironcev self-assigned this Apr 14, 2026
@cursor
Copy link
Copy Markdown

cursor Bot commented Apr 14, 2026

PR Summary

High Risk
Touches core compiler IR/verification and ASM/codegen paths for storage access, so bugs could change contract state semantics or produce invalid bytecode. Adds multiple new opcodes with new pointer-type requirements and $err-based signaling, increasing integration surface across tooling and tests.

Overview
Adds full pipeline support for FuelVM dynamic storage operations by introducing new IR instructions (StateClearSlots, StateReadSlot, StateWriteSlot, StateUpdateSlot, StatePreload) and new ASM opcodes (sclr, srdd, srdi, swrd, swri, supd, supi, spld) with printing/parsing and allocation support.

Updates codegen (fuel_asm_builder) to emit the new VirtualOp variants, selecting immediate vs register forms for len where applicable, and adjusts IR verification/type rules (including new untyped-pointer checks and new error variants) plus purity/CEI effect analysis to classify the new storage reads/writes correctly.

Extends forc-debug variable decoding for the new instructions and adds/updates serialization and e2e VM tests to cover the new dynamic storage behaviors and purity diagnostics.

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

@ironcev ironcev added compiler General compiler. Should eventually become more specific as the issue is triaged compiler: ir IRgen and sway-ir including optimization passes compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler: parser Everything to do with the parser compiler: codegen Everything to do with IR->ASM, register allocation, etc. labels Apr 14, 2026
@ironcev
Copy link
Copy Markdown
Member Author

ironcev commented Apr 14, 2026

👍

Comment thread sway-ir/src/verify.rs
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 14, 2026

Merging this PR will not alter performance

✅ 25 untouched benchmarks


Comparing ironcev/dynamic-storage-add-opcodes (a871f09) with master (f9b8158)

Open in CodSpeed

@ironcev ironcev marked this pull request as ready for review April 15, 2026 05:37
@ironcev ironcev requested review from a team as code owners April 15, 2026 05:37
@ironcev ironcev requested a review from Dentosal April 15, 2026 05:37
@ironcev ironcev enabled auto-merge (squash) April 15, 2026 05:37
Comment thread sway-core/src/semantic_analysis/cei_pattern_analysis.rs
Comment thread sway-ir/src/verify.rs
Copy link
Copy Markdown

@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.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4cdd67d. Configure here.

Comment thread sway-ir/src/printer.rs Outdated
@ironcev ironcev merged commit fbb524c into master Apr 16, 2026
43 checks passed
@ironcev ironcev deleted the ironcev/dynamic-storage-add-opcodes branch April 16, 2026 10:15
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: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler: ir IRgen and sway-ir including optimization passes compiler: parser Everything to do with the parser 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.

2 participants