nir/constant_folding: Use nir_shader_instruction_pass
This gets rid of so much boilerplate... Reviewed-by: Eric Anholt <eric@anholt.net> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6974>
This commit is contained in:
parent
9df1ff3678
commit
1ada83504f
|
@ -35,14 +35,12 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct constant_fold_state {
|
struct constant_fold_state {
|
||||||
nir_builder build;
|
|
||||||
|
|
||||||
bool has_load_constant;
|
bool has_load_constant;
|
||||||
bool has_indirect_load_const;
|
bool has_indirect_load_const;
|
||||||
};
|
};
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
constant_fold_alu_instr(nir_builder *b, nir_alu_instr *instr)
|
try_fold_alu(nir_builder *b, 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];
|
||||||
|
|
||||||
|
@ -113,13 +111,11 @@ constant_fold_alu_instr(nir_builder *b, nir_alu_instr *instr)
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
constant_fold_intrinsic_instr(struct constant_fold_state *state,
|
try_fold_intrinsic(nir_builder *b, nir_intrinsic_instr *instr,
|
||||||
nir_intrinsic_instr *instr)
|
struct constant_fold_state *state)
|
||||||
{
|
{
|
||||||
bool progress = false;
|
bool progress = false;
|
||||||
|
|
||||||
nir_builder *b = &state->build;
|
|
||||||
|
|
||||||
if ((instr->intrinsic == nir_intrinsic_demote_if ||
|
if ((instr->intrinsic == nir_intrinsic_demote_if ||
|
||||||
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])) {
|
||||||
|
@ -174,63 +170,30 @@ constant_fold_intrinsic_instr(struct constant_fold_state *state,
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
constant_fold_block(struct constant_fold_state *state, nir_block *block)
|
try_fold_instr(nir_builder *b, nir_instr *instr, void *_state)
|
||||||
{
|
{
|
||||||
bool progress = false;
|
switch (instr->type) {
|
||||||
|
case nir_instr_type_alu:
|
||||||
nir_foreach_instr_safe(instr, block) {
|
return try_fold_alu(b, nir_instr_as_alu(instr));
|
||||||
switch (instr->type) {
|
case nir_instr_type_intrinsic:
|
||||||
case nir_instr_type_alu:
|
return try_fold_intrinsic(b, nir_instr_as_intrinsic(instr), _state);
|
||||||
progress |= constant_fold_alu_instr(&state->build,
|
default:
|
||||||
nir_instr_as_alu(instr));
|
/* Don't know how to constant fold */
|
||||||
break;
|
return false;
|
||||||
case nir_instr_type_intrinsic:
|
|
||||||
progress |=
|
|
||||||
constant_fold_intrinsic_instr(state, nir_instr_as_intrinsic(instr));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
/* Don't know how to constant fold */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return progress;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
nir_opt_constant_folding_impl(struct constant_fold_state *state,
|
|
||||||
nir_function_impl *impl)
|
|
||||||
{
|
|
||||||
bool progress = false;
|
|
||||||
|
|
||||||
nir_builder_init(&state->build, impl);
|
|
||||||
|
|
||||||
nir_foreach_block(block, impl) {
|
|
||||||
progress |= constant_fold_block(state, block);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (progress) {
|
|
||||||
nir_metadata_preserve(impl, nir_metadata_block_index |
|
|
||||||
nir_metadata_dominance);
|
|
||||||
} else {
|
|
||||||
nir_metadata_preserve(impl, nir_metadata_all);
|
|
||||||
}
|
|
||||||
|
|
||||||
return progress;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
nir_opt_constant_folding(nir_shader *shader)
|
nir_opt_constant_folding(nir_shader *shader)
|
||||||
{
|
{
|
||||||
bool progress = false;
|
|
||||||
struct constant_fold_state state;
|
struct constant_fold_state state;
|
||||||
state.has_load_constant = false;
|
state.has_load_constant = false;
|
||||||
state.has_indirect_load_const = false;
|
state.has_indirect_load_const = false;
|
||||||
|
|
||||||
nir_foreach_function(function, shader) {
|
bool progress = nir_shader_instructions_pass(shader, try_fold_instr,
|
||||||
if (function->impl)
|
nir_metadata_block_index |
|
||||||
progress |= nir_opt_constant_folding_impl(&state, function->impl);
|
nir_metadata_dominance,
|
||||||
}
|
&state);
|
||||||
|
|
||||||
/* This doesn't free the constant data if there are no constant loads because
|
/* This doesn't free the constant data if there are no constant loads because
|
||||||
* the data might still be used but the loads have been lowered to load_ubo
|
* the data might still be used but the loads have been lowered to load_ubo
|
||||||
|
|
Loading…
Reference in New Issue