po-nuvai opened PR #15130 from po-nuvai:fix-15036-elem-eval-order to bytecodealliance:main:
Active element segments whose expressions may trap (
struct.new*/array.new*) are now evaluated up front, alongside passive segments, into a hidden segment slot that wasm code cannot access. They are then copied into their table in the normal active-segment order and dropped. This means a trapping element expression no longer leaves earlier writes behind in imported tables, matching the spec. Segments that cannot trap keep the existing fast path.The
cow-order-trapping-elem-expr.wasttest expected the old behavior (an earlier segment's write remaining in the imported table), so it is updated to expect an uninitialized element instead.Fixes #15036
<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease review the Bytecode Alliance's AI tool usage policy at
https://github.com/bytecodealliance/governance/blob/main/AI_TOOL_POLICY.mdPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
po-nuvai removed the review request for wasmtime-compiler-reviewers from PR #15130.
po-nuvai requested cfallin for a review on PR #15130.
po-nuvai requested wasmtime-compiler-reviewers for a review on PR #15130.
po-nuvai requested wasmtime-core-reviewers for a review on PR #15130.
:thumbs_up: cfallin submitted PR review:
This approach seems plausible to me. It might be worthwhile to cite the relevant bit of the Wasm standard's description of instantiation that gives the relevant side-effect ordering here, too, just for reader clarity?
cc @alexcrichton perhaps for a second look just to ensure I haven't missed something subtle, too...
:memo: alexcrichton submitted PR review.
:speech_balloon: alexcrichton created PR review comment:
Could these two fields be stored as an
enuminstead of side-by-side? That I think might better represent the invariant here where the elements are either defined directly (in which case they can't trap and also don't ever get reflected invm::Instance)
:speech_balloon: alexcrichton created PR review comment:
For
stagedhere -- I think this only applies to imported tables, right? For locally-defined tables thestagedbit I don't think should be necessary. (and will help speed up initialization of those tables a bit)
po-nuvai updated PR #15130.
:memo: po-nuvai submitted PR review.
:speech_balloon: po-nuvai created PR review comment:
Done, thanks! It's now a TableSegmentInit enum.
po-nuvai commented on PR #15130:
@cfallin Thanks! Added a comment linking to the spec's instantiation algorithm in 548129882f.
:memo: po-nuvai submitted PR review.
:speech_balloon: po-nuvai created PR review comment:
Good point about avoiding the cost for local tables! I think the target table of the
trapping segment itself isn't quite the right condition though: in the first test case from
#15036 the trapping segment targets a local table ($t2), but an earlier segment writes to
the imported table, so without staging$fwould still be visible. (This is the case at Good point about avoiding the cost for local tables! I think the target table of the
trapping segment itself isn't quite the right condition though: in the first test case from
#15036 the trapping segment targets a local table ($t2), but an earlier segment writes to
the imported table, so without staging$fwould still be visible. (This is the case at
line 31 ofissue15036.wast.)How about skipping staging entirely when the module has no imported tables? Then nothing
outside the instance can observe a partial initialization. Does that sound right to you?line 31 of
issue15036.wast.)How about skipping staging entirely when the module has no imported tables? Then nothing
outside the instance can observe a partial initialization. Does that sound right to you?
:speech_balloon: po-nuvai edited PR review comment.
po-nuvai requested alexcrichton for a review on PR #15130.
po-nuvai requested cfallin for a review on PR #15130.
po-nuvai updated PR #15130.
po-nuvai commented on PR #15130:
While testing I found that the staged path crashed when compiling modules with 64-bit tables: the copy used the table's index type for the length instead of i32 like
table.init. Fixed in 2204077acc, with a new test inissue15036.wast.
:speech_balloon: alexcrichton created PR review comment:
Oh I see and yeah that makes sense. Instead of looking for imported tables vs not, how about a local
boolwhile translating element segments, something likeside_effects_visibleand once a segment applies to an imported table that flips totrue, and the condition for using staged requiresside_effects_visible && may_trap?Mostly I'm hopeful we can avoid initializing an entire element segment for a module with a defined table where the side effects aren't visible because it'd help optimize instantiation a bit.
:memo: alexcrichton submitted PR review.
po-nuvai updated PR #15130.
:memo: po-nuvai submitted PR review.
:speech_balloon: po-nuvai created PR review comment:
Done in <12b358d908> , thanks!
The flag also counts the current segment, since a trapping segment that writes to an imported table must still be staged (test case 2 in
issue15036.wastcovers this).I also added a test for a trapping segment before any imported table write, which is now evaluated in place.
:thumbs_up: alexcrichton submitted PR review.
alexcrichton added PR #15130 Evaluate trapping active element segments before table writes to the merge queue.
:check: alexcrichton merged PR #15130.
alexcrichton removed PR #15130 Evaluate trapping active element segments before table writes from the merge queue.
po-nuvai requested alexcrichton for a review on PR #15130.
Last updated: Nov 19 2026 at 20:05 UTC