From 0bea0e7141a7698118bfd465fdb4adf8e0b21bc8 Mon Sep 17 00:00:00 2001 From: Rob Clark Date: Tue, 22 Mar 2016 15:02:42 -0400 Subject: [PATCH] nir: fix dangling ssadef->name ptrs In many places, the convention is to pass an existing ssadef name ptr when construction/initializing a new nir_ssa_def. But that goes badly (as noticed by garbage in nir_print output) when the original string gets freed. Just use ralloc_strdup() instead, and add ralloc_free() in the two places that would care (not that the strings wouldn't eventually get freed anyways). Also fixup the nir_search code which was directly setting ssadef->name to use the parent instruction as memctx. Signed-off-by: Rob Clark Reviewed-by: Jason Ekstrand --- src/compiler/nir/nir.c | 4 +++- src/compiler/nir/nir_search.c | 6 +++--- src/compiler/nir/nir_to_ssa.c | 2 ++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/compiler/nir/nir.c b/src/compiler/nir/nir.c index b11498132a6..20f1a182b77 100644 --- a/src/compiler/nir/nir.c +++ b/src/compiler/nir/nir.c @@ -1317,12 +1317,13 @@ nir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest, nir_dest new_dest) src_add_all_uses(dest->reg.indirect, instr, NULL); } +/* note: does *not* take ownership of 'name' */ void nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def, unsigned num_components, unsigned bit_size, const char *name) { - def->name = name; + def->name = ralloc_strdup(instr, name); def->parent_instr = instr; list_inithead(&def->uses); list_inithead(&def->if_uses); @@ -1339,6 +1340,7 @@ nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def, } } +/* note: does *not* take ownership of 'name' */ void nir_ssa_dest_init(nir_instr *instr, nir_dest *dest, unsigned num_components, unsigned bit_size, diff --git a/src/compiler/nir/nir_search.c b/src/compiler/nir/nir_search.c index 110ab5e2362..6e630631453 100644 --- a/src/compiler/nir/nir_search.c +++ b/src/compiler/nir/nir_search.c @@ -469,7 +469,7 @@ construct_value(const nir_search_value *value, switch (c->type) { case nir_type_float: - load->def.name = ralloc_asprintf(mem_ctx, "%f", c->data.d); + load->def.name = ralloc_asprintf(load, "%f", c->data.d); switch (bitsize->dest_size) { case 32: load->value.f32[0] = c->data.d; @@ -483,7 +483,7 @@ construct_value(const nir_search_value *value, break; case nir_type_int: - load->def.name = ralloc_asprintf(mem_ctx, "%ld", c->data.i); + load->def.name = ralloc_asprintf(load, "%ld", c->data.i); switch (bitsize->dest_size) { case 32: load->value.i32[0] = c->data.i; @@ -497,7 +497,7 @@ construct_value(const nir_search_value *value, break; case nir_type_uint: - load->def.name = ralloc_asprintf(mem_ctx, "%lu", c->data.u); + load->def.name = ralloc_asprintf(load, "%lu", c->data.u); switch (bitsize->dest_size) { case 32: load->value.u32[0] = c->data.u; diff --git a/src/compiler/nir/nir_to_ssa.c b/src/compiler/nir/nir_to_ssa.c index 06406071166..d588d7d2df3 100644 --- a/src/compiler/nir/nir_to_ssa.c +++ b/src/compiler/nir/nir_to_ssa.c @@ -221,6 +221,7 @@ rewrite_def_forwards(nir_dest *dest, void *_state) list_del(&dest->reg.def_link); nir_ssa_dest_init(state->parent_instr, dest, reg->num_components, reg->bit_size, name); + ralloc_free(name); /* push our SSA destination on the stack */ state->states[index].index++; @@ -274,6 +275,7 @@ rewrite_alu_instr_forward(nir_alu_instr *instr, rewrite_state *state) list_del(&instr->dest.dest.reg.def_link); nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, reg->bit_size, name); + ralloc_free(name); if (nir_op_infos[instr->op].output_size == 0) { /* -- 2.30.2