2 # Copyright (C) 2014 Intel Corporation
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the "Software"),
6 # to deal in the Software without restriction, including without limitation
7 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Software, and to permit persons to whom the
9 # Software is furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice (including the next
12 # paragraph) shall be included in all copies or substantial portions of the
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 # Jason Ekstrand (jason@jlekstrand.net)
26 from __future__
import print_function
28 from collections
import OrderedDict
36 from nir_opcodes
import opcodes
38 if sys
.version_info
< (3, 0):
44 _type_re
= re
.compile(r
"(?P<type>int|uint|bool|float)?(?P<bits>\d+)?")
46 def type_bits(type_str
):
47 m
= _type_re
.match(type_str
)
48 assert m
.group('type')
50 if m
.group('bits') is None:
53 return int(m
.group('bits'))
55 # Represents a set of variables, each with a unique id
59 self
.ids
= itertools
.count()
60 self
.immutable
= False;
62 def __getitem__(self
, name
):
63 if name
not in self
.names
:
64 assert not self
.immutable
, "Unknown replacement variable: " + name
65 self
.names
[name
] = next(self
.ids
)
67 return self
.names
[name
]
74 def create(val
, name_base
, varset
):
75 if isinstance(val
, bytes
):
76 val
= val
.decode('utf-8')
78 if isinstance(val
, tuple):
79 return Expression(val
, name_base
, varset
)
80 elif isinstance(val
, Expression
):
82 elif isinstance(val
, string_type
):
83 return Variable(val
, name_base
, varset
)
84 elif isinstance(val
, (bool, int, long, float)):
85 return Constant(val
, name_base
)
87 __template
= mako
.template
.Template("""
88 static const ${val.c_type} ${val.name} = {
89 { ${val.type_enum}, ${val.bit_size} },
90 % if isinstance(val, Constant):
91 ${val.type()}, { ${val.hex()} /* ${val.value} */ },
92 % elif isinstance(val, Variable):
93 ${val.index}, /* ${val.var_name} */
94 ${'true' if val.is_constant else 'false'},
95 ${val.type() or 'nir_type_invalid' },
96 ${val.cond if val.cond else 'NULL'},
97 % elif isinstance(val, Expression):
98 ${'true' if val.inexact else 'false'},
100 { ${', '.join(src.c_ptr for src in val.sources)} },
101 ${val.cond if val.cond else 'NULL'},
105 def __init__(self
, name
, type_str
):
107 self
.type_str
= type_str
111 return "nir_search_value_" + self
.type_str
115 return "nir_search_" + self
.type_str
119 return "&{0}.value".format(self
.name
)
122 return self
.__template
.render(val
=self
,
125 Expression
=Expression
)
127 _constant_re
= re
.compile(r
"(?P<value>[^@\(]+)(?:@(?P<bits>\d+))?")
129 class Constant(Value
):
130 def __init__(self
, val
, name
):
131 Value
.__init
__(self
, name
, "constant")
133 if isinstance(val
, (str)):
134 m
= _constant_re
.match(val
)
135 self
.value
= ast
.literal_eval(m
.group('value'))
136 self
.bit_size
= int(m
.group('bits')) if m
.group('bits') else 0
141 if isinstance(self
.value
, bool):
142 assert self
.bit_size
== 0 or self
.bit_size
== 32
146 if isinstance(self
.value
, (bool)):
147 return 'NIR_TRUE' if self
.value
else 'NIR_FALSE'
148 if isinstance(self
.value
, (int, long)):
149 return hex(self
.value
)
150 elif isinstance(self
.value
, float):
151 i
= struct
.unpack('Q', struct
.pack('d', self
.value
))[0]
154 # On Python 2 this 'L' suffix is automatically added, but not on Python 3
155 # Adding it explicitly makes the generated file identical, regardless
156 # of the Python version running this script.
157 if h
[-1] != 'L' and i
> sys
.maxsize
:
165 if isinstance(self
.value
, (bool)):
166 return "nir_type_bool32"
167 elif isinstance(self
.value
, (int, long)):
168 return "nir_type_int"
169 elif isinstance(self
.value
, float):
170 return "nir_type_float"
172 _var_name_re
= re
.compile(r
"(?P<const>#)?(?P<name>\w+)"
173 r
"(?:@(?P<type>int|uint|bool|float)?(?P<bits>\d+)?)?"
174 r
"(?P<cond>\([^\)]+\))?")
176 class Variable(Value
):
177 def __init__(self
, val
, name
, varset
):
178 Value
.__init
__(self
, name
, "variable")
180 m
= _var_name_re
.match(val
)
181 assert m
and m
.group('name') is not None
183 self
.var_name
= m
.group('name')
184 self
.is_constant
= m
.group('const') is not None
185 self
.cond
= m
.group('cond')
186 self
.required_type
= m
.group('type')
187 self
.bit_size
= int(m
.group('bits')) if m
.group('bits') else 0
189 if self
.required_type
== 'bool':
190 assert self
.bit_size
== 0 or self
.bit_size
== 32
193 if self
.required_type
is not None:
194 assert self
.required_type
in ('float', 'bool', 'int', 'uint')
196 self
.index
= varset
[self
.var_name
]
199 if self
.required_type
== 'bool':
200 return "nir_type_bool32"
201 elif self
.required_type
in ('int', 'uint'):
202 return "nir_type_int"
203 elif self
.required_type
== 'float':
204 return "nir_type_float"
206 _opcode_re
= re
.compile(r
"(?P<inexact>~)?(?P<opcode>\w+)(?:@(?P<bits>\d+))?"
207 r
"(?P<cond>\([^\)]+\))?")
209 class Expression(Value
):
210 def __init__(self
, expr
, name_base
, varset
):
211 Value
.__init
__(self
, name_base
, "expression")
212 assert isinstance(expr
, tuple)
214 m
= _opcode_re
.match(expr
[0])
215 assert m
and m
.group('opcode') is not None
217 self
.opcode
= m
.group('opcode')
218 self
.bit_size
= int(m
.group('bits')) if m
.group('bits') else 0
219 self
.inexact
= m
.group('inexact') is not None
220 self
.cond
= m
.group('cond')
221 self
.sources
= [ Value
.create(src
, "{0}_{1}".format(name_base
, i
), varset
)
222 for (i
, src
) in enumerate(expr
[1:]) ]
225 srcs
= "\n".join(src
.render() for src
in self
.sources
)
226 return srcs
+ super(Expression
, self
).render()
228 class IntEquivalenceRelation(object):
229 """A class representing an equivalence relation on integers.
231 Each integer has a canonical form which is the maximum integer to which it
232 is equivalent. Two integers are equivalent precisely when they have the
235 The convention of maximum is explicitly chosen to make using it in
236 BitSizeValidator easier because it means that an actual bit_size (if any)
237 will always be the canonical form.
242 def get_canonical(self
, x
):
243 """Get the canonical integer corresponding to x."""
245 return self
.get_canonical(self
._remap
[x
])
249 def add_equiv(self
, a
, b
):
250 """Add an equivalence and return the canonical form."""
251 c
= max(self
.get_canonical(a
), self
.get_canonical(b
))
262 class BitSizeValidator(object):
263 """A class for validating bit sizes of expressions.
265 NIR supports multiple bit-sizes on expressions in order to handle things
266 such as fp64. The source and destination of every ALU operation is
267 assigned a type and that type may or may not specify a bit size. Sources
268 and destinations whose type does not specify a bit size are considered
269 "unsized" and automatically take on the bit size of the corresponding
270 register or SSA value. NIR has two simple rules for bit sizes that are
271 validated by nir_validator:
273 1) A given SSA def or register has a single bit size that is respected by
274 everything that reads from it or writes to it.
276 2) The bit sizes of all unsized inputs/outputs on any given ALU
277 instruction must match. They need not match the sized inputs or
278 outputs but they must match each other.
280 In order to keep nir_algebraic relatively simple and easy-to-use,
281 nir_search supports a type of bit-size inference based on the two rules
282 above. This is similar to type inference in many common programming
283 languages. If, for instance, you are constructing an add operation and you
284 know the second source is 16-bit, then you know that the other source and
285 the destination must also be 16-bit. There are, however, cases where this
286 inference can be ambiguous or contradictory. Consider, for instance, the
287 following transformation:
289 (('usub_borrow', a, b), ('b2i', ('ult', a, b)))
291 This transformation can potentially cause a problem because usub_borrow is
292 well-defined for any bit-size of integer. However, b2i always generates a
293 32-bit result so it could end up replacing a 64-bit expression with one
294 that takes two 64-bit values and produces a 32-bit value. As another
295 example, consider this expression:
297 (('bcsel', a, b, 0), ('iand', a, b))
299 In this case, in the search expression a must be 32-bit but b can
300 potentially have any bit size. If we had a 64-bit b value, we would end up
301 trying to and a 32-bit value with a 64-bit value which would be invalid
303 This class solves that problem by providing a validation layer that proves
304 that a given search-and-replace operation is 100% well-defined before we
305 generate any code. This ensures that bugs are caught at compile time
306 rather than at run time.
308 The basic operation of the validator is very similar to the bitsize_tree in
309 nir_search only a little more subtle. Instead of simply tracking bit
310 sizes, it tracks "bit classes" where each class is represented by an
311 integer. A value of 0 means we don't know anything yet, positive values
312 are actual bit-sizes, and negative values are used to track equivalence
313 classes of sizes that must be the same but have yet to receive an actual
314 size. The first stage uses the bitsize_tree algorithm to assign bit
315 classes to each variable. If it ever comes across an inconsistency, it
316 assert-fails. Then the second stage uses that information to prove that
317 the resulting expression can always validly be constructed.
320 def __init__(self
, varset
):
321 self
._num
_classes
= 0
322 self
._var
_classes
= [0] * len(varset
.names
)
323 self
._class
_relation
= IntEquivalenceRelation()
325 def validate(self
, search
, replace
):
326 dst_class
= self
._propagate
_bit
_size
_up
(search
)
328 dst_class
= self
._new
_class
()
329 self
._propagate
_bit
_class
_down
(search
, dst_class
)
331 validate_dst_class
= self
._validate
_bit
_class
_up
(replace
)
332 assert validate_dst_class
== 0 or validate_dst_class
== dst_class
333 self
._validate
_bit
_class
_down
(replace
, dst_class
)
335 def _new_class(self
):
336 self
._num
_classes
+= 1
337 return -self
._num
_classes
339 def _set_var_bit_class(self
, var_id
, bit_class
):
340 assert bit_class
!= 0
341 var_class
= self
._var
_classes
[var_id
]
343 self
._var
_classes
[var_id
] = bit_class
345 canon_class
= self
._class
_relation
.get_canonical(var_class
)
346 assert canon_class
< 0 or canon_class
== bit_class
347 var_class
= self
._class
_relation
.add_equiv(var_class
, bit_class
)
348 self
._var
_classes
[var_id
] = var_class
350 def _get_var_bit_class(self
, var_id
):
351 return self
._class
_relation
.get_canonical(self
._var
_classes
[var_id
])
353 def _propagate_bit_size_up(self
, val
):
354 if isinstance(val
, (Constant
, Variable
)):
357 elif isinstance(val
, Expression
):
358 nir_op
= opcodes
[val
.opcode
]
360 for i
in range(nir_op
.num_inputs
):
361 src_bits
= self
._propagate
_bit
_size
_up
(val
.sources
[i
])
365 src_type_bits
= type_bits(nir_op
.input_types
[i
])
366 if src_type_bits
!= 0:
367 assert src_bits
== src_type_bits
369 assert val
.common_size
== 0 or src_bits
== val
.common_size
370 val
.common_size
= src_bits
372 dst_type_bits
= type_bits(nir_op
.output_type
)
373 if dst_type_bits
!= 0:
374 assert val
.bit_size
== 0 or val
.bit_size
== dst_type_bits
377 if val
.common_size
!= 0:
378 assert val
.bit_size
== 0 or val
.bit_size
== val
.common_size
380 val
.common_size
= val
.bit_size
381 return val
.common_size
383 def _propagate_bit_class_down(self
, val
, bit_class
):
384 if isinstance(val
, Constant
):
385 assert val
.bit_size
== 0 or val
.bit_size
== bit_class
387 elif isinstance(val
, Variable
):
388 assert val
.bit_size
== 0 or val
.bit_size
== bit_class
389 self
._set
_var
_bit
_class
(val
.index
, bit_class
)
391 elif isinstance(val
, Expression
):
392 nir_op
= opcodes
[val
.opcode
]
393 dst_type_bits
= type_bits(nir_op
.output_type
)
394 if dst_type_bits
!= 0:
395 assert bit_class
== 0 or bit_class
== dst_type_bits
397 assert val
.common_size
== 0 or val
.common_size
== bit_class
398 val
.common_size
= bit_class
401 common_class
= val
.common_size
402 elif nir_op
.num_inputs
:
403 # If we got here then we have no idea what the actual size is.
404 # Instead, we use a generic class
405 common_class
= self
._new
_class
()
407 for i
in range(nir_op
.num_inputs
):
408 src_type_bits
= type_bits(nir_op
.input_types
[i
])
409 if src_type_bits
!= 0:
410 self
._propagate
_bit
_class
_down
(val
.sources
[i
], src_type_bits
)
412 self
._propagate
_bit
_class
_down
(val
.sources
[i
], common_class
)
414 def _validate_bit_class_up(self
, val
):
415 if isinstance(val
, Constant
):
418 elif isinstance(val
, Variable
):
419 var_class
= self
._get
_var
_bit
_class
(val
.index
)
420 # By the time we get to validation, every variable should have a class
421 assert var_class
!= 0
423 # If we have an explicit size provided by the user, the variable
424 # *must* exactly match the search. It cannot be implicitly sized
425 # because otherwise we could end up with a conflict at runtime.
426 assert val
.bit_size
== 0 or val
.bit_size
== var_class
430 elif isinstance(val
, Expression
):
431 nir_op
= opcodes
[val
.opcode
]
433 for i
in range(nir_op
.num_inputs
):
434 src_class
= self
._validate
_bit
_class
_up
(val
.sources
[i
])
438 src_type_bits
= type_bits(nir_op
.input_types
[i
])
439 if src_type_bits
!= 0:
440 assert src_class
== src_type_bits
442 assert val
.common_class
== 0 or src_class
== val
.common_class
443 val
.common_class
= src_class
445 dst_type_bits
= type_bits(nir_op
.output_type
)
446 if dst_type_bits
!= 0:
447 assert val
.bit_size
== 0 or val
.bit_size
== dst_type_bits
450 if val
.common_class
!= 0:
451 assert val
.bit_size
== 0 or val
.bit_size
== val
.common_class
453 val
.common_class
= val
.bit_size
454 return val
.common_class
456 def _validate_bit_class_down(self
, val
, bit_class
):
457 # At this point, everything *must* have a bit class. Otherwise, we have
458 # a value we don't know how to define.
459 assert bit_class
!= 0
461 if isinstance(val
, Constant
):
462 assert val
.bit_size
== 0 or val
.bit_size
== bit_class
464 elif isinstance(val
, Variable
):
465 assert val
.bit_size
== 0 or val
.bit_size
== bit_class
467 elif isinstance(val
, Expression
):
468 nir_op
= opcodes
[val
.opcode
]
469 dst_type_bits
= type_bits(nir_op
.output_type
)
470 if dst_type_bits
!= 0:
471 assert bit_class
== dst_type_bits
473 assert val
.common_class
== 0 or val
.common_class
== bit_class
474 val
.common_class
= bit_class
476 for i
in range(nir_op
.num_inputs
):
477 src_type_bits
= type_bits(nir_op
.input_types
[i
])
478 if src_type_bits
!= 0:
479 self
._validate
_bit
_class
_down
(val
.sources
[i
], src_type_bits
)
481 self
._validate
_bit
_class
_down
(val
.sources
[i
], val
.common_class
)
483 _optimization_ids
= itertools
.count()
485 condition_list
= ['true']
487 class SearchAndReplace(object):
488 def __init__(self
, transform
):
489 self
.id = next(_optimization_ids
)
491 search
= transform
[0]
492 replace
= transform
[1]
493 if len(transform
) > 2:
494 self
.condition
= transform
[2]
496 self
.condition
= 'true'
498 if self
.condition
not in condition_list
:
499 condition_list
.append(self
.condition
)
500 self
.condition_index
= condition_list
.index(self
.condition
)
503 if isinstance(search
, Expression
):
506 self
.search
= Expression(search
, "search{0}".format(self
.id), varset
)
510 if isinstance(replace
, Value
):
511 self
.replace
= replace
513 self
.replace
= Value
.create(replace
, "replace{0}".format(self
.id), varset
)
515 BitSizeValidator(varset
).validate(self
.search
, self
.replace
)
517 _algebraic_pass_template
= mako
.template
.Template("""
519 #include "nir_search.h"
520 #include "nir_search_helpers.h"
522 #ifndef NIR_OPT_ALGEBRAIC_STRUCT_DEFS
523 #define NIR_OPT_ALGEBRAIC_STRUCT_DEFS
526 const nir_search_expression *search;
527 const nir_search_value *replace;
528 unsigned condition_offset;
533 % for (opcode, xform_list) in xform_dict.items():
534 % for xform in xform_list:
535 ${xform.search.render()}
536 ${xform.replace.render()}
539 static const struct transform ${pass_name}_${opcode}_xforms[] = {
540 % for xform in xform_list:
541 { &${xform.search.name}, ${xform.replace.c_ptr}, ${xform.condition_index} },
547 ${pass_name}_block(nir_block *block, const bool *condition_flags,
550 bool progress = false;
552 nir_foreach_instr_reverse_safe(instr, block) {
553 if (instr->type != nir_instr_type_alu)
556 nir_alu_instr *alu = nir_instr_as_alu(instr);
557 if (!alu->dest.dest.is_ssa)
561 % for opcode in xform_dict.keys():
562 case nir_op_${opcode}:
563 for (unsigned i = 0; i < ARRAY_SIZE(${pass_name}_${opcode}_xforms); i++) {
564 const struct transform *xform = &${pass_name}_${opcode}_xforms[i];
565 if (condition_flags[xform->condition_offset] &&
566 nir_replace_instr(alu, xform->search, xform->replace,
583 ${pass_name}_impl(nir_function_impl *impl, const bool *condition_flags)
585 void *mem_ctx = ralloc_parent(impl);
586 bool progress = false;
588 nir_foreach_block_reverse(block, impl) {
589 progress |= ${pass_name}_block(block, condition_flags, mem_ctx);
593 nir_metadata_preserve(impl, nir_metadata_block_index |
594 nir_metadata_dominance);
601 ${pass_name}(nir_shader *shader)
603 bool progress = false;
604 bool condition_flags[${len(condition_list)}];
605 const nir_shader_compiler_options *options = shader->options;
608 % for index, condition in enumerate(condition_list):
609 condition_flags[${index}] = ${condition};
612 nir_foreach_function(function, shader) {
614 progress |= ${pass_name}_impl(function->impl, condition_flags);
621 class AlgebraicPass(object):
622 def __init__(self
, pass_name
, transforms
):
623 self
.xform_dict
= OrderedDict()
624 self
.pass_name
= pass_name
628 for xform
in transforms
:
629 if not isinstance(xform
, SearchAndReplace
):
631 xform
= SearchAndReplace(xform
)
633 print("Failed to parse transformation:", file=sys
.stderr
)
634 print(" " + str(xform
), file=sys
.stderr
)
635 traceback
.print_exc(file=sys
.stderr
)
636 print('', file=sys
.stderr
)
640 if xform
.search
.opcode
not in self
.xform_dict
:
641 self
.xform_dict
[xform
.search
.opcode
] = []
643 self
.xform_dict
[xform
.search
.opcode
].append(xform
)
649 return _algebraic_pass_template
.render(pass_name
=self
.pass_name
,
650 xform_dict
=self
.xform_dict
,
651 condition_list
=condition_list
)