lima/ppir: duplicate consts in nir
[mesa.git] / src / gallium / drivers / lima / ir / pp / nir.c
index cc92722861c6958e6d13baaf40f14af3e51c5f23..5175888d2473908d4acdf56075a089a88f6f5a2f 100644 (file)
@@ -42,8 +42,6 @@ static void *ppir_node_create_ssa(ppir_block *block, ppir_op op, nir_ssa_def *ss
    ppir_dest *dest = ppir_node_get_dest(node);
    dest->type = ppir_target_ssa;
    dest->ssa.num_components = ssa->num_components;
-   dest->ssa.live_in = INT_MAX;
-   dest->ssa.live_out = 0;
    dest->write_mask = u_bit_consecutive(0, ssa->num_components);
 
    if (node->type == ppir_node_type_load ||
@@ -101,48 +99,22 @@ static void ppir_node_add_src(ppir_compiler *comp, ppir_node *node,
 
    if (ns->is_ssa) {
       child = comp->var_nodes[ns->ssa->index];
-      /* Clone consts for each successor */
       switch (child->op) {
-      case ppir_op_const:
-         child = ppir_node_clone(node->block, child);
-         break;
-      case ppir_op_load_texture:
-         /* Clone texture loads for each block */
-         if (child->block != node->block) {
-            child = ppir_node_clone(node->block, child);
-            comp->var_nodes[ns->ssa->index] = child;
-         }
-         break;
       case ppir_op_load_varying:
-         if ((node->op != ppir_op_load_texture)) {
-            /* Clone varying loads for each block */
-            if (child->block != node->block) {
-               child = ppir_node_clone(node->block, child);
-               comp->var_nodes[ns->ssa->index] = child;
-            }
-            break;
-         }
-         /* At least one successor is load_texture, promote it to load_coords
-          * to ensure that is has exactly one successor */
-         child->op = ppir_op_load_coords;
-         /* Fallthrough */
-      case ppir_op_load_uniform:
-      case ppir_op_load_coords:
-         /* Clone uniform and texture coord loads for each block.
-          * Also ensure that each load has a single successor.
-          * Let's do a fetch each time and hope for a cache hit instead
-          * of increasing reg pressure.
-          */
-         if (child->block != node->block || !ppir_node_is_root(child)) {
-            child = ppir_node_clone(node->block, child);
-            comp->var_nodes[ns->ssa->index] = child;
+         /* If at least one successor is load_texture, promote it to
+          * load_coords to ensure that is has exactly one successor */
+         if (node->op == ppir_op_load_texture) {
+            nir_tex_src *nts = (nir_tex_src *)ns;
+            if (nts->src_type == nir_tex_src_coord)
+               child->op = ppir_op_load_coords;
          }
          break;
       default:
          break;
       }
 
-      ppir_node_add_dep(node, child);
+      if (child->op != ppir_op_undef)
+         ppir_node_add_dep(node, child, ppir_dep_src);
    }
    else {
       nir_register *reg = ns->reg.reg;
@@ -157,7 +129,7 @@ static void ppir_node_add_src(ppir_compiler *comp, ppir_node *node,
          }
          /* Don't add dummies or recursive deps for ops like r1 = r1 + ssa1 */
          if (child && node != child && child->op != ppir_op_dummy)
-            ppir_node_add_dep(node, child);
+            ppir_node_add_dep(node, child, ppir_dep_src);
       }
    }
 
@@ -318,6 +290,12 @@ static ppir_node *ppir_emit_intrinsic(ppir_block *block, nir_instr *ni)
 
       lnode->num_components = instr->num_components;
       lnode->index = nir_intrinsic_base(instr) * 4 + nir_intrinsic_component(instr);
+      if (nir_src_is_const(instr->src[0]))
+         lnode->index += (uint32_t)(nir_src_as_float(instr->src[0]) * 4);
+      else {
+         lnode->num_src = 1;
+         ppir_node_add_src(block->comp, &lnode->node, &lnode->src, instr->src, 1);
+      }
       return &lnode->node;
 
    case nir_intrinsic_load_frag_coord:
@@ -359,7 +337,12 @@ static ppir_node *ppir_emit_intrinsic(ppir_block *block, nir_instr *ni)
 
       lnode->num_components = instr->num_components;
       lnode->index = nir_intrinsic_base(instr);
-      lnode->index += (uint32_t)nir_src_as_float(instr->src[0]);
+      if (nir_src_is_const(instr->src[0]))
+         lnode->index += (uint32_t)nir_src_as_float(instr->src[0]);
+      else {
+         lnode->num_src = 1;
+         ppir_node_add_src(block->comp, &lnode->node, &lnode->src, instr->src, 1);
+      }
 
       return &lnode->node;
 
@@ -371,8 +354,6 @@ static ppir_node *ppir_emit_intrinsic(ppir_block *block, nir_instr *ni)
       ppir_dest *dest = ppir_node_get_dest(&alu_node->node);
       dest->type = ppir_target_ssa;
       dest->ssa.num_components = instr->num_components;
-      dest->ssa.live_in = INT_MAX;
-      dest->ssa.live_out = 0;
       dest->ssa.index = 0;
       dest->write_mask = u_bit_consecutive(0, instr->num_components);
 
@@ -418,8 +399,16 @@ static ppir_node *ppir_emit_load_const(ppir_block *block, nir_instr *ni)
 
 static ppir_node *ppir_emit_ssa_undef(ppir_block *block, nir_instr *ni)
 {
-   ppir_error("nir_ssa_undef_instr not support\n");
-   return NULL;
+   nir_ssa_undef_instr *undef = nir_instr_as_ssa_undef(ni);
+   ppir_node *node = ppir_node_create_ssa(block, ppir_op_undef, &undef->def);
+   if (!node)
+      return NULL;
+   ppir_alu_node *alu = ppir_node_to_alu(node);
+
+   ppir_dest *dest = &alu->dest;
+   dest->ssa.undef = true;
+
+   return node;
 }
 
 static ppir_node *ppir_emit_tex(ppir_block *block, nir_instr *ni)
@@ -427,7 +416,12 @@ static ppir_node *ppir_emit_tex(ppir_block *block, nir_instr *ni)
    nir_tex_instr *instr = nir_instr_as_tex(ni);
    ppir_load_texture_node *node;
 
-   if (instr->op != nir_texop_tex) {
+   switch (instr->op) {
+   case nir_texop_tex:
+   case nir_texop_txb:
+   case nir_texop_txl:
+      break;
+   default:
       ppir_error("unsupported texop %d\n", instr->op);
       return NULL;
    }
@@ -444,6 +438,7 @@ static ppir_node *ppir_emit_tex(ppir_block *block, nir_instr *ni)
 
    switch (instr->sampler_dim) {
    case GLSL_SAMPLER_DIM_2D:
+   case GLSL_SAMPLER_DIM_CUBE:
    case GLSL_SAMPLER_DIM_RECT:
    case GLSL_SAMPLER_DIM_EXTERNAL:
       break;
@@ -455,17 +450,24 @@ static ppir_node *ppir_emit_tex(ppir_block *block, nir_instr *ni)
    node->sampler_dim = instr->sampler_dim;
 
    for (int i = 0; i < instr->coord_components; i++)
-         node->src_coords.swizzle[i] = i;
+         node->src[0].swizzle[i] = i;
 
    for (int i = 0; i < instr->num_srcs; i++) {
       switch (instr->src[i].src_type) {
       case nir_tex_src_coord:
-         ppir_node_add_src(block->comp, &node->node, &node->src_coords, &instr->src[i].src,
+         ppir_node_add_src(block->comp, &node->node, &node->src[0], &instr->src[i].src,
                            u_bit_consecutive(0, instr->coord_components));
+         node->num_src++;
+         break;
+      case nir_tex_src_bias:
+      case nir_tex_src_lod:
+         node->lod_bias_en = true;
+         node->explicit_lod = (instr->src[i].src_type == nir_tex_src_lod);
+         ppir_node_add_src(block->comp, &node->node, &node->src[1], &instr->src[i].src, 1);
+         node->num_src++;
          break;
       default:
          ppir_error("unsupported texture source type\n");
-         assert(0);
          return NULL;
       }
    }
@@ -738,7 +740,7 @@ static void ppir_add_ordering_deps(ppir_compiler *comp)
       ppir_node *prev_node = NULL;
       list_for_each_entry_rev(ppir_node, node, &block->node_list, list) {
          if (prev_node && ppir_node_is_root(node) && node->op != ppir_op_const) {
-            ppir_node_add_dep(prev_node, node);
+            ppir_node_add_dep(prev_node, node, ppir_dep_sequence);
          }
          if (node->op == ppir_op_discard ||
              node->op == ppir_op_store_color ||
@@ -781,8 +783,10 @@ static void ppir_add_write_after_read_deps(ppir_compiler *comp)
                ppir_src *src = ppir_node_get_src(node, i);
                if (src && src->type == ppir_target_register &&
                    src->reg == reg &&
-                   write)
-                  ppir_node_add_dep(write, node);
+                   write) {
+                  ppir_debug("Adding dep %d for write %d\n", node->index, write->index);
+                  ppir_node_add_dep(write, node, ppir_dep_write_after_read);
+               }
             }
             ppir_dest *dest = ppir_node_get_dest(node);
             if (dest && dest->type == ppir_target_register &&
@@ -854,8 +858,6 @@ bool ppir_compile_nir(struct lima_fs_shader_state *prog, struct nir_shader *nir,
 
       r->index = reg->index;
       r->num_components = reg->num_components;
-      r->live_in = INT_MAX;
-      r->live_out = 0;
       r->is_head = false;
       list_addtail(&r->list, &comp->reg_list);
    }