Skip to content

JIT: fix bug in loop cloning with down-counting loops#126770

Open
AndyAyersMS wants to merge 1 commit intodotnet:mainfrom
AndyAyersMS:FixLoopCloningBug
Open

JIT: fix bug in loop cloning with down-counting loops#126770
AndyAyersMS wants to merge 1 commit intodotnet:mainfrom
AndyAyersMS:FixLoopCloningBug

Conversation

@AndyAyersMS
Copy link
Copy Markdown
Member

We were not creating proper cloning conditions, so the fast path might execute in cases where it shouldn't.

We need to always verify for down counting that the initial value is strictly less than the array length(s).

We were not creating proper cloning conditions, so the fast path
might execute in cases where it shouldn't.

We need to always verify for down counting that the initial value
is strictly less than the array length(s).
Copilot AI review requested due to automatic review settings April 10, 2026 21:41
@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 10, 2026
@AndyAyersMS
Copy link
Copy Markdown
Member Author

@jakobbotsch PTAL
fyi @dotnet/jit-contrib

@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

Fixes an issue in CoreCLR JIT loop cloning where down-counting loops could generate overly-permissive fast-path conditions, allowing bounds-check removal when the initial index equals the array/span length.

Changes:

  • Tighten loop cloning limit condition derivation for down-counting loops to require the initial iterator value be strictly less than the relevant length.
  • Add new JIT regression tests covering down-counting loops that start at Length for both arrays and spans.
  • Add a dedicated test project for the new regression test.

Reviewed changes

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

File Description
src/coreclr/jit/loopcloning.cpp Adjusts derived cloning condition operator so down-counting loops require init < length rather than allowing init == length.
src/tests/JIT/opt/Cloning/DownCounted.cs Adds regression coverage for array and span down-counting loops that should throw on a[Length] unless cloning conditions are correct.
src/tests/JIT/opt/Cloning/DownCounted.csproj Adds a minimal test project definition consistent with other tests in the directory.
Comments suppressed due to low confidence (1)

src/tests/JIT/opt/Cloning/DownCounted.cs:73

  • There are extra blank lines (including whitespace-only lines) at the end of the file. Please trim trailing whitespace / remove the extra blank lines to keep diffs clean and comply with typical repo formatting rules.


}
    

Comment on lines +29 to +36
bool hasProblem = ArrayProblem(a, 100);
Console.WriteLine($"failed, has problem={hasProblem}");
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("passed");
result = 100;
}
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

The caught IndexOutOfRangeException is assigned to e but never used. Consider removing the exception variable (or using _) to avoid unused-variable warnings and match other tests in this directory.

Copilot uses AI. Check for mistakes.
Comment on lines +59 to +66
bool hasProblem = SpanProblem(a, 100);
Console.WriteLine($"failed, has problem={hasProblem}");
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("passed");
result = 100;
}
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

The caught IndexOutOfRangeException is assigned to e but never used. Consider removing the exception variable (or using _) to avoid unused-variable warnings and match other tests in this directory.

Copilot uses AI. Check for mistakes.
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.

3 participants