diff --git a/src/compiler/nir/nir_builder.c b/src/compiler/nir/nir_builder.c index 8a93394730f..3e2a64ec976 100644 --- a/src/compiler/nir/nir_builder.c +++ b/src/compiler/nir/nir_builder.c @@ -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) { diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h index 661c70d8f16..0fbad8e0fdb 100644 --- a/src/compiler/nir/nir_builder.h +++ b/src/compiler/nir/nir_builder.h @@ -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; }