Skip to content

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Aug 11, 2025

Fixes a codegen issue @stephentoub noticed in #107499

There were 2 issues:

  1. Phase ordering - "optimize bools" phase glues BBJ_COND(return true/return false) back to BBJ_RETURN, so switch recognition should be earlier (and it used to be earlier)
  2. Empty BBJ_ALWAYS fallthrough blocks confuse the optimizer - I didn't want to call fgUpdateFlowGraph just because of them so I applied a fix here.
  3. I enabled the switch-recognition for ARM64, just bumped the number of checks (driven by diffs). Still had to keep it disabled for ARM32

Diffs

@Copilot Copilot AI review requested due to automatic review settings August 11, 2025 22:01
@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 Aug 11, 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 fixes a codegen issue in the JIT's switch recognition optimization by addressing two problems: phase ordering and handling of empty fallthrough blocks. The optimization was running too late in the compilation pipeline, after the "optimize bools" phase had already converted conditional jumps back to returns, making switch recognition ineffective.

Key changes:

  • Moved switch recognition phase to run before boolean optimization phase
  • Added logic to skip empty BBJ_ALWAYS fallthrough blocks during switch pattern matching

Reviewed Changes

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

File Description
src/coreclr/jit/switchrecognition.cpp Added SkipFallthroughBlocks helper function and updated target resolution to handle empty fallthrough blocks
src/coreclr/jit/compiler.cpp Moved PHASE_SWITCH_RECOGNITION to run before PHASE_OPTIMIZE_BOOLS

Copy link
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 draft August 11, 2025 22:11
@EgorBo EgorBo marked this pull request as ready for review August 18, 2025 13:14
@EgorBo
Copy link
Member Author

EgorBo commented Aug 18, 2025

PTAL @dotnet/jit-contrib fixes an optimization that someone broke (see PR desc).

@EgorBo EgorBo requested a review from a team August 18, 2025 13:16
Copy link
Contributor

@amanasifkhalid amanasifkhalid left a comment

Choose a reason for hiding this comment

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

fixes an optimization that someone broke (see PR desc).

That was probably me. Thanks for fixing this!

@EgorBo
Copy link
Member Author

EgorBo commented Sep 4, 2025

@AndyAyersMS I've addressed your feedback, looks like Aman's approval is no longer green, so can you approve? thanks

@EgorBo EgorBo requested a review from AndyAyersMS September 4, 2025 11:56
@EgorBo EgorBo enabled auto-merge (squash) September 5, 2025 05:56
@EgorBo EgorBo merged commit 203b67e into dotnet:main Sep 5, 2025
113 checks passed
@EgorBo EgorBo deleted the fix-switchrecognition branch September 5, 2025 05:56
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