2014-11-14 17:47:56 -08:00
|
|
|
#
|
|
|
|
|
# Copyright (C) 2014 Intel Corporation
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
# copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
# to deal in the Software without restriction, including without limitation
|
|
|
|
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
# and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
# Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice (including the next
|
|
|
|
|
# paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
# Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
# IN THE SOFTWARE.
|
|
|
|
|
#
|
|
|
|
|
# Authors:
|
|
|
|
|
# Jason Ekstrand (jason@jlekstrand.net)
|
|
|
|
|
|
2016-04-25 11:36:08 -07:00
|
|
|
from __future__ import print_function
|
2016-04-25 12:23:38 -07:00
|
|
|
import ast
|
2018-11-07 14:32:19 -06:00
|
|
|
from collections import defaultdict
|
2014-11-14 17:47:56 -08:00
|
|
|
import itertools
|
|
|
|
|
import struct
|
|
|
|
|
import sys
|
|
|
|
|
import mako.template
|
2015-01-28 16:42:20 -08:00
|
|
|
import re
|
2016-04-25 11:36:08 -07:00
|
|
|
import traceback
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2018-11-07 15:40:02 -06:00
|
|
|
from nir_opcodes import opcodes, type_sizes
|
|
|
|
|
|
|
|
|
|
# These opcodes are only employed by nir_search. This provides a mapping from
|
|
|
|
|
# opcode to destination type.
|
|
|
|
|
conv_opcode_types = {
|
|
|
|
|
'i2f' : 'float',
|
|
|
|
|
'u2f' : 'float',
|
|
|
|
|
'f2f' : 'float',
|
|
|
|
|
'f2u' : 'uint',
|
|
|
|
|
'f2i' : 'int',
|
|
|
|
|
'u2u' : 'uint',
|
|
|
|
|
'i2i' : 'int',
|
2018-11-07 13:43:40 -06:00
|
|
|
'b2f' : 'float',
|
|
|
|
|
'b2i' : 'int',
|
|
|
|
|
'i2b' : 'bool',
|
|
|
|
|
'f2b' : 'bool',
|
2018-11-07 15:40:02 -06:00
|
|
|
}
|
2016-04-25 20:58:47 -07:00
|
|
|
|
2018-08-09 10:27:21 +02:00
|
|
|
if sys.version_info < (3, 0):
|
2018-08-09 10:27:22 +02:00
|
|
|
integer_types = (int, long)
|
2018-08-09 10:27:21 +02:00
|
|
|
string_type = unicode
|
|
|
|
|
|
|
|
|
|
else:
|
2018-08-09 10:27:22 +02:00
|
|
|
integer_types = (int, )
|
2018-08-09 10:27:21 +02:00
|
|
|
string_type = str
|
|
|
|
|
|
2016-04-25 20:58:47 -07:00
|
|
|
_type_re = re.compile(r"(?P<type>int|uint|bool|float)?(?P<bits>\d+)?")
|
|
|
|
|
|
|
|
|
|
def type_bits(type_str):
|
|
|
|
|
m = _type_re.match(type_str)
|
|
|
|
|
assert m.group('type')
|
|
|
|
|
|
|
|
|
|
if m.group('bits') is None:
|
|
|
|
|
return 0
|
|
|
|
|
else:
|
|
|
|
|
return int(m.group('bits'))
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
# Represents a set of variables, each with a unique id
|
|
|
|
|
class VarSet(object):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.names = {}
|
|
|
|
|
self.ids = itertools.count()
|
2015-01-29 11:45:31 -08:00
|
|
|
self.immutable = False;
|
2014-11-14 17:47:56 -08:00
|
|
|
|
|
|
|
|
def __getitem__(self, name):
|
|
|
|
|
if name not in self.names:
|
2015-01-29 11:45:31 -08:00
|
|
|
assert not self.immutable, "Unknown replacement variable: " + name
|
2018-07-05 15:17:39 +02:00
|
|
|
self.names[name] = next(self.ids)
|
2014-11-14 17:47:56 -08:00
|
|
|
|
|
|
|
|
return self.names[name]
|
|
|
|
|
|
2015-01-29 11:45:31 -08:00
|
|
|
def lock(self):
|
|
|
|
|
self.immutable = True
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
class Value(object):
|
|
|
|
|
@staticmethod
|
|
|
|
|
def create(val, name_base, varset):
|
2018-08-09 10:27:21 +02:00
|
|
|
if isinstance(val, bytes):
|
|
|
|
|
val = val.decode('utf-8')
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
if isinstance(val, tuple):
|
|
|
|
|
return Expression(val, name_base, varset)
|
|
|
|
|
elif isinstance(val, Expression):
|
|
|
|
|
return val
|
2018-08-09 10:27:21 +02:00
|
|
|
elif isinstance(val, string_type):
|
2014-11-14 17:47:56 -08:00
|
|
|
return Variable(val, name_base, varset)
|
2018-08-09 10:27:22 +02:00
|
|
|
elif isinstance(val, (bool, float) + integer_types):
|
2014-11-14 17:47:56 -08:00
|
|
|
return Constant(val, name_base)
|
|
|
|
|
|
|
|
|
|
__template = mako.template.Template("""
|
|
|
|
|
static const ${val.c_type} ${val.name} = {
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
{ ${val.type_enum}, ${val.c_bit_size} },
|
2014-11-14 17:47:56 -08:00
|
|
|
% if isinstance(val, Constant):
|
python: Don't abuse hex()
The hex() builtin returns a string containing the hexa-decimal
representation of an integer.
When the argument is not an integer, then the function calls that
object's __hex__() method, if one is defined. That method is supposed to
return a string.
While that's not explicitly documented, that string is supposed to be a
valid hexa-decimal representation for a number. Python 2 doesn't enforce
this though, which is why we got away with returning things like
'NIR_TRUE' which are not numbers.
In Python 3, the hex() builtin instead calls an object's __index__()
method, which itself must return an integer. That integer is then
automatically converted to a string with its hexa-decimal representation
by the rest of the hex() function.
As a result, we really can't make this compatible with Python 3 as it
is.
The solution is to stop using the hex() builtin, and instead use a hex()
object method, which can return whatever we want, in Python 2 and 3.
Signed-off-by: Mathieu Bridon <bochecha@daitauha.fr>
Reviewed-by: Eric Engestrom <eric.engestrom@intel.com>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-06-17 17:53:16 +02:00
|
|
|
${val.type()}, { ${val.hex()} /* ${val.value} */ },
|
2014-11-14 17:47:56 -08:00
|
|
|
% elif isinstance(val, Variable):
|
|
|
|
|
${val.index}, /* ${val.var_name} */
|
2015-01-28 16:42:20 -08:00
|
|
|
${'true' if val.is_constant else 'false'},
|
2015-08-14 11:45:30 -07:00
|
|
|
${val.type() or 'nir_type_invalid' },
|
2016-05-07 13:01:24 -04:00
|
|
|
${val.cond if val.cond else 'NULL'},
|
2014-11-14 17:47:56 -08:00
|
|
|
% elif isinstance(val, Expression):
|
2016-03-17 11:04:49 -07:00
|
|
|
${'true' if val.inexact else 'false'},
|
nir/search: Search for all combinations of commutative ops
Consider the following search expression and NIR sequence:
('iadd', ('imul', a, b), b)
ssa_2 = imul ssa_0, ssa_1
ssa_3 = iadd ssa_2, ssa_0
The current algorithm is greedy and, the moment the imul finds a match,
it commits those variable names and returns success. In the above
example, it maps a -> ssa_0 and b -> ssa_1. When we then try to match
the iadd, it sees that ssa_0 is not b and fails to match. The iadd
match will attempt to flip itself and try again (which won't work) but
it cannot ask the imul to try a flipped match.
This commit instead counts the number of commutative ops in each
expression and assigns an index to each. It then does a loop and loops
over the full combinatorial matrix of commutative operations. In order
to keep things sane, we limit it to at most 4 commutative operations (16
combinations). There is only one optimization in opt_algebraic that
goes over this limit and it's the bitfieldReverse detection for some UE4
demo.
Shader-db results on Kaby Lake:
total instructions in shared programs: 15310125 -> 15302469 (-0.05%)
instructions in affected programs: 1797123 -> 1789467 (-0.43%)
helped: 6751
HURT: 2264
total cycles in shared programs: 357346617 -> 357202526 (-0.04%)
cycles in affected programs: 15931005 -> 15786914 (-0.90%)
helped: 6024
HURT: 3436
total loops in shared programs: 4360 -> 4360 (0.00%)
loops in affected programs: 0 -> 0
helped: 0
HURT: 0
total spills in shared programs: 23675 -> 23666 (-0.04%)
spills in affected programs: 235 -> 226 (-3.83%)
helped: 5
HURT: 1
total fills in shared programs: 32040 -> 32032 (-0.02%)
fills in affected programs: 190 -> 182 (-4.21%)
helped: 6
HURT: 2
LOST: 18
GAINED: 5
Reviewed-by: Thomas Helland <thomashelland90@gmail.com>
2019-03-22 17:45:29 -05:00
|
|
|
${val.comm_expr_idx}, ${val.comm_exprs},
|
2018-11-07 15:40:02 -06:00
|
|
|
${val.c_opcode()},
|
2014-11-14 17:47:56 -08:00
|
|
|
{ ${', '.join(src.c_ptr for src in val.sources)} },
|
2017-01-10 15:47:31 +11:00
|
|
|
${val.cond if val.cond else 'NULL'},
|
2014-11-14 17:47:56 -08:00
|
|
|
% endif
|
|
|
|
|
};""")
|
|
|
|
|
|
2018-10-19 14:01:31 -05:00
|
|
|
def __init__(self, val, name, type_str):
|
|
|
|
|
self.in_val = str(val)
|
2014-11-14 17:47:56 -08:00
|
|
|
self.name = name
|
|
|
|
|
self.type_str = type_str
|
|
|
|
|
|
2018-10-19 14:01:31 -05:00
|
|
|
def __str__(self):
|
|
|
|
|
return self.in_val
|
|
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
def get_bit_size(self):
|
|
|
|
|
"""Get the physical bit-size that has been chosen for this value, or if
|
|
|
|
|
there is none, the canonical value which currently represents this
|
|
|
|
|
bit-size class. Variables will be preferred, i.e. if there are any
|
|
|
|
|
variables in the equivalence class, the canonical value will be a
|
|
|
|
|
variable. We do this since we'll need to know which variable each value
|
|
|
|
|
is equivalent to when constructing the replacement expression. This is
|
|
|
|
|
the "find" part of the union-find algorithm.
|
|
|
|
|
"""
|
|
|
|
|
bit_size = self
|
|
|
|
|
|
|
|
|
|
while isinstance(bit_size, Value):
|
|
|
|
|
if bit_size._bit_size is None:
|
|
|
|
|
break
|
|
|
|
|
bit_size = bit_size._bit_size
|
|
|
|
|
|
|
|
|
|
if bit_size is not self:
|
|
|
|
|
self._bit_size = bit_size
|
|
|
|
|
return bit_size
|
|
|
|
|
|
|
|
|
|
def set_bit_size(self, other):
|
|
|
|
|
"""Make self.get_bit_size() return what other.get_bit_size() return
|
|
|
|
|
before calling this, or just "other" if it's a concrete bit-size. This is
|
|
|
|
|
the "union" part of the union-find algorithm.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self_bit_size = self.get_bit_size()
|
|
|
|
|
other_bit_size = other if isinstance(other, int) else other.get_bit_size()
|
|
|
|
|
|
|
|
|
|
if self_bit_size == other_bit_size:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self_bit_size._bit_size = other_bit_size
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
@property
|
|
|
|
|
def type_enum(self):
|
|
|
|
|
return "nir_search_value_" + self.type_str
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def c_type(self):
|
|
|
|
|
return "nir_search_" + self.type_str
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def c_ptr(self):
|
|
|
|
|
return "&{0}.value".format(self.name)
|
|
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
@property
|
|
|
|
|
def c_bit_size(self):
|
|
|
|
|
bit_size = self.get_bit_size()
|
|
|
|
|
if isinstance(bit_size, int):
|
|
|
|
|
return bit_size
|
|
|
|
|
elif isinstance(bit_size, Variable):
|
|
|
|
|
return -bit_size.index - 1
|
|
|
|
|
else:
|
|
|
|
|
# If the bit-size class is neither a variable, nor an actual bit-size, then
|
|
|
|
|
# - If it's in the search expression, we don't need to check anything
|
|
|
|
|
# - If it's in the replace expression, either it's ambiguous (in which
|
|
|
|
|
# case we'd reject it), or it equals the bit-size of the search value
|
|
|
|
|
# We represent these cases with a 0 bit-size.
|
|
|
|
|
return 0
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
def render(self):
|
|
|
|
|
return self.__template.render(val=self,
|
|
|
|
|
Constant=Constant,
|
|
|
|
|
Variable=Variable,
|
|
|
|
|
Expression=Expression)
|
|
|
|
|
|
2016-05-07 13:01:24 -04:00
|
|
|
_constant_re = re.compile(r"(?P<value>[^@\(]+)(?:@(?P<bits>\d+))?")
|
2016-04-25 12:23:38 -07:00
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
class Constant(Value):
|
|
|
|
|
def __init__(self, val, name):
|
2018-10-19 14:01:31 -05:00
|
|
|
Value.__init__(self, val, name, "constant")
|
2016-04-25 12:23:38 -07:00
|
|
|
|
|
|
|
|
if isinstance(val, (str)):
|
|
|
|
|
m = _constant_re.match(val)
|
|
|
|
|
self.value = ast.literal_eval(m.group('value'))
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
self._bit_size = int(m.group('bits')) if m.group('bits') else None
|
2016-04-25 12:23:38 -07:00
|
|
|
else:
|
|
|
|
|
self.value = val
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
self._bit_size = None
|
2016-04-25 12:23:38 -07:00
|
|
|
|
|
|
|
|
if isinstance(self.value, bool):
|
2018-12-05 21:36:10 -06:00
|
|
|
assert self._bit_size is None or self._bit_size == 1
|
|
|
|
|
self._bit_size = 1
|
2014-11-14 17:47:56 -08:00
|
|
|
|
python: Don't abuse hex()
The hex() builtin returns a string containing the hexa-decimal
representation of an integer.
When the argument is not an integer, then the function calls that
object's __hex__() method, if one is defined. That method is supposed to
return a string.
While that's not explicitly documented, that string is supposed to be a
valid hexa-decimal representation for a number. Python 2 doesn't enforce
this though, which is why we got away with returning things like
'NIR_TRUE' which are not numbers.
In Python 3, the hex() builtin instead calls an object's __index__()
method, which itself must return an integer. That integer is then
automatically converted to a string with its hexa-decimal representation
by the rest of the hex() function.
As a result, we really can't make this compatible with Python 3 as it
is.
The solution is to stop using the hex() builtin, and instead use a hex()
object method, which can return whatever we want, in Python 2 and 3.
Signed-off-by: Mathieu Bridon <bochecha@daitauha.fr>
Reviewed-by: Eric Engestrom <eric.engestrom@intel.com>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-06-17 17:53:16 +02:00
|
|
|
def hex(self):
|
2014-11-14 17:47:56 -08:00
|
|
|
if isinstance(self.value, (bool)):
|
|
|
|
|
return 'NIR_TRUE' if self.value else 'NIR_FALSE'
|
2018-08-09 10:27:22 +02:00
|
|
|
if isinstance(self.value, integer_types):
|
2016-02-01 16:35:41 -08:00
|
|
|
return hex(self.value)
|
2014-11-14 17:47:56 -08:00
|
|
|
elif isinstance(self.value, float):
|
2018-06-25 18:31:01 +02:00
|
|
|
i = struct.unpack('Q', struct.pack('d', self.value))[0]
|
|
|
|
|
h = hex(i)
|
|
|
|
|
|
|
|
|
|
# On Python 2 this 'L' suffix is automatically added, but not on Python 3
|
|
|
|
|
# Adding it explicitly makes the generated file identical, regardless
|
|
|
|
|
# of the Python version running this script.
|
|
|
|
|
if h[-1] != 'L' and i > sys.maxsize:
|
|
|
|
|
h += 'L'
|
|
|
|
|
|
|
|
|
|
return h
|
2014-11-14 17:47:56 -08:00
|
|
|
else:
|
|
|
|
|
assert False
|
|
|
|
|
|
2015-08-14 11:45:30 -07:00
|
|
|
def type(self):
|
|
|
|
|
if isinstance(self.value, (bool)):
|
2018-10-18 22:31:08 -05:00
|
|
|
return "nir_type_bool"
|
2018-08-09 10:27:22 +02:00
|
|
|
elif isinstance(self.value, integer_types):
|
2015-08-14 11:45:30 -07:00
|
|
|
return "nir_type_int"
|
|
|
|
|
elif isinstance(self.value, float):
|
|
|
|
|
return "nir_type_float"
|
|
|
|
|
|
2016-04-25 12:23:38 -07:00
|
|
|
_var_name_re = re.compile(r"(?P<const>#)?(?P<name>\w+)"
|
2016-05-07 13:01:24 -04:00
|
|
|
r"(?:@(?P<type>int|uint|bool|float)?(?P<bits>\d+)?)?"
|
|
|
|
|
r"(?P<cond>\([^\)]+\))?")
|
2015-01-28 16:42:20 -08:00
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
class Variable(Value):
|
|
|
|
|
def __init__(self, val, name, varset):
|
2018-10-19 14:01:31 -05:00
|
|
|
Value.__init__(self, val, name, "variable")
|
2015-01-28 16:42:20 -08:00
|
|
|
|
|
|
|
|
m = _var_name_re.match(val)
|
|
|
|
|
assert m and m.group('name') is not None
|
|
|
|
|
|
|
|
|
|
self.var_name = m.group('name')
|
2018-12-18 13:28:22 -08:00
|
|
|
|
|
|
|
|
# Prevent common cases where someone puts quotes around a literal
|
|
|
|
|
# constant. If we want to support names that have numeric or
|
|
|
|
|
# punctuation characters, we can me the first assertion more flexible.
|
|
|
|
|
assert self.var_name.isalpha()
|
|
|
|
|
assert self.var_name is not 'True'
|
|
|
|
|
assert self.var_name is not 'False'
|
|
|
|
|
|
2015-01-28 16:42:20 -08:00
|
|
|
self.is_constant = m.group('const') is not None
|
2016-05-07 13:01:24 -04:00
|
|
|
self.cond = m.group('cond')
|
2015-01-28 16:42:20 -08:00
|
|
|
self.required_type = m.group('type')
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
self._bit_size = int(m.group('bits')) if m.group('bits') else None
|
2016-04-25 12:23:38 -07:00
|
|
|
|
|
|
|
|
if self.required_type == 'bool':
|
2018-12-05 21:36:10 -06:00
|
|
|
if self._bit_size is not None:
|
|
|
|
|
assert self._bit_size in type_sizes(self.required_type)
|
|
|
|
|
else:
|
|
|
|
|
self._bit_size = 1
|
2015-01-28 16:42:20 -08:00
|
|
|
|
|
|
|
|
if self.required_type is not None:
|
2016-04-25 12:00:12 -07:00
|
|
|
assert self.required_type in ('float', 'bool', 'int', 'uint')
|
2015-01-28 16:42:20 -08:00
|
|
|
|
|
|
|
|
self.index = varset[self.var_name]
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2015-08-14 11:45:30 -07:00
|
|
|
def type(self):
|
|
|
|
|
if self.required_type == 'bool':
|
2018-10-18 22:31:08 -05:00
|
|
|
return "nir_type_bool"
|
2016-04-25 12:00:12 -07:00
|
|
|
elif self.required_type in ('int', 'uint'):
|
2015-08-14 11:45:30 -07:00
|
|
|
return "nir_type_int"
|
|
|
|
|
elif self.required_type == 'float':
|
|
|
|
|
return "nir_type_float"
|
|
|
|
|
|
2017-01-10 15:47:31 +11:00
|
|
|
_opcode_re = re.compile(r"(?P<inexact>~)?(?P<opcode>\w+)(?:@(?P<bits>\d+))?"
|
|
|
|
|
r"(?P<cond>\([^\)]+\))?")
|
2016-03-17 11:04:49 -07:00
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
class Expression(Value):
|
|
|
|
|
def __init__(self, expr, name_base, varset):
|
2018-10-19 14:01:31 -05:00
|
|
|
Value.__init__(self, expr, name_base, "expression")
|
2014-11-14 17:47:56 -08:00
|
|
|
assert isinstance(expr, tuple)
|
|
|
|
|
|
2016-03-17 11:04:49 -07:00
|
|
|
m = _opcode_re.match(expr[0])
|
|
|
|
|
assert m and m.group('opcode') is not None
|
|
|
|
|
|
|
|
|
|
self.opcode = m.group('opcode')
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
self._bit_size = int(m.group('bits')) if m.group('bits') else None
|
2016-03-17 11:04:49 -07:00
|
|
|
self.inexact = m.group('inexact') is not None
|
2017-01-10 15:47:31 +11:00
|
|
|
self.cond = m.group('cond')
|
2014-11-14 17:47:56 -08:00
|
|
|
self.sources = [ Value.create(src, "{0}_{1}".format(name_base, i), varset)
|
|
|
|
|
for (i, src) in enumerate(expr[1:]) ]
|
|
|
|
|
|
2018-11-07 15:40:02 -06:00
|
|
|
if self.opcode in conv_opcode_types:
|
|
|
|
|
assert self._bit_size is None, \
|
|
|
|
|
'Expression cannot use an unsized conversion opcode with ' \
|
|
|
|
|
'an explicit size; that\'s silly.'
|
|
|
|
|
|
nir/search: Search for all combinations of commutative ops
Consider the following search expression and NIR sequence:
('iadd', ('imul', a, b), b)
ssa_2 = imul ssa_0, ssa_1
ssa_3 = iadd ssa_2, ssa_0
The current algorithm is greedy and, the moment the imul finds a match,
it commits those variable names and returns success. In the above
example, it maps a -> ssa_0 and b -> ssa_1. When we then try to match
the iadd, it sees that ssa_0 is not b and fails to match. The iadd
match will attempt to flip itself and try again (which won't work) but
it cannot ask the imul to try a flipped match.
This commit instead counts the number of commutative ops in each
expression and assigns an index to each. It then does a loop and loops
over the full combinatorial matrix of commutative operations. In order
to keep things sane, we limit it to at most 4 commutative operations (16
combinations). There is only one optimization in opt_algebraic that
goes over this limit and it's the bitfieldReverse detection for some UE4
demo.
Shader-db results on Kaby Lake:
total instructions in shared programs: 15310125 -> 15302469 (-0.05%)
instructions in affected programs: 1797123 -> 1789467 (-0.43%)
helped: 6751
HURT: 2264
total cycles in shared programs: 357346617 -> 357202526 (-0.04%)
cycles in affected programs: 15931005 -> 15786914 (-0.90%)
helped: 6024
HURT: 3436
total loops in shared programs: 4360 -> 4360 (0.00%)
loops in affected programs: 0 -> 0
helped: 0
HURT: 0
total spills in shared programs: 23675 -> 23666 (-0.04%)
spills in affected programs: 235 -> 226 (-3.83%)
helped: 5
HURT: 1
total fills in shared programs: 32040 -> 32032 (-0.02%)
fills in affected programs: 190 -> 182 (-4.21%)
helped: 6
HURT: 2
LOST: 18
GAINED: 5
Reviewed-by: Thomas Helland <thomashelland90@gmail.com>
2019-03-22 17:45:29 -05:00
|
|
|
self.__index_comm_exprs(0)
|
|
|
|
|
|
|
|
|
|
def __index_comm_exprs(self, base_idx):
|
|
|
|
|
"""Recursively count and index commutative expressions
|
|
|
|
|
"""
|
|
|
|
|
self.comm_exprs = 0
|
|
|
|
|
if self.opcode not in conv_opcode_types and \
|
|
|
|
|
"commutative" in opcodes[self.opcode].algebraic_properties:
|
|
|
|
|
self.comm_expr_idx = base_idx
|
|
|
|
|
self.comm_exprs += 1
|
|
|
|
|
else:
|
|
|
|
|
self.comm_expr_idx = -1
|
|
|
|
|
|
|
|
|
|
for s in self.sources:
|
|
|
|
|
if isinstance(s, Expression):
|
|
|
|
|
s.__index_comm_exprs(base_idx + self.comm_exprs)
|
|
|
|
|
self.comm_exprs += s.comm_exprs
|
|
|
|
|
|
|
|
|
|
return self.comm_exprs
|
2018-11-07 15:40:02 -06:00
|
|
|
|
|
|
|
|
def c_opcode(self):
|
|
|
|
|
if self.opcode in conv_opcode_types:
|
|
|
|
|
return 'nir_search_op_' + self.opcode
|
|
|
|
|
else:
|
|
|
|
|
return 'nir_op_' + self.opcode
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
def render(self):
|
|
|
|
|
srcs = "\n".join(src.render() for src in self.sources)
|
|
|
|
|
return srcs + super(Expression, self).render()
|
|
|
|
|
|
2016-04-25 20:58:47 -07:00
|
|
|
class BitSizeValidator(object):
|
|
|
|
|
"""A class for validating bit sizes of expressions.
|
|
|
|
|
|
|
|
|
|
NIR supports multiple bit-sizes on expressions in order to handle things
|
|
|
|
|
such as fp64. The source and destination of every ALU operation is
|
|
|
|
|
assigned a type and that type may or may not specify a bit size. Sources
|
|
|
|
|
and destinations whose type does not specify a bit size are considered
|
|
|
|
|
"unsized" and automatically take on the bit size of the corresponding
|
|
|
|
|
register or SSA value. NIR has two simple rules for bit sizes that are
|
|
|
|
|
validated by nir_validator:
|
|
|
|
|
|
|
|
|
|
1) A given SSA def or register has a single bit size that is respected by
|
|
|
|
|
everything that reads from it or writes to it.
|
|
|
|
|
|
|
|
|
|
2) The bit sizes of all unsized inputs/outputs on any given ALU
|
|
|
|
|
instruction must match. They need not match the sized inputs or
|
|
|
|
|
outputs but they must match each other.
|
|
|
|
|
|
|
|
|
|
In order to keep nir_algebraic relatively simple and easy-to-use,
|
|
|
|
|
nir_search supports a type of bit-size inference based on the two rules
|
|
|
|
|
above. This is similar to type inference in many common programming
|
|
|
|
|
languages. If, for instance, you are constructing an add operation and you
|
|
|
|
|
know the second source is 16-bit, then you know that the other source and
|
|
|
|
|
the destination must also be 16-bit. There are, however, cases where this
|
|
|
|
|
inference can be ambiguous or contradictory. Consider, for instance, the
|
|
|
|
|
following transformation:
|
|
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
(('usub_borrow', a, b), ('b2i@32', ('ult', a, b)))
|
2016-04-25 20:58:47 -07:00
|
|
|
|
|
|
|
|
This transformation can potentially cause a problem because usub_borrow is
|
|
|
|
|
well-defined for any bit-size of integer. However, b2i always generates a
|
|
|
|
|
32-bit result so it could end up replacing a 64-bit expression with one
|
|
|
|
|
that takes two 64-bit values and produces a 32-bit value. As another
|
|
|
|
|
example, consider this expression:
|
|
|
|
|
|
|
|
|
|
(('bcsel', a, b, 0), ('iand', a, b))
|
|
|
|
|
|
|
|
|
|
In this case, in the search expression a must be 32-bit but b can
|
|
|
|
|
potentially have any bit size. If we had a 64-bit b value, we would end up
|
|
|
|
|
trying to and a 32-bit value with a 64-bit value which would be invalid
|
|
|
|
|
|
|
|
|
|
This class solves that problem by providing a validation layer that proves
|
|
|
|
|
that a given search-and-replace operation is 100% well-defined before we
|
|
|
|
|
generate any code. This ensures that bugs are caught at compile time
|
|
|
|
|
rather than at run time.
|
|
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
Each value maintains a "bit-size class", which is either an actual bit size
|
|
|
|
|
or an equivalence class with other values that must have the same bit size.
|
|
|
|
|
The validator works by combining bit-size classes with each other according
|
|
|
|
|
to the NIR rules outlined above, checking that there are no inconsistencies.
|
|
|
|
|
When doing this for the replacement expression, we make sure to never change
|
|
|
|
|
the equivalence class of any of the search values. We could make the example
|
|
|
|
|
transforms above work by doing some extra run-time checking of the search
|
|
|
|
|
expression, but we make the user specify those constraints themselves, to
|
|
|
|
|
avoid any surprises. Since the replacement bitsizes can only be connected to
|
|
|
|
|
the source bitsize via variables (variables must have the same bitsize in
|
|
|
|
|
the source and replacment expressions) or the roots of the expression (the
|
|
|
|
|
replacement expression must produce the same bit size as the search
|
|
|
|
|
expression), we prevent merging a variable with anything when processing the
|
|
|
|
|
replacement expression, or specializing the search bitsize
|
|
|
|
|
with anything. The former prevents
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
(('bcsel', a, b, 0), ('iand', a, b))
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
from being allowed, since we'd have to merge the bitsizes for a and b due to
|
|
|
|
|
the 'iand', while the latter prevents
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
(('usub_borrow', a, b), ('b2i@32', ('ult', a, b)))
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
from being allowed, since the search expression has the bit size of a and b,
|
|
|
|
|
which can't be specialized to 32 which is the bitsize of the replace
|
|
|
|
|
expression. It also prevents something like:
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
(('b2i', ('i2b', a)), ('ineq', a, 0))
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
since the bitsize of 'b2i', which can be anything, can't be specialized to
|
|
|
|
|
the bitsize of a.
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
After doing all this, we check that every subexpression of the replacement
|
|
|
|
|
was assigned a constant bitsize, the bitsize of a variable, or the bitsize
|
|
|
|
|
of the search expresssion, since those are the things that are known when
|
|
|
|
|
constructing the replacement expresssion. Finally, we record the bitsize
|
|
|
|
|
needed in nir_search_value so that we know what to do when building the
|
|
|
|
|
replacement expression.
|
|
|
|
|
"""
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
def __init__(self, varset):
|
|
|
|
|
self._var_classes = [None] * len(varset.names)
|
|
|
|
|
|
|
|
|
|
def compare_bitsizes(self, a, b):
|
|
|
|
|
"""Determines which bitsize class is a specialization of the other, or
|
|
|
|
|
whether neither is. When we merge two different bitsizes, the
|
|
|
|
|
less-specialized bitsize always points to the more-specialized one, so
|
|
|
|
|
that calling get_bit_size() always gets you the most specialized bitsize.
|
|
|
|
|
The specialization partial order is given by:
|
|
|
|
|
- Physical bitsizes are always the most specialized, and a different
|
|
|
|
|
bitsize can never specialize another.
|
|
|
|
|
- In the search expression, variables can always be specialized to each
|
|
|
|
|
other and to physical bitsizes. In the replace expression, we disallow
|
|
|
|
|
this to avoid adding extra constraints to the search expression that
|
|
|
|
|
the user didn't specify.
|
|
|
|
|
- Expressions and constants without a bitsize can always be specialized to
|
|
|
|
|
each other and variables, but not the other way around.
|
|
|
|
|
|
|
|
|
|
We return -1 if a <= b (b can be specialized to a), 0 if a = b, 1 if a >= b,
|
|
|
|
|
and None if they are not comparable (neither a <= b nor b <= a).
|
|
|
|
|
"""
|
|
|
|
|
if isinstance(a, int):
|
|
|
|
|
if isinstance(b, int):
|
|
|
|
|
return 0 if a == b else None
|
|
|
|
|
elif isinstance(b, Variable):
|
|
|
|
|
return -1 if self.is_search else None
|
|
|
|
|
else:
|
|
|
|
|
return -1
|
|
|
|
|
elif isinstance(a, Variable):
|
|
|
|
|
if isinstance(b, int):
|
|
|
|
|
return 1 if self.is_search else None
|
|
|
|
|
elif isinstance(b, Variable):
|
|
|
|
|
return 0 if self.is_search or a.index == b.index else None
|
|
|
|
|
else:
|
|
|
|
|
return -1
|
|
|
|
|
else:
|
|
|
|
|
if isinstance(b, int):
|
|
|
|
|
return 1
|
|
|
|
|
elif isinstance(b, Variable):
|
|
|
|
|
return 1
|
|
|
|
|
else:
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
def unify_bit_size(self, a, b, error_msg):
|
|
|
|
|
"""Record that a must have the same bit-size as b. If both
|
|
|
|
|
have been assigned conflicting physical bit-sizes, call "error_msg" with
|
|
|
|
|
the bit-sizes of self and other to get a message and raise an error.
|
|
|
|
|
In the replace expression, disallow merging variables with other
|
|
|
|
|
variables and physical bit-sizes as well.
|
|
|
|
|
"""
|
|
|
|
|
a_bit_size = a.get_bit_size()
|
|
|
|
|
b_bit_size = b if isinstance(b, int) else b.get_bit_size()
|
|
|
|
|
|
|
|
|
|
cmp_result = self.compare_bitsizes(a_bit_size, b_bit_size)
|
|
|
|
|
|
|
|
|
|
assert cmp_result is not None, \
|
|
|
|
|
error_msg(a_bit_size, b_bit_size)
|
|
|
|
|
|
|
|
|
|
if cmp_result < 0:
|
|
|
|
|
b_bit_size.set_bit_size(a)
|
|
|
|
|
elif not isinstance(a_bit_size, int):
|
|
|
|
|
a_bit_size.set_bit_size(b)
|
|
|
|
|
|
|
|
|
|
def merge_variables(self, val):
|
|
|
|
|
"""Perform the first part of type inference by merging all the different
|
|
|
|
|
uses of the same variable. We always do this as if we're in the search
|
|
|
|
|
expression, even if we're actually not, since otherwise we'd get errors
|
|
|
|
|
if the search expression specified some constraint but the replace
|
|
|
|
|
expression didn't, because we'd be merging a variable and a constant.
|
|
|
|
|
"""
|
|
|
|
|
if isinstance(val, Variable):
|
|
|
|
|
if self._var_classes[val.index] is None:
|
|
|
|
|
self._var_classes[val.index] = val
|
|
|
|
|
else:
|
|
|
|
|
other = self._var_classes[val.index]
|
|
|
|
|
self.unify_bit_size(other, val,
|
|
|
|
|
lambda other_bit_size, bit_size:
|
|
|
|
|
'Variable {} has conflicting bit size requirements: ' \
|
|
|
|
|
'it must have bit size {} and {}'.format(
|
|
|
|
|
val.var_name, other_bit_size, bit_size))
|
2016-04-25 20:58:47 -07:00
|
|
|
elif isinstance(val, Expression):
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
for src in val.sources:
|
|
|
|
|
self.merge_variables(src)
|
|
|
|
|
|
|
|
|
|
def validate_value(self, val):
|
|
|
|
|
"""Validate the an expression by performing classic Hindley-Milner
|
|
|
|
|
type inference on bitsizes. This will detect if there are any conflicting
|
|
|
|
|
requirements, and unify variables so that we know which variables must
|
|
|
|
|
have the same bitsize. If we're operating on the replace expression, we
|
|
|
|
|
will refuse to merge different variables together or merge a variable
|
|
|
|
|
with a constant, in order to prevent surprises due to rules unexpectedly
|
|
|
|
|
not matching at runtime.
|
|
|
|
|
"""
|
|
|
|
|
if not isinstance(val, Expression):
|
|
|
|
|
return
|
|
|
|
|
|
2018-11-07 15:40:02 -06:00
|
|
|
# Generic conversion ops are special in that they have a single unsized
|
|
|
|
|
# source and an unsized destination and the two don't have to match.
|
|
|
|
|
# This means there's no validation or unioning to do here besides the
|
|
|
|
|
# len(val.sources) check.
|
|
|
|
|
if val.opcode in conv_opcode_types:
|
|
|
|
|
assert len(val.sources) == 1, \
|
|
|
|
|
"Expression {} has {} sources, expected 1".format(
|
|
|
|
|
val, len(val.sources))
|
|
|
|
|
self.validate_value(val.sources[0])
|
|
|
|
|
return
|
|
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
nir_op = opcodes[val.opcode]
|
|
|
|
|
assert len(val.sources) == nir_op.num_inputs, \
|
|
|
|
|
"Expression {} has {} sources, expected {}".format(
|
|
|
|
|
val, len(val.sources), nir_op.num_inputs)
|
|
|
|
|
|
|
|
|
|
for src in val.sources:
|
|
|
|
|
self.validate_value(src)
|
|
|
|
|
|
|
|
|
|
dst_type_bits = type_bits(nir_op.output_type)
|
|
|
|
|
|
|
|
|
|
# First, unify all the sources. That way, an error coming up because two
|
|
|
|
|
# sources have an incompatible bit-size won't produce an error message
|
|
|
|
|
# involving the destination.
|
|
|
|
|
first_unsized_src = None
|
|
|
|
|
for src_type, src in zip(nir_op.input_types, val.sources):
|
|
|
|
|
src_type_bits = type_bits(src_type)
|
|
|
|
|
if src_type_bits == 0:
|
|
|
|
|
if first_unsized_src is None:
|
|
|
|
|
first_unsized_src = src
|
2016-04-25 20:58:47 -07:00
|
|
|
continue
|
|
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
if self.is_search:
|
|
|
|
|
self.unify_bit_size(first_unsized_src, src,
|
|
|
|
|
lambda first_unsized_src_bit_size, src_bit_size:
|
|
|
|
|
'Source {} of {} must have bit size {}, while source {} ' \
|
|
|
|
|
'must have incompatible bit size {}'.format(
|
|
|
|
|
first_unsized_src, val, first_unsized_src_bit_size,
|
|
|
|
|
src, src_bit_size))
|
2016-04-25 20:58:47 -07:00
|
|
|
else:
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
self.unify_bit_size(first_unsized_src, src,
|
|
|
|
|
lambda first_unsized_src_bit_size, src_bit_size:
|
|
|
|
|
'Sources {} (bit size of {}) and {} (bit size of {}) ' \
|
|
|
|
|
'of {} may not have the same bit size when building the ' \
|
|
|
|
|
'replacement expression.'.format(
|
|
|
|
|
first_unsized_src, first_unsized_src_bit_size, src,
|
|
|
|
|
src_bit_size, val))
|
2016-04-25 20:58:47 -07:00
|
|
|
else:
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
if self.is_search:
|
|
|
|
|
self.unify_bit_size(src, src_type_bits,
|
|
|
|
|
lambda src_bit_size, unused:
|
|
|
|
|
'{} must have {} bits, but as a source of nir_op_{} '\
|
|
|
|
|
'it must have {} bits'.format(
|
|
|
|
|
src, src_bit_size, nir_op.name, src_type_bits))
|
|
|
|
|
else:
|
|
|
|
|
self.unify_bit_size(src, src_type_bits,
|
|
|
|
|
lambda src_bit_size, unused:
|
|
|
|
|
'{} has the bit size of {}, but as a source of ' \
|
|
|
|
|
'nir_op_{} it must have {} bits, which may not be the ' \
|
|
|
|
|
'same'.format(
|
|
|
|
|
src, src_bit_size, nir_op.name, src_type_bits))
|
|
|
|
|
|
|
|
|
|
if dst_type_bits == 0:
|
|
|
|
|
if first_unsized_src is not None:
|
|
|
|
|
if self.is_search:
|
|
|
|
|
self.unify_bit_size(val, first_unsized_src,
|
|
|
|
|
lambda val_bit_size, src_bit_size:
|
|
|
|
|
'{} must have the bit size of {}, while its source {} ' \
|
|
|
|
|
'must have incompatible bit size {}'.format(
|
|
|
|
|
val, val_bit_size, first_unsized_src, src_bit_size))
|
2016-04-25 20:58:47 -07:00
|
|
|
else:
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
self.unify_bit_size(val, first_unsized_src,
|
|
|
|
|
lambda val_bit_size, src_bit_size:
|
|
|
|
|
'{} must have {} bits, but its source {} ' \
|
|
|
|
|
'(bit size of {}) may not have that bit size ' \
|
|
|
|
|
'when building the replacement.'.format(
|
|
|
|
|
val, val_bit_size, first_unsized_src, src_bit_size))
|
|
|
|
|
else:
|
|
|
|
|
self.unify_bit_size(val, dst_type_bits,
|
|
|
|
|
lambda dst_bit_size, unused:
|
|
|
|
|
'{} must have {} bits, but as a destination of nir_op_{} ' \
|
|
|
|
|
'it must have {} bits'.format(
|
|
|
|
|
val, dst_bit_size, nir_op.name, dst_type_bits))
|
|
|
|
|
|
|
|
|
|
def validate_replace(self, val, search):
|
|
|
|
|
bit_size = val.get_bit_size()
|
|
|
|
|
assert isinstance(bit_size, int) or isinstance(bit_size, Variable) or \
|
|
|
|
|
bit_size == search.get_bit_size(), \
|
|
|
|
|
'Ambiguous bit size for replacement value {}: ' \
|
|
|
|
|
'it cannot be deduced from a variable, a fixed bit size ' \
|
|
|
|
|
'somewhere, or the search expression.'.format(val)
|
|
|
|
|
|
|
|
|
|
if isinstance(val, Expression):
|
|
|
|
|
for src in val.sources:
|
|
|
|
|
self.validate_replace(src, search)
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
def validate(self, search, replace):
|
|
|
|
|
self.is_search = True
|
|
|
|
|
self.merge_variables(search)
|
|
|
|
|
self.merge_variables(replace)
|
|
|
|
|
self.validate_value(search)
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
self.is_search = False
|
|
|
|
|
self.validate_value(replace)
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
# Check that search is always more specialized than replace. Note that
|
|
|
|
|
# we're doing this in replace mode, disallowing merging variables.
|
|
|
|
|
search_bit_size = search.get_bit_size()
|
|
|
|
|
replace_bit_size = replace.get_bit_size()
|
|
|
|
|
cmp_result = self.compare_bitsizes(search_bit_size, replace_bit_size)
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
assert cmp_result is not None and cmp_result <= 0, \
|
|
|
|
|
'The search expression bit size {} and replace expression ' \
|
|
|
|
|
'bit size {} may not be the same'.format(
|
|
|
|
|
search_bit_size, replace_bit_size)
|
2016-04-25 20:58:47 -07:00
|
|
|
|
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
2018-11-23 17:34:19 +01:00
|
|
|
replace.set_bit_size(search)
|
|
|
|
|
|
|
|
|
|
self.validate_replace(replace, search)
|
2016-04-25 20:58:47 -07:00
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
_optimization_ids = itertools.count()
|
|
|
|
|
|
2015-02-02 16:20:06 -08:00
|
|
|
condition_list = ['true']
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
class SearchAndReplace(object):
|
2015-02-02 16:20:06 -08:00
|
|
|
def __init__(self, transform):
|
2018-07-05 15:17:39 +02:00
|
|
|
self.id = next(_optimization_ids)
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2015-02-02 16:20:06 -08:00
|
|
|
search = transform[0]
|
|
|
|
|
replace = transform[1]
|
|
|
|
|
if len(transform) > 2:
|
|
|
|
|
self.condition = transform[2]
|
|
|
|
|
else:
|
|
|
|
|
self.condition = 'true'
|
|
|
|
|
|
|
|
|
|
if self.condition not in condition_list:
|
|
|
|
|
condition_list.append(self.condition)
|
|
|
|
|
self.condition_index = condition_list.index(self.condition)
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
varset = VarSet()
|
|
|
|
|
if isinstance(search, Expression):
|
|
|
|
|
self.search = search
|
|
|
|
|
else:
|
|
|
|
|
self.search = Expression(search, "search{0}".format(self.id), varset)
|
|
|
|
|
|
2015-01-29 11:45:31 -08:00
|
|
|
varset.lock()
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
if isinstance(replace, Value):
|
|
|
|
|
self.replace = replace
|
|
|
|
|
else:
|
|
|
|
|
self.replace = Value.create(replace, "replace{0}".format(self.id), varset)
|
|
|
|
|
|
2016-04-25 20:58:47 -07:00
|
|
|
BitSizeValidator(varset).validate(self.search, self.replace)
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
_algebraic_pass_template = mako.template.Template("""
|
|
|
|
|
#include "nir.h"
|
2018-10-22 14:08:13 -05:00
|
|
|
#include "nir_builder.h"
|
2014-11-14 17:47:56 -08:00
|
|
|
#include "nir_search.h"
|
2017-01-18 09:21:07 -08:00
|
|
|
#include "nir_search_helpers.h"
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2015-03-23 17:22:44 -07:00
|
|
|
#ifndef NIR_OPT_ALGEBRAIC_STRUCT_DEFS
|
|
|
|
|
#define NIR_OPT_ALGEBRAIC_STRUCT_DEFS
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
struct transform {
|
|
|
|
|
const nir_search_expression *search;
|
|
|
|
|
const nir_search_value *replace;
|
2015-02-02 16:20:06 -08:00
|
|
|
unsigned condition_offset;
|
2014-11-14 17:47:56 -08:00
|
|
|
};
|
|
|
|
|
|
2015-03-23 17:22:44 -07:00
|
|
|
#endif
|
|
|
|
|
|
2018-11-07 14:32:19 -06:00
|
|
|
% for xform in xforms:
|
2014-11-14 17:47:56 -08:00
|
|
|
${xform.search.render()}
|
|
|
|
|
${xform.replace.render()}
|
|
|
|
|
% endfor
|
|
|
|
|
|
2018-11-07 14:32:19 -06:00
|
|
|
% for (opcode, xform_list) in sorted(opcode_xforms.items()):
|
2015-01-27 16:42:38 -08:00
|
|
|
static const struct transform ${pass_name}_${opcode}_xforms[] = {
|
2014-11-14 17:47:56 -08:00
|
|
|
% for xform in xform_list:
|
2015-02-02 16:20:06 -08:00
|
|
|
{ &${xform.search.name}, ${xform.replace.c_ptr}, ${xform.condition_index} },
|
2014-11-14 17:47:56 -08:00
|
|
|
% endfor
|
|
|
|
|
};
|
|
|
|
|
% endfor
|
|
|
|
|
|
|
|
|
|
static bool
|
2018-10-22 14:08:13 -05:00
|
|
|
${pass_name}_block(nir_builder *build, nir_block *block,
|
|
|
|
|
const bool *condition_flags)
|
2014-11-14 17:47:56 -08:00
|
|
|
{
|
2016-04-12 15:30:22 -04:00
|
|
|
bool progress = false;
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2016-04-26 18:34:19 -07:00
|
|
|
nir_foreach_instr_reverse_safe(instr, block) {
|
2014-11-14 17:47:56 -08:00
|
|
|
if (instr->type != nir_instr_type_alu)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
nir_alu_instr *alu = nir_instr_as_alu(instr);
|
|
|
|
|
if (!alu->dest.dest.is_ssa)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
switch (alu->op) {
|
2018-11-07 14:32:19 -06:00
|
|
|
% for opcode in sorted(opcode_xforms.keys()):
|
2014-11-14 17:47:56 -08:00
|
|
|
case nir_op_${opcode}:
|
|
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(${pass_name}_${opcode}_xforms); i++) {
|
2015-01-27 16:42:38 -08:00
|
|
|
const struct transform *xform = &${pass_name}_${opcode}_xforms[i];
|
2016-04-12 15:30:22 -04:00
|
|
|
if (condition_flags[xform->condition_offset] &&
|
2018-10-22 14:08:13 -05:00
|
|
|
nir_replace_instr(build, alu, xform->search, xform->replace)) {
|
2016-04-12 15:30:22 -04:00
|
|
|
progress = true;
|
2015-01-14 19:08:32 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2014-11-14 17:47:56 -08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
% endfor
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-12 15:30:22 -04:00
|
|
|
return progress;
|
2014-11-14 17:47:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2015-02-02 16:20:06 -08:00
|
|
|
${pass_name}_impl(nir_function_impl *impl, const bool *condition_flags)
|
2014-11-14 17:47:56 -08:00
|
|
|
{
|
2016-04-12 15:30:22 -04:00
|
|
|
bool progress = false;
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2018-10-22 14:08:13 -05:00
|
|
|
nir_builder build;
|
|
|
|
|
nir_builder_init(&build, impl);
|
|
|
|
|
|
2016-04-12 15:30:22 -04:00
|
|
|
nir_foreach_block_reverse(block, impl) {
|
2018-10-22 14:08:13 -05:00
|
|
|
progress |= ${pass_name}_block(&build, block, condition_flags);
|
2016-04-12 15:30:22 -04:00
|
|
|
}
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2018-09-10 14:31:29 -07:00
|
|
|
if (progress) {
|
2014-12-12 16:22:46 -08:00
|
|
|
nir_metadata_preserve(impl, nir_metadata_block_index |
|
|
|
|
|
nir_metadata_dominance);
|
2018-09-10 14:31:29 -07:00
|
|
|
} else {
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
impl->valid_metadata &= ~nir_metadata_not_properly_reset;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2016-04-12 15:30:22 -04:00
|
|
|
return progress;
|
2014-11-14 17:47:56 -08:00
|
|
|
}
|
|
|
|
|
|
2015-02-02 16:20:06 -08:00
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
bool
|
|
|
|
|
${pass_name}(nir_shader *shader)
|
|
|
|
|
{
|
|
|
|
|
bool progress = false;
|
2015-02-02 16:20:06 -08:00
|
|
|
bool condition_flags[${len(condition_list)}];
|
|
|
|
|
const nir_shader_compiler_options *options = shader->options;
|
2016-04-07 15:03:39 -07:00
|
|
|
(void) options;
|
2015-02-02 16:20:06 -08:00
|
|
|
|
|
|
|
|
% for index, condition in enumerate(condition_list):
|
|
|
|
|
condition_flags[${index}] = ${condition};
|
|
|
|
|
% endfor
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2016-04-26 20:26:42 -07:00
|
|
|
nir_foreach_function(function, shader) {
|
2015-12-26 10:00:47 -08:00
|
|
|
if (function->impl)
|
|
|
|
|
progress |= ${pass_name}_impl(function->impl, condition_flags);
|
2014-11-14 17:47:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
class AlgebraicPass(object):
|
|
|
|
|
def __init__(self, pass_name, transforms):
|
2018-11-07 14:32:19 -06:00
|
|
|
self.xforms = []
|
|
|
|
|
self.opcode_xforms = defaultdict(lambda : [])
|
2014-11-14 17:47:56 -08:00
|
|
|
self.pass_name = pass_name
|
|
|
|
|
|
2016-04-25 11:36:08 -07:00
|
|
|
error = False
|
|
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
for xform in transforms:
|
|
|
|
|
if not isinstance(xform, SearchAndReplace):
|
2016-04-25 11:36:08 -07:00
|
|
|
try:
|
|
|
|
|
xform = SearchAndReplace(xform)
|
|
|
|
|
except:
|
|
|
|
|
print("Failed to parse transformation:", file=sys.stderr)
|
|
|
|
|
print(" " + str(xform), file=sys.stderr)
|
|
|
|
|
traceback.print_exc(file=sys.stderr)
|
|
|
|
|
print('', file=sys.stderr)
|
|
|
|
|
error = True
|
|
|
|
|
continue
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2018-11-07 14:32:19 -06:00
|
|
|
self.xforms.append(xform)
|
2018-11-07 15:40:02 -06:00
|
|
|
if xform.search.opcode in conv_opcode_types:
|
|
|
|
|
dst_type = conv_opcode_types[xform.search.opcode]
|
|
|
|
|
for size in type_sizes(dst_type):
|
|
|
|
|
sized_opcode = xform.search.opcode + str(size)
|
|
|
|
|
self.opcode_xforms[sized_opcode].append(xform)
|
|
|
|
|
else:
|
|
|
|
|
self.opcode_xforms[xform.search.opcode].append(xform)
|
2014-11-14 17:47:56 -08:00
|
|
|
|
2016-04-25 11:36:08 -07:00
|
|
|
if error:
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
2018-11-07 14:32:19 -06:00
|
|
|
|
2014-11-14 17:47:56 -08:00
|
|
|
def render(self):
|
|
|
|
|
return _algebraic_pass_template.render(pass_name=self.pass_name,
|
2018-11-07 14:32:19 -06:00
|
|
|
xforms=self.xforms,
|
|
|
|
|
opcode_xforms=self.opcode_xforms,
|
2015-02-02 16:20:06 -08:00
|
|
|
condition_list=condition_list)
|