vouillon opened PR #13466 from vouillon:gc-fix to bytecodealliance:main:
Follow-up to #13449 addressing review feedback from this comment:
SecondaryMap<Variable, EntitySet<Value>>is dense — O(max value id) bits per variable — and wasteful for the sparse sets that show up in practice.- The bookkeeping should drop to zero when no caller has flagged any variable as needing stack maps.
Instead of tracking every SSA value ever bound to a variable and propagating the needs-stack-map bit at finalize time, this PR records values eagerly at the two binding sites —
SSABuilder::def_varandSSABuilder::find_var(the latter when SSA construction synthesizes a block parameter). A new helperrecord_stack_map_binding(var, val)checksstack_map_varsand inserts on a hit; the per-variable set,values_for_var, and the finalize loop all go away.
stack_map_varsandstack_map_valuesmove fromFunctionBuilderContextintoSSABuilderso the binding sites can reach them. The publicFunctionBuilderAPI is unchanged.Result:
- Sparse memory: only the single
EntitySet<Value>of values that need stack maps is ever allocated — no per-variable structure.- Free when unused: with no tracked variables,
stack_map_vars.contains(var)short-circuits insiderecord_stack_map_bindingand nothing is touched.
vouillon requested wasmtime-compiler-reviewers for a review on PR #13466.
vouillon requested cfallin for a review on PR #13466.
vouillon updated PR #13466.
vouillon updated PR #13466.
github-actions[bot] added the label cranelift on PR #13466.
:memo: cfallin submitted PR review:
Thanks -- general shape looks OK, but the API behavior does change per below, so we'll need to be careful.
:speech_balloon: cfallin created PR review comment:
This implies an ordering constraint in the API that wasn't there before, right? In particular: one needs to
declare_var_needs_stack_mapbefore one does anydef_var. That wasn't the case before -- all defs were found duringfinalize.I think that's fine, but we should (i) add a note to the public API docs, and (ii) audit our use in Wasmtime to make sure this is truly the case.
vouillon updated PR #13466.
:memo: vouillon submitted PR review.
:speech_balloon: vouillon created PR review comment:
Indeed, this implies an ordering constraint in the API.
I considered keeping track of overridden values, using a set of pairs (variable, value), but the code was awkward. Also, it is not possible to skip maintaining any additional info when stack maps are not requested by the user of Cranelift without imposing an ordering contraint.
I have added a comment, and an assertion which gets triggered if
declare_var_needs_stack_mapis called afterdef_var.Function
declare_var_needs_stack_mapis used in the following files:
crates/cranelift/src/translate/func_translator.rs
- when declaring parameters (
declare_wasm_parameters);- when declaring locals ('declare_locals');
cranelift/fuzzgen/src/function_generator.rs
inbuild_variable_pool;
cranelift/frontend/src/frontend/safepoints.rs
in various tests.I have checked that all calls to
declare_var_needs_stack_mapare right after the call todeclare_varthat defines the variable. I fixed the now incorrect order incranelift/fuzzgen/src/function_generator.rs.
:memo: cfallin submitted PR review:
Thanks -- one more change on your latest commit and I think this is good to go.
:speech_balloon: cfallin created PR review comment:
Let's make this a
debug_assertsince it's doing a potentially expensive linear search.
:memo: vouillon submitted PR review.
:speech_balloon: vouillon created PR review comment:
In the intended call pattern, the variable has no recorded definitions yet, so the scan is O(1). It's only linear in the misuse case, which panics anyway. But I can switch to
debug_assert!if that feels safer to you.
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
All the same, we try to keep checks like these
debug_assert. Please make the change, thanks.
vouillon updated PR #13466.
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
We shouldn't remove the "panics" comment here: it still panics if it panics in debug-builds.
vouillon updated PR #13466.
:thumbs_up: cfallin submitted PR review.
cfallin added PR #13466 cranelift: avoid per-variable value sets when tracking stack-map bindings to the merge queue.
:check: cfallin merged PR #13466.
cfallin removed PR #13466 cranelift: avoid per-variable value sets when tracking stack-map bindings from the merge queue.
Last updated: Jun 01 2026 at 09:49 UTC