Skip to content

Break the rematerialization work into ISS-404 through ISS-407, and fix two invalid issue types - #497

Merged
Milky2018 merged 2 commits into
mainfrom
milky/iss-404-407-remat-breakdown
Aug 7, 2026
Merged

Break the rematerialization work into ISS-404 through ISS-407, and fix two invalid issue types#497
Milky2018 merged 2 commits into
mainfrom
milky/iss-404-407-remat-breakdown

Conversation

@Milky2018

@Milky2018 Milky2018 commented Aug 7, 2026

Copy link
Copy Markdown
Owner

ISS-403 (merged in #495) recorded the defect and, after reading Cranelift, the shape of the fix. This splits the work so the ordering is a dependency graph rather than prose. Tracker files only — no code changes.

issue depends on what
ISS-404 Materialization-cost predicate
ISS-405 ISS-404 Rebuild marked values once per using block
ISS-406 ISS-405 Fold the AArch64 EnvironmentField remat into the shared mechanism
ISS-407 Fold constant offsets into load/store displacements

ISS-403 now depends on all four and closes on the observable outcome. ISS-404 and ISS-407 are in the ready queue.

Why ISS-404 exists at all

It is the piece Cranelift structurally lacks. Their remat rules live in the target-independent mid-end, so they cannot consult a cost — which is why remat.isle marks every iconst, f32const and f64const regardless of what materializing one actually costs on the target.

Measured on AArch64, against the 1-instruction reload it would replace:

value instructions
5, 1020, 65535, 65536 1
4294967295, 305419896 2
81985529216486895 4
f64.const 3.14159 5

At one instruction, rebuilding matches the reload and additionally saves the spill store, the stack slot and the cross-block live range — a strict win. At four or five it is several times the reload. The measured fixture's addresses are all in the 1-instruction band, so a cost-gated rule captures the full 2× while staying safe on wide constants.

The first cut is conservative and target-independent (single-movz form, also one instruction on x64), with a cost-callback seam left for the caller that already knows the target. Wasmoon has an advantage here that Cranelift does not: optimize_with_level is a plain function call from a JIT that already resolved the target, where Cranelift's ISLE rules are compiled ahead of time.

Two non-goals, recorded rather than left implicit

Both are places where copying Cranelift would be wrong here:

  • The ALU-with-one-constant-operand rules. Rematerializing y = a + k swaps which value is live rather than reducing the count, and since remat does not recurse, it can move the long live range onto a instead. Cranelift's own comment only claims "neutral" for this case.
  • Float and vector constant remat. Five instructions on AArch64 against a four-byte reload.

Also worth noting for whoever picks this up: Cranelift's saturating_sub(1) LICM hoist-level default is a magic constant compensating for the missing cost model — the comment says as much, citing vconst not being easily rematerialized. With a cost predicate that special case is unnecessary.

Second commit: two invalid issue types

ISS-398 and ISS-399 were typed refactor, which the tracker does not define — its five types are bug, feature, task, epic and chore, and task explicitly covers "implementation, tests, docs, or refactoring work".

The effect was a warning on every index regeneration plus a bogus refactor entry in the type column for ISS-399, which is open and therefore listed. ISS-398 is closed, so it only appeared in the warnings. Warnings are now empty, and a sweep over the other 402 issue files finds no further invalid types.

Carried on this branch rather than its own: both changes rewrite the derived issues/README.md, and separate PRs would conflict the moment either merges.

ISS-403 recorded the defect and, after reading Cranelift, the shape of
the fix. This splits the work so the ordering is a dependency graph
rather than prose.

- ISS-404 adds the materialization-cost predicate. This is the piece
  Cranelift structurally lacks: its remat rules sit in the
  target-independent mid-end and so cannot consult a cost, which is why
  they mark every iconst and float const regardless of what materializing
  one costs. Measured on AArch64, a constant costs 1 instruction in the
  single-movz form, 2 or 4 for wider values, and 5 for an f64 — against 1
  for the reload it would replace. The first cut is deliberately
  conservative and target-independent, with a cost callback seam left for
  the caller that already knows the target.
- ISS-405 is the change that fixes the measured doubling: rebuild marked
  values once per using block instead of reusing a dominating definition.
  Depends on ISS-404.
- ISS-406 folds the AArch64-only EnvironmentField remat into that shared
  mechanism so future targets do not reimplement it. Depends on ISS-405.
- ISS-407 folds constant offsets into load and store displacements. No
  dependency; adjacent to ISS-405 and measured separately so the two
  effects stay distinguishable.

ISS-403 now depends on all four and closes on the observable outcome.

Two things are recorded as non-goals rather than left implicit, both
places where copying Cranelift would be wrong here: the
ALU-with-one-constant-operand remat rules, which swap which value is live
rather than reducing the count, and float and vector constant remat,
which loses to a four-byte reload at five instructions on AArch64.
@Milky2018
Milky2018 enabled auto-merge August 7, 2026 07:11
@Milky2018
Milky2018 disabled auto-merge August 7, 2026 07:13
Both were typed `refactor`, which the tracker does not define. Its five
types are bug, feature, task, epic and chore, and `task` explicitly
covers "implementation, tests, docs, or refactoring work", so that is
where these belong.

The effect was a warning on every index regeneration and a bogus
`refactor` entry in the type column for ISS-399, which is open and
therefore listed. ISS-398 is closed, so it only ever showed up in the
warnings.

Warnings are now empty, and a sweep over the other 402 issue files finds
no further invalid types.
@Milky2018 Milky2018 changed the title Break the rematerialization work into ISS-404 through ISS-407 Break the rematerialization work into ISS-404 through ISS-407, and fix two invalid issue types Aug 7, 2026
@Milky2018
Milky2018 enabled auto-merge August 7, 2026 07:15
@Milky2018
Milky2018 merged commit 31030d8 into main Aug 7, 2026
2 checks passed
This was referenced Aug 11, 2026
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.

1 participant