meson: inline `inc_common`
[mesa.git] / src / compiler / nir / nir_clone.c
index 9594eb8044d430d8cc2f5fbff2baaee1582301ad..d5ae7f177931148ec0997060b23e250ed075530d 100644 (file)
@@ -115,7 +115,7 @@ remap_global(clone_state *state, const void *ptr)
 static nir_register *
 remap_reg(clone_state *state, const nir_register *reg)
 {
-   return _lookup_ptr(state, reg, reg->is_global);
+   return _lookup_ptr(state, reg, false);
 }
 
 static nir_variable *
@@ -151,9 +151,11 @@ nir_variable_clone(const nir_variable *var, nir_shader *shader)
    nvar->name = ralloc_strdup(nvar, var->name);
    nvar->data = var->data;
    nvar->num_state_slots = var->num_state_slots;
-   nvar->state_slots = ralloc_array(nvar, nir_state_slot, var->num_state_slots);
-   memcpy(nvar->state_slots, var->state_slots,
-          var->num_state_slots * sizeof(nir_state_slot));
+   if (var->num_state_slots) {
+      nvar->state_slots = ralloc_array(nvar, nir_state_slot, var->num_state_slots);
+      memcpy(nvar->state_slots, var->state_slots,
+             var->num_state_slots * sizeof(nir_state_slot));
+   }
    if (var->constant_initializer) {
       nvar->constant_initializer =
          nir_constant_clone(var->constant_initializer, nvar);
@@ -206,8 +208,6 @@ clone_register(clone_state *state, const nir_register *reg)
    nreg->num_array_elems = reg->num_array_elems;
    nreg->index = reg->index;
    nreg->name = ralloc_strdup(nreg, reg->name);
-   nreg->is_global = reg->is_global;
-   nreg->is_packed = reg->is_packed;
 
    /* reconstructing uses/defs/if_uses handled by nir_instr_insert() */
    list_inithead(&nreg->uses);
@@ -265,11 +265,33 @@ __clone_dst(clone_state *state, nir_instr *ninstr,
    }
 }
 
+nir_alu_instr *
+nir_alu_instr_clone(nir_shader *shader, const nir_alu_instr *orig)
+{
+   nir_alu_instr *clone = nir_alu_instr_create(shader, orig->op);
+
+   clone->exact = orig->exact;
+
+   for (unsigned i = 0; i < nir_op_infos[orig->op].num_inputs; i++)
+      nir_alu_src_copy(&clone->src[i], &orig->src[i], clone);
+
+   nir_ssa_dest_init(&clone->instr,
+                     &clone->dest.dest,
+                     orig->dest.dest.ssa.num_components,
+                     orig->dest.dest.ssa.bit_size,
+                     orig->dest.dest.ssa.name);
+   clone->dest.write_mask = orig->dest.write_mask;
+
+   return clone;
+}
+
 static nir_alu_instr *
 clone_alu(clone_state *state, const nir_alu_instr *alu)
 {
    nir_alu_instr *nalu = nir_alu_instr_create(state->ns, alu->op);
    nalu->exact = alu->exact;
+   nalu->no_signed_wrap = alu->no_signed_wrap;
+   nalu->no_unsigned_wrap = alu->no_unsigned_wrap;
 
    __clone_dst(state, &nalu->instr, &nalu->dest.dest, &alu->dest.dest);
    nalu->dest.saturate = alu->dest.saturate;
@@ -357,7 +379,7 @@ clone_load_const(clone_state *state, const nir_load_const_instr *lc)
       nir_load_const_instr_create(state->ns, lc->def.num_components,
                                   lc->def.bit_size);
 
-   memcpy(&nlc->value, &lc->value, sizeof(nlc->value));
+   memcpy(&nlc->value, &lc->value, sizeof(*nlc->value) * lc->def.num_components);
 
    add_remap(state, &nlc->def, &lc->def);
 
@@ -397,9 +419,11 @@ clone_tex(clone_state *state, const nir_tex_instr *tex)
    memcpy(ntex->tg4_offsets, tex->tg4_offsets, sizeof(tex->tg4_offsets));
 
    ntex->texture_index = tex->texture_index;
-   ntex->texture_array_size = tex->texture_array_size;
    ntex->sampler_index = tex->sampler_index;
 
+   ntex->texture_non_uniform = tex->texture_non_uniform;
+   ntex->sampler_non_uniform = tex->sampler_non_uniform;
+
    return ntex;
 }
 
@@ -537,6 +561,7 @@ static nir_if *
 clone_if(clone_state *state, struct exec_list *cf_list, const nir_if *i)
 {
    nir_if *ni = nir_if_create(state->ns);
+   ni->control = i->control;
 
    __clone_src(state, ni, &ni->condition, &i->condition);
 
@@ -552,6 +577,7 @@ static nir_loop *
 clone_loop(clone_state *state, struct exec_list *cf_list, const nir_loop *loop)
 {
    nir_loop *nloop = nir_loop_create(state->ns);
+   nloop->control = loop->control;
    nloop->partially_unrolled = loop->partially_unrolled;
 
    nir_cf_node_insert_end(cf_list, &nloop->cf_node);
@@ -605,7 +631,7 @@ fixup_phi_srcs(clone_state *state)
          list_addtail(&src->src.use_link, &src->src.reg.reg->uses);
       }
    }
-   assert(list_empty(&state->phi_srcs));
+   assert(list_is_empty(&state->phi_srcs));
 }
 
 void
@@ -645,7 +671,7 @@ clone_function_impl(clone_state *state, const nir_function_impl *fi)
    clone_reg_list(state, &nfi->registers, &fi->registers);
    nfi->reg_alloc = fi->reg_alloc;
 
-   assert(list_empty(&state->phi_srcs));
+   assert(list_is_empty(&state->phi_srcs));
 
    clone_cf_list(state, &nfi->body, &fi->body);
 
@@ -682,8 +708,10 @@ clone_function(clone_state *state, const nir_function *fxn, nir_shader *ns)
    add_remap(state, nfxn, fxn);
 
    nfxn->num_params = fxn->num_params;
-   nfxn->params = ralloc_array(state->ns, nir_parameter, fxn->num_params);
-   memcpy(nfxn->params, fxn->params, sizeof(nir_parameter) * fxn->num_params);
+   if (fxn->num_params) {
+           nfxn->params = ralloc_array(state->ns, nir_parameter, fxn->num_params);
+           memcpy(nfxn->params, fxn->params, sizeof(nir_parameter) * fxn->num_params);
+   }
    nfxn->is_entrypoint = fxn->is_entrypoint;
 
    /* At first glance, it looks like we should clone the function_impl here.
@@ -726,9 +754,6 @@ nir_shader_clone(void *mem_ctx, const nir_shader *s)
       nfxn->impl->function = nfxn;
    }
 
-   clone_reg_list(&state, &ns->registers, &s->registers);
-   ns->reg_alloc = s->reg_alloc;
-
    ns->info = s->info;
    ns->info.name = ralloc_strdup(ns, ns->info.name);
    if (ns->info.label)
@@ -738,6 +763,7 @@ nir_shader_clone(void *mem_ctx, const nir_shader *s)
    ns->num_uniforms = s->num_uniforms;
    ns->num_outputs = s->num_outputs;
    ns->num_shared = s->num_shared;
+   ns->scratch_size = s->scratch_size;
 
    ns->constant_data_size = s->constant_data_size;
    if (s->constant_data_size > 0) {
@@ -749,3 +775,42 @@ nir_shader_clone(void *mem_ctx, const nir_shader *s)
 
    return ns;
 }
+
+/** Overwrites dst and replaces its contents with src
+ *
+ * Everything ralloc parented to dst and src itself (but not its children)
+ * will be freed.
+ *
+ * This should only be used by test code which needs to swap out shaders with
+ * a cloned or deserialized version.
+ */
+void
+nir_shader_replace(nir_shader *dst, nir_shader *src)
+{
+   /* Delete all of dest's ralloc children */
+   void *dead_ctx = ralloc_context(NULL);
+   ralloc_adopt(dead_ctx, dst);
+   ralloc_free(dead_ctx);
+
+   /* Re-parent all of src's ralloc children to dst */
+   ralloc_adopt(dst, src);
+
+   memcpy(dst, src, sizeof(*dst));
+
+   /* We have to move all the linked lists over separately because we need the
+    * pointers in the list elements to point to the lists in dst and not src.
+    */
+   exec_list_move_nodes_to(&src->uniforms,      &dst->uniforms);
+   exec_list_move_nodes_to(&src->inputs,        &dst->inputs);
+   exec_list_move_nodes_to(&src->outputs,       &dst->outputs);
+   exec_list_move_nodes_to(&src->shared,        &dst->shared);
+   exec_list_move_nodes_to(&src->globals,       &dst->globals);
+   exec_list_move_nodes_to(&src->system_values, &dst->system_values);
+
+   /* Now move the functions over.  This takes a tiny bit more work */
+   exec_list_move_nodes_to(&src->functions, &dst->functions);
+   nir_foreach_function(function, dst)
+      function->shader = dst;
+
+   ralloc_free(src);
+}