Merge remote-tracking branch 'public/master' into vulkan
[mesa.git] / src / compiler / nir / nir_algebraic.py
1 #! /usr/bin/env python
2 #
3 # Copyright (C) 2014 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 DEALINGS
22 # IN THE SOFTWARE.
23 #
24 # Authors:
25 # Jason Ekstrand (jason@jlekstrand.net)
26
27 import itertools
28 import struct
29 import sys
30 import mako.template
31 import re
32
33 # Represents a set of variables, each with a unique id
34 class VarSet(object):
35 def __init__(self):
36 self.names = {}
37 self.ids = itertools.count()
38 self.immutable = False;
39
40 def __getitem__(self, name):
41 if name not in self.names:
42 assert not self.immutable, "Unknown replacement variable: " + name
43 self.names[name] = self.ids.next()
44
45 return self.names[name]
46
47 def lock(self):
48 self.immutable = True
49
50 class Value(object):
51 @staticmethod
52 def create(val, name_base, varset):
53 if isinstance(val, tuple):
54 return Expression(val, name_base, varset)
55 elif isinstance(val, Expression):
56 return val
57 elif isinstance(val, (str, unicode)):
58 return Variable(val, name_base, varset)
59 elif isinstance(val, (bool, int, long, float)):
60 return Constant(val, name_base)
61
62 __template = mako.template.Template("""
63 static const ${val.c_type} ${val.name} = {
64 { ${val.type_enum} },
65 % if isinstance(val, Constant):
66 ${val.type()}, { ${hex(val)} /* ${val.value} */ },
67 % elif isinstance(val, Variable):
68 ${val.index}, /* ${val.var_name} */
69 ${'true' if val.is_constant else 'false'},
70 ${val.type() or 'nir_type_invalid' },
71 % elif isinstance(val, Expression):
72 ${'true' if val.inexact else 'false'},
73 nir_op_${val.opcode},
74 { ${', '.join(src.c_ptr for src in val.sources)} },
75 % endif
76 };""")
77
78 def __init__(self, name, type_str):
79 self.name = name
80 self.type_str = type_str
81
82 @property
83 def type_enum(self):
84 return "nir_search_value_" + self.type_str
85
86 @property
87 def c_type(self):
88 return "nir_search_" + self.type_str
89
90 @property
91 def c_ptr(self):
92 return "&{0}.value".format(self.name)
93
94 def render(self):
95 return self.__template.render(val=self,
96 Constant=Constant,
97 Variable=Variable,
98 Expression=Expression)
99
100 class Constant(Value):
101 def __init__(self, val, name):
102 Value.__init__(self, name, "constant")
103 self.value = val
104
105 def __hex__(self):
106 if isinstance(self.value, (bool)):
107 return 'NIR_TRUE' if self.value else 'NIR_FALSE'
108 if isinstance(self.value, (int, long)):
109 return hex(self.value)
110 elif isinstance(self.value, float):
111 return hex(struct.unpack('Q', struct.pack('d', self.value))[0])
112 else:
113 assert False
114
115 def type(self):
116 if isinstance(self.value, (bool)):
117 return "nir_type_bool32"
118 elif isinstance(self.value, (int, long)):
119 return "nir_type_int"
120 elif isinstance(self.value, float):
121 return "nir_type_float"
122
123 _var_name_re = re.compile(r"(?P<const>#)?(?P<name>\w+)(?:@(?P<type>\w+))?")
124
125 class Variable(Value):
126 def __init__(self, val, name, varset):
127 Value.__init__(self, name, "variable")
128
129 m = _var_name_re.match(val)
130 assert m and m.group('name') is not None
131
132 self.var_name = m.group('name')
133 self.is_constant = m.group('const') is not None
134 self.required_type = m.group('type')
135
136 if self.required_type is not None:
137 assert self.required_type in ('float', 'bool', 'int', 'unsigned')
138
139 self.index = varset[self.var_name]
140
141 def type(self):
142 if self.required_type == 'bool':
143 return "nir_type_bool32"
144 elif self.required_type in ('int', 'unsigned'):
145 return "nir_type_int"
146 elif self.required_type == 'float':
147 return "nir_type_float"
148
149 _opcode_re = re.compile(r"(?P<inexact>~)?(?P<opcode>\w+)")
150
151 class Expression(Value):
152 def __init__(self, expr, name_base, varset):
153 Value.__init__(self, name_base, "expression")
154 assert isinstance(expr, tuple)
155
156 m = _opcode_re.match(expr[0])
157 assert m and m.group('opcode') is not None
158
159 self.opcode = m.group('opcode')
160 self.inexact = m.group('inexact') is not None
161 self.sources = [ Value.create(src, "{0}_{1}".format(name_base, i), varset)
162 for (i, src) in enumerate(expr[1:]) ]
163
164 def render(self):
165 srcs = "\n".join(src.render() for src in self.sources)
166 return srcs + super(Expression, self).render()
167
168 _optimization_ids = itertools.count()
169
170 condition_list = ['true']
171
172 class SearchAndReplace(object):
173 def __init__(self, transform):
174 self.id = _optimization_ids.next()
175
176 search = transform[0]
177 replace = transform[1]
178 if len(transform) > 2:
179 self.condition = transform[2]
180 else:
181 self.condition = 'true'
182
183 if self.condition not in condition_list:
184 condition_list.append(self.condition)
185 self.condition_index = condition_list.index(self.condition)
186
187 varset = VarSet()
188 if isinstance(search, Expression):
189 self.search = search
190 else:
191 self.search = Expression(search, "search{0}".format(self.id), varset)
192
193 varset.lock()
194
195 if isinstance(replace, Value):
196 self.replace = replace
197 else:
198 self.replace = Value.create(replace, "replace{0}".format(self.id), varset)
199
200 _algebraic_pass_template = mako.template.Template("""
201 #include "nir.h"
202 #include "nir_search.h"
203
204 #ifndef NIR_OPT_ALGEBRAIC_STRUCT_DEFS
205 #define NIR_OPT_ALGEBRAIC_STRUCT_DEFS
206
207 struct transform {
208 const nir_search_expression *search;
209 const nir_search_value *replace;
210 unsigned condition_offset;
211 };
212
213 struct opt_state {
214 void *mem_ctx;
215 bool progress;
216 const bool *condition_flags;
217 };
218
219 #endif
220
221 % for (opcode, xform_list) in xform_dict.iteritems():
222 % for xform in xform_list:
223 ${xform.search.render()}
224 ${xform.replace.render()}
225 % endfor
226
227 static const struct transform ${pass_name}_${opcode}_xforms[] = {
228 % for xform in xform_list:
229 { &${xform.search.name}, ${xform.replace.c_ptr}, ${xform.condition_index} },
230 % endfor
231 };
232 % endfor
233
234 static bool
235 ${pass_name}_block(nir_block *block, void *void_state)
236 {
237 struct opt_state *state = void_state;
238
239 nir_foreach_instr_reverse_safe(block, instr) {
240 if (instr->type != nir_instr_type_alu)
241 continue;
242
243 nir_alu_instr *alu = nir_instr_as_alu(instr);
244 if (!alu->dest.dest.is_ssa)
245 continue;
246
247 switch (alu->op) {
248 % for opcode in xform_dict.keys():
249 case nir_op_${opcode}:
250 for (unsigned i = 0; i < ARRAY_SIZE(${pass_name}_${opcode}_xforms); i++) {
251 const struct transform *xform = &${pass_name}_${opcode}_xforms[i];
252 if (state->condition_flags[xform->condition_offset] &&
253 nir_replace_instr(alu, xform->search, xform->replace,
254 state->mem_ctx)) {
255 state->progress = true;
256 break;
257 }
258 }
259 break;
260 % endfor
261 default:
262 break;
263 }
264 }
265
266 return true;
267 }
268
269 static bool
270 ${pass_name}_impl(nir_function_impl *impl, const bool *condition_flags)
271 {
272 struct opt_state state;
273
274 state.mem_ctx = ralloc_parent(impl);
275 state.progress = false;
276 state.condition_flags = condition_flags;
277
278 nir_foreach_block_reverse(impl, ${pass_name}_block, &state);
279
280 if (state.progress)
281 nir_metadata_preserve(impl, nir_metadata_block_index |
282 nir_metadata_dominance);
283
284 return state.progress;
285 }
286
287
288 bool
289 ${pass_name}(nir_shader *shader)
290 {
291 bool progress = false;
292 bool condition_flags[${len(condition_list)}];
293 const nir_shader_compiler_options *options = shader->options;
294 (void) options;
295
296 % for index, condition in enumerate(condition_list):
297 condition_flags[${index}] = ${condition};
298 % endfor
299
300 nir_foreach_function(shader, function) {
301 if (function->impl)
302 progress |= ${pass_name}_impl(function->impl, condition_flags);
303 }
304
305 return progress;
306 }
307 """)
308
309 class AlgebraicPass(object):
310 def __init__(self, pass_name, transforms):
311 self.xform_dict = {}
312 self.pass_name = pass_name
313
314 for xform in transforms:
315 if not isinstance(xform, SearchAndReplace):
316 xform = SearchAndReplace(xform)
317
318 if xform.search.opcode not in self.xform_dict:
319 self.xform_dict[xform.search.opcode] = []
320
321 self.xform_dict[xform.search.opcode].append(xform)
322
323 def render(self):
324 return _algebraic_pass_template.render(pass_name=self.pass_name,
325 xform_dict=self.xform_dict,
326 condition_list=condition_list)