diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 7666f68cad1..26cb0921296 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -6240,6 +6240,8 @@ bool nir_opt_algebraic_integer_promotion(nir_shader *shader); bool nir_opt_reassociate_matrix_mul(nir_shader *shader); bool nir_opt_constant_folding(nir_shader *shader); +nir_def *nir_try_constant_fold_alu(nir_builder *b, nir_alu_instr *alu); + /* Try to combine a and b into a. Return true if combination was possible, * which will result in b being removed by the pass. Return false if * combination wasn't possible. diff --git a/src/compiler/nir/nir_builder.c b/src/compiler/nir/nir_builder.c index 2b89343ec88..493aac67cdf 100644 --- a/src/compiler/nir/nir_builder.c +++ b/src/compiler/nir/nir_builder.c @@ -123,6 +123,14 @@ nir_builder_alu_instr_finish_and_insert(nir_builder *build, nir_alu_instr *instr nir_def_init(&instr->instr, &instr->def, num_components, bit_size); + if (build->constant_fold_alu) { + nir_def *new_def = nir_try_constant_fold_alu(build, instr); + if (new_def) { + nir_instr_free(&instr->instr); + return new_def; + } + } + nir_builder_instr_insert(build, &instr->instr); return &instr->def; diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h index 0d9e2c5c464..4c8e02d30d1 100644 --- a/src/compiler/nir/nir_builder.h +++ b/src/compiler/nir/nir_builder.h @@ -40,6 +40,9 @@ typedef struct nir_builder { /* Whether new ALU instructions will be marked "exact" */ bool exact; + /* Whether new ALU instruction will be constanst-folded if possible. */ + bool constant_fold_alu; + /* Float_controls2 bits. See nir_alu_instr for details. */ uint32_t fp_fast_math; diff --git a/src/compiler/nir/nir_opt_constant_folding.c b/src/compiler/nir/nir_opt_constant_folding.c index 08f29be01f8..487c1a0d035 100644 --- a/src/compiler/nir/nir_opt_constant_folding.c +++ b/src/compiler/nir/nir_opt_constant_folding.c @@ -36,8 +36,8 @@ struct constant_fold_state { bool has_indirect_load_const; }; -static nir_def * -try_fold_alu(nir_builder *b, nir_alu_instr *alu) +nir_def * +nir_try_constant_fold_alu(nir_builder *b, nir_alu_instr *alu) { nir_const_value src[NIR_ALU_MAX_INPUTS][NIR_MAX_VEC_COMPONENTS]; @@ -437,7 +437,7 @@ try_fold_instr(nir_builder *b, nir_instr *instr, void *_state) { switch (instr->type) { case nir_instr_type_alu: - return try_fold_alu(b, nir_instr_as_alu(instr)); + return nir_try_constant_fold_alu(b, nir_instr_as_alu(instr)); case nir_instr_type_intrinsic: return try_fold_intrinsic(b, nir_instr_as_intrinsic(instr), _state); case nir_instr_type_tex: