Repository navigation
[wasm] Forward single-use local copies before stackification - #136729
Conversation
Morph creates temps for call arguments with side effects (e.g. runtime lookups feeding CORINFO_HELP_VIRTUAL_FUNC_PTR). After CSE replaces those values with CSE locals, each temp is left as a single-use copy of another local. The WASM register allocator gives every candidate its own wasm local and does not coalesce, so these copies cost a local.get/local.set pair and an extra local each. Add a lowering pass that rewrites STORE_LCL_VAR<tmp>(LCL_VAR<src>) ... LCL_VAR<tmp> into a direct use of src when tmp has no other references and src is not redefined in between. Contributes to dotnet#136662. Co-authored-by: Copilot App <[email protected]>
LCLHEAP updates the shadow stack pointer without an IR local definition, so a copy of it can't be safely replaced by a later read. Co-authored-by: Copilot App <[email protected]>
The SP local is implicitly referenced until register allocation, so the lvImplicitlyReferenced check already covers it. Co-authored-by: Copilot App <[email protected]>
|
Azure Pipelines: Successfully started running 6 pipeline(s). 9 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
|
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara |
Only walk blocks that contain a local-to-local copy (returning early when none do), and only track copies whose temp's single use is in the same block, so pending copies don't linger to the end of the block. Co-authored-by: Copilot App <[email protected]>
AndyAyersMS
left a comment
There was a problem hiding this comment.
If we do something like this, we should take advantage of tracked locals / liveness / local ref counts, instead of recomputing the information from scratch.
But we should also consider fixing this by implementing a better register allocator and/or adding coalescing there.
Yeah, Adam is looking into the RA improvements, this was to see if we could improve things now with a more targeted approach. I'm happy to update this or wait for the improve RA work. |
Instead of recounting local references in an extra walk over all nodes, lowering records the blocks that contain local-to-local copies, and a new wasm phase after late liveness forwards copies in just those blocks using the exact ref counts and last-use flags that liveness computes. Last-use flags are kept up to date when a copy is forwarded. Co-authored-by: Copilot App <[email protected]>
|
@AndyAyersMS Reworked per your suggestion: lowering now only records blocks that contain local-to-local copies (in Note This comment was generated by GitHub Copilot (AI-generated content). |
|
Can we remeasure this? We have talked elsewhere about doing a "late forward sub" which might be more general. |
|
@AndyAyersMS Remeasured after the rework (CI build 1660595): wasm TP is now +0.06% (was +0.71% for the original lowering pass), with -17,273 bytes / 2,318 improvements / 0 regressions on the wasm libraries collection. I merged main to rerun against tip, but the JIT-EE GUID bump in #135553 means there are no SPMI collections yet; I'll rerun once they exist. A late forward sub sounds like the more general home for this; if that's planned I'm happy to close this in favor of it, or keep it as a stopgap until then. Note This comment was generated by GitHub Copilot (AI-generated content). |
|
/azp run runtime-coreclr superpmi-diffs |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
@AndyAyersMS Reran superpmi-diffs against current main with the new collections (1678019). wasm TP is +0.05% on libraries and +0.02% on coreclr_tests. Asm diffs: libraries −28,227 bytes (3,415 improved, 0 regressed); coreclr_tests −241,308 bytes (7,407 improved, 3 regressed). The 3 regressions are the same method ( Note This comment was generated by GitHub Copilot (AI-generated content). |
Drop the lvHasLdAddrOp checks, which reflect IL address-taken state that is no longer relevant after local morph; address exposure and LCL_ADDR nodes in the walk cover the remaining cases. Use a range-based for over the recorded blocks. Co-authored-by: Copilot App <[email protected]>
When two pending copies share a source local and the first is forwarded, the forwarded use becomes the source's latest read. Record it as the last use for the other pending copies so that forwarding them moves the death flag off it; otherwise the source was marked dead before a later read, miscompiling JIT/Directed/cmov/Int_Or_Op on wasm R2R. Co-authored-by: Copilot App <[email protected]>
Cap the number of copies tracked at once so the per-node scans stay bounded and the phase is linear in the size of the visited blocks. A cap of 256 covers calls with very many arguments (one pending copy each) without losing any forwarding on the wasm SuperPMI collections. Co-authored-by: Copilot App <[email protected]>
|
Can we re-measure now that #137473 is in? |
|
@AndyAyersMS Remeasured with #137473 merged (superpmi-diffs 1686035):
No diffs on other targets. Since the last measurement I also fixed a last-use tracking bug when two pending copies share a source (caught by Note This comment was generated by GitHub Copilot (AI-generated content). |
|
I think this looks pretty good. @adamperlin can you take a look too? |
|
I also think this looks pretty good. If we ever add coalescing support to Wasm RA then we might not need this phase, but I wasn't planning for that in a first iteration of the new RA so this seems beneficial overall. |
|
/ba-g infra failure is not related to wasm only change |
…36662 # Conflicts: # src/coreclr/jit/compiler.h # src/coreclr/jit/fgwasm.cpp
|
@AndyAyersMS had to resolve a merge conflict, please reapprove when you get a chance |
|
/ba-g stuck ios-arm64 Release AllSubsets_CoreCLR_Smoke is not related, manually verified all other helix jobs completed with only known issues |
Statement splitting (inlining, late cast/runtime lookup/static init expansion), CSE and morph can leave ``` STORE_LCL_VAR<tmp>(V) ... LCL_VAR<tmp> ``` where `V` is a constant or an invariant, non-faulting load of an invariant address, such as a ReadyToRun handle. On Wasm each such temp costs a `local.set`/`local.get` pair and a Wasm local, since the Wasm register allocator gives every candidate its own local and nothing downstream (R2R output is not run through `wasm-opt`) removes them. This extends the Wasm forward-local-copies phase from #136729 to move `V` to the single use of `tmp` instead: - Lowering records blocks containing stores of such values. - After late liveness, `tmp` must have exactly one def and one use, be tracked, non-GC, non-small and not address exposed. - The def and use may be in different blocks (late helper expansions split the block). `tmp` must not be live into the method entry or any handler entry, so the single def reaches the use on every path; this also keeps values from moving into funclets. - Uses in blocks with a higher weight than the def are skipped, so that loop-hoisted values stay hoisted. The phase only runs for Wasm. ## Results Wasm SuperPMI (`coreclr_tests` crossgen2 collection, browser/x64, base `upstream/main`): | | Delta | |---|---| | Total bytes | -73,008 (-0.02%) | | Contexts with diffs | 9,415 (9,376 improvements, 0 regressions, 39 same size) | Blazor Havit documentation app, R2R composite (65 assemblies), same crossgen2, only the JIT differs: | | Base | Diff | Delta | |---|---|---|---| | Code section | 21,879,495 | 21,850,009 | -29,486 (-0.13%) | | Functions smaller / larger | | | 5,385 / 0 | | Declared Wasm locals | | | -5,819 | Throughput (non-PGO Release JITs, same collection): +0.04% overall. MinOpts also shows +0.04% although the new code does not run there, so most of this is noise; the largest FullOpts per-method increase is +1.31%. Example (`b80045.AA:Static2`, 69 → 65 bytes, one fewer local): ```diff memory.fill 0 + local.get 0 global.get 4331048 i32.load 0 reloc 0x422f98 - local.set 3 ;; V05 - local.get 0 - local.get 3 ;; V05 i32.store 0 4 ``` ## Validation - `.\build.cmd clr.crossarchtools -os browser -arch wasm -c Checked /p:RuntimeFlavor=CoreCLR` (and Release): passed. - `python src\coreclr\scripts\superpmi.py asmdiffs -target_os browser -target_arch wasm -arch x64 -mch_arch x64 ...` on the `coreclr_tests` collection with a Checked JIT: 209,043 contexts replayed, no asserts or failures (12 contexts missing data with both base and diff; method context 37882 was excluded because it hits a pre-existing CRT abort with the base JIT). - Not run: Wasm runtime/library tests in a browser. Contributes to #135985 > [!NOTE] > This PR description was generated with GitHub Copilot. --------- Co-authored-by: Copilot <[email protected]> Copilot-Session: bfdc6001-97e4-4f7a-9de1-f9a04223c8ff
Morph spills side-effecting call arguments to temps, e.g. the generic-handle lookups feeding
CORINFO_HELP_VIRTUAL_FUNC_PTR. CSE then replaces those lookups with CSE locals, leaving single-usetmp = cseNcopies. On other targets LSRA preferencing usually coalesces such copies. The wasm RA gives every candidate its own wasm local, so each copy costs alocal.get/local.setpair and an extra local. R2R output isn't run throughwasm-optby default, so nothing downstream removes them.Wasm lowering records which blocks contain a
STORE_LCL_VAR<tmp>(LCL_VAR<src>)copy. A new wasm phase, run right after late liveness, visits only those blocks. It uses the exact ref counts and last-use flags from liveness to rewrite... LCL_VAR<tmp>into a direct use ofsrcwhen:tmphas exactly that one def and one use, in the same block;srcisn't redefined in between, address-taken, or implicitly referenced;The last-use flag on
srcis moved as needed. It doesn't change the wasm RA.SpanHelpers.IndexOfAny<__Canon>(ref T, T, T, int), before → after:Local SuperPMI asmdiffs on the wasm
libraries.crossgen2collection: -17,273 bytes, 2,318 contexts improved, 0 regressed.Resolves #136662
Note
This PR description was generated by GitHub Copilot (AI-generated content).