Skip to content

JIT: Fix hang when a switch targets an empty-block cycle - #133686

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:fix-switch-empty-block-cycle
Sep 14, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:fix-switch-empty-block-cycle

Conversation

@EgorBo

@EgorBo EgorBo commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

Fixes #133677.

fgOptimizeSwitchBranches can repeatedly redirect a switch edge around a cycle of empty unconditional blocks without making progress. Share BitVec-based cycle detection with fgOptimizeBranchToEmptyUnconditional and leave cyclic targets unchanged.

Add Runtime_133677.cs to the globbed Regression_ro_2 runner. The test emits the two-block cycle directly, so it needs neither unoptimized C# compilation nor environment overrides or a separate project.

Validation: the regression hangs with the baseline JIT and passes with the fixed Checked JIT, including default tiering and JitStress=2. Cached benchmarks.run. replay has no failures, and the asm comparison has no diffs (37 contexts lack recorded data).

The original repro was in C# + debug attribute, converted to IL to avoid the dependency on Roslyn optimizations.

Share visited-block cycle detection between branch and switch optimization, and add a single-file Reflection.Emit regression for the two-block cycle.

Fixes dotnet#133677

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 96e8fae2-5073-4d66-8b12-6b26a9a75dd2
Copilot AI lite review requested due to automatic review settings September 11, 2026 12:04
@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 11, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 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 requested a review from AndyAyersMS September 11, 2026 12:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Retain allocation-free cycle detection or reuse a scratch bit vector to avoid repeated graph-sized allocations.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Fixes a JIT hang caused by switch targets entering cycles of empty unconditional blocks.

Changes:

  • Adds shared cycle detection for branch and switch optimization.
  • Declares the cycle-detection helper.
  • Adds a Reflection.Emit regression test.
File summaries
File Summary
src/tests/JIT/Regression_ro_2/Runtime_133677.cs Adds regression coverage for the two-block cycle.
src/coreclr/jit/fgopt.cpp Detects cyclic empty-block chains before redirecting edges.
src/coreclr/jit/compiler.h Declares the cycle-detection helper.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread src/coreclr/jit/fgopt.cpp Resolved
@EgorBo

EgorBo commented Sep 11, 2026 •

Copy link
Copy Markdown
Member Author

PTAL @AndyAyersMS @dotnet/jit-contrib No diffs

The original repro was in C# + debug attribute, converted to IL to avoid the dependency on Roslyn optimizations.

@AndyAyersMS AndyAyersMS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You could probably add a C# test under the regression_d or Regression_NoOptimize_d projects, but not sure it's worth the trouble.

@EgorBo

EgorBo commented Sep 14, 2026

Copy link
Copy Markdown
Member Author

/ba-g unrelated mono failures

@EgorBo
EgorBo merged commit 7ee2d1b into dotnet:main Sep 14, 2026
144 of 146 checks passed
@EgorBo
EgorBo deleted the fix-switch-empty-block-cycle branch September 14, 2026 11:02
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 15, 2026
jtschuster pushed a commit to jtschuster/runtime that referenced this pull request Sep 18, 2026
Fixes dotnet#133677.

`fgOptimizeSwitchBranches` can repeatedly redirect a switch edge around
a cycle of empty unconditional blocks without making progress. Share
`BitVec`-based cycle detection with
`fgOptimizeBranchToEmptyUnconditional` and leave cyclic targets
unchanged.

Add `Runtime_133677.cs` to the globbed `Regression_ro_2` runner. The
test emits the two-block cycle directly, so it needs neither unoptimized
C# compilation nor environment overrides or a separate project.

Validation: the regression hangs with the baseline JIT and passes with
the fixed Checked JIT, including default tiering and `JitStress=2`.
Cached `benchmarks.run.` replay has no failures, and the asm comparison
has no diffs (37 contexts lack recorded data).


The original [repro
](dotnet#133677 in C# + debug
attribute, converted to IL to avoid the dependency on Roslyn
optimizations.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 96e8fae2-5073-4d66-8b12-6b26a9a75dd2
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 15, 2026
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.

JIT: (bug) hang in fgOptimizeSwitchBranches on a multi-block empty branch cycle

3 participants