Skip to content

Draft: nir: implement loop invariant code motion (LICM) pass

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

Draft for now since checking for nir_intrinsic_can_reorder is not sufficient and we first need to make use of ACCESS_CAN_SPECULATE.

nir: implement loop invariant code motion (LICM) pass

This simple LICM pass hoists all loop-invariant instructions
from the loops' top-level control flow, skipping any nested CF.
The hoisted instructions are placed right before the loop.

Some GFX11 stats for the LICM pass. To me it looks quite beneficial.

Totals:
MaxWaves: 2415652 -> 2415504 (-0.01%); split: +0.00%, -0.01%
Instrs: 45309794 -> 45296989 (-0.03%); split: -0.06%, +0.04%
CodeSize: 235735256 -> 235634772 (-0.04%); split: -0.08%, +0.04%
VGPRs: 3082480 -> 3083368 (+0.03%); split: -0.01%, +0.04%
SpillSGPRs: 6935 -> 6982 (+0.68%); split: -13.68%, +14.36%
SpillVGPRs: 653 -> 745 (+14.09%)
Scratch: 6959872 -> 6963968 (+0.06%)
Latency: 300361437 -> 298365091 (-0.66%); split: -0.69%, +0.03%
InvThroughput: 48908954 -> 48591557 (-0.65%); split: -0.72%, +0.07%
VClause: 884873 -> 884762 (-0.01%); split: -0.07%, +0.05%
SClause: 1388421 -> 1377818 (-0.76%); split: -0.82%, +0.06%
Copies: 2937998 -> 2944504 (+0.22%); split: -0.25%, +0.48%
Branches: 899234 -> 899368 (+0.01%); split: -0.01%, +0.02%
PreSGPRs: 2445827 -> 2462883 (+0.70%); split: -0.02%, +0.71%
PreVGPRs: 2249572 -> 2250266 (+0.03%); split: -0.00%, +0.03%
VALU: 25207745 -> 25211343 (+0.01%); split: -0.01%, +0.02%
SALU: 4398056 -> 4399755 (+0.04%); split: -0.20%, +0.24%
VMEM: 1598134 -> 1598254 (+0.01%); split: -0.00%, +0.01%
SMEM: 2469245 -> 2460311 (-0.36%)
VOPD: 8801 -> 8791 (-0.11%); split: +0.31%, -0.42%

Totals from 5575 (7.02% of 79395) affected shaders:
MaxWaves: 128709 -> 128561 (-0.11%); split: +0.02%, -0.13%
Instrs: 15018601 -> 15005796 (-0.09%); split: -0.19%, +0.11%
CodeSize: 78159916 -> 78059432 (-0.13%); split: -0.25%, +0.12%
VGPRs: 393568 -> 394456 (+0.23%); split: -0.06%, +0.29%
SpillSGPRs: 4216 -> 4263 (+1.11%); split: -22.51%, +23.62%
SpillVGPRs: 518 -> 610 (+17.76%)
Scratch: 6108160 -> 6112256 (+0.07%)
Latency: 177467906 -> 175471560 (-1.12%); split: -1.17%, +0.05%
InvThroughput: 31979180 -> 31661783 (-0.99%); split: -1.10%, +0.11%
VClause: 306050 -> 305939 (-0.04%); split: -0.19%, +0.15%
SClause: 425042 -> 414439 (-2.49%); split: -2.69%, +0.20%
Copies: 1059298 -> 1065804 (+0.61%); split: -0.71%, +1.32%
Branches: 407064 -> 407198 (+0.03%); split: -0.02%, +0.05%
PreSGPRs: 302651 -> 319707 (+5.64%); split: -0.14%, +5.78%
PreVGPRs: 346505 -> 347199 (+0.20%); split: -0.02%, +0.22%
VALU: 8439279 -> 8442877 (+0.04%); split: -0.03%, +0.07%
SALU: 1523515 -> 1525214 (+0.11%); split: -0.58%, +0.69%
VMEM: 505281 -> 505401 (+0.02%); split: -0.00%, +0.03%
SMEM: 636590 -> 627656 (-1.40%)
VOPD: 3201 -> 3191 (-0.31%); split: +0.84%, -1.16%

 PERCENTAGE DELTAS                   Shaders  MaxWaves   Instrs   CodeSize   VGPRs   SpillSGPRs SpillVGPRs  Scratch   Latency  InvThroughput  VClause   SClause    Copies   Branches  PreSGPRs  PreVGPRs    VALU      SALU      VMEM      SMEM      VOPD   
 assassins_creed_valhalla            830         .       -0.15%    -0.20%    -0.07%    -53.73%       .         .       -2.78%      -3.36%      +0.11%    -0.75%    +0.15%    -0.04%    +1.21%    +0.12%    -0.20%    +0.27%      .       -0.07%    -0.89%  
 control                             4242      -0.01%    -0.02%    -0.02%    +0.05%    +77.27%       .         .       -0.77%      -0.74%      +0.01%    +0.04%    -0.23%      .       +0.86%    +0.03%    +0.02%    -0.15%      .       -0.04%      .     
 cyberpunk_2077                      2221      -0.00%    -0.20%    -0.27%    +0.01%    +74.09%       .         .       -0.45%      -0.59%      +0.04%    -1.13%    -0.64%    +0.01%    +1.15%    +0.05%    -0.06%    -0.57%      .       -0.88%      .     
 dead_space                          400         .       -0.95%    -0.91%      .       +4.05%        .         .       -1.67%      -1.72%      -0.07%    -6.01%    -5.84%    -0.03%    +2.64%    +0.13%    +0.01%    -6.24%      .       -6.16%      .     
 deathloop                           1130      +0.03%    -0.00%    -0.02%    -0.05%    +3.17%        .         .       -0.30%      -0.39%      +0.12%    +0.22%    +0.21%    +0.00%    +3.82%    +0.00%    +0.02%    +0.13%      .         .         .     
 detroit_become_human                995       -0.01%    -0.01%    -0.01%    +0.03%    +86.05%       .         .       -1.10%      -0.65%      +0.01%    -1.48%    +0.08%      .       +2.65%      .       +0.02%    +0.05%      .       -0.23%      .     
 deus_ex_md                          8063        .       -0.03%    -0.04%      .          .          .         .       -0.42%      -0.10%        .       -0.28%    -0.02%      .       +0.10%    +0.00%    -0.00%    -0.02%      .       -0.17%      .     
 doom_eternal                        461         .       -0.17%    -0.27%    +0.06%       .          .         .       -0.67%      -1.17%      -0.44%    -7.60%    +0.97%    -0.00%    +2.76%    +0.17%    +0.08%    +0.29%      .       -4.51%   -40.62%  
 dota2                               3840        .         .         .         .          .          .         .         .           .           .         .         .         .         .         .         .         .         .         .         .     
 dow3                                280         .       +0.00%    +0.01%      .          .          .         .       -0.77%      -0.44%        .       -0.29%    +0.06%      .       +0.35%    +0.04%    +0.01%    +0.01%      .         .         .     
 dredge                              2517        .       +0.06%    +0.07%      .          .          .         .       +0.04%      +0.03%        .       +0.20%    +0.01%      .       +0.82%      .       +0.00%      .         .       -0.01%      .     
 elden_ring                          3733      -0.02%    +0.02%    +0.03%    +0.20%    +94.12%       .         .       -0.21%      -0.15%        .       +0.01%    +0.34%    +0.02%    +0.24%    +0.02%    +0.01%    +0.22%      .         .       -0.14%  
 f1_23                               4006        .       -0.04%    -0.03%      .       -11.11%       .         .       -0.42%      -0.27%      +0.01%    +0.21%    -0.07%    +0.02%    +1.43%    +0.01%    +0.00%    -0.16%      .         .       +0.56%  
 farcry5                             805       -0.04%    -0.05%    -0.07%    +0.11%       .          .         .       -0.88%      -2.32%        .       -1.36%    -0.00%      .       +0.89%    +0.14%    +0.00%    -0.03%      .       -0.26%      .     
 god_of_war                          1029      -0.01%    -0.02%    -0.03%    +0.09%    +3.29%        .         .       -1.23%      -0.44%        .       -0.53%    +0.11%      .       +0.89%    +0.01%    +0.01%    +0.06%      .       -0.34%      .     
 gtav                                2955        .       +0.00%    +0.00%      .          .          .         .       -0.02%      -0.01%        .       -0.01%      .         .       +0.00%      .         .         .         .         .         .     
 heaven_d3d9_strict_float_ultra_4xaa 286         .       +0.00%    -0.00%      .          .          .         .       -0.18%      -0.11%        .       -0.19%    -0.09%      .       +0.05%      .         .       -0.07%      .       -0.02%      .     
 hogwarts_legacy                     887       -0.04%    +0.01%    +0.00%    +0.09%    +50.00%       .         .       -0.54%      -0.60%      +0.02%    -0.13%    +0.21%      .       +0.54%    +0.13%    +0.01%    +0.10%      .       -0.07%      .     
 horizon_zero_dawn                   1800        .       +0.00%    +0.00%      .          .          .         .       -0.00%      -0.00%        .         .       +0.00%      .         .         .       +0.00%      .         .         .         .     
 madmax                              922         .       +0.00%    -0.03%      .          .          .         .       -0.41%      -0.08%        .       -0.03%      .         .       +0.10%      .         .         .         .         .         .     
 metro_exodus                        2627        .       -0.01%    +0.00%      .          .          .         .       -0.23%      -0.22%      -0.01%    -0.05%    -0.07%      .       +0.37%      .       -0.00%    -0.06%      .         .       +1.29%  
 PERCENTAGE DELTAS                   Shaders  MaxWaves   Instrs   CodeSize   VGPRs   SpillSGPRs SpillVGPRs  Scratch   Latency  InvThroughput  VClause   SClause    Copies   Branches  PreSGPRs  PreVGPRs    VALU      SALU      VMEM      SMEM      VOPD   
 monster_hunter_world                1103        .       -0.01%    -0.02%      .          .          .         .       -0.18%      -0.19%      +0.04%    -0.49%    +0.15%    +0.02%    +0.55%    +0.01%    +0.00%    +0.05%      .       -0.08%      .     
 nier                                2422      -0.02%    -0.04%    -0.04%    +0.03%       .          .         .       -0.33%      -0.06%      -0.02%    -0.47%    -0.01%      .       +0.04%    +0.05%    -0.00%    -0.01%      .       -0.24%      .     
 no_mans_sky                         1978        .       -0.00%    -0.00%      .          .          .         .       -0.01%      -0.04%      +0.02%      .       +0.00%      .       +0.00%      .         .       +0.00%      .         .         .     
 parallel_rdp                        539         .       +0.64%    +0.72%    +1.16%     +inf%     +20.63%   +13.11%    -0.56%      +0.12%      +1.14%    -0.08%    +3.73%    +0.21%    +1.91%    +0.11%    +0.92%    +0.09%    +1.34%      .         .     
 persona_5                           1566        .         .         .         .          .          .         .         .           .           .         .         .         .         .         .         .         .         .         .         .     
 q2rtx                               48          .       -0.07%    -0.01%      .          .          .         .       -0.19%      -0.64%        .         .       -0.71%      .       +1.94%    +0.97%    -0.00%    -0.77%      .       -0.04%      .     
 rdr2                                3649      -0.04%    -0.05%    -0.09%    +0.09%    +4.69%        .         .       -1.79%      -0.99%      +0.46%    -1.00%    +0.27%    +0.00%    +1.76%    +0.15%    +0.00%    +0.13%      .       -0.33%    +0.92%  
 redout                              2485        .       -0.01%    -0.00%      .        +inf%        .         .       +0.11%      -0.00%      +0.01%    -0.11%    -0.00%      .       +0.17%    +0.00%    +0.00%    -0.02%      .       -0.05%      .     
 resident_evil_village               1157        .       -0.27%    -0.25%    -0.05%    +2.90%        .         .       -1.60%      -0.44%      -0.09%    -2.40%    -1.98%      .       +0.82%    +0.02%    +0.01%    -1.39%      .       -1.16%      .     
 second_extinction                   437         .       +0.10%    +0.07%      .          .          .         .       -0.20%      -0.25%        .       -0.40%    +1.76%      .       +0.48%    +0.01%      .       +1.16%      .         .         .     
 skyrim_dx9                          3305        .       -0.03%    -0.02%    +0.03%       .          .         .       -0.27%      -0.38%        .       -0.01%    -0.41%      .       +0.99%    +0.03%      .       -0.20%      .       -0.00%      .     
 sniper_elite_5                      4278        .       +0.13%    +0.09%      .          .          .         .       +0.01%      +0.01%      -0.00%      .       +1.29%    +0.01%    +0.03%    +0.02%    +0.00%    +0.90%      .         .         .     
 sottr                               542       -0.01%    +0.00%    -0.01%    +0.04%       .          .         .       +0.10%      +0.25%        .       -0.45%    -0.17%      .       +0.07%    +0.02%    -0.00%    -0.08%      .       -0.00%      .     
 spiderman_remastered                648         .       -0.24%    -0.06%      .          .          .         .       -2.13%      -2.19%        .       -1.90%    -0.54%    +0.01%    +5.47%    +0.01%    -0.03%    -0.82%      .       -1.06%      .     
 talos_principle                     1999        .       +0.01%    -0.03%      .          .          .         .       -0.55%      -0.03%        .       +0.05%    +0.06%      .       +0.08%    +0.00%    +0.00%    +0.02%      .         .         .     
 the_last_of_us_part1                1198      -0.09%    +0.22%    +0.27%    +0.24%    +62.16%       .         .       -0.68%      +0.12%      -0.15%    -0.04%    +1.12%    +0.41%    +1.67%    +0.30%    +0.19%    +0.47%    -0.05%    -0.06%    -1.53%  
 thewitness                          3150        .       +0.00%    -0.00%      .          .          .         .       -0.00%      -0.01%        .         .         .         .         .         .         .         .         .         .         .     
 total_warhammer_3                   632         .       -0.00%    -0.01%      .          .          .         .       -0.08%      -0.01%      -0.01%    -0.33%    +0.01%    -0.04%    +0.20%    -0.00%    -0.00%      .         .       -0.01%    +9.09%  
 war_thunder                         2100        .       -0.02%    +0.03%      .          .          .         .       -1.69%      -0.82%      +0.02%    -0.30%    -0.05%      .       +0.22%    +0.03%    -0.00%    -0.02%      .       -0.02%    -0.32%  
 worldofwarships                     2130        .       -0.04%    -0.04%      .          .          .         .       -1.04%      -0.14%        .       -0.46%    -0.04%      .       +0.90%    +0.02%    +0.00%    -0.05%      .       -0.01%      .     
 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 All affected                        5575      -0.11%    -0.09%    -0.13%    +0.23%    +1.11%     +17.76%    +0.07%    -1.12%      -0.99%      -0.04%    -2.49%    +0.61%    +0.03%    +5.64%    +0.20%    +0.04%    +0.11%    +0.02%    -1.40%    -0.31%  
 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Total                               79395     -0.01%    -0.03%    -0.04%    +0.03%    +0.68%     +14.09%    +0.06%    -0.66%      -0.65%      -0.01%    -0.76%    +0.22%    +0.01%    +0.70%    +0.03%    +0.01%    +0.04%    +0.01%    -0.36%    -0.11%  
Edited by Daniel Schürmann

Merge request reports