nir/constant_folding: add back and use constant_fold_state

Useful for load_constant folding.

Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Connor Abbott <cwabbott0@gmail.com>
This commit is contained in:
Rhys Perry
2019-09-26 11:19:07 +01:00
parent f7ca072ab2
commit ec054a67da

View File

@@ -32,8 +32,13 @@
* Implements SSA-based constant folding. * Implements SSA-based constant folding.
*/ */
struct constant_fold_state {
nir_shader *shader;
unsigned execution_mode;
};
static bool static bool
constant_fold_alu_instr(nir_alu_instr *instr, void *mem_ctx, unsigned execution_mode) constant_fold_alu_instr(struct constant_fold_state *state, nir_alu_instr *instr)
{ {
nir_const_value src[NIR_MAX_VEC_COMPONENTS][NIR_MAX_VEC_COMPONENTS]; nir_const_value src[NIR_MAX_VEC_COMPONENTS][NIR_MAX_VEC_COMPONENTS];
@@ -88,10 +93,10 @@ constant_fold_alu_instr(nir_alu_instr *instr, void *mem_ctx, unsigned execution_
for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; ++i) for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; ++i)
srcs[i] = src[i]; srcs[i] = src[i];
nir_eval_const_opcode(instr->op, dest, instr->dest.dest.ssa.num_components, nir_eval_const_opcode(instr->op, dest, instr->dest.dest.ssa.num_components,
bit_size, srcs, execution_mode); bit_size, srcs, state->execution_mode);
nir_load_const_instr *new_instr = nir_load_const_instr *new_instr =
nir_load_const_instr_create(mem_ctx, nir_load_const_instr_create(state->shader,
instr->dest.dest.ssa.num_components, instr->dest.dest.ssa.num_components,
instr->dest.dest.ssa.bit_size); instr->dest.dest.ssa.bit_size);
@@ -109,7 +114,7 @@ constant_fold_alu_instr(nir_alu_instr *instr, void *mem_ctx, unsigned execution_
} }
static bool static bool
constant_fold_intrinsic_instr(nir_intrinsic_instr *instr) constant_fold_intrinsic_instr(struct constant_fold_state *state, nir_intrinsic_instr *instr)
{ {
bool progress = false; bool progress = false;
@@ -117,19 +122,10 @@ constant_fold_intrinsic_instr(nir_intrinsic_instr *instr)
instr->intrinsic == nir_intrinsic_discard_if) && instr->intrinsic == nir_intrinsic_discard_if) &&
nir_src_is_const(instr->src[0])) { nir_src_is_const(instr->src[0])) {
if (nir_src_as_bool(instr->src[0])) { if (nir_src_as_bool(instr->src[0])) {
/* This method of getting a nir_shader * from a nir_instr is
* admittedly gross, but given the rarity of hitting this case I think
* it's preferable to plumbing an otherwise unused nir_shader *
* parameter through four functions to get here.
*/
nir_cf_node *cf_node = &instr->instr.block->cf_node;
nir_function_impl *impl = nir_cf_node_get_function(cf_node);
nir_shader *shader = impl->function->shader;
nir_intrinsic_op op = instr->intrinsic == nir_intrinsic_discard_if ? nir_intrinsic_op op = instr->intrinsic == nir_intrinsic_discard_if ?
nir_intrinsic_discard : nir_intrinsic_discard :
nir_intrinsic_demote; nir_intrinsic_demote;
nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(shader, op); nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(state->shader, op);
nir_instr_insert_before(&instr->instr, &new_instr->instr); nir_instr_insert_before(&instr->instr, &new_instr->instr);
nir_instr_remove(&instr->instr); nir_instr_remove(&instr->instr);
progress = true; progress = true;
@@ -144,18 +140,18 @@ constant_fold_intrinsic_instr(nir_intrinsic_instr *instr)
} }
static bool static bool
constant_fold_block(nir_block *block, void *mem_ctx, unsigned execution_mode) constant_fold_block(struct constant_fold_state *state, nir_block *block)
{ {
bool progress = false; bool progress = false;
nir_foreach_instr_safe(instr, block) { nir_foreach_instr_safe(instr, block) {
switch (instr->type) { switch (instr->type) {
case nir_instr_type_alu: case nir_instr_type_alu:
progress |= constant_fold_alu_instr(nir_instr_as_alu(instr), mem_ctx, execution_mode); progress |= constant_fold_alu_instr(state, nir_instr_as_alu(instr));
break; break;
case nir_instr_type_intrinsic: case nir_instr_type_intrinsic:
progress |= progress |=
constant_fold_intrinsic_instr(nir_instr_as_intrinsic(instr)); constant_fold_intrinsic_instr(state, nir_instr_as_intrinsic(instr));
break; break;
default: default:
/* Don't know how to constant fold */ /* Don't know how to constant fold */
@@ -167,13 +163,12 @@ constant_fold_block(nir_block *block, void *mem_ctx, unsigned execution_mode)
} }
static bool static bool
nir_opt_constant_folding_impl(nir_function_impl *impl, unsigned execution_mode) nir_opt_constant_folding_impl(struct constant_fold_state *state, nir_function_impl *impl)
{ {
void *mem_ctx = ralloc_parent(impl);
bool progress = false; bool progress = false;
nir_foreach_block(block, impl) { nir_foreach_block(block, impl) {
progress |= constant_fold_block(block, mem_ctx, execution_mode); progress |= constant_fold_block(state, block);
} }
if (progress) { if (progress) {
@@ -192,11 +187,13 @@ bool
nir_opt_constant_folding(nir_shader *shader) nir_opt_constant_folding(nir_shader *shader)
{ {
bool progress = false; bool progress = false;
unsigned execution_mode = shader->info.float_controls_execution_mode; struct constant_fold_state state;
state.shader = shader;
state.execution_mode = shader->info.float_controls_execution_mode;
nir_foreach_function(function, shader) { nir_foreach_function(function, shader) {
if (function->impl) if (function->impl)
progress |= nir_opt_constant_folding_impl(function->impl, execution_mode); progress |= nir_opt_constant_folding_impl(&state, function->impl);
} }
return progress; return progress;