Skip to content

cranelift: avoid per-variable value sets when tracking stack-map bindings#13466

Merged
cfallin merged 3 commits into
bytecodealliance:mainfrom
vouillon:gc-fix
May 27, 2026
Merged

cranelift: avoid per-variable value sets when tracking stack-map bindings#13466
cfallin merged 3 commits into
bytecodealliance:mainfrom
vouillon:gc-fix

Conversation

@vouillon
Copy link
Copy Markdown
Contributor

Follow-up to #13449 addressing review feedback from this comment:

  1. SecondaryMap<Variable, EntitySet<Value>> is dense — O(max value id) bits per variable — and wasteful for the sparse sets that show up in practice.
  2. 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_var and SSABuilder::find_var (the latter when SSA construction synthesizes a block parameter). A new helper record_stack_map_binding(var, val) checks stack_map_vars and inserts on a hit; the per-variable set, values_for_var, and the finalize loop all go away.

stack_map_vars and stack_map_values move from FunctionBuilderContext into SSABuilder so the binding sites can reach them. The public FunctionBuilder API 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 inside record_stack_map_binding and nothing is touched.

@vouillon vouillon requested a review from a team as a code owner May 23, 2026 17:57
@vouillon vouillon requested review from cfallin and removed request for a team May 23, 2026 17:57
Move `stack_map_vars` and `stack_map_values` from `FunctionBuilderContext`
into `SSABuilder` itself so that bindings can be recorded as they happen,
in the two places SSA values get bound to a variable:

  * `SSABuilder::def_var`, when the user supplies a new definition; and
  * `SSABuilder::find_var`, when SSA construction synthesises a block
    parameter as the definition.
@github-actions github-actions Bot added the cranelift Issues related to the Cranelift code generator label May 23, 2026
Copy link
Copy Markdown
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks -- general shape looks OK, but the API behavior does change per below, so we'll need to be careful.

Comment thread cranelift/frontend/src/ssa.rs
Marking a variable as needing a stack map now tracks bindings eagerly, so
`declare_var_needs_stack_map` must be called before any `def_var` of the
variable; earlier definitions would be silently omitted from stack maps.

  * Document the ordering constraint on `declare_var_needs_stack_map`.
  * Assert in `SSABuilder::mark_var_needs_stack_map` that the variable has no
    recorded definitions yet, so misuse panics rather than silently dropping a
    GC root from stack maps.
  * Fix the fuzzer's variable pool, which declared the stack-map need after
    the initial `def_var`, dropping that definition from stack maps.
Copy link
Copy Markdown
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks -- one more change on your latest commit and I think this is good to go.

Comment thread cranelift/frontend/src/ssa.rs Outdated
&mut self.stack_map_vars
/// Mark `var` as needing its bindings tracked for stack maps.
pub fn mark_var_needs_stack_map(&mut self, var: Variable) {
assert!(
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.

Let's make this a debug_assert since it's doing a potentially expensive linear search.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

All the same, we try to keep checks like these debug_assert. Please make the change, thanks.

Comment thread cranelift/frontend/src/frontend.rs Outdated
/// Panics if the variable's type is larger than 16 bytes, if this
/// variable has not been declared yet, or if it has already been defined.
/// Panics if the variable's type is larger than 16 bytes or if this
/// variable has not been declared yet.
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.

We shouldn't remove the "panics" comment here: it still panics if it panics in debug-builds.

The check that `mark_var_needs_stack_map` runs before any definition of the
variable scans the variable's per-block definitions. Demote it from `assert!`
to `debug_assert!` so it does not run in release builds.
@cfallin cfallin added this pull request to the merge queue May 27, 2026
Merged via the queue into bytecodealliance:main with commit edfc7f1 May 27, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants