Repository navigation
[Wasm] Compress ReadyToRun runtime-function tables - #136259
davidwrighton wants to merge 17 commits into
Conversation
Encode grouped runtime functions with GC-info-relative unwind offsets, preserve padded table ordinals, and update native and managed readers. Retain deduplication while sorting metadata by frequency, size, and method order. Co-authored-by: Copilot <[email protected]>
|
Azure Pipelines: Successfully started running 7 pipeline(s). 8 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: @dotnet/crossgen-contrib |
Cluster sparse fields while keeping the virtual-IP base and all offsets in the first 64-byte cache line. Update native layout assertions, emission, managed decoding, and boundary tests together. Co-authored-by: Copilot <[email protected]>
|
Refined the group field order after compression experiments: The selected offsets are 0, 4, 8, 24, 32, 64, and 96. The eight padding bytes remain zero. The base at bytes 0–3 and the complete offset array at bytes 32–63 share the first 64-byte cache line; both mid/low unwind arrays fit in the second line. Screened 720 base-first permutations using table compression, then compared 37 shortlisted/structured layouts using whole-file Brotli 1.2.0, quality 11, generic mode,
Moving both high nibbles and padding to the end was not consistently better: it added 1,644 bytes on each retained image but saved 1,097 bytes on the rebuild. The selected layout improved all three images while preserving lookup locality. Some alternatives split the virtual-IP fields across cache lines and were not preferred for small compression differences. The rebuilt table's logical field bytes and all other ReadyToRun sections exactly match the retained output. There are 326 differing bytes elsewhere in the images after undoing the permutation; Brotli is sensitive to those differences, so the 2,821-byte experimental saving is not presented as the rebuilt implementation's saving. All permutations and Brotli round trips were checked. The untouched pre-change compiler was rerun with the final build's inputs for the updated overall comparison: 20,915,032 → 20,696,904 raw bytes, and 3,809,206 → 3,759,036 Brotli bytes (1.317% smaller). There are still zero non-alignment skips, 16 final padding entries, and zero escapes. The native struct and offset assertions, compiler emitter, managed reader, and existing layout/boundary tests were updated together. The Release Wasm runtime/tools/CoreLib build passed; 129 Wasm tests passed, and 54 native tests passed with 15 expected platform skips. The application was recompiled with the new layout and passed recursive live-GC-root, exception-filter, finally-funclet, and stack-trace checks (50 catches and 50 outer finally blocks). CPU throughput and browser automation were not measured. Note This comment and the field-order refinement were generated with GitHub Copilot. |
Keep the interpreter GC-info encoders and native-hosted cross-JITs, including the Wasm cross-JIT used by crossgen2 and tests. Co-authored-by: Copilot <[email protected]>
Apply the scalar-local pinning contract to object and byref fields of value types so filter and parent frames can safely report shared shadow-stack slots. Document the prior pinning and reporting PRs and add optimized and unoptimized GCInfo regression cases. Co-authored-by: Copilot <[email protected]>
Exclude native JIT function lookup on Wasm and remove its unused end-address macro and implementation. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]> # Conflicts: # src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/R2RTestSuites.cs # src/coreclr/vm/codeman.cpp
Align WASI image buffers for grouped runtime-function tables and validate table slots against padded ordinals. Initialize NativeImage cleanup pointers before throwing initialization and cover interior padding in composite and noncomposite compiler tests. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
cDAC still requires the omitted runtime-function fields and assumes the old layout, preventing it from reading Wasm runtime functions for method lookup and stack walking.
1 open issue
What changed in this PR
This PR reduces metadata size in CoreCLR’s unpublished WebAssembly ReadyToRun format, updating the compiler, runtime, inspection tools, and payload loaders.
Changes:
- Introduces grouped runtime-function encoding and section-relative unwind offsets while preserving function ordinals.
- Reorders deduplicated GC information, adds skip-aware decoding, and aligns loaded payloads to 128 bytes.
- Disables Wasm-hosted compiler targets, fixes pinning of GC fields in value types, and adds regression coverage.
Validation here was limited to source inspection; no builds or tests were run.
| File | Description |
|---|---|
src/native/libs/Common/JavaScript/host/assets.ts |
Aligns Webcil allocations to 128 bytes. |
src/native/corehost/wasihost/wasihost.cpp |
Aligns the WASI placeholder payload. |
src/mono/wasi/build/WasiApp.CoreCLR.targets |
Generates aligned WASI payload buffers. |
src/coreclr/vm/wasm/helpers.cpp |
Unwinds using grouped offsets and the GC-info base. |
src/coreclr/vm/stackwalk.cpp |
Passes the appropriate unwind-info base. |
src/coreclr/vm/readytoruninfo.h |
Stores the Wasm GC-info base. |
src/coreclr/vm/readytoruninfo.cpp |
Validates groups and handles trailing skips. |
src/coreclr/vm/nativeimage.cpp |
Initializes metadata pointers. |
src/coreclr/vm/jitinterface.h |
Excludes native-JIT unwind overrides on Wasm. |
src/coreclr/vm/jitinterface.cpp |
Guards unwind allocation and resolves the new base. |
src/coreclr/vm/datadescriptor/datadescriptor.inc |
Omits flat runtime-function fields on Wasm. |
src/coreclr/vm/codeman.h |
Declares unwind-base access. |
src/coreclr/vm/codeman.cpp |
Adds skip-aware lookup and GC-info addressing. |
src/coreclr/tools/Common/Internal/Runtime/ModuleHeaders.cs |
Defines the managed WasmGCInfo section identifier. |
src/coreclr/tools/Common/Compiler/ObjectWriter/WebCilObjectWriter.cs |
Checks table slots against padded ordinals. |
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWriter.cs |
Inserts matching function-table padding slots. |
src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunReader.cs |
Decodes groups, skips, and unwind escapes. |
src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunMethod.cs |
Parses real entries while skipping padding. |
src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ILCompiler.Reflection.ReadyToRun.csproj |
Exposes reader internals to tests. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRunCodegenNodeFactory.cs |
Registers the Wasm GC-info section. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/RuntimeFunctionsTableNode.cs |
Lays out and emits compressed groups. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/RuntimeFunctionsGCInfoNode.cs |
Orders and caches Wasm GC-info data. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/MethodGCInfoNode.cs |
Calculates encoded block sizes for ordering. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ArrayOfEmbeddedDataNode.cs |
Adds an overridable sorting hook. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCasesRunner/WasmR2RAssert.cs |
Validates group layout and decoder boundaries. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/Webcil/WasmWebcilModule.cs |
Adds object/byref value-type test inputs. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/R2RTestSuites.cs |
Adds padding and GC-field pinning tests. |
src/coreclr/pal/inc/pal.h |
Makes Wasm runtime-function entries opaque. |
src/coreclr/jit/unwindwasm.cpp |
Asserts the minimum virtual-IP span. |
src/coreclr/jit/gcencode.cpp |
Pins Wasm value-type GC fields. |
src/coreclr/jit/CMakeLists.txt |
Disables Wasm-hosted JIT targets. |
src/coreclr/inc/readytorun.h |
Defines the native WasmGCInfo section identifier. |
src/coreclr/inc/crosscomp.h |
Defines opaque cross-target Wasm entries. |
src/coreclr/inc/corcompile.h |
Uses the target runtime-function pointer type. |
src/coreclr/inc/clrnt.h |
Defines grouped layout and native decoding helpers. |
src/coreclr/hosts/corerun/wasm/libCorerun.js |
Aligns browser corerun payload allocations. |
src/coreclr/hosts/corerun/corerun.cpp |
Aligns the embedded WASI payload. |
src/coreclr/gcinfo/CMakeLists.txt |
Preserves interpreter encoding while skipping Wasm-hosted compiler variants. |
Review effort: Balanced
Teach the cDAC ExecutionManager and StackWalk contracts to decode the grouped Wasm ReadyToRun runtime-function format, including padding, funclet bits, escaped unwind offsets, and GC-info-relative unwind data. Expose ReadyToRunInfo::m_gcInfoBase through the data descriptors, update mock fixtures and tests, and update the contract documentation. Co-authored-by: Copilot <[email protected]>
Resolve conflicts with singleton virtual IP elision (dotnet#137465) by reading Wasm unwind data relative to GCInfoBase in the grouped runtime function format. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
…ouped-runtime-functions Co-authored-by: Copilot <[email protected]> # Conflicts: # src/coreclr/inc/readytorun.h # src/coreclr/tools/Common/Internal/Runtime/ModuleHeaders.cs
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>

Summary
Prototype a smaller runtime-function table for the unpublished WebAssembly ReadyToRun format:
WasmGCInfosection.0xF0000.Other architectures keep their existing table representation and metadata ordering. The shared ReadyToRun version is unchanged because Wasm has not shipped.
Wasm build and GCInfo follow-up fixes
Disable Wasm-hosted JITs and their compiler-only GC-info variants. Wasm cannot run these compilers; building the alternate x86 GC-info encoder was the actual CI compilation failure, not insufficient Webcil alignment. Keep the interpreter's GC-info encoder and packaged
gcinfo_unix_wasmlibrary. Native-hosted cross-JITs, including the Wasm cross-JIT used by crossgen2 and tests, remain enabled.Real GCStress validation also exposed a pre-existing omission in Wasm GCInfo: GC fields inside value types were not marked pinned. Scalar locals already followed the pinning contract established by #131374, but the separate struct-field encoding path did not. Mark both object-reference and interior-pointer fields pinned under
TARGET_WASM, and document the reason in the encoder.This matters because #133932 allows Wasm filters to report untracked roots, and #134769 also permits reporting from aborted Wasm frames, relying on those roots being pinned. A filter and its parent can enumerate the same shadow-stack field. In the failing workload, both reported the same interior-pointer field without pinning, triggering the duplicate-root assertion. The GCInfo had eight unique slot definitions, not duplicate encoded slots; the untouched pre-session compiler produced the identical faulty GCInfo bytes.
The fix applies the existing Wasm pinning contract to the missing value-type path; it does not suppress duplicate-reporting checks or change filter/parent enumeration. Optimized and unoptimized regression cases cover both object and byref fields, and both fail without the fix.
Measurements
Baseline: clean pre-session revision
492b4de1f27822eb6783f623b00be7be81f53f3e, built in a separate worktree. After rebuilding the GCInfo fix, the untouched baseline compiler was rerun against the same current inputs as the prototype. Both final compilations use the same Release browser/wasm CoreLib IL, merged MIBC, references, and crossgen2 arguments. The baseline compiler and native JIT are unmodified; the prototype includes the value-type pinning fix, so these are complete current-change measurements, not an isolated table-format comparison..wasmfileBrotli 1.2.0, generic mode, quality 11,
lgwin=22; decompression was checked against each original file. These measurements include the GCInfo fix.The original, retained comparison before field reordering was 3,810,639 baseline bytes versus 3,759,610 prototype bytes (1.339% reduction). Rebuilds changed a small amount of non-table data, so those historical totals are not used to attribute the field-order improvement.
There are 54,608 real entries, zero non-alignment skips, 16 final alignment-padding entries, and zero unwind escapes. Both publication conditions are met: fewer than 24 non-alignment skips and a Brotli reduction strictly greater than 1%.
Group field order
VirtualIPBaseIsFuncletBitsHighBitsOfUnwindOffsetVirtualIPOffsetsMidBitsOfUnwindOffsetLowBitsOfUnwindOffsetThe group remains 128 bytes with 128-byte alignment.
VirtualIPBaseoccupies bytes 0–3 and the entireVirtualIPOffsetsarray occupies bytes 32–63, preserving single-cache-line lookup locality. Padding stays zero; no encoding or ordinal semantics change.Field-order experiments
These controlled experiments preceded the value-type GCInfo fix. Their totals isolate field ordering and are historical; the headline measurements above cover the current implementation including pinning.
Screened all 720 permutations with
VirtualIPBasefirst using table-only compression, then measured 37 shortlisted/structured layouts against the complete.wasmimage. Table-only rankings were not treated as whole-image results. Each permutation was reversed and checked, and all Brotli outputs were decompressed and compared with their inputs.In the retained retail CoreLib table, high unwind nibbles and padding are entirely zero; virtual-IP offsets, funclet bytes, and mid unwind bytes are approximately 79%, 85%, and 81% zero, respectively. The selected layout clusters sparse fields and keeps the denser mid/low unwind arrays together.
The rebuilt source-generated table has exactly the same logical field bytes as the retained table; all other ReadyToRun sections also match. There are 326 differing bytes elsewhere in the complete images after reversing the permutation, and whole-file Brotli decisions are sensitive to those differences. The selected order improves all three controlled images; this is not a claim of a universal optimal permutation.
Moving both high nibbles and padding to the end instead increased Brotli size by 1,644 bytes on each retained image, although it saved 1,097 bytes on the rebuilt image. The selected layout was preferred for its consistent win and lookup locality.
Validation
The browser product build, all 131 Wasm tests, and both GCStress runs below passed again after adding the final explanatory code comment. The native shared-resource tests passed with the GCInfo fix before that comment-only edit.
./build.sh clr.runtime+clr.tools+clr.nativecorelib -os browser -c Release— passed, zero warnings/errors../dotnet.sh test src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/ILCompiler.ReadyToRun.Tests.csproj -c Release /p:TargetOS=browser /p:TargetArchitecture=wasm /p:CoreCLRConfiguration=Release --filter 'FullyQualifiedName~Wasm'— passed, 131 tests, zero failures/skips. Includes both newWasmStructGCFieldsArePinnedcases and grouped-layout/decoder coverage../dotnet.sh test src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/ILCompiler.ReadyToRun.Tests.csproj -c Release /p:TargetOS=linux /p:TargetArchitecture=x64 /p:CoreCLRConfiguration=Release --filter 'FullyQualifiedName~IntrinsicTypeHandlesAreModuleEager|FullyQualifiedName~StaticFieldIntrinsicsDoNotRequireStaticBaseFixups'— passed, 10 tests, zero failures; covers native consumers of the shared test input.WasmStructGCFieldsArePinnedcases were first run without the encoder fix and failed on missing pin flags; both pass with it.node --experimental-wasm-exnrefwithDOTNET_GCStress=0x1and0x3, each withDOTNET_CheckDoubleReporting=1— passed. Each run triggered 8,412 collections in every generation, completed 50 catches and 50 outer finally blocks, and preserved recursive live roots and stack traces. The workload contains no explicitGC.Collect()calls. The same workload failed the duplicate-root assertion before the pinning fix.Main, including its value-type object/byref fields, are pinned after recompilation.artifacts/tools/clang-format --dry-run --Werror src/coreclr/jit/gcencode.cpp src/coreclr/jit/unwindwasm.cppandgit diff --check— passed.Earlier prototype validation, before the build/GCInfo follow-ups:
./build.sh clr.runtime+clr.tools -c Release— passed, zero warnings/errors../dotnet.sh test src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/ILCompiler.ReadyToRun.Tests.csproj -c Release /p:TargetOS=linux /p:TargetArchitecture=x64 /p:CoreCLRConfiguration=Release --filter 'FullyQualifiedName~R2RTestSuites'— passed, 54 tests, 15 expected platform-specific skips, zero failures.node --experimental-wasm-exnref artifacts/bin/coreclr/browser.wasm.Release/corerun.js -c <Release-runtime> -c <browser-libraries> <HelloWorld.dll>— passed.jitformat.pywas retried with the repository SDK onPATH, but its configure step still could not generate the linux/wasm compile-command layout; direct pinned clang-format validation passed. WASI, Windows, and CI were not rerun locally. No CPU-throughput benchmarks or browser automation were run.Note
This draft PR and its implementation were generated with GitHub Copilot.