Skip to content

Don't give up on folding obj.GetType if obj can be null#129371

Open
EgorBo wants to merge 2 commits into
dotnet:mainfrom
EgorBo:remove-nullchecks
Open

Don't give up on folding obj.GetType if obj can be null#129371
EgorBo wants to merge 2 commits into
dotnet:mainfrom
EgorBo:remove-nullchecks

Conversation

@EgorBo

@EgorBo EgorBo commented Jun 13, 2026

Copy link
Copy Markdown
Member

Example:

string _fld;

Type Foo() => _fld.GetType();

Was:

; Method Proga:Foo():System.Type:this (FullOpts)
       sub      rsp, 40
       mov      rcx, gword ptr [rcx+0x08]
       call     [System.Object:GetType():System.Type:this]
       nop
       add      rsp, 40
       ret      

Now:

; Method Proga:Foo():System.Type:this (FullOpts)
       mov      eax, dword ptr [rcx+0x08] ;; nullcheck
       mov      rax, 0x2BC495400E0      ; 'System.String'
       ret      

diffs

Copilot AI review requested due to automatic review settings June 13, 2026 19:05
@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 Jun 13, 2026
@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.

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.

Pull request overview

This PR adjusts CoreCLR JIT type/value-numbering to allow folding obj.GetType() when the JIT can prove the receiver’s exact type, even if it can’t prove the receiver is non-null, by preserving the implicit null-check via the VN exception set. It also strengthens gtGetClassHandle’s ability to conclude “non-null” from structural nullability and (when available) VN-based non-null information.

Changes:

  • In fgValueNumberIntrinsic, fold NI_System_Object_GetType when the receiver type is exact, and union in NullPtrExc when the receiver may be null to avoid suppressing NullReferenceException.
  • In gtGetClassHandle, if a class handle is known but pIsNonNull is false, attempt to prove non-null using fgAddrCouldBeNull and (when available) vnStore->IsKnownNonNull(optConservativeNormalVN(tree)).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/coreclr/jit/valuenum.cpp Enables folding obj.GetType() for exact types even when nullability is unknown, while preserving null-check exceptions in the VN.
src/coreclr/jit/gentree.cpp Improves gtGetClassHandle non-null inference using structural nullability and VN-known-non-null information.

Comment thread src/coreclr/jit/valuenum.cpp
@EgorBo

EgorBo commented Jun 13, 2026

Copy link
Copy Markdown
Member Author

@MihuBot

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.

2 participants