JIT: RBO can now infer nested isinst outcomes from class relationships#130204
Draft
AndyAyersMS wants to merge 5 commits into
Draft
JIT: RBO can now infer nested isinst outcomes from class relationships#130204AndyAyersMS wants to merge 5 commits into
AndyAyersMS wants to merge 5 commits into
Conversation
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>
Member
Author
|
@MihuBot -nuget |
Contributor
There was a problem hiding this comment.
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::optRelopTryInferFromTypeCheckand invoke it fromoptRelopImpliesRelop. - Use EE type relationship queries (
compareTypesForCast) to infer dominatedEQ/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. |
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.
This was referenced Jul 4, 2026
Open
…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).
Member
Author
|
@MihuBot -nuget |
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.