X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fcompiler%2Fnir%2Fnir_opt_copy_propagate.c;h=3cd476a1b9728e87a119bc6ffc915a0443ea3fd8;hb=67c728f7a9450b04d4de1a29f1dcfb9265a7ebfd;hp=acae60e233fd317661b0bdcbc4abd23578ad38e4;hpb=b6dc940ec273252678d40707d300851fa1c85ea5;p=mesa.git diff --git a/src/compiler/nir/nir_opt_copy_propagate.c b/src/compiler/nir/nir_opt_copy_propagate.c index acae60e233f..3cd476a1b97 100644 --- a/src/compiler/nir/nir_opt_copy_propagate.c +++ b/src/compiler/nir/nir_opt_copy_propagate.c @@ -99,11 +99,12 @@ is_swizzleless_move(nir_alu_instr *instr) } static bool -copy_prop_src(nir_src *src, nir_instr *parent_instr, nir_if *parent_if) +copy_prop_src(nir_src *src, nir_instr *parent_instr, nir_if *parent_if, + unsigned num_components) { if (!src->is_ssa) { if (src->reg.indirect) - return copy_prop_src(src, parent_instr, parent_if); + return copy_prop_src(src->reg.indirect, parent_instr, parent_if, 1); return false; } @@ -115,17 +116,8 @@ copy_prop_src(nir_src *src, nir_instr *parent_instr, nir_if *parent_if) if (!is_swizzleless_move(alu_instr)) return false; - /* Don't let copy propagation land us with a phi that has more - * components in its source than it has in its destination. That badly - * messes up out-of-ssa. - */ - if (parent_instr && parent_instr->type == nir_instr_type_phi) { - nir_phi_instr *phi = nir_instr_as_phi(parent_instr); - assert(phi->dest.is_ssa); - if (phi->dest.ssa.num_components != - alu_instr->src[0].src.ssa->num_components) - return false; - } + if (alu_instr->src[0].src.ssa->num_components != num_components) + return false; if (parent_instr) { nir_instr_rewrite_src(parent_instr, src, @@ -146,7 +138,7 @@ copy_prop_alu_src(nir_alu_instr *parent_alu_instr, unsigned index) if (!src->src.is_ssa) { if (src->src.reg.indirect) return copy_prop_src(src->src.reg.indirect, &parent_alu_instr->instr, - NULL); + NULL, 1); return false; } @@ -193,72 +185,119 @@ copy_prop_alu_src(nir_alu_instr *parent_alu_instr, unsigned index) return true; } -typedef struct { - nir_instr *parent_instr; - bool progress; -} copy_prop_state; +static bool +copy_prop_dest(nir_dest *dest, nir_instr *instr) +{ + if (!dest->is_ssa && dest->reg.indirect) + return copy_prop_src(dest->reg.indirect, instr, NULL, 1); + + return false; +} static bool -copy_prop_src_cb(nir_src *src, void *_state) +copy_prop_deref_var(nir_instr *instr, nir_deref_var *deref_var) { - copy_prop_state *state = (copy_prop_state *) _state; - while (copy_prop_src(src, state->parent_instr, NULL)) - state->progress = true; + if (!deref_var) + return false; - return true; + bool progress = false; + for (nir_deref *deref = deref_var->deref.child; + deref; deref = deref->child) { + if (deref->deref_type != nir_deref_type_array) + continue; + + nir_deref_array *arr = nir_deref_as_array(deref); + if (arr->deref_array_type != nir_deref_array_type_indirect) + continue; + + while (copy_prop_src(&arr->indirect, instr, NULL, 1)) + progress = true; + } + return progress; } static bool copy_prop_instr(nir_instr *instr) { - if (instr->type == nir_instr_type_alu) { + bool progress = false; + switch (instr->type) { + case nir_instr_type_alu: { nir_alu_instr *alu_instr = nir_instr_as_alu(instr); - bool progress = false; for (unsigned i = 0; i < nir_op_infos[alu_instr->op].num_inputs; i++) while (copy_prop_alu_src(alu_instr, i)) progress = true; - if (!alu_instr->dest.dest.is_ssa && alu_instr->dest.dest.reg.indirect) - while (copy_prop_src(alu_instr->dest.dest.reg.indirect, instr, NULL)) + while (copy_prop_dest(&alu_instr->dest.dest, instr)) + progress = true; + + return progress; + } + + case nir_instr_type_tex: { + nir_tex_instr *tex = nir_instr_as_tex(instr); + for (unsigned i = 0; i < tex->num_srcs; i++) { + unsigned num_components = nir_tex_instr_src_size(tex, i); + while (copy_prop_src(&tex->src[i].src, instr, NULL, num_components)) progress = true; + } + + if (copy_prop_deref_var(instr, tex->texture)) + progress = true; + if (copy_prop_deref_var(instr, tex->sampler)) + progress = true; + + while (copy_prop_dest(&tex->dest, instr)) + progress = true; return progress; } - copy_prop_state state; - state.parent_instr = instr; - state.progress = false; - nir_foreach_src(instr, copy_prop_src_cb, &state); + case nir_instr_type_intrinsic: { + nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); + for (unsigned i = 0; + i < nir_intrinsic_infos[intrin->intrinsic].num_srcs; i++) { + unsigned num_components = nir_intrinsic_src_components(intrin, i); - return state.progress; -} + while (copy_prop_src(&intrin->src[i], instr, NULL, num_components)) + progress = true; + } -static bool -copy_prop_if(nir_if *if_stmt) -{ - return copy_prop_src(&if_stmt->condition, NULL, if_stmt); -} + for (unsigned i = 0; + i < nir_intrinsic_infos[intrin->intrinsic].num_variables; i++) { + if (copy_prop_deref_var(instr, intrin->variables[i])) + progress = true; + } -static bool -copy_prop_block(nir_block *block, void *_state) -{ - bool *progress = (bool *) _state; + if (nir_intrinsic_infos[intrin->intrinsic].has_dest) { + while (copy_prop_dest(&intrin->dest, instr)) + progress = true; + } + + return progress; + } + + case nir_instr_type_phi: { + nir_phi_instr *phi = nir_instr_as_phi(instr); + assert(phi->dest.is_ssa); + unsigned num_components = phi->dest.ssa.num_components; + nir_foreach_phi_src(src, phi) { + while (copy_prop_src(&src->src, instr, NULL, num_components)) + progress = true; + } - nir_foreach_instr(block, instr) { - if (copy_prop_instr(instr)) - *progress = true; + return progress; } - if (block->cf_node.node.next != NULL && /* check that we aren't the end node */ - !nir_cf_node_is_last(&block->cf_node) && - nir_cf_node_next(&block->cf_node)->type == nir_cf_node_if) { - nir_if *if_stmt = nir_cf_node_as_if(nir_cf_node_next(&block->cf_node)); - if (copy_prop_if(if_stmt)) - *progress = true; + default: + return false; } +} - return true; +static bool +copy_prop_if(nir_if *if_stmt) +{ + return copy_prop_src(&if_stmt->condition, NULL, if_stmt, 1); } static bool @@ -266,7 +305,16 @@ nir_copy_prop_impl(nir_function_impl *impl) { bool progress = false; - nir_foreach_block_call(impl, copy_prop_block, &progress); + nir_foreach_block(block, impl) { + nir_foreach_instr(instr, block) { + if (copy_prop_instr(instr)) + progress = true; + } + + nir_if *if_stmt = nir_block_get_following_if(block); + if (if_stmt && copy_prop_if(if_stmt)) + progress = true; + } if (progress) { nir_metadata_preserve(impl, nir_metadata_block_index | @@ -281,7 +329,7 @@ nir_copy_prop(nir_shader *shader) { bool progress = false; - nir_foreach_function(shader, function) { + nir_foreach_function(function, shader) { if (function->impl && nir_copy_prop_impl(function->impl)) progress = true; }