nir: Fix a bunch of ralloc parenting errors

As of a10d4937, we would really like things associated with an instruction
to be allocated out of that instruction and not out of the shader.  In
particular, you should be passing the instruction that will ultimately be
holding the source into nir_src_copy rather than an arbitrary memory
context.

We also change the prototypes of nir_dest_copy and nir_alu_src/dest_copy to
explicitly take an instruction so we catch this earlier in the future.

Cc: "11.0" <mesa-stable@lists.freedesktop.org>
Reviewed-by: Thomas Helland <thomashelland90@gmail.com>
This commit is contained in:
Jason Ekstrand
2015-09-09 13:18:29 -07:00
parent 794355e771
commit 8c8fc5f833
10 changed files with 32 additions and 31 deletions

View File

@@ -153,7 +153,7 @@ void nir_src_copy(nir_src *dest, const nir_src *src, void *mem_ctx)
} }
} }
void nir_dest_copy(nir_dest *dest, const nir_dest *src, void *mem_ctx) void nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr)
{ {
/* Copying an SSA definition makes no sense whatsoever. */ /* Copying an SSA definition makes no sense whatsoever. */
assert(!src->is_ssa); assert(!src->is_ssa);
@@ -163,17 +163,18 @@ void nir_dest_copy(nir_dest *dest, const nir_dest *src, void *mem_ctx)
dest->reg.base_offset = src->reg.base_offset; dest->reg.base_offset = src->reg.base_offset;
dest->reg.reg = src->reg.reg; dest->reg.reg = src->reg.reg;
if (src->reg.indirect) { if (src->reg.indirect) {
dest->reg.indirect = ralloc(mem_ctx, nir_src); dest->reg.indirect = ralloc(instr, nir_src);
nir_src_copy(dest->reg.indirect, src->reg.indirect, mem_ctx); nir_src_copy(dest->reg.indirect, src->reg.indirect, instr);
} else { } else {
dest->reg.indirect = NULL; dest->reg.indirect = NULL;
} }
} }
void void
nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src, void *mem_ctx) nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
nir_alu_instr *instr)
{ {
nir_src_copy(&dest->src, &src->src, mem_ctx); nir_src_copy(&dest->src, &src->src, &instr->instr);
dest->abs = src->abs; dest->abs = src->abs;
dest->negate = src->negate; dest->negate = src->negate;
for (unsigned i = 0; i < 4; i++) for (unsigned i = 0; i < 4; i++)
@@ -181,9 +182,10 @@ nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src, void *mem_ctx)
} }
void void
nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src, void *mem_ctx) nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
nir_alu_instr *instr)
{ {
nir_dest_copy(&dest->dest, &src->dest, mem_ctx); nir_dest_copy(&dest->dest, &src->dest, &instr->instr);
dest->write_mask = src->write_mask; dest->write_mask = src->write_mask;
dest->saturate = src->saturate; dest->saturate = src->saturate;
} }
@@ -1210,14 +1212,14 @@ nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx)
nir_foreach_use_safe(def, use_src) { nir_foreach_use_safe(def, use_src) {
nir_instr *src_parent_instr = use_src->parent_instr; nir_instr *src_parent_instr = use_src->parent_instr;
list_del(&use_src->use_link); list_del(&use_src->use_link);
nir_src_copy(use_src, &new_src, mem_ctx); nir_src_copy(use_src, &new_src, src_parent_instr);
src_add_all_uses(use_src, src_parent_instr, NULL); src_add_all_uses(use_src, src_parent_instr, NULL);
} }
nir_foreach_if_use_safe(def, use_src) { nir_foreach_if_use_safe(def, use_src) {
nir_if *src_parent_if = use_src->parent_if; nir_if *src_parent_if = use_src->parent_if;
list_del(&use_src->use_link); list_del(&use_src->use_link);
nir_src_copy(use_src, &new_src, mem_ctx); nir_src_copy(use_src, &new_src, src_parent_if);
src_add_all_uses(use_src, NULL, src_parent_if); src_add_all_uses(use_src, NULL, src_parent_if);
} }
} }

View File

@@ -580,8 +580,8 @@ nir_dest_for_reg(nir_register *reg)
return dest; return dest;
} }
void nir_src_copy(nir_src *dest, const nir_src *src, void *mem_ctx); void nir_src_copy(nir_src *dest, const nir_src *src, void *instr_or_if);
void nir_dest_copy(nir_dest *dest, const nir_dest *src, void *mem_ctx); void nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr);
typedef struct { typedef struct {
nir_src src; nir_src src;
@@ -630,10 +630,6 @@ typedef struct {
unsigned write_mask : 4; /* ignored if dest.is_ssa is true */ unsigned write_mask : 4; /* ignored if dest.is_ssa is true */
} nir_alu_dest; } nir_alu_dest;
void nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src, void *mem_ctx);
void nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
void *mem_ctx);
typedef enum { typedef enum {
nir_type_invalid = 0, /* Not a valid type */ nir_type_invalid = 0, /* Not a valid type */
nir_type_float, nir_type_float,
@@ -702,6 +698,11 @@ typedef struct nir_alu_instr {
nir_alu_src src[]; nir_alu_src src[];
} nir_alu_instr; } nir_alu_instr;
void nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
nir_alu_instr *instr);
void nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
nir_alu_instr *instr);
/* is this source channel used? */ /* is this source channel used? */
static inline bool static inline bool
nir_alu_instr_channel_used(nir_alu_instr *instr, unsigned src, unsigned channel) nir_alu_instr_channel_used(nir_alu_instr *instr, unsigned src, unsigned channel)

View File

@@ -556,7 +556,7 @@ emit_copy(nir_parallel_copy_instr *pcopy, nir_src src, nir_src dest_src,
assert(src.reg.reg->num_components >= dest_src.reg.reg->num_components); assert(src.reg.reg->num_components >= dest_src.reg.reg->num_components);
nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov); nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov);
nir_src_copy(&mov->src[0].src, &src, mem_ctx); nir_src_copy(&mov->src[0].src, &src, mov);
mov->dest.dest = nir_dest_for_reg(dest_src.reg.reg); mov->dest.dest = nir_dest_for_reg(dest_src.reg.reg);
mov->dest.write_mask = (1 << dest_src.reg.reg->num_components) - 1; mov->dest.write_mask = (1 << dest_src.reg.reg->num_components) - 1;

View File

@@ -46,11 +46,11 @@ lower_reduction(nir_alu_instr *instr, nir_op chan_op, nir_op merge_op,
for (unsigned i = 0; i < num_components; i++) { for (unsigned i = 0; i < num_components; i++) {
nir_alu_instr *chan = nir_alu_instr_create(mem_ctx, chan_op); nir_alu_instr *chan = nir_alu_instr_create(mem_ctx, chan_op);
nir_alu_ssa_dest_init(chan, 1); nir_alu_ssa_dest_init(chan, 1);
nir_alu_src_copy(&chan->src[0], &instr->src[0], mem_ctx); nir_alu_src_copy(&chan->src[0], &instr->src[0], chan);
chan->src[0].swizzle[0] = chan->src[0].swizzle[i]; chan->src[0].swizzle[0] = chan->src[0].swizzle[i];
if (nir_op_infos[chan_op].num_inputs > 1) { if (nir_op_infos[chan_op].num_inputs > 1) {
assert(nir_op_infos[chan_op].num_inputs == 2); assert(nir_op_infos[chan_op].num_inputs == 2);
nir_alu_src_copy(&chan->src[1], &instr->src[1], mem_ctx); nir_alu_src_copy(&chan->src[1], &instr->src[1], chan);
chan->src[1].swizzle[0] = chan->src[1].swizzle[i]; chan->src[1].swizzle[0] = chan->src[1].swizzle[i];
} }
@@ -153,7 +153,7 @@ lower_alu_instr_scalar(nir_alu_instr *instr, void *mem_ctx)
unsigned src_chan = (nir_op_infos[instr->op].input_sizes[i] == 1 ? unsigned src_chan = (nir_op_infos[instr->op].input_sizes[i] == 1 ?
0 : chan); 0 : chan);
nir_alu_src_copy(&lower->src[i], &instr->src[i], mem_ctx); nir_alu_src_copy(&lower->src[i], &instr->src[i], lower);
for (int j = 0; j < 4; j++) for (int j = 0; j < 4; j++)
lower->src[i].swizzle[j] = instr->src[i].swizzle[src_chan]; lower->src[i].swizzle[j] = instr->src[i].swizzle[src_chan];
} }

View File

@@ -91,7 +91,7 @@ lower_instr(nir_intrinsic_instr *instr, nir_function_impl *impl)
nir_alu_instr *mul = nir_alu_instr_create(mem_ctx, nir_op_imul); nir_alu_instr *mul = nir_alu_instr_create(mem_ctx, nir_op_imul);
nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL); nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL);
mul->dest.write_mask = 0x1; mul->dest.write_mask = 0x1;
nir_src_copy(&mul->src[0].src, &deref_array->indirect, mem_ctx); nir_src_copy(&mul->src[0].src, &deref_array->indirect, mul);
mul->src[1].src.is_ssa = true; mul->src[1].src.is_ssa = true;
mul->src[1].src.ssa = &atomic_counter_size->def; mul->src[1].src.ssa = &atomic_counter_size->def;
nir_instr_insert_before(&instr->instr, &mul->instr); nir_instr_insert_before(&instr->instr, &mul->instr);

View File

@@ -221,7 +221,7 @@ nir_lower_io_block(nir_block *block, void *void_state)
store->const_index[0] = offset; store->const_index[0] = offset;
nir_src_copy(&store->src[0], &intrin->src[0], state->mem_ctx); nir_src_copy(&store->src[0], &intrin->src[0], store);
if (has_indirect) if (has_indirect)
store->src[1] = indirect; store->src[1] = indirect;

View File

@@ -183,8 +183,7 @@ get_deref_reg_src(nir_deref_var *deref, nir_instr *instr,
nir_alu_instr *add = nir_alu_instr_create(state->shader, nir_alu_instr *add = nir_alu_instr_create(state->shader,
nir_op_iadd); nir_op_iadd);
add->src[0].src = *src.reg.indirect; add->src[0].src = *src.reg.indirect;
nir_src_copy(&add->src[1].src, &deref_array->indirect, nir_src_copy(&add->src[1].src, &deref_array->indirect, add);
state->shader);
add->dest.write_mask = 1; add->dest.write_mask = 1;
nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL); nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
nir_instr_insert_before(instr, &add->instr); nir_instr_insert_before(instr, &add->instr);
@@ -225,7 +224,7 @@ lower_locals_to_regs_block(nir_block *block, void *void_state)
nir_src_for_ssa(&mov->dest.dest.ssa), nir_src_for_ssa(&mov->dest.dest.ssa),
state->shader); state->shader);
} else { } else {
nir_dest_copy(&mov->dest.dest, &intrin->dest, state->shader); nir_dest_copy(&mov->dest.dest, &intrin->dest, &mov->instr);
} }
nir_instr_insert_before(&intrin->instr, &mov->instr); nir_instr_insert_before(&intrin->instr, &mov->instr);
@@ -241,7 +240,7 @@ lower_locals_to_regs_block(nir_block *block, void *void_state)
&intrin->instr, state); &intrin->instr, state);
nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov); nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
nir_src_copy(&mov->src[0].src, &intrin->src[0], state->shader); nir_src_copy(&mov->src[0].src, &intrin->src[0], mov);
mov->dest.write_mask = (1 << intrin->num_components) - 1; mov->dest.write_mask = (1 << intrin->num_components) - 1;
mov->dest.dest.is_ssa = false; mov->dest.dest.is_ssa = false;
mov->dest.dest.reg.reg = reg_src.reg.reg; mov->dest.dest.reg.reg = reg_src.reg.reg;

View File

@@ -60,8 +60,8 @@ insert_mov(nir_alu_instr *vec, unsigned start_channel,
assert(src_idx < nir_op_infos[vec->op].num_inputs); assert(src_idx < nir_op_infos[vec->op].num_inputs);
nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov); nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov);
nir_alu_src_copy(&mov->src[0], &vec->src[src_idx], mem_ctx); nir_alu_src_copy(&mov->src[0], &vec->src[src_idx], mov);
nir_alu_dest_copy(&mov->dest, &vec->dest, mem_ctx); nir_alu_dest_copy(&mov->dest, &vec->dest, mov);
mov->dest.write_mask = (1u << start_channel); mov->dest.write_mask = (1u << start_channel);
mov->src[0].swizzle[start_channel] = vec->src[src_idx].swizzle[0]; mov->src[0].swizzle[start_channel] = vec->src[src_idx].swizzle[0];

View File

@@ -216,8 +216,7 @@ nir_opt_peephole_ffma_block(nir_block *block, void *void_state)
for (unsigned j = 0; j < add->dest.dest.ssa.num_components; j++) for (unsigned j = 0; j < add->dest.dest.ssa.num_components; j++)
ffma->src[i].swizzle[j] = mul->src[i].swizzle[swizzle[j]]; ffma->src[i].swizzle[j] = mul->src[i].swizzle[swizzle[j]];
} }
nir_alu_src_copy(&ffma->src[2], &add->src[1 - add_mul_src], nir_alu_src_copy(&ffma->src[2], &add->src[1 - add_mul_src], ffma);
state->mem_ctx);
assert(add->dest.dest.is_ssa); assert(add->dest.dest.is_ssa);

View File

@@ -196,7 +196,7 @@ nir_opt_peephole_select_block(nir_block *block, void *void_state)
nir_phi_instr *phi = nir_instr_as_phi(instr); nir_phi_instr *phi = nir_instr_as_phi(instr);
nir_alu_instr *sel = nir_alu_instr_create(state->mem_ctx, nir_op_bcsel); nir_alu_instr *sel = nir_alu_instr_create(state->mem_ctx, nir_op_bcsel);
nir_src_copy(&sel->src[0].src, &if_stmt->condition, state->mem_ctx); nir_src_copy(&sel->src[0].src, &if_stmt->condition, sel);
/* Splat the condition to all channels */ /* Splat the condition to all channels */
memset(sel->src[0].swizzle, 0, sizeof sel->src[0].swizzle); memset(sel->src[0].swizzle, 0, sizeof sel->src[0].swizzle);
@@ -206,7 +206,7 @@ nir_opt_peephole_select_block(nir_block *block, void *void_state)
assert(src->src.is_ssa); assert(src->src.is_ssa);
unsigned idx = src->pred == then_block ? 1 : 2; unsigned idx = src->pred == then_block ? 1 : 2;
nir_src_copy(&sel->src[idx].src, &src->src, state->mem_ctx); nir_src_copy(&sel->src[idx].src, &src->src, sel);
} }
nir_ssa_dest_init(&sel->instr, &sel->dest.dest, nir_ssa_dest_init(&sel->instr, &sel->dest.dest,