Skip to content

JIT: Don't clone or unroll cold loops #115744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025

Conversation

amanasifkhalid
Copy link
Member

Follow-up to #115734 (comment).

@Copilot Copilot AI review requested due to automatic review settings May 19, 2025 21:24
@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 May 19, 2025
Copy link
Contributor

@Copilot 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

This PR prevents unnecessary JIT work by skipping loop unrolling and cloning for cold loops, as indicated by the updated logic in both functions.

  • Added a cold loop check in the loop unrolling function in optimizer.cpp.
  • Added the same cold loop check in the loop cloning function in loopcloning.cpp.

Reviewed Changes

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

File Description
src/coreclr/jit/optimizer.cpp Added early exit for unrolling of cold loops
src/coreclr/jit/loopcloning.cpp Added early exit for cloning of cold loops

Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@amanasifkhalid
Copy link
Member Author

@dotnet/jit-contrib PTAL. Diffs show some size increases in cases where cloning previously removed a bounds check, and RBO later removed the second loop body; now, we leave the bounds check in-place. For PGO collections, there are few PerfScore diffs since we're targeting cold code, and we try to reestablish profile consistency right before loop opts. If we're ok with the behavior diverging, I'm ok with reenabling cloning/unrolling cold loops for non-PGO scenarios since we aren't as confident about what is actually cold. Thanks!

Copy link
Member

@EgorBo EgorBo left a comment

Choose a reason for hiding this comment

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

I think we need some sort of a knob to replace all cold blocks with some minimal weight so none of the opts give up on them (with isRunRarely) and check that on large benchmarks. I suspect we loose a bit of perf by giving up on what we think is cold, while in fact it is not (e.g. unreliable static pgo, lack of context-oriented dynamic PGO that bakes weights for the first use during the start)

But the general question is: should we turn off opts for cold parts if the TP wins aren't too big?

@amanasifkhalid
Copy link
Member Author

I think we need some sort of a knob to replace all cold blocks with some minimal weight so none of the opts give up on them

I think that would be interesting to try. I think the simplest way to quickly try this is to make the threshold isRunRarely checks some negative weight we never expect a block to have. As we can see in the diffs, bailing on loop cloning can inadvertently increase cold code size, and I've found cases where not inverting a cold loop can pessimize RBO and also lead to increased code size, though I don't think we can predict whether these opts will be size-increasing or decreasing so early.

But the general question is: should we turn off opts for cold parts if the TP wins aren't too big?

If the optimization is typically size-increasing, and we are confident the code is cold, I think we ought to disable it regardless of the TP wins. Without hot/cold splitting, this means being able to place more hot code on each memory page. With my current splitting prototype, it also means being able to fit more hot code in each code heap.

@amanasifkhalid
Copy link
Member Author

Leaving this open until after the Preview 5 snap, fyi

@amanasifkhalid
Copy link
Member Author

/ba-g build timeouts, and jit-format broken

@amanasifkhalid amanasifkhalid merged commit c8cb0f8 into dotnet:main May 21, 2025
102 of 109 checks passed
@amanasifkhalid amanasifkhalid deleted the skip-cold-loop-opts branch May 21, 2025 03:33
SimaTian pushed a commit that referenced this pull request May 27, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jun 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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.

2 participants