<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 12, 2016 at 8:12 PM, Matt Turner <span dir="ltr"><<a href="mailto:mattst88@gmail.com" target="_blank">mattst88@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Tue, May 10, 2016 at 11:57 AM, Rob Clark <<a href="mailto:robdclark@gmail.com">robdclark@gmail.com</a>> wrote:<br>
</span><div><div class="h5">> From: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
><br>
> Some optimizations, like converting integer multiply/divide into left/<br>
> right shifts, have additional constraints on the search expression.<br>
> Like requiring that a variable is a constant power of two.  Support<br>
> these cases by allowing a fxn name to be appended to the search var<br>
> expression (ie. "a#32(is_power_of_two)").<br>
><br>
> TODO update doc/comment explaining search var syntax<br>
> TODO the eagle-eyed viewer might have noticed that this could also<br>
> replace the existing const syntax (ie. "#a").  Not sure if we should<br>
> keep that.. we could make it syntactic sugar (ie '#' automatically sets<br>
> the cond fxn ptr to 'is_const') or just get rid of it entirely?  Maybe<br>
> that is a follow-on clean-up patch?<br>
><br>
> Signed-off-by: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
> ---<br>
>  src/compiler/nir/nir_algebraic.py     |  8 +++--<br>
>  src/compiler/nir/nir_opt_algebraic.py |  5 +++<br>
>  src/compiler/nir/nir_search.c         |  3 ++<br>
>  src/compiler/nir/nir_search.h         | 10 ++++++<br>
>  src/compiler/nir/nir_search_helpers.h | 66 +++++++++++++++++++++++++++++++++++<br>
>  5 files changed, 90 insertions(+), 2 deletions(-)<br>
>  create mode 100644 src/compiler/nir/nir_search_helpers.h<br>
><br>
> diff --git a/src/compiler/nir/nir_algebraic.py b/src/compiler/nir/nir_algebraic.py<br>
> index 285f853..19ac6ee 100644<br>
> --- a/src/compiler/nir/nir_algebraic.py<br>
> +++ b/src/compiler/nir/nir_algebraic.py<br>
> @@ -76,6 +76,7 @@ class Value(object):<br>
>           return Constant(val, name_base)<br>
><br>
>     __template = mako.template.Template("""<br>
> +#include "compiler/nir/nir_search_helpers.h"<br>
>  static const ${val.c_type} ${<a href="http://val.name" rel="noreferrer" target="_blank">val.name</a>} = {<br>
>     { ${val.type_enum}, ${val.bit_size} },<br>
>  % if isinstance(val, Constant):<br>
> @@ -84,6 +85,7 @@ static const ${val.c_type} ${<a href="http://val.name" rel="noreferrer" target="_blank">val.name</a>} = {<br>
>     ${val.index}, /* ${val.var_name} */<br>
>     ${'true' if val.is_constant else 'false'},<br>
>     ${val.type() or 'nir_type_invalid' },<br>
> +   ${val.cond if val.cond else 'NULL'},<br>
>  % elif isinstance(val, Expression):<br>
>     ${'true' if val.inexact else 'false'},<br>
>     nir_op_${val.opcode},<br>
> @@ -113,7 +115,7 @@ static const ${val.c_type} ${<a href="http://val.name" rel="noreferrer" target="_blank">val.name</a>} = {<br>
>                                      Variable=Variable,<br>
>                                      Expression=Expression)<br>
><br>
> -_constant_re = re.compile(r"(?P<value>[^@]+)(?:@(?P<bits>\d+))?")<br>
> +_constant_re = re.compile(r"(?P<value>[^@\(]+)(?:@(?P<bits>\d+))?")<br>
><br>
>  class Constant(Value):<br>
>     def __init__(self, val, name):<br>
> @@ -150,7 +152,8 @@ class Constant(Value):<br>
>           return "nir_type_float"<br>
><br>
>  _var_name_re = re.compile(r"(?P<const>#)?(?P<name>\w+)"<br>
> -                          r"(?:@(?P<type>int|uint|bool|float)?(?P<bits>\d+)?)?")<br>
> +                          r"(?:@(?P<type>int|uint|bool|float)?(?P<bits>\d+)?)?"<br>
> +                          r"(?P<cond>\([^\)]+\))?")<br>
><br>
>  class Variable(Value):<br>
>     def __init__(self, val, name, varset):<br>
> @@ -161,6 +164,7 @@ class Variable(Value):<br>
><br>
>        self.var_name = m.group('name')<br>
>        self.is_constant = m.group('const') is not None<br>
> +      self.cond = m.group('cond')<br>
>        self.required_type = m.group('type')<br>
>        self.bit_size = int(m.group('bits')) if m.group('bits') else 0<br>
><br>
> diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py<br>
> index 0a95725..952a91a 100644<br>
> --- a/src/compiler/nir/nir_opt_algebraic.py<br>
> +++ b/src/compiler/nir/nir_opt_algebraic.py<br>
> @@ -62,6 +62,11 @@ d = 'd'<br>
>  # constructed value should have that bit-size.<br>
><br>
>  optimizations = [<br>
> +<br>
> +   (('imul', a, '#b@32(is_power_of_two)'), ('ishl', a, ('find_lsb', b))),<br>
> +   (('udiv', a, '#b@32(is_power_of_two)'), ('ushr', a, ('find_lsb', b))),<br>
> +   (('umod', a, '#b(is_power_of_two)'),    ('iand', a, ('isub', b, 1))),<br>
> +<br>
>     (('fneg', ('fneg', a)), a),<br>
>     (('ineg', ('ineg', a)), a),<br>
>     (('fabs', ('fabs', a)), ('fabs', a)),<br>
> diff --git a/src/compiler/nir/nir_search.c b/src/compiler/nir/nir_search.c<br>
> index 2c2fd92..b21fb2c 100644<br>
> --- a/src/compiler/nir/nir_search.c<br>
> +++ b/src/compiler/nir/nir_search.c<br>
> @@ -127,6 +127,9 @@ match_value(const nir_search_value *value, nir_alu_instr *instr, unsigned src,<br>
>               instr->src[src].src.ssa->parent_instr->type != nir_instr_type_load_const)<br>
>              return false;<br>
><br>
> +         if (var->cond && !var->cond(instr, src, num_components, new_swizzle))<br>
> +            return false;<br>
> +<br>
>           if (var->type != nir_type_invalid) {<br>
>              if (instr->src[src].src.ssa->parent_instr->type != nir_instr_type_alu)<br>
>                 return false;<br>
> diff --git a/src/compiler/nir/nir_search.h b/src/compiler/nir/nir_search.h<br>
> index a500feb..f55d797 100644<br>
> --- a/src/compiler/nir/nir_search.h<br>
> +++ b/src/compiler/nir/nir_search.h<br>
> @@ -68,6 +68,16 @@ typedef struct {<br>
>      * never match anything.<br>
>      */<br>
>     nir_alu_type type;<br>
> +<br>
> +   /** Optional condition fxn ptr<br>
<br>
</div></div>Can we write out "function"?<br></blockquote><div><br></div><div>And "pointer" while we're at it? <br></div></div><br></div></div>