Skip to content

[browser] Bundle composite R2R WebCIL IL components into a single module - #137713

Open
davidwrighton wants to merge 6 commits into
dotnet:mainfrom
davidwrighton:ConsolidatedWebCIL_ILBinarySupport
Open

davidwrighton wants to merge 6 commits into
dotnet:mainfrom
davidwrighton:ConsolidatedWebCIL_ILBinarySupport

Conversation

@davidwrighton

@davidwrighton davidwrighton commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Browser CoreCLR composite ReadyToRun publishing currently produces one runtime module, one composite R2R code module, and a separate self-installing WebCIL module for every IL assembly (~65 for the Havit Blazor documentation app). Each of those IL modules is fetched, compiled, and instantiated individually during startup.

This change makes crossgen2 package all composite IL components into one WebCIL wrapper (<app>.components.wasm), which the host instantiates once. A normal browser composite publish now loads two compiler-produced modules: the R2R code module and the IL bundle.

Changes

  • Format (docs/design/mono/webcil.md): a version 2 self-installing wrapper (tableSize 0) whose payload is a WbIB bundle. It has a header, a directory, and a name table, followed by 16-byte-aligned, independent WebCIL v1 component images.
  • crossgen2: new --webcil-component-bundle:<path> option. In composite WebCIL mode it writes the component images into a single bundle (WebcilBundleWriter) instead of one wrapper per assembly. Default output without the option is unchanged.
  • Browser host (src/native/libs/Common/JavaScript/host): the boot asset is marked isWebcilBundle. The host instantiates it once into one aligned allocation and validates the directory. It then registers each image as an (address, size) view in the TPA without copying. INTERNAL.getLoadedAssemblyBytes(path) returns a copy of a single assembly view for consumers such as debugger tooling.
  • corerun (browser): probes for and installs the bundle, then registers its entries, so runtime tests run against bundled output.
  • WebAssembly SDK pack:
    • Bundling is on by default for browser composite publishing. Opt out with WasmBundleCompositeComponents=false.
    • The publish state is stamped, so changes to inputs, options, or mode invalidate ReadyToRun outputs and stale files are cleaned. First-class tracking of the bundle as a compiler output is in Track additional ReadyToRun compiler outputs for incremental builds sdk#57255 and will be wired into the runtime targets and task shim separately.
    • Boot JSON carries the bundle asset.
    • Lazy-loaded assemblies (BlazorWebAssemblyLazyLoad) are excluded from the composite and compiled as non-composite R2R. Publishing fails if a composite assembly references a lazy assembly directly (CompositeLazyAssemblies), because composite images assume all of their component IL is present together.
    • WebcilBundleReader lets pack tasks read the bundle's entries.
  • WASI keeps its existing per-component stub flow.
  • Tests:
    • src/tests/Common/CLRTest.CrossGen.targets stages the bundle for browser composite runtime tests.
    • Wasm.Build.Tests: format and malformed-input coverage (WebcilInWasmSizesTests). Also browser execution, incremental publish, opt-out, mode switches, workload relink with wasm-opt, lazy loading, satellites, and debug levels.

Measurements (Havit Blazor documentation, local headless Chrome)

Median time to reach managed code; 10 cold and 5 warm runs; same source revision:

Profile Before (cold) Bundle (cold) Before (warm) Bundle (warm)
Desktop 1419 ms 1163 ms 953 ms 772 ms
Mobile (emulated) 6284 ms 5992 ms 2442 ms 2235 ms

Cold requests fell from 97 to 33 and cold transfer from about 9.00 MB to 8.78 MB. Startup-instantiated Wasm modules went from 67 to 3. Resource satellites remain separate.

Validation

  • ./build.sh clr.native+clr.tools+host.native -os browser -arch wasm -c Release /p:RuntimeFlavor=CoreCLR: passed.
  • ./build.sh tasks -c Release: passed.
  • ./dotnet.sh build src/mono/wasm/Wasm.Build.Tests/Wasm.Build.Tests.csproj -c Release -t:Test -p:TargetOS=browser -p:TargetArchitecture=wasm -p:RuntimeFlavor=CoreCLR -p:TestUsingWorkloads=false -p:XUnitClassName=Wasm.Build.Tests.WebcilInWasmSizesTests: 33 passed, 0 failed (after rebasing onto current main).
  • Before the rebase, the browser Wasm.Build.Tests scenarios above passed locally. These covered stock-SDK and workload publish, incremental cases, opt-out, and lazy loading. Browser corerun composite smoke tests also passed, including negative cases for missing or corrupt bundles.

Known limitations / follow-ups

  • The owner-image invalidation workaround has been removed. Track additional ReadyToRun compiler outputs for incremental builds sdk#57255 adds the AdditionalOutputs contract; the runtime targets and in-tree Crossgen2Tasks shim still need to set the bundle as an extra output. Until that support reaches the stock SDK, a missing bundle by itself may not trigger a rebuild in the explicit stock-SDK path. The browser composite bundle has not shipped, so this temporary gap is not a shipped-version compatibility constraint.
  • getLoadedAssemblyBytes exposes per-assembly bytes, but no CoreCLR browser debugger consumer has been integrated yet.
  • WASI host composition and the generated Windows batch wrappers for runtime tests were not executed.

Note

This PR description was generated with GitHub Copilot.

Emit a single IL component bundle, register per-assembly views in browser and corerun hosts, and integrate default-on bundling with lazy assembly validation and incremental publishing.

Co-authored-by: Copilot <[email protected]>
@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.

- Drop MSBuild task output metadata from the build-time bundle asset so
  AttachWebcilSizes reads the bundle rather than the project file.
- Skip publish boot JSON generation in the nested publish; it otherwise
  writes an incomplete boot config and stamp that the outer publish reuses.
- Avoid escaped quotes when writing the crossgen2 bundle option in
  generated runtime test scripts.
- Wasm.Build.Tests: accept fingerprinted files whose superseded version
  was removed, and assert composite R2R output for partial-profile publish.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Default-on bundling spans compiler, SDK publishing and cleanup, and browser-host contracts, requiring human validation of unverified post-rebase browser, incremental mode-switch, and Windows-wrapper paths.

No open issues 🎉

What changed in this PR

This PR bundles browser CoreCLR composite ReadyToRun IL components into one WebCIL module, reducing startup requests and module instantiations. It connects crossgen2 output with browser loading, SDK publishing, and runtime tests while preserving WASI’s existing component flow.

Changes:

  • Adds the bundle format, crossgen2 option, and payload-only component emission.
  • Registers bundled assemblies without copying their images and exposes individual byte copies for tooling.
  • Enables bundling by default for browser composite publishing, with incremental tracking, opt-out, lazy-loading safeguards, and regression coverage.
File Description
src/​tests/​Loader/​TrustedPlatformAssembliesProperty/​TrustedPlatformAssemblies.cs Checks browser ReadyToRun assembly location behavior.
src/​tests/​Loader/​ExternalAssemblyProbe/​ExternalAssemblyProbe.cs Adds browser external-probe coverage.
src/​tests/​Common/​CLRTest.CrossGen.targets Generates and stages bundles for composite runtime tests.
src/​tasks/​Microsoft.NET.WebAssembly.Webcil/​WebcilWasmWrapper.cs Corrects payload alignment across encoded-length boundaries.
src/​tasks/​Microsoft.NET.WebAssembly.Webcil/​WebcilReader.cs Resets the stream before reading unwrapped payloads.
src/​tasks/​Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/​WebcilBundleReader.cs Validates bundles and matches component assembly identifiers.
src/​tasks/​Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/​GenerateWasmBootJson.cs Classifies bundle assets in boot configuration.
src/​tasks/​Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/​FilterWebcilBundleComponents.cs Filters bundled assemblies from standalone asset candidates.
src/​tasks/​Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/​ConvertDllsToWebCil.cs Skips bundled components and validates separate lazy images.
src/​tasks/​Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/​CompositeLazyAssemblies.cs Excludes lazy assemblies and rejects direct composite references.
src/​tasks/​Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/​BootJsonData.cs Adds the boot asset bundle flag.
src/​tasks/​Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/​BootJsonBuilderHelper.cs Propagates bundle flags into generated assets.
src/​tasks/​Crossgen2Tasks/​Microsoft.NET.CrossGen.targets Passes composite-specific compiler arguments.
src/​native/​libs/​Common/​JavaScript/​types/​public-api.ts Describes bundle assets in public TypeScript types.
src/​native/​libs/​Common/​JavaScript/​types/​internal.ts Adds internal bundle asset metadata.
src/​native/​libs/​Common/​JavaScript/​loader/​assets.ts Forwards bundle metadata and preserves bundle paths.
src/​native/​libs/​Common/​JavaScript/​host/​index.ts Exposes loaded assembly byte retrieval.
src/​native/​libs/​Common/​JavaScript/​host/​host.ts Includes bundle entries in trusted assembly paths.
src/​native/​libs/​Common/​JavaScript/​host/​assets.ts Validates and registers bundle views; returns byte copies.
src/​mono/​wasm/​Wasm.Build.Tests/​WebcilInWasmSizesTests.cs Covers alignment, bundle validation, and lazy-reference checks.
src/​mono/​wasm/​Wasm.Build.Tests/​Wasm.Build.Tests.csproj References ReadyToRun inspection helpers.
src/​mono/​wasm/​Wasm.Build.Tests/​SatelliteLoadingTests.cs Exercises satellite loading with composite bundles.
src/​mono/​wasm/​Wasm.Build.Tests/​ReadyToRunTests.cs Covers publishing, incremental rebuilds, mode switches, and execution.
src/​mono/​wasm/​Wasm.Build.Tests/​ProjectProviderBase.cs Updates fingerprint tracking and bundle-aware boot assertions.
src/​mono/​wasm/​Wasm.Build.Tests/​LazyLoadingTests.cs Verifies separate lazy images and invalid-reference rejection.
src/​mono/​wasm/​Wasm.Build.Tests/​DebugLevelTests.cs Checks stack-trace source information with bundles.
src/​mono/​nuget/​Microsoft.NET.Sdk.WebAssembly.Pack/​build/​Microsoft.NET.Sdk.WebAssembly.Browser.targets Stages bundle assets and tracks publish cleanup.
src/​mono/​nuget/​Microsoft.NET.Sdk.WebAssembly.Pack/​build/​Microsoft.NET.Sdk.WebAssembly.Browser.CoreCLR.targets Enables bundling and manages exclusions and rebuild invalidation.
src/​mono/​browser/​build/​BrowserWasmApp.CoreCLR.targets Returns bundle outputs from nested publishing.
src/​coreclr/​tools/​Common/​Compiler/​ObjectWriter/​WebCilObjectWriter.cs Adds payload-only emission for bundled components.
src/​coreclr/​tools/​Common/​Compiler/​ObjectWriter/​WebcilBundleWriter.cs Writes aligned component images into a single wrapper.
src/​coreclr/​tools/​Common/​Compiler/​ObjectWriter/​Wasm/​WebcilPayloadDataSegment.cs Separates raw payload emission from segment encoding.
src/​coreclr/​tools/​aot/​ILCompiler.ReadyToRun/​ILCompiler.ReadyToRun.csproj Includes bundle-writing and wrapper sources.
src/​coreclr/​tools/​aot/​ILCompiler.ReadyToRun/​Compiler/​ReadyToRunCodegenCompilationBuilder.cs Carries the bundle output setting into compilation.
src/​coreclr/​tools/​aot/​ILCompiler.ReadyToRun/​Compiler/​ReadyToRunCodegenCompilation.cs Collects rewritten component payloads into the bundle.
src/​coreclr/​tools/​aot/​crossgen2/​Properties/​Resources.resx Adds bundle option help and validation messages.
src/​coreclr/​tools/​aot/​crossgen2/​Program.cs Validates bundle mode and output path collisions.
src/​coreclr/​tools/​aot/​crossgen2/​Crossgen2RootCommand.cs Registers the bundle command-line option.
src/​coreclr/​hosts/​corerun/​wasm/​libCorerun.js Discovers, installs, and probes bundled assemblies.
src/​coreclr/​hosts/​corerun/​corerun.cpp Integrates bundle paths into browser assembly resolution.
docs/​design/​mono/​webcil.md Documents the bundle layout and loading contract.

Review effort: Balanced

@davidwrighton
davidwrighton marked this pull request as ready for review November 17, 2026 22:03
@pavelsavara pavelsavara added arch-wasm WebAssembly architecture os-browser Browser variant of arch-wasm labels Nov 18, 2026
@pavelsavara pavelsavara added this to the 12.0.0 milestone Nov 18, 2026
@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
pavelsavara self-requested a review November 18, 2026 09:52
…datedWebCIL_ILBinarySupport

Resolve the Crossgen2 task invocation conflict by keeping main's instruction-set
arguments and the composite-only argument plumbing. Make the composite argument
property private (_PublishReadyToRunCrossgen2CompositeExtraArgs) and include it in
the ReadyToRun compiler-argument fingerprint.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 96e51893-de9d-4081-a1ad-dc7f5ed33991
Rely on ReadyToRun output tracking for the composite bundle instead of
invalidating the owner image through a separate target.

Co-authored-by: Copilot <[email protected]>

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-Host os-browser Browser variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants