Skip to content

Conversation

jakobbotsch
Copy link
Member

  • Follow unique preds when locating the init statement for the iterator variable
  • Generalize zero-trip detection by similarly following unique preds

This at least allows us to analyze the loop iteration here: #119277 (comment)

However, we still do not unroll because we bail out on this condition in unrolling:

// The loop test must be both an exit and a backedge.
// FlowGraphNaturalLoop::AnalyzeIteration ensures it is an exit but we must
// make sure it is a backedge so that we can legally redirect it to the
// next iteration. If it isn't a backedge then redirecting it would skip
// all code between the loop test and the backedge.
assert(loop->ContainsBlock(iterInfo.TestBlock->GetTrueTarget()) !=
loop->ContainsBlock(iterInfo.TestBlock->GetFalseTarget()));
if (!iterInfo.TestBlock->TrueTargetIs(loop->GetHeader()) && !iterInfo.TestBlock->FalseTargetIs(loop->GetHeader()))
{
JITDUMP("Failed to unroll loop " FMT_LP ": test block is not a backedge\n", loop->GetIndex());
return false;
}

I think we truly do need to improve heuristics for inversion to fix the case.

- Follow unique preds when locating the init statement for the iterator
  variable
- Generalize zero-trip detection by similarly following unique preds
@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 Sep 10, 2025
@jakobbotsch jakobbotsch marked this pull request as ready for review September 11, 2025 10:50
@Copilot Copilot AI review requested due to automatic review settings September 11, 2025 10:50
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 generalizes pattern-based induction variable (IV) analysis in the JIT compiler to improve loop optimization capabilities. The changes focus on following unique predecessors when locating initialization statements and generalizing zero-trip detection.

Key changes:

  • Remove init extraction from optExtractInitTestIncr and create a new focused method optExtractTestIncr
  • Add new methods FindConstInit and HasZeroTripTest that follow unique predecessors to locate initialization and zero-trip tests
  • Add gtTreeHasLocalStore utility to check for stores affecting specified locals

Reviewed Changes

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

File Description
src/coreclr/jit/optimizer.cpp Refactored to remove init extraction logic from optExtractInitTestIncr, renamed to optExtractTestIncr
src/coreclr/jit/gentree.cpp Added new gtTreeHasLocalStore method to detect stores affecting a local variable
src/coreclr/jit/flowgraph.cpp Added FindConstInit and HasZeroTripTest methods, removed old init matching logic
src/coreclr/jit/compiler.h Updated method signatures and added new method declarations

@jakobbotsch
Copy link
Member Author

cc @dotnet/jit-contrib PTAL @AndyAyersMS

Diffs. This mostly results in more loops we can unroll. Comparing on benchmarks.run_pgo, we go from 1443 cloned, 260 unrolled to 1445 cloned, 342 unrolled with this change.

@jakobbotsch jakobbotsch merged commit 6c2d1f8 into dotnet:main Sep 12, 2025
112 checks passed
@jakobbotsch jakobbotsch deleted the improve-pattern-iv-analysis branch September 12, 2025 08:25
@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 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