nir/builder: add option to immediately constant-fold ALU instructions upon insertion

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/37195>
This commit is contained in:
Daniel Schürmann 2025-10-14 12:46:26 +02:00 committed by Marge Bot
parent 280eb2d689
commit f61cd64af8
4 changed files with 16 additions and 3 deletions

View file

@ -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_reassociate_matrix_mul(nir_shader *shader);
bool nir_opt_constant_folding(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, /* 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 * which will result in b being removed by the pass. Return false if
* combination wasn't possible. * combination wasn't possible.

View file

@ -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, nir_def_init(&instr->instr, &instr->def, num_components,
bit_size); 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); nir_builder_instr_insert(build, &instr->instr);
return &instr->def; return &instr->def;

View file

@ -40,6 +40,9 @@ typedef struct nir_builder {
/* Whether new ALU instructions will be marked "exact" */ /* Whether new ALU instructions will be marked "exact" */
bool 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. */ /* Float_controls2 bits. See nir_alu_instr for details. */
uint32_t fp_fast_math; uint32_t fp_fast_math;

View file

@ -36,8 +36,8 @@ struct constant_fold_state {
bool has_indirect_load_const; bool has_indirect_load_const;
}; };
static nir_def * nir_def *
try_fold_alu(nir_builder *b, nir_alu_instr *alu) nir_try_constant_fold_alu(nir_builder *b, nir_alu_instr *alu)
{ {
nir_const_value src[NIR_ALU_MAX_INPUTS][NIR_MAX_VEC_COMPONENTS]; 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) { switch (instr->type) {
case nir_instr_type_alu: 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: case nir_instr_type_intrinsic:
return try_fold_intrinsic(b, nir_instr_as_intrinsic(instr), _state); return try_fold_intrinsic(b, nir_instr_as_intrinsic(instr), _state);
case nir_instr_type_tex: case nir_instr_type_tex: