<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 17, 2014 at 3:30 PM, Connor Abbott <span dir="ltr"><<a href="mailto:cwabbott0@gmail.com" target="_blank">cwabbott0@gmail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Whew! Other than a few minor things below,<br>
<br>
Reviewed-by: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br>
<br>
I tried to understand it all as much as I could, but it is rather<br>
tricky... but I can't suggest anything to make it easier to<br>
understand, after all the paper itself is rather tricky and your<br>
comments help a lot. If anyone has any ideas, please say something...<br>
<div><div class="h5"><br>
On Tue, Dec 16, 2014 at 1:05 AM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> This commit rewrites the out-of-SSA pass to not be nearly as naieve. It's<br>
> based on "Revisiting Out-of-SSA Translation for Correctness, Code Quality,<br>
> and Efficiency" by Boissinot et. al. It should be fairly close to<br>
> state-of-the art.<br>
> ---<br>
> src/glsl/nir/nir_from_ssa.c | 793 +++++++++++++++++++++++++++++++++++++++-----<br>
> 1 file changed, 715 insertions(+), 78 deletions(-)<br>
><br>
> diff --git a/src/glsl/nir/nir_from_ssa.c b/src/glsl/nir/nir_from_ssa.c<br>
> index a26f0c4..62a54fe 100644<br>
> --- a/src/glsl/nir/nir_from_ssa.c<br>
> +++ b/src/glsl/nir/nir_from_ssa.c<br>
> @@ -28,54 +28,474 @@<br>
> #include "nir.h"<br>
><br>
> /*<br>
> - * Implements a quick-and-dirty out-of-ssa pass.<br>
> + * This file implements an out-of-SSA pass as described in "Revisiting<br>
> + * Out-of-SSA Translation for Correctness, Code Quality, and Efficiency" by<br>
> + * Boissinot et. al.<br>
> */<br>
><br>
> struct from_ssa_state {<br>
> void *mem_ctx;<br>
> void *dead_ctx;<br>
> struct hash_table *ssa_table;<br>
> - nir_function_impl *current_impl;<br>
> + struct hash_table *merge_node_table;<br>
> + nir_instr *instr;<br>
> + nir_function_impl *impl;<br>
> };<br>
><br>
> +/* Returns true if a dominates b */<br>
> static bool<br>
> -rewrite_ssa_src(nir_src *src, void *void_state)<br>
> +ssa_def_dominates(nir_ssa_def *a, nir_ssa_def *b)<br>
> +{<br>
> + if (a->live_index == 0) {<br>
> + /* SSA undefs always dominate */<br>
> + return true;<br>
> + } else if (b->live_index < a->live_index) {<br>
> + return false;<br>
> + } else if (a->parent_instr->block == b->parent_instr->block) {<br>
> + return a->live_index <= b->live_index;<br>
> + } else {<br>
> + nir_block *block = b->parent_instr->block;<br>
> + while (block->imm_dom != NULL) {<br>
> + if (block->imm_dom == a->parent_instr->block)<br>
> + return true;<br>
> + block = block->imm_dom;<br>
> + }<br>
> + return false;<br>
> + }<br>
> +}<br>
> +<br>
> +<br>
> +/* The following data structure, which I have named merge_set is a way of<br>
> + * representing a set registers of non-interfering registers. This is<br>
> + * based on the concept of a "dominence forest" presented in "Fast Copy<br>
> + * Coalescing and Live-Range Identification" by Budimlic et. al. but the<br>
> + * implementation concept is taken from "Revisiting Out-of-SSA Translation<br>
> + * for Correctness, Code Quality, and Efficiency" by Boissinot et. al..<br>
> + *<br>
> + * Each SSA definition is associated with a merge_node and the association<br>
> + * is represented by a combination of a hash table and the "def" parameter<br>
> + * in the merge_node structure. The merge_set stores a linked list of<br>
> + * merge_node's in dominence order of the ssa definitions. (Since the<br>
> + * liveness analysis pass indexes the SSA values in dominence order for us,<br>
> + * this is an easy thing to keep up.) It is assumed that no pair of the<br>
> + * nodes in a given set interfere. Merging two sets or checking for<br>
> + * interference can be done in a single linear-time merge-sort walk of the<br>
> + * two lists of nodes.<br>
> + */<br>
> +struct merge_set;<br>
> +<br>
> +typedef struct {<br>
> + struct exec_node node;<br>
> + struct merge_set *set;<br>
> + nir_ssa_def *def;<br>
> +} merge_node;<br>
> +<br>
> +typedef struct merge_set {<br>
> + struct exec_list nodes;<br>
> + unsigned size;<br>
> + nir_register *reg;<br>
> +} merge_set;<br>
> +<br>
> +#if 0<br>
> +static void<br>
> +merge_set_dump(merge_set *set, FILE *fp)<br>
> +{<br>
> + nir_ssa_def *dom[set->size];<br>
> + int dom_idx = -1;<br>
> +<br>
> + foreach_list_typed(merge_node, node, node, &set->nodes) {<br>
> + while (dom_idx >= 0 && !ssa_def_dominates(dom[dom_idx], node->def))<br>
> + dom_idx--;<br>
> +<br>
> + for (int i = 0; i <= dom_idx; i++)<br>
> + fprintf(fp, " ");<br>
> +<br>
> + if (node->def->name)<br>
> + fprintf(fp, "ssa_%d /* %s */\n", node->def->index, node->def->name);<br>
> + else<br>
> + fprintf(fp, "ssa_%d\n", node->def->index);<br>
> +<br>
> + dom[++dom_idx] = node->def;<br>
> + }<br>
> +}<br>
> +#endif<br>
> +<br>
> +static merge_node *<br>
> +get_merge_node(nir_ssa_def *def, struct from_ssa_state *state)<br>
> +{<br>
> + struct hash_entry *entry =<br>
> + _mesa_hash_table_search(state->merge_node_table,<br>
> + _mesa_hash_pointer(def), def);<br>
> + if (entry)<br>
> + return entry->data;<br>
> +<br>
> + merge_set *set = ralloc(state->dead_ctx, merge_set);<br>
> + exec_list_make_empty(&set->nodes);<br>
> + set->size = 1;<br>
> + set->reg = NULL;<br>
> +<br>
> + merge_node *node = ralloc(state->dead_ctx, merge_node);<br>
> + node->set = set;<br>
> + node->def = def;<br>
> + exec_list_push_head(&set->nodes, &node->node);<br>
> +<br>
> + _mesa_hash_table_insert(state->merge_node_table,<br>
> + _mesa_hash_pointer(def), def, node);<br>
> +<br>
> + return node;<br>
> +}<br>
> +<br>
> +static bool<br>
> +merge_nodes_interfere(merge_node *a, merge_node *b)<br>
> +{<br>
> + return nir_ssa_defs_interfere(a->def, b->def);<br>
> +}<br>
> +<br>
> +/* Merges b into a */<br>
> +static merge_set *<br>
> +merge_merge_sets(merge_set *a, merge_set *b)<br>
> +{<br>
> + struct exec_node *an = exec_list_get_head(&a->nodes);<br>
> + struct exec_node *bn = exec_list_get_head(&b->nodes);<br>
> + while (!exec_node_is_tail_sentinel(bn)) {<br>
> + merge_node *a_node = exec_node_data(merge_node, an, node);<br>
> + merge_node *b_node = exec_node_data(merge_node, bn, node);<br>
> +<br>
> + if (exec_node_is_tail_sentinel(an) ||<br>
> + a_node->def->live_index > b_node->def->live_index) {<br>
> + struct exec_node *next = bn->next;<br>
> + exec_node_remove(bn);<br>
> + exec_node_insert_node_before(an, bn);<br>
> + exec_node_data(merge_node, bn, node)->set = a;<br>
> + bn = next;<br>
> + } else {<br>
> + an = an->next;<br>
> + }<br>
> + }<br>
> +<br>
> + a->size += b->size;<br>
> + b->size = 0;<br>
> +<br>
> + return a;<br>
> +}<br>
> +<br>
> +/* Checks for any interference between two merge sets<br>
> + *<br>
> + * This is an implementation of Algorithm 2 in "Revisiting Out-of-SSA<br>
> + * Translation for Correctness, Code Quality, and Efficiency" by<br>
> + * Boissinot et. al.<br>
> + */<br>
> +static bool<br>
> +merge_sets_interfere(merge_set *a, merge_set *b)<br>
> +{<br>
> + merge_node *dom[a->size + b->size];<br>
> + int dom_idx = -1;<br>
> +<br>
> + struct exec_node *an = exec_list_get_head(&a->nodes);<br>
> + struct exec_node *bn = exec_list_get_head(&b->nodes);<br>
> + while (!exec_node_is_tail_sentinel(an) ||<br>
> + !exec_node_is_tail_sentinel(bn)) {<br>
> +<br>
> + merge_node *current;<br>
> + if (exec_node_is_tail_sentinel(an)) {<br>
> + current = exec_node_data(merge_node, bn, node);<br>
> + bn = bn->next;<br>
> + } else if (exec_node_is_tail_sentinel(bn)) {<br>
> + current = exec_node_data(merge_node, an, node);<br>
> + an = an->next;<br>
> + } else {<br>
> + merge_node *a_node = exec_node_data(merge_node, an, node);<br>
> + merge_node *b_node = exec_node_data(merge_node, bn, node);<br>
> +<br>
> + if (a_node->def->live_index <= b_node->def->live_index) {<br>
> + current = a_node;<br>
> + an = an->next;<br>
> + } else {<br>
> + current = b_node;<br>
> + bn = bn->next;<br>
> + }<br>
> + }<br>
> +<br>
> + while (dom_idx >= 0 &&<br>
> + !ssa_def_dominates(dom[dom_idx]->def, current->def))<br>
> + dom_idx--;<br>
> +<br>
> + if (dom_idx >= 0 && merge_nodes_interfere(current, dom[dom_idx]))<br>
> + return true;<br>
> +<br>
> + dom[++dom_idx] = current;<br>
> + }<br>
> +<br>
> + return false;<br>
> +}<br>
> +<br>
> +static nir_parallel_copy_instr *<br>
> +block_get_parallel_copy_at_end(nir_block *block, void *mem_ctx)<br>
> +{<br>
> + nir_instr *last_instr = nir_block_last_instr(block);<br>
> +<br>
> + /* First we try and find a parallel copy if it already exists. If the<br>
> + * last instruction is a jump, it will be right before the jump;<br>
> + * otherwise, it will be the last instruction.<br>
> + */<br>
> + nir_instr *pcopy_instr;<br>
> + if (last_instr != NULL && last_instr->type == nir_instr_type_jump)<br>
> + pcopy_instr = nir_instr_prev(last_instr);<br>
> + else<br>
> + pcopy_instr = last_instr;<br>
> +<br>
> + if (pcopy_instr != NULL &&<br>
> + pcopy_instr->type == nir_instr_type_parallel_copy) {<br>
> + /* A parallel copy already exists. */<br>
> + nir_parallel_copy_instr *pcopy = nir_instr_as_parallel_copy(pcopy_instr);<br>
> +<br>
> + /* This parallel copy may be the copy for the beginning of some<br>
> + * block, so we need to check for that before we return it.<br>
> + */<br>
> + if (pcopy->at_end)<br>
> + return pcopy;<br>
> + }<br>
> +<br>
> + /* At this point, we haven't found a suitable parallel copy, so we<br>
> + * have to create one.<br>
> + */<br>
> + nir_parallel_copy_instr *pcopy = nir_parallel_copy_instr_create(mem_ctx);<br>
> + pcopy->at_end = true;<br>
> +<br>
> + if (last_instr && last_instr->type == nir_instr_type_jump) {<br>
> + nir_instr_insert_before(last_instr, &pcopy->instr);<br>
> + } else {<br>
> + nir_instr_insert_after_block(block, &pcopy->instr);<br>
> + }<br>
> +<br>
> + return pcopy;<br>
> +}<br>
> +<br>
> +static bool<br>
> +isolate_phi_nodes_block(nir_block *block, void *void_state)<br>
> {<br>
> struct from_ssa_state *state = void_state;<br>
><br>
> - if (src->is_ssa) {<br>
> - struct hash_entry *entry =<br>
> - _mesa_hash_table_search(state->ssa_table,<br>
> - _mesa_hash_pointer(src->ssa),<br>
> - src->ssa);<br>
> - assert(entry);<br>
> - memset(src, 0, sizeof *src);<br>
> - src->reg.reg = (nir_register *)entry->data;<br>
> + nir_instr *last_phi_instr = NULL;<br>
> + nir_foreach_instr(block, instr) {<br>
> + /* Phi nodes only ever come at the start of a block */<br>
> + if (instr->type != nir_instr_type_phi)<br>
> + break;<br>
> +<br>
> + last_phi_instr = instr;<br>
> + }<br>
> +<br>
> + /* If we don't have any phi's, then there's nothing for us to do. */<br>
> + if (last_phi_instr == NULL)<br>
> + return true;<br>
> +<br>
> + /* If we have phi nodes, we need to create a parallel copy at the<br>
> + * start of this block but after the phi nodes.<br>
> + */<br>
> + nir_parallel_copy_instr *block_pcopy =<br>
> + nir_parallel_copy_instr_create(state->dead_ctx);<br>
> + nir_instr_insert_after(last_phi_instr, &block_pcopy->instr);<br>
> +<br>
> + nir_foreach_instr(block, instr) {<br>
> + /* Phi nodes only ever come at the start of a block */<br>
> + if (instr->type != nir_instr_type_phi)<br>
> + break;<br>
> +<br>
> + nir_phi_instr *phi = nir_instr_as_phi(instr);<br>
> + assert(phi->dest.is_ssa);<br>
> + foreach_list_typed(nir_phi_src, src, node, &phi->srcs) {<br>
> + nir_parallel_copy_instr *pcopy =<br>
> + block_get_parallel_copy_at_end(src->pred, state->dead_ctx);<br>
> +<br>
> + nir_parallel_copy_copy *copy = ralloc(state->dead_ctx,<br>
> + nir_parallel_copy_copy);<br>
> + exec_list_push_tail(&pcopy->copies, ©->node);<br>
> +<br>
> + copy->src = nir_src_copy(src->src, state->dead_ctx);<br>
> + _mesa_set_add(src->src.ssa->uses,<br>
> + _mesa_hash_pointer(&pcopy->instr), &pcopy->instr);<br>
> +<br>
> + copy->dest.is_ssa = true;<br>
> + nir_ssa_def_init(state->impl, &pcopy->instr, ©->dest.ssa,<br>
> + phi->dest.ssa.num_components, src->src.ssa->name);<br>
> +<br>
> + struct set_entry *entry = _mesa_set_search(src->src.ssa->uses,<br>
> + _mesa_hash_pointer(instr),<br>
> + instr);<br>
> + if (entry)<br>
> + /* It is possible that a phi node can use the same source twice<br>
> + * but for different basic blocks. If that happens, entry will<br>
> + * be NULL because we already deleted it. This is safe<br>
> + * because, by the time the loop is done, we will have deleted<br>
> + * all of the sources of the phi from their respective use sets<br>
> + * and moved them to the parallel copy definitions.<br>
> + */<br>
> + _mesa_set_remove(src->src.ssa->uses, entry);<br>
> +<br>
> + src->src.ssa = ©->dest.ssa;<br>
> + _mesa_set_add(copy->dest.ssa.uses, _mesa_hash_pointer(instr), instr);<br>
> + }<br>
> +<br>
> + nir_parallel_copy_copy *copy = ralloc(state->dead_ctx,<br>
> + nir_parallel_copy_copy);<br>
> + exec_list_push_tail(&block_pcopy->copies, ©->node);<br>
> +<br>
> + copy->dest.is_ssa = true;<br>
> + nir_ssa_def_init(state->impl, &block_pcopy->instr, ©->dest.ssa,<br>
> + phi->dest.ssa.num_components, phi-><a href="http://dest.ssa.name" target="_blank">dest.ssa.name</a>);<br>
> +<br>
> + nir_src copy_dest_src = {<br>
> + .ssa = ©->dest.ssa,<br>
> + .is_ssa = true,<br>
> + };<br>
> + nir_ssa_def_rewrite_uses(&phi->dest.ssa, copy_dest_src, state->mem_ctx);<br>
> +<br>
> + copy->src.is_ssa = true;<br>
> + copy->src.ssa = &phi->dest.ssa;<br>
> + _mesa_set_add(phi->dest.ssa.uses,<br>
> + _mesa_hash_pointer(&block_pcopy->instr),<br>
> + &block_pcopy->instr);<br>
> + }<br>
> +<br>
> + return true;<br>
> +}<br>
> +<br>
> +static bool<br>
> +coalesce_phi_nodes_block(nir_block *block, void *void_state)<br>
> +{<br>
> + struct from_ssa_state *state = void_state;<br>
> +<br>
> + nir_foreach_instr(block, instr) {<br>
> + /* Phi nodes only ever come at the start of a block */<br>
> + if (instr->type != nir_instr_type_phi)<br>
> + break;<br>
> +<br>
> + nir_phi_instr *phi = nir_instr_as_phi(instr);<br>
> +<br>
> + assert(phi->dest.is_ssa);<br>
> + merge_node *dest_node = get_merge_node(&phi->dest.ssa, state);<br>
> +<br>
> + foreach_list_typed(nir_phi_src, src, node, &phi->srcs) {<br>
> + assert(src->src.is_ssa);<br>
> + merge_node *src_node = get_merge_node(src->src.ssa, state);<br>
> + if (src_node->set != dest_node->set)<br>
> + merge_merge_sets(dest_node->set, src_node->set);<br>
> + }<br>
> + }<br>
> +<br>
> + return true;<br>
> +}<br>
> +<br>
> +static void<br>
> +agressive_coalesce_parallel_copy(nir_parallel_copy_instr *pcopy,<br>
> + struct from_ssa_state *state)<br>
> +{<br>
> + foreach_list_typed_safe(nir_parallel_copy_copy, copy, node, &pcopy->copies) {<br>
> + if (!copy->src.is_ssa)<br>
> + continue;<br>
> +<br>
> + /* Don't try and coalesce these */<br>
> + if (copy->dest.ssa.num_components != copy->src.ssa->num_components)<br>
> + continue;<br>
> +<br>
> + merge_node *src_node = get_merge_node(copy->src.ssa, state);<br>
> + merge_node *dest_node = get_merge_node(©->dest.ssa, state);<br>
> +<br>
> + if (src_node->set == dest_node->set)<br>
> + continue;<br>
> +<br>
> + if (!merge_sets_interfere(src_node->set, dest_node->set))<br>
> + merge_merge_sets(src_node->set, dest_node->set);<br>
> + }<br>
> +}<br>
> +<br>
> +static bool<br>
> +agressive_coalesce_block(nir_block *block, void *void_state)<br>
> +{<br>
> + struct from_ssa_state *state = void_state;<br>
> +<br>
> + nir_foreach_instr(block, instr) {<br>
> + /* Phi nodes only ever come at the start of a block */<br>
> + if (instr->type != nir_instr_type_phi) {<br>
> + if (instr->type != nir_instr_type_parallel_copy)<br>
> + break; /* The parallel copy must be right after the phis */<br>
> +<br>
> + nir_parallel_copy_instr *pcopy = nir_instr_as_parallel_copy(instr);<br>
> +<br>
> + agressive_coalesce_parallel_copy(pcopy, state);<br>
> +<br>
> + if (pcopy->at_end)<br>
> + return true;<br>
> +<br>
> + break;<br>
> + }<br>
> + }<br>
> +<br>
> + nir_instr *last_instr = nir_block_last_instr(block);<br>
> + if (last_instr && last_instr->type == nir_instr_type_parallel_copy) {<br>
> + nir_parallel_copy_instr *pcopy = nir_instr_as_parallel_copy(last_instr);<br>
> + if (pcopy->at_end)<br>
> + agressive_coalesce_parallel_copy(pcopy, state);<br>
> }<br>
><br>
> return true;<br>
> }<br>
><br>
> static nir_register *<br>
> -reg_create_from_def(nir_ssa_def *def, struct from_ssa_state *state)<br>
> +get_register_for_ssa_def(nir_ssa_def *def, struct from_ssa_state *state)<br>
> +{<br>
> + struct hash_entry *entry =<br>
> + _mesa_hash_table_search(state->merge_node_table,<br>
> + _mesa_hash_pointer(def), def);<br>
> + if (entry) {<br>
> + merge_node *node = (merge_node *)entry->data;<br>
> +<br>
> + /* If it doesn't have a register yet, create one. Note that all of<br>
> + * the things in the merge set should be the same so it doesn't<br>
> + * matter which node's definition we use.<br>
> + */<br>
> + if (node->set->reg == NULL) {<br>
> + node->set->reg = nir_local_reg_create(state->impl);<br>
> + node->set->reg->name = def->name;<br>
> + node->set->reg->num_components = def->num_components;<br>
> + node->set->reg->num_array_elems = 0;<br>
> + }<br>
> +<br>
> + return node->set->reg;<br>
> + }<br>
> +<br>
> + entry = _mesa_hash_table_search(state->ssa_table,<br>
> + _mesa_hash_pointer(def), def);<br>
> + if (entry) {<br>
> + return (nir_register *)entry->data;<br>
> + } else {<br>
> + nir_register *reg = nir_local_reg_create(state->impl);<br>
> + reg->name = def->name;<br>
> + reg->num_components = def->num_components;<br>
> + reg->num_array_elems = 0;<br>
> +<br>
> + _mesa_hash_table_insert(state->ssa_table,<br>
> + _mesa_hash_pointer(def), def, reg);<br>
> + return reg;<br>
> + }<br>
> +}<br>
> +<br>
> +static bool<br>
> +rewrite_ssa_src(nir_src *src, void *void_state)<br>
> {<br>
> - nir_register *reg = nir_local_reg_create(state->current_impl);<br>
> - reg->name = def->name;<br>
> - reg->num_components = def->num_components;<br>
> - reg->num_array_elems = 0;<br>
> + struct from_ssa_state *state = void_state;<br>
><br>
> - /* Might as well steal the use-def information from SSA */<br>
> - _mesa_set_destroy(reg->uses, NULL);<br>
> - reg->uses = def->uses;<br>
> - _mesa_set_destroy(reg->if_uses, NULL);<br>
> - reg->if_uses = def->if_uses;<br>
> - _mesa_set_add(reg->defs, _mesa_hash_pointer(def->parent_instr),<br>
> - def->parent_instr);<br>
> + if (src->is_ssa) {<br>
> + /* We don't need to remove it from the uses set because that is going<br>
> + * away. We just need to add it to the one for the register. */<br>
> + nir_register *reg = get_register_for_ssa_def(src->ssa, state);<br>
> + memset(src, 0, sizeof *src);<br>
> + src->reg.reg = reg;<br>
><br>
> - /* Add the new register to the table and rewrite the destination */<br>
> - _mesa_hash_table_insert(state->ssa_table, _mesa_hash_pointer(def), def, reg);<br>
> + _mesa_set_add(reg->uses, _mesa_hash_pointer(state->instr), state->instr);<br>
> + }<br>
><br>
> - return reg;<br>
> + return true;<br>
> }<br>
><br>
> static bool<br>
> @@ -84,82 +504,285 @@ rewrite_ssa_dest(nir_dest *dest, void *void_state)<br>
> struct from_ssa_state *state = void_state;<br>
><br>
> if (dest->is_ssa) {<br>
> - nir_register *reg = reg_create_from_def(&dest->ssa, state);<br>
> + _mesa_set_destroy(dest->ssa.uses, NULL);<br>
> + _mesa_set_destroy(dest->ssa.if_uses, NULL);<br>
> +<br>
> + nir_register *reg = get_register_for_ssa_def(&dest->ssa, state);<br>
> memset(dest, 0, sizeof *dest);<br>
> dest->reg.reg = reg;<br>
> +<br>
> + _mesa_set_add(reg->defs, _mesa_hash_pointer(state->instr), state->instr);<br>
> }<br>
><br>
> return true;<br>
> }<br>
><br>
> +/* Resolves ssa definitions to registers. While we're at it, we also<br>
> + * remove phi nodes and ssa_undef instructions<br>
> + */<br>
> static bool<br>
> -convert_from_ssa_block(nir_block *block, void *void_state)<br>
> +resolve_registers_block(nir_block *block, void *void_state)<br>
> {<br>
> struct from_ssa_state *state = void_state;<br>
><br>
> nir_foreach_instr_safe(block, instr) {<br>
> - if (instr->type == nir_instr_type_ssa_undef) {<br>
> - nir_ssa_undef_instr *undef = nir_instr_as_ssa_undef(instr);<br>
> - reg_create_from_def(&undef->def, state);<br>
> - exec_node_remove(&instr->node);<br>
> + state->instr = instr;<br>
> + nir_foreach_src(instr, rewrite_ssa_src, state);<br>
> + nir_foreach_dest(instr, rewrite_ssa_dest, state);<br>
> +<br>
> + if (instr->type == nir_instr_type_ssa_undef ||<br>
> + instr->type == nir_instr_type_phi) {<br>
> + nir_instr_remove(instr);<br>
> ralloc_steal(state->dead_ctx, instr);<br>
> - } else {<br>
> - nir_foreach_src(instr, rewrite_ssa_src, state);<br>
> - nir_foreach_dest(instr, rewrite_ssa_dest, state);<br>
> + continue;<br>
> }<br>
> }<br>
> + state->instr = NULL;<br>
><br>
> nir_if *following_if = nir_block_following_if(block);<br>
> - if (following_if)<br>
> - rewrite_ssa_src(&following_if->condition, state);<br>
> + if (following_if && following_if->condition.is_ssa) {<br>
> + nir_register *reg = get_register_for_ssa_def(following_if->condition.ssa,<br>
> + state);<br>
> + memset(&following_if->condition, 0, sizeof following_if->condition);<br>
> + following_if->condition.reg.reg = reg;<br>
> +<br>
> + _mesa_set_add(reg->if_uses, _mesa_hash_pointer(following_if),<br>
> + following_if);<br>
> + }<br>
><br>
> return true;<br>
> }<br>
><br>
> +static void<br>
> +emit_copy(nir_parallel_copy_instr *pcopy, nir_src src, nir_src dest_src,<br>
> + void *mem_ctx)<br>
> +{<br>
> + assert(!dest_src.is_ssa &&<br>
> + dest_src.reg.indirect == NULL &&<br>
> + dest_src.reg.base_offset == 0);<br>
> + nir_dest dest = {<br>
> + .reg.reg = dest_src.reg.reg,<br>
> + .reg.indirect = NULL,<br>
> + .reg.base_offset = 0,<br>
> + .is_ssa = false,<br>
> + };<br>
> +<br>
> + if (src.is_ssa)<br>
> + assert(src.ssa->num_components >= dest.reg.reg->num_components);<br>
> + else<br>
> + assert(src.reg.reg->num_components >= dest.reg.reg->num_components);<br>
> +<br>
> + nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov);<br>
> + mov->src[0].src = nir_src_copy(src, mem_ctx);<br>
> + mov->dest.dest = nir_dest_copy(dest, mem_ctx);<br>
> + mov->dest.write_mask = (1 << dest.reg.reg->num_components) - 1;<br>
> +<br>
> + nir_instr_insert_before(&pcopy->instr, &mov->instr);<br>
> +}<br>
> +<br>
> +/* Resolves a single parallel copy operation into a sequence of mov's<br>
> + *<br>
> + * This is based on Algorithm 1 from "Revisiting Out-of-SSA Translation for<br>
> + * Correctness, Code Quality, and Efficiency" by Boissinot et. al..<br>
> + * However, I never got the algorithm to work as written, so this version<br>
> + * is slightly modified.<br>
> + *<br>
> + * The algorithm works by playing this little shell game with the values.<br>
> + * We start by recording where every source value is and which source value<br>
> + * each destination value should recieve. We then grab any copy whose<br>
> + * destination is "empty", i.e. not used as a source, and do the following:<br>
> + * - Find where its source value currently lives<br>
> + * - Emit the move instruction<br>
> + * - Set the location of the source value to the destination<br>
> + * - Mark the location containing the source value<br>
> + * - Mark the destination as no longer needing to be copied<br>
> + *<br>
> + * When we run out of "empty" destinations, we have a cycle and so we<br>
> + * create a temporary register, copy to that register, and mark the value<br>
> + * we copied as living in that temporary. Now, the cycle is broken, so we<br>
> + * can continue with the above steps.<br>
> + */<br>
> +static void<br>
> +resolve_parallel_copy(nir_parallel_copy_instr *pcopy,<br>
> + struct from_ssa_state *state)<br>
> +{<br>
> + unsigned num_copies = 0;<br>
> + foreach_list_typed_safe(nir_parallel_copy_copy, copy, node, &pcopy->copies) {<br>
> + /* Sources may be SSA */<br>
> + if (!copy->src.is_ssa && copy->src.reg.reg == copy->dest.reg.reg)<br>
> + continue;<br>
> +<br>
> + /* Set both indices equal to UINT_MAX to mark them as not indexed yet. */<br>
<br>
</div></div>Stale comment?<br></blockquote><div><br></div><div>Removed.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5"><br>
> + num_copies++;<br>
> + }<br>
> +<br>
> + if (num_copies == 0) {<br>
> + /* Hooray, we don't need any copies! */<br>
> + nir_instr_remove(&pcopy->instr);<br>
> + return;<br>
> + }<br>
> +<br>
> + /* The register/source corresponding to the given index */<br>
> + nir_src values[num_copies * 2];<br>
> + memset(values, 0, sizeof values);<br>
> +<br>
> + /* The current location of a given piece of data */<br>
> + int loc[num_copies * 2];<br>
> +<br>
> + /* The piece of data that the given piece of data is to be copied from */<br>
> + int pred[num_copies * 2];<br>
> +<br>
> + /* Initialize loc and pred. We will use -1 for "null" */<br>
> + memset(loc, -1, sizeof loc);<br>
> + memset(pred, -1, sizeof pred);<br>
> +<br>
> + /* The destinations we have yet to properly fill */<br>
> + int to_do[num_copies * 2];<br>
> + int to_do_idx = -1;<br>
> +<br>
> + /* Now we set everything up:<br>
> + * - All values get assigned a temporary index<br>
> + * - Current locations are set from sources<br>
> + * - Predicessors are recorded from sources and destinations<br>
> + */<br>
> + int num_vals = 0;<br>
> + foreach_list_typed(nir_parallel_copy_copy, copy, node, &pcopy->copies) {<br>
> + /* Sources may be SSA */<br>
> + if (!copy->src.is_ssa && copy->src.reg.reg == copy->dest.reg.reg)<br>
> + continue;<br>
> +<br>
> + int src_idx = -1;<br>
> + for (int i = 0; i < num_vals; ++i) {<br>
> + if (nir_srcs_equal(values[i], copy->src))<br>
> + src_idx = i;<br>
> + }<br>
> + if (src_idx < 0) {<br>
> + src_idx = num_vals++;<br>
> + values[src_idx] = copy->src;<br>
> + }<br>
> +<br>
> + nir_src dest_src = {<br>
> + .reg.reg = copy->dest.reg.reg,<br>
> + .reg.indirect = NULL,<br>
> + .reg.base_offset = 0,<br>
> + .is_ssa = false,<br>
> + };<br>
> +<br>
> + int dest_idx = -1;<br>
> + for (int i = 0; i < num_vals; ++i) {<br>
> + if (nir_srcs_equal(values[i], dest_src))<br>
> + dest_idx = i;<br>
<br>
</div></div>Can we add an "assert(pred[dest_idx] == -1);" here to check that we<br>
don't repeat the same destination more than once in the parallel copy<br>
(plus a comment explaining what the assertion checks)? More sanity<br>
checks are always better for tricky code like this.<br></blockquote><div><br></div><div>Good plan. I'll go ahead and squash that change in to this patch since it's so small. I've made a seperate patch for cleaning up the parallel copy stuff.<br></div><div>--Jason<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5"><br>
> + }<br>
> + if (dest_idx < 0) {<br>
> + dest_idx = num_vals++;<br>
> + values[dest_idx] = dest_src;<br>
> + }<br>
> +<br>
> + loc[src_idx] = src_idx;<br>
> + pred[dest_idx] = src_idx;<br>
> +<br>
> + to_do[++to_do_idx] = dest_idx;<br>
> + }<br>
> +<br>
> + /* Currently empty destinations we can go ahead and fill */<br>
> + int ready[num_copies * 2];<br>
> + int ready_idx = -1;<br>
> +<br>
> + /* Mark the ones that are ready for copying. We know an index is a<br>
> + * destination if it has a predecessor and it's ready for copying if<br>
> + * it's not marked as containing data.<br>
> + */<br>
> + for (int i = 0; i < num_vals; i++) {<br>
> + if (pred[i] != -1 && loc[i] == -1)<br>
> + ready[++ready_idx] = i;<br>
> + }<br>
> +<br>
> + while (to_do_idx >= 0) {<br>
> + while (ready_idx >= 0) {<br>
> + int b = ready[ready_idx--];<br>
> + int a = pred[b];<br>
> + emit_copy(pcopy, values[loc[a]], values[b], state->mem_ctx);<br>
> +<br>
> + /* If any other copies want a they can find it at b */<br>
> + loc[a] = b;<br>
> +<br>
> + /* b has been filled, mark it as not needing to be copied */<br>
> + pred[b] = -1;<br>
> +<br>
> + /* If a needs to be filled, it's ready for copying now */<br>
> + if (pred[a] != -1)<br>
> + ready[++ready_idx] = a;<br>
> + }<br>
> + int b = to_do[to_do_idx--];<br>
> + if (pred[b] == -1)<br>
> + continue;<br>
> +<br>
> + /* If we got here, then we don't have any more trivial copies that we<br>
> + * can do. We have to break a cycle, so we create a new temporary<br>
> + * register for that purpose. Normally, if going out of SSA after<br>
> + * register allocation, you would want to avoid creating temporary<br>
> + * registers. However, we are going out of SSA before register<br>
> + * allocation, so we would rather not create extra register<br>
> + * dependencies for the backend to deal with. If it wants, the<br>
> + * backend can coalesce the (possibly multiple) temporaries.<br>
> + */<br>
> + assert(num_vals < num_copies * 2);<br>
> + nir_register *reg = nir_local_reg_create(state->impl);<br>
> + reg->name = "copy_temp";<br>
> + reg->num_array_elems = 0;<br>
> + if (values[b].is_ssa)<br>
> + reg->num_components = values[b].ssa->num_components;<br>
> + else<br>
> + reg->num_components = values[b].reg.reg->num_components;<br>
> + values[num_vals].is_ssa = false;<br>
> + values[num_vals].reg.reg = reg;<br>
> +<br>
> + emit_copy(pcopy, values[b], values[num_vals], state->mem_ctx);<br>
> + loc[b] = num_vals;<br>
> + ready[++ready_idx] = b;<br>
> + num_vals++;<br>
> + }<br>
> +<br>
> + nir_instr_remove(&pcopy->instr);<br>
> +}<br>
> +<br>
> +/* Resolves the parallel copies in a block. Each block can have at most<br>
> + * two: One at the beginning, right after all the phi noces, and one at<br>
> + * the end (or right before the final jump if it exists).<br>
> + */<br>
> static bool<br>
> -remove_phi_nodes(nir_block *block, void *void_state)<br>
> +resolve_parallel_copies_block(nir_block *block, void *void_state)<br>
> {<br>
> struct from_ssa_state *state = void_state;<br>
><br>
> - nir_foreach_instr_safe(block, instr) {<br>
> - /* Phi nodes only ever come at the start of a block */<br>
> - if (instr->type != nir_instr_type_phi)<br>
> - break;<br>
> + /* At this point, we have removed all of the phi nodes. If a parallel<br>
> + * copy existed right after the phi nodes in this block, it is now the<br>
> + * first instruction.<br>
> + */<br>
> + nir_instr *first_instr = nir_block_first_instr(block);<br>
> + if (first_instr == NULL)<br>
> + return true; /* Empty, nothing to do. */<br>
><br>
> - nir_foreach_dest(instr, rewrite_ssa_dest, state);<br>
> + if (first_instr->type == nir_instr_type_parallel_copy) {<br>
> + nir_parallel_copy_instr *pcopy = nir_instr_as_parallel_copy(first_instr);<br>
><br>
> - nir_phi_instr *phi = nir_instr_as_phi(instr);<br>
> - foreach_list_typed(nir_phi_src, src, node, &phi->srcs) {<br>
> - assert(src->src.is_ssa);<br>
> - struct hash_entry *entry =<br>
> - _mesa_hash_table_search(state->ssa_table,<br>
> - _mesa_hash_pointer(src->src.ssa),<br>
> - src->src.ssa);<br>
> - nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx, nir_op_imov);<br>
> - mov->dest.dest = nir_dest_copy(phi->dest, state->mem_ctx);<br>
> - if (entry) {<br>
> - nir_register *reg = (nir_register *)entry->data;<br>
> - mov->src[0].src.reg.reg = reg;<br>
> - mov->dest.write_mask = (1 << reg->num_components) - 1;<br>
> - } else {<br>
> - mov->src[0].src = nir_src_copy(src->src, state->mem_ctx);<br>
> - mov->dest.write_mask = (1 << src->src.ssa->num_components) - 1;<br>
> - }<br>
> + resolve_parallel_copy(pcopy, state);<br>
> + }<br>
><br>
> - nir_instr *block_end = nir_block_last_instr(src->pred);<br>
> - if (block_end && block_end->type == nir_instr_type_jump) {<br>
> - /* If the last instruction in the block is a jump, we want to<br>
> - * place the moves after the jump. Otherwise, we want to place<br>
> - * them at the very end.<br>
> - */<br>
> - exec_node_insert_node_before(&block_end->node, &mov->instr.node);<br>
> - } else {<br>
> - exec_list_push_tail(&src->pred->instr_list, &mov->instr.node);<br>
> - }<br>
> - }<br>
> + nir_instr *last_instr = nir_block_last_instr(block);<br>
> + if (last_instr == NULL)<br>
> + return true; /* Now empty, nothing to do. */<br>
><br>
> - exec_node_remove(&instr->node);<br>
> - ralloc_steal(state->dead_ctx, instr);<br>
> + /* If the last instruction is a jump, the parallel copy will be before<br>
> + * the jump.<br>
> + */<br>
> + if (last_instr->type == nir_instr_type_jump)<br>
> + last_instr = nir_instr_prev(last_instr);<br>
> +<br>
> + if (last_instr && last_instr->type == nir_instr_type_parallel_copy) {<br>
> + nir_parallel_copy_instr *pcopy = nir_instr_as_parallel_copy(last_instr);<br>
> + if (pcopy->at_end)<br>
> + resolve_parallel_copy(pcopy, state);<br>
> }<br>
><br>
> return true;<br>
> @@ -172,15 +795,29 @@ nir_convert_from_ssa_impl(nir_function_impl *impl)<br>
><br>
> state.mem_ctx = ralloc_parent(impl);<br>
> state.dead_ctx = ralloc_context(NULL);<br>
> - state.current_impl = impl;<br>
> + state.impl = impl;<br>
> + state.merge_node_table = _mesa_hash_table_create(NULL,<br>
> + _mesa_key_pointer_equal);<br>
> +<br>
> + nir_foreach_block(impl, isolate_phi_nodes_block, &state);<br>
> +<br>
> + nir_metadata_dirty(impl, nir_metadata_block_index |<br>
> + nir_metadata_dominance);<br>
> + nir_metadata_require(impl, nir_metadata_live_variables |<br>
> + nir_metadata_dominance);<br>
> +<br>
> + nir_foreach_block(impl, coalesce_phi_nodes_block, &state);<br>
> + nir_foreach_block(impl, agressive_coalesce_block, &state);<br>
> +<br>
> state.ssa_table = _mesa_hash_table_create(NULL, _mesa_key_pointer_equal);<br>
> + nir_foreach_block(impl, resolve_registers_block, &state);<br>
><br>
> - nir_foreach_block(impl, remove_phi_nodes, &state);<br>
> - nir_foreach_block(impl, convert_from_ssa_block, &state);<br>
> + nir_foreach_block(impl, resolve_parallel_copies_block, &state);<br>
><br>
> - /* Clean up dead instructions and the hash table */<br>
> - ralloc_free(state.dead_ctx);<br>
> + /* Clean up dead instructions and the hash tables */<br>
> _mesa_hash_table_destroy(state.ssa_table, NULL);<br>
> + _mesa_hash_table_destroy(state.merge_node_table, NULL);<br>
> + ralloc_free(state.dead_ctx);<br>
> }<br>
><br>
> void<br>
> --<br>
> 2.2.0<br>
><br>
</div></div>> _______________________________________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> <a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div></div></div>