nir/algebraic: allow swizzle in nir_algebraic replace expression

This is to allow optimizations in nir_opt_algebraic not otherwise possible

Signed-off-by: Jonathan Marek <jonathan@marek.ca>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Acked-by: Matt Turner <mattst88@gmail.com>
This commit is contained in:
Jonathan Marek 2019-06-20 21:23:53 -04:00
parent b4f4768672
commit 5a4e71c082
4 changed files with 22 additions and 6 deletions

View file

@ -198,6 +198,7 @@ class Value(object):
${'true' if val.is_constant else 'false'},
${val.type() or 'nir_type_invalid' },
${val.cond if val.cond else 'NULL'},
${val.swizzle()},
% elif isinstance(val, Expression):
${'true' if val.inexact else 'false'},
${val.comm_expr_idx}, ${val.comm_exprs},
@ -284,7 +285,8 @@ class Constant(Value):
_var_name_re = re.compile(r"(?P<const>#)?(?P<name>\w+)"
r"(?:@(?P<type>int|uint|bool|float)?(?P<bits>\d+)?)?"
r"(?P<cond>\([^\)]+\))?")
r"(?P<cond>\([^\)]+\))?"
r"(?P<swiz>\.[xyzw]+)?")
class Variable(Value):
def __init__(self, val, name, varset):
@ -306,6 +308,7 @@ class Variable(Value):
self.cond = m.group('cond')
self.required_type = m.group('type')
self._bit_size = int(m.group('bits')) if m.group('bits') else None
self.swiz = m.group('swiz')
if self.required_type == 'bool':
if self._bit_size is not None:
@ -339,6 +342,12 @@ class Variable(Value):
return self.index == other.index
def swizzle(self):
if self.swiz is not None:
swizzles = {'x' : 0, 'y' : 1, 'z' : 2, 'w': 3}
return '{' + ', '.join([str(swizzles[c]) for c in self.swiz[1:]]) + '}'
return '{0, 1, 2, 3}'
_opcode_re = re.compile(r"(?P<inexact>~)?(?P<opcode>\w+)(?:@(?P<bits>\d+))?"
r"(?P<cond>\([^\)]+\))?")

View file

@ -50,11 +50,12 @@ e = 'e'
# however, be used for backend-requested lowering operations as those need to
# happen regardless of precision.
#
# Variable names are specified as "[#]name[@type][(cond)]" where "#" inicates
# that the given variable will only match constants and the type indicates that
# the given variable will only match values from ALU instructions with the
# given output type, and (cond) specifies an additional condition function
# (see nir_search_helpers.h).
# Variable names are specified as "[#]name[@type][(cond)][.swiz]" where:
# "#" indicates that the given variable will only match constants,
# type indicates that the given variable will only match values from ALU
# instructions with the given output type,
# (cond) specifies an additional condition function (see nir_search_helpers.h),
# swiz is a swizzle applied to the variable (only in the <replace> expression)
#
# For constants, you have to be careful to make sure that it is the right
# type because python is unaware of the source and destination types of the

View file

@ -506,6 +506,9 @@ construct_value(nir_builder *build,
(void *)build->shader);
assert(!var->is_constant);
for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
val.swizzle[i] = state->variables[var->variable].swizzle[var->swizzle[i]];
return val;
}

View file

@ -95,6 +95,9 @@ typedef struct {
*/
bool (*cond)(nir_alu_instr *instr, unsigned src,
unsigned num_components, const uint8_t *swizzle);
/** Swizzle (for replace only) */
uint8_t swizzle[NIR_MAX_VEC_COMPONENTS];
} nir_search_variable;
typedef struct {