Repository navigation
Coalesce runs of non-empty table entries into single segments - #29
Merged
cfallin merged 5 commits intoNov 16, 2026
Merged
Conversation
The items of an element segment are parsed into a list of `Func`s inline in the active-segment case. Move that code into `parse_element_items` so that passive and declarative segments can use it as well. No functional change.
An element segment item that is a constant expression other than `ref.func` or `ref.null`, such as `global.get`, made the frontend panic. Return `FrontendError::UnsupportedFeature` instead, as the frontend does for other constructs it does not support.
Member
Author
|
Note: stacked on top of #28, but I can't figure out how to use stacks 🙃 |
The frontend dropped passive data segments and passive and declarative element segments, and the backend never emitted a DataCount section. Function bodies that are not expanded are copied byte for byte, so a `memory.init`, `data.drop`, `table.init` or `elem.drop` in such a body then referred to a different segment or to one that no longer existed, a module using `memory.init` or `data.drop` failed validation because DataCount was missing, and a `ref.func` of a function that only a declarative segment declared failed validation as well. Record these segments in the module with their original segment index: `Module::passive_data_segments` holds passive data segments and `Module::element_segments` holds passive and declarative element segments. `Module::has_data_count` records whether the input had a DataCount section, and `Module::orig_data_segment_count` and `Module::orig_element_segment_count` record how many segments of each kind the input had. The backend lays out each segment index space so that every recorded segment lands at its original index. Active segments fill the remaining indices in order. Instantiation drops each active segment after applying it, so code that refers to an active data segment's index behaves the same whichever active data segment occupies it, or if an empty data segment occupies it. The same holds for element segments of the same element type. When there are fewer active segments than free indices below the highest recorded index or below the input's segment count, empty segments fill the rest, so every index the input had stays valid. Active segments beyond that are appended. Data segments are padded to the input's count only when the input had a DataCount section. Validation requires that section for every instruction that names a data segment, so code in an input without one names no data segment. Empty data segments are passive. Empty element segments are active segments of no entries at offset 0 of table 0, in the 1.0 encoding, when table 0 is a `funcref` table, and passive segments otherwise. Instantiation applies an empty active segment without writing an entry or trapping, for any table size, and then drops it. Code that names its index therefore behaves as it does for an empty passive segment. For every input that validates under WebAssembly 1.0 or 2.0, the output requires bulk memory only when the input did. A module that uses reference types without bulk memory and has a table 0 that is not a `funcref` table still gets passive padding. Empty element segments are `funcref` segments. A `table.init` into a typed function reference table that names such an index, or an index now holding an active segment of another element type, fails validation. A DataCount section is emitted when the module has a passive data segment or the input had a DataCount section.
`ModuleDisplay` printed table contents and active data segments only. Print `Module::passive_data_segments` and `Module::element_segments` as well, in the same format.
The backend emitted one active element segment for each non-null table entry, so a module whose table was initialized by one segment of n entries came back with n single-entry segments. The table contents are the same, but each segment repeats the segment header in the binary and is applied separately at instantiation. Emit one active segment for each run of consecutive non-null entries instead. Null entries are still not emitted. The runs go through `segment_layout`, which pads the element segment index space to the input's element segment count, so an unexpanded `elem.drop` or `table.init` that names an active segment's index still validates when merging lowers the number of active segments.
tschneidereit
force-pushed
the
coalesce-element-segments
branch
from
November 11, 2026 23:27
bf2d293 to
e8fcc9d
Compare
Member
|
(I think this should merge OK even with the stack since I don't have squash-merges enabled here -- let's see if it does...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The backend emitted one active element segment for each non-null table entry, so a module whose table was initialized by one segment of
nentries came back withnsingle-entry segments. The table contents are the same, but each segment repeats the segment header in the binary and is applied separately at instantiation.Emit one active segment for each run of consecutive non-null entries instead. Null entries are still not emitted. The runs go through
segment_layout, which pads the element segment index space to the input's element segment count, so an unexpandedelem.droportable.initthat names an active segment's index still validates when merging lowers the number of active segments.The items of an element segment are parsed into a list ofFuncsinline in the active-segment case. Move that code into
parse_element_itemsso that passive and declarative segments canuse it as well. No functional change.