Skip to content

Recompile ReadyToRun images when the crossgen2 arguments change - #137563

Merged
lewing merged 3 commits into
dotnet:mainfrom
lewing:lewing-crossgen2-args-r2r-incrementality
Nov 18, 2026
Merged

lewing merged 3 commits into
dotnet:mainfrom
lewing:lewing-crossgen2-args-r2r-incrementality

Conversation

@lewing

@lewing lewing commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

_CreateR2RImages only compares files, so an incremental publish that changes only the crossgen2 arguments reuses stale ReadyToRun images. Jan's repro: publish a console app with /p:PublishReadyToRun=true /p:PublishReadyToRunCrossgen2ExtraArgs=--instructionset:avx2, then again with avx512. The second publish does not recompile.

Change

This follows the established SDK pattern (for example, CreateGeneratedAssemblyInfoInputsCacheFile). A new target, _CreateReadyToRunCompilerArgumentsCacheFile, runs before _CreateR2RImages. It uses the Hash task to hash:

  • PublishReadyToRunCrossgen2ExtraArgs
  • the target-computed _PublishReadyToRunCrossgen2ExtraArgs
  • PublishReadyToRunInstructionSet (via _ReadyToRunInstructionSetArgs)
  • PublishReadyToRunComposite
  • PublishReadyToRunContainerFormat
  • PublishReadyToRunUseCrossgen2
  • PublishReadyToRunPerfmapFormatVersion

It writes the hash to $(IntermediateOutputPath)$(MSBuildProjectName).ReadyToRunCompilerArguments.cache only when the hash changes. _CreateR2RImages lists that file as an input, so changing any of these values makes every image out of date.

Target ordering: BeforeTargets hooks run in declaration order. The new target therefore sees arguments added by hooks declared earlier, including project and Directory.Build.targets hooks. Hooks imported after the SDK targets must also run before _CreateReadyToRunCompilerArgumentsCacheFile, or list their own inputs in _ReadyToRunCompilerInputs. The WASI and browser hooks that add --directpinvoke already list their pinvoke manifests that way. The browser --partial PGO argument is tracked by browser-r2r-state.stamp, which is left unchanged; it also tracks the PGO file list and the wasm-opt post-processing state.

PublishReadyToRunInstructionSet with command-line extra args: #137112 appended --instruction-set: to PublishReadyToRunCrossgen2ExtraArgs at evaluation time. When that property is passed with -p:, it is a global property, so the append was ignored and the instruction set was silently dropped. maraf found this in dotnet/sdk#57127. The instruction set now goes through an internal _ReadyToRunInstructionSetArgs property, which both RunReadyToRunCompiler calls pass and the hash includes. This mirrors dotnet/sdk@485b9063d4.

Validation

All runs used a dotnet new console app with the .NET 11 SDK (11.0.100-preview.6), -r osx-arm64 -p:PublishReadyToRun=true, and the in-tree targets imported via -p:AfterMicrosoftNETSdkTargets=<this file>:

Scenario Stock SDK This PR
Change PublishReadyToRunCrossgen2ExtraArgs (--instruction-set:apple-m1 → armv8.2-a) _CreateR2RImages skipped (bug) rebuilt
Unchanged republish — skipped
Switch back to the earlier args — rebuilt
Args added to _PublishReadyToRunCrossgen2ExtraArgs by a BeforeTargets="_CreateR2RImages" hook, then changed — rebuilt; the new ISA was on the crossgen2 command line
Self-contained PublishReadyToRunComposite=true → false — rebuilt
Failing args (--bogus-option), republish, then revert to the earlier args — the failed compile retried, then reverting recompiled
Source-only change with the same args, then republish — rebuilt once, then skipped
PublishReadyToRunInstructionSet=apple-m1 with -p:PublishReadyToRunCrossgen2ExtraArgs=--Ot --instruction-set dropped (main's targets) both arguments passed to crossgen2
Change PublishReadyToRunInstructionSet, then republish unchanged — rebuilt, then skipped
Invalid PublishReadyToRunInstructionSet — crossgen2 reports the invalid instruction set

I did not build a WASI or browser CoreCLR app. The hook scenario above uses a hook declared in Directory.Build.targets.

dotnet/sdk

The SDK has the same targets in src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.CrossGen.targets. The equivalent change is in dotnet/sdk#57127. It also hashes PublishReadyToRunCrossgen2CompositeExtraArgs, which only exists there, and adds a ReadyToRun publish test that changing the crossgen2 arguments recompiles.

Relates to #137112 (review comments: #137112 (comment), #137112 (comment))

Note

This PR description was generated with GitHub Copilot.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 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.

lewing added a commit that referenced this pull request Nov 15, 2026
…axed SIMD on for WASI (#137112)

Enables wasm relaxed SIMD by default on WASI for both CoreCLR ReadyToRun
and NativeAOT. Browser stays opt-in, and Mono is unchanged.

| Target | Relaxed SIMD | Opt out / in |
|---|---|---|
| CoreCLR WASI (R2R) | On |
`PublishReadyToRunInstructionSet=-relaxed-simd` |
| NativeAOT WASI | On | `IlcInstructionSet=-relaxed-simd` |
| CoreCLR browser (R2R) | Off |
`PublishReadyToRunInstructionSet=relaxed-simd` |
| Mono | Not implemented | — |

## Instruction sets come from the compiler

- **WASI default.** `InstructionSetHelpers` adds `relaxed-simd` to the
default instruction sets for wasm32 WASI, so crossgen2 and ILC both pick
it up without callers passing anything.
- **`PublishReadyToRunInstructionSet`.** A general crossgen2 property
that maps to `--instruction-set:` and accepts `+x`/`-x` relative to the
defaults, like `IlcInstructionSet` for NativeAOT. It replaces the
browser-only `WasmEnableRelaxedSimd`, which only fed the same crossgen2
switch plus native flags no runtime code needed (`-mrelaxed-simd`, a
relink trigger and a workload requirement). wasm-opt now always enables
relaxed SIMD, so the ReadyToRun wasm-opt pass accepts relaxed images.
`Microsoft.NET.CrossGen.targets` is shared with dotnet/sdk, so a
matching sdk change will follow.
- **Images are checked against CoreLib.** Wasm has no runtime ISA
detection. The runtime now adopts the instruction sets CoreLib's R2R
image was compiled for, and every other image is checked against them; a
mismatch is rejected like on other platforms. This removes the
browserhost hook that set the flag from a runtime config property.
- **NativeAOT.** NativeAOT's CoreLib now compiles the wasm intrinsics.
ILC maps `System.Runtime.Intrinsics.Wasm` types to instruction sets, and
the JIT treats wasm ISA support as exact under the NativeAOT ABI, so
`IsSupported` is a compile-time constant. `SupportsWasmIntrinsics` is
now true for every wasm CoreLib, and relaxed SIMD for every non-Mono
one.
- **`WasiEnableCompositeR2R` removed.** WASI R2R already requires
`PublishReadyToRunComposite=true`, so the host link flags depend only on
`PublishReadyToRun`.
- **No trimmer folding of `IsSupported` on CoreCLR.** The JIT and
interpreter decide support per method, so folding could make interpreted
code take an intrinsic path and throw. Measured on a trimmed WASI R2R
app it saved about 2.7 KB of R2R code (6 KB of IL). The substitutions
remain for Mono only.
- **Incremental publish.** CoreLib's crossgen step records
`PublishReadyToRunInstructionSet`, so changing it recompiles CoreLib.
Changing crossgen2 arguments doesn't recompile app images on any target;
#137563 fixes that in the common crossgen2 targets.

`simd128` remains required on wasm; #137553 tracks making that explicit
by folding it into `base`.

## Trade-offs of the WASI default

- There's no runtime feature detection, so a host without relaxed SIMD
fails to load the module. Use the opt-out for such hosts.
- Relaxed SIMD results can differ by host CPU.
- The pinned wasmtime 49.0.1 enables relaxed SIMD by default.

## Tests

- **`hardware-intrinsics-wasm` pipeline.** The browser leg now really
enables relaxed SIMD; before, the property was never forwarded. A new
WASI leg uses `WholeProgramCompositeBuildMode`, which compiles CoreLib
into each test composite.
- **Other WASI R2R runtime tests** run R2R code against an interpreted
CoreLib (#134949), so they default to the baseline instruction sets, and
two `PackedSimdTests` stay quarantined there.
- **Library tests.** `System.Runtime.Intrinsics.Tests` now builds for
WASI, and the `PackedSimd`/`RelaxedSimd` tests run on browser and WASI.
- **Wasm.Build.Tests.** `PublishRelaxedSimdCoreClr` publishes R2R, runs
relaxed operations when enabled, and expects
`PlatformNotSupportedException` when disabled.

## Validation (local, wasmtime 49.0.1)

- **CoreCLR WASI `HardwareIntrinsics_Wasm_r`.** Whole-program mode with
relaxed SIMD: 73 passed, 1 skipped (the test that expects relaxed SIMD
to be unsupported). Default mode: 72 passed, 2 skipped (the #134949
quarantines).
- **NativeAOT WASI `HardwareIntrinsics_Wasm_r`.** Default: 73 passed, 1
skipped, and the module is rejected under `-W relaxed-simd=n`. With
`IlcInstructionSet=-relaxed-simd`: all 74 pass under `-W
relaxed-simd=n`. The NativeAOT WASI CI leg only builds, so these runs
are local only.
- **`System.Runtime.Intrinsics.Tests` on WASI.** Trimmed R2R: 13,340
passed. Interpreter: 13,296 passed. Opt-out: 53 of 53 passed.
- **WASI app.** R2R default, opt-out, and non-R2R publishes all behave
correctly.
- **Browser** is covered by CI only.

Relates to #130223, #134949, #137553

> [!NOTE]
> This PR description was generated with GitHub Copilot.

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lewing
lewing requested review from akoeplinger and maraf November 16, 2026 16:25
lewing and others added 3 commits November 16, 2026 10:53
_CreateR2RImages only compared files, so an incremental publish that changed
only the crossgen2 arguments (for example PublishReadyToRunCrossgen2ExtraArgs)
kept the stale images. Each batch now also outputs a state file named after a
hash of the arguments. Outputs is expanded after the targets that run before
_CreateR2RImages, so arguments they add to _PublishReadyToRunCrossgen2ExtraArgs
are included. The state file is written after every batch compiles.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Follow the established SDK pattern (for example
CreateGeneratedAssemblyInfoInputsCacheFile): hash the arguments with the Hash
task into a fixed cache file written only when it changes, and list it as an
input of _CreateR2RImages.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
PublishReadyToRunCrossgen2ExtraArgs set with -p: is a global property, so
appending to it at evaluation time was ignored and PublishReadyToRunInstructionSet
was silently dropped. Pass it through an internal property consumed by both
compiler invocations and the arguments cache hash. Mirrors dotnet/sdk@485b9063d4.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@davidwrighton

Copy link
Copy Markdown
Member

@jtschuster for visibility.

@lewing

lewing commented Nov 18, 2026

Copy link
Copy Markdown
Member Author

/ba-g mac timeout not related

@lewing
lewing merged commit 52221af into dotnet:main Nov 18, 2026
100 of 104 checks passed
@lewing
lewing deleted the lewing-crossgen2-args-r2r-incrementality branch November 18, 2026 23:03
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
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.

4 participants