Skip to content

wasm-encoder: add ComponentBuilder::start() - #2657

Merged
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
zacharywhitley:wasmos/component-start
Sep 14, 2026
Merged

alexcrichton merged 1 commit into
bytecodealliance:mainfrom
zacharywhitley:wasmos/component-start

Conversation

@zacharywhitley

Copy link
Copy Markdown
Contributor

Summary

Adds a public ComponentBuilder::start() helper for emitting component start sections. Currently every other section shape has a builder helper (import, export, alias, lift_func, lower_func, core_module, core_module_raw, component_raw, instantiate, instantiate_exports per #2655, raw_custom_section, …), but component start is missing — callers have to fall back to the lower-level Component::section(&ComponentStartSection { … }) interface and manually maintain their own value-index counter.

pub fn start(&mut self, function_index: u32, args: Vec<u32>, results: u32) -> u32 {
    self.flush();
    self.component.section(&ComponentStartSection {
        function_index,
        args,
        results,
    });
    let base = self.values.count;
    for _ in 0..results {
        self.values.add(None);
    }
    base
}

Returns the index of the first produced value in the component value index space; subsequent produced values occupy contiguous indices from first+1 up to first+results-1.

Motivation

Downstream tooling (specifically wasmos-component-opt, a component-level optimizer) needs to round-trip components that carry start sections. The reader / IR / DCE walker are all in place; only the encoding step was gated on this API. The component_start_section_roundtrips integration test in that crate exercises the full path (parse → optimize → re-encode → validate).

Design notes

  • Component start is a singleton section rather than an aggregating one, so it isn't routed through the section_accessors macro that handles component_instances, types, imports, etc. The helper flush()es any prior section first so section ordering stays coherent.
  • The Component Model spec permits at most one start section per component. This helper does not enforce that — matching the underlying ComponentStartSection encoder's posture; the caller carries the responsibility.
  • Return value is deliberately consistent with instantiate_exports (wasm-encoder: add ComponentBuilder::instantiate_exports #2655): a u32 naming the first new index-space entry produced. Callers can iterate first..first+results if they need each slot separately.

Test plan

  • cargo build -p wasm-encoder clean.
  • cargo test -p wasm-encoder — 36 tests, all pass.
  • Downstream integration verified against wasmos-component-opt's component_start_section_roundtrips test (parse a component with a start section → run the -O2 pipeline → re-encode via b.start(...) → validate).

Second wasmos-required helper on the same fork branch as
`instantiate_exports`. Emits a component start section, invoking
`function_index` at instantiation time with `args` (each an index
into the component value index space) and producing `results` new
values.

Returns the index of the first produced value so callers can
register the newly-populated value-index slots for downstream
consumers (canonical option refs, exports, etc.). Subsequent
produced values occupy `first+1`, `first+2`, …, `first+results-1`.

Component start is a singleton section (spec allows at most one
per component); this helper does not enforce that, matching the
underlying `ComponentStartSection` encoder's posture. Flushes any
prior aggregating section before emitting so section ordering
stays coherent.

Unblocks wasmos-component-opt's `SectionItem::ComponentStart`
writer arm, which previously `bail!`'d pending this method. See
`docs/upstream-needs.md` in that crate for the full context.
@zacharywhitley
zacharywhitley requested a review from a team as a code owner September 14, 2026 13:06
@zacharywhitley
zacharywhitley requested review from pchickey and removed request for a team September 14, 2026 13:06
@alexcrichton
alexcrichton added this pull request to the merge queue Sep 14, 2026
Merged via the queue into bytecodealliance:main with commit d7303f0 Sep 14, 2026
37 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