Skip to content

JIT: Convert multi-target switches to branchless checks#124567

Merged
JulieLeeMSFT merged 4 commits intodotnet:mainfrom
JulieLeeMSFT:optPatternMatchSwitch
Feb 24, 2026
Merged

JIT: Convert multi-target switches to branchless checks#124567
JulieLeeMSFT merged 4 commits intodotnet:mainfrom
JulieLeeMSFT:optPatternMatchSwitch

Conversation

@JulieLeeMSFT
Copy link
Member

@JulieLeeMSFT JulieLeeMSFT commented Feb 18, 2026

Fixes #123858.

This PR improves JIT codegen for multi-target switch-style tests by converting eligible cases into branchless checks. The goal is to produce optimal codegen that matches the intent of C# pattern matching with or (e.g., x is A or B or C).

When a switch has exactly 2 unique successors (all non-default cases target one block, default targets another), convert it from Switch to an unsigned range comparison (In fgOptimizeSwitchBranches).
When both targets are simple return blocks, fgFoldCondToReturnBlock further folds this into branchless return.

Example

        private static bool IsLetterCategory(UnicodeCategory uc)
        {
            return uc == UnicodeCategory.UppercaseLetter
                 || uc == UnicodeCategory.LowercaseLetter
                 || uc == UnicodeCategory.TitlecaseLetter
                 || uc == UnicodeCategory.ModifierLetter
                 || uc == UnicodeCategory.OtherLetter;
        }

Before

cmp      ecx, 4
ja       SHORT G_M22758_IG05
mov      eax, 1
ret
G_M22758_IG05:
xor      eax, eax
ret

After

cmp      ecx, 4
setbe    al
movzx    rax, al
ret

Details

  • The comparison direction is chosen to GT_LT.
  • Edge dup counts are fixed up after conversion.
  • Added tests for zero-based and non-zero-based consecutive ranges.

ASMDiffs

  • SPMI asmdiffs show code size improvements of 83 bytes.

@JulieLeeMSFT JulieLeeMSFT added this to the 11.0.0 milestone Feb 18, 2026
Copilot AI review requested due to automatic review settings February 18, 2026 19:25
@JulieLeeMSFT JulieLeeMSFT added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Feb 18, 2026
@dotnet-policy-service
Copy link
Contributor

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

@JulieLeeMSFT
Copy link
Member Author

@EgorBo, PTAL.
CC @dotnet/jit-contrib.

Copy link
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

This PR optimizes multi-target switches where all non-default cases target a single block (distinct from the default target) by converting them into unsigned range comparisons. This enables branchless code generation when both targets are simple return blocks, addressing the disparity between pattern matching and explicit equality comparisons noted in issue #123858.

Changes:

  • Adds logic in fgOptimizeSwitchBranches to detect and optimize switches with exactly 2 unique successors
  • Transforms such switches into unsigned LE/GT comparisons, choosing the direction to favor fall-through
  • Adds comprehensive test coverage for both zero-based and non-zero-based consecutive ranges

Reviewed changes

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

File Description
src/coreclr/jit/fgopt.cpp Implements the switch-to-range-check optimization with edge weight/dup count fixup and profile weight updates
src/tests/JIT/opt/OptSwitchRecognition/optSwitchRecognition.cs Adds tests for zero-based (0-4) and non-zero-based (10-14) consecutive value ranges with comprehensive boundary cases

Copilot AI review requested due to automatic review settings February 18, 2026 19:41
Copy link
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

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

Copilot AI review requested due to automatic review settings February 18, 2026 19:48
Copy link
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

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

Copilot AI review requested due to automatic review settings February 18, 2026 23:01
Copy link
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

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

@JulieLeeMSFT
Copy link
Member Author

@EgorBo, I addressed all comments. It's ready to review.

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.

LGTM with a few suggestions

Copilot AI review requested due to automatic review settings February 23, 2026 19:53
Copy link
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

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

@JulieLeeMSFT
Copy link
Member Author

/ba-g Failures are timeouts in Android pipeline. Not related to this PR.

@JulieLeeMSFT JulieLeeMSFT merged commit c94ed87 into dotnet:main Feb 24, 2026
133 of 135 checks passed
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.

IL and asm differences from pattern matching vs comparisons

3 participants