nir/lower_system_values: Refactor and use the builder.
Now that we have a helper in the builder for system values and a helper in core NIR to get the intrinsic opcode, there's really no point in having things split out into a helper function. This commit "modernizes" this pass to use helpers better and look more like newer passes. Reviewed-by: Eric Anholt <eric@anholt.net>
This commit is contained in:
@@ -26,44 +26,43 @@
|
||||
*/
|
||||
|
||||
#include "nir.h"
|
||||
#include "main/mtypes.h"
|
||||
#include "nir_builder.h"
|
||||
|
||||
struct lower_system_values_state {
|
||||
nir_builder builder;
|
||||
bool progress;
|
||||
};
|
||||
|
||||
static bool
|
||||
convert_instr(nir_intrinsic_instr *instr)
|
||||
convert_block(nir_block *block, void *void_state)
|
||||
{
|
||||
if (instr->intrinsic != nir_intrinsic_load_var)
|
||||
return false;
|
||||
struct lower_system_values_state *state = void_state;
|
||||
|
||||
nir_variable *var = instr->variables[0]->var;
|
||||
if (var->data.mode != nir_var_system_value)
|
||||
return false;
|
||||
|
||||
void *mem_ctx = ralloc_parent(instr);
|
||||
|
||||
assert(instr->dest.is_ssa);
|
||||
|
||||
nir_intrinsic_op op = nir_intrinsic_from_system_value(var->data.location);
|
||||
nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(mem_ctx, op);
|
||||
|
||||
nir_ssa_dest_init(&new_instr->instr, &new_instr->dest,
|
||||
instr->dest.ssa.num_components, NULL);
|
||||
nir_ssa_def_rewrite_uses(&instr->dest.ssa,
|
||||
nir_src_for_ssa(&new_instr->dest.ssa));
|
||||
|
||||
nir_instr_insert_before(&instr->instr, &new_instr->instr);
|
||||
nir_instr_remove(&instr->instr);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
convert_block(nir_block *block, void *state)
|
||||
{
|
||||
bool *progress = state;
|
||||
nir_builder *b = &state->builder;
|
||||
|
||||
nir_foreach_instr_safe(block, instr) {
|
||||
if (instr->type == nir_instr_type_intrinsic)
|
||||
*progress = convert_instr(nir_instr_as_intrinsic(instr)) || *progress;
|
||||
if (instr->type != nir_instr_type_intrinsic)
|
||||
continue;
|
||||
|
||||
nir_intrinsic_instr *load_var = nir_instr_as_intrinsic(instr);
|
||||
|
||||
if (load_var->intrinsic != nir_intrinsic_load_var)
|
||||
continue;
|
||||
|
||||
nir_variable *var = load_var->variables[0]->var;
|
||||
if (var->data.mode != nir_var_system_value)
|
||||
continue;
|
||||
|
||||
b->cursor = nir_after_instr(&load_var->instr);
|
||||
|
||||
nir_intrinsic_op sysval_op =
|
||||
nir_intrinsic_from_system_value(var->data.location);
|
||||
nir_ssa_def *sysval = nir_load_system_value(b, sysval_op, 0);
|
||||
|
||||
nir_ssa_def_rewrite_uses(&load_var->dest.ssa, nir_src_for_ssa(sysval));
|
||||
nir_instr_remove(&load_var->instr);
|
||||
|
||||
state->progress = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -72,12 +71,15 @@ convert_block(nir_block *block, void *state)
|
||||
static bool
|
||||
convert_impl(nir_function_impl *impl)
|
||||
{
|
||||
bool progress = false;
|
||||
struct lower_system_values_state state;
|
||||
|
||||
nir_foreach_block(impl, convert_block, &progress);
|
||||
state.progress = false;
|
||||
nir_builder_init(&state.builder, impl);
|
||||
|
||||
nir_foreach_block(impl, convert_block, &state);
|
||||
nir_metadata_preserve(impl, nir_metadata_block_index |
|
||||
nir_metadata_dominance);
|
||||
return progress;
|
||||
return state.progress;
|
||||
}
|
||||
|
||||
bool
|
||||
|
||||
Reference in New Issue
Block a user