Skip to content

Coalesce runs of non-empty table entries into single segments - #29

Merged
cfallin merged 5 commits into
bytecodealliance:mainfrom
tschneidereit:coalesce-element-segments
Nov 16, 2026
Merged

cfallin merged 5 commits into
bytecodealliance:mainfrom
tschneidereit:coalesce-element-segments

Conversation

@tschneidereit

Copy link
Copy Markdown
Member

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.The items of an element segment are parsed into a list of Funcs
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.

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.
@tschneidereit

Copy link
Copy Markdown
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.

@cfallin cfallin 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.

Last commit LGTM, thanks!

@cfallin

cfallin commented Nov 16, 2026

Copy link
Copy Markdown
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...)

@cfallin
cfallin merged commit 311eb89 into bytecodealliance:main Nov 16, 2026
3 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.

2 participants