Skip to content

JIT: preserve relocatable handles when simplifying ordered comparisons - #135484

Merged
EgorBo merged 2 commits into
dotnet:mainfrom
EgorBo:egorbo/fix-jit-134891
Oct 10, 2026
Merged

EgorBo merged 2 commits into
dotnet:mainfrom
EgorBo:egorbo/fix-jit-134891

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Reject signed relational constant folding involving relocatable handles, just as for unsigned comparisons and arithmetic. A compile-time handle placeholder does not establish the ordering of its relocated runtime address.

Also prevent long-to-int VN narrowing from converting handle VNs into ordinary numeric constants and bypassing that rejection. Retain the existing identity-based equality/null folding.

Fixes #134891

No diffs

PR description draft:
<!-- -->
Reject signed relational constant folding involving relocatable handles, just as for unsigned comparisons and arithmetic. A compile-time handle placeholder does not establish the ordering of its relocated runtime address.

Also prevent long-to-int VN narrowing from converting handle VNs into ordinary numeric constants and bypassing that rejection. Retain the existing identity-based equality/null folding.

Add a single-file regression comparing a runtime type handle against a numeric bound for all signed relational operators, reversed operands, and unsigned controls.

Fixes dotnet#134891

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ae8155d6-f240-4542-a1da-c721ba933c91
@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 Oct 9, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 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.

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.

🟡 Changes recommended

Zero-bound comparisons can still fold through placeholder-based non-negativity inference.

1 open finding
What changed in this PR

Prevents JIT value numbering from folding relocatable handle comparisons using compile-time placeholder addresses.

Changes:

  • Preserves handle VNs during narrowing.
  • Rejects signed ordered folding under relocation.
  • Adds NativeAOT regression coverage.
File Description
src/​coreclr/​jit/​valuenum.cpp Updates handle folding and narrowing rules.
src/​tests/​JIT/​Regression_ro_2/​Runtime_134891.cs Tests ordered handle comparisons.

🧠 Review effort: Balanced

Comment thread src/coreclr/jit/valuenum.cpp Resolved
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 58815d76-2d1f-4ad8-9153-cca0f87b58e5
@EgorBo

EgorBo commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

PTAL @dotnet/jit-contrib

@EgorBo
EgorBo requested a review from a team October 9, 2026 14:49

@JulieLeeMSFT JulieLeeMSFT left a comment

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.

LGTM.

  Tree: (long)handleVN > Bound
                │
                ▼
      VNIgnoreIntToLongCast(vn)  -- narrows LONG VN to INT VN
                │
      [BEFORE] any integral const --> plain int VN (handle-ness lost!)
                │
                ▼
      VNEvalCanFoldBinaryFunc(GT_GT, arg0VN, arg1VN)
                │
      checks IsVNHandle(arg0VN) || IsVNHandle(arg1VN)
                │
      [BEFORE] GT_GT/GE/LT/LE not in reloc-guarded case --> folds anyway (WRONG)
      [AFTER]  GT_GT/GE/LT/LE added to guarded case, and handle-ness preserved
               --> folding correctly rejected under compReloc

@EgorBo

EgorBo commented Oct 10, 2026

Copy link
Copy Markdown
Member Author

/ba-g 135574

@EgorBo
EgorBo merged commit 0e45f6c into dotnet:main Oct 10, 2026
145 of 147 checks passed
EgorBo added a commit that referenced this pull request Oct 12, 2026
<!-- -->

Fixes #135604.

#135484 stopped VN folding of relocatable handle comparisons, but ARM32
can still fold them during assertion propagation using the compile-time
handle placeholder.

Reject handle VNs in numeric-constant and non-negative queries, and
avoid deriving numeric ranges or signs from tree constants that need
relocation. Keep identity-based equality folding unchanged.

A few
[diffs](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1631303&view=ms.vss-build-web.run-extensions-tab)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: afd2a57d-a85b-4904-8fed-4980955c9ac0
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 13, 2026
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: (bug) NativeAOT: relational comparison of a type handle with a constant is folded using the compile-time handle placeholder

3 participants