Skip to content

JIT: Unprotect handler entry after finally removal #115907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

amanasifkhalid
Copy link
Member

Found while working on #115850. Suppose we have the following flowgraph:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------
BBnum BBid ref try hnd preds           weight   [IL range]   [jump]                            [EH region]        [flags]
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
BB01 [0000]  1                             1    [000..00D)-> BB02(1)                 (always)                   i
BB02 [0001]  1  0    BB01                  1    [00D..00F)-> BB03(1)                 (callf ) T0    try {       i keep
BB07 [0006]  1  0    BB05                  1    [???..???)-> BB08(1)                 (callfr) T0    }           i internal
BB08 [0007]  1       BB07                  1    [???..???)-> BB06(1)                 (ALWAYS)                   i internal KEEP
BB03 [0002]  2     0 BB02                  1    [00F..012)-> BB05(0.5),BB04(0.5)     ( cond )    H0 finally {   i keep
BB04 [0003]  1     0 BB03                  1    [012..018)-> BB05(1)                 (always)    H0             i
BB05 [0004]  2     0 BB03,BB04             1    [018..019)-> BB07(1)                 (finret)    H0 }           i
BB06 [0005]  1       BB08                  1    [019..01A)                           (return)                   i
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

fgRemoveEmptyTry will remove the BBF_DONT_REMOVE guard from the try entry if it decides to remove the region, but it won't do so for the corresponding finally entry:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------
BBnum BBid ref try hnd preds           weight   [IL range]   [jump]                            [EH region]        [flags]
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
BB01 [0000]  1                             1    [000..00D)-> BB02(1)                 (always)                   i
BB02 [0001]  1       BB01                  1    [00D..00F)-> BB03(1)                 (always)                   i
BB08 [0007]  1       BB05                  1    [???..???)-> BB06(1)                 (always)                   i internal
BB03 [0002]  1       BB02                  1    [00F..012)-> BB05(0.5),BB04(0.5)     ( cond )                   i keep
BB04 [0003]  1       BB03                  1    [012..018)-> BB05(1)                 (always)                   i
BB05 [0004]  2       BB03,BB04             1    [018..019)-> BB08(1)                 (always)                   i
BB06 [0005]  1       BB08                  1    [019..01A)                           (return)                   i
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

If we remove the guard for the finally entry in this case, loop inversion will make it unreachable, and flow opts will remove it entirely, rather than converting it into an unnecessary BBJ_THROW. This change enables that.

@Copilot Copilot AI review requested due to automatic review settings May 22, 2025 16:45
@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 May 22, 2025
Copy link
Contributor

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

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 ensures that the handler entry block loses its BBF_DONT_REMOVE guard when an empty try is removed, and it cleans up duplicate flag removals in the cloned finally logic.

  • Unprotect handler entry block in fgRemoveEmptyTry
  • Consolidate BBF_DONT_REMOVE removals in fgCloneFinally

@amanasifkhalid
Copy link
Member Author

@dotnet/jit-contrib PTAL. Small diffs. Thanks!

@amanasifkhalid amanasifkhalid enabled auto-merge (squash) May 22, 2025 20:00
@amanasifkhalid
Copy link
Member Author

/ba-g android build stuck

@amanasifkhalid amanasifkhalid merged commit 17a94e2 into dotnet:main May 22, 2025
107 of 109 checks passed
@amanasifkhalid amanasifkhalid deleted the remove-handler-protection branch May 22, 2025 23:07
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.

2 participants