<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Dec 27, 2016 at 9:04 PM, Connor Abbott <span dir="ltr"><<a href="mailto:cwabbott0@gmail.com" target="_blank">cwabbott0@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Nice reduction in code size! The whole series is<br>
<br>
Reviewed-by: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br></blockquote><div><br></div><div>After rebasing on top of loop unrolling etc. I found a bug.  The fix is here:<br><br><a href="https://cgit.freedesktop.org/~jekstrand/mesa/commit/?h=wip/nir-to-ssa">https://cgit.freedesktop.org/~jekstrand/mesa/commit/?h=wip/nir-to-ssa</a><br><br></div><div>Mind giving it a quick look.  I plan to squash it in.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
For the last patch I looked at the 'review/regs-to-ssa' branch on cgit<br>
which seems like an earlier version of the branch, although AFAIK the<br>
last patch there is the same as this patch.<br>
<div><div class="gmail-h5"><br>
On Fri, Dec 16, 2016 at 2:05 PM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> This keeps some of Connor's original code.  However, while I was at it,<br>
> I updated this very old pass to a bit more modern NIR.<br>
> ---<br>
>  src/compiler/nir/nir_lower_<wbr>regs_to_ssa.c | 589 +++++++++---------------------<wbr>-<br>
>  1 file changed, 167 insertions(+), 422 deletions(-)<br>
><br>
> diff --git a/src/compiler/nir/nir_lower_<wbr>regs_to_ssa.c b/src/compiler/nir/nir_lower_<wbr>regs_to_ssa.c<br>
> index 74c1961..4ed34ef 100644<br>
> --- a/src/compiler/nir/nir_lower_<wbr>regs_to_ssa.c<br>
> +++ b/src/compiler/nir/nir_lower_<wbr>regs_to_ssa.c<br>
> @@ -26,513 +26,258 @@<br>
>   */<br>
><br>
>  #include "nir.h"<br>
> -#include <stdlib.h><br>
> +#include "nir_builder.h"<br>
> +#include "nir_phi_builder.h"<br>
> +#include "nir_vla.h"<br>
><br>
> -/*<br>
> - * Implements the classic to-SSA algorithm described by Cytron et. al. in<br>
> - * "Efficiently Computing Static Single Assignment Form and the Control<br>
> - * Dependence Graph."<br>
> - */<br>
> -<br>
> -/* inserts a phi node of the form reg = phi(reg, reg, reg, ...) */<br>
> -<br>
> -static void<br>
> -insert_trivial_phi(nir_<wbr>register *reg, nir_block *block, void *mem_ctx)<br>
> -{<br>
> -   nir_phi_instr *instr = nir_phi_instr_create(mem_ctx);<br>
> -<br>
> -   instr->dest.reg.reg = reg;<br>
> -   struct set_entry *entry;<br>
> -   set_foreach(block-><wbr>predecessors, entry) {<br>
> -      nir_block *pred = (nir_block *) entry->key;<br>
> -<br>
> -      nir_phi_src *src = ralloc(instr, nir_phi_src);<br>
> -      src->pred = pred;<br>
> -      src->src.is_ssa = false;<br>
> -      src->src.reg.base_offset = 0;<br>
> -      src->src.reg.indirect = NULL;<br>
> -      src->src.reg.reg = reg;<br>
> -      exec_list_push_tail(&instr-><wbr>srcs, &src->node);<br>
> -   }<br>
> -<br>
> -   nir_instr_insert_before_block(<wbr>block, &instr->instr);<br>
> -}<br>
> -<br>
> -static void<br>
> -insert_phi_nodes(nir_<wbr>function_impl *impl)<br>
> -{<br>
> -   void *mem_ctx = ralloc_parent(impl);<br>
> -<br>
> -   unsigned *work = calloc(impl->num_blocks, sizeof(unsigned));<br>
> -   unsigned *has_already = calloc(impl->num_blocks, sizeof(unsigned));<br>
> -<br>
> -   /*<br>
> -    * Since the work flags already prevent us from inserting a node that has<br>
> -    * ever been inserted into W, we don't need to use a set to represent W.<br>
> -    * Also, since no block can ever be inserted into W more than once, we know<br>
> -    * that the maximum size of W is the number of basic blocks in the<br>
> -    * function. So all we need to handle W is an array and a pointer to the<br>
> -    * next element to be inserted and the next element to be removed.<br>
> -    */<br>
> -   nir_block **W = malloc(impl->num_blocks * sizeof(nir_block *));<br>
> -   unsigned w_start, w_end;<br>
> -<br>
> -   unsigned iter_count = 0;<br>
> -<br>
> -   nir_index_blocks(impl);<br>
> -<br>
> -   foreach_list_typed(nir_<wbr>register, reg, node, &impl->registers) {<br>
> -      if (reg->num_array_elems != 0)<br>
> -         continue;<br>
> -<br>
> -      w_start = w_end = 0;<br>
> -      iter_count++;<br>
> +struct regs_to_ssa_state {<br>
> +   nir_shader *shader;<br>
><br>
> -      nir_foreach_def(dest, reg) {<br>
> -         nir_instr *def = dest->reg.parent_instr;<br>
> -         if (work[def->block->index] < iter_count)<br>
> -            W[w_end++] = def->block;<br>
> -         work[def->block->index] = iter_count;<br>
> -      }<br>
> -<br>
> -      while (w_start != w_end) {<br>
> -         nir_block *cur = W[w_start++];<br>
> -         struct set_entry *entry;<br>
> -         set_foreach(cur->dom_frontier, entry) {<br>
> -            nir_block *next = (nir_block *) entry->key;<br>
> -<br>
> -            /*<br>
> -             * If there's more than one return statement, then the end block<br>
> -             * can be a join point for some definitions. However, there are<br>
> -             * no instructions in the end block, so nothing would use those<br>
> -             * phi nodes. Of course, we couldn't place those phi nodes<br>
> -             * anyways due to the restriction of having no instructions in the<br>
> -             * end block...<br>
> -             */<br>
> -            if (next == impl->end_block)<br>
> -               continue;<br>
> -<br>
> -            if (has_already[next->index] < iter_count) {<br>
> -               insert_trivial_phi(reg, next, mem_ctx);<br>
> -               has_already[next->index] = iter_count;<br>
> -               if (work[next->index] < iter_count) {<br>
> -                  work[next->index] = iter_count;<br>
> -                  W[w_end++] = next;<br>
> -               }<br>
> -            }<br>
> -         }<br>
> -      }<br>
> -   }<br>
> -<br>
> -   free(work);<br>
> -   free(has_already);<br>
> -   free(W);<br>
> -}<br>
> -<br>
> -typedef struct {<br>
> -   nir_ssa_def **stack;<br>
> -   int index;<br>
> -   unsigned num_defs; /** < used to add indices to debug names */<br>
> -#ifndef NDEBUG<br>
> -   unsigned stack_size;<br>
> -#endif<br>
> -} reg_state;<br>
> -<br>
> -typedef struct {<br>
> -   reg_state *states;<br>
> -   void *mem_ctx;<br>
> -   nir_instr *parent_instr;<br>
> -   nir_if *parent_if;<br>
> -   nir_function_impl *impl;<br>
> -<br>
> -   /* map from SSA value -> original register */<br>
> -   struct hash_table *ssa_map;<br>
> -} rewrite_state;<br>
> -<br>
> -static nir_ssa_def *get_ssa_src(nir_register *reg, rewrite_state *state)<br>
> -{<br>
> -   unsigned index = reg->index;<br>
> -<br>
> -   if (state->states[index].index == -1) {<br>
> -      /*<br>
> -       * We're using an undefined register, create a new undefined SSA value<br>
> -       * to preserve the information that this source is undefined<br>
> -       */<br>
> -      nir_ssa_undef_instr *instr =<br>
> -         nir_ssa_undef_instr_create(<wbr>state->mem_ctx, reg->num_components,<br>
> -                                    reg->bit_size);<br>
> -<br>
> -      /*<br>
> -       * We could just insert the undefined instruction before the instruction<br>
> -       * we're rewriting, but we could be rewriting a phi source in which case<br>
> -       * we can't do that, so do the next easiest thing - insert it at the<br>
> -       * beginning of the program. In the end, it doesn't really matter where<br>
> -       * the undefined instructions are because they're going to be ignored<br>
> -       * in the backend.<br>
> -       */<br>
> -      nir_instr_insert_before_cf_<wbr>list(&state->impl->body, &instr->instr);<br>
> -      return &instr->def;<br>
> -   }<br>
> -<br>
> -   return state->states[index].stack[<wbr>state->states[index].index];<br>
> -}<br>
> +   struct nir_phi_builder_value **values;<br>
> +};<br>
><br>
>  static bool<br>
> -rewrite_use(nir_src *src, void *_state)<br>
> +rewrite_src(nir_src *src, void *_state)<br>
>  {<br>
> -   rewrite_state *state = (rewrite_state *) _state;<br>
> +   struct regs_to_ssa_state *state = _state;<br>
><br>
>     if (src->is_ssa)<br>
>        return true;<br>
><br>
> -   unsigned index = src->reg.reg->index;<br>
> -<br>
> -   if (state->states[index].stack == NULL)<br>
> +   nir_instr *instr = src->parent_instr;<br>
> +   nir_register *reg = src->reg.reg;<br>
> +   struct nir_phi_builder_value *value = state->values[reg->index];<br>
> +   if (!value)<br>
>        return true;<br>
><br>
> -   nir_ssa_def *def = get_ssa_src(src->reg.reg, state);<br>
> -   if (state->parent_instr)<br>
> -      nir_instr_rewrite_src(state-><wbr>parent_instr, src, nir_src_for_ssa(def));<br>
> -   else<br>
> -      nir_if_rewrite_condition(<wbr>state->parent_if, nir_src_for_ssa(def));<br>
> +   nir_ssa_def *def = nir_phi_builder_value_get_<wbr>block_def(value, instr->block);<br>
> +   nir_instr_rewrite_src(instr, src, nir_src_for_ssa(def));<br>
><br>
>     return true;<br>
>  }<br>
><br>
> +static void<br>
> +rewrite_if_condition(nir_if *nif, struct regs_to_ssa_state *state)<br>
> +{<br>
> +   if (nif->condition.is_ssa)<br>
> +      return;<br>
> +<br>
> +   nir_block *block = nir_cf_node_as_block(nir_cf_<wbr>node_prev(&nif->cf_node));<br>
> +   nir_register *reg = nif->condition.reg.reg;<br>
> +   struct nir_phi_builder_value *value = state->values[reg->index];<br>
> +   if (!value)<br>
> +      return;<br>
> +<br>
> +   nir_ssa_def *def = nir_phi_builder_value_get_<wbr>block_def(value, block);<br>
> +   nir_if_rewrite_condition(nif, nir_src_for_ssa(def));<br>
> +}<br>
> +<br>
>  static bool<br>
> -rewrite_def_forwards(nir_dest *dest, void *_state)<br>
> +rewrite_dest(nir_dest *dest, void *_state)<br>
>  {<br>
> -   rewrite_state *state = (rewrite_state *) _state;<br>
> +   struct regs_to_ssa_state *state = _state;<br>
><br>
>     if (dest->is_ssa)<br>
>        return true;<br>
><br>
> +   nir_instr *instr = dest->reg.parent_instr;<br>
>     nir_register *reg = dest->reg.reg;<br>
> -   unsigned index = reg->index;<br>
> -<br>
> -   if (state->states[index].stack == NULL)<br>
> +   struct nir_phi_builder_value *value = state->values[reg->index];<br>
> +   if (!value)<br>
>        return true;<br>
><br>
> -   char *name = NULL;<br>
> -   if (dest->reg.reg->name)<br>
> -      name = ralloc_asprintf(state->mem_<wbr>ctx, "%s_%u", dest->reg.reg->name,<br>
> -                             state->states[index].num_defs)<wbr>;<br>
> -<br>
>     list_del(&dest->reg.def_link);<br>
> -   nir_ssa_dest_init(state-><wbr>parent_instr, dest, reg->num_components,<br>
> -                     reg->bit_size, name);<br>
> -   ralloc_free(name);<br>
> -<br>
> -   /* push our SSA destination on the stack */<br>
> -   state->states[index].index++;<br>
> -   assert(state->states[index].<wbr>index < state->states[index].stack_<wbr>size);<br>
> -   state->states[index].stack[<wbr>state->states[index].index] = &dest->ssa;<br>
> -   state->states[index].num_defs+<wbr>+;<br>
> +   nir_ssa_dest_init(instr, dest, reg->num_components,<br>
> +                     reg->bit_size, reg->name);<br>
><br>
> -   _mesa_hash_table_insert(state-<wbr>>ssa_map, &dest->ssa, reg);<br>
> +   nir_phi_builder_value_set_<wbr>block_def(value, instr->block, &dest->ssa);<br>
><br>
>     return true;<br>
>  }<br>
><br>
>  static void<br>
> -rewrite_alu_instr_forward(<wbr>nir_alu_instr *instr, rewrite_state *state)<br>
> +rewrite_alu_instr(nir_alu_<wbr>instr *alu, struct regs_to_ssa_state *state)<br>
>  {<br>
> -   state->parent_instr = &instr->instr;<br>
> +   nir_foreach_src(&alu->instr, rewrite_src, state);<br>
><br>
> -   nir_foreach_src(&instr->instr, rewrite_use, state);<br>
> -<br>
> -   if (instr->dest.dest.is_ssa)<br>
> +   if (alu->dest.dest.is_ssa)<br>
>        return;<br>
><br>
> -   nir_register *reg = instr->dest.dest.reg.reg;<br>
> -   unsigned index = reg->index;<br>
> +   nir_register *reg = alu->dest.dest.reg.reg;<br>
> +   struct nir_phi_builder_value *value = state->values[reg->index];<br>
> +   if (!value)<br>
> +      return;<br>
><br>
> -   if (state->states[index].stack == NULL)<br>
> +   unsigned write_mask = alu->dest.write_mask;<br>
> +   if (write_mask == (1 << reg->num_components) - 1) {<br>
> +      /* This is the simple case where the instruction writes all the<br>
> +       * components.  We can handle that the same as any other destination.<br>
> +       */<br>
> +      rewrite_dest(&alu->dest.dest, state);<br>
>        return;<br>
> +   }<br>
><br>
> -   unsigned write_mask = instr->dest.write_mask;<br>
> -   if (write_mask != (1 << instr->dest.dest.reg.reg->num_<wbr>components) - 1) {<br>
> -      /*<br>
> -       * Calculate the number of components the final instruction, which for<br>
> -       * per-component things is the number of output components of the<br>
> -       * instruction and non-per-component things is the number of enabled<br>
> -       * channels in the write mask.<br>
> +   /* Calculate the number of components the final instruction, which for<br>
> +    * per-component things is the number of output components of the<br>
> +    * instruction and non-per-component things is the number of enabled<br>
> +    * channels in the write mask.<br>
> +    */<br>
> +   unsigned num_components;<br>
> +   unsigned vec_swizzle[4] = { 0, 1, 2, 3 };<br>
> +   if (nir_op_infos[alu->op].output_<wbr>size == 0) {<br>
> +      /* Figure out the swizzle we need on the vecN operation and compute<br>
> +       * the number of components in the SSA def at the same time.<br>
>         */<br>
> -      unsigned num_components;<br>
> -      if (nir_op_infos[instr->op].<wbr>output_size == 0) {<br>
> -         unsigned temp = (write_mask & 0x5) + ((write_mask >> 1) & 0x5);<br>
> -         num_components = (temp & 0x3) + ((temp >> 2) & 0x3);<br>
> -      } else {<br>
> -         num_components = nir_op_infos[instr->op].<wbr>output_size;<br>
> +      num_components = 0;<br>
> +      for (unsigned index = 0; index < 4; index++) {<br>
> +         if (write_mask & (1 << index))<br>
> +            vec_swizzle[index] = num_components++;<br>
>        }<br>
><br>
> -      char *name = NULL;<br>
> -      if (instr->dest.dest.reg.reg-><wbr>name)<br>
> -         name = ralloc_asprintf(state->mem_<wbr>ctx, "%s_%u",<br>
> -                                reg->name, state->states[index].num_defs)<wbr>;<br>
> -<br>
> -      instr->dest.write_mask = (1 << num_components) - 1;<br>
> -      list_del(&instr->dest.dest.<wbr>reg.def_link);<br>
> -      nir_ssa_dest_init(&instr-><wbr>instr, &instr->dest.dest, num_components,<br>
> -                        reg->bit_size, name);<br>
> -      ralloc_free(name);<br>
> +      /* When we change the output writemask, we need to change<br>
> +       * the swizzles for per-component inputs too<br>
> +       */<br>
> +      for (unsigned i = 0; i < nir_op_infos[alu->op].num_<wbr>inputs; i++) {<br>
> +         if (nir_op_infos[alu->op].input_<wbr>sizes[i] != 0)<br>
> +            continue;<br>
><br>
> -      if (nir_op_infos[instr->op].<wbr>output_size == 0) {<br>
>           /*<br>
> -          * When we change the output writemask, we need to change the<br>
> -          * swizzles for per-component inputs too<br>
> +          * We keep two indices:<br>
> +          * 1. The index of the original (non-SSA) component<br>
> +          * 2. The index of the post-SSA, compacted, component<br>
> +          *<br>
> +          * We need to map the swizzle component at index 1 to the swizzle<br>
> +          * component at index 2.  Since index 1 is always larger than<br>
> +          * index 2, we can do it in a single loop.<br>
>            */<br>
> -         for (unsigned i = 0; i < nir_op_infos[instr->op].num_<wbr>inputs; i++) {<br>
> -            if (nir_op_infos[instr->op].<wbr>input_sizes[i] != 0)<br>
> -               continue;<br>
> -<br>
> -            unsigned new_swizzle[4] = {0, 0, 0, 0};<br>
> -<br>
> -            /*<br>
> -             * We keep two indices:<br>
> -             * 1. The index of the original (non-SSA) component<br>
> -             * 2. The index of the post-SSA, compacted, component<br>
> -             *<br>
> -             * We need to map the swizzle component at index 1 to the swizzle<br>
> -             * component at index 2.<br>
> -             */<br>
><br>
> -            unsigned ssa_index = 0;<br>
> -            for (unsigned index = 0; index < 4; index++) {<br>
> -               if (!((write_mask >> index) & 1))<br>
> -                  continue;<br>
> -<br>
> -               new_swizzle[ssa_index] = instr->src[i].swizzle[index];<br>
> -               ssa_index++;<br>
> -            }<br>
> -<br>
> -            for (unsigned j = 0; j < 4; j++)<br>
> -               instr->src[i].swizzle[j] = new_swizzle[j];<br>
> -         }<br>
> -      }<br>
> -<br>
> -      nir_op op;<br>
> -      switch (reg->num_components) {<br>
> -      case 2: op = nir_op_vec2; break;<br>
> -      case 3: op = nir_op_vec3; break;<br>
> -      case 4: op = nir_op_vec4; break;<br>
> -      default: unreachable("not reached");<br>
> -      }<br>
> -<br>
> -      nir_alu_instr *vec = nir_alu_instr_create(state-><wbr>mem_ctx, op);<br>
> -<br>
> -      vec->dest.dest.reg.reg = reg;<br>
> -      vec->dest.write_mask = (1 << reg->num_components) - 1;<br>
> -<br>
> -      nir_ssa_def *old_src = get_ssa_src(reg, state);<br>
> -      nir_ssa_def *new_src = &instr->dest.dest.ssa;<br>
> +         unsigned ssa_index = 0;<br>
> +         for (unsigned index = 0; index < 4; index++) {<br>
> +            if (!((write_mask >> index) & 1))<br>
> +               continue;<br>
><br>
> -      unsigned ssa_index = 0;<br>
> -      for (unsigned i = 0; i < reg->num_components; i++) {<br>
> -         vec->src[i].src.is_ssa = true;<br>
> -         if ((write_mask >> i) & 1) {<br>
> -            vec->src[i].src.ssa = new_src;<br>
> -            if (nir_op_infos[instr->op].<wbr>output_size == 0)<br>
> -               vec->src[i].swizzle[0] = ssa_index;<br>
> -            else<br>
> -               vec->src[i].swizzle[0] = i;<br>
> -            ssa_index++;<br>
> -         } else {<br>
> -            vec->src[i].src.ssa = old_src;<br>
> -            vec->src[i].swizzle[0] = i;<br>
> +            alu->src[i].swizzle[ssa_index+<wbr>+] = alu->src[i].swizzle[index];<br>
>           }<br>
> +         assert(ssa_index == num_components);<br>
>        }<br>
> -<br>
> -      nir_instr_insert_after(&instr-<wbr>>instr, &vec->instr);<br>
> -<br>
> -      state->parent_instr = &vec->instr;<br>
> -      rewrite_def_forwards(&vec-><wbr>dest.dest, state);<br>
>     } else {<br>
> -      rewrite_def_forwards(&instr-><wbr>dest.dest, state);<br>
> +      num_components = nir_op_infos[alu->op].output_<wbr>size;<br>
> +   }<br>
> +   assert(num_components <= 4);<br>
> +<br>
> +   alu->dest.write_mask = (1 << num_components) - 1;<br>
> +   list_del(&alu->dest.dest.reg.<wbr>def_link);<br>
> +   nir_ssa_dest_init(&alu->instr, &alu->dest.dest, num_components,<br>
> +                     reg->bit_size, reg->name);<br>
> +<br>
> +   nir_op vecN_op;<br>
> +   switch (reg->num_components) {<br>
> +   case 2: vecN_op = nir_op_vec2; break;<br>
> +   case 3: vecN_op = nir_op_vec3; break;<br>
> +   case 4: vecN_op = nir_op_vec4; break;<br>
> +   default: unreachable("not reached");<br>
>     }<br>
> -}<br>
><br>
> -static void<br>
> -rewrite_phi_instr(nir_phi_<wbr>instr *instr, rewrite_state *state)<br>
> -{<br>
> -   state->parent_instr = &instr->instr;<br>
> -   rewrite_def_forwards(&instr-><wbr>dest, state);<br>
> -}<br>
> +   nir_alu_instr *vec = nir_alu_instr_create(state-><wbr>shader, vecN_op);<br>
><br>
> -static void<br>
> -rewrite_instr_forward(nir_<wbr>instr *instr, rewrite_state *state)<br>
> -{<br>
> -   if (instr->type == nir_instr_type_alu) {<br>
> -      rewrite_alu_instr_forward(nir_<wbr>instr_as_alu(instr), state);<br>
> -      return;<br>
> -   }<br>
> +   nir_ssa_def *old_src =<br>
> +      nir_phi_builder_value_get_<wbr>block_def(value, alu->instr.block);<br>
> +   nir_ssa_def *new_src = &alu->dest.dest.ssa;<br>
><br>
> -   if (instr->type == nir_instr_type_phi) {<br>
> -      rewrite_phi_instr(nir_instr_<wbr>as_phi(instr), state);<br>
> -      return;<br>
> +   for (unsigned i = 0; i < reg->num_components; i++) {<br>
> +      if (write_mask & (1 << i)) {<br>
> +         vec->src[i].src = nir_src_for_ssa(new_src);<br>
> +         vec->src[i].swizzle[0] = vec_swizzle[i];<br>
> +      } else {<br>
> +         vec->src[i].src = nir_src_for_ssa(old_src);<br>
> +         vec->src[i].swizzle[0] = i;<br>
> +      }<br>
>     }<br>
><br>
> -   state->parent_instr = instr;<br>
> +   nir_ssa_dest_init(&vec->instr, &vec->dest.dest, reg->num_components,<br>
> +                     reg->bit_size, reg->name);<br>
> +   nir_instr_insert(nir_after_<wbr>instr(&alu->instr), &vec->instr);<br>
><br>
> -   nir_foreach_src(instr, rewrite_use, state);<br>
> -   nir_foreach_dest(instr, rewrite_def_forwards, state);<br>
> +   nir_phi_builder_value_set_<wbr>block_def(value, alu->instr.block,<br>
> +                                       &vec->dest.dest.ssa);<br>
>  }<br>
><br>
>  static void<br>
> -rewrite_phi_sources(nir_block *block, nir_block *pred, rewrite_state *state)<br>
> -{<br>
> -   nir_foreach_instr(instr, block) {<br>
> -      if (instr->type != nir_instr_type_phi)<br>
> -         break;<br>
> -<br>
> -      nir_phi_instr *phi_instr = nir_instr_as_phi(instr);<br>
> -<br>
> -      state->parent_instr = instr;<br>
> -<br>
> -      nir_foreach_phi_src(src, phi_instr) {<br>
> -         if (src->pred == pred) {<br>
> -            rewrite_use(&src->src, state);<br>
> -            break;<br>
> -         }<br>
> -      }<br>
> -   }<br>
> -}<br>
> -<br>
> -static bool<br>
> -rewrite_def_backwards(nir_<wbr>dest *dest, void *_state)<br>
> +lower_regs_to_ssa_impl(nir_<wbr>function_impl *impl)<br>
>  {<br>
> -   rewrite_state *state = (rewrite_state *) _state;<br>
> +   if (exec_list_is_empty(&impl-><wbr>registers))<br>
> +      return;<br>
><br>
> -   if (!dest->is_ssa)<br>
> -      return true;<br>
> +   nir_metadata_require(impl, nir_metadata_block_index |<br>
> +                              nir_metadata_dominance);<br>
> +   nir_index_local_regs(impl);<br>
><br>
> -   struct hash_entry *entry =<br>
> -      _mesa_hash_table_search(state-<wbr>>ssa_map, &dest->ssa);<br>
> +   struct regs_to_ssa_state state;<br>
> +   state.shader = impl->function->shader;<br>
> +   state.values = malloc(impl->reg_alloc * sizeof(*state.values));<br>
><br>
> -   if (!entry)<br>
> -      return true;<br>
> +   struct nir_phi_builder *phi_build = nir_phi_builder_create(impl);<br>
><br>
> -   nir_register *reg = (nir_register *) entry->data;<br>
> -   unsigned index = reg->index;<br>
> +   const unsigned block_set_words = BITSET_WORDS(impl->num_blocks)<wbr>;<br>
> +   NIR_VLA(BITSET_WORD, defs, block_set_words);<br>
><br>
> -   state->states[index].index--;<br>
> -   assert(state->states[index].<wbr>index >= -1);<br>
> -<br>
> -   return true;<br>
> -}<br>
> +   nir_foreach_register(reg, &impl->registers) {<br>
> +      if (reg->num_array_elems != 0 || reg->is_packed) {<br>
> +         /* This pass only really works on "plain" registers.  If it's a<br>
> +          * packed or array register, just set the value to NULL so that the<br>
> +          * rewrite portion of the pass will know to ignore it.<br>
> +          */<br>
> +         state.values[reg->index] = NULL;<br>
> +         continue;<br>
> +      }<br>
><br>
> -static void<br>
> -rewrite_instr_backwards(nir_<wbr>instr *instr, rewrite_state *state)<br>
> -{<br>
> -   nir_foreach_dest(instr, rewrite_def_backwards, state);<br>
> -}<br>
> +      memset(defs, 0, block_set_words * sizeof(*defs));<br>
><br>
> -static void<br>
> -rewrite_block(nir_block *block, rewrite_state *state)<br>
> -{<br>
> -   /* This will skip over any instructions after the current one, which is<br>
> -    * what we want because those instructions (vector gather, conditional<br>
> -    * select) will already be in SSA form.<br>
> -    */<br>
> -   nir_foreach_instr_safe(instr, block) {<br>
> -      rewrite_instr_forward(instr, state);<br>
> -   }<br>
> +      nir_foreach_def(dest, reg)<br>
> +         BITSET_SET(defs, dest->reg.parent_instr->block-<wbr>>index);<br>
><br>
> -   if (block != state->impl->end_block &&<br>
> -       !nir_cf_node_is_last(&block-><wbr>cf_node) &&<br>
> -       nir_cf_node_next(&block->cf_<wbr>node)->type == nir_cf_node_if) {<br>
> -      nir_if *if_stmt = nir_cf_node_as_if(nir_cf_node_<wbr>next(&block->cf_node));<br>
> -      state->parent_instr = NULL;<br>
> -      state->parent_if = if_stmt;<br>
> -      rewrite_use(&if_stmt-><wbr>condition, state);<br>
> +      state.values[reg->index] =<br>
> +         nir_phi_builder_add_value(phi_<wbr>build, reg->num_components,<br>
> +                                   reg->bit_size, defs);<br>
>     }<br>
><br>
> -   if (block->successors[0])<br>
> -      rewrite_phi_sources(block-><wbr>successors[0], block, state);<br>
> -   if (block->successors[1])<br>
> -      rewrite_phi_sources(block-><wbr>successors[1], block, state);<br>
> -<br>
> -   for (unsigned i = 0; i < block->num_dom_children; i++)<br>
> -      rewrite_block(block->dom_<wbr>children[i], state);<br>
> +   nir_foreach_block(block, impl) {<br>
> +      nir_foreach_instr(instr, block) {<br>
> +         if (instr->type == nir_instr_type_alu) {<br>
> +            rewrite_alu_instr(nir_instr_<wbr>as_alu(instr), &state);<br>
> +         } else {<br>
> +            nir_foreach_src(instr, rewrite_src, &state);<br>
> +            nir_foreach_dest(instr, rewrite_dest, &state);<br>
> +         }<br>
> +      }<br>
><br>
> -   nir_foreach_instr_reverse(<wbr>instr, block) {<br>
> -      rewrite_instr_backwards(instr, state);<br>
> +      nir_if *following_if = nir_block_get_following_if(<wbr>block);<br>
> +      if (following_if)<br>
> +         rewrite_if_condition(<wbr>following_if, &state);<br>
>     }<br>
> -}<br>
><br>
> -static void<br>
> -remove_unused_regs(nir_<wbr>function_impl *impl, rewrite_state *state)<br>
> -{<br>
> -   foreach_list_typed_safe(nir_<wbr>register, reg, node, &impl->registers) {<br>
> -      if (state->states[reg->index].<wbr>stack != NULL)<br>
> -         exec_node_remove(&reg->node);<br>
> -   }<br>
> -}<br>
> +   nir_phi_builder_finish(phi_<wbr>build);<br>
><br>
> -static void<br>
> -init_rewrite_state(nir_<wbr>function_impl *impl, rewrite_state *state)<br>
> -{<br>
> -   state->impl = impl;<br>
> -   state->mem_ctx = ralloc_parent(impl);<br>
> -   state->ssa_map = _mesa_hash_table_create(NULL, _mesa_hash_pointer,<br>
> -                                            _mesa_key_pointer_equal);<br>
> -   state->states = rzalloc_array(NULL, reg_state, impl->reg_alloc);<br>
> -<br>
> -   foreach_list_typed(nir_<wbr>register, reg, node, &impl->registers) {<br>
> -      assert(reg->index < impl->reg_alloc);<br>
> -      if (reg->num_array_elems > 0) {<br>
> -         state->states[reg->index].<wbr>stack = NULL;<br>
> -      } else {<br>
> -         /*<br>
> -          * Calculate a conservative estimate of the stack size based on the<br>
> -          * number of definitions there are. Note that this function *must* be<br>
> -          * called after phi nodes are inserted so we can count phi node<br>
> -          * definitions too.<br>
> -          */<br>
> -         unsigned stack_size = list_length(&reg->defs);<br>
> -<br>
> -         state->states[reg->index].<wbr>stack = ralloc_array(state->states,<br>
> -                                                        nir_ssa_def *,<br>
> -                                                        stack_size);<br>
> -#ifndef NDEBUG<br>
> -         state->states[reg->index].<wbr>stack_size = stack_size;<br>
> -#endif<br>
> -         state->states[reg->index].<wbr>index = -1;<br>
> -         state->states[reg->index].num_<wbr>defs = 0;<br>
> +   nir_foreach_register_safe(reg, &impl->registers) {<br>
> +      if (state.values[reg->index]) {<br>
> +         assert(list_empty(&reg->uses))<wbr>;<br>
> +         assert(list_empty(&reg->if_<wbr>uses));<br>
> +         assert(list_empty(&reg->defs))<wbr>;<br>
> +         exec_node_remove(&reg->node);<br>
>        }<br>
>     }<br>
> -}<br>
><br>
> -static void<br>
> -destroy_rewrite_state(<wbr>rewrite_state *state)<br>
> -{<br>
> -   _mesa_hash_table_destroy(<wbr>state->ssa_map, NULL);<br>
> -   ralloc_free(state->states);<br>
> -}<br>
> -<br>
> -static void<br>
> -lower_regs_to_ssa_impl(nir_<wbr>function_impl *impl)<br>
> -{<br>
> -   nir_metadata_require(impl, nir_metadata_dominance);<br>
> -<br>
> -   insert_phi_nodes(impl);<br>
> -<br>
> -   rewrite_state state;<br>
> -   init_rewrite_state(impl, &state);<br>
> -<br>
> -   rewrite_block(nir_start_block(<wbr>impl), &state);<br>
> -<br>
> -   remove_unused_regs(impl, &state);<br>
> +   free(state.values);<br>
><br>
>     nir_metadata_preserve(impl, nir_metadata_block_index |<br>
>                                 nir_metadata_dominance);<br>
> -<br>
> -   destroy_rewrite_state(&state);<br>
>  }<br>
><br>
>  void<br>
>  nir_lower_regs_to_ssa(nir_<wbr>shader *shader)<br>
>  {<br>
> +   assert(exec_list_is_empty(&<wbr>shader->registers));<br>
> +<br>
>     nir_foreach_function(function, shader) {<br>
>        if (function->impl)<br>
>           lower_regs_to_ssa_impl(<wbr>function->impl);<br>
> --<br>
> 2.5.0.400.gff86faf<br>
><br>
</div></div>> ______________________________<wbr>_________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> <a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</blockquote></div><br></div></div>