nir: Make nir_copy_deref follow the "clone" pattern
authorJason Ekstrand <jason.ekstrand@intel.com>
Sat, 24 Dec 2016 17:42:34 +0000 (09:42 -0800)
committerJason Ekstrand <jason.ekstrand@intel.com>
Fri, 30 Dec 2016 20:38:04 +0000 (12:38 -0800)
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_as_var.

Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
13 files changed:
src/compiler/glsl/glsl_to_nir.cpp
src/compiler/nir/nir.c
src/compiler/nir/nir.h
src/compiler/nir/nir_builder.h
src/compiler/nir/nir_inline_functions.c
src/compiler/nir/nir_lower_indirect_derefs.c
src/compiler/nir/nir_lower_tex.c
src/compiler/nir/nir_lower_var_copies.c
src/compiler/nir/nir_split_var_copies.c
src/compiler/spirv/spirv_to_nir.c
src/compiler/spirv/vtn_glsl450.c
src/compiler/spirv/vtn_variables.c
src/intel/vulkan/anv_nir_lower_input_attachments.c

index 4127638d2afd2d009ca1def8f0aef97b21c0e9a7..4e0a33a9e74d2f407f7da5a97b03b1aa27efe0d8 100644 (file)
@@ -1178,8 +1178,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) {
index 885616e99c386a83e1b491845610ae27cd68bdb2..1f6837a88dd21c9afbc9a2254794b6328847aafa 100644 (file)
@@ -624,18 +624,21 @@ 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)
 {
+   if (deref == NULL)
+      return NULL;
+
    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 +648,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");
    }
index 71983be0344f13c4a3a6859017d81c361f0da04a..b96b0b1f0c60806cd52d98aade750d24c5b6c91f 100644 (file)
@@ -1932,8 +1932,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);
@@ -2241,6 +2239,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);
index 0ee7d1a6f39527ea3a66a90a81e9f25e79170945..2ea9af1e63d37257a2d5819ef3f27cfb8d56ed72 100644 (file)
@@ -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);
 }
 
index c36748d6cf5863e815af775cd023d5e216bb1670..b91e7bc86da8f0192eeb429f15b7e89febde4d01 100644 (file)
@@ -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);
index 5c97dc8e5fe9ab8ad66680d7a122705eb2d296ad..09cc9a310b11cdba60e0f1afc606d7999c01bc27 100644 (file)
@@ -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);
    }
index 3e3ac8c255897b4d5456f1764e4dfb02d65ece9b..66e231762f898879614ef25a025b9c18058c77e8 100644 (file)
@@ -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);
index b7e9989808b47b81ed9fc75c255fd7aa38dae496..b12d953820fa0e814463d8e3291694dced96aff5 100644 (file)
@@ -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;
index 63a7611b914c7c3a278b24a27a2b414181a692e6..cfebb0bb94826127d6c993f0fffd4657db005bb1 100644 (file)
@@ -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:
index 07980aa2019d185f464195e7ca5f357b18d55314..41da0e85c9db7456a9cd1df0de174d1133f280cf 100644 (file)
@@ -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:
index fbc7ce6fd84d0c2701c33ab76b3b8b5421914cfa..a19676fcc2c759635f470c8109e8de9d1f8466d0 100644 (file)
@@ -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:
index be64dd955061d1fa7f3c0d4f9ebd67665005a319..de7c8fe1f765907f599a61a0602af3f35fde5009 100644 (file)
@@ -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) {
index e11b5637cda6a5bea1e222403ef625a584e2af42..1d6f727258a8df4574e095aa632dbe6a7c75cba9 100644 (file)
@@ -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;