X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fglsl%2Fnir%2Fnir_lower_vars_to_ssa.c;h=e670dbdc7e734227a664a99c1523b321afc39970;hb=d5c9955d3eaa7311e2b2350b6964bae516c7b7b2;hp=2b40ededc7d9126230cbd39bfe29e51ffabc58ba;hpb=55b5058e69859ba28c2f32de6edf5f0df3c6c28c;p=mesa.git diff --git a/src/glsl/nir/nir_lower_vars_to_ssa.c b/src/glsl/nir/nir_lower_vars_to_ssa.c index 2b40ededc7d..e670dbdc7e7 100644 --- a/src/glsl/nir/nir_lower_vars_to_ssa.c +++ b/src/glsl/nir/nir_lower_vars_to_ssa.c @@ -26,6 +26,8 @@ */ #include "nir.h" +#include "nir_vla.h" + struct deref_node { struct deref_node *parent; @@ -33,6 +35,13 @@ struct deref_node { bool lower_to_ssa; + /* Only valid for things that end up in the direct list. + * Note that multiple nir_deref_vars may correspond to this node, but they + * will all be equivalent, so any is as good as the other. + */ + nir_deref_var *deref; + struct exec_node direct_derefs_link; + struct set *loads; struct set *stores; struct set *copies; @@ -46,7 +55,7 @@ struct deref_node { }; struct lower_variables_state { - void *mem_ctx; + nir_shader *shader; void *dead_ctx; nir_function_impl *impl; @@ -67,157 +76,76 @@ struct lower_variables_state { * wildcards and no indirects, these are precisely the derefs that we * can actually consider lowering. */ - struct hash_table *direct_deref_nodes; + struct exec_list direct_deref_nodes; + + /* Controls whether get_deref_node will add variables to the + * direct_deref_nodes table. This is turned on when we are initially + * scanning for load/store instructions. It is then turned off so we + * don't accidentally change the direct_deref_nodes table while we're + * iterating throug it. + */ + bool add_to_direct_deref_nodes; /* A hash table mapping phi nodes to deref_state data */ struct hash_table *phi_table; }; -/* The following two functions implement a hash and equality check for - * variable dreferences. When the hash or equality function encounters an - * array, all indirects are treated as equal and are never equal to a - * direct dereference or a wildcard. - */ -static uint32_t -hash_deref(const void *void_deref) -{ - uint32_t hash = _mesa_fnv32_1a_offset_bias; - - const nir_deref_var *deref_var = void_deref; - hash = _mesa_fnv32_1a_accumulate(hash, deref_var->var); - - for (const nir_deref *deref = deref_var->deref.child; - deref; deref = deref->child) { - switch (deref->deref_type) { - case nir_deref_type_array: { - nir_deref_array *deref_array = nir_deref_as_array(deref); - - hash = _mesa_fnv32_1a_accumulate(hash, deref_array->deref_array_type); - - if (deref_array->deref_array_type == nir_deref_array_type_direct) - hash = _mesa_fnv32_1a_accumulate(hash, deref_array->base_offset); - break; - } - case nir_deref_type_struct: { - nir_deref_struct *deref_struct = nir_deref_as_struct(deref); - hash = _mesa_fnv32_1a_accumulate(hash, deref_struct->index); - break; - } - default: - assert("Invalid deref chain"); - } - } - - return hash; -} - -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) { - if (a->deref_type != b->deref_type) - return false; - - switch (a->deref_type) { - case nir_deref_type_array: { - nir_deref_array *a_arr = nir_deref_as_array(a); - nir_deref_array *b_arr = nir_deref_as_array(b); - - if (a_arr->deref_array_type != b_arr->deref_array_type) - return false; - - if (a_arr->deref_array_type == nir_deref_array_type_direct && - a_arr->base_offset != b_arr->base_offset) - return false; - break; - } - case nir_deref_type_struct: - if (nir_deref_as_struct(a)->index != nir_deref_as_struct(b)->index) - return false; - break; - default: - assert("Invalid deref chain"); - return false; - } - - assert((a->child == NULL) == (b->child == NULL)); - if((a->child == NULL) != (b->child == NULL)) - return false; - } - - return true; -} - -static int -type_get_length(const struct glsl_type *type) -{ - switch (glsl_get_base_type(type)) { - case GLSL_TYPE_STRUCT: - case GLSL_TYPE_ARRAY: - return glsl_get_length(type); - case GLSL_TYPE_FLOAT: - case GLSL_TYPE_INT: - case GLSL_TYPE_UINT: - case GLSL_TYPE_BOOL: - if (glsl_type_is_matrix(type)) - return glsl_get_matrix_columns(type); - else - return glsl_get_vector_elements(type); - default: - unreachable("Invalid deref base type"); - } -} - static struct deref_node * deref_node_create(struct deref_node *parent, - const struct glsl_type *type, void *mem_ctx) + const struct glsl_type *type, nir_shader *shader) { size_t size = sizeof(struct deref_node) + - type_get_length(type) * sizeof(struct deref_node *); + glsl_get_length(type) * sizeof(struct deref_node *); - struct deref_node *node = rzalloc_size(mem_ctx, size); + struct deref_node *node = rzalloc_size(shader, size); node->type = type; node->parent = parent; + node->deref = NULL; + exec_node_init(&node->direct_derefs_link); return node; } -/* Gets the deref_node for the given deref chain and creates it if it - * doesn't yet exist. If the deref is fully-qualified and direct and - * add_to_direct_deref_nodes is true, it will be added to the hash table of - * of fully-qualified direct derefs. +/* Returns the deref node associated with the given variable. This will be + * the root of the tree representing all of the derefs of the given variable. */ static struct deref_node * -get_deref_node(nir_deref_var *deref, bool add_to_direct_deref_nodes, - struct lower_variables_state *state) +get_deref_node_for_var(nir_variable *var, struct lower_variables_state *state) { - bool is_direct = true; - struct deref_node *node; struct hash_entry *var_entry = - _mesa_hash_table_search(state->deref_var_nodes, deref->var); + _mesa_hash_table_search(state->deref_var_nodes, var); if (var_entry) { - node = var_entry->data; + return var_entry->data; } else { - node = deref_node_create(NULL, deref->deref.type, state->dead_ctx); - _mesa_hash_table_insert(state->deref_var_nodes, deref->var, node); + node = deref_node_create(NULL, var->type, state->dead_ctx); + _mesa_hash_table_insert(state->deref_var_nodes, var, node); + return node; } +} + +/* Gets the deref_node for the given deref chain and creates it if it + * doesn't yet exist. If the deref is fully-qualified and direct and + * state->add_to_direct_deref_nodes is true, it will be added to the hash + * table of of fully-qualified direct derefs. + */ +static struct deref_node * +get_deref_node(nir_deref_var *deref, struct lower_variables_state *state) +{ + bool is_direct = true; + + /* Start at the base of the chain. */ + struct deref_node *node = get_deref_node_for_var(deref->var, state); + assert(deref->deref.type == node->type); for (nir_deref *tail = deref->deref.child; tail; tail = tail->child) { switch (tail->deref_type) { case nir_deref_type_struct: { nir_deref_struct *deref_struct = nir_deref_as_struct(tail); - assert(deref_struct->index < type_get_length(node->type)); + assert(deref_struct->index < glsl_get_length(node->type)); if (node->children[deref_struct->index] == NULL) node->children[deref_struct->index] = @@ -236,7 +164,7 @@ get_deref_node(nir_deref_var *deref, bool add_to_direct_deref_nodes, * out-of-bounds offset. We need to handle this at least * somewhat gracefully. */ - if (arr->base_offset >= type_get_length(node->type)) + if (arr->base_offset >= glsl_get_length(node->type)) return NULL; if (node->children[arr->base_offset] == NULL) @@ -276,8 +204,14 @@ get_deref_node(nir_deref_var *deref, bool add_to_direct_deref_nodes, assert(node); - if (is_direct && add_to_direct_deref_nodes) - _mesa_hash_table_insert(state->direct_deref_nodes, deref, node); + /* Only insert if it isn't already in the list. */ + if (is_direct && state->add_to_direct_deref_nodes && + node->direct_derefs_link.next == NULL) { + node->deref = deref; + assert(deref->var != NULL); + exec_list_push_tail(&state->direct_deref_nodes, + &node->direct_derefs_link); + } return node; } @@ -341,7 +275,7 @@ foreach_deref_node_match(nir_deref_var *deref, { nir_deref_var var_deref = *deref; var_deref.deref.child = NULL; - struct deref_node *node = get_deref_node(&var_deref, false, state); + struct deref_node *node = get_deref_node(&var_deref, state); if (node == NULL) return false; @@ -363,6 +297,10 @@ deref_may_be_aliased_node(struct deref_node *node, nir_deref *deref, if (arr->deref_array_type == nir_deref_array_type_indirect) return true; + /* If there is an indirect at this level, we're aliased. */ + if (node->indirect) + return true; + assert(arr->deref_array_type == nir_deref_array_type_direct); if (node->children[arr->base_offset] && @@ -411,64 +349,56 @@ static bool deref_may_be_aliased(nir_deref_var *deref, struct lower_variables_state *state) { - nir_deref_var var_deref = *deref; - var_deref.deref.child = NULL; - struct deref_node *node = get_deref_node(&var_deref, false, state); - - /* An invalid dereference can't be aliased. */ - if (node == NULL) - return false; - - return deref_may_be_aliased_node(node, &deref->deref, state); + return deref_may_be_aliased_node(get_deref_node_for_var(deref->var, state), + &deref->deref, state); } static void -register_load_instr(nir_intrinsic_instr *load_instr, bool create_node, +register_load_instr(nir_intrinsic_instr *load_instr, struct lower_variables_state *state) { - struct deref_node *node = get_deref_node(load_instr->variables[0], - create_node, state); + struct deref_node *node = get_deref_node(load_instr->variables[0], state); if (node == NULL) return; if (node->loads == NULL) - node->loads = _mesa_set_create(state->dead_ctx, + node->loads = _mesa_set_create(state->dead_ctx, _mesa_hash_pointer, _mesa_key_pointer_equal); - _mesa_set_add(node->loads, _mesa_hash_pointer(load_instr), load_instr); + _mesa_set_add(node->loads, load_instr); } static void -register_store_instr(nir_intrinsic_instr *store_instr, bool create_node, +register_store_instr(nir_intrinsic_instr *store_instr, struct lower_variables_state *state) { - struct deref_node *node = get_deref_node(store_instr->variables[0], - create_node, state); + struct deref_node *node = get_deref_node(store_instr->variables[0], state); if (node == NULL) return; if (node->stores == NULL) - node->stores = _mesa_set_create(state->dead_ctx, + node->stores = _mesa_set_create(state->dead_ctx, _mesa_hash_pointer, _mesa_key_pointer_equal); - _mesa_set_add(node->stores, _mesa_hash_pointer(store_instr), store_instr); + _mesa_set_add(node->stores, store_instr); } static void -register_copy_instr(nir_intrinsic_instr *copy_instr, bool create_node, +register_copy_instr(nir_intrinsic_instr *copy_instr, struct lower_variables_state *state) { for (unsigned idx = 0; idx < 2; idx++) { - struct deref_node *node = get_deref_node(copy_instr->variables[idx], - create_node, state); + struct deref_node *node = + get_deref_node(copy_instr->variables[idx], state); + if (node == NULL) continue; if (node->copies == NULL) - node->copies = _mesa_set_create(state->dead_ctx, + node->copies = _mesa_set_create(state->dead_ctx, _mesa_hash_pointer, _mesa_key_pointer_equal); - _mesa_set_add(node->copies, _mesa_hash_pointer(copy_instr), copy_instr); + _mesa_set_add(node->copies, copy_instr); } } @@ -486,15 +416,15 @@ register_variable_uses_block(nir_block *block, void *void_state) switch (intrin->intrinsic) { case nir_intrinsic_load_var: - register_load_instr(intrin, true, state); + register_load_instr(intrin, state); break; case nir_intrinsic_store_var: - register_store_instr(intrin, true, state); + register_store_instr(intrin, state); break; case nir_intrinsic_copy_var: - register_copy_instr(intrin, true, state); + register_copy_instr(intrin, state); break; default: @@ -505,128 +435,6 @@ register_variable_uses_block(nir_block *block, void *void_state) return true; } -/* Walks down the deref chain and returns the next deref in the chain whose - * child is a wildcard. In other words, given the chain a[1].foo[*].bar, - * this function will return the deref to foo. Calling it a second time - * with the [*].bar, it will return NULL. - */ -static nir_deref * -deref_next_wildcard_parent(nir_deref *deref) -{ - for (nir_deref *tail = deref; tail->child; tail = tail->child) { - if (tail->child->deref_type != nir_deref_type_array) - continue; - - nir_deref_array *arr = nir_deref_as_array(tail->child); - - if (arr->deref_array_type == nir_deref_array_type_wildcard) - return tail; - } - - return NULL; -} - -/* Returns the last deref in the chain. - */ -static nir_deref * -get_deref_tail(nir_deref *deref) -{ - while (deref->child) - deref = deref->child; - - return deref; -} - -/* This function recursively walks the given deref chain and replaces the - * given copy instruction with an equivalent sequence load/store - * operations. - * - * @copy_instr The copy instruction to replace; new instructions will be - * inserted before this one - * - * @dest_head The head of the destination variable deref chain - * - * @src_head The head of the source variable deref chain - * - * @dest_tail The current tail of the destination variable deref chain; - * this is used for recursion and external callers of this - * function should call it with tail == head - * - * @src_tail The current tail of the source variable deref chain; - * this is used for recursion and external callers of this - * function should call it with tail == head - * - * @state The current variable lowering state - */ -static void -emit_copy_load_store(nir_intrinsic_instr *copy_instr, - nir_deref_var *dest_head, nir_deref_var *src_head, - nir_deref *dest_tail, nir_deref *src_tail, - struct lower_variables_state *state) -{ - /* Find the next pair of wildcards */ - nir_deref *src_arr_parent = deref_next_wildcard_parent(src_tail); - nir_deref *dest_arr_parent = deref_next_wildcard_parent(dest_tail); - - if (src_arr_parent || dest_arr_parent) { - /* Wildcards had better come in matched pairs */ - assert(dest_arr_parent && dest_arr_parent); - - nir_deref_array *src_arr = nir_deref_as_array(src_arr_parent->child); - nir_deref_array *dest_arr = nir_deref_as_array(dest_arr_parent->child); - - unsigned length = type_get_length(src_arr_parent->type); - /* The wildcards should represent the same number of elements */ - assert(length == type_get_length(dest_arr_parent->type)); - assert(length > 0); - - /* Walk over all of the elements that this wildcard refers to and - * call emit_copy_load_store on each one of them */ - src_arr->deref_array_type = nir_deref_array_type_direct; - dest_arr->deref_array_type = nir_deref_array_type_direct; - for (unsigned i = 0; i < length; i++) { - src_arr->base_offset = i; - dest_arr->base_offset = i; - emit_copy_load_store(copy_instr, dest_head, src_head, - &dest_arr->deref, &src_arr->deref, state); - } - src_arr->deref_array_type = nir_deref_array_type_wildcard; - dest_arr->deref_array_type = nir_deref_array_type_wildcard; - } else { - /* In this case, we have no wildcards anymore, so all we have to do - * is just emit the load and store operations. */ - src_tail = get_deref_tail(src_tail); - dest_tail = get_deref_tail(dest_tail); - - assert(src_tail->type == dest_tail->type); - - unsigned num_components = glsl_get_vector_elements(src_tail->type); - - nir_deref *src_deref = nir_copy_deref(state->mem_ctx, &src_head->deref); - nir_deref *dest_deref = nir_copy_deref(state->mem_ctx, &dest_head->deref); - - nir_intrinsic_instr *load = - nir_intrinsic_instr_create(state->mem_ctx, nir_intrinsic_load_var); - load->num_components = num_components; - load->variables[0] = nir_deref_as_var(src_deref); - load->dest.is_ssa = true; - nir_ssa_def_init(&load->instr, &load->dest.ssa, num_components, NULL); - - nir_instr_insert_before(©_instr->instr, &load->instr); - register_load_instr(load, false, state); - - nir_intrinsic_instr *store = - nir_intrinsic_instr_create(state->mem_ctx, nir_intrinsic_store_var); - store->num_components = num_components; - store->variables[0] = nir_deref_as_var(dest_deref); - store->src[0].is_ssa = true; - store->src[0].ssa = &load->dest.ssa; - - nir_instr_insert_before(©_instr->instr, &store->instr); - register_store_instr(store, false, state); - } -} - /* Walks over all of the copy instructions to or from the given deref_node * and lowers them to load/store intrinsics. */ @@ -641,20 +449,17 @@ lower_copies_to_load_store(struct deref_node *node, set_foreach(node->copies, copy_entry) { nir_intrinsic_instr *copy = (void *)copy_entry->key; - emit_copy_load_store(copy, copy->variables[0], copy->variables[1], - ©->variables[0]->deref, - ©->variables[1]->deref, - state); + nir_lower_var_copy_instr(copy, state->shader); for (unsigned i = 0; i < 2; ++i) { - struct deref_node *arg_node = get_deref_node(copy->variables[i], - false, state); - if (arg_node == NULL) + struct deref_node *arg_node = + get_deref_node(copy->variables[i], state); + + /* Only bother removing copy entries for other nodes */ + if (arg_node == NULL || arg_node == node) continue; - struct set_entry *arg_entry = _mesa_set_search(arg_node->copies, - copy_entry->hash, - copy); + struct set_entry *arg_entry = _mesa_set_search(arg_node->copies, copy); assert(arg_entry); _mesa_set_remove(node->copies, arg_entry); } @@ -662,68 +467,9 @@ lower_copies_to_load_store(struct deref_node *node, nir_instr_remove(©->instr); } - return true; -} + node->copies = NULL; -/* Returns a load_const instruction that represents the constant - * initializer for the given deref chain. The caller is responsible for - * ensuring that there actually is a constant initializer. - */ -static nir_load_const_instr * -get_const_initializer_load(const nir_deref_var *deref, - struct lower_variables_state *state) -{ - nir_constant *constant = deref->var->constant_initializer; - const nir_deref *tail = &deref->deref; - unsigned matrix_offset = 0; - while (tail->child) { - switch (tail->child->deref_type) { - case nir_deref_type_array: { - nir_deref_array *arr = nir_deref_as_array(tail->child); - assert(arr->deref_array_type == nir_deref_array_type_direct); - if (glsl_type_is_matrix(tail->type)) { - assert(arr->deref.child == NULL); - matrix_offset = arr->base_offset; - } else { - constant = constant->elements[arr->base_offset]; - } - break; - } - - case nir_deref_type_struct: { - constant = constant->elements[nir_deref_as_struct(tail->child)->index]; - break; - } - - default: - unreachable("Invalid deref child type"); - } - - tail = tail->child; - } - - nir_load_const_instr *load = - nir_load_const_instr_create(state->mem_ctx, - glsl_get_vector_elements(tail->type)); - - matrix_offset *= load->def.num_components; - for (unsigned i = 0; i < load->def.num_components; i++) { - switch (glsl_get_base_type(tail->type)) { - case GLSL_TYPE_FLOAT: - case GLSL_TYPE_INT: - case GLSL_TYPE_UINT: - load->value.u[i] = constant->value.u[matrix_offset + i]; - break; - case GLSL_TYPE_BOOL: - load->value.u[i] = constant->value.u[matrix_offset + i] ? - NIR_TRUE : NIR_FALSE; - break; - default: - unreachable("Invalid immediate type"); - } - } - - return load; + return true; } /** Pushes an SSA def onto the def stack for the given node @@ -794,7 +540,7 @@ get_ssa_def_for_block(struct deref_node *node, nir_block *block, * given block. This means that we need to add an undef and use that. */ nir_ssa_undef_instr *undef = - nir_ssa_undef_instr_create(state->mem_ctx, + nir_ssa_undef_instr_create(state->shader, glsl_get_vector_elements(node->type)); nir_instr_insert_before_cf_list(&state->impl->body, &undef->instr); def_stack_push(node, &undef->def, state); @@ -822,12 +568,13 @@ add_phi_sources(nir_block *block, nir_block *pred, struct deref_node *node = entry->data; - nir_phi_src *src = ralloc(state->mem_ctx, nir_phi_src); + nir_phi_src *src = ralloc(phi, nir_phi_src); src->pred = pred; + src->src.parent_instr = &phi->instr; src->src.is_ssa = true; src->src.ssa = get_ssa_def_for_block(node, pred, state); - _mesa_set_add(src->src.ssa->uses, _mesa_hash_pointer(instr), instr); + list_addtail(&src->src.use_link, &src->src.ssa->uses); exec_list_push_tail(&phi->srcs, &src->node); } @@ -864,8 +611,8 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state) switch (intrin->intrinsic) { case nir_intrinsic_load_var: { - struct deref_node *node = get_deref_node(intrin->variables[0], - false, state); + struct deref_node *node = + get_deref_node(intrin->variables[0], state); if (node == NULL) { /* If we hit this path then we are referencing an invalid @@ -874,26 +621,21 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state) * should result in an undefined value. */ nir_ssa_undef_instr *undef = - nir_ssa_undef_instr_create(state->mem_ctx, + nir_ssa_undef_instr_create(state->shader, intrin->num_components); nir_instr_insert_before(&intrin->instr, &undef->instr); nir_instr_remove(&intrin->instr); - nir_src new_src = { - .is_ssa = true, - .ssa = &undef->def, - }; - - nir_ssa_def_rewrite_uses(&intrin->dest.ssa, new_src, - state->mem_ctx); + nir_ssa_def_rewrite_uses(&intrin->dest.ssa, + nir_src_for_ssa(&undef->def)); continue; } if (!node->lower_to_ssa) continue; - nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx, + nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov); mov->src[0].src.is_ssa = true; mov->src[0].src.ssa = get_ssa_def_for_block(node, block, state); @@ -903,26 +645,20 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state) assert(intrin->dest.is_ssa); mov->dest.write_mask = (1 << intrin->num_components) - 1; - mov->dest.dest.is_ssa = true; - nir_ssa_def_init(&mov->instr, &mov->dest.dest.ssa, - intrin->num_components, NULL); + nir_ssa_dest_init(&mov->instr, &mov->dest.dest, + intrin->num_components, NULL); nir_instr_insert_before(&intrin->instr, &mov->instr); nir_instr_remove(&intrin->instr); - nir_src new_src = { - .is_ssa = true, - .ssa = &mov->dest.dest.ssa, - }; - - nir_ssa_def_rewrite_uses(&intrin->dest.ssa, new_src, - state->mem_ctx); + nir_ssa_def_rewrite_uses(&intrin->dest.ssa, + nir_src_for_ssa(&mov->dest.dest.ssa)); break; } case nir_intrinsic_store_var: { - struct deref_node *node = get_deref_node(intrin->variables[0], - false, state); + struct deref_node *node = + get_deref_node(intrin->variables[0], state); if (node == NULL) { /* Probably an out-of-bounds array store. That should be a @@ -939,7 +675,7 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state) assert(intrin->src[0].is_ssa); - nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx, + nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov); mov->src[0].src.is_ssa = true; mov->src[0].src.ssa = intrin->src[0].ssa; @@ -947,9 +683,8 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state) mov->src[0].swizzle[i] = 0; mov->dest.write_mask = (1 << intrin->num_components) - 1; - mov->dest.dest.is_ssa = true; - nir_ssa_def_init(&mov->instr, &mov->dest.dest.ssa, - intrin->num_components, NULL); + nir_ssa_dest_init(&mov->instr, &mov->dest.dest, + intrin->num_components, NULL); nir_instr_insert_before(&intrin->instr, &mov->instr); @@ -1000,8 +735,7 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state) if (intrin->intrinsic != nir_intrinsic_store_var) continue; - struct deref_node *node = get_deref_node(intrin->variables[0], - false, state); + struct deref_node *node = get_deref_node(intrin->variables[0], state); if (!node) continue; @@ -1025,8 +759,8 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state) static void insert_phi_nodes(struct lower_variables_state *state) { - unsigned work[state->impl->num_blocks]; - unsigned has_already[state->impl->num_blocks]; + NIR_VLA_ZERO(unsigned, work, state->impl->num_blocks); + NIR_VLA_ZERO(unsigned, has_already, state->impl->num_blocks); /* * Since the work flags already prevent us from inserting a node that has @@ -1036,18 +770,13 @@ insert_phi_nodes(struct lower_variables_state *state) * function. So all we need to handle W is an array and a pointer to the * next element to be inserted and the next element to be removed. */ - nir_block *W[state->impl->num_blocks]; - - memset(work, 0, sizeof work); - memset(has_already, 0, sizeof has_already); + NIR_VLA(nir_block *, W, state->impl->num_blocks); unsigned w_start, w_end; unsigned iter_count = 0; - struct hash_entry *deref_entry; - hash_table_foreach(state->direct_deref_nodes, deref_entry) { - struct deref_node *node = deref_entry->data; - + foreach_list_typed(struct deref_node, node, direct_derefs_link, + &state->direct_deref_nodes) { if (node->stores == NULL) continue; @@ -1083,10 +812,9 @@ insert_phi_nodes(struct lower_variables_state *state) continue; if (has_already[next->index] < iter_count) { - nir_phi_instr *phi = nir_phi_instr_create(state->mem_ctx); - phi->dest.is_ssa = true; - nir_ssa_def_init(&phi->instr, &phi->dest.ssa, - glsl_get_vector_elements(node->type), NULL); + nir_phi_instr *phi = nir_phi_instr_create(state->shader); + nir_ssa_dest_init(&phi->instr, &phi->dest, + glsl_get_vector_elements(node->type), NULL); nir_instr_insert_before_block(next, &phi->instr); _mesa_hash_table_insert(state->phi_table, phi, node); @@ -1135,40 +863,40 @@ nir_lower_vars_to_ssa_impl(nir_function_impl *impl) { struct lower_variables_state state; - state.mem_ctx = ralloc_parent(impl); - state.dead_ctx = ralloc_context(state.mem_ctx); + state.shader = impl->overload->function->shader; + state.dead_ctx = ralloc_context(state.shader); state.impl = impl; state.deref_var_nodes = _mesa_hash_table_create(state.dead_ctx, _mesa_hash_pointer, _mesa_key_pointer_equal); - state.direct_deref_nodes = _mesa_hash_table_create(state.dead_ctx, - hash_deref, derefs_equal); + exec_list_make_empty(&state.direct_deref_nodes); state.phi_table = _mesa_hash_table_create(state.dead_ctx, _mesa_hash_pointer, _mesa_key_pointer_equal); + /* Build the initial deref structures and direct_deref_nodes table */ + state.add_to_direct_deref_nodes = true; nir_foreach_block(impl, register_variable_uses_block, &state); - struct set *outputs = _mesa_set_create(state.dead_ctx, - _mesa_key_pointer_equal); - bool progress = false; nir_metadata_require(impl, nir_metadata_block_index); - struct hash_entry *entry; - hash_table_foreach(state.direct_deref_nodes, entry) { - nir_deref_var *deref = (void *)entry->key; - struct deref_node *node = entry->data; + /* We're about to iterate through direct_deref_nodes. Don't modify it. */ + state.add_to_direct_deref_nodes = false; + + foreach_list_typed_safe(struct deref_node, node, direct_derefs_link, + &state.direct_deref_nodes) { + nir_deref_var *deref = node->deref; if (deref->var->data.mode != nir_var_local) { - _mesa_hash_table_remove(state.direct_deref_nodes, entry); + exec_node_remove(&node->direct_derefs_link); continue; } if (deref_may_be_aliased(deref, &state)) { - _mesa_hash_table_remove(state.direct_deref_nodes, entry); + exec_node_remove(&node->direct_derefs_link); continue; } @@ -1176,16 +904,14 @@ nir_lower_vars_to_ssa_impl(nir_function_impl *impl) progress = true; if (deref->var->constant_initializer) { - nir_load_const_instr *load = get_const_initializer_load(deref, &state); + nir_load_const_instr *load = + nir_deref_get_const_initializer_load(state.shader, deref); nir_ssa_def_init(&load->instr, &load->def, glsl_get_vector_elements(node->type), NULL); nir_instr_insert_before_cf_list(&impl->body, &load->instr); def_stack_push(node, &load->def, &state); } - if (deref->var->data.mode == nir_var_shader_out) - _mesa_set_add(outputs, _mesa_hash_pointer(node), node); - foreach_deref_node_match(deref, lower_copies_to_load_store, &state); } @@ -1194,8 +920,16 @@ nir_lower_vars_to_ssa_impl(nir_function_impl *impl) nir_metadata_require(impl, nir_metadata_dominance); + /* We may have lowered some copy instructions to load/store + * instructions. The uses from the copy instructions hav already been + * removed but we need to rescan to ensure that the uses from the newly + * added load/store instructions are registered. We need this + * information for phi node insertion below. + */ + nir_foreach_block(impl, register_variable_uses_block, &state); + insert_phi_nodes(&state); - rename_variables_block(impl->start_block, &state); + rename_variables_block(nir_start_block(impl), &state); nir_metadata_preserve(impl, nir_metadata_block_index | nir_metadata_dominance);