Repository navigation
Don't give up on folding obj.GetType if obj can be null - #129371
Conversation
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
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, foldNI_System_Object_GetTypewhen the receiver type is exact, and union inNullPtrExcwhen the receiver may be null to avoid suppressingNullReferenceException. - In
gtGetClassHandle, if a class handle is known butpIsNonNullis false, attempt to prove non-null usingfgAddrCouldBeNulland (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. |
|
PTAL @jakobbotsch @dotnet/jit-contrib |
jakobbotsch
left a comment
There was a problem hiding this comment.
LGTM, couple of questions
|
/ba-g timeouts |
This PR:
* in vn when we call `gtGetClassHandle` to fold `obj.GetType()` into a
constant (on VN level) we shouldn't give up if `gtGetClassHandle` is
unsure about `obj` being never null
* `gtGetClassHandle` now calls `fgAddrCouldBeNull` and
`vnStore->IsKnownNonNull` for its `isNonNull` out param.
Example:
```cs
string _fld;
Type Foo() => _fld.GetType();
```
Was:
```asm
; 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:
```asm
; Method Proga:Foo():System.Type:this (FullOpts)
mov eax, dword ptr [rcx+0x08] ;; nullcheck
mov rax, 0x2BC495400E0 ; 'System.String'
ret
```
[diffs](MihuBot/runtime-utils#1978)
This PR:
gtGetClassHandleto foldobj.GetType()into a constant (on VN level) we shouldn't give up ifgtGetClassHandleis unsure aboutobjbeing never nullgtGetClassHandlenow callsfgAddrCouldBeNullandvnStore->IsKnownNonNullfor itsisNonNullout param.Example:
Was:
Now:
diffs