nir/constant_folding: add back and use constant_fold_state
authorRhys Perry <pendingchaos02@gmail.com>
Thu, 26 Sep 2019 10:19:07 +0000 (11:19 +0100)
committerRhys Perry <pendingchaos02@gmail.com>
Mon, 7 Oct 2019 18:49:53 +0000 (19:49 +0100)
Useful for load_constant folding.

Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Connor Abbott <cwabbott0@gmail.com>
src/compiler/nir/nir_opt_constant_folding.c

index 80d9c613e5b4adb2e00a186f3522463d44a05c86..6012081389f5309d0ff8659bce043e103537a04e 100644 (file)
  * 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;