Skip to content

[multibyte] Add multibyte array store instructions.#8059

Merged
brendandahl merged 24 commits into
WebAssembly:mainfrom
brendandahl:multibyte-array-store
Mar 18, 2026
Merged

[multibyte] Add multibyte array store instructions.#8059
brendandahl merged 24 commits into
WebAssembly:mainfrom
brendandahl:multibyte-array-store

Conversation

@brendandahl

@brendandahl brendandahl commented Nov 18, 2025

Copy link
Copy Markdown
Collaborator

Prototype implementation of the new multibyte array proposal that reuses the existing memory instructions.
The syntax for the new instructions is as follows:
<i32|i64|f32|f64>.store (type $type_idx) <array ref> <index> <value>

Some spec related TODOs:

  • Decide on new instructions vs existing memory instructions with memarg flag.
  • If using existing instructions:
    • What does the wat syntax look like? e.g. i32.store type=array x y or i32.store array x y or ???
    • Do we support immediate offsets? - Do we support i32.store8? (effectively the same as array.set on i8)
  • Do we support atomic instructions?

Some implementation TODOs:

  • Add feature flag.
  • Add v128.store.
  • Go through the various visitArrayStore functions and implement them or make sure the copied versions makes sense.
  • Add fuzzing support.
  • Make it so there's only one WasmBinaryReader::getMemarg
  • Add JS API

Comment thread src/wasm.h Outdated
Comment on lines +1846 to +1847
// TODO is this needed?
MemoryOrder order = MemoryOrder::Unordered;

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.

Probably in the long run, but not urgently. I would leave it out for now.

Comment thread test/lit/array-multibyte.wast Outdated
Comment thread src/wasm/wasm-binary.cpp Outdated
case BinaryConsts::I32StoreMem8: {
auto [mem, align, offset] = getMemarg();
return builder.makeStore(1, offset, align, Type::i32, mem);
auto [mem, align, offset, backing] = getMemargWithBacking();

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.

The binary format will need the type immediate as well, so this will have to look something like this:

Suggested change
auto [mem, align, offset, backing] = getMemargWithBacking();
auto [mem, align, offset, backing] = getMemargWithBacking();
if (backing == BackingType::Array) {
HeapType type = getIndexedHeapType();
...

At that point we might as well add a separate IRBuilder method for making array stores, which should help separate concerns more.

@brendandahl brendandahl force-pushed the multibyte-array-store branch 4 times, most recently from 46cdca3 to 60f68c8 Compare February 19, 2026 18:34
@brendandahl brendandahl force-pushed the multibyte-array-store branch 2 times, most recently from 0a6ba01 to 2f3a132 Compare February 28, 2026 01:13
Add support for multibyte array store instructions as proposed in the
WebAssembly multibyte proposal. These instructions allow storing values
of various byte widths directly into i8 arrays, avoiding the need for
manual bit manipulation and masking.

Link: https://github.com/WebAssembly/multibyte
@brendandahl brendandahl force-pushed the multibyte-array-store branch from 2f3a132 to 0847ada Compare March 2, 2026 17:46
@brendandahl brendandahl requested a review from tlively March 2, 2026 18:26

@tlively tlively left a comment

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.

Nice!

Comment thread src/parser/parsers.h
Comment thread src/parser/parsers.h Outdated
Comment thread src/ir/subtype-exprs.h Outdated
Comment thread src/wasm.h Outdated
Comment thread src/wasm/wasm-stack.cpp Outdated
Comment thread test/lit/array-multibyte.wast Outdated
Comment thread test/spec/array-multibyte.wast Outdated
Comment thread test/spec/array-multibyte.wast
Comment thread test/spec/array-multibyte.wast
Comment thread test/spec/array-multibyte.wast
@brendandahl brendandahl requested a review from tlively March 4, 2026 00:06
Comment thread src/wasm/wasm-ir-builder.cpp Outdated
Comment thread test/lit/array-multibyte.wast
Comment thread test/lit/array-multibyte.wast
Comment thread test/spec/array-multibyte.wast
Comment thread src/wasm.h Outdated
@brendandahl brendandahl changed the title [WIP][multibyte] Add multibyte array store instructions. [multibyte] Add multibyte array store instructions. Mar 6, 2026
@brendandahl brendandahl requested a review from tlively March 6, 2026 22:38
@brendandahl brendandahl requested a review from kripken March 6, 2026 22:38

@tlively tlively left a comment

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.

LGTM % final comment.

Comment thread src/ir/child-typer.h Outdated
}
note(&curr->ref, Type(*ht, Nullable));
note(&curr->index, Type::i32);
note(&curr->value, valueType ? *valueType : curr->value->type);

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.

We should self().noteUnknown() if curr->value->type == Type::unreachable.

Comment thread src/ir/possible-contents.cpp
Comment thread src/passes/Print.cpp

@kripken kripken left a comment

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.

Please fuzz before landing.

@brendandahl

Copy link
Copy Markdown
Collaborator Author

Please fuzz before landing.

How long would you recommend letting it run for?

@kripken

kripken commented Mar 10, 2026

Copy link
Copy Markdown
Member

I would give it half a day, personally.

// As in readFromData, normalize to the proper cone.
auto cone = refContents.getCone();
auto normalizedDepth = getNormalizedConeDepth(cone.type, cone.depth);
if (multibyte) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@kripken Here's where I'm having issues.

(module
 (type $0 (func))
 (type $1 (array (mut i8)))
 (global $global$0 (ref $1) (array.new_default $1
  (i32.const 4)
 ))
 (func $0
  (f64.store (type $1)
   (global.get $global$0)
   (i32.const 1)
   (f64.const 2)
  )
 )
)

with
wasm-opt a.wasm -o b.wasm --discard-global-effects --roundtrip --signature-refining --gufa-cast-all --shrink-level=1 --closed-world --dce --closed-world -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.

I am guessing that filterExpressionContents applies the type of the expression in the wasm, and here it writes a different type, so the filtering ends up making things worse.

Updating filterExpressionContents should fix this. I assume the issue is on ArrayGet. When such a get sees data of another type in a numeric array, rather than combining them to get Many, we can apply PossibleContents::fromType(value->type). That is, an unknown value in array.get is still going to be of the type that particular get reads.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Alright here's what I came up with in c148eaf . I'll let it cook on the fuzzer for a bit.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@kripken This has run against the fuzzer for a day. Did you want to review the above changes or is this good to land?

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.

lgtm with the suggested TODO

Comment thread src/ir/possible-contents.cpp
@brendandahl brendandahl enabled auto-merge (squash) March 18, 2026 00:16
@brendandahl brendandahl merged commit 29ef3a4 into WebAssembly:main Mar 18, 2026
16 checks passed
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