aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRhys Perry <[email protected]>2019-09-26 11:19:07 +0100
committerRhys Perry <[email protected]>2019-10-07 19:49:53 +0100
commitec054a67daa606b60201da144f4a3ad1a1cde451 (patch)
tree34b3fd443002772d7b5cdc1c849c789cbe3d5f04
parentf7ca072ab21b3ac1976b31ad81891f3626e3b876 (diff)
nir/constant_folding: add back and use constant_fold_state
Useful for load_constant folding. Signed-off-by: Rhys Perry <[email protected]> Reviewed-by: Connor Abbott <[email protected]>
-rw-r--r--src/compiler/nir/nir_opt_constant_folding.c41
1 files changed, 19 insertions, 22 deletions
diff --git a/src/compiler/nir/nir_opt_constant_folding.c b/src/compiler/nir/nir_opt_constant_folding.c
index 80d9c613e5b..6012081389f 100644
--- a/src/compiler/nir/nir_opt_constant_folding.c
+++ b/src/compiler/nir/nir_opt_constant_folding.c
@@ -32,8 +32,13 @@
* Implements SSA-based constant folding.
*/
+struct constant_fold_state {
+ nir_shader *shader;
+ unsigned execution_mode;
+};
+
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];
@@ -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)
srcs[i] = src[i];
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_create(mem_ctx,
+ nir_load_const_instr_create(state->shader,
instr->dest.dest.ssa.num_components,
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
-constant_fold_intrinsic_instr(nir_intrinsic_instr *instr)
+constant_fold_intrinsic_instr(struct constant_fold_state *state, nir_intrinsic_instr *instr)
{
bool progress = false;
@@ -117,19 +122,10 @@ constant_fold_intrinsic_instr(nir_intrinsic_instr *instr)
instr->intrinsic == nir_intrinsic_discard_if) &&
nir_src_is_const(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_discard :
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_remove(&instr->instr);
progress = true;
@@ -144,18 +140,18 @@ constant_fold_intrinsic_instr(nir_intrinsic_instr *instr)
}
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;
nir_foreach_instr_safe(instr, block) {
switch (instr->type) {
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;
case nir_instr_type_intrinsic:
progress |=
- constant_fold_intrinsic_instr(nir_instr_as_intrinsic(instr));
+ constant_fold_intrinsic_instr(state, nir_instr_as_intrinsic(instr));
break;
default:
/* 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
-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;
nir_foreach_block(block, impl) {
- progress |= constant_fold_block(block, mem_ctx, execution_mode);
+ progress |= constant_fold_block(state, block);
}
if (progress) {
@@ -192,11 +187,13 @@ bool
nir_opt_constant_folding(nir_shader *shader)
{
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) {
if (function->impl)
- progress |= nir_opt_constant_folding_impl(function->impl, execution_mode);
+ progress |= nir_opt_constant_folding_impl(&state, function->impl);
}
return progress;