Add a pass to relax nullabilities of definitions

Open
#9,352 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@stevenfontanella is already working on this.

Since Nov 5, 2026.

  • #9349 by @stevenfontanella — merged
  • #9380 by @stevenfontanella — merged
  • #9389 by @stevenfontanella — open

Assessment

This issue has not been assessed yet.

Description

enhancement

As a form of generalization, we can make all definitions nullable (e.g. globals, locals, function params, struct fields, arrays, tags), and insert ref.as_non_null as needed for cases that can't be relaxed (e.g. imported/exported functions, public structs). This transformation is valid since it only introduces ref.as_non_nulls that are no-ops.

The benefits of this are:

  • When --traps-never-happen is enabled, it allows us to remove the majority of ref.as_non_null instructions with --strip-refining-casts
  • It exposes more opportunities for locals to share a declaration group
  • It also exposes more opportunities for type merging by causing more function and struct types to be identical

TypeGeneralizing (which is WIP) may also relax nullability, however it would never 'overgeneralize' and allow something to be nullable that would require inserting ref.as_non_null. So this pass may still be useful even alongside TypeGeneralizing.

In a prototype of this pass, I found that this pass reduces the size of a Google Wasm binary by ~3 percent (both in gzipped and uncompressed size).


Implementation plan:

  • RelaxNullability for locals and params (#9349)
  • Relax function bodies when params can't otherwise be relaxed (e.g. exported functions or referenced functions that may escape) (#9380)
  • Relax private functions even if they may be called indirectly (#9389)
  • Relax private struct/array fields
  • Handle multi-value returns
  • Double-check if relaxing return values is worth it
  • Consider relaxing binaryen.js.called functions when --traps-never-happen
Dominant language
WebAssembly
Stars
8.7k
Forks
913
Avg merge
2d 14h
Merged PRs (30d)
92

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from WebAssembly/binaryen

All issues in WebAssembly/binaryen

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.