Skip to content

Cross-build browser WASM performance host tools - #133143

Merged
lewing merged 1 commit into
mainfrom
lewing-portable-wasm-crossgen
Sep 3, 2026
Merged

Cross-build browser WASM performance host tools#133143
lewing merged 1 commit into
mainfrom
lewing-portable-wasm-crossgen

Conversation

@lewing

@lewing lewing commented Sep 3, 2026

Copy link
Copy Markdown
Member

Summary

  • add an opt-in CrossBuildHostTools property that defaults to false
  • use it only for the host cross-component CoreCLR child build
  • enable it for the browser CoreCLR performance build so the universal WASM JIT uses the existing Bionic sysroot
  • leave the actual browser runtime build, normal browser builds, Mono, and mobile platforms unchanged

Validation

  • evaluated ProjectToBuild with the repository SDK and confirmed the default host child remains CrossBuild=false
  • confirmed the performance opt-in changes only the x64/Linux host child to CrossBuild=true, with ClrWasmJitSubset=true and CLR_CROSS_COMPONENTS_BUILD=1
  • confirmed the top-level browser runtime build keeps CrossBuild unset
  • confirmed Android and Mono continue to exclude the host Crossgen2 package
  • confirmed the targeted clr.crossarchtools+clr.tools+packs graph includes the cross-built WASM JIT and host Crossgen2 pack
  • parsed the modified YAML and ran git diff --check

Native GLIBC dependency inspection was not run locally because the development workspace is macOS without the Linux webassembly container. A runtime-wasm-perf run is required to verify the packaged JIT against Ubuntu 22.04 end to end.

Note

This pull request description was generated with GitHub Copilot.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 3, 2026 01:37
@lewing
lewing requested a review from akoeplinger as a code owner September 3, 2026 01:37
@azure-pipelines

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

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

This changes cross-build behavior in the build graph and performance pipeline; it needs end-to-end CI validation to confirm it packages and runs correctly across the intended Linux environments.

Pull request overview

Adds an opt-in MSBuild property to control whether the CoreCLR host cross-components child build is invoked with CrossBuild=true, and enables that opt-in for the browser WASM performance build job to allow cross-building the host toolset in that scenario.

Changes:

  • Introduced CrossBuildHostTools (defaulting to false) to gate cross-building of the host cross-components build.
  • Switched the CoreCLR cross-tools runtime.proj child invocation from CrossBuild=false to CrossBuild=$(CrossBuildHostTools).
  • Enabled /p:CrossBuildHostTools=true for the browser_wasm CoreCLR perf build job.
File summaries
File Description
eng/Subsets.props Adds CrossBuildHostTools and uses it to control CrossBuild for the CoreCLR host cross-components child build.
eng/pipelines/performance/templates/perf-wasm-build-jobs.yml Enables CrossBuildHostTools for the browser WASM CoreCLR perf build.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

@lewing

lewing commented Sep 3, 2026

Copy link
Copy Markdown
Member Author

@lewing

lewing commented Sep 3, 2026

Copy link
Copy Markdown
Member Author

/ba0g the sos failures are tracked in #133175

@lewing

lewing commented Sep 3, 2026

Copy link
Copy Markdown
Member Author

/ba-g the sos failures are tracked in #133175

@lewing
lewing merged commit 6981024 into main Sep 3, 2026
184 of 190 checks passed
@lewing
lewing deleted the lewing-portable-wasm-crossgen branch September 3, 2026 16:12
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 4, 2026
radekdoulik added a commit that referenced this pull request Sep 4, 2026
…stem (#131877)

Replaces the hardcoded struct-size table in the CoreCLR wasm P/Invoke
generator with crossgen2's real field-layout engine.

## The problem

`ManagedToNativeGenerator` computed wasm ABI signature strings from
`System.Reflection.MetadataLoadContext`, which has no field-layout
engine. Struct sizes came from a 7-entry hardcoded table, and anything
outside it was a hard build error:

```
error WASM0067: SignatureMapper: unknown multi-field struct 'X' (fields: N)
- add its size to s_knownStructSizes in SignatureMapper.cs
```

Size matters because the CoreCLR interpreter lays struct arguments out
inline across 8-byte slots — `TokenToSlotCount` returns `max((size + 7)
/ 8, 1)` for an `S<N>` token. A wrong `N` misaligns the interpreter
frame.

Mono's generator needs none of this: its alphabet has no `S`, and it
encodes every struct as a pointer.

## The change

crossgen2 gains `--generate-portable-callhelpers <dir>`, which writes
the three C++ call-helper files directly. It sets up its type system as
for a real wasm compilation, scans the input assemblies and emits — no
JIT, no R2R image. The option requires `--targetarch wasm` with
`--targetos browser|wasi`.

The CoreCLR half of the MSBuild task is deleted rather than adapted:
`ManagedToNativeGenerator`, `PInvokeCollector`, `PInvokeTableGenerator`,
`SignatureMapper`, `InternalCallSignatureCollector`,
`InterpToNativeGenerator`. `_CoreCLRGenerateManagedToNative` keeps its
name and position in the target graph; its final step changes from
`<UsingTask>` to `<Exec>`. The regeneration scripts move next to their
output under `src/coreclr/vm/wasm/` and drive
`generate-coreclr-helpers.proj`. Mono's generator is untouched.

**−2269 lines under `src/tasks`, +1541 under
`ILCompiler.ReadyToRun/PortableCallHelpers`.** A move, not an addition:
the second implementation of wasm ABI lowering is gone, and the one that
remains is the one the JIT interface itself calls. Sizes are computed,
not enumerated. The only change to `WasmLowering` is widening
`WasmValueTypeToSigChar` from `private` to `internal`.

### Naming

Portable entry points exist for any platform that cannot generate code
at run time; wasm is the only one today. Per [review
feedback](#131877)
nothing in this functionality is named after wasm. Symbols shared by the
runtime and the generated tables were renamed on both sides at once:

| before | after |
|---|---|
| `StringToWasmSigThunk` | `StringToPortableSigThunk` |
| `g_wasmThunks[Count]` | `g_portableCallHelperThunks[Count]` |
| `wasm_ret_S<n>` | `portable_callhelper_ret_S<n>` |

What keeps wasm in its name is what is genuinely about wasm: the ABI in
`WasmLowering`, the `--targetos browser|wasi` requirement, and the
wasm-specific corerun the runtime tests link.

### Finding crossgen2

Three paths, tried in order:

- **Override** — `$(PortableCallHelpersGeneratorPath)`, which must name
a crossgen2 executable.
- **In repo** — `$(Crossgen2InBuildDir)`; crossgen2 is built
unconditionally by the `clr` subset.
- **Out of repo** — the `wasm-tools` workload declares the existing
`Microsoft.NETCore.App.Crossgen2.<host-rid>` pack, whose `Sdk/Sdk.props`
defines `$(Crossgen2ToolPath)`. ~12.5 MB.

The SDK resolves this pack only when `PublishReadyToRun` is set, which
wasm CoreCLR apps never set — hence the workload. dotnet/sdk#56119
proposes acquiring it directly instead, which would let the workload
entry go. If none of the three resolve, the targets error rather than
passing an empty path down.

The pack is named for the machine that *runs* crossgen2, not the target:
generation never loads the JIT, so a host-targeting crossgen2 answers
wasm ABI questions correctly.

The workload-testing legs do not set `$(BuildHostTools)`, so nothing
produced a crossgen2 pack for their local package feed. (The perf
browser-wasm leg does produce one, but only because it opts in —
#133143.) `Microsoft.NETCore.App.Crossgen2.Host.sfxproj` pins the RID to
the build host, and is now built by the CoreCLR browser-wasm leg behind
`$(BuildCrossgen2HostPackForWorkloadTesting)`, guarded on
`$(BuildHostTools)` being unset so the two paths can never emit the same
package id twice. The official build is untouched — it already publishes
this pack from the host platform legs.

## Behaviour changes

**`WASM0066` is removed.** The old task warned for every `DllImport`
whose module did not resolve to a linked-in native library — a
CoreCLR-only divergence that fires on ordinary cross-platform code never
executed on wasm (#131874 reports ten from SkiaSharp alone on a shipped
Preview 7 SDK). In-tree it had already accumulated two `NoWarn`
suppressions and a `WarnOnUnresolvedPInvokeModules=false`; all three go,
along with the `--no-warn-unresolved-directpinvoke` opt-out that existed
only to silence it. An unresolved module is not knowably wrong at build
time: `callhelpers_pinvoke_override` returns `nullptr` on a miss, so a
call that actually happens throws `DllNotFoundException` naming the
module, as on every other platform. Dropping a warning is strictly
loosening.

**`WASM0065` is added, as a message.** Per module, when it declares
P/Invokes without `[assembly: DisableRuntimeMarshalling]`, since the
generated helpers assume signatures cross unmarshalled. A message rather
than a warning: it reports something the app author often cannot fix,
and as a warning it would fail `-warnaserror` builds. Four fire across
the 181 framework assemblies.

**Exported callbacks with an ambiguous name are rejected.** An export
wrapper resolves its `MethodDesc` through
`LookupUnmanagedCallersOnlyMethodByName`, which takes the first
`[UnmanagedCallersOnly]` method of matching name and compares no
signature — so two exported overloads resolve to the same method and one
wrapper calls it with the wrong arguments. Everything the generator
controls carries the arity, so the existing duplicate-key and
duplicate-symbol checks both pass. Generation now fails instead, naming
both signatures. Only exports: a non-exported callback is found by the
arity-aware key and never reaches the name lookup.

## Known limitations

- **wasi has no out-of-repo acquisition path.** `wasi-experimental`
extends `microsoft-net-runtime-mono-tooling`, not `wasm-tools`, so it
picks up no crossgen2 pack; the targets error explicitly there. Browser
is the shipping wasm/CoreCLR target.
- **Reverse thunks allocate one `int64_t` slot per managed parameter**,
while a by-value struct argument occupies `ceil(size/8)` interpreter
slots. No `[UnmanagedCallersOnly]` callback in CoreLib or the libraries
takes a by-value struct, so nothing exercises this. The old generator
rejected such callbacks with `WASM0067`; this one accepts them, so user
code would get a bad thunk rather than a diagnostic.
- **`'V'` (v128) has no case in the C++ emission helpers.**
Pre-existing; fails loudly.
- **Multi-slot types (`Int128`, `Vector256`, …) are rejected at the
thunk emitter** rather than at the interop boundary, so the diagnostic
differs from the old `WASM0068`. Still a clean `crossgen2 : error :`
with exit 1. No such P/Invoke exists today.
- Does not re-enable the tests disabled in #131811 (#133187), and does
not address gaps #3#7 there.

## Verification

- **Regeneration reproduces the committed helpers byte for byte**, apart
from the rename above, with zero
`WASM0001`/`WASM0060`/`WASM0061`/`WASM0062` warnings across a full
CoreLib+libraries scan. (The checked-in P/Invoke table is already
slightly stale against `main` independently of this PR; that drift is
left alone.)
- `WasmArgumentLayoutTests` goes from 17 to 22 test methods. The two
covering the rejection above were checked against a disabled check, so
they test it rather than agree with it.
- `clr+libs` builds clean for `browser` and `wasi`; `WasmAppBuilder`
still builds for both `net11.0` and `net472`.
- Both flavors build end to end from the in-tree samples, with
per-architecture native payloads, a non-PE file and duplicate-culture
satellites injected into the bundle.
- The renamed runtime contract was checked by building:
`libcoreclr_static.a` exports `g_portableCallHelperThunks` and no
`g_wasmThunks`, and the browser sample links its generated tables
against it.

Contributes to #131811, closing blocking gap #1 and the struct half of
gap #2: a 3-int and a 5-double struct in `[UnmanagedFunctionPointer]`
signatures now resolve to `vS12` / `S12i` / `vS40i`, where all three
previously threw `NotSupportedException`.

> [!NOTE]
> This pull request description was drafted with the help of GitHub
Copilot.

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Copilot-Session: f6d6e5a4-5b25-4198-b42d-d5b2dc781f47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants