<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 10, 2016 at 11:57 AM, Rob Clark <span dir="ltr"><<a href="mailto:robdclark@gmail.com" target="_blank">robdclark@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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>
<span class=""><br>
Signed-off-by: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
---<br>
</span> 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>
<span class=""> static const ${val.c_type} ${<a href="http://val.name" rel="noreferrer" target="_blank">val.name</a>} = {<br>
</span> { ${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>
<span class=""> ${val.index}, /* ${val.var_name} */<br>
${'true' if val.is_constant else 'false'},<br>
</span><span class=""> ${val.type() or 'nir_type_invalid' },<br>
</span>+ ${val.cond if val.cond else 'NULL'},<br>
<span class=""> % elif isinstance(val, Expression):<br>
${'true' if val.inexact else 'false'},<br>
</span> 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>
<span class=""> Variable=Variable,<br>
Expression=Expression)<br>
<br>
-_constant_re = re.compile(r"(?P<value>[^@]+)(?:@(?P<bits>\d+))?")<br>
</span>+_constant_re = re.compile(r"(?P<value>[^@\(]+)(?:@(?P<bits>\d+))?")<br></blockquote><div><br></div><div>Spurious change?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class=""><br>
class Constant(Value):<br>
def __init__(self, val, name):<br>
</span>@@ -150,7 +152,8 @@ class Constant(Value):<br>
return "nir_type_float"<br>
<span class=""><br>
_var_name_re = re.compile(r"(?P<const>#)?(?P<name>\w+)"<br>
</span>- 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>
<span class=""><br>
self.var_name = m.group('name')<br>
self.is_constant = m.group('const') is not None<br>
</span>+ self.cond = m.group('cond')<br>
<span class=""> 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>
</span>index 0a95725..952a91a 100644<br>
<span class="">--- 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>
</span>+ (('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>
<span class="">+<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>
</span>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>
<span class=""> instr->src[src].src.ssa->parent_instr->type != nir_instr_type_load_const)<br>
return false;<br>
<br>
</span>+ if (var->cond && !var->cond(instr, src, num_components, new_swizzle))<br>
+ return false;<br>
+<br>
<span class=""> 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>
</span>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>
+ * This is only allowed in search expressions, and allows additional<br>
+ * constraints to be placed on the match. Typically used for 'is_constant'<br>
+ * variables to require, for example, power-of-two in order for the search<br>
+ * to match.<br>
+ */<br>
+ bool (*cond)(nir_alu_instr *instr, unsigned src,<br>
+ unsigned num_components, const uint8_t *swizzle);<br>
} nir_search_variable;<br>
<br>
typedef struct {<br>
diff --git a/src/compiler/nir/nir_search_helpers.h b/src/compiler/nir/nir_search_helpers.h<br>
new file mode 100644<br>
index 0000000..8ed2ca0<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_search_helpers.h<br>
@@ -0,0 +1,66 @@<br>
+/*<br>
+ * Copyright © 2016 Red Hat<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ *<br>
+ * Authors:<br>
+ * Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
+ */<br>
+<br>
+#ifndef _NIR_SEARCH_HELPERS_<br>
+#define _NIR_SEARCH_HELPERS_<br>
+<br>
+#include "nir.h"<br>
+<br>
+static inline bool<br>
+__is_power_of_two(unsigned int x)<br>
<span class="">+{<br>
+ return ((x != 0) && !(x & (x - 1)));<br>
+}<br>
+<br>
</span>+static inline bool<br>
+is_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,<br>
+ const uint8_t *swizzle)<br>
+{<br>
<span class="">+ nir_const_value *val = nir_src_as_const_value(instr->src[src].src);<br>
+<br>
</span>+ /* only constant src's: */<br>
+ if (!val)<br>
+ return false;<br>
+<br>
<span class="">+ for (unsigned i = 0; i < num_components; i++) {<br>
+ switch (nir_op_infos[instr->op].input_types[src]) {<br>
+ case nir_type_int:<br>
</span>+ if (!__is_power_of_two(val->i32[swizzle[i]]))<br>
<span class="">+ return false;<br>
+ break;<br>
+ case nir_type_uint:<br>
</span>+ if (!__is_power_of_two(val->u32[swizzle[i]]))<br>
<span class="">+ return false;<br></span></blockquote><div><br></div><div>Your implementation of __is_power_of_two takes an unsigned. There's no benefit to splitting it into two cases and it just creates a false distinction. If you do, you can probably inline the helper (I don't care much one way or the other on that).<br><br></div><div>All in all, this seems ok. The "(some_function)" syntax seems clunky but I can't come up with anything better off-hand. Let's just go with it for now and we can always change it later.<br><br></div><div>Reviewed-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
+ break;<br>
+ default:<br>
+ return false;<br>
+ }<br>
+ }<br>
+<br>
</span>+ return true;<br>
+}<br>
+<br>
+#endif /* _NIR_SEARCH_ */<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.5.5<br>
<br>
</font></span></blockquote></div><br></div></div>