<p dir="ltr"><br>
On Oct 12, 2015 1:26 AM, "Iago Toral" <<a href="mailto:itoral@igalia.com">itoral@igalia.com</a>> wrote:<br>
><br>
> On Fri, 2015-10-09 at 07:09 -0700, Jason Ekstrand wrote:<br>
> > ---<br>
> > src/glsl/nir/glsl_to_nir.cpp | 40 ++++---------------------<br>
> > src/glsl/nir/nir.c | 66 ++++++++++++++++++++++++++++++++++++++++++<br>
> > src/glsl/nir/nir.h | 20 +++++++++++++<br>
> > src/mesa/program/prog_to_nir.c | 19 +++++-------<br>
> > 4 files changed, 99 insertions(+), 46 deletions(-)<br>
> ><br>
> > diff --git a/src/glsl/nir/glsl_to_nir.cpp b/src/glsl/nir/glsl_to_nir.cpp<br>
> > index 874e361..5250080 100644<br>
> > --- a/src/glsl/nir/glsl_to_nir.cpp<br>
> > +++ b/src/glsl/nir/glsl_to_nir.cpp<br>
> > @@ -389,35 +389,10 @@ nir_visitor::visit(ir_variable *ir)<br>
> ><br>
> > var->interface_type = ir->get_interface_type();<br>
> ><br>
> > - switch (var->data.mode) {<br>
> > - case nir_var_local:<br>
> > - exec_list_push_tail(&impl->locals, &var->node);<br>
> > - break;<br>
> > -<br>
> > - case nir_var_global:<br>
> > - exec_list_push_tail(&shader->globals, &var->node);<br>
> > - break;<br>
> > -<br>
> > - case nir_var_shader_in:<br>
> > - exec_list_push_tail(&shader->inputs, &var->node);<br>
> > - break;<br>
> > -<br>
> > - case nir_var_shader_out:<br>
> > - exec_list_push_tail(&shader->outputs, &var->node);<br>
> > - break;<br>
> > -<br>
> > - case nir_var_uniform:<br>
> > - case nir_var_shader_storage:<br>
> > - exec_list_push_tail(&shader->uniforms, &var->node);<br>
> > - break;<br>
> > -<br>
> > - case nir_var_system_value:<br>
> > - exec_list_push_tail(&shader->system_values, &var->node);<br>
> > - break;<br>
> > -<br>
> > - default:<br>
> > - unreachable("not reached");<br>
> > - }<br>
> > + if (var->data.mode == nir_var_local)<br>
> > + nir_function_impl_add_variable(impl, var);<br>
> > + else<br>
> > + nir_shader_add_variable(shader, var);<br>
> ><br>
> > _mesa_hash_table_insert(var_table, ir, var);<br>
> > this->var = var;<br>
> > @@ -2023,13 +1998,10 @@ nir_visitor::visit(ir_constant *ir)<br>
> > * constant initializer and return a dereference.<br>
> > */<br>
> ><br>
> > - nir_variable *var = ralloc(this->shader, nir_variable);<br>
> > - var->name = ralloc_strdup(var, "const_temp");<br>
> > - var->type = ir->type;<br>
> > - var->data.mode = nir_var_local;<br>
> > + nir_variable *var =<br>
> > + nir_local_variable_create(this->impl, ir->type, "const_temp");<br>
> > var->data.read_only = true;<br>
> > var->constant_initializer = constant_copy(ir, var);<br>
> > - exec_list_push_tail(&this->impl->locals, &var->node);<br>
> ><br>
> > this->deref_head = nir_deref_var_create(this->shader, var);<br>
> > this->deref_tail = &this->deref_head->deref;<br>
> > diff --git a/src/glsl/nir/nir.c b/src/glsl/nir/nir.c<br>
> > index e12da80..3645726 100644<br>
> > --- a/src/glsl/nir/nir.c<br>
> > +++ b/src/glsl/nir/nir.c<br>
> > @@ -103,6 +103,72 @@ nir_reg_remove(nir_register *reg)<br>
> > exec_node_remove(®->node);<br>
> > }<br>
> ><br>
> > +void<br>
> > +nir_shader_add_variable(nir_shader *shader, nir_variable *var)<br>
> > +{<br>
> > + switch (var->data.mode) {<br>
> > + case nir_var_local:<br>
> > + assert(!"nir_shader_add_variable cannot be used for local variables");<br>
> > + break;<br>
> > +<br>
> > + case nir_var_global:<br>
> > + exec_list_push_tail(&shader->globals, &var->node);<br>
> > + break;<br>
> > +<br>
> > + case nir_var_shader_in:<br>
> > + exec_list_push_tail(&shader->inputs, &var->node);<br>
> > + break;<br>
> > +<br>
> > + case nir_var_shader_out:<br>
> > + exec_list_push_tail(&shader->outputs, &var->node);<br>
> > + break;<br>
> > +<br>
> > + case nir_var_uniform:<br>
> > + case nir_var_shader_storage:<br>
> > + exec_list_push_tail(&shader->uniforms, &var->node);<br>
> > + break;<br>
> > +<br>
> > + case nir_var_system_value:<br>
> > + exec_list_push_tail(&shader->system_values, &var->node);<br>
> > + break;<br>
> > + }<br>
> > +}<br>
> > +<br>
> > +nir_variable *<br>
> > +nir_variable_create(nir_shader *shader, nir_variable_mode mode,<br>
> > + const struct glsl_type *type, const char *name)<br>
> > +{<br>
> > + nir_variable *var = rzalloc(shader, nir_variable);<br>
> > + var->name = ralloc_strdup(var, name);<br>
> > + var->type = type;<br>
> > + var->data.mode = mode;<br>
> > +<br>
> > + if ((mode == nir_var_shader_in && shader->stage != MESA_SHADER_VERTEX) ||<br>
> > + (mode == nir_var_shader_out && shader->stage != MESA_SHADER_FRAGMENT))<br>
> > + var->data.interpolation = INTERP_QUALIFIER_SMOOTH;<br>
> > +<br>
> > + if (mode == nir_var_shader_in || mode == nir_var_uniform)<br>
> > + var->data.read_only = true;<br>
> > +<br>
> > + nir_shader_add_variable(shader, var);<br>
> > +<br>
> > + return var;<br>
> > +}<br>
> > +<br>
> > +nir_variable *<br>
> > +nir_local_variable_create(nir_function_impl *impl,<br>
> > + const struct glsl_type *type, const char *name)<br>
> > +{<br>
> > + nir_variable *var = rzalloc(impl->overload->function->shader, nir_variable);<br>
> > + var->name = ralloc_strdup(var, name);<br>
> > + var->type = type;<br>
> > + var->data.mode = nir_var_local;<br>
> > +<br>
> > + nir_function_impl_add_variable(impl, var);<br>
> > +<br>
> > + return var;<br>
> > +}<br>
> > +<br>
> > nir_function *<br>
> > nir_function_create(nir_shader *shader, const char *name)<br>
> > {<br>
> > diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h<br>
> > index bde9f49..997e2bd 100644<br>
> > --- a/src/glsl/nir/nir.h<br>
> > +++ b/src/glsl/nir/nir.h<br>
> > @@ -1559,6 +1559,26 @@ nir_register *nir_local_reg_create(nir_function_impl *impl);<br>
> ><br>
> > void nir_reg_remove(nir_register *reg);<br>
> ><br>
> > +/** Adds a variable to the appropreate list in nir_shader */<br>
> > +void nir_shader_add_variable(nir_shader *shader, nir_variable *var);<br>
> > +<br>
> > +static inline void<br>
> > +nir_function_impl_add_variable(nir_function_impl *impl, nir_variable *var)<br>
> > +{<br>
> > + assert(var->data.mode == nir_var_local);<br>
> > + exec_list_push_tail(&impl->locals, &var->node);<br>
> > +}<br>
> > +<br>
> > +/** creates a variable, sets a few defaults, and adds it to the list */<br>
> > +nir_variable *nir_variable_create(nir_shader *shader,<br>
> > + nir_variable_mode mode,<br>
> > + const struct glsl_type *type,<br>
> > + const char *name);<br>
> > +/** creates a local variable and adds it to the list */<br>
> > +nir_variable *nir_local_variable_create(nir_function_impl *impl,<br>
> > + const struct glsl_type *type,<br>
> > + const char *name);<br>
> > +<br>
> > /** creates a function and adds it to the shader's list of functions */<br>
> > nir_function *nir_function_create(nir_shader *shader, const char *name);<br>
> ><br>
> > diff --git a/src/mesa/program/prog_to_nir.c b/src/mesa/program/prog_to_nir.c<br>
> > index d9b1854..ea47f79 100644<br>
> > --- a/src/mesa/program/prog_to_nir.c<br>
> > +++ b/src/mesa/program/prog_to_nir.c<br>
> > @@ -958,11 +958,10 @@ setup_registers_and_variables(struct ptn_compile *c)<br>
> > for (int i = 0; i < num_inputs; i++) {<br>
> > if (!(c->prog->InputsRead & BITFIELD64_BIT(i)))<br>
> > continue;<br>
> > - nir_variable *var = rzalloc(shader, nir_variable);<br>
> > - var->type = glsl_vec4_type();<br>
> > - var->data.read_only = true;<br>
> > - var->data.mode = nir_var_shader_in;<br>
> > - var->name = ralloc_asprintf(var, "in_%d", i);<br>
> > +<br>
> > + nir_variable *var =<br>
> > + nir_variable_create(shader, nir_var_shader_in, glsl_vec4_type(),<br>
> > + ralloc_asprintf(var, "in_%d", i));<br>
><br>
> Isn't this leaking the name? We are going to strdup it again inside<br>
> nir_variable_create.</p>
<p dir="ltr">Sort-of. First, passing var in as the parent before creating var is illegal. I've already fixed that locally by parenting it to the shader. Since it's parented to the shader it's not truly leaked but will get deleted with the shader. It can also get cleaned up even earlier if nir_sweep() is called on the shader.</p>
<p dir="ltr">For what its worth, just ralloc_strdup'ing and dealing with it later is sort-of the standard in NIR for dealing with strings. It's the easiest way to dealing with strings of you don't know where they came from.</p>
<p dir="ltr">> Other than that,<br>
> Reviewed-by: Iago Toral Quiroga <<a href="mailto:itoral@igalia.com">itoral@igalia.com</a>><br>
><br>
> > var->data.location = i;<br>
> > var->data.index = 0;<br>
> ><br>
> > @@ -992,12 +991,9 @@ setup_registers_and_variables(struct ptn_compile *c)<br>
> > nir_ssa_def *f001 = nir_vec4(b, &load_x->dest.ssa, nir_imm_float(b, 0.0),<br>
> > nir_imm_float(b, 0.0), nir_imm_float(b, 1.0));<br>
> ><br>
> > - nir_variable *fullvar = rzalloc(shader, nir_variable);<br>
> > - fullvar->type = glsl_vec4_type();<br>
> > - fullvar->data.mode = nir_var_local;<br>
> > - fullvar->name = "fogcoord_tmp";<br>
> > - exec_list_push_tail(&b->impl->locals, &fullvar->node);<br>
> > -<br>
> > + nir_variable *fullvar =<br>
> > + nir_local_variable_create(b->impl, glsl_vec4_type(),<br>
> > + "fogcoord_tmp");<br>
> > nir_intrinsic_instr *store =<br>
> > nir_intrinsic_instr_create(shader, nir_intrinsic_store_var);<br>
> > store->num_components = 4;<br>
> > @@ -1015,7 +1011,6 @@ setup_registers_and_variables(struct ptn_compile *c)<br>
> > }<br>
> > }<br>
> ><br>
> > - exec_list_push_tail(&shader->inputs, &var->node);<br>
> > c->input_vars[i] = var;<br>
> > }<br>
> ><br>
><br>
><br>
</p>