nir/lower_indirect_derefs: Add a threshold
[mesa.git] / src / compiler / spirv / vtn_gather_types_c.py
1 COPYRIGHT = """\
2 /*
3 * Copyright (C) 2017 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24 """
25
26 import argparse
27 import json
28 from sys import stdout
29 from mako.template import Template
30
31 def find_result_types(spirv):
32 seen = set()
33 for inst in spirv['instructions']:
34 # Handle aliases by choosing the first one in the grammar.
35 if inst['opcode'] in seen:
36 continue
37 seen.add(inst['opcode'])
38
39 name = inst['opname']
40
41 if 'operands' not in inst:
42 continue
43
44 res_arg_idx = -1
45 res_type_arg_idx = -1
46 for idx, arg in enumerate(inst['operands']):
47 if arg['kind'] == 'IdResult':
48 res_arg_idx = idx
49 elif arg['kind'] == 'IdResultType':
50 res_type_arg_idx = idx
51
52 if res_type_arg_idx >= 0:
53 assert res_arg_idx >= 0
54 elif res_arg_idx >= 0:
55 untyped_insts = [
56 'OpString',
57 'OpExtInstImport',
58 'OpDecorationGroup',
59 'OpLabel',
60 ]
61 assert name.startswith('OpType') or name in untyped_insts
62
63 if res_arg_idx >= 0 or res_type_arg_idx >= 0:
64 yield (name, res_arg_idx, res_type_arg_idx)
65
66 TEMPLATE = Template(COPYRIGHT + """\
67
68 /* DO NOT EDIT - This file is generated automatically by the
69 * vtn_gather_types_c.py script
70 */
71
72 #include "vtn_private.h"
73
74 struct type_args {
75 int res_idx;
76 int res_type_idx;
77 };
78
79 static struct type_args
80 result_type_args_for_opcode(SpvOp opcode)
81 {
82 switch (opcode) {
83 % for opcode in opcodes:
84 case Spv${opcode[0]}: return (struct type_args){ ${opcode[1]}, ${opcode[2]} };
85 % endfor
86 default: return (struct type_args){ -1, -1 };
87 }
88 }
89
90 bool
91 vtn_set_instruction_result_type(struct vtn_builder *b, SpvOp opcode,
92 const uint32_t *w, unsigned count)
93 {
94 struct type_args args = result_type_args_for_opcode(opcode);
95
96 if (args.res_idx >= 0 && args.res_type_idx >= 0) {
97 struct vtn_value *val = vtn_untyped_value(b, w[1 + args.res_idx]);
98 val->type = vtn_value(b, w[1 + args.res_type_idx],
99 vtn_value_type_type)->type;
100 }
101
102 return true;
103 }
104
105 """)
106
107 if __name__ == "__main__":
108 p = argparse.ArgumentParser()
109 p.add_argument("json")
110 p.add_argument("out")
111 args = p.parse_args()
112
113 spirv_info = json.JSONDecoder().decode(open(args.json, "r").read())
114
115 opcodes = list(find_result_types(spirv_info))
116
117 try:
118 with open(args.out, 'w') as f:
119 f.write(TEMPLATE.render(opcodes=opcodes))
120 except Exception:
121 # In the even there's an error this imports some helpers from mako
122 # to print a useful stack trace and prints it, then exits with
123 # status 1, if python is run with debug; otherwise it just raises
124 # the exception
125 if __debug__:
126 import sys
127 from mako import exceptions
128 sys.stderr.write(exceptions.text_error_template().render() + '\n')
129 sys.exit(1)
130 raise