Skip to content

nir/opt_if: optimize phis between similar IFs

Daniel Schürmann requested to merge daniel-schuermann/mesa:nir_phi_undef into main

Edit: For the record: There are two more optimization possibilities for the instructions between similar IFs:

  • similar to peephole_select: if it's worth it, we can duplicated the code into both branches. unrelated instructions can be moved down.
  • if there are only phis, we can merge the branches. The code mentions register pressure issues when doing so. This needs to be investigated further. Maybe we can check if the phis are only used inside the following IF.

This small optimization targets phis between two IF statements with the same condition. If the phi dst is only used on one branch leg, the 'unused' phi source gets replaced with undef.

 PERCENTAGE DELTAS                Shaders  VGPRs    CodeSize Instrs   Cycles   VMEM     SMEM     VClause  Copies   Branches PreVGPRs 
 assassins_creed_origins          2114        .     -0.00%   -0.00%   -0.00%   +0.03%      .        .     -0.04%   -0.04%      .     
 battlefront2                     4568        .        .        .        .        .        .        .        .        .        .     
 britannia                        469         .        .        .        .        .        .        .        .        .        .     
 control_msaa4x                   752         .        .        .        .        .        .        .        .        .        .     
 control_no_msaa                  752         .        .        .        .        .        .        .        .        .        .     
 dark_souls_1_remastered          4249        .        .        .        .        .        .        .        .        .        .     
 dark_souls_2_sotfs               6876        .        .        .        .        .        .        .        .        .        .     
 dark_souls_3                     8935     -0.00%   -0.00%   -0.00%   -0.00%      .        .        .     -0.00%   -0.00%   -0.00%   
 death_stranding                  1467        .     +0.00%   +0.00%   +0.00%      .        .        .     +0.00%      .        .     
 detroit_become_human             19959       .        .        .        .        .        .        .        .        .        .     
 deus_ex_md                       8063        .     -0.00%   -0.00%   -0.00%   -0.00%      .        .     -0.01%   -0.06%      .     
 dirt4                            4078        .        .        .        .        .        .        .        .        .        .     
 doom_2016                        2963        .        .        .        .        .        .        .        .        .        .     
 doom_eternal                     1211        .        .        .        .        .        .        .        .        .        .     
 dota2                            1015        .        .        .        .        .        .        .        .        .        .     
 dow3                             282         .        .        .        .        .        .        .        .        .        .     
 evilwithin_demo                  742         .        .        .        .        .        .        .        .        .        .     
 f12017                           5503        .     -0.00%   -0.00%   -0.00%      .        .     +0.00%   -0.01%   -0.04%      .     
 gtav                             2991        .        .        .        .        .        .        .        .        .        .     
 hitman                           2785        .        .        .        .        .        .        .        .        .        .     
 PERCENTAGE DELTAS                Shaders  VGPRs    CodeSize Instrs   Cycles   VMEM     SMEM     VClause  Copies   Branches PreVGPRs 
 madmax                           948         .        .        .        .        .        .        .        .        .        .     
 nier                             5492        .     -0.01%   -0.01%   -0.06%   -0.00%   -0.01%   -0.01%   -0.07%      .     -0.00%   
 parallel-rdp/small_subgroup      246      -0.09%   -0.00%   -0.00%   -0.00%   +0.01%      .        .     +0.02%      .     -0.04%   
 parallel-rdp/small_uber_subgroup 55          .        .        .        .        .        .        .        .        .        .     
 parallel-rdp/subgroup            327         .        .        .        .        .        .        .        .        .        .     
 parallel-rdp/uber_subgroup       55          .        .        .        .        .        .        .        .        .        .     
 rdr2                             3888        .     -0.01%   -0.01%   -0.00%   -0.00%      .        .     -0.08%   -0.03%      .     
 redout                           2520        .        .        .        .        .        .        .        .        .        .     
 rottr                            8130        .     -0.00%   -0.00%   -0.00%      .        .        .     -0.01%   -0.02%      .     
 sottr_demo                       5711        .     -0.00%   -0.00%   -0.00%      .        .        .     -0.01%   -0.00%      .     
 strange_brigade                  1417        .     -0.04%   -0.05%   -0.05%      .        .        .     -0.64%      .        .     
 talos                            794         .        .        .        .        .        .        .        .        .        .     
 thewitness                       3151        .        .        .        .        .        .        .        .        .        .     
 threekingdoms                    847         .        .        .        .        .        .        .        .        .        .     
 tomb_raider                      16232       .        .        .        .        .        .        .        .        .        .     
 warhammer2                       1803        .        .        .        .        .        .        .        .        .        .     
 wolfenstein2_fp16                1012        .        .        .        .        .        .        .        .        .        .     
 worldofwarships                  5590        .        .        .        .        .        .        .        .        .        .     
 youngblood                       843         .        .        .        .        .        .        .        .        .        .     
 youngblood_fp16                  682         .        .        .        .        .        .        .        .        .        .     
 -------------------------------------------------------------------------------------------------------------------------------------
 All affected                     177      -0.09%   -0.35%   -0.45%   -0.63%   +0.14%   -0.15%   -0.10%   -3.94%   -1.13%   -0.14%   
 -------------------------------------------------------------------------------------------------------------------------------------
 Total                            139517   -0.00%   -0.00%   -0.00%   -0.00%   +0.00%   -0.00%   -0.00%   -0.01%   -0.00%   -0.00%   

The effect isn't overwhelming, but it's simple and 2 NieR shaders safe 1/3 of the instructions.

Edited by Daniel Schürmann

Merge request reports