X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fcompiler%2Fnir%2Fnir_lower_locals_to_regs.c;h=dcac46727cf9174e4f1a296855597ed826f0066c;hb=43d22c8f2011b780467df97e58981522edccfa21;hp=0438802d3b22b6bd273973d76ae940f6e944252d;hpb=3124ce699bb3844e793f00e00bfbea5c91744f90;p=mesa.git diff --git a/src/compiler/nir/nir_lower_locals_to_regs.c b/src/compiler/nir/nir_lower_locals_to_regs.c index 0438802d3b2..dcac46727cf 100644 --- a/src/compiler/nir/nir_lower_locals_to_regs.c +++ b/src/compiler/nir/nir_lower_locals_to_regs.c @@ -26,21 +26,14 @@ */ #include "nir.h" -#include "nir_array.h" +#include "nir_builder.h" struct locals_to_regs_state { - nir_shader *shader; - nir_function_impl *impl; + nir_builder builder; /* A hash table mapping derefs to registers */ struct hash_table *regs_table; - /* A growing array of derefs that we have encountered. There is exactly - * one element of this array per element in the hash table. This is - * used to make adding register initialization code deterministic. - */ - nir_array derefs_array; - bool progress; }; @@ -52,84 +45,93 @@ struct locals_to_regs_state { static uint32_t hash_deref(const void *void_deref) { - uint32_t hash = _mesa_fnv32_1a_offset_bias; + uint32_t hash = 0; + + for (const nir_deref_instr *deref = void_deref; deref; + deref = nir_deref_instr_parent(deref)) { + switch (deref->deref_type) { + case nir_deref_type_var: + return XXH32(&deref->var, sizeof(deref->var), hash); + + case nir_deref_type_array: + continue; /* Do nothing */ - const nir_deref_var *deref_var = void_deref; - hash = _mesa_fnv32_1a_accumulate(hash, deref_var->var); + case nir_deref_type_struct: + hash = XXH32(&deref->strct.index, sizeof(deref->strct.index), hash); + continue; - for (const nir_deref *deref = deref_var->deref.child; - deref; deref = deref->child) { - if (deref->deref_type == nir_deref_type_struct) { - const nir_deref_struct *deref_struct = nir_deref_as_struct(deref); - hash = _mesa_fnv32_1a_accumulate(hash, deref_struct->index); + default: + unreachable("Invalid deref type"); } } - return hash; + unreachable("We should have hit a variable dereference"); } static bool derefs_equal(const void *void_a, const void *void_b) { - const nir_deref_var *a_var = void_a; - const nir_deref_var *b_var = void_b; - - if (a_var->var != b_var->var) - return false; - - for (const nir_deref *a = a_var->deref.child, *b = b_var->deref.child; - a != NULL; a = a->child, b = b->child) { + for (const nir_deref_instr *a = void_a, *b = void_b; a || b; + a = nir_deref_instr_parent(a), b = nir_deref_instr_parent(b)) { if (a->deref_type != b->deref_type) return false; - if (a->deref_type == nir_deref_type_struct) { - if (nir_deref_as_struct(a)->index != nir_deref_as_struct(b)->index) + switch (a->deref_type) { + case nir_deref_type_var: + return a->var == b->var; + + case nir_deref_type_array: + continue; /* Do nothing */ + + case nir_deref_type_struct: + if (a->strct.index != b->strct.index) return false; - } - /* Do nothing for arrays. They're all the same. */ + continue; - assert((a->child == NULL) == (b->child == NULL)); - if((a->child == NULL) != (b->child == NULL)) - return false; + default: + unreachable("Invalid deref type"); + } } - return true; + unreachable("We should have hit a variable dereference"); } static nir_register * -get_reg_for_deref(nir_deref_var *deref, struct locals_to_regs_state *state) +get_reg_for_deref(nir_deref_instr *deref, struct locals_to_regs_state *state) { uint32_t hash = hash_deref(deref); + assert(nir_deref_instr_get_variable(deref)->constant_initializer == NULL && + nir_deref_instr_get_variable(deref)->pointer_initializer == NULL); + struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(state->regs_table, hash, deref); if (entry) return entry->data; unsigned array_size = 1; - nir_deref *tail = &deref->deref; - while (tail->child) { - if (tail->child->deref_type == nir_deref_type_array) - array_size *= glsl_get_length(tail->type); - tail = tail->child; + for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) { + if (d->deref_type == nir_deref_type_array) + array_size *= glsl_get_length(nir_deref_instr_parent(d)->type); } - assert(glsl_type_is_vector(tail->type) || glsl_type_is_scalar(tail->type)); + assert(glsl_type_is_vector_or_scalar(deref->type)); - nir_register *reg = nir_local_reg_create(state->impl); - reg->num_components = glsl_get_vector_elements(tail->type); + nir_register *reg = nir_local_reg_create(state->builder.impl); + reg->num_components = glsl_get_vector_elements(deref->type); reg->num_array_elems = array_size > 1 ? array_size : 0; + reg->bit_size = glsl_get_bit_size(deref->type); _mesa_hash_table_insert_pre_hashed(state->regs_table, hash, deref, reg); - nir_array_add(&state->derefs_array, nir_deref_var *, deref); return reg; } static nir_src -get_deref_reg_src(nir_deref_var *deref, nir_instr *instr, - struct locals_to_regs_state *state) +get_deref_reg_src(nir_deref_instr *deref, struct locals_to_regs_state *state) { + nir_builder *b = &state->builder; + nir_src src; src.is_ssa = false; @@ -145,79 +147,59 @@ get_deref_reg_src(nir_deref_var *deref, nir_instr *instr, if (src.reg.reg->num_array_elems == 0) return src; - nir_deref *tail = &deref->deref; - while (tail->child != NULL) { - const struct glsl_type *parent_type = tail->type; - tail = tail->child; - - if (tail->deref_type != nir_deref_type_array) + unsigned inner_array_size = 1; + for (const nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) { + if (d->deref_type != nir_deref_type_array) continue; - nir_deref_array *deref_array = nir_deref_as_array(tail); - - src.reg.base_offset *= glsl_get_length(parent_type); - src.reg.base_offset += deref_array->base_offset; - - if (src.reg.indirect) { - nir_load_const_instr *load_const = - nir_load_const_instr_create(state->shader, 1); - load_const->value.u32[0] = glsl_get_length(parent_type); - nir_instr_insert_before(instr, &load_const->instr); - - nir_alu_instr *mul = nir_alu_instr_create(state->shader, nir_op_imul); - mul->src[0].src = *src.reg.indirect; - mul->src[1].src.is_ssa = true; - mul->src[1].src.ssa = &load_const->def; - mul->dest.write_mask = 1; - nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, 32, NULL); - nir_instr_insert_before(instr, &mul->instr); - - src.reg.indirect->is_ssa = true; - src.reg.indirect->ssa = &mul->dest.dest.ssa; - } - - if (deref_array->deref_array_type == nir_deref_array_type_indirect) { - if (src.reg.indirect == NULL) { - src.reg.indirect = ralloc(state->shader, nir_src); - nir_src_copy(src.reg.indirect, &deref_array->indirect, - state->shader); + if (nir_src_is_const(d->arr.index) && !src.reg.indirect) { + src.reg.base_offset += nir_src_as_uint(d->arr.index) * + inner_array_size; + } else { + if (src.reg.indirect) { + assert(src.reg.base_offset == 0); } else { - nir_alu_instr *add = nir_alu_instr_create(state->shader, - nir_op_iadd); - add->src[0].src = *src.reg.indirect; - nir_src_copy(&add->src[1].src, &deref_array->indirect, add); - add->dest.write_mask = 1; - nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, 32, NULL); - nir_instr_insert_before(instr, &add->instr); - - src.reg.indirect->is_ssa = true; - src.reg.indirect->ssa = &add->dest.dest.ssa; + src.reg.indirect = ralloc(b->shader, nir_src); + *src.reg.indirect = + nir_src_for_ssa(nir_imm_int(b, src.reg.base_offset)); + src.reg.base_offset = 0; } + + assert(src.reg.indirect->is_ssa); + nir_ssa_def *index = nir_i2i(b, nir_ssa_for_src(b, d->arr.index, 1), 32); + src.reg.indirect->ssa = + nir_iadd(b, src.reg.indirect->ssa, + nir_imul(b, index, nir_imm_int(b, inner_array_size))); } + + inner_array_size *= glsl_get_length(nir_deref_instr_parent(d)->type); } return src; } static bool -lower_locals_to_regs_block(nir_block *block, void *void_state) +lower_locals_to_regs_block(nir_block *block, + struct locals_to_regs_state *state) { - struct locals_to_regs_state *state = void_state; + nir_builder *b = &state->builder; - nir_foreach_instr_safe(block, instr) { + nir_foreach_instr_safe(instr, block) { if (instr->type != nir_instr_type_intrinsic) continue; nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); switch (intrin->intrinsic) { - case nir_intrinsic_load_var: { - if (intrin->variables[0]->var->data.mode != nir_var_local) + case nir_intrinsic_load_deref: { + nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]); + if (deref->mode != nir_var_function_temp) continue; - nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov); - mov->src[0].src = get_deref_reg_src(intrin->variables[0], - &intrin->instr, state); + b->cursor = nir_before_instr(&intrin->instr); + + nir_alu_instr *mov = nir_alu_instr_create(b->shader, nir_op_mov); + mov->src[0].src = get_deref_reg_src(deref, state); mov->dest.write_mask = (1 << intrin->num_components) - 1; if (intrin->dest.is_ssa) { nir_ssa_dest_init(&mov->instr, &mov->dest.dest, @@ -228,36 +210,38 @@ lower_locals_to_regs_block(nir_block *block, void *void_state) } else { nir_dest_copy(&mov->dest.dest, &intrin->dest, &mov->instr); } - nir_instr_insert_before(&intrin->instr, &mov->instr); + nir_builder_instr_insert(b, &mov->instr); nir_instr_remove(&intrin->instr); state->progress = true; break; } - case nir_intrinsic_store_var: { - if (intrin->variables[0]->var->data.mode != nir_var_local) + case nir_intrinsic_store_deref: { + nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]); + if (deref->mode != nir_var_function_temp) continue; - nir_src reg_src = get_deref_reg_src(intrin->variables[0], - &intrin->instr, state); + b->cursor = nir_before_instr(&intrin->instr); - nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov); - nir_src_copy(&mov->src[0].src, &intrin->src[0], mov); + nir_src reg_src = get_deref_reg_src(deref, state); + + nir_alu_instr *mov = nir_alu_instr_create(b->shader, nir_op_mov); + nir_src_copy(&mov->src[0].src, &intrin->src[1], mov); mov->dest.write_mask = nir_intrinsic_write_mask(intrin); mov->dest.dest.is_ssa = false; mov->dest.dest.reg.reg = reg_src.reg.reg; mov->dest.dest.reg.base_offset = reg_src.reg.base_offset; mov->dest.dest.reg.indirect = reg_src.reg.indirect; - nir_instr_insert_before(&intrin->instr, &mov->instr); + nir_builder_instr_insert(b, &mov->instr); nir_instr_remove(&intrin->instr); state->progress = true; break; } - case nir_intrinsic_copy_var: + case nir_intrinsic_copy_deref: unreachable("There should be no copies whatsoever at this point"); break; @@ -269,115 +253,24 @@ lower_locals_to_regs_block(nir_block *block, void *void_state) return true; } -static nir_block * -compute_reg_usedef_lca(nir_register *reg) -{ - nir_block *lca = NULL; - - list_for_each_entry(nir_dest, def_dest, ®->defs, reg.def_link) - lca = nir_dominance_lca(lca, def_dest->reg.parent_instr->block); - - list_for_each_entry(nir_src, use_src, ®->uses, use_link) - lca = nir_dominance_lca(lca, use_src->parent_instr->block); - - list_for_each_entry(nir_src, use_src, ®->if_uses, use_link) { - nir_cf_node *prev_node = nir_cf_node_prev(&use_src->parent_if->cf_node); - assert(prev_node->type == nir_cf_node_block); - lca = nir_dominance_lca(lca, nir_cf_node_as_block(prev_node)); - } - - return lca; -} - -static void -insert_constant_initializer(nir_deref_var *deref_head, nir_deref *deref_tail, - nir_block *block, - struct locals_to_regs_state *state) -{ - if (deref_tail->child) { - switch (deref_tail->child->deref_type) { - case nir_deref_type_array: { - unsigned array_elems = glsl_get_length(deref_tail->type); - - nir_deref_array arr_deref; - arr_deref.deref = *deref_tail->child; - arr_deref.deref_array_type = nir_deref_array_type_direct; - - nir_deref *old_child = deref_tail->child; - deref_tail->child = &arr_deref.deref; - for (unsigned i = 0; i < array_elems; i++) { - arr_deref.base_offset = i; - insert_constant_initializer(deref_head, &arr_deref.deref, - block, state); - } - deref_tail->child = old_child; - return; - } - - case nir_deref_type_struct: - insert_constant_initializer(deref_head, deref_tail->child, - block, state); - return; - - default: - unreachable("Invalid deref child type"); - } - } - - assert(deref_tail->child == NULL); - - nir_load_const_instr *load = - nir_deref_get_const_initializer_load(state->shader, deref_head); - nir_instr_insert_before_block(block, &load->instr); - - nir_src reg_src = get_deref_reg_src(deref_head, &load->instr, state); - - nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov); - mov->src[0].src = nir_src_for_ssa(&load->def); - mov->dest.write_mask = (1 << load->def.num_components) - 1; - mov->dest.dest.is_ssa = false; - mov->dest.dest.reg.reg = reg_src.reg.reg; - mov->dest.dest.reg.base_offset = reg_src.reg.base_offset; - mov->dest.dest.reg.indirect = reg_src.reg.indirect; - - nir_instr_insert_after(&load->instr, &mov->instr); - state->progress = true; -} - static bool nir_lower_locals_to_regs_impl(nir_function_impl *impl) { struct locals_to_regs_state state; - state.shader = impl->function->shader; - state.impl = impl; + nir_builder_init(&state.builder, impl); state.progress = false; state.regs_table = _mesa_hash_table_create(NULL, hash_deref, derefs_equal); - nir_array_init(&state.derefs_array, NULL); nir_metadata_require(impl, nir_metadata_dominance); - nir_foreach_block(impl, lower_locals_to_regs_block, &state); - - nir_array_foreach(&state.derefs_array, nir_deref_var *, deref_ptr) { - nir_deref_var *deref = *deref_ptr; - struct hash_entry *deref_entry = - _mesa_hash_table_search(state.regs_table, deref); - assert(deref_entry && deref_entry->key == deref); - nir_register *reg = (nir_register *)deref_entry->data; - - if (deref->var->constant_initializer == NULL) - continue; - - nir_block *usedef_lca = compute_reg_usedef_lca(reg); - - insert_constant_initializer(deref, &deref->deref, usedef_lca, &state); + nir_foreach_block(block, impl) { + lower_locals_to_regs_block(block, &state); } nir_metadata_preserve(impl, nir_metadata_block_index | nir_metadata_dominance); - nir_array_fini(&state.derefs_array); _mesa_hash_table_destroy(state.regs_table, NULL); return state.progress; @@ -388,7 +281,7 @@ nir_lower_locals_to_regs(nir_shader *shader) { bool progress = false; - nir_foreach_function(shader, function) { + nir_foreach_function(function, shader) { if (function->impl) progress = nir_lower_locals_to_regs_impl(function->impl) || progress; }