Skip to content

JIT: RBO can now infer nested isinst outcomes from class relationships#130204

Draft
AndyAyersMS wants to merge 5 commits into
dotnet:mainfrom
AndyAyersMS:andyayersms/fix-97581
Draft

JIT: RBO can now infer nested isinst outcomes from class relationships#130204
AndyAyersMS wants to merge 5 commits into
dotnet:mainfrom
AndyAyersMS:andyayersms/fix-97581

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Extend optRelopImpliesRelop so that a dominating "obj is A" test implies the value of a dominated "obj is B" test when the runtime reports compareTypesForCast(A, B) as Must or MustNot.

Fixes #97581.

Extend optRelopImpliesRelop so that a dominating "obj is A" test
implies the value of a dominated "obj is B" test when the runtime
reports compareTypesForCast(A, B) as Must or MustNot.

Fixes dotnet#97581.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 4, 2026 17:24
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@MihuBot -nuget

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 extends the JIT’s Redundant Branch Optimization (RBO) implication logic so it can infer the outcome of a nested is/isinst-based null comparison from a dominating is check on the same object, using compareTypesForCast to determine Must vs MustNot. It also adds a JitBlue regression test covering both “always true” (Derived→Base) and “always false” (unrelated types) cases.

Changes:

  • Add Compiler::optRelopTryInferFromTypeCheck and invoke it from optRelopImpliesRelop.
  • Use EE type relationship queries (compareTypesForCast) to infer dominated EQ/NE(IsInstanceOf(...), null) results under a dominating type test.
  • Add a JitBlue regression test and include it in Regression_ro_2.csproj.

Reviewed changes

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

File Description
src/coreclr/jit/redundantbranchopts.cpp Adds new type-test-based inference path in optRelopImpliesRelop.
src/coreclr/jit/compiler.h Declares the new optRelopTryInferFromTypeCheck helper.
src/tests/JIT/Regression/Regression_ro_2.csproj Includes the new JitBlue regression source file in the test project.
src/tests/JIT/Regression/JitBlue/Runtime_97581/Runtime_97581.cs New regression test validating implied nested is outcomes for Must/MustNot relationships.

Comment thread src/coreclr/jit/redundantbranchopts.cpp
IDynamicInterfaceCastable can make an object dynamically satisfy an
interface cast that its static type hierarchy says MustNot. Only apply
the MustNot fold when the tree target is not an interface. Must is
still safe: static inheritance can't be removed by IDCC. Also switch
class-handle extraction to the safe IsVNTypeHandle(vn, CORINFO_CLASS_HANDLE*)
overload per review feedback.
…om domCls

compareTypesForCast(base, subclass) returns MustNot because the static cast can't
succeed, but for isinst inference obj is base -> obj is not subclass' is unsound:
the runtime object could still be the subclass. Also reject when either class is an
interface, where the answer isn't a proof about arbitrary objects. Fixes bogus fold
in System.Xml.Schema.Compiler.BuildParticleContentModel (dom=XmlSchemaGroupBase,
tree=XmlSchemaChoice).
When treeCls derives from domCls (compareTypesForCast(treeCls, domCls) is Must),
the isinsts are related by contrapositive: obj is-a treeCls implies obj is-a domCls,
so obj is-NOT-a domCls implies obj is-NOT-a treeCls. This lets RBO fold a nested
'is Derived' check on the branch where the outer 'is Base' failed. Only sound when
treeCls is not an interface (otherwise IDCC can invalidate the contrapositive).
Copilot AI review requested due to automatic review settings July 5, 2026 16:05
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@MihuBot -nuget

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 4 out of 4 changed files in this pull request and generated 2 comments.

Comment on lines +860 to +864
if (forwardCast == TypeCompareState::Must)
{
inferredCondIsObjIsA = true;
objIsBUnderCond = true;
}
Comment on lines +11 to +14
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
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: Redundant (implied) type test is not folded

2 participants