<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Dec 11, 2017 at 8:19 AM, Juan A. Suarez Romero <span dir="ltr"><<a href="mailto:jasuarez@igalia.com" target="_blank">jasuarez@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 Thu, 2017-12-07 at 08:12 -0800, Jason Ekstrand wrote:<br>
> This autogenerated pass will automatically find and set the type field<br>
> on all vtn_values.  This way we always have the type and can use it for<br>
> validation and other checks.<br>
> ---<br>
>  src/compiler/<a href="http://Makefile.nir.am" rel="noreferrer" target="_blank">Makefile.nir.am</a>             |   4 +<br>
>  src/compiler/nir/meson.build             |  11 ++-<br>
>  src/compiler/spirv/spirv_to_<wbr>nir.c        |   6 +-<br>
>  src/compiler/spirv/vtn_gather_<wbr>types_c.py | 125 ++++++++++++++++++++++++++++++<wbr>+<br>
>  src/compiler/spirv/vtn_<wbr>private.h         |   4 +<br>
>  5 files changed, 148 insertions(+), 2 deletions(-)<br>
>  create mode 100644 src/compiler/spirv/vtn_gather_<wbr>types_c.py<br>
><br>
> diff --git a/src/compiler/<a href="http://Makefile.nir.am" rel="noreferrer" target="_blank">Makefile.nir.am</a> b/src/compiler/<a href="http://Makefile.nir.am" rel="noreferrer" target="_blank">Makefile.nir.am</a><br>
> index 1533ee5..dd38c45 100644<br>
> --- a/src/compiler/<a href="http://Makefile.nir.am" rel="noreferrer" target="_blank">Makefile.nir.am</a><br>
> +++ b/src/compiler/<a href="http://Makefile.nir.am" rel="noreferrer" target="_blank">Makefile.nir.am</a><br>
> @@ -56,6 +56,10 @@ spirv/spirv_info.c: spirv/spirv_info_c.py spirv/spirv.core.grammar.json<br>
>       $(MKDIR_GEN)<br>
>       $(PYTHON_GEN) $(srcdir)/spirv/spirv_info_c.<wbr>py $(srcdir)/spirv/spirv.core.<wbr>grammar.json $@ || ($(RM) $@; false)<br>
><br>
> +spirv/vtn_gather_types.c: spirv/vtn_gather_types_c.py spirv/spirv.core.grammar.json<br>
> +     $(MKDIR_GEN)<br>
> +     $(PYTHON_GEN) $(srcdir)/spirv/vtn_gather_<wbr>types_c.py $(srcdir)/spirv/spirv.core.<wbr>grammar.json $@ || ($(RM) $@; false)<br>
> +<br>
><br>
<br>
</span>spirv/vtn_gather_types.c must be included also in<br>
src/compiler/Makefile.sources, under the SPIRV_GENERATED_FILES.<br></blockquote><div><br></div><div>Thanks!  Clearly, I don't use autotools anymore. :-)<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Otherwise the file won't be generated.<br>
<br>
<br>
        J.A.<br>
<div><div class="h5"><br>
>  noinst_PROGRAMS += spirv2nir<br>
><br>
>  spirv2nir_SOURCES = \<br>
> diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build<br>
> index b61a077..5dd21e6 100644<br>
> --- a/src/compiler/nir/meson.build<br>
> +++ b/src/compiler/nir/meson.build<br>
> @@ -72,6 +72,14 @@ spirv_info_c = custom_target(<br>
>    command : [prog_python2, '@INPUT0@', '@INPUT1@', '@OUTPUT@'],<br>
>  )<br>
><br>
> +vtn_gather_types_c = custom_target(<br>
> +  'vtn_gather_types.c',<br>
> +  input : files('../spirv/vtn_gather_<wbr>types_c.py',<br>
> +                '../spirv/spirv.core.grammar.<wbr>json'),<br>
> +  output : 'vtn_gather_types.c',<br>
> +  command : [prog_python2, '@INPUT0@', '@INPUT1@', '@OUTPUT@'],<br>
> +)<br>
> +<br>
>  files_libnir = files(<br>
>    'nir.c',<br>
>    'nir.h',<br>
> @@ -189,7 +197,8 @@ files_libnir = files(<br>
>  libnir = static_library(<br>
>    'nir',<br>
>    [files_libnir, spirv_info_c, nir_opt_algebraic_c, nir_opcodes_c,<br>
> -   nir_opcodes_h, nir_constant_expressions_c, nir_builder_opcodes_h],<br>
> +   nir_opcodes_h, nir_constant_expressions_c, nir_builder_opcodes_h,<br>
> +   vtn_gather_types_c],<br>
>    include_directories : [inc_common, inc_compiler, include_directories('../spirv'<wbr>)],<br>
>    c_args : [c_vis_args, c_msvc_compat_args, no_override_init_args],<br>
>    link_with : libcompiler,<br>
> diff --git a/src/compiler/spirv/spirv_to_<wbr>nir.c b/src/compiler/spirv/spirv_to_<wbr>nir.c<br>
> index a50b14d..a2426bc 100644<br>
> --- a/src/compiler/spirv/spirv_to_<wbr>nir.c<br>
> +++ b/src/compiler/spirv/spirv_to_<wbr>nir.c<br>
> @@ -1261,7 +1261,6 @@ vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,<br>
>                      const uint32_t *w, unsigned count)<br>
>  {<br>
>     struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_constant);<br>
> -   val->type = vtn_value(b, w[1], vtn_value_type_type)->type;<br>
>     val->constant = rzalloc(b, nir_constant);<br>
>     switch (opcode) {<br>
>     case SpvOpConstantTrue:<br>
> @@ -3268,6 +3267,8 @@ static bool<br>
>  vtn_handle_variable_or_type_<wbr>instruction(struct vtn_builder *b, SpvOp opcode,<br>
>                                          const uint32_t *w, unsigned count)<br>
>  {<br>
> +   vtn_set_instruction_result_<wbr>type(b, opcode, w, count);<br>
> +<br>
>     switch (opcode) {<br>
>     case SpvOpSource:<br>
>     case SpvOpSourceContinued:<br>
> @@ -3658,6 +3659,9 @@ spirv_to_nir(const uint32_t *words, size_t word_count,<br>
>     words = vtn_foreach_instruction(b, words, word_end,<br>
>                                     vtn_handle_variable_or_type_<wbr>instruction);<br>
><br>
> +   /* Set types on all vtn_values */<br>
> +   vtn_foreach_instruction(b, words, word_end, vtn_set_instruction_result_<wbr>type);<br>
> +<br>
>     vtn_build_cfg(b, words, word_end);<br>
><br>
>     assert(b->entry_point->value_<wbr>type == vtn_value_type_function);<br>
> diff --git a/src/compiler/spirv/vtn_<wbr>gather_types_c.py b/src/compiler/spirv/vtn_<wbr>gather_types_c.py<br>
> new file mode 100644<br>
> index 0000000..8cd8d9f<br>
> --- /dev/null<br>
> +++ b/src/compiler/spirv/vtn_<wbr>gather_types_c.py<br>
> @@ -0,0 +1,125 @@<br>
> +COPYRIGHT = """\<br>
> +/*<br>
> + * Copyright (C) 2017 Intel Corporation<br>
> + *<br>
> + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> + * copy of this software and associated documentation files (the "Software"),<br>
> + * to deal in the Software without restriction, including without limitation<br>
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> + * and/or sell copies of the Software, and to permit persons to whom the<br>
> + * Software is furnished to do so, subject to the following conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice (including the next<br>
> + * paragraph) shall be included in all copies or substantial portions of the<br>
> + * Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER<br>
> + * DEALINGS IN THE SOFTWARE.<br>
> + */<br>
> +"""<br>
> +<br>
> +import argparse<br>
> +import json<br>
> +from sys import stdout<br>
> +from mako.template import Template<br>
> +<br>
> +def find_result_types(spirv):<br>
> +    for inst in spirv['instructions']:<br>
> +        name = inst['opname']<br>
> +<br>
> +        if 'operands' not in inst:<br>
> +            continue<br>
> +<br>
> +        res_arg_idx = -1<br>
> +        res_type_arg_idx = -1<br>
> +        for idx, arg in enumerate(inst['operands']):<br>
> +            if arg['kind'] == 'IdResult':<br>
> +                res_arg_idx = idx<br>
> +            elif arg['kind'] == 'IdResultType':<br>
> +                res_type_arg_idx = idx<br>
> +<br>
> +        if res_type_arg_idx >= 0:<br>
> +            assert res_arg_idx >= 0<br>
> +        elif res_arg_idx >= 0:<br>
> +            untyped_insts = [<br>
> +                'OpString',<br>
> +                'OpExtInstImport',<br>
> +                'OpDecorationGroup',<br>
> +                'OpLabel',<br>
> +            ]<br>
> +            assert name.startswith('OpType') or name in untyped_insts<br>
> +<br>
> +        if res_arg_idx >= 0 or res_type_arg_idx >= 0:<br>
> +            yield (name, res_arg_idx, res_type_arg_idx)<br>
> +<br>
> +TEMPLATE  = Template(COPYRIGHT + """\<br>
> +<br>
> +/* DO NOT EDIT - This file is generated automatically by the<br>
> + * vtn_gather_types_c.py script<br>
> + */<br>
> +<br>
> +#include "vtn_private.h"<br>
> +<br>
> +struct type_args {<br>
> +    int res_idx;<br>
> +    int res_type_idx;<br>
> +};<br>
> +<br>
> +static struct type_args<br>
> +result_type_args_for_opcode(<wbr>SpvOp opcode)<br>
> +{<br>
> +   switch (opcode) {<br>
> +% for opcode in opcodes:<br>
> +   case Spv${opcode[0]}: return (struct type_args){ ${opcode[1]}, ${opcode[2]} };<br>
> +% endfor<br>
> +   default: return (struct type_args){ -1, -1 };<br>
> +   }<br>
> +}<br>
> +<br>
> +bool<br>
> +vtn_set_instruction_result_<wbr>type(struct vtn_builder *b, SpvOp opcode,<br>
> +                                const uint32_t *w, unsigned count)<br>
> +{<br>
> +   struct type_args args = result_type_args_for_opcode(<wbr>opcode);<br>
> +<br>
> +   if (args.res_idx < 0 || args.res_type_idx < 0)<br>
> +      return true;<br>
> +<br>
> +   struct vtn_value *val = vtn_untyped_value(b, w[1 + args.res_idx]);<br>
> +   val->type = vtn_value(b, w[1 + args.res_type_idx],<br>
> +                         vtn_value_type_type)->type;<br>
> +<br>
> +   return true;<br>
> +}<br>
> +<br>
> +""")<br>
> +<br>
> +if __name__ == "__main__":<br>
> +    p = argparse.ArgumentParser()<br>
> +    p.add_argument("json")<br>
> +    p.add_argument("out")<br>
> +    args = p.parse_args()<br>
> +<br>
> +    spirv_info = json.JSONDecoder().decode(<wbr>open(args.json, "r").read())<br>
> +<br>
> +    opcodes = list(find_result_types(spirv_<wbr>info))<br>
> +<br>
> +    try:<br>
> +        with open(args.out, 'w') as f:<br>
> +            f.write(TEMPLATE.render(<wbr>opcodes=opcodes))<br>
> +    except Exception:<br>
> +        # In the even there's an error this imports some helpers from mako<br>
> +        # to print a useful stack trace and prints it, then exits with<br>
> +        # status 1, if python is run with debug; otherwise it just raises<br>
> +        # the exception<br>
> +        if __debug__:<br>
> +            import sys<br>
> +            from mako import exceptions<br>
> +            sys.stderr.write(exceptions.<wbr>text_error_template().render() + '\n')<br>
> +            sys.exit(1)<br>
> +        raise<br>
> diff --git a/src/compiler/spirv/vtn_<wbr>private.h b/src/compiler/spirv/vtn_<wbr>private.h<br>
> index 6d4ad3c..a0a4f3a 100644<br>
> --- a/src/compiler/spirv/vtn_<wbr>private.h<br>
> +++ b/src/compiler/spirv/vtn_<wbr>private.h<br>
> @@ -620,6 +620,10 @@ vtn_value(struct vtn_builder *b, uint32_t value_id,<br>
>     return val;<br>
>  }<br>
><br>
> +bool<br>
> +vtn_set_instruction_result_<wbr>type(struct vtn_builder *b, SpvOp opcode,<br>
> +                                const uint32_t *w, unsigned count);<br>
> +<br>
>  struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);<br>
><br>
>  struct vtn_ssa_value *vtn_create_ssa_value(struct vtn_builder *b,<br>
</div></div>______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</blockquote></div><br></div></div>