Skip to content

[NO MERGE] JIT: Experiment with non-null this for Wasm R2R - #134677

Draft
adamperlin wants to merge 2 commits into
dotnet:mainfrom
adamperlin:adamperlin-jit-non-null-this
Draft

adamperlin wants to merge 2 commits into
dotnet:mainfrom
adamperlin:adamperlin-jit-non-null-this

Conversation

@adamperlin

@adamperlin adamperlin commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • add a default-off Wasm JIT option that assumes incoming class this is non-null during ReadyToRun compilation
  • represent the incoming value with a distinct known-non-null initial value number
  • require a TYP_REF receiver and explicitly exclude declaring types marked CORINFO_FLG_VALUECLASS or CORINFO_FLG_INTERFACE
  • leave static methods, struct instance methods, interface methods, non-R2R compilation, and default behavior unchanged

Ordinary struct this arguments are byrefs and already fail the TYP_REF guard. The explicit value-class exclusion documents the intended boundary. Interface methods are also excluded because default interface implementations can execute on struct receivers, including boxed structs; excluding only value-class declaring types would not cover that case.

This is an experimental upper-bound measurement, not a proposed production contract. Arbitrary IL can invoke an instance method with a null receiver using call, so enabling this option is intentionally unsound unless the runtime/toolchain provides an additional guarantee or checked-entry mechanism.

Results

Current class-only experiment

Wasm SuperPMI with the separately built unmodified base JIT and updated treatment JIT measured 9,750,029 → 9,429,057 bytes: -320,972 (-3.29%) across 28,729 mutually replayable CoreLib contexts. There were 7,601 size improvements and no size regressions. The treatment requested JIT/EE data absent from the baseline-collected MCH in 256 of 28,985 contexts, while the base had no missing contexts. This is a common-context result, not a complete-corpus total.

A focused six-method Crossgen2 probe showed the class receiver's this is null comparison shrinking from 34 to 23 bytes. The default interface implementation retained its comparison at 34 bytes, including when the interface is implemented by a struct. Struct field-access codegen, struct object-argument checks, and static object-argument checks were identical with the option enabled and disabled. These are codegen checks, not execution tests.

Historical measurements before excluding interface methods

The following untrimmed browser-Wasm Release System.Private.CoreLib R2R measurements were collected with the earlier reference-receiver experiment. They have not been rerun for the current class-only guard:

Metric Base Treatment Delta
JIT-reported managed code 16,306,053 15,761,992 -544,061 (-3.34%)
Wasm Code section 14,336,449 13,867,040 -469,409 (-3.27%)
Whole R2R image 28,693,727 28,219,792 -473,935 (-1.65%)
Brotli-compressed image 4,876,667 4,843,917 -32,750 (-0.67%)

Both historical builds compiled the same 52,503 managed methods. 12,619 methods became smaller and none became larger. SafeFileHandle.get_IsInvalid shrank from 124 to 42 bytes. Static and value-type instance controls were byte-identical.

The earlier Wasm SuperPMI comparison measured 9,750,029 → 9,427,949 bytes across the same 28,729 common contexts: -322,080 (-3.30%), with 7,620 size improvements and no regressions.

Validation

Current guard:

  • .\build.cmd clr.wasmjit -c Release — passed, zero warnings/errors.
  • Roslyn compilation of the focused receiver probe and three sequential Crossgen2 compilations (old option-on, updated option-on, updated option-off), using --parallelism:1 --targetarch:wasm --targetos:browser --obj-format:wasm --target-allows-runtime-code-generation:false -O --codegenopt:JitDisasm=* — passed; Python assertions checked all six method bodies and the expected null-comparison/code-size changes.
  • python src\coreclr\scripts\superpmi.py asmdiffs -target_os browser -target_arch wasm --altjit -mch_files <published-CoreLib-MCH> -product_location artifacts\bin\coreclr\windows.x64.Release -base_jit_path <unmodified-base-JIT> -diff_jit_path artifacts\bin\coreclr\windows.x64.Release\clrjit_universal_wasm_x64.dll -diff_jit_option JitWasmAssumeNonNullThis=1 --summary_as_json -tag class-only -log_level info — completed; 256 treatment missing-data contexts, as noted above.
  • jit-format.exe -r <worktree> -o windows -a x64 -b Release --untidy valuenum.cpp jitconfigvalues.h — passed clang-format check for both modified files; clang-tidy was not run.
  • git diff --check — passed.

Historical validation:

  • build.cmd -os browser -c Release -subset clr+libs — passed before the receiver restriction.
  • direct production-style CoreLib Crossgen2 base/treatment compilation — completed before the receiver restriction.

No Wasm execution tests or runtime throughput benchmarks were run for this update.

Resolves #134674

Note

This pull request description was created with GitHub Copilot.

@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Sep 25, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 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: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@adamperlin adamperlin changed the title JIT: Experiment with non-null this for Wasm R2R [NO MERGE] JIT: Experiment with non-null this for Wasm R2R Sep 25, 2026
Exclude value classes explicitly and interface methods that may run on boxed structs.

Co-authored-by: Copilot App <[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

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: Explore assuming non-null reference-type this

1 participant