*/
#include "nir.h"
-#include "nir_control_flow_private.h"
+#include "nir_control_flow.h"
/* Secret Decoder Ring:
* clone_foo():
/* True if we are cloning an entire shader. */
bool global_clone;
+ /* If true allows the clone operation to fall back to the original pointer
+ * if no clone pointer is found in the remap table. This allows us to
+ * clone a loop body without having to add srcs from outside the loop to
+ * the remap table. This is useful for loop unrolling.
+ */
+ bool allow_remap_fallback;
+
/* maps orig ptr -> cloned ptr: */
struct hash_table *remap_table;
} clone_state;
static void
-init_clone_state(clone_state *state, bool global)
+init_clone_state(clone_state *state, struct hash_table *remap_table,
+ bool global, bool allow_remap_fallback)
{
state->global_clone = global;
- state->remap_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
- _mesa_key_pointer_equal);
+ state->allow_remap_fallback = allow_remap_fallback;
+
+ if (remap_table) {
+ state->remap_table = remap_table;
+ } else {
+ state->remap_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
+ _mesa_key_pointer_equal);
+ }
+
list_inithead(&state->phi_srcs);
}
return (void *)ptr;
entry = _mesa_hash_table_search(state->remap_table, ptr);
- assert(entry && "Failed to find pointer!");
- if (!entry)
- return NULL;
+ if (!entry) {
+ assert(state->allow_remap_fallback);
+ return (void *)ptr;
+ }
return entry->data;
}
return _lookup_ptr(state, var, nir_variable_is_global(var));
}
-static nir_constant *
-clone_constant(clone_state *state, const nir_constant *c, nir_variable *nvar)
+nir_constant *
+nir_constant_clone(const nir_constant *c, nir_variable *nvar)
{
nir_constant *nc = ralloc(nvar, nir_constant);
- nc->value = c->value;
+ memcpy(nc->values, c->values, sizeof(nc->values));
nc->num_elements = c->num_elements;
nc->elements = ralloc_array(nvar, nir_constant *, c->num_elements);
for (unsigned i = 0; i < c->num_elements; i++) {
- nc->elements[i] = clone_constant(state, c->elements[i], nvar);
+ nc->elements[i] = nir_constant_clone(c->elements[i], nvar);
}
return nc;
}
-/* NOTE: for cloning nir_variable's, bypass nir_variable_create to avoid
+/* NOTE: for cloning nir_variables, bypass nir_variable_create to avoid
* having to deal with locals and globals separately:
*/
-static nir_variable *
-clone_variable(clone_state *state, const nir_variable *var)
+nir_variable *
+nir_variable_clone(const nir_variable *var, nir_shader *shader)
{
- nir_variable *nvar = rzalloc(state->ns, nir_variable);
- add_remap(state, nvar, var);
+ nir_variable *nvar = rzalloc(shader, nir_variable);
nvar->type = var->type;
nvar->name = ralloc_strdup(nvar, var->name);
var->num_state_slots * sizeof(nir_state_slot));
if (var->constant_initializer) {
nvar->constant_initializer =
- clone_constant(state, var->constant_initializer, nvar);
+ nir_constant_clone(var->constant_initializer, nvar);
}
nvar->interface_type = var->interface_type;
return nvar;
}
+static nir_variable *
+clone_variable(clone_state *state, const nir_variable *var)
+{
+ nir_variable *nvar = nir_variable_clone(var, state->ns);
+ add_remap(state, nvar, var);
+
+ return nvar;
+}
+
/* clone list of nir_variable: */
static void
clone_var_list(clone_state *state, struct exec_list *dst,
}
}
-/* NOTE: for cloning nir_register's, bypass nir_global/local_reg_create()
+/* NOTE: for cloning nir_registers, bypass nir_global/local_reg_create()
* to avoid having to deal with locals and globals separately:
*/
static nir_register *
add_remap(state, nreg, reg);
nreg->num_components = reg->num_components;
+ nreg->bit_size = reg->bit_size;
nreg->num_array_elems = reg->num_array_elems;
nreg->index = reg->index;
nreg->name = ralloc_strdup(nreg, reg->name);
{
ndst->is_ssa = 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.bit_size, dst->ssa.name);
add_remap(state, &ndst->ssa, &dst->ssa);
} else {
ndst->reg.reg = remap_reg(state, dst->reg.reg);
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;
__clone_dst(state, &nalu->instr, &nalu->dest.dest, &alu->dest.dest);
nalu->dest.saturate = alu->dest.saturate;
clone_load_const(clone_state *state, const nir_load_const_instr *lc)
{
nir_load_const_instr *nlc =
- nir_load_const_instr_create(state->ns, lc->def.num_components);
+ nir_load_const_instr_create(state->ns, lc->def.num_components,
+ lc->def.bit_size);
memcpy(&nlc->value, &lc->value, sizeof(nlc->value));
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);
+ nir_ssa_undef_instr_create(state->ns, sa->def.num_components,
+ sa->def.bit_size);
add_remap(state, &nsa->def, &sa->def);
/* We need this for phi sources */
add_remap(state, nblk, blk);
- nir_foreach_instr(blk, instr) {
+ nir_foreach_instr(instr, blk) {
if (instr->type == nir_instr_type_phi) {
/* Phi instructions are a bit of a special case when cloning because
* we don't want inserting the instruction to automatically handle
}
}
+/* After we've cloned almost everything, we have to walk the list of phi
+ * sources and fix them up. Thanks to loops, the block and SSA value for a
+ * phi source may not be defined when we first encounter it. Instead, we
+ * add it to the phi_srcs list and we fix it up here.
+ */
+static void
+fixup_phi_srcs(clone_state *state)
+{
+ list_for_each_entry_safe(nir_phi_src, src, &state->phi_srcs, src.use_link) {
+ src->pred = remap_local(state, src->pred);
+
+ /* Remove from this list */
+ list_del(&src->src.use_link);
+
+ if (src->src.is_ssa) {
+ src->src.ssa = remap_local(state, src->src.ssa);
+ list_addtail(&src->src.use_link, &src->src.ssa->uses);
+ } else {
+ src->src.reg.reg = remap_reg(state, src->src.reg.reg);
+ list_addtail(&src->src.use_link, &src->src.reg.reg->uses);
+ }
+ }
+ assert(list_empty(&state->phi_srcs));
+}
+
+void
+nir_cf_list_clone(nir_cf_list *dst, nir_cf_list *src, nir_cf_node *parent,
+ struct hash_table *remap_table)
+{
+ exec_list_make_empty(&dst->list);
+ dst->impl = src->impl;
+
+ if (exec_list_is_empty(&src->list))
+ return;
+
+ clone_state state;
+ init_clone_state(&state, remap_table, false, true);
+
+ /* We use the same shader */
+ state.ns = src->impl->function->shader;
+
+ /* The control-flow code assumes that the list of cf_nodes always starts
+ * and ends with a block. We start by adding an empty block.
+ */
+ nir_block *nblk = nir_block_create(state.ns);
+ nblk->cf_node.parent = parent;
+ exec_list_push_tail(&dst->list, &nblk->cf_node.node);
+
+ clone_cf_list(&state, &dst->list, &src->list);
+
+ fixup_phi_srcs(&state);
+}
+
static nir_function_impl *
clone_function_impl(clone_state *state, const nir_function_impl *fi)
{
clone_cf_list(state, &nfi->body, &fi->body);
- /* After we've cloned almost everything, we have to walk the list of phi
- * sources and fix them up. Thanks to loops, the block and SSA value for a
- * phi source may not be defined when we first encounter it. Instead, we
- * 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 = remap_local(state, src->pred);
- assert(src->src.is_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);
- list_addtail(&src->src.use_link, &src->src.ssa->uses);
- }
- assert(list_empty(&state->phi_srcs));
+ fixup_phi_srcs(state);
/* All metadata is invalidated in the cloning process */
nfi->valid_metadata = 0;
nir_function_impl_clone(const nir_function_impl *fi)
{
clone_state state;
- init_clone_state(&state, false);
+ init_clone_state(&state, NULL, false, false);
/* We use the same shader */
state.ns = fi->function->shader;
/* At first glance, it looks like we should clone the function_impl here.
* However, call instructions need to be able to reference at least the
- * function and those will get processed as we clone the function_impl's.
+ * function and those will get processed as we clone the function_impls.
* We stop here and do function_impls as a second pass.
*/
nir_shader_clone(void *mem_ctx, const nir_shader *s)
{
clone_state state;
- init_clone_state(&state, true);
+ init_clone_state(&state, NULL, true, false);
- nir_shader *ns = nir_shader_create(mem_ctx, s->stage, s->options);
+ nir_shader *ns = nir_shader_create(mem_ctx, s->stage, s->options, NULL);
state.ns = ns;
clone_var_list(&state, &ns->uniforms, &s->uniforms);
clone_var_list(&state, &ns->inputs, &s->inputs);
clone_var_list(&state, &ns->outputs, &s->outputs);
+ clone_var_list(&state, &ns->shared, &s->shared);
clone_var_list(&state, &ns->globals, &s->globals);
clone_var_list(&state, &ns->system_values, &s->system_values);
clone_function(&state, fxn, ns);
/* Only after all functions are cloned can we clone the actual function
- * implementations. This is because nir_call_instr's need to reference the
+ * implementations. This is because nir_call_instrs need to reference the
* functions of other functions and we don't know what order the functions
* will have in the list.
*/
- nir_foreach_function(s, fxn) {
+ nir_foreach_function(fxn, s) {
nir_function *nfxn = remap_global(&state, fxn);
nfxn->impl = clone_function_impl(&state, fxn->impl);
nfxn->impl->function = nfxn;
ns->num_inputs = s->num_inputs;
ns->num_uniforms = s->num_uniforms;
ns->num_outputs = s->num_outputs;
+ ns->num_shared = s->num_shared;
free_clone_state(&state);