spirv: Add a prepass to set types on vtn_values
[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 for inst in spirv['instructions']:
33 name = inst['opname']
34
35 if 'operands' not in inst:
36 continue
37
38 res_arg_idx = -1
39 res_type_arg_idx = -1
40 for idx, arg in enumerate(inst['operands']):
41 if arg['kind'] == 'IdResult':
42 res_arg_idx = idx
43 elif arg['kind'] == 'IdResultType':
44 res_type_arg_idx = idx
45
46 if res_type_arg_idx >= 0:
47 assert res_arg_idx >= 0
48 elif res_arg_idx >= 0:
49 untyped_insts = [
50 'OpString',
51 'OpExtInstImport',
52 'OpDecorationGroup',
53 'OpLabel',
54 ]
55 assert name.startswith('OpType') or name in untyped_insts
56
57 if res_arg_idx >= 0 or res_type_arg_idx >= 0:
58 yield (name, res_arg_idx, res_type_arg_idx)
59
60 TEMPLATE = Template(COPYRIGHT + """\
61
62 /* DO NOT EDIT - This file is generated automatically by the
63 * vtn_gather_types_c.py script
64 */
65
66 #include "vtn_private.h"
67
68 struct type_args {
69 int res_idx;
70 int res_type_idx;
71 };
72
73 static struct type_args
74 result_type_args_for_opcode(SpvOp opcode)
75 {
76 switch (opcode) {
77 % for opcode in opcodes:
78 case Spv${opcode[0]}: return (struct type_args){ ${opcode[1]}, ${opcode[2]} };
79 % endfor
80 default: return (struct type_args){ -1, -1 };
81 }
82 }
83
84 bool
85 vtn_set_instruction_result_type(struct vtn_builder *b, SpvOp opcode,
86 const uint32_t *w, unsigned count)
87 {
88 struct type_args args = result_type_args_for_opcode(opcode);
89
90 if (args.res_idx >= 0 && args.res_type_idx >= 0) {
91 struct vtn_value *val = vtn_untyped_value(b, w[1 + args.res_idx]);
92 val->type = vtn_value(b, w[1 + args.res_type_idx],
93 vtn_value_type_type)->type;
94 }
95
96 return true;
97 }
98
99 """)
100
101 if __name__ == "__main__":
102 p = argparse.ArgumentParser()
103 p.add_argument("json")
104 p.add_argument("out")
105 args = p.parse_args()
106
107 spirv_info = json.JSONDecoder().decode(open(args.json, "r").read())
108
109 opcodes = list(find_result_types(spirv_info))
110
111 try:
112 with open(args.out, 'w') as f:
113 f.write(TEMPLATE.render(opcodes=opcodes))
114 except Exception:
115 # In the even there's an error this imports some helpers from mako
116 # to print a useful stack trace and prints it, then exits with
117 # status 1, if python is run with debug; otherwise it just raises
118 # the exception
119 if __debug__:
120 import sys
121 from mako import exceptions
122 sys.stderr.write(exceptions.text_error_template().render() + '\n')
123 sys.exit(1)
124 raise