summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/compiler/nir/nir.h1
-rw-r--r--src/compiler/nir/nir_clone.c112
2 files changed, 81 insertions, 32 deletions
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 40169adbcf2..34f31eb9859 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -1995,6 +1995,7 @@ void nir_print_shader(nir_shader *shader, FILE *fp);
void nir_print_instr(const nir_instr *instr, FILE *fp);
nir_shader * nir_shader_clone(void *mem_ctx, const nir_shader *s);
+nir_function_impl *nir_function_impl_clone(const nir_function_impl *fi);
#ifdef DEBUG
void nir_validate_shader(nir_shader *shader);
diff --git a/src/compiler/nir/nir_clone.c b/src/compiler/nir/nir_clone.c
index b6bb5fe5668..198ca8b9b12 100644
--- a/src/compiler/nir/nir_clone.c
+++ b/src/compiler/nir/nir_clone.c
@@ -32,8 +32,11 @@
*/
typedef struct {
+ /* True if we are cloning an entire shader. */
+ bool global_clone;
+
/* maps orig ptr -> cloned ptr: */
- struct hash_table *ptr_table;
+ struct hash_table *remap_table;
/* List of phi sources. */
struct list_head phi_srcs;
@@ -43,28 +46,32 @@ typedef struct {
} clone_state;
static void
-init_clone_state(clone_state *state)
+init_clone_state(clone_state *state, bool global)
{
- state->ptr_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
- _mesa_key_pointer_equal);
+ state->global_clone = global;
+ state->remap_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
+ _mesa_key_pointer_equal);
list_inithead(&state->phi_srcs);
}
static void
free_clone_state(clone_state *state)
{
- _mesa_hash_table_destroy(state->ptr_table, NULL);
+ _mesa_hash_table_destroy(state->remap_table, NULL);
}
-static void *
-lookup_ptr(clone_state *state, const void *ptr)
+static inline void *
+_lookup_ptr(clone_state *state, const void *ptr, bool global)
{
struct hash_entry *entry;
if (!ptr)
return NULL;
- entry = _mesa_hash_table_search(state->ptr_table, ptr);
+ if (!state->global_clone && global)
+ return (void *)ptr;
+
+ entry = _mesa_hash_table_search(state->remap_table, ptr);
assert(entry && "Failed to find pointer!");
if (!entry)
return NULL;
@@ -73,9 +80,33 @@ lookup_ptr(clone_state *state, const void *ptr)
}
static void
-store_ptr(clone_state *state, void *nptr, const void *ptr)
+add_remap(clone_state *state, void *nptr, const void *ptr)
+{
+ _mesa_hash_table_insert(state->remap_table, ptr, nptr);
+}
+
+static void *
+remap_local(clone_state *state, const void *ptr)
+{
+ return _lookup_ptr(state, ptr, false);
+}
+
+static void *
+remap_global(clone_state *state, const void *ptr)
+{
+ return _lookup_ptr(state, ptr, true);
+}
+
+static nir_register *
+remap_reg(clone_state *state, const nir_register *reg)
{
- _mesa_hash_table_insert(state->ptr_table, ptr, nptr);
+ return _lookup_ptr(state, reg, reg->is_global);
+}
+
+static nir_variable *
+remap_var(clone_state *state, const nir_variable *var)
+{
+ return _lookup_ptr(state, var, nir_variable_is_global(var));
}
static nir_constant *
@@ -100,7 +131,7 @@ static nir_variable *
clone_variable(clone_state *state, const nir_variable *var)
{
nir_variable *nvar = rzalloc(state->ns, nir_variable);
- store_ptr(state, nvar, var);
+ add_remap(state, nvar, var);
nvar->type = var->type;
nvar->name = ralloc_strdup(nvar, var->name);
@@ -137,7 +168,7 @@ static nir_register *
clone_register(clone_state *state, const nir_register *reg)
{
nir_register *nreg = rzalloc(state->ns, nir_register);
- store_ptr(state, nreg, reg);
+ add_remap(state, nreg, reg);
nreg->num_components = reg->num_components;
nreg->num_array_elems = reg->num_array_elems;
@@ -172,9 +203,9 @@ __clone_src(clone_state *state, void *ninstr_or_if,
{
nsrc->is_ssa = src->is_ssa;
if (src->is_ssa) {
- nsrc->ssa = lookup_ptr(state, src->ssa);
+ nsrc->ssa = remap_local(state, src->ssa);
} else {
- nsrc->reg.reg = lookup_ptr(state, src->reg.reg);
+ nsrc->reg.reg = remap_reg(state, src->reg.reg);
if (src->reg.indirect) {
nsrc->reg.indirect = ralloc(ninstr_or_if, nir_src);
__clone_src(state, ninstr_or_if, nsrc->reg.indirect, src->reg.indirect);
@@ -190,9 +221,9 @@ __clone_dst(clone_state *state, nir_instr *ninstr,
ndst->is_ssa = dst->is_ssa;
if (dst->is_ssa) {
nir_ssa_dest_init(ninstr, ndst, dst->ssa.num_components, dst->ssa.name);
- store_ptr(state, &ndst->ssa, &dst->ssa);
+ add_remap(state, &ndst->ssa, &dst->ssa);
} else {
- ndst->reg.reg = lookup_ptr(state, dst->reg.reg);
+ ndst->reg.reg = remap_reg(state, dst->reg.reg);
if (dst->reg.indirect) {
ndst->reg.indirect = ralloc(ninstr, nir_src);
__clone_src(state, ninstr, ndst->reg.indirect, dst->reg.indirect);
@@ -208,7 +239,7 @@ static nir_deref_var *
clone_deref_var(clone_state *state, const nir_deref_var *dvar,
nir_instr *ninstr)
{
- nir_variable *nvar = lookup_ptr(state, dvar->var);
+ nir_variable *nvar = remap_var(state, dvar->var);
nir_deref_var *ndvar = nir_deref_var_create(ninstr, nvar);
if (dvar->deref.child)
@@ -322,7 +353,7 @@ clone_load_const(clone_state *state, const nir_load_const_instr *lc)
memcpy(&nlc->value, &lc->value, sizeof(nlc->value));
- store_ptr(state, &nlc->def, &lc->def);
+ add_remap(state, &nlc->def, &lc->def);
return nlc;
}
@@ -333,7 +364,7 @@ clone_ssa_undef(clone_state *state, const nir_ssa_undef_instr *sa)
nir_ssa_undef_instr *nsa =
nir_ssa_undef_instr_create(state->ns, sa->def.num_components);
- store_ptr(state, &nsa->def, &sa->def);
+ add_remap(state, &nsa->def, &sa->def);
return nsa;
}
@@ -424,7 +455,7 @@ clone_jump(clone_state *state, const nir_jump_instr *jmp)
static nir_call_instr *
clone_call(clone_state *state, const nir_call_instr *call)
{
- nir_function *ncallee = lookup_ptr(state, call->callee);
+ nir_function *ncallee = remap_global(state, call->callee);
nir_call_instr *ncall = nir_call_instr_create(state->ns, ncallee);
for (unsigned i = 0; i < ncall->num_params; i++)
@@ -477,7 +508,7 @@ clone_block(clone_state *state, struct exec_list *cf_list, const nir_block *blk)
assert(exec_list_is_empty(&nblk->instr_list));
/* We need this for phi sources */
- store_ptr(state, nblk, blk);
+ add_remap(state, nblk, blk);
nir_foreach_instr(blk, instr) {
if (instr->type == nir_instr_type_phi) {
@@ -550,10 +581,9 @@ clone_cf_list(clone_state *state, struct exec_list *dst,
}
static nir_function_impl *
-clone_function_impl(clone_state *state, const nir_function_impl *fi,
- nir_function *nfxn)
+clone_function_impl(clone_state *state, const nir_function_impl *fi)
{
- nir_function_impl *nfi = nir_function_impl_create(nfxn);
+ nir_function_impl *nfi = nir_function_impl_create_bare(state->ns);
clone_var_list(state, &nfi->locals, &fi->locals);
clone_reg_list(state, &nfi->registers, &fi->registers);
@@ -562,9 +592,10 @@ clone_function_impl(clone_state *state, const nir_function_impl *fi,
nfi->num_params = fi->num_params;
nfi->params = ralloc_array(state->ns, nir_variable *, fi->num_params);
for (unsigned i = 0; i < fi->num_params; i++) {
- nfi->params[i] = lookup_ptr(state, fi->params[i]);
+ nfi->params[i] = clone_variable(state, fi->params[i]);
}
- nfi->return_var = lookup_ptr(state, fi->return_var);
+ if (fi->return_var)
+ nfi->return_var = clone_variable(state, fi->return_var);
assert(list_empty(&state->phi_srcs));
@@ -576,9 +607,9 @@ clone_function_impl(clone_state *state, const nir_function_impl *fi,
* add it to the phi_srcs list and we fix it up here.
*/
list_for_each_entry_safe(nir_phi_src, src, &state->phi_srcs, src.use_link) {
- src->pred = lookup_ptr(state, src->pred);
+ src->pred = remap_local(state, src->pred);
assert(src->src.is_ssa);
- src->src.ssa = lookup_ptr(state, src->src.ssa);
+ src->src.ssa = remap_local(state, src->src.ssa);
/* Remove from this list and place in the uses of the SSA def */
list_del(&src->src.use_link);
@@ -592,6 +623,22 @@ clone_function_impl(clone_state *state, const nir_function_impl *fi,
return nfi;
}
+nir_function_impl *
+nir_function_impl_clone(const nir_function_impl *fi)
+{
+ clone_state state;
+ init_clone_state(&state, false);
+
+ /* We use the same shader */
+ state.ns = fi->function->shader;
+
+ nir_function_impl *nfi = clone_function_impl(&state, fi);
+
+ free_clone_state(&state);
+
+ return nfi;
+}
+
static nir_function *
clone_function(clone_state *state, const nir_function *fxn, nir_shader *ns)
{
@@ -599,7 +646,7 @@ clone_function(clone_state *state, const nir_function *fxn, nir_shader *ns)
nir_function *nfxn = nir_function_create(ns, fxn->name);
/* Needed for call instructions */
- store_ptr(state, nfxn, fxn);
+ add_remap(state, nfxn, fxn);
nfxn->num_params = fxn->num_params;
nfxn->params = ralloc_array(state->ns, nir_parameter, fxn->num_params);
@@ -620,7 +667,7 @@ nir_shader *
nir_shader_clone(void *mem_ctx, const nir_shader *s)
{
clone_state state;
- init_clone_state(&state);
+ init_clone_state(&state, true);
nir_shader *ns = nir_shader_create(mem_ctx, s->stage, s->options);
state.ns = ns;
@@ -641,8 +688,9 @@ nir_shader_clone(void *mem_ctx, const nir_shader *s)
* will have in the list.
*/
nir_foreach_function(s, fxn) {
- nir_function *nfxn = lookup_ptr(&state, fxn);
- clone_function_impl(&state, fxn->impl, nfxn);
+ nir_function *nfxn = remap_global(&state, fxn);
+ nfxn->impl = clone_function_impl(&state, fxn->impl);
+ nfxn->impl->function = nfxn;
}
clone_reg_list(&state, &ns->registers, &s->registers);