Skip to content

JIT: Fix likelihoods in range test bool opts #116867

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
merged 1 commit into from
Jun 20, 2025

Conversation

amanasifkhalid
Copy link
Member

When optimizing compound conditionals into one check and branch, we'd expect the likelihood of the optimized branch to be the product of the conditionals' likelihoods. We currently get this wrong in range test opts if the second comparison is flipped, yielding behavior like this:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------
BBnum BBid ref try hnd preds           weight     IBC [IL range]   [jump]                            [EH region]        [flags]
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
BB01 [0000]  1                             1    33784 [000..00C)-> BB12(0.407),BB11(0.593) ( cond )                   i IBC
BB11 [0009]  1       BB01                  0.59 20037 [000..001)                           (throw )                   i IBC gcsafe
BB12 [0010]  1       BB01                  0.41 13747 [000..001)-> BB14(0.899),BB08(0.101) ( cond )                   i IBC
BB14 [0012]  3       BB03,BB08,BB12        0.37 12357 [000..021)                           (return)                   i IBC jmp
BB08 [0006]  1       BB12                  0.45 15138 [000..015)-> BB14(0.2),BB03(0.8)     ( cond )                   i IBC
BB03 [0002]  1       BB08                  0        0 [015..019)-> BB14(1)                 (always)                   i IBC rare
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

*************** In optOptimizeBools()
setting likelihood of BB12 -> BB14 from 0.8988597 to 0.0809122
setting likelihood of BB12 -> BB03 to 0.9190878
fgRemoveBlock BB08, unreachable=true

Removing unreachable BB08


---------------------------------------------------------------------------------------------------------------------------------------------------------------------
BBnum BBid ref try hnd preds           weight     IBC [IL range]   [jump]                            [EH region]        [flags]
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
BB01 [0000]  1                             1    33784 [000..00C)-> BB12(0.407),BB11(0.593) ( cond )                   i IBC
BB11 [0009]  1       BB01                  0.59 20037 [000..001)                           (throw )                   i IBC gcsafe
BB12 [0010]  1       BB01                  0.41 13747 [000..001)-> BB14(0.0809),BB03(0.919)  ( cond )                   i IBC
BB14 [0012]  2       BB03,BB12             0.03  1112 [000..021)                           (return)                   i IBC jmp
BB03 [0002]  1       BB12                  0.37 12635 [015..019)-> BB14(1)                 (always)                   i IBC
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Based on what the profile initially looked like, the likelihoods out of BB12 should be flipped.

@Copilot Copilot AI review requested due to automatic review settings June 20, 2025 16:30
@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 Jun 20, 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 the likelihood computation in range tests for bool optimizations when the second comparison is reversed.

  • Moves likelihood assignments outside the if/else branches to ensure a consistent calculation of the branch probabilities.
  • Removes redundant likelihood assignments that led to incorrect flipping of probabilities.
Comments suppressed due to low confidence (2)

src/coreclr/jit/optimizebools.cpp:828

  • Ensure that moving the likelihood assignments outside the if/else block correctly reflects the intended behavior for both cmp2IsReversed cases. Verify that the new structure accurately flips the likelihoods as expected.
    newEdge->setLikelihood(inRangeLikelihood);

src/coreclr/jit/optimizebools.cpp:841

  • Check that the removal of the redundant likelihood assignments in the reversed branch does not inadvertently affect the logic ensuring the branch likelihood is correctly flipped.
        assert(m_b1->TrueTargetIs(notInRangeBb));

Copy link
Contributor

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

@amanasifkhalid
Copy link
Member Author

amanasifkhalid commented Jun 20, 2025

@dotnet/jit-contrib PTAL. Diffs. coreclr_tests on x64 is a big outlier -- this change affects a lot of our HW intrinsics test code:

Top method regressions (percentages):
           2 (10.000% of base) : 529608.dasm - RedundantBranchUnsigned2:Test_Span1(System.Span`1[int],int) (FullOpts)
          57 (8.321% of base) : 441611.dasm - JIT.HardwareIntrinsics.General._Vector128.VectorWidenTest__WidenInt16+DataTable:.ctor(sbyte[],short[],short[],int):this (FullOpts)
          57 (8.321% of base) : 441560.dasm - JIT.HardwareIntrinsics.General._Vector128.VectorWidenTest__WidenUInt16+DataTable:.ctor(byte[],ushort[],ushort[],int):this (FullOpts)
          57 (8.321% of base) : 442963.dasm - JIT.HardwareIntrinsics.General._Vector256.VectorWidenTest__WidenInt16+DataTable:.ctor(sbyte[],short[],short[],int):this (FullOpts)
          57 (8.321% of base) : 442954.dasm - JIT.HardwareIntrinsics.General._Vector256.VectorWidenTest__WidenUInt16+DataTable:.ctor(byte[],ushort[],ushort[],int):this (FullOpts)
          57 (8.321% of base) : 448002.dasm - JIT.HardwareIntrinsics.General._Vector512.VectorWidenTest__WidenInt16+DataTable:.ctor(sbyte[],short[],short[],int):this (FullOpts)
          57 (8.321% of base) : 447993.dasm - JIT.HardwareIntrinsics.General._Vector512.VectorWidenTest__WidenUInt16+DataTable:.ctor(byte[],ushort[],ushort[],int):this (FullOpts)
          57 (8.321% of base) : 457511.dasm - JIT.HardwareIntrinsics.General._Vector64.VectorWidenTest__WidenInt16+DataTable:.ctor(sbyte[],short[],short[],int):this (FullOpts)
          57 (8.321% of base) : 457461.dasm - JIT.HardwareIntrinsics.General._Vector64.VectorWidenTest__WidenUInt16+DataTable:.ctor(byte[],ushort[],ushort[],int):this (FullOpts)
          57 (8.273% of base) : 441577.dasm - JIT.HardwareIntrinsics.General._Vector128.VectorWidenTest__WidenInt32+DataTable:.ctor(short[],int[],int[],int):this (FullOpts)
          57 (8.273% of base) : 441645.dasm - JIT.HardwareIntrinsics.General._Vector128.VectorWidenTest__WidenUInt32+DataTable:.ctor(ushort[],uint[],uint[],int):this (FullOpts)
          57 (8.273% of base) : 442957.dasm - JIT.HardwareIntrinsics.General._Vector256.VectorWidenTest__WidenInt32+DataTable:.ctor(short[],int[],int[],int):this (FullOpts)
          57 (8.273% of base) : 442969.dasm - JIT.HardwareIntrinsics.General._Vector256.VectorWidenTest__WidenUInt32+DataTable:.ctor(ushort[],uint[],uint[],int):this (FullOpts)
          57 (8.273% of base) : 447996.dasm - JIT.HardwareIntrinsics.General._Vector512.VectorWidenTest__WidenInt32+DataTable:.ctor(short[],int[],int[],int):this (FullOpts)
          57 (8.273% of base) : 448008.dasm - JIT.HardwareIntrinsics.General._Vector512.VectorWidenTest__WidenUInt32+DataTable:.ctor(ushort[],uint[],uint[],int):this (FullOpts)
          57 (8.273% of base) : 457477.dasm - JIT.HardwareIntrinsics.General._Vector64.VectorWidenTest__WidenInt32+DataTable:.ctor(short[],int[],int[],int):this (FullOpts)
          57 (8.273% of base) : 457545.dasm - JIT.HardwareIntrinsics.General._Vector64.VectorWidenTest__WidenUInt32+DataTable:.ctor(ushort[],uint[],uint[],int):this (FullOpts)
          57 (8.261% of base) : 442960.dasm - JIT.HardwareIntrinsics.General._Vector256.VectorWidenTest__WidenInt64+DataTable:.ctor(int[],long[],long[],int):this (FullOpts)
          57 (8.261% of base) : 447999.dasm - JIT.HardwareIntrinsics.General._Vector512.VectorWidenTest__WidenInt64+DataTable:.ctor(int[],long[],long[],int):this (FullOpts)
          57 (8.261% of base) : 457528.dasm - JIT.HardwareIntrinsics.General._Vector64.VectorWidenTest__WidenDouble+DataTable:.ctor(float[],double[],double[],int):this (FullOpts)

@amanasifkhalid amanasifkhalid merged commit 430ff53 into dotnet:main Jun 20, 2025
106 of 110 checks passed
@amanasifkhalid amanasifkhalid deleted the bool-opts-likelihoods branch June 20, 2025 18:37
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