Skip to content

Introduce GT_ASSERTION node#127193

Open
EgorBo wants to merge 2 commits intodotnet:mainfrom
EgorBo:jit-gt-assertion-node
Open

Introduce GT_ASSERTION node#127193
EgorBo wants to merge 2 commits intodotnet:mainfrom
EgorBo:jit-gt-assertion-node

Conversation

@EgorBo
Copy link
Copy Markdown
Member

@EgorBo EgorBo commented Apr 21, 2026

an alternative for #127117

Diffs

Synthesize a GT_ASSERTION marker statement at the start of stayInLoopSucc
in optTryInvertWhileLoop, recording that the loop guard is known to be true
on entry to the loop body. This compensates for cyclic value numbers from
phi merges that otherwise prevent later phases (assertion prop, range check)
from proving the guard still holds inside the loop body.

The node is dropped before LIR by Rationalizer and skipped by IV strength
reduction so it does not pin primary IVs.

Alternative for dotnet#127117.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 21, 2026 00:00
@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 Apr 21, 2026
@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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Introduces a new HIR-only IR node (GT_ASSERTION) to seed assertion propagation with “known-true” loop-guard facts (especially after loop inversion), improving the JIT’s ability to eliminate redundant checks inside loops.

Changes:

  • Add GT_ASSERTION as a new unary, no-value, not-LIR GenTree operator and plumb it through core visitors/analyses (VN, operand iterators, etc.).
  • Insert GT_ASSERTION at the top of the loop body after loop inversion to record the loop guard as true on loop entry.
  • Extend assertion propagation to generate one-sided bounds assertions from GT_ASSERTION and ensure GT_ASSERTION statements don’t reach LIR.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/coreclr/jit/gtlist.h Defines the new GT_ASSERTION node kind and its properties (HIR-only / not LIR).
src/coreclr/jit/optimizer.cpp Inserts GT_ASSERTION at loop-body entry after loop inversion to seed later optimizations.
src/coreclr/jit/assertionprop.cpp Generates one-sided bounds assertions from GT_ASSERTION; adds a “no complementary assertion” mode.
src/coreclr/jit/compiler.h Updates assertion helper signature to support optional complementary assertion creation.
src/coreclr/jit/rationalize.cpp Drops GT_ASSERTION root statements before converting to LIR (backstop).
src/coreclr/jit/valuenum.cpp Ensures VN can traverse GT_ASSERTION as a unary node.
src/coreclr/jit/gentree.cpp Adds GT_ASSERTION support to operand-use iteration utilities.
src/coreclr/jit/compiler.hpp Adds GT_ASSERTION to operand-use visitation.
src/coreclr/jit/liveness.cpp Accounts for GT_ASSERTION in LIR liveness/DCE switch handling.
src/coreclr/jit/inductionvariableopts.cpp Skips GT_ASSERTION statements so they don’t pin IV uses during analysis.
Comments suppressed due to low confidence (1)

src/coreclr/jit/liveness.cpp:2574

  • GT_ASSERTION is introduced as a pure marker (no codegen/observable effects), but here it is grouped under LIR nodes that are treated as "always side-effecting" for dead-code elimination. If GT_ASSERTION ever leaks into LIR, this classification will prevent DCE from removing it and makes it more likely to reach Lowering/CodeGen where it isn't supported. Consider treating GT_ASSERTION as removable/no-op in LIR DCE, or asserting it should never appear in LIR.
            case GT_RECORD_ASYNC_RESUME:
            case GT_KEEPALIVE:
            case GT_ASSERTION:
            case GT_SWIFT_ERROR_RET:
            case GT_GCPOLL:
            case GT_WASM_JEXCEPT:
                // Never remove these nodes, as they are always side-effecting.

Comment thread src/coreclr/jit/gtlist.h Outdated
Comment thread src/coreclr/jit/rationalize.cpp Outdated
@EgorBo
Copy link
Copy Markdown
Member Author

EgorBo commented Apr 21, 2026

PTAL @jakobbotsch this is the impl you suggested. cc @dotnet/jit-contrib

@EgorBo EgorBo requested a review from jakobbotsch April 21, 2026 08:21
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