-#! /usr/bin/env python
#
# Copyright (C) 2014 Intel Corporation
#
${val.index}, /* ${val.var_name} */
${'true' if val.is_constant else 'false'},
${val.type() or 'nir_type_invalid' },
+ ${val.cond if val.cond else 'NULL'},
% elif isinstance(val, Expression):
${'true' if val.inexact else 'false'},
nir_op_${val.opcode},
{ ${', '.join(src.c_ptr for src in val.sources)} },
+ ${val.cond if val.cond else 'NULL'},
% endif
};""")
Variable=Variable,
Expression=Expression)
-_constant_re = re.compile(r"(?P<value>[^@]+)(?:@(?P<bits>\d+))?")
+_constant_re = re.compile(r"(?P<value>[^@\(]+)(?:@(?P<bits>\d+))?")
class Constant(Value):
def __init__(self, val, name):
return "nir_type_float"
_var_name_re = re.compile(r"(?P<const>#)?(?P<name>\w+)"
- r"(?:@(?P<type>int|uint|bool|float)?(?P<bits>\d+)?)?")
+ r"(?:@(?P<type>int|uint|bool|float)?(?P<bits>\d+)?)?"
+ r"(?P<cond>\([^\)]+\))?")
class Variable(Value):
def __init__(self, val, name, varset):
self.var_name = m.group('name')
self.is_constant = m.group('const') is not None
+ self.cond = m.group('cond')
self.required_type = m.group('type')
self.bit_size = int(m.group('bits')) if m.group('bits') else 0
elif self.required_type == 'float':
return "nir_type_float"
-_opcode_re = re.compile(r"(?P<inexact>~)?(?P<opcode>\w+)(?:@(?P<bits>\d+))?")
+_opcode_re = re.compile(r"(?P<inexact>~)?(?P<opcode>\w+)(?:@(?P<bits>\d+))?"
+ r"(?P<cond>\([^\)]+\))?")
class Expression(Value):
def __init__(self, expr, name_base, varset):
self.opcode = m.group('opcode')
self.bit_size = int(m.group('bits')) if m.group('bits') else 0
self.inexact = m.group('inexact') is not None
+ self.cond = m.group('cond')
self.sources = [ Value.create(src, "{0}_{1}".format(name_base, i), varset)
for (i, src) in enumerate(expr[1:]) ]
_algebraic_pass_template = mako.template.Template("""
#include "nir.h"
#include "nir_search.h"
+#include "nir_search_helpers.h"
#ifndef NIR_OPT_ALGEBRAIC_STRUCT_DEFS
#define NIR_OPT_ALGEBRAIC_STRUCT_DEFS
unsigned condition_offset;
};
-struct opt_state {
- void *mem_ctx;
- bool progress;
- const bool *condition_flags;
-};
-
#endif
% for (opcode, xform_list) in xform_dict.iteritems():
% endfor
static bool
-${pass_name}_block(nir_block *block, void *void_state)
+${pass_name}_block(nir_block *block, const bool *condition_flags,
+ void *mem_ctx)
{
- struct opt_state *state = void_state;
+ bool progress = false;
- nir_foreach_instr_reverse_safe(block, instr) {
+ nir_foreach_instr_reverse_safe(instr, block) {
if (instr->type != nir_instr_type_alu)
continue;
case nir_op_${opcode}:
for (unsigned i = 0; i < ARRAY_SIZE(${pass_name}_${opcode}_xforms); i++) {
const struct transform *xform = &${pass_name}_${opcode}_xforms[i];
- if (state->condition_flags[xform->condition_offset] &&
+ if (condition_flags[xform->condition_offset] &&
nir_replace_instr(alu, xform->search, xform->replace,
- state->mem_ctx)) {
- state->progress = true;
+ mem_ctx)) {
+ progress = true;
break;
}
}
}
}
- return true;
+ return progress;
}
static bool
${pass_name}_impl(nir_function_impl *impl, const bool *condition_flags)
{
- struct opt_state state;
-
- state.mem_ctx = ralloc_parent(impl);
- state.progress = false;
- state.condition_flags = condition_flags;
+ void *mem_ctx = ralloc_parent(impl);
+ bool progress = false;
- nir_foreach_block_reverse_call(impl, ${pass_name}_block, &state);
+ nir_foreach_block_reverse(block, impl) {
+ progress |= ${pass_name}_block(block, condition_flags, mem_ctx);
+ }
- if (state.progress)
+ if (progress)
nir_metadata_preserve(impl, nir_metadata_block_index |
nir_metadata_dominance);
- return state.progress;
+ return progress;
}
condition_flags[${index}] = ${condition};
% endfor
- nir_foreach_function(shader, function) {
+ nir_foreach_function(function, shader) {
if (function->impl)
progress |= ${pass_name}_impl(function->impl, condition_flags);
}