SIGN IN SIGN UP

MemoryPacking: Optimize overlapping segments on imported memories when provably in-bounds (#8882)

Follow-up to #8834, implementing the TODO added there at @tlively's
suggestion.

When active segments overlap and the memory is imported, the pass gave
up entirely: a later out-of-bounds segment traps mid-instantiation and
leaves the partially-written state visible in the imported memory (which
outlives the failed instantiation), so even trampled data mattered.

However, if every active segment is provably in bounds of the memory's
declared minimum size, no segment can trap during instantiation at all.
Instantiation then always applies every segment, only the final memory
contents are observable, and we can zero out trampled data exactly as we
do for a memory defined in the module.

This is intentionally coarser than the full condition in the TODO (only
the segments after a trampled segment, up to and including its trampling
segment, need to be provably in-bounds): that would require tracking
which coverage is usable per trampled byte, and the all-in-bounds case
is the one that occurs in practice. The TODO is updated to describe the
remaining refinement, and a test documents the conservative case.

Also removes the dead `maxAddress` computation in `canOptimize` (it has
been unused for a while).

New tests cover: in-bounds trampling now optimized (zero and nonzero
tramplers), a possibly-out-of-bounds trampler still bailing, and the
conservative unrelated-out-of-bounds case.
J
Jacky Li committed
db30c15f6f0a83950c802411ab11ecfe24c8d8df
Parent: 2182234
Committed by GitHub <noreply@github.com> on 7/10/2026, 6:33:14 PM