nir/clone: Add support for cloning a single function_impl
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
This commit is contained in:
@@ -1995,6 +1995,7 @@ void nir_print_shader(nir_shader *shader, FILE *fp);
|
|||||||
void nir_print_instr(const nir_instr *instr, 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_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
|
#ifdef DEBUG
|
||||||
void nir_validate_shader(nir_shader *shader);
|
void nir_validate_shader(nir_shader *shader);
|
||||||
|
@@ -32,8 +32,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
/* True if we are cloning an entire shader. */
|
||||||
|
bool global_clone;
|
||||||
|
|
||||||
/* maps orig ptr -> cloned ptr: */
|
/* maps orig ptr -> cloned ptr: */
|
||||||
struct hash_table *ptr_table;
|
struct hash_table *remap_table;
|
||||||
|
|
||||||
/* List of phi sources. */
|
/* List of phi sources. */
|
||||||
struct list_head phi_srcs;
|
struct list_head phi_srcs;
|
||||||
@@ -43,28 +46,32 @@ typedef struct {
|
|||||||
} clone_state;
|
} clone_state;
|
||||||
|
|
||||||
static void
|
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,
|
state->global_clone = global;
|
||||||
_mesa_key_pointer_equal);
|
state->remap_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
|
||||||
|
_mesa_key_pointer_equal);
|
||||||
list_inithead(&state->phi_srcs);
|
list_inithead(&state->phi_srcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
free_clone_state(clone_state *state)
|
free_clone_state(clone_state *state)
|
||||||
{
|
{
|
||||||
_mesa_hash_table_destroy(state->ptr_table, NULL);
|
_mesa_hash_table_destroy(state->remap_table, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *
|
static inline void *
|
||||||
lookup_ptr(clone_state *state, const void *ptr)
|
_lookup_ptr(clone_state *state, const void *ptr, bool global)
|
||||||
{
|
{
|
||||||
struct hash_entry *entry;
|
struct hash_entry *entry;
|
||||||
|
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
return NULL;
|
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!");
|
assert(entry && "Failed to find pointer!");
|
||||||
if (!entry)
|
if (!entry)
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -73,9 +80,33 @@ lookup_ptr(clone_state *state, const void *ptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
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->ptr_table, ptr, nptr);
|
_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)
|
||||||
|
{
|
||||||
|
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 *
|
static nir_constant *
|
||||||
@@ -100,7 +131,7 @@ static nir_variable *
|
|||||||
clone_variable(clone_state *state, const nir_variable *var)
|
clone_variable(clone_state *state, const nir_variable *var)
|
||||||
{
|
{
|
||||||
nir_variable *nvar = rzalloc(state->ns, nir_variable);
|
nir_variable *nvar = rzalloc(state->ns, nir_variable);
|
||||||
store_ptr(state, nvar, var);
|
add_remap(state, nvar, var);
|
||||||
|
|
||||||
nvar->type = var->type;
|
nvar->type = var->type;
|
||||||
nvar->name = ralloc_strdup(nvar, var->name);
|
nvar->name = ralloc_strdup(nvar, var->name);
|
||||||
@@ -137,7 +168,7 @@ static nir_register *
|
|||||||
clone_register(clone_state *state, const nir_register *reg)
|
clone_register(clone_state *state, const nir_register *reg)
|
||||||
{
|
{
|
||||||
nir_register *nreg = rzalloc(state->ns, nir_register);
|
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_components = reg->num_components;
|
||||||
nreg->num_array_elems = reg->num_array_elems;
|
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;
|
nsrc->is_ssa = src->is_ssa;
|
||||||
if (src->is_ssa) {
|
if (src->is_ssa) {
|
||||||
nsrc->ssa = lookup_ptr(state, src->ssa);
|
nsrc->ssa = remap_local(state, src->ssa);
|
||||||
} else {
|
} else {
|
||||||
nsrc->reg.reg = lookup_ptr(state, src->reg.reg);
|
nsrc->reg.reg = remap_reg(state, src->reg.reg);
|
||||||
if (src->reg.indirect) {
|
if (src->reg.indirect) {
|
||||||
nsrc->reg.indirect = ralloc(ninstr_or_if, nir_src);
|
nsrc->reg.indirect = ralloc(ninstr_or_if, nir_src);
|
||||||
__clone_src(state, ninstr_or_if, nsrc->reg.indirect, src->reg.indirect);
|
__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;
|
ndst->is_ssa = dst->is_ssa;
|
||||||
if (dst->is_ssa) {
|
if (dst->is_ssa) {
|
||||||
nir_ssa_dest_init(ninstr, ndst, dst->ssa.num_components, dst->ssa.name);
|
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 {
|
} else {
|
||||||
ndst->reg.reg = lookup_ptr(state, dst->reg.reg);
|
ndst->reg.reg = remap_reg(state, dst->reg.reg);
|
||||||
if (dst->reg.indirect) {
|
if (dst->reg.indirect) {
|
||||||
ndst->reg.indirect = ralloc(ninstr, nir_src);
|
ndst->reg.indirect = ralloc(ninstr, nir_src);
|
||||||
__clone_src(state, ninstr, ndst->reg.indirect, dst->reg.indirect);
|
__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,
|
clone_deref_var(clone_state *state, const nir_deref_var *dvar,
|
||||||
nir_instr *ninstr)
|
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);
|
nir_deref_var *ndvar = nir_deref_var_create(ninstr, nvar);
|
||||||
|
|
||||||
if (dvar->deref.child)
|
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));
|
memcpy(&nlc->value, &lc->value, sizeof(nlc->value));
|
||||||
|
|
||||||
store_ptr(state, &nlc->def, &lc->def);
|
add_remap(state, &nlc->def, &lc->def);
|
||||||
|
|
||||||
return nlc;
|
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 *nsa =
|
||||||
nir_ssa_undef_instr_create(state->ns, sa->def.num_components);
|
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;
|
return nsa;
|
||||||
}
|
}
|
||||||
@@ -424,7 +455,7 @@ clone_jump(clone_state *state, const nir_jump_instr *jmp)
|
|||||||
static nir_call_instr *
|
static nir_call_instr *
|
||||||
clone_call(clone_state *state, const nir_call_instr *call)
|
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);
|
nir_call_instr *ncall = nir_call_instr_create(state->ns, ncallee);
|
||||||
|
|
||||||
for (unsigned i = 0; i < ncall->num_params; i++)
|
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));
|
assert(exec_list_is_empty(&nblk->instr_list));
|
||||||
|
|
||||||
/* We need this for phi sources */
|
/* We need this for phi sources */
|
||||||
store_ptr(state, nblk, blk);
|
add_remap(state, nblk, blk);
|
||||||
|
|
||||||
nir_foreach_instr(blk, instr) {
|
nir_foreach_instr(blk, instr) {
|
||||||
if (instr->type == nir_instr_type_phi) {
|
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 *
|
static nir_function_impl *
|
||||||
clone_function_impl(clone_state *state, const nir_function_impl *fi,
|
clone_function_impl(clone_state *state, const nir_function_impl *fi)
|
||||||
nir_function *nfxn)
|
|
||||||
{
|
{
|
||||||
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_var_list(state, &nfi->locals, &fi->locals);
|
||||||
clone_reg_list(state, &nfi->registers, &fi->registers);
|
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->num_params = fi->num_params;
|
||||||
nfi->params = ralloc_array(state->ns, nir_variable *, fi->num_params);
|
nfi->params = ralloc_array(state->ns, nir_variable *, fi->num_params);
|
||||||
for (unsigned i = 0; i < fi->num_params; i++) {
|
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));
|
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.
|
* 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) {
|
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);
|
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 */
|
/* Remove from this list and place in the uses of the SSA def */
|
||||||
list_del(&src->src.use_link);
|
list_del(&src->src.use_link);
|
||||||
@@ -592,6 +623,22 @@ clone_function_impl(clone_state *state, const nir_function_impl *fi,
|
|||||||
return nfi;
|
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 *
|
static nir_function *
|
||||||
clone_function(clone_state *state, const nir_function *fxn, nir_shader *ns)
|
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);
|
nir_function *nfxn = nir_function_create(ns, fxn->name);
|
||||||
|
|
||||||
/* Needed for call instructions */
|
/* Needed for call instructions */
|
||||||
store_ptr(state, nfxn, fxn);
|
add_remap(state, nfxn, fxn);
|
||||||
|
|
||||||
nfxn->num_params = fxn->num_params;
|
nfxn->num_params = fxn->num_params;
|
||||||
nfxn->params = ralloc_array(state->ns, nir_parameter, 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)
|
nir_shader_clone(void *mem_ctx, const nir_shader *s)
|
||||||
{
|
{
|
||||||
clone_state state;
|
clone_state state;
|
||||||
init_clone_state(&state);
|
init_clone_state(&state, true);
|
||||||
|
|
||||||
nir_shader *ns = nir_shader_create(mem_ctx, s->stage, s->options);
|
nir_shader *ns = nir_shader_create(mem_ctx, s->stage, s->options);
|
||||||
state.ns = ns;
|
state.ns = ns;
|
||||||
@@ -641,8 +688,9 @@ nir_shader_clone(void *mem_ctx, const nir_shader *s)
|
|||||||
* will have in the list.
|
* will have in the list.
|
||||||
*/
|
*/
|
||||||
nir_foreach_function(s, fxn) {
|
nir_foreach_function(s, fxn) {
|
||||||
nir_function *nfxn = lookup_ptr(&state, fxn);
|
nir_function *nfxn = remap_global(&state, fxn);
|
||||||
clone_function_impl(&state, fxn->impl, nfxn);
|
nfxn->impl = clone_function_impl(&state, fxn->impl);
|
||||||
|
nfxn->impl->function = nfxn;
|
||||||
}
|
}
|
||||||
|
|
||||||
clone_reg_list(&state, &ns->registers, &s->registers);
|
clone_reg_list(&state, &ns->registers, &s->registers);
|
||||||
|
Reference in New Issue
Block a user