nir: Skip opt_if_merge when next_if has block ending in a jump

Similar to commit 6cef804067 ("nir/opt_if: fix opt_if_merge when
destination branch has a jump"), we shouldn't combine if statements when
the second if-then-else has a block that ends in a jump.

This fixes a case where opt_if_merge combines

    if (cond) {
        [then-block-1]
    } else {
        [else-block-1]
    }

    if (cond) {
        [then-block-2]
    } else {
        [else-block-2]
    }

where `then-block-2` or `else-block-2` ends in a jump. The phi nodes
following the control flow will be incorrectly updated to have an input
from a block that is not a predecessor.

Fixes: 4d3f6cb973 ("nir: merge some basic consecutive ifs")
Reviewed-by: Rhys Perry <pendingchaos02@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30629>
(cherry picked from commit d2e6be94ae)
This commit is contained in:
Matt Turner 2024-08-12 15:26:57 -04:00 committed by Eric Engestrom
parent 6a1c3de8d5
commit 5c87e824fc
2 changed files with 8 additions and 1 deletions

View file

@ -2884,7 +2884,7 @@
"description": "nir: Skip opt_if_merge when next_if has block ending in a jump",
"nominated": true,
"nomination_type": 1,
"resolution": 0,
"resolution": 1,
"main_sha": null,
"because_sha": "4d3f6cb9739dfeaf9605fcd2f5318e03acf5066e",
"notes": null

View file

@ -1159,6 +1159,13 @@ opt_if_merge(nir_if *nif)
nir_block_ends_in_jump(nir_if_last_else_block(nif)))
return false;
/* This optimization is not prepared to handle updating phis other than
* immediately after the second if-statement.
*/
if (nir_block_ends_in_jump(nir_if_last_then_block(next_if)) ||
nir_block_ends_in_jump(nir_if_last_else_block(next_if)))
return false;
simple_merge_if(nif, next_if, true, true);
simple_merge_if(nif, next_if, false, false);