Skip to content

JIT: Don't peel the constant off an overflow-checked loop limit - #137705

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:jit-fix-137677
Nov 18, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:jit-fix-137677

Conversation

@EgorBo

@EgorBo EgorBo commented Nov 17, 2026

Copy link
Copy Markdown
Member

Fixes #137677

FlowGraphNaturalLoop::MatchLimit peels base + cns off the loop limit into LimitBase/LimitOffset and loop cloning derives its conditions from the wrapped sum. For a checked(limit + cns) limit that overflows, the loop test throws instead (here, swallowed by a catch that re-enters the loop), so the conditions don't describe the loop and the fast clone without bounds checks was selected:

sum += a[i];
try
{
    i++;
    if (i >= checked(limit + (int.MinValue + 5)))
        break;
}
catch (OverflowException) { ... }

Don't peel overflow-checked ADD/SUB limits.

Fixes dotnet#137677

`FlowGraphNaturalLoop::MatchLimit` peels `base + cns` off the loop limit into
`LimitBase`/`LimitOffset` and loop cloning derives its conditions from the
wrapped sum. For a `checked(limit + cns)` limit that overflows, the loop test
throws instead (here, swallowed by a `catch` that re-enters the loop), so the
conditions don't describe the loop and the fast clone without bounds checks was
selected:

```cs
sum += a[i];
try
{
    i++;
    if (i >= checked(limit + (int.MinValue + 5)))
        break;
}
catch (OverflowException) { ... }
```

Don't peel overflow-checked `ADD`/`SUB` limits.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: f6e7f97d-0dd3-45a4-84b3-0276864afd0c
@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.

@EgorBo

EgorBo commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

PTAL @AndyAyersMS @dotnet/jit-contrib

@EgorBo
EgorBo requested a review from AndyAyersMS November 17, 2026 17:38
@EgorBo

EgorBo commented Nov 18, 2026

Copy link
Copy Markdown
Member Author

/ba-g timeouts

@EgorBo
EgorBo enabled auto-merge (squash) November 18, 2026 13:42
@EgorBo
EgorBo merged commit d7e9c16 into dotnet:main Nov 18, 2026
127 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

Development

Successfully merging this pull request may close these issues.

JIT: (bug) Loop cloning removes the bounds check when the loop limit is a checked(local + const) that always overflows and the catch re-enters the loop

2 participants