broadcom/compiler: make several passes to return a progress

Two advantages:
   * When using NIR_DEBUG=nir_print_xx, will print outcome only if
     there is a change
   * We can use NIR_PASS(_, ...) instead of NIR_PASS_V, that has
     slightly more validation checks.

This includes:
  * v3d_nir_lower_image_load_store
  * v3d_nir_lower_io
  * v3d_nir_lower_line_smooth
  * v3d_nir_lower_load_store_bitsize
  * v3d_nir_lower_robust_buffer_access
  * v3d_nir_lower_scratch
  * v3d_nir_lower_txf_ms

As we are here we also simplify some of them by using the
nir_shader_instructions_pass helper.

Reviewed-by: Iago Toral Quiroga <itoral@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17609>
This commit is contained in:
Alejandro Piñeiro 2022-07-19 13:28:55 +02:00 committed by Marge Bot
parent 81ca0b4191
commit 0a50330c3d
9 changed files with 143 additions and 123 deletions

View File

@ -1156,14 +1156,14 @@ bool vir_opt_redundant_flags(struct v3d_compile *c);
bool vir_opt_small_immediates(struct v3d_compile *c);
bool vir_opt_vpm(struct v3d_compile *c);
bool vir_opt_constant_alu(struct v3d_compile *c);
void v3d_nir_lower_io(nir_shader *s, struct v3d_compile *c);
void v3d_nir_lower_line_smooth(nir_shader *shader);
void v3d_nir_lower_logic_ops(nir_shader *s, struct v3d_compile *c);
void v3d_nir_lower_robust_buffer_access(nir_shader *shader, struct v3d_compile *c);
void v3d_nir_lower_scratch(nir_shader *s);
void v3d_nir_lower_txf_ms(nir_shader *s, struct v3d_compile *c);
void v3d_nir_lower_image_load_store(nir_shader *s);
void v3d_nir_lower_load_store_bitsize(nir_shader *s, struct v3d_compile *c);
bool v3d_nir_lower_io(nir_shader *s, struct v3d_compile *c);
bool v3d_nir_lower_line_smooth(nir_shader *shader);
bool v3d_nir_lower_logic_ops(nir_shader *s, struct v3d_compile *c);
bool v3d_nir_lower_robust_buffer_access(nir_shader *shader, struct v3d_compile *c);
bool v3d_nir_lower_scratch(nir_shader *s);
bool v3d_nir_lower_txf_ms(nir_shader *s, struct v3d_compile *c);
bool v3d_nir_lower_image_load_store(nir_shader *s);
bool v3d_nir_lower_load_store_bitsize(nir_shader *s, struct v3d_compile *c);
void v3d33_vir_vpm_read_setup(struct v3d_compile *c, int num_components);
void v3d33_vir_vpm_write_setup(struct v3d_compile *c);

View File

@ -84,7 +84,7 @@ pack_bits(nir_builder *b, nir_ssa_def *color, const unsigned *bits,
return nir_vec(b, results, DIV_ROUND_UP(offset, 32));
}
static void
static bool
v3d_nir_lower_image_store(nir_builder *b, nir_intrinsic_instr *instr)
{
enum pipe_format format = nir_intrinsic_format(instr);
@ -160,16 +160,18 @@ v3d_nir_lower_image_store(nir_builder *b, nir_intrinsic_instr *instr)
nir_instr_rewrite_src(&instr->instr, &instr->src[3],
nir_src_for_ssa(formatted));
instr->num_components = formatted->num_components;
return true;
}
static void
static bool
v3d_nir_lower_image_load(nir_builder *b, nir_intrinsic_instr *instr)
{
static const unsigned bits16[] = {16, 16, 16, 16};
enum pipe_format format = nir_intrinsic_format(instr);
if (v3d_gl_format_is_return_32(format))
return;
return false;
b->cursor = nir_after_instr(&instr->instr);
@ -191,41 +193,37 @@ v3d_nir_lower_image_load(nir_builder *b, nir_intrinsic_instr *instr)
nir_ssa_def_rewrite_uses_after(&instr->dest.ssa, result,
result->parent_instr);
return true;
}
void
static bool
v3d_nir_lower_image_load_store_cb(nir_builder *b,
nir_instr *instr,
void *_state)
{
if (instr->type != nir_instr_type_intrinsic)
return false;
nir_intrinsic_instr *intr =
nir_instr_as_intrinsic(instr);
switch (intr->intrinsic) {
case nir_intrinsic_image_load:
return v3d_nir_lower_image_load(b, intr);
case nir_intrinsic_image_store:
return v3d_nir_lower_image_store(b, intr);
default:
return false;
}
return false;
}
bool
v3d_nir_lower_image_load_store(nir_shader *s)
{
nir_foreach_function(function, s) {
if (!function->impl)
continue;
nir_builder b;
nir_builder_init(&b, function->impl);
nir_foreach_block(block, function->impl) {
nir_foreach_instr_safe(instr, block) {
if (instr->type != nir_instr_type_intrinsic)
continue;
nir_intrinsic_instr *intr =
nir_instr_as_intrinsic(instr);
switch (intr->intrinsic) {
case nir_intrinsic_image_load:
v3d_nir_lower_image_load(&b, intr);
break;
case nir_intrinsic_image_store:
v3d_nir_lower_image_store(&b, intr);
break;
default:
break;
}
}
}
nir_metadata_preserve(function->impl,
nir_metadata_block_index |
nir_metadata_dominance);
}
return nir_shader_instructions_pass(s, v3d_nir_lower_image_load_store_cb,
nir_metadata_block_index |
nir_metadata_dominance, NULL);
}

View File

@ -693,7 +693,7 @@ emit_gs_vpm_output_header_prolog(struct v3d_compile *c, nir_builder *b,
v3d_nir_store_output(b, 0, NULL, header);
}
void
bool
v3d_nir_lower_io(nir_shader *s, struct v3d_compile *c)
{
struct v3d_nir_lower_io_state state = { 0 };
@ -745,4 +745,10 @@ v3d_nir_lower_io(nir_shader *s, struct v3d_compile *c)
s->info.stage == MESA_SHADER_GEOMETRY) {
v3d_nir_lower_io_update_output_var_base(c, &state);
}
/* It is really unlikely that we don't get progress here, and fully
* filtering when not would make code more complex, but we are still
* interested on getting this lowering going through NIR_PASS
*/
return true;
}

View File

@ -54,10 +54,12 @@ lower_line_smooth_intrinsic(struct lower_line_smooth_state *state,
nir_src_for_ssa(new_val));
}
static void
static bool
lower_line_smooth_func(struct lower_line_smooth_state *state,
nir_function_impl *impl)
{
bool progress = false;
nir_builder b;
nir_builder_init(&b, impl);
@ -77,8 +79,11 @@ lower_line_smooth_func(struct lower_line_smooth_state *state,
continue;
lower_line_smooth_intrinsic(state, &b, intr);
progress = true;
}
}
return progress;
}
static void
@ -140,9 +145,11 @@ make_coverage_var(nir_shader *s)
return var;
}
void
bool
v3d_nir_lower_line_smooth(nir_shader *s)
{
bool progress = false;
assert(s->info.stage == MESA_SHADER_FRAGMENT);
struct lower_line_smooth_state state = {
@ -154,6 +161,16 @@ v3d_nir_lower_line_smooth(nir_shader *s)
if (function->is_entrypoint)
initialise_coverage_var(&state, function->impl);
lower_line_smooth_func(&state, function->impl);
progress |= lower_line_smooth_func(&state, function->impl);
if (progress) {
nir_metadata_preserve(function->impl,
nir_metadata_block_index |
nir_metadata_dominance);
} else {
nir_metadata_preserve(function->impl, nir_metadata_all);
}
}
return progress;
}

View File

@ -257,12 +257,12 @@ lower_load_store_bitsize(nir_builder *b, nir_instr *instr, void *data)
}
}
void
bool
v3d_nir_lower_load_store_bitsize(nir_shader *s, struct v3d_compile *c)
{
nir_shader_instructions_pass(s,
lower_load_store_bitsize,
nir_metadata_block_index |
nir_metadata_dominance,
c);
return nir_shader_instructions_pass(s,
lower_load_store_bitsize,
nir_metadata_block_index |
nir_metadata_dominance,
c);
}

View File

@ -351,6 +351,8 @@ v3d_nir_lower_logic_op_instr(struct v3d_compile *c,
static bool
v3d_nir_lower_logic_ops_block(nir_block *block, struct v3d_compile *c)
{
bool progress = false;
nir_foreach_instr_safe(instr, block) {
if (instr->type != nir_instr_type_intrinsic)
continue;
@ -390,29 +392,40 @@ v3d_nir_lower_logic_ops_block(nir_block *block, struct v3d_compile *c)
nir_builder_init(&b, impl);
b.cursor = nir_before_instr(&intr->instr);
v3d_nir_lower_logic_op_instr(c, &b, intr, rt);
progress = true;
}
}
return true;
return progress;
}
void
bool
v3d_nir_lower_logic_ops(nir_shader *s, struct v3d_compile *c)
{
bool progress = false;
/* Nothing to do if logic op is 'copy src to dst' or if logic ops are
* disabled (we set the logic op to copy in that case).
*/
if (c->fs_key->logicop_func == PIPE_LOGICOP_COPY)
return;
return false;
nir_foreach_function(function, s) {
if (function->impl) {
nir_foreach_block(block, function->impl)
v3d_nir_lower_logic_ops_block(block, c);
progress |= v3d_nir_lower_logic_ops_block(block, c);
nir_metadata_preserve(function->impl,
nir_metadata_block_index |
nir_metadata_dominance);
if (progress) {
nir_metadata_preserve(function->impl,
nir_metadata_block_index |
nir_metadata_dominance);
} else {
nir_metadata_preserve(function->impl,
nir_metadata_all);
}
}
}
return progress;
}

View File

@ -101,21 +101,23 @@ lower_shared(struct v3d_compile *c,
nir_src_for_ssa(offset));
}
static void
lower_instr(struct v3d_compile *c, nir_builder *b, struct nir_instr *instr)
static bool
lower_instr(nir_builder *b, nir_instr *instr, void *_state)
{
struct v3d_compile *c = _state;
if (instr->type != nir_instr_type_intrinsic)
return;
return false;
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
switch (intr->intrinsic) {
case nir_intrinsic_load_ubo:
case nir_intrinsic_load_ssbo:
lower_load(c, b, intr);
break;
return true;
case nir_intrinsic_store_ssbo:
lower_store(c, b, intr);
break;
return true;
case nir_intrinsic_ssbo_atomic_add:
case nir_intrinsic_ssbo_atomic_imin:
case nir_intrinsic_ssbo_atomic_umin:
@ -127,7 +129,7 @@ lower_instr(struct v3d_compile *c, nir_builder *b, struct nir_instr *instr)
case nir_intrinsic_ssbo_atomic_exchange:
case nir_intrinsic_ssbo_atomic_comp_swap:
lower_atomic(c, b, intr);
break;
return true;
case nir_intrinsic_load_shared:
case nir_intrinsic_shared_atomic_add:
case nir_intrinsic_shared_atomic_imin:
@ -140,28 +142,16 @@ lower_instr(struct v3d_compile *c, nir_builder *b, struct nir_instr *instr)
case nir_intrinsic_shared_atomic_exchange:
case nir_intrinsic_shared_atomic_comp_swap:
lower_shared(c, b, intr);
break;
return true;
default:
break;
return false;
}
}
void
bool
v3d_nir_lower_robust_buffer_access(nir_shader *s, struct v3d_compile *c)
{
nir_foreach_function(function, s) {
if (function->impl) {
nir_builder b;
nir_builder_init(&b, function->impl);
nir_foreach_block(block, function->impl) {
nir_foreach_instr_safe(instr, block)
lower_instr(c, &b, instr);
}
nir_metadata_preserve(function->impl,
nir_metadata_block_index |
nir_metadata_dominance);
}
}
return nir_shader_instructions_pass(s, lower_instr,
nir_metadata_block_index |
nir_metadata_dominance, c);
}

View File

@ -115,39 +115,35 @@ v3d_nir_lower_store_scratch(nir_builder *b, nir_intrinsic_instr *instr)
nir_instr_remove(&instr->instr);
}
void
static bool
v3d_nir_lower_scratch_cb(nir_builder *b,
nir_instr *instr,
void *_state)
{
if (instr->type != nir_instr_type_intrinsic)
return false;
nir_intrinsic_instr *intr =
nir_instr_as_intrinsic(instr);
switch (intr->intrinsic) {
case nir_intrinsic_load_scratch:
v3d_nir_lower_load_scratch(b, intr);
return true;
case nir_intrinsic_store_scratch:
v3d_nir_lower_store_scratch(b, intr);
return true;
default:
return false;
}
return false;
}
bool
v3d_nir_lower_scratch(nir_shader *s)
{
nir_foreach_function(function, s) {
if (!function->impl)
continue;
nir_builder b;
nir_builder_init(&b, function->impl);
nir_foreach_block(block, function->impl) {
nir_foreach_instr_safe(instr, block) {
if (instr->type != nir_instr_type_intrinsic)
continue;
nir_intrinsic_instr *intr =
nir_instr_as_intrinsic(instr);
switch (intr->intrinsic) {
case nir_intrinsic_load_scratch:
v3d_nir_lower_load_scratch(&b, intr);
break;
case nir_intrinsic_store_scratch:
v3d_nir_lower_store_scratch(&b, intr);
break;
default:
break;
}
}
}
nir_metadata_preserve(function->impl,
nir_metadata_block_index |
nir_metadata_dominance);
}
return nir_shader_instructions_pass(s, v3d_nir_lower_scratch_cb,
nir_metadata_block_index |
nir_metadata_dominance, NULL);
}

View File

@ -75,11 +75,11 @@ v3d_nir_lower_txf_ms_filter(const nir_instr *instr, const void *data)
nir_instr_as_tex(instr)->op == nir_texop_txf_ms);
}
void
bool
v3d_nir_lower_txf_ms(nir_shader *s, struct v3d_compile *c)
{
nir_shader_lower_instructions(s,
v3d_nir_lower_txf_ms_filter,
v3d_nir_lower_txf_ms_instr,
NULL);
return nir_shader_lower_instructions(s,
v3d_nir_lower_txf_ms_filter,
v3d_nir_lower_txf_ms_instr,
NULL);
}