nir/builder: Correctly handle decl_reg or undef as the first instruction

These are both handled by inserting them directly at the top of the
nir_function_impl.  However, if the cursor is already at the top, it
never gets updated so we end up inserting other stuff after the newly
inserted undef or decl_reg.  It's an odd edge case to be sure but I hit
it with my new NIR CF pass for NAK.

Fixes: 1be4c61c95 ("nir/builder: Add a helper for creating undefs")
Reviewed-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Daniel Schürmann <daniel@schuermann.dev>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/28300>
This commit is contained in:
Faith Ekstrand 2024-03-18 18:12:41 -05:00 committed by Marge Bot
parent b069151e62
commit a782809f81
2 changed files with 19 additions and 4 deletions

View file

@ -379,6 +379,22 @@ nir_builder_instr_insert(nir_builder *build, nir_instr *instr)
build->cursor = nir_after_instr(instr);
}
void
nir_builder_instr_insert_at_top(nir_builder *build, nir_instr *instr)
{
nir_cursor top = nir_before_impl(build->impl);
const bool at_top = build->cursor.block != NULL &&
nir_cursors_equal(build->cursor, top);
nir_instr_insert(top, instr);
if (build->update_divergence)
nir_update_instr_divergence(build->shader, instr);
if (at_top)
build->cursor = nir_after_instr(instr);
}
void
nir_builder_cf_insert(nir_builder *build, nir_cf_node *cf)
{

View file

@ -182,6 +182,7 @@ nir_shader_intrinsics_pass(nir_shader *shader,
}
void nir_builder_instr_insert(nir_builder *build, nir_instr *instr);
void nir_builder_instr_insert_at_top(nir_builder *build, nir_instr *instr);
static inline nir_instr *
nir_builder_last_instr(nir_builder *build)
@ -250,9 +251,7 @@ nir_undef(nir_builder *build, unsigned num_components, unsigned bit_size)
if (!undef)
return NULL;
nir_instr_insert(nir_before_impl(build->impl), &undef->instr);
if (build->update_divergence)
nir_update_instr_divergence(build->shader, &undef->instr);
nir_builder_instr_insert_at_top(build, &undef->instr);
return &undef->def;
}
@ -1832,7 +1831,7 @@ nir_decl_reg(nir_builder *b, unsigned num_components, unsigned bit_size,
nir_intrinsic_set_divergent(decl, true);
nir_def_init(&decl->instr, &decl->def, 1, 32);
nir_instr_insert(nir_before_impl(b->impl), &decl->instr);
nir_builder_instr_insert_at_top(b, &decl->instr);
return &decl->def;
}