Skip to content

[Wasm] Emit cold method bodies to an auxiliary module - #138163

Draft
jtschuster wants to merge 20 commits into
dotnet:mainfrom
jtschuster:wasm-aux-bodies
Draft

jtschuster wants to merge 20 commits into
dotnet:mainfrom
jtschuster:wasm-aux-bodies

Conversation

@jtschuster

Copy link
Copy Markdown
Member

Adds crossgen2 support for emitting cold method bodies to an auxiliary module. The set of methods are determined by a .mibc file along with some heuristics. If no profile is provided, all methods go into the primary module. All methods in the .mibc are placed in the primary module when a .mibc file is provided no matter the call count for each. NodeFactory.IsAuxiliaryCandidate() also uses some heuristics to make sure some methods are always in the primary module.

Methods are sorted before passing to the ObjectWriter such that all auxiliary methods come after all primary module methods. The object writer can then split the WasmCode section at the first method symbol where IsAuxiliary == true. Thunks are sorted ahead of MethodWithGCInfo and placed in the primary unconditionally to accomodate this as well. Only MethodWithGCInfo nodes can be placed in the auxiliary module.

Non-default method sorting is not supported with auxiliary methods in this PR. I think we can support it later if necessary.

ActivateModule() is exported from the auxiliary module. It copies the table element at __table_base + <primaryTableSlotCount>. The module contains no data segments.

Perfmap and symbol info for auxiliary modules are emitted into separate files following the same naming pattern as the wasm aux module (<PrimaryModuleName>.1.wasm/<PrimaryModuleName>.1.symbols).

Todo in followups:

  • Auxiliary module downloading and activation in the host.
  • JIT helpers are all kept in the primary module, and auxiliary methods call them indirectly.
  • The entire Type section is duplicated in both the primary and auxiliary modules, even a method signature is only needed in one module.
  • EntryPoints module emission and runtime downloading and reading.

jtschuster and others added 19 commits November 13, 2026 10:43
Remove the temporary runtime-generated metadata fallback now that crossgen2 emits the AuxiliaryModuleMetadata section.

Co-authored-by: Copilot <[email protected]>
- Sort Wasm thunks ahead of compiled methods via CustomSort instead of
  partitioning in the WebCIL object writer, and assert the invariant.
- Emit the sidecar Function and Code sections with
  WasmExternallyCountedSection; rename primary section fields and store
  auxiliary code as a slice.
- Update tests to account for the first compiled method's table slot.

Co-authored-by: Copilot <[email protected]>
@jtschuster jtschuster self-assigned this Nov 23, 2026
@jtschuster jtschuster added arch-wasm WebAssembly architecture area-ReadyToRun labels Nov 23, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/crossgen-contrib
See info in area-owners.md if you want to be subscribed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

@pavelsavara

Copy link
Copy Markdown
Member

@jtschuster I found #138199 problem during my experiments and created jtschuster#108 on top of this PR for it. You can take what makes sense to you and/or close it.

@jtschuster

jtschuster commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

HavitApp size and startup measurements

I built crossgen2 and the browser-wasm packs from this PR at 5db59ea9cba. Then I published the Havit Blazor app from macro-benchmarks (1a2c63431) as a CoreCLR composite R2R build.

Profile: DotNet_Blazor_Wasm.mibc from optimization.browser-wasm.MIBC.Runtime 1.0.0-prerelease.26502.1. That's the version pinned in this repo and in macro-benchmarks. I passed it as --mibc through PublishReadyToRunCrossgen2ExtraArgs, so the SDK doesn't add --partial.

Variants:

  • baseline: no profile, no split
  • profile only: --mibc
  • split: --auxiliary-modules --mibc

Composite R2R module sizes

Variant Raw Brotli Functions
baseline 28.35 MB 4.04 MB
profile only 28.42 MB 4.06 MB
split: primary module 6.86 MB 1.69 MB 4,130
split: aux module (.1.wasm) 24.06 MB 2.48 MB 74,695
split: total 30.93 MB (+8.8% vs profile only) 4.17 MB (+2.6% vs profile only)

Most of the growth is code (+2.37 MB):

  • identical method bodies are no longer merged across the two modules, and
  • every call made from an aux method is indirect.

The primary module's data section also grew by about 140 KB.

Startup measurements

The PR doesn't load the aux module at runtime yet, so I added a local prototype (not part of this PR) to do it:

  • The SDK targets keep <name>.r2r.1.wasm and publish it without a fingerprint, outside the startup assets.
  • After the runtime is ready, the JS loader fetches the module, instantiates it against the shared memory and function table, and calls ActivateModule().

Measured with the macro-benchmarks harness on Playwright Chromium: 10 cold and 5 warm loads per profile. All times are in ms. "TTM" (time to managed code) runs from page script start until the first App component's OnInitializedAsync finishes.

Variant Cold download Desktop TTM cold / warm Desktop walkthrough Mobile TTM cold / warm
baseline 8.54 MiB 363 / 238 2388 4147 / 607
profile only 8.57 MiB 368 / 236 2609 4158 / 596
split + delayed aux load 6.30 MiB 403 / 279 2899 3558 / 755
  • The startup download is 26% smaller.
  • On mobile, where network time dominates, cold TTM is about 600 ms faster.
  • On desktop, cold TTM is about 35–40 ms slower, and warm TTM is slower on both profiles. All 10 desktop cold samples for split (391–420 ms) were slower than all 10 for profile only (360–377 ms).
  • In an earlier run without the delayed load, where the aux module is never loaded and everything outside the profile stays interpreted, the walkthrough took 8173 ms.

Why TTM is slower: startup code lands in the aux module

I enabled DOTNET_WasmPerformanceInstrumentation=* and counted interpreted calls before managed-ready:

Build Interpreted methods Interpreted calls Runtime created → managed ready
profile only 47 62 115 ms
split 528 2,245 231 ms

Methods are placed in the aux module when they aren't in the profile, and the generic Blazor profile doesn't cover Havit's startup path. Of the extra interpreted methods:

  • 249 appear only in the aux module's symbol file, for example Program.Main, WebAssemblyHost.RunAsyncCore, culture resource loading and thread-pool dispatch.
  • 89 have names in both modules.
  • 150 are generic instantiations I couldn't match by name.

Many are framework startup code: the WebAssemblyHostBuilder/Renderer constructors, DI call-site visitors, ConcurrentDictionary.GetOrAdd, and async state machines. Startup finishes before the aux module arrives, so all of this runs interpreted.

Ideas for fixing this (not tested):

  • use an app-specific or broader startup profile, or
  • always keep the call tree reachable from the entry point and host startup in the primary module.

Caveats

  • One measurement run per variant, on a single machine.
  • The delayed-load prototype is local and unreviewed. It doesn't verify integrity, and it currently requests .1.wasm even in builds without an aux module, which returns a 404 after startup.
  • The SDK target _WasmCoreClrPruneStalePerAppR2R deletes the .1.wasm output, so the PR's SDK flow drops the aux module unless that target is changed.
  • The primary module isn't byte-for-byte reproducible: about 33 bytes in a table vary between builds. Builds without --auxiliary-modules vary the same way, so this isn't caused by the split.

Note

This comment was generated with GitHub Copilot from locally collected measurements.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: c60f0ec2-58cf-479f-9e53-fa5a466cfbe8

# Conflicts:
#	src/coreclr/tools/Common/Compiler/ObjectWriter/WebCilObjectWriter.cs

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-ReadyToRun

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants