3 # Copyright (C) 2014 Intel Corporation
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:
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
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
25 # Jason Ekstrand (jason@jlekstrand.net)
33 # Represents a set of variables, each with a unique id
37 self
.ids
= itertools
.count()
38 self
.immutable
= False;
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()
45 return self
.names
[name
]
52 def create(val
, name_base
, varset
):
53 if isinstance(val
, tuple):
54 return Expression(val
, name_base
, varset
)
55 elif isinstance(val
, Expression
):
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
)
62 __template
= mako
.template
.Template("""
63 static const ${val.c_type} ${val.name} = {
65 % if isinstance(val, Constant):
66 { ${hex(val)} /* ${val.value} */ },
67 % elif isinstance(val, Variable):
68 ${val.index}, /* ${val.var_name} */
69 ${'true' if val.is_constant else 'false'},
70 nir_type_${ val.required_type or 'invalid' },
71 % elif isinstance(val, Expression):
73 { ${', '.join(src.c_ptr for src in val.sources)} },
77 def __init__(self
, name
, type_str
):
79 self
.type_str
= type_str
83 return "nir_search_value_" + self
.type_str
87 return "nir_search_" + self
.type_str
91 return "&{0}.value".format(self
.name
)
94 return self
.__template
.render(val
=self
,
97 Expression
=Expression
)
99 class Constant(Value
):
100 def __init__(self
, val
, name
):
101 Value
.__init
__(self
, name
, "constant")
105 # Even if it's an integer, we still need to unpack as an unsigned
106 # int. This is because, without C99, we can only assign to the first
107 # element of a union in an initializer.
108 if isinstance(self
.value
, (bool)):
109 return 'NIR_TRUE' if self
.value
else 'NIR_FALSE'
110 if isinstance(self
.value
, (int, long)):
111 return hex(struct
.unpack('I', struct
.pack('i', self
.value
))[0])
112 elif isinstance(self
.value
, float):
113 return hex(struct
.unpack('I', struct
.pack('f', self
.value
))[0])
117 _var_name_re
= re
.compile(r
"(?P<const>#)?(?P<name>\w+)(?:@(?P<type>\w+))?")
119 class Variable(Value
):
120 def __init__(self
, val
, name
, varset
):
121 Value
.__init
__(self
, name
, "variable")
123 m
= _var_name_re
.match(val
)
124 assert m
and m
.group('name') is not None
126 self
.var_name
= m
.group('name')
127 self
.is_constant
= m
.group('const') is not None
128 self
.required_type
= m
.group('type')
130 if self
.required_type
is not None:
131 assert self
.required_type
in ('float', 'bool', 'int', 'unsigned')
133 self
.index
= varset
[self
.var_name
]
135 class Expression(Value
):
136 def __init__(self
, expr
, name_base
, varset
):
137 Value
.__init
__(self
, name_base
, "expression")
138 assert isinstance(expr
, tuple)
140 self
.opcode
= expr
[0]
141 self
.sources
= [ Value
.create(src
, "{0}_{1}".format(name_base
, i
), varset
)
142 for (i
, src
) in enumerate(expr
[1:]) ]
145 srcs
= "\n".join(src
.render() for src
in self
.sources
)
146 return srcs
+ super(Expression
, self
).render()
148 _optimization_ids
= itertools
.count()
150 condition_list
= ['true']
152 class SearchAndReplace(object):
153 def __init__(self
, transform
):
154 self
.id = _optimization_ids
.next()
156 search
= transform
[0]
157 replace
= transform
[1]
158 if len(transform
) > 2:
159 self
.condition
= transform
[2]
161 self
.condition
= 'true'
163 if self
.condition
not in condition_list
:
164 condition_list
.append(self
.condition
)
165 self
.condition_index
= condition_list
.index(self
.condition
)
168 if isinstance(search
, Expression
):
171 self
.search
= Expression(search
, "search{0}".format(self
.id), varset
)
175 if isinstance(replace
, Value
):
176 self
.replace
= replace
178 self
.replace
= Value
.create(replace
, "replace{0}".format(self
.id), varset
)
180 _algebraic_pass_template
= mako
.template
.Template("""
182 #include "nir_search.h"
184 #ifndef NIR_OPT_ALGEBRAIC_STRUCT_DEFS
185 #define NIR_OPT_ALGEBRAIC_STRUCT_DEFS
188 const nir_search_expression *search;
189 const nir_search_value *replace;
190 unsigned condition_offset;
196 const bool *condition_flags;
201 % for (opcode, xform_list) in xform_dict.iteritems():
202 % for xform in xform_list:
203 ${xform.search.render()}
204 ${xform.replace.render()}
207 static const struct transform ${pass_name}_${opcode}_xforms[] = {
208 % for xform in xform_list:
209 { &${xform.search.name}, ${xform.replace.c_ptr}, ${xform.condition_index} },
215 ${pass_name}_block(nir_block *block, void *void_state)
217 struct opt_state *state = void_state;
219 nir_foreach_instr_safe(block, instr) {
220 if (instr->type != nir_instr_type_alu)
223 nir_alu_instr *alu = nir_instr_as_alu(instr);
224 if (!alu->dest.dest.is_ssa)
228 % for opcode in xform_dict.keys():
229 case nir_op_${opcode}:
230 for (unsigned i = 0; i < ARRAY_SIZE(${pass_name}_${opcode}_xforms); i++) {
231 const struct transform *xform = &${pass_name}_${opcode}_xforms[i];
232 if (state->condition_flags[xform->condition_offset] &&
233 nir_replace_instr(alu, xform->search, xform->replace,
235 state->progress = true;
250 ${pass_name}_impl(nir_function_impl *impl, const bool *condition_flags)
252 struct opt_state state;
254 state.mem_ctx = ralloc_parent(impl);
255 state.progress = false;
256 state.condition_flags = condition_flags;
258 nir_foreach_block(impl, ${pass_name}_block, &state);
261 nir_metadata_preserve(impl, nir_metadata_block_index |
262 nir_metadata_dominance);
264 return state.progress;
269 ${pass_name}(nir_shader *shader)
271 bool progress = false;
272 bool condition_flags[${len(condition_list)}];
273 const nir_shader_compiler_options *options = shader->options;
275 % for index, condition in enumerate(condition_list):
276 condition_flags[${index}] = ${condition};
279 nir_foreach_overload(shader, overload) {
281 progress |= ${pass_name}_impl(overload->impl, condition_flags);
288 class AlgebraicPass(object):
289 def __init__(self
, pass_name
, transforms
):
291 self
.pass_name
= pass_name
293 for xform
in transforms
:
294 if not isinstance(xform
, SearchAndReplace
):
295 xform
= SearchAndReplace(xform
)
297 if xform
.search
.opcode
not in self
.xform_dict
:
298 self
.xform_dict
[xform
.search
.opcode
] = []
300 self
.xform_dict
[xform
.search
.opcode
].append(xform
)
303 return _algebraic_pass_template
.render(pass_name
=self
.pass_name
,
304 xform_dict
=self
.xform_dict
,
305 condition_list
=condition_list
)