Skip to content

JIT: allow if-conversion inside loops when the select is not loop-carried - #138237

Open
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:ifcvt-loops
Open

EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:ifcvt-loops

Conversation

@EgorBo

@EgorBo EgorBo commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

If-conversion currently bails on any block inside a loop (detected via block weight or reachability). This relaxes it: inside a loop, a STORE_LCL_VAR select is still converted unless a cheap scan (MayBeLoopCarried) finds that the selected value, or a local computed from it in the following blocks, is either:

  • read in the JTRUE block before being redefined (feeds the next iteration's condition), or
  • used as an address (indirection / array length) - with a branch the CPU can speculate past that load, a cmov serializes it.

GT_RETURN selects are always allowed.

Benchmarks from https://ben-chmarks.com/gemini-neon (C# solutions, local drivers), ms, base -> diff:

heap (Heap.cs)

Case Base Diff
K-way merge 197 117 (-41%)
Top k of a stream 66 47 (-28%)
Hold (replace) 11.5 9.7 (-16%)
Push / Pop neutral neutral

union-find

Case Base Diff
Set sizes 214 205 (-4%)
Balanced merging 150 149 (neutral, regressed when address-feeding selects were converted)

Converting address-feeding selects too gives bigger wins in some cases (e.g. rbtree lookups -25..45%, union-find Set sizes 211 -> 89) but regresses predictable-branch cases such as union-find Balanced merging (145 -> 190), so the heuristic keeps those as branches for now.

SPMI diffs

…ried

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2dce5dc0-e40b-43a6-8407-b9340ddeb589
@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 24, 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 marked this pull request as ready for review November 25, 2026 00:30
@EgorBo

EgorBo commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

@AndyAyersMS @jakobbotsch @dotnet/jit-contrib any ideas if it can be simplified/to be done differently? Diffs imply it's not TP heavy. The change is purely a heuristic so presumably it's not about correctness.

Motivation: AI think it's one of the bottlenecks in https://ben-chmarks.com/gemini-neon benchmark (I know it's silly & C# is heavily unsafe there, but we seem to know our cmov+loop is problematic and has to be improved).

@AndyAyersMS

Copy link
Copy Markdown
Member

Can you also check the examples in #96380 and #96593

@dhartglassMSFT

Copy link
Copy Markdown
Contributor

Can you also check the examples in #96380 and #96593

There's also a function in Random that would make a good positive test as something we want if-converted, and don't when it's inlined
#131714

@EgorBo

EgorBo commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

@AndyAyersMS @dhartglassMSFT checked the examples from #96380, #96593, and #131714 on x64 and arm64:

Local x64 Release BDN (Ryzen 9 7950X, base -> PR):

  • #96593 ParserParse: 13.53 -> 13.99 μs (1.03±0.03)
  • #96593 AggregateLoop (min/max saturate early on random input): 1.97 -> 1.99 μs (1.01±0.02)
  • #96380 Iterate: 2.05 -> 2.03 μs (0.99, unchanged)
  • #96380 CStrCaseCmp: 1.82 -> 1.77 μs (0.97, unchanged)
  • #96380 SumClamped: 2.31 -> 2.21 μs (0.96±0.06, unchanged)
  • #131714 RandomInlined: 8.26 -> 8.38 μs (1.01, unchanged)

@AndyAyersMS

Copy link
Copy Markdown
Member

Maybe try those examples both ways, and see which actually performs better? I suspect loop carried will only matter in fairly tight loops?

Or have AI create a raft of test cases maybe?

This branch has not been deployed

No deployments
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