Skip to content

Improve nullability analysis in ValueNumStore::IsKnownNonNull#129447

Draft
EgorBo wants to merge 3 commits into
dotnet:mainfrom
EgorBo:improve-IsKnownNonNull
Draft

Improve nullability analysis in ValueNumStore::IsKnownNonNull#129447
EgorBo wants to merge 3 commits into
dotnet:mainfrom
EgorBo:improve-IsKnownNonNull

Conversation

@EgorBo

@EgorBo EgorBo commented Jun 15, 2026

Copy link
Copy Markdown
Member

-80kb diffs in SPMI

Removes a redundant nullcheck in this method:

string Foo(Dictionary<string, string> map) => map["some string key"];

Minimal repo:

static void Test(MyStruct[] arr, int i)
{
    ref MyStruct x = ref arr[i];
    if (i > 10)
    {
        _ = x.Y; // redundant nullcheck in main
    }
}

struct MyStruct
{
    public int X;
    public int Y;
}

Copilot AI review requested due to automatic review settings June 15, 2026 23:45
@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 15, 2026
@EgorBo

EgorBo commented Jun 15, 2026

Copy link
Copy Markdown
Member Author

@MihuBot -nuget -jitutils-repo EgorBo/jitutils -jitutils-branch pmi-deterministic-cctors

@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 updates ValueNumStore::IsKnownNonNull (JIT value numbering) to improve non-null inference by normalizing VNs through offset peeling and recognizing additional VN patterns as non-null, which can enable more aggressive null-check elimination and related optimizations.

Changes:

  • Early-return on NoVN, then peel constant offsets from the input VN before doing the non-null walk.
  • Treat VNF_PtrToArrElem as known-non-null during the reaching-VN walk.
  • Bail out when the peeled offset is considered “big” (per fgIsBigOffset).

Comment thread src/coreclr/jit/valuenum.cpp Outdated
Comment thread src/coreclr/jit/valuenum.cpp
Comment thread src/coreclr/jit/valuenum.cpp Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 15, 2026 23:55

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

Comments suppressed due to low confidence (1)

src/coreclr/jit/valuenum.cpp:1648

  • VNF_PtrToArrElem is being treated as known-non-null via a hard-coded special-case here, but the rest of this method relies on the centralized VN function attribute table (knownNonNull in valuenumfuncs.h -> VNFOA_KnownNonNull). To keep nullability rules in one place and avoid drift, it would be more maintainable to mark PtrToArrElem as knownNonNull=true in valuenumfuncs.h and then remove this special-case.
                return VNVisit::Continue;
            }
        }
        return VNVisit::Abort;
    };
    return VNVisitReachingVNs(vn, vnVisitor) == VNVisit::Continue;

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