[Mesa-dev] [PATCH 1/8] nir: Make nir_copy_deref follow the "clone" pattern

Jason Ekstrand jason at jlekstrand.net
Mon Dec 26 17:13:28 UTC 2016


We rename it to nir_deref_clone, re-order the sources to match the other
clone functions, and expose nir_deref_var_clone.  This past part, in
particular, lets us get rid of quite a few lines since we no longer have
to call nir_copy_deref and wrap it in deref_to_var.
---
 src/compiler/glsl/glsl_to_nir.cpp                  |  3 +--
 src/compiler/nir/nir.c                             | 22 +++++++++++-----------
 src/compiler/nir/nir.h                             |  4 ++--
 src/compiler/nir/nir_builder.h                     |  6 +++---
 src/compiler/nir/nir_inline_functions.c            |  2 +-
 src/compiler/nir/nir_lower_indirect_derefs.c       |  6 ++----
 src/compiler/nir/nir_lower_tex.c                   | 18 ++++++------------
 src/compiler/nir/nir_lower_var_copies.c            |  4 ++--
 src/compiler/nir/nir_split_var_copies.c            | 17 +++++++----------
 src/compiler/spirv/spirv_to_nir.c                  | 22 ++++++++++------------
 src/compiler/spirv/vtn_glsl450.c                   |  3 +--
 src/compiler/spirv/vtn_variables.c                 |  3 +--
 src/intel/vulkan/anv_nir_lower_input_attachments.c |  3 +--
 13 files changed, 48 insertions(+), 65 deletions(-)

diff --git a/src/compiler/glsl/glsl_to_nir.cpp b/src/compiler/glsl/glsl_to_nir.cpp
index fcaca9f..8efd030 100644
--- a/src/compiler/glsl/glsl_to_nir.cpp
+++ b/src/compiler/glsl/glsl_to_nir.cpp
@@ -1179,8 +1179,7 @@ nir_visitor::visit(ir_assignment *ir)
       nir_intrinsic_instr_create(this->shader, nir_intrinsic_store_var);
    store->num_components = ir->lhs->type->vector_elements;
    nir_intrinsic_set_write_mask(store, ir->write_mask);
-   nir_deref *store_deref = nir_copy_deref(store, &lhs_deref->deref);
-   store->variables[0] = nir_deref_as_var(store_deref);
+   store->variables[0] = nir_deref_var_clone(lhs_deref, store);
    store->src[0] = nir_src_for_ssa(src);
 
    if (ir->condition) {
diff --git a/src/compiler/nir/nir.c b/src/compiler/nir/nir.c
index 885616e..6057143 100644
--- a/src/compiler/nir/nir.c
+++ b/src/compiler/nir/nir.c
@@ -624,18 +624,18 @@ nir_deref_struct_create(void *mem_ctx, unsigned field_index)
    return deref;
 }
 
-static nir_deref_var *
-copy_deref_var(void *mem_ctx, nir_deref_var *deref)
+nir_deref_var *
+nir_deref_var_clone(const nir_deref_var *deref, void *mem_ctx)
 {
    nir_deref_var *ret = nir_deref_var_create(mem_ctx, deref->var);
    ret->deref.type = deref->deref.type;
    if (deref->deref.child)
-      ret->deref.child = nir_copy_deref(ret, deref->deref.child);
+      ret->deref.child = nir_deref_clone(deref->deref.child, ret);
    return ret;
 }
 
 static nir_deref_array *
-copy_deref_array(void *mem_ctx, nir_deref_array *deref)
+deref_array_clone(const nir_deref_array *deref, void *mem_ctx)
 {
    nir_deref_array *ret = nir_deref_array_create(mem_ctx);
    ret->base_offset = deref->base_offset;
@@ -645,33 +645,33 @@ copy_deref_array(void *mem_ctx, nir_deref_array *deref)
    }
    ret->deref.type = deref->deref.type;
    if (deref->deref.child)
-      ret->deref.child = nir_copy_deref(ret, deref->deref.child);
+      ret->deref.child = nir_deref_clone(deref->deref.child, ret);
    return ret;
 }
 
 static nir_deref_struct *
-copy_deref_struct(void *mem_ctx, nir_deref_struct *deref)
+deref_struct_clone(const nir_deref_struct *deref, void *mem_ctx)
 {
    nir_deref_struct *ret = nir_deref_struct_create(mem_ctx, deref->index);
    ret->deref.type = deref->deref.type;
    if (deref->deref.child)
-      ret->deref.child = nir_copy_deref(ret, deref->deref.child);
+      ret->deref.child = nir_deref_clone(deref->deref.child, ret);
    return ret;
 }
 
 nir_deref *
-nir_copy_deref(void *mem_ctx, nir_deref *deref)
+nir_deref_clone(const nir_deref *deref, void *mem_ctx)
 {
    if (deref == NULL)
       return NULL;
 
    switch (deref->deref_type) {
    case nir_deref_type_var:
-      return &copy_deref_var(mem_ctx, nir_deref_as_var(deref))->deref;
+      return &nir_deref_var_clone(nir_deref_as_var(deref), mem_ctx)->deref;
    case nir_deref_type_array:
-      return &copy_deref_array(mem_ctx, nir_deref_as_array(deref))->deref;
+      return &deref_array_clone(nir_deref_as_array(deref), mem_ctx)->deref;
    case nir_deref_type_struct:
-      return &copy_deref_struct(mem_ctx, nir_deref_as_struct(deref))->deref;
+      return &deref_struct_clone(nir_deref_as_struct(deref), mem_ctx)->deref;
    default:
       unreachable("Invalid dereference type");
    }
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 6557631..d896679 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -1927,8 +1927,6 @@ nir_deref_var *nir_deref_var_create(void *mem_ctx, nir_variable *var);
 nir_deref_array *nir_deref_array_create(void *mem_ctx);
 nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
 
-nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
-
 typedef bool (*nir_deref_foreach_leaf_cb)(nir_deref_var *deref, void *state);
 bool nir_deref_foreach_leaf(nir_deref_var *deref,
                             nir_deref_foreach_leaf_cb cb, void *state);
@@ -2236,6 +2234,8 @@ nir_shader *nir_shader_clone(void *mem_ctx, const nir_shader *s);
 nir_function_impl *nir_function_impl_clone(const nir_function_impl *fi);
 nir_constant *nir_constant_clone(const nir_constant *c, nir_variable *var);
 nir_variable *nir_variable_clone(const nir_variable *c, nir_shader *shader);
+nir_deref *nir_deref_clone(const nir_deref *deref, void *mem_ctx);
+nir_deref_var *nir_deref_var_clone(const nir_deref_var *deref, void *mem_ctx);
 
 #ifdef DEBUG
 void nir_validate_shader(nir_shader *shader);
diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h
index 0ee7d1a..2ea9af1 100644
--- a/src/compiler/nir/nir_builder.h
+++ b/src/compiler/nir/nir_builder.h
@@ -437,7 +437,7 @@ nir_store_deref_var(nir_builder *build, nir_deref_var *deref,
       nir_intrinsic_instr_create(build->shader, nir_intrinsic_store_var);
    store->num_components = num_components;
    store->const_index[0] = writemask & ((1 << num_components) - 1);
-   store->variables[0] = nir_deref_as_var(nir_copy_deref(store, &deref->deref));
+   store->variables[0] = nir_deref_var_clone(deref, store);
    store->src[0] = nir_src_for_ssa(value);
    nir_builder_instr_insert(build, &store->instr);
 }
@@ -450,8 +450,8 @@ nir_copy_deref_var(nir_builder *build, nir_deref_var *dest, nir_deref_var *src)
 
    nir_intrinsic_instr *copy =
       nir_intrinsic_instr_create(build->shader, nir_intrinsic_copy_var);
-   copy->variables[0] = nir_deref_as_var(nir_copy_deref(copy, &dest->deref));
-   copy->variables[1] = nir_deref_as_var(nir_copy_deref(copy, &src->deref));
+   copy->variables[0] = nir_deref_var_clone(dest, copy);
+   copy->variables[1] = nir_deref_var_clone(src, copy);
    nir_builder_instr_insert(build, &copy->instr);
 }
 
diff --git a/src/compiler/nir/nir_inline_functions.c b/src/compiler/nir/nir_inline_functions.c
index c36748d..b91e7bc 100644
--- a/src/compiler/nir/nir_inline_functions.c
+++ b/src/compiler/nir/nir_inline_functions.c
@@ -49,7 +49,7 @@ convert_deref_to_param_deref(nir_instr *instr, nir_deref_var **deref,
    /* Now we make a new deref by concatenating the deref in the call's
     * parameter with the deref we were given.
     */
-   nir_deref_var *new_deref = nir_deref_as_var(nir_copy_deref(instr, &call_deref->deref));
+   nir_deref_var *new_deref = nir_deref_var_clone(call_deref, instr);
    nir_deref *new_tail = nir_deref_tail(&new_deref->deref);
    new_tail->child = (*deref)->deref.child;
    ralloc_steal(new_tail, new_tail->child);
diff --git a/src/compiler/nir/nir_lower_indirect_derefs.c b/src/compiler/nir/nir_lower_indirect_derefs.c
index 5c97dc8e..09cc9a3 100644
--- a/src/compiler/nir/nir_lower_indirect_derefs.c
+++ b/src/compiler/nir/nir_lower_indirect_derefs.c
@@ -122,8 +122,7 @@ emit_load_store(nir_builder *b, nir_intrinsic_instr *orig_instr,
       nir_intrinsic_instr *load =
          nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_var);
       load->num_components = orig_instr->num_components;
-      load->variables[0] =
-         nir_deref_as_var(nir_copy_deref(load, &deref->deref));
+      load->variables[0] = nir_deref_var_clone(deref, load);
       unsigned bit_size = orig_instr->dest.ssa.bit_size;
       nir_ssa_dest_init(&load->instr, &load->dest,
                         load->num_components, bit_size, NULL);
@@ -135,8 +134,7 @@ emit_load_store(nir_builder *b, nir_intrinsic_instr *orig_instr,
          nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_var);
       store->num_components = orig_instr->num_components;
       nir_intrinsic_set_write_mask(store, nir_intrinsic_write_mask(orig_instr));
-      store->variables[0] =
-         nir_deref_as_var(nir_copy_deref(store, &deref->deref));
+      store->variables[0] = nir_deref_var_clone(deref, store);
       store->src[0] = nir_src_for_ssa(src);
       nir_builder_instr_insert(b, &store->instr);
    }
diff --git a/src/compiler/nir/nir_lower_tex.c b/src/compiler/nir/nir_lower_tex.c
index 3e3ac8c..66e2317 100644
--- a/src/compiler/nir/nir_lower_tex.c
+++ b/src/compiler/nir/nir_lower_tex.c
@@ -163,11 +163,9 @@ get_texture_size(nir_builder *b, nir_tex_instr *tex)
    txs->is_shadow = tex->is_shadow;
    txs->is_new_style_shadow = tex->is_new_style_shadow;
    txs->texture_index = tex->texture_index;
-   txs->texture = (nir_deref_var *)
-      nir_copy_deref(txs, &tex->texture->deref);
+   txs->texture = nir_deref_var_clone(tex->texture, txs);
    txs->sampler_index = tex->sampler_index;
-   txs->sampler = (nir_deref_var *)
-      nir_copy_deref(txs, &tex->sampler->deref);
+   txs->sampler = nir_deref_var_clone(tex->sampler, txs);
    txs->dest_type = nir_type_int;
 
    /* only single src, the lod: */
@@ -221,11 +219,9 @@ sample_plane(nir_builder *b, nir_tex_instr *tex, int plane)
    plane_tex->coord_components = 2;
 
    plane_tex->texture_index = tex->texture_index;
-   plane_tex->texture = (nir_deref_var *)
-      nir_copy_deref(plane_tex, &tex->texture->deref);
+   plane_tex->texture = nir_deref_var_clone(tex->texture, plane_tex);
    plane_tex->sampler_index = tex->sampler_index;
-   plane_tex->sampler = (nir_deref_var *)
-      nir_copy_deref(plane_tex, &tex->sampler->deref);
+   plane_tex->sampler = nir_deref_var_clone(tex->sampler, plane_tex);
 
    nir_ssa_dest_init(&plane_tex->instr, &plane_tex->dest, 4, 32, NULL);
 
@@ -325,10 +321,8 @@ replace_gradient_with_lod(nir_builder *b, nir_ssa_def *lod, nir_tex_instr *tex)
    txl->is_shadow = tex->is_shadow;
    txl->is_new_style_shadow = tex->is_new_style_shadow;
    txl->sampler_index = tex->sampler_index;
-   txl->texture = (nir_deref_var *)
-      nir_copy_deref(txl, &tex->texture->deref);
-   txl->sampler = (nir_deref_var *)
-      nir_copy_deref(txl, &tex->sampler->deref);
+   txl->texture = nir_deref_var_clone(tex->texture, txl);
+   txl->sampler = nir_deref_var_clone(tex->sampler, txl);
    txl->coord_components = tex->coord_components;
 
    nir_ssa_dest_init(&txl->instr, &txl->dest, 4, 32, NULL);
diff --git a/src/compiler/nir/nir_lower_var_copies.c b/src/compiler/nir/nir_lower_var_copies.c
index b7e9989..b12d953 100644
--- a/src/compiler/nir/nir_lower_var_copies.c
+++ b/src/compiler/nir/nir_lower_var_copies.c
@@ -121,7 +121,7 @@ emit_copy_load_store(nir_intrinsic_instr *copy_instr,
       nir_intrinsic_instr *load =
          nir_intrinsic_instr_create(mem_ctx, nir_intrinsic_load_var);
       load->num_components = num_components;
-      load->variables[0] = nir_deref_as_var(nir_copy_deref(load, &src_head->deref));
+      load->variables[0] = nir_deref_var_clone(src_head, load);
       nir_ssa_dest_init(&load->instr, &load->dest, num_components, bit_size,
                         NULL);
 
@@ -131,7 +131,7 @@ emit_copy_load_store(nir_intrinsic_instr *copy_instr,
          nir_intrinsic_instr_create(mem_ctx, nir_intrinsic_store_var);
       store->num_components = num_components;
       nir_intrinsic_set_write_mask(store, (1 << num_components) - 1);
-      store->variables[0] = nir_deref_as_var(nir_copy_deref(store, &dest_head->deref));
+      store->variables[0] = nir_deref_var_clone(dest_head, store);
 
       store->src[0].is_ssa = true;
       store->src[0].ssa = &load->dest.ssa;
diff --git a/src/compiler/nir/nir_split_var_copies.c b/src/compiler/nir/nir_split_var_copies.c
index 63a7611..cfebb0b 100644
--- a/src/compiler/nir/nir_split_var_copies.c
+++ b/src/compiler/nir/nir_split_var_copies.c
@@ -82,7 +82,7 @@ struct split_var_copies_state {
  */
 static void
 split_var_copy_instr(nir_intrinsic_instr *old_copy,
-                     nir_deref *dest_head, nir_deref *src_head,
+                     nir_deref_var *dest_head, nir_deref_var *src_head,
                      nir_deref *dest_tail, nir_deref *src_tail,
                      struct split_var_copies_state *state)
 {
@@ -182,11 +182,8 @@ split_var_copy_instr(nir_intrinsic_instr *old_copy,
           * belongs to the copy instruction and b) the deref chains may
           * have some of the same links due to the way we constructed them
           */
-         nir_deref *src = nir_copy_deref(new_copy, src_head);
-         nir_deref *dest = nir_copy_deref(new_copy, dest_head);
-
-         new_copy->variables[0] = nir_deref_as_var(dest);
-         new_copy->variables[1] = nir_deref_as_var(src);
+         new_copy->variables[0] = nir_deref_var_clone(dest_head, new_copy);
+         new_copy->variables[1] = nir_deref_var_clone(src_head, new_copy);
 
          /* Emit the copy instruction after the old instruction.  We'll
           * remove the old one later.
@@ -216,10 +213,10 @@ split_var_copies_block(nir_block *block, struct split_var_copies_state *state)
       if (intrinsic->intrinsic != nir_intrinsic_copy_var)
          continue;
 
-      nir_deref *dest_head = &intrinsic->variables[0]->deref;
-      nir_deref *src_head = &intrinsic->variables[1]->deref;
-      nir_deref *dest_tail = nir_deref_tail(dest_head);
-      nir_deref *src_tail = nir_deref_tail(src_head);
+      nir_deref_var *dest_head = intrinsic->variables[0];
+      nir_deref_var *src_head = intrinsic->variables[1];
+      nir_deref *dest_tail = nir_deref_tail(&dest_head->deref);
+      nir_deref *src_tail = nir_deref_tail(&src_head->deref);
 
       switch (glsl_get_base_type(src_tail->type)) {
       case GLSL_TYPE_ARRAY:
diff --git a/src/compiler/spirv/spirv_to_nir.c b/src/compiler/spirv/spirv_to_nir.c
index 57d0886..739fa71 100644
--- a/src/compiler/spirv/spirv_to_nir.c
+++ b/src/compiler/spirv/spirv_to_nir.c
@@ -1206,7 +1206,7 @@ vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
       struct vtn_value *arg = vtn_untyped_value(b, arg_id);
       if (arg->value_type == vtn_value_type_access_chain) {
          nir_deref_var *d = vtn_access_chain_to_deref(b, arg->access_chain);
-         call->params[i] = nir_deref_as_var(nir_copy_deref(call, &d->deref));
+         call->params[i] = nir_deref_var_clone(d, call);
       } else {
          struct vtn_ssa_value *arg_ssa = vtn_ssa_value(b, arg_id);
 
@@ -1542,15 +1542,15 @@ vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
    }
 
    nir_deref_var *sampler = vtn_access_chain_to_deref(b, sampled.sampler);
-   nir_deref *texture;
+   nir_deref_var *texture;
    if (sampled.image) {
       nir_deref_var *image = vtn_access_chain_to_deref(b, sampled.image);
-      texture = &image->deref;
+      texture = image;
    } else {
-      texture = &sampler->deref;
+      texture = sampler;
    }
 
-   instr->texture = nir_deref_as_var(nir_copy_deref(instr, texture));
+   instr->texture = nir_deref_var_clone(texture, instr);
 
    switch (instr->op) {
    case nir_texop_tex:
@@ -1558,7 +1558,7 @@ vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
    case nir_texop_txl:
    case nir_texop_txd:
       /* These operations require a sampler */
-      instr->sampler = nir_deref_as_var(nir_copy_deref(instr, &sampler->deref));
+      instr->sampler = nir_deref_var_clone(sampler, instr);
       break;
    case nir_texop_txf:
    case nir_texop_txf_ms:
@@ -1599,8 +1599,7 @@ vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
          instrs[i]->is_new_style_shadow = instr->is_new_style_shadow;
          instrs[i]->component = instr->component;
          instrs[i]->dest_type = instr->dest_type;
-         instrs[i]->texture =
-            nir_deref_as_var(nir_copy_deref(instrs[i], texture));
+         instrs[i]->texture = nir_deref_var_clone(texture, instrs[i]);
          instrs[i]->sampler = NULL;
 
          memcpy(instrs[i]->src, srcs, instr->num_srcs * sizeof(*instr->src));
@@ -1807,8 +1806,7 @@ vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
    nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
 
    nir_deref_var *image_deref = vtn_access_chain_to_deref(b, image.image);
-   intrin->variables[0] =
-      nir_deref_as_var(nir_copy_deref(&intrin->instr, &image_deref->deref));
+   intrin->variables[0] = nir_deref_var_clone(image_deref, intrin);
 
    /* ImageQuerySize doesn't take any extra parameters */
    if (opcode != SpvOpImageQuerySize) {
@@ -1967,10 +1965,10 @@ vtn_handle_ssbo_or_shared_atomic(struct vtn_builder *b, SpvOp opcode,
 
    if (chain->var->mode == vtn_variable_mode_workgroup) {
       struct vtn_type *type = chain->var->type;
-      nir_deref *deref = &vtn_access_chain_to_deref(b, chain)->deref;
+      nir_deref_var *deref = vtn_access_chain_to_deref(b, chain);
       nir_intrinsic_op op = get_shared_nir_atomic_op(opcode);
       atomic = nir_intrinsic_instr_create(b->nb.shader, op);
-      atomic->variables[0] = nir_deref_as_var(nir_copy_deref(atomic, deref));
+      atomic->variables[0] = nir_deref_var_clone(deref, atomic);
 
       switch (opcode) {
       case SpvOpAtomicLoad:
diff --git a/src/compiler/spirv/vtn_glsl450.c b/src/compiler/spirv/vtn_glsl450.c
index fbc7ce6..a19676f 100644
--- a/src/compiler/spirv/vtn_glsl450.c
+++ b/src/compiler/spirv/vtn_glsl450.c
@@ -667,8 +667,7 @@ handle_glsl450_interpolation(struct vtn_builder *b, enum GLSLstd450 opcode,
    nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->nb.shader, op);
 
    nir_deref_var *deref = vtn_nir_deref(b, w[5]);
-   intrin->variables[0] =
-      nir_deref_as_var(nir_copy_deref(intrin, &deref->deref));
+   intrin->variables[0] = nir_deref_var_clone(deref, intrin);
 
    switch (opcode) {
    case GLSLstd450InterpolateAtCentroid:
diff --git a/src/compiler/spirv/vtn_variables.c b/src/compiler/spirv/vtn_variables.c
index be64dd9..de7c8fe 100644
--- a/src/compiler/spirv/vtn_variables.c
+++ b/src/compiler/spirv/vtn_variables.c
@@ -186,8 +186,7 @@ _vtn_local_load_store(struct vtn_builder *b, bool load, nir_deref_var *deref,
                                    nir_intrinsic_store_var;
 
       nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
-      intrin->variables[0] =
-         nir_deref_as_var(nir_copy_deref(intrin, &deref->deref));
+      intrin->variables[0] = nir_deref_var_clone(deref, intrin);
       intrin->num_components = glsl_get_vector_elements(tail->type);
 
       if (load) {
diff --git a/src/intel/vulkan/anv_nir_lower_input_attachments.c b/src/intel/vulkan/anv_nir_lower_input_attachments.c
index e11b563..1d6f727 100644
--- a/src/intel/vulkan/anv_nir_lower_input_attachments.c
+++ b/src/intel/vulkan/anv_nir_lower_input_attachments.c
@@ -86,8 +86,7 @@ try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load)
    tex->is_array = true;
    tex->is_shadow = false;
 
-   tex->texture =
-      nir_deref_as_var(nir_copy_deref(tex, &load->variables[0]->deref));
+   tex->texture = nir_deref_var_clone(load->variables[0], tex);
    tex->sampler = NULL;
    tex->texture_index = 0;
    tex->sampler_index = 0;
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list