Uh oh!
There was an error while loading. Please reload this page.
JIT: fix loop cloning condition for decreasing loops with array-length limit - #129187
Conversation
…h limit `optDeriveLoopCloningConditions` was unconditionally overwriting `ident` with the limit array's length in the `HasArrayLengthLimit` branch, leaving decreasing loops with the wrong per-access condition (`limit <relop> accessArr.Length` instead of `init <relop> accessArr.Length`). Gate the overwrite on `isIncreasingLoop` to match the parallel `HasConstLimit` and `HasInvariantLocalLimit` branches. Fixesdotnet#129176. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
AndyAyersMS
commented
Jun 9, 2026
@jakobbotsch PTAL Bug has been in the code since .NET 7, so may not be a strong case for a backport. |
There was a problem hiding this comment.
Pull request overview
This PR fixes loop-cloning condition derivation in optDeriveLoopCloningConditions for decreasing loops when the loop limit is an array length, ensuring the per-access bounds condition is derived from the loop init (upper end of the decreasing IV range) rather than incorrectly overwriting it with the limit array’s .Length. It also adds a JIT regression test covering the unsafe fast-clone scenario.
Changes:
- Fix: In
HasArrayLengthLimit, only overwriteidentwith the limit array length for increasing loops (leave decreasing-loopidentas the init-derived identifier). - Tests: Add a new JitBlue regression test that ensures decreasing loops with mismatched limit/indexed arrays still throw
IndexOutOfRangeExceptionwhen init is out of bounds. - Build/test wiring: Include the new test source file in
Regression_ro_2.csproj.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/coreclr/jit/loopcloning.cpp | Gates ident overwrite in HasArrayLengthLimit to increasing loops so decreasing loops keep init-based per-access conditions. |
| src/tests/JIT/Regression/Regression_ro_2.csproj | Adds compile include for the new JIT regression test. |
| src/tests/JIT/Regression/JitBlue/Runtime_129176/Runtime_129176.cs | New regression test exercising decreasing-loop cloning correctness when limit and indexed arrays differ. |
Uh oh!
There was an error while loading. Please reload this page.
AndyAyersMS
commented
Jun 9, 2026
interestingly there is one method with a diff |
Uh oh!
There was an error while loading. Please reload this page.
…h limit (#129187) `optDeriveLoopCloningConditions` was unconditionally overwriting `ident` with the limit array's length in the `HasArrayLengthLimit` branch, leaving decreasing loops with the wrong per-access condition (`limit <relop> accessArr.Length` instead of `init <relop> accessArr.Length`). Gate the overwrite on `isIncreasingLoop` to match the parallel `HasConstLimit` and `HasInvariantLocalLimit` branches. Fixes#129176. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
optDeriveLoopCloningConditionswas unconditionally overwritingidentwith the limit array's length in theHasArrayLengthLimitbranch, leaving decreasing loops with the wrong per-access condition (limit <relop> accessArr.Lengthinstead ofinit <relop> accessArr.Length). Gate the overwrite onisIncreasingLoopto match the parallelHasConstLimitandHasInvariantLocalLimitbranches.Fixes#129176.