Skip to content

feat: (CM64) context.{set, get} accept i64 immediate - #2502

Merged
alexcrichton merged 7 commits into
bytecodealliance:mainfrom
michael-weigelt:mwe/context
Apr 27, 2026
Merged

alexcrichton merged 7 commits into
bytecodealliance:mainfrom
michael-weigelt:mwe/context

Conversation

@michael-weigelt

Copy link
Copy Markdown
Contributor

This is part of a series of PRs to enable verification of 64bit components.. The effort is tracked here.

This PR allows context.{set, get} to accept i64 as well as i32.

Comment on lines +373 to +381
;; Other value types (e.g. `f32`) are rejected regardless of the `cm64` feature.
(assert_invalid
(component
(core func (canon context.get f32 0)))
"`context.get` only supports `i32` or `i64`")
(assert_invalid
(component
(core func (canon context.set f32 0)))
"`context.set` only supports `i32` or `i64`")

@michael-weigelt michael-weigelt Apr 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moving the discussion from there to here:

@michael-weigelt michael-weigelt Apr 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

adambratschikaye
I think these ones should be malformed instead of invalid because the spec doesn't even permit the construction with other values. I guess this means you need to have a check in the parser itself as well.

michael-weigelt
Ah I wrongly assumed that this is extensible, coming from the resource rep type. Then maybe I should not use ValType anywhere at all. (?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a good question! By the letter of the spec here @adambratschikaye is correct where these tests should be assert_malformed rather than assert_invalid. Additionally following the letter of the spec all usage of ValType would want to get replaced with something else, yeah. (in Wasmtime we have an IndexType for memories/tables which may be another possible name).

cc @lukewagner on this specific point though, how do you feel about this? Should the binary format for context.{get,set} allow any core wasm value type in the binary encoding (and, thus, the text encoding too)? Or should the binary/text formats only allow i32 and i64? I could see us having a desire in the future to allow something like anyref here too for GC-using programs, in which case it'd be easiest to relax and allow any core value type here with validation restricting it to i32 and i64.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In the binary format, we use the <valtype> opcodes for i32 and i64 (0x7f and 0x7e) directly in the decoding rule instead of decoding a generic <valtype> and then using validation rules to rule out everything except i32/i64 (just like we do for the (rep i32)/(rep i64) of a resource type). But in the AST, we have (canon context.{get,set} <valtype> ...) and verbiage about validation rejecting everything but i32/i64, so I suppose it's ambiguous. Probably we should align Binary.md with Explainer.md and go the decode+validate route, hence assert_invalid?

Practically speaking, I hope we don't do anyref since it'll raise gross questions of what to do if you, e.g., context.set anyref 0 and then context.get i32 0. With integers we say that they alias like 64-bit registers do on x64, but less clear what the right thing to do with anyref that avoids overhead. My thinking was that, if you wanted to store a reference, you'd store it in a table and then store the i32 index in TLS.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I hadn't thought about the view-typing of slot 0 and slot 1 as different integer widths and that's a good point... Do you think it's feasible that we could guarantee that each slot is viewed as exactly one type in validation? For example within a component context.get 0 could only have one type?

Otherwise though I agree that syncing the text format and Binary.md makes sense, namely by changing Binary.md to use a valtype, which @michael-weigelt would keep this as assert_invalid yeah.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do you think it's feasible that we could guarantee that each slot is viewed as exactly one type in validation?

That's a good point; I suppose we could add a whole-component validation rule to that effect. It'd be the only validation rule like that and usually a rule like that has composability problems (compiling unrelated bits of code that disagree), but given that the use of context.{get,set} requires all the code in the same component to agree on the ABI anyways, I think it'd be be fine in this case. I think I'll work on a CM PR for this and the Binary.md fix.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Makes sense, and sounds good! In that case @michael-weigelt let's keep all the ValType bits as-is in this PR

Comment thread crates/wast/src/component/func.rs Resolved
Comment thread crates/wast/src/component/resolve.rs Resolved
Comment thread crates/wit-component/src/validation.rs Resolved
Comment thread crates/wit-component/src/encoding.rs Resolved
Comment thread crates/wast/src/component/func.rs Resolved
Comment on lines +373 to +381
;; Other value types (e.g. `f32`) are rejected regardless of the `cm64` feature.
(assert_invalid
(component
(core func (canon context.get f32 0)))
"`context.get` only supports `i32` or `i64`")
(assert_invalid
(component
(core func (canon context.set f32 0)))
"`context.set` only supports `i32` or `i64`")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a good question! By the letter of the spec here @adambratschikaye is correct where these tests should be assert_malformed rather than assert_invalid. Additionally following the letter of the spec all usage of ValType would want to get replaced with something else, yeah. (in Wasmtime we have an IndexType for memories/tables which may be another possible name).

cc @lukewagner on this specific point though, how do you feel about this? Should the binary format for context.{get,set} allow any core wasm value type in the binary encoding (and, thus, the text encoding too)? Or should the binary/text formats only allow i32 and i64? I could see us having a desire in the future to allow something like anyref here too for GC-using programs, in which case it'd be easiest to relax and allow any core value type here with validation restricting it to i32 and i64.

@michael-weigelt
michael-weigelt marked this pull request as ready for review April 27, 2026 16:12
@michael-weigelt
michael-weigelt requested a review from a team as a code owner April 27, 2026 16:12
@michael-weigelt
michael-weigelt requested review from fitzgen and removed request for a team April 27, 2026 16:12
@alexcrichton
alexcrichton requested review from alexcrichton and removed request for fitzgen April 27, 2026 16:17
@alexcrichton
alexcrichton added this pull request to the merge queue Apr 27, 2026
Merged via the queue into bytecodealliance:main with commit 61a7487 Apr 27, 2026
37 checks passed
imikushin pushed a commit to imikushin/wasm-tools that referenced this pull request Nov 6, 2026
)

This commit is a start to support the `cm64` feature in wit-component
itself. This is intended to start plumbing necessary knobs, variables,
etc, to be able to produce a component when the linear memory uses a
64-bit index type instead of today's 32-bit index type. No output from
today is expected to change, but modules using a 64-bit linear memory
are intended to, in theory "just start working" with this.

Note that this is not yet fully complete. The goal of this commit is to
set up a lot of the initial plumbing for index types but there are a few
notable exceptions to supporting this:

1. Exported functions taking a `borrow<T>` which is owned by the
   component itself do not work correctly. These incorrectly are
   expected to take `i32` instead of the resource's `rep` field, in this
   case `i64`. Part of this is bytecodealliance#2804 where `wasmparser`'s validation
   needs to be updated, but part of this is that larger `wit-parser`
   changes are going to be necessary since it's not as simple to produce
   the core wasm signature of a WIT function any more. Overall this is
   untested at this time and deferred to a future change.

2. The `wasm-tools component link` command does not yet work. That stage
   still only supports 32-bit modules and 64-bit modules is entirely
   unsupported. That's intended to come as a future change as well.

3. The `wasm-tools component embed --dummy` subcommand only supports
   32-bit memories so far and doesn't support 64-bit just yet.

The general intent here is to fill out the "easy" part of memory64
support to move the needle a bit on supporting it. Wasmtime does not yet
support this, for example, but that doesn't meant that tooling
shouldn't. In theory this should make the next sufficiently-motivated
individual to work on cm64 have a little less work to do.

One final notable part of this PR is that this removes the interim names
added for some intrinsics in initial cm64 support. Specifically the
support added in bytecodealliance#2502 and bytecodealliance#2514 is removed where the types required and
involved are inferred and don't need explicit annotation in the import
name itself. This is intended to make bindings easier to generate (just
need the types to be right, not the names), and it's expected that no
existing generator is relying on these names just yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants