Skip to content

JIT: fix loop cloning condition merging - #137776

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-loop-cloning-condition-merging
Nov 18, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-loop-cloning-condition-merging

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Loop cloning could drop a signed non-negativity guard when it matched an
unsigned zero-trip guard, allowing the fast loop to access past the end
of an array.

Only combine ordered conditions with matching signedness, and use
SwapRelop when swapping operands so strict and inclusive comparisons
are not treated as equivalent.

Validation

  • .\build.cmd clr+libs+host -c Checked: passed.
  • .\build.cmd clr.jit -c Checked: passed.
  • .\src\tests\build.cmd -GenerateLayoutOnly x64 Checked: passed.
  • .\src\tests\build.cmd -Test JIT/Regression_ro_2/Regression_ro_2.csproj x64 Checked: passed.
  • Regression_ro_2.cmd: 506 passed, 7 skipped. The new regression crashes
    without the fix and passes with it.
  • clang-format --dry-run --Werror -style=file src\coreclr\jit\loopcloning.cpp:
    passed.

Resolves #137676

Note

This PR description was generated with GitHub Copilot.

Loop cloning could drop a signed non-negativity guard when it matched an unsigned zero-trip guard, allowing out-of-bounds array accesses.

Only combine ordered conditions with matching signedness and use SwapRelop when swapping operands.

Fixes dotnet#137676

Co-authored-by: Copilot <[email protected]>
@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 Nov 17, 2026
@azure-pipelines

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

@AndyAyersMS
AndyAyersMS requested review from EgorBo and a balanced review from Copilot November 17, 2026 22:05
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@EgorBo ptal
fyi @dotnet/jit-contrib

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.

🟢 Approval recommended

No active review issues are recorded for the loop-cloning condition-merging fix.

No open issues 🎉

What changed in this PR

This PR fixes condition merging in JIT loop cloning so required bounds guards are not discarded, preventing out-of-bounds array access in optimized loops.

Changes:

  • Require matching signedness when merging ordered comparisons.
  • Use SwapRelop for swapped operands, preserving strict versus inclusive comparisons.
  • Add a regression test that expects IndexOutOfRangeException for a large unsigned loop bound.
File Description
src/​tests/​JIT/​Regression_ro_2/​Runtime_137676.cs Adds regression coverage for an unsigned loop bound of 0x80000000u.
src/​coreclr/​jit/​loopcloning.cpp Corrects signedness checks and swapped-comparison equivalence during condition merging.

Review effort: Balanced

@AndyAyersMS
AndyAyersMS merged commit 2cfadf0 into dotnet:main Nov 18, 2026
124 of 130 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 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

3 participants