<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Dec 1, 2016 at 12:46 AM, Iago Toral <span dir="ltr"><<a href="mailto:itoral@igalia.com" target="_blank">itoral@igalia.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Tue, 2016-11-29 at 22:51 -0800, Jason Ekstrand wrote:<br>
> This has bothered me for about as long as NIR has been around.  Why<br>
> do we<br>
> have two different unions for constants?  No good reason other than<br>
> one of<br>
> them is a direct port from GLSL IR.<br>
> ---<br>
>  src/compiler/glsl/glsl_to_<wbr>nir.cpp  | 35 ++++++++++++++++--------<br>
>  src/compiler/nir/nir.c       <wbr>      | 36 +++++++++++--------------<br>
>  src/compiler/nir/nir.h       <wbr>      | 30 +++++++--------------<br>
>  src/compiler/nir/nir_clone.c <wbr>      |  2 +-<br>
>  src/compiler/nir/nir_print.c <wbr>      | 29 ++++++++++++--------<br>
>  src/compiler/spirv/spirv_to_<wbr>nir.c  | 55 ++++++++++++++++----------<br>
> ------------<br>
>  src/compiler/spirv/vtn_<wbr>variables.c |  8 +++---<br>
>  7 files changed, 96 insertions(+), 99 deletions(-)<br>
</span>(...)<br>
<div><div class="h5">> @@ -838,24 +838,20 @@ nir_deref_get_const_<wbr>initializer_load(nir_shader<br>
> *shader, nir_deref_var *deref)<br>
>        nir_load_const_instr_<wbr>create(shader,<br>
> glsl_get_vector_elements(tail-<wbr>>type),<br>
>                               <wbr>     bit_size);<br>
>  <br>
> -   matrix_offset *= load->def.num_components;<br>
> -   for (unsigned i = 0; i < load->def.num_components; i++) {<br>
> -      switch (glsl_get_base_type(tail-><wbr>type)) {<br>
> -      case GLSL_TYPE_FLOAT:<br>
> -      case GLSL_TYPE_INT:<br>
> -      case GLSL_TYPE_UINT:<br>
> -         load->value.u32[i] = constant->value.u[matrix_<wbr>offset + i];<br>
> -         break;<br>
> -      case GLSL_TYPE_DOUBLE:<br>
> -         load->value.f64[i] = constant->value.d[matrix_<wbr>offset + i];<br>
> -         break;<br>
> -      case GLSL_TYPE_BOOL:<br>
> -         load->value.u32[i] = constant->value.b[matrix_<wbr>offset + i] ?<br>
> -                             <wbr>NIR_TRUE : NIR_FALSE;<br>
> -         break;<br>
> -      default:<br>
> -         unreachable("Invalid immediate type");<br>
> -      }<br>
> +   switch (glsl_get_base_type(tail-><wbr>type)) {<br>
> +   case GLSL_TYPE_FLOAT:<br>
> +   case GLSL_TYPE_INT:<br>
> +   case GLSL_TYPE_UINT:<br>
> +      load->value = constant->values[matrix_col];<br>
> +      break;<br>
> +   case GLSL_TYPE_DOUBLE:<br>
> +      load->value = constant->values[matrix_col];<br>
> +      break;<br>
> +   case GLSL_TYPE_BOOL:<br>
> +      load->value = constant->values[matrix_col];<br>
> +      break;<br>
<br>
</div></div>You can merge the double and bool cases in with the rest, it is the<br>
same code now.<span class=""><br></span></blockquote><div><br></div><div>Good Call.  Fixed.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
> +   default:<br>
> +      unreachable("Invalid immediate type");<br>
>     }<br>
>  <br>
</span>(...)<br>
<span class="">>  <br>
> diff --git a/src/compiler/spirv/spirv_to_<wbr>nir.c<br>
> b/src/compiler/spirv/spirv_to_<wbr>nir.c<br>
> index 34968a4..f41df32 100644<br>
> --- a/src/compiler/spirv/spirv_to_<wbr>nir.c<br>
> +++ b/src/compiler/spirv/spirv_to_<wbr>nir.c<br>
</span>(...)<br>
<div><div class="h5">>  <br>
>           uint32_t u[8];<br>
>           for (unsigned i = 0; i < len0; i++)<br>
> -            u[i] = v0->constant->value.u[i];<br>
> +            u[i] = v0->constant->values[0].u32[i]<wbr>;<br>
>           for (unsigned i = 0; i < len1; i++)<br>
> -            u[len0 + i] = v1->constant->value.u[i];<br>
> +            u[len0 + i] = v1->constant->values[0].u32[i]<wbr>;<br>
>  <br>
>           for (unsigned i = 0; i < count - 6; i++) {<br>
>              uint32_t comp = w[i + 6];<br>
>              if (comp == (uint32_t)-1) {<br>
> -               val->constant-<wbr>>value.u[i] = 0xdeadbeef;<br>
> +               val->constant-<wbr>>values[0].u32[i] = 0xdeadbeef;<br>
>              } else {<br>
> -               val->constant-<wbr>>value.u[i] = u[comp];<br>
> +               val->constant-<wbr>>values[0].u32[i] = u[comp];<br>
>              }<br>
>           }<br>
>           break;<br>
> @@ -1137,7 +1133,7 @@ vtn_handle_constant(struct vtn_builder *b,<br>
> SpvOp opcode,<br>
>              } else {<br>
>                 unsigned num_components =<br>
> glsl_get_vector_elements(type)<wbr>;<br>
>                 for (unsigned i = 0; i < num_components; i++)<br>
> -                  val-><wbr>constant->value.u[i] = (*c)->value.u[elem +<br>
> i];<br>
> +                  val-><wbr>constant->values[0].u32[i] = (*c)-<br>
> >values[0].u32[elem + i];<br>
>              }<br>
>           } else {<br>
>              struct vtn_value *insert =<br>
> @@ -1148,7 +1144,7 @@ vtn_handle_constant(struct vtn_builder *b,<br>
> SpvOp opcode,<br>
>              } else {<br>
>                 unsigned num_components =<br>
> glsl_get_vector_elements(type)<wbr>;<br>
>                 for (unsigned i = 0; i < num_components; i++)<br>
> -                  (*c)-><wbr>value.u[elem + i] = insert->constant-<br>
> >value.u[i];<br>
> +                  (*c)-><wbr>values[0].u32[elem + i] = insert->constant-<br>
> >values[0].u32[i];<br>
<br>
</div></div>Is this correct for matrix types? We do:<br>
<br>
elem += w[i] * glsl_get_vector_elements(type)<wbr>;<br>
<br>
in a loop above, so I guess elem can be > 4 and we would end up<br>
indexing out of bounds  here. Don't we need to use elem to index into<br>
'values' instead and maybe tweak the code above to have elem track the<br>
number of columns instead of individual components we need to offset<font color="#888888">?</font></blockquote><div><br></div><div>You're right.  I'm sending a v2.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="HOEnZb"><font color="#888888"><br>
Iago<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
>              }<br>
>           }<br>
>           break;<br>
> @@ -1170,16 +1166,11 @@ vtn_handle_constant(struct vtn_builder *b,<br>
> SpvOp opcode,<br>
>  <br>
>              unsigned j = swap ? 1 - i : i;<br>
>              assert(bit_size == 32);<br>
> -            for (unsigned k = 0; k < num_components; k++)<br>
> -               src[j].u32[k] = c->value.u[k];<br>
> +            src[j] = c->values[0];<br>
>           }<br>
>  <br>
> -         nir_const_value res = nir_eval_const_opcode(op,<br>
> num_components,<br>
> -                             <wbr>                        bit_<wbr>size, src);<br>
> -<br>
> -         for (unsigned k = 0; k < num_components; k++)<br>
> -            val->constant-><wbr>value.u[k] = res.u32[k];<br>
> -<br>
> +         val->constant-><wbr>values[0] =<br>
> +            nir_eval_const_<wbr>opcode(op, num_components, bit_size,<br>
> src);<br>
>           break;<br>
>        } /* default */<br>
>        }<br>
> @@ -1475,7 +1466,7 @@ vtn_handle_texture(struct vtn_builder *b, SpvOp<br>
> opcode,<br>
>     case SpvOpImageGather:<br>
>        /* This has a component as its next source */<br>
>        gather_component =<br>
> -         vtn_value(b, w[idx++], vtn_value_type_constant)-><wbr>constant-<br>
> >value.u[0];<br>
> +         vtn_value(b, w[idx++], vtn_value_type_constant)-><wbr>constant-<br>
> >values[0].u32[0];<br>
>        break;<br>
>  <br>
>     default:<br>
> diff --git a/src/compiler/spirv/vtn_<wbr>variables.c<br>
> b/src/compiler/spirv/vtn_<wbr>variables.c<br>
> index 14366dc..917aa9d 100644<br>
> --- a/src/compiler/spirv/vtn_<wbr>variables.c<br>
> +++ b/src/compiler/spirv/vtn_<wbr>variables.c<br>
> @@ -938,9 +938,9 @@ apply_var_decoration(struct vtn_builder *b,<br>
> nir_variable *nir_var,<br>
>           nir_var->data.read_<wbr>only = true;<br>
>  <br>
>           nir_constant *c = rzalloc(nir_var, nir_constant);<br>
> -         c->value.u[0] = b->shader->info->cs.local_<wbr>size[0];<br>
> -         c->value.u[1] = b->shader->info->cs.local_<wbr>size[1];<br>
> -         c->value.u[2] = b->shader->info->cs.local_<wbr>size[2];<br>
> +         c->values[0].u32[0] = b->shader->info->cs.local_<wbr>size[0];<br>
> +         c->values[0].u32[1] = b->shader->info->cs.local_<wbr>size[1];<br>
> +         c->values[0].u32[2] = b->shader->info->cs.local_<wbr>size[2];<br>
>           nir_var->constant_<wbr>initializer = c;<br>
>           break;<br>
>        }<br>
> @@ -1388,7 +1388,7 @@ vtn_handle_variables(struct vtn_builder *b,<br>
> SpvOp opcode,<br>
>           struct vtn_value *link_val = vtn_untyped_value(b, w[i]);<br>
>           if (link_val->value_type == vtn_value_type_constant) {<br>
>              chain->link[idx].<wbr>mode = vtn_access_mode_literal;<br>
> -            chain->link[idx].<wbr>id = link_val->constant->value.u[0]<wbr>;<br>
> +            chain->link[idx].<wbr>id = link_val->constant-<br>
> >values[0].u32[0];<br>
>           } else {<br>
>              chain->link[idx].<wbr>mode = vtn_access_mode_id;<br>
>              chain->link[idx].<wbr>id = w[i];</div></div></blockquote></div><br></div></div>