<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 8, 2016 at 8:21 AM, Eduardo Lima Mitev <span dir="ltr"><<a href="mailto:elima@igalia.com" target="_blank">elima@igalia.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 04/08/2016 01:35 AM, Kenneth Graunke wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Many shaders contain expression trees of the form:<br>
<br>
const_1 * (value * const_2)<br>
<br>
Reorganizing these to<br>
<br>
(const_1 * const_2) * value<br>
<br>
will allow constant folding to combine the constants. Sometimes, these<br>
constants are 2 and 0.5, so we can remove a multiply altogether. Other<br>
times, it can create more immediate constants, which can actually hurt.<br></blockquote></span></blockquote><div><br></div><div>Further justification would be that the original was 4 NIR instructions: 2 load_const and 2 multiplies. Once constant folding takes over, we are again left with 4 NIR instructions: 2 load_const and 2 multiplies. The difference is that we now have (value * const_2) and (value * (const_1 * const_2)). So we have no more instructions in the end and, hopefully, the (value * const_2) goes away. Obviously, the emperical evidence says this actually hurts sometimes, but that's not surprising.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Finding a good balance here is tricky. While much more could be done,<br>
this simple patch seems to have a lot of positive benefit while having<br>
a low downside.<br>
<br>
</blockquote>
<br></span>
Makes sense.<br>
<br>
Reviewed-by: Eduardo Lima Mitev <<a href="mailto:elima@igalia.com" target="_blank">elima@igalia.com</a>><span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
shader-db results on Broadwell:<br>
<br>
total instructions in shared programs: 8963768 -> 8961369 (-0.03%)<br>
instructions in affected programs: 438318 -> 435919 (-0.55%)<br>
helped: 1502<br>
HURT: 245<br>
<br>
total cycles in shared programs: 71527354 -> 71421516 (-0.15%)<br>
cycles in affected programs: 11541788 -> 11435950 (-0.92%)<br>
helped: 3445<br>
HURT: 1224<br>
<br>
</blockquote>
<br></span>
FWIW, these are my results on HSW:<br>
<br>
total instructions in shared programs: 6223995 -> 6222470 (-0.02%)<br>
instructions in affected programs: 300945 -> 299420 (-0.51%)<br>
helped: 1231<br>
HURT: 240<br>
<br>
total cycles in shared programs: 56809432 -> 56615552 (-0.34%)<br>
cycles in affected programs: 7980160 -> 7786280 (-2.43%)<br>
helped: 2087<br>
HURT: 634<span class="HOEnZb"><font color="#888888"><br>
<br>
Eduardo</font></span><span class="im HOEnZb"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Signed-off-by: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org" target="_blank">kenneth@whitecape.org</a>><br>
---<br>
src/compiler/nir/nir_opt_algebraic.py | 8 ++++++++<br>
1 file changed, 8 insertions(+)<br>
<br>
diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py<br>
index e72b4a7..420d9d9 100644<br>
--- a/src/compiler/nir/nir_opt_algebraic.py<br>
+++ b/src/compiler/nir/nir_opt_algebraic.py<br>
@@ -274,6 +274,14 @@ optimizations = [<br>
(('fmul', ('fneg', a), b), ('fneg', ('fmul', a, b))),<br>
(('imul', ('ineg', a), b), ('ineg', ('imul', a, b))),<br>
<br>
+ # Reassociate constants in add/mul chains so they can be folded together.<br>
+ # For now, we only handle cases where the constants are separated by<br>
+ # a single non-constant. We could do better eventually.<br>
+ (('~fmul', '#a', ('fmul', b, '#c')), ('fmul', ('fmul', a, c), b)),<br>
+ (('imul', '#a', ('imul', b, '#c')), ('imul', ('imul', a, c), b)),<br>
+ (('~fadd', '#a', ('fadd', b, '#c')), ('fadd', ('fadd', a, c), b)),<br>
+ (('iadd', '#a', ('iadd', b, '#c')), ('iadd', ('iadd', a, c), b)),<br>
+<br>
# Misc. lowering<br>
(('fmod', a, b), ('fsub', a, ('fmul', b, ('ffloor', ('fdiv', a, b)))), 'options->lower_fmod'),<br>
(('uadd_carry', a, b), ('b2i', ('ult', ('iadd', a, b), a)), 'options->lower_uadd_carry'),<br>
<br>
</blockquote>
<br></span><div class="HOEnZb"><div class="h5">
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</div></div></blockquote></div><br></div></div>