Skip to content

Conversation

@spotandjake
Copy link
Member

@spotandjake spotandjake commented Nov 12, 2025

This replaces our custom bulk memory polyfills to use the binaryen pass implementation.

Notes:

  • We no longer test the polyfill behaviour as we are not trying to test binaryen and we don't have a good way of inspecting the wasm output.
  • The feature flag must be left on, as the instructions get optimized away, and if it was off, adding the instructions would throw exceptions in binaryen.
  • Bulk memory is now supported across the board, so this polyfill is far less important than it was initially. This reduces our maintenance complexity, shifting it over to binaryen.

Closes: #2330

@spotandjake spotandjake self-assigned this Nov 12, 2025
@spotandjake spotandjake force-pushed the spotandjake-bulk-memory-polyfill branch 3 times, most recently from eef1b53 to db1f96f Compare November 13, 2025 02:03
@spotandjake spotandjake marked this pull request as ready for review November 13, 2025 02:25
@spotandjake spotandjake force-pushed the spotandjake-bulk-memory-polyfill branch from db1f96f to e659c4b Compare November 13, 2025 15:09
[
("source", Builtin_types.type_wasmi32),
("destination", Builtin_types.type_wasmi32),
("dest", Builtin_types.type_wasmi32),
Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed these signatures were slightly wrong previously, we never saw this behaviour because the names ended up coming from the polyfills rather than from the primitives however without correcting this mistake this pr would be breaking if you tried to use named arguments.

@spotandjake spotandjake force-pushed the spotandjake-bulk-memory-polyfill branch from e659c4b to 7ecd783 Compare November 14, 2025 00:38
Copy link
Member

@ospencer ospencer left a comment

Choose a reason for hiding this comment

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

I do think we need some kind of smoke test here. We don't need to test Binaryen's behavior in the sense that we need to make sure that the polyfill is correct, but we do need something that makes sure if those couple lines of code get deleted the test fails.

An idea: parse the finished module and verify that the bulk memory feature isn't on.

@spotandjake spotandjake force-pushed the spotandjake-bulk-memory-polyfill branch from 7ecd783 to 8290f80 Compare November 14, 2025 04:08
@spotandjake
Copy link
Member Author

Made those changes

@ospencer
Copy link
Member

And the smoke test?

@spotandjake
Copy link
Member Author

spotandjake commented Nov 14, 2025

I do think we need some kind of smoke test here. We don't need to test Binaryen's behavior in the sense that we need to make sure that the polyfill is correct, but we do need something that makes sure if those couple lines of code get deleted the test fails.

An idea: parse the finished module and verify that the bulk memory feature isn't on.

~~I don't think that this would be trivial; it would basically be implementing a WASM parser. It could be a little simpler, we could scan the code section for the instruction codes instead of parsing, but we would need to ensure they aren't showing up elsewhere ever do to any other changes, such as in an i32.const, align or any other cases.

If we're really serious about this, we could maybe use binaryen to load the code back in and check it with that, but I don't think that's the most trivial thing either.~~ Nevermind I understand what you mean now.

@spotandjake spotandjake force-pushed the spotandjake-bulk-memory-polyfill branch from 8290f80 to 81b5028 Compare November 14, 2025 05:11
@spotandjake
Copy link
Member Author

spotandjake commented Nov 14, 2025

Some tests added.

@spotandjake spotandjake force-pushed the spotandjake-bulk-memory-polyfill branch from 81b5028 to 54bffa7 Compare November 14, 2025 14:25
@spotandjake
Copy link
Member Author

Addressed those comments. I export the Memory.fill and Memory.copy directly so they can never be optimized away.

This replaces our custom bulk memory polyfills to use the binaryen pass implementation.

Notes:
* We no longer test this behaviour as we have no way to test wasm output and we are not trying to test binaryen
* We leave the feature flag on now as the instructions get optimized out after and if its off it causes issues.
* Bulk memory is now supported across the board in runtimes so its far less of a concern then it was initially.

Draft: This is a draft as we need to merge the binaryen 124 stuff before we can merge this

Closes: grain-lang#2330
@spotandjake spotandjake force-pushed the spotandjake-bulk-memory-polyfill branch from 54bffa7 to 35fdd78 Compare November 14, 2025 14:31
Copy link
Member

@ospencer ospencer left a comment

Choose a reason for hiding this comment

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

Thanks, looks great!

@ospencer ospencer changed the title feat(compiler): Use binaryens bulk memory polyfill feat(compiler): Use Binaryen's bulk memory polyfill Nov 14, 2025
@ospencer ospencer enabled auto-merge November 14, 2025 14:52
@ospencer ospencer added this pull request to the merge queue Nov 14, 2025
Merged via the queue into grain-lang:main with commit 1c5478e Nov 14, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace bulkMemory polyfill with llvm-memory-copy-fill-lowering binaryen pass

2 participants