Skip to content

JIT: Fold impossible type tests between unrelated classes - #130805

Draft
EgorBo wants to merge 2 commits into
dotnet:mainfrom
EgorBo:egorbo/fix-106966-impossible-type-test
Draft

JIT: Fold impossible type tests between unrelated classes#130805
EgorBo wants to merge 2 commits into
dotnet:mainfrom
EgorBo:egorbo/fix-106966-impossible-type-test

Conversation

@EgorBo

@EgorBo EgorBo commented Jul 15, 2026

Copy link
Copy Markdown
Member

Fixes #106966

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bfa09cf3-e00d-4d69-98d1-a03f33c6283e
Copilot AI review requested due to automatic review settings July 15, 2026 17:56
@EgorBo

EgorBo commented Jul 15, 2026

Copy link
Copy Markdown
Member Author

@MihuBot -nuget

@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 Jul 15, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 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.

@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.

Comment thread src/coreclr/jit/importer.cpp Outdated
const unsigned fromClassAttribs = info.compCompHnd->getClassAttribs(fromClass);
if ((fromClassAttribs & nonClassTypeAttribs) == 0)
{
castMustFail =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is correct... but castMustFail = ... MustNot is so confusing to read 😂

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to an explicit reverse-cast result check.

@EgorBo
EgorBo marked this pull request as draft July 15, 2026 18:00

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 enhances JIT import-time folding for isinst / castclass by proving certain type tests are statically impossible even when the source type isn’t exact, enabling the JIT to return null for isinst in more cases (notably when both types are ordinary unrelated classes with non-overlapping hierarchies).

Changes:

  • Extend impOptimizeCastClassOrIsInst to detect when no subtype of the source class could ever be an instance of the target class (for isinst).
  • Gate the new proof to “ordinary classes” (excluding arrays, delegates, generic type variables, interfaces, and value classes) to avoid unsound overlap cases.
  • Preserve existing behavior for castclass (still not optimizing always-throw cases here).

@EgorBo

EgorBo commented Jul 15, 2026

Copy link
Copy Markdown
Member Author

@tannergooding it's actually just an AI fix for the issue to check the diffs 🙂

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bfa09cf3-e00d-4d69-98d1-a03f33c6283e
Copilot AI review requested due to automatic review settings July 15, 2026 18:34

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

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

Comment thread src/coreclr/jit/importer.cpp
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.

Type test List<T> is Iterator<T> that can never succeed is not optimized away

3 participants