microsoft/compiler: Fix codegen when a loop ends in a jump
authorJesse Natalie <jenatali@microsoft.com>
Fri, 9 Dec 2022 19:29:02 +0000 (11:29 -0800)
committerMarge Bot <emma+marge@anholt.net>
Mon, 12 Dec 2022 17:18:45 +0000 (17:18 +0000)
Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/7792
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/20255>

src/microsoft/compiler/nir_to_dxil.c

index f9cb78b613776baee2810330b97fd713633c77e6..1d851a79b31b886eb6810749fde817ca9e6ab3b8 100644 (file)
@@ -5342,9 +5342,10 @@ static bool
 emit_loop(struct ntd_context *ctx, nir_loop *loop)
 {
    nir_block *first_block = nir_loop_first_block(loop);
+   nir_block *last_block = nir_loop_last_block(loop);
 
-   assert(nir_loop_last_block(loop)->successors[0]);
-   assert(!nir_loop_last_block(loop)->successors[1]);
+   assert(last_block->successors[0]);
+   assert(!last_block->successors[1]);
 
    if (!emit_branch(ctx, first_block->index))
       return false;
@@ -5352,7 +5353,12 @@ emit_loop(struct ntd_context *ctx, nir_loop *loop)
    if (!emit_cf_list(ctx, &loop->body))
       return false;
 
-   if (!emit_branch(ctx, first_block->index))
+   /* If the loop's last block doesn't explicitly jump somewhere, then there's
+    * an implicit continue that should take it back to the first loop block
+    */
+   nir_instr *last_instr = nir_block_last_instr(last_block);
+   if ((!last_instr || last_instr->type != nir_instr_type_jump) &&
+       !emit_branch(ctx, first_block->index))
       return false;
 
    return true;