<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 17, 2014 at 5:13 PM, Connor Abbott <span dir="ltr"><<a href="mailto:cwabbott0@gmail.com" target="_blank">cwabbott0@gmail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Would it be possible to drop this patch since this all gets deleted later?<br></blockquote><div><br></div><div>We could, but I'd rather leave it in the history.<br></div><div>--Jason<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5"><br>
On Tue, Dec 16, 2014 at 1:05 AM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> ---<br>
> src/glsl/Makefile.sources | 1 +<br>
> src/glsl/nir/nir.h | 1 +<br>
> src/glsl/nir/nir_opcodes.h | 1 +<br>
> src/glsl/nir/nir_opt_peephole_ffma.c | 191 +++++++++++++++++++++++++++++++<br>
> src/mesa/drivers/dri/i965/brw_fs_nir.cpp | 2 +<br>
> 5 files changed, 196 insertions(+)<br>
> create mode 100644 src/glsl/nir/nir_opt_peephole_ffma.c<br>
><br>
> diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
> index 38ffb85..31608dc 100644<br>
> --- a/src/glsl/Makefile.sources<br>
> +++ b/src/glsl/Makefile.sources<br>
> @@ -32,6 +32,7 @@ NIR_FILES = \<br>
> $(GLSL_SRCDIR)/nir/nir_opt_copy_propagate.c \<br>
> $(GLSL_SRCDIR)/nir/nir_opt_dce.c \<br>
> $(GLSL_SRCDIR)/nir/nir_opt_global_to_local.c \<br>
> + $(GLSL_SRCDIR)/nir/nir_opt_peephole_ffma.c \<br>
> $(GLSL_SRCDIR)/nir/nir_opt_peephole_select.c \<br>
> $(GLSL_SRCDIR)/nir/nir_print.c \<br>
> $(GLSL_SRCDIR)/nir/nir_remove_dead_variables.c \<br>
> diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h<br>
> index bc2280b..e20eb7c 100644<br>
> --- a/src/glsl/nir/nir.h<br>
> +++ b/src/glsl/nir/nir.h<br>
> @@ -1374,6 +1374,7 @@ bool nir_opt_dce_impl(nir_function_impl *impl);<br>
> bool nir_opt_dce(nir_shader *shader);<br>
><br>
> bool nir_opt_peephole_select(nir_shader *shader);<br>
> +bool nir_opt_peephole_ffma(nir_shader *shader);<br>
><br>
> #ifdef __cplusplus<br>
> } /* extern "C" */<br>
> diff --git a/src/glsl/nir/nir_opcodes.h b/src/glsl/nir/nir_opcodes.h<br>
> index 988f691..35d4634 100644<br>
> --- a/src/glsl/nir/nir_opcodes.h<br>
> +++ b/src/glsl/nir/nir_opcodes.h<br>
> @@ -307,6 +307,7 @@ BINOP_HORIZ(vec2, 2, nir_type_unsigned, 1, nir_type_unsigned, 1, nir_type_unsign<br>
> ARR(src1_size, src2_size, src3_size), \<br>
> ARR(nir_type_unsigned, nir_type_unsigned, nir_type_unsigned))<br>
><br>
> +/* fma(a, b, c) = (a * b) + c */<br>
> TRIOP(ffma, nir_type_float)<br>
><br>
> TRIOP(flrp, nir_type_float)<br>
> diff --git a/src/glsl/nir/nir_opt_peephole_ffma.c b/src/glsl/nir/nir_opt_peephole_ffma.c<br>
> new file mode 100644<br>
> index 0000000..2c9b8e5<br>
> --- /dev/null<br>
> +++ b/src/glsl/nir/nir_opt_peephole_ffma.c<br>
> @@ -0,0 +1,191 @@<br>
> +/*<br>
> + * Copyright © 2014 Intel Corporation<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>
> + * Jason Ekstrand (<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>)<br>
> + *<br>
> + */<br>
> +<br>
> +#include "nir.h"<br>
> +<br>
> +/*<br>
> + * Implements a small peephole optimization that looks for a multiply that<br>
> + * is only ever used in an add and replaces both with an fma.<br>
> + */<br>
> +<br>
> +struct peephole_ffma_state {<br>
> + void *mem_ctx;<br>
> + nir_function_impl *impl;<br>
> + bool progress;<br>
> +};<br>
> +<br>
> +static inline nir_alu_instr *<br>
> +get_mul_for_src(nir_alu_instr *add, unsigned idx)<br>
> +{<br>
> + if (!add->src[idx].src.is_ssa)<br>
> + return NULL;<br>
> +<br>
> + /* We can't handle these in between the operations */<br>
> + if (add->src[idx].negate || add->src[idx].abs)<br>
> + return NULL;<br>
> +<br>
> + nir_instr *instr = add->src[idx].src.ssa->parent_instr;<br>
> + if (instr->type != nir_instr_type_alu)<br>
> + return NULL;<br>
> +<br>
> + nir_alu_instr *mul = nir_instr_as_alu(instr);<br>
> + if (mul->op != nir_op_fmul)<br>
> + return NULL;<br>
> +<br>
> + /* Can't handle a saturate in between */<br>
> + if (mul->dest.saturate)<br>
> + return NULL;<br>
> +<br>
> + /* We already know that the same source is not used twice in the add and<br>
> + * we will assume valid use-def information, so this check is sufficient<br>
> + */<br>
> + if (mul->dest.dest.ssa.uses->entries > 1)<br>
> + return NULL; /* Not the only use */<br>
> +<br>
> + return mul;<br>
> +}<br>
> +<br>
> +/* Copies (and maybe swizzles) the given ALU source */<br>
> +static inline void<br>
> +copy_alu_src(void *mem_ctx, nir_alu_src *new_src, nir_alu_src old_src,<br>
> + uint8_t *swizzle)<br>
> +{<br>
> + new_src->src = nir_src_copy(old_src.src, mem_ctx);<br>
> + new_src->abs = old_src.abs;<br>
> + new_src->negate = old_src.negate;<br>
> +<br>
> + if (swizzle == NULL) {<br>
> + memcpy(new_src->swizzle, old_src.swizzle, sizeof old_src.swizzle);<br>
> + } else {<br>
> + for (int i = 0; i < 4; ++i) {<br>
> + if (swizzle[i] < 4)<br>
> + new_src->swizzle[i] = old_src.swizzle[swizzle[i]];<br>
> + }<br>
> + }<br>
> +}<br>
> +<br>
> +static bool<br>
> +nir_opt_peephole_ffma_block(nir_block *block, void *void_state)<br>
> +{<br>
> + struct peephole_ffma_state *state = void_state;<br>
> +<br>
> + nir_foreach_instr_safe(block, instr) {<br>
> + if (instr->type != nir_instr_type_alu)<br>
> + continue;<br>
> +<br>
> + nir_alu_instr *add = nir_instr_as_alu(instr);<br>
> + if (add->op != nir_op_fadd)<br>
> + continue;<br>
> +<br>
> + /* TODO: Maybe bail if this expression is considered "precise"? */<br>
> +<br>
> + /* This, is the case a + a. We would rather handle this with an<br>
> + * algebraic reduction than fuse it. Also, we want to only fuse<br>
> + * things where the multiply is used only once and, in this case,<br>
> + * it would be used twice by the same instruction.<br>
> + */<br>
> + if (add->src[0].src.is_ssa && add->src[1].src.is_ssa &&<br>
> + add->src[0].src.ssa == add->src[1].src.ssa)<br>
> + continue;<br>
> +<br>
> + nir_alu_instr *mul = get_mul_for_src(add, 0);<br>
> + unsigned mul_src = 0;<br>
> +<br>
> + if (mul == NULL) {<br>
> + mul = get_mul_for_src(add, 1);<br>
> + mul_src = 1;<br>
> + }<br>
> +<br>
> + if (mul == NULL)<br>
> + continue;<br>
> +<br>
> + nir_alu_instr *ffma = nir_alu_instr_create(state->mem_ctx, nir_op_ffma);<br>
> + ffma->dest.saturate = add->dest.saturate;<br>
> + ffma->dest.write_mask = add->dest.write_mask;<br>
> +<br>
> + copy_alu_src(state->mem_ctx, &ffma->src[0], mul->src[0],<br>
> + add->src[mul_src].swizzle);<br>
> + copy_alu_src(state->mem_ctx, &ffma->src[1], mul->src[1],<br>
> + add->src[mul_src].swizzle);<br>
> + copy_alu_src(state->mem_ctx, &ffma->src[2], add->src[1 - mul_src], NULL);<br>
> +<br>
> + if (add->dest.dest.is_ssa) {<br>
> + ffma->dest.dest.is_ssa = true;<br>
> + nir_ssa_def_init(state->impl, &ffma->instr, &ffma->dest.dest.ssa,<br>
> + add->dest.dest.ssa.num_components,<br>
> + add-><a href="http://dest.dest.ssa.name" target="_blank">dest.dest.ssa.name</a>);<br>
> +<br>
> + nir_src ffma_dest_src = {<br>
> + .is_ssa = true,<br>
> + .ssa = &ffma->dest.dest.ssa,<br>
> + };<br>
> + nir_ssa_def_rewrite_uses(&add->dest.dest.ssa, ffma_dest_src,<br>
> + state->mem_ctx);<br>
> + } else {<br>
> + ffma->dest.dest = nir_dest_copy(add->dest.dest, state->mem_ctx);<br>
> + }<br>
> +<br>
> + nir_instr_insert_before(&add->instr, &ffma->instr);<br>
> + nir_instr_remove(&add->instr);<br>
> + nir_instr_remove(&mul->instr);<br>
> +<br>
> + state->progress = true;<br>
> + }<br>
> +<br>
> + return true;<br>
> +}<br>
> +<br>
> +static bool<br>
> +nir_opt_peephole_ffma_impl(nir_function_impl *impl)<br>
> +{<br>
> + struct peephole_ffma_state state;<br>
> +<br>
> + state.mem_ctx = ralloc_parent(impl);<br>
> + state.impl = impl;<br>
> + state.progress = false;<br>
> +<br>
> + nir_foreach_block(impl, nir_opt_peephole_ffma_block, &state);<br>
> +<br>
> + if (state.progress)<br>
> + nir_metadata_dirty(impl, nir_metadata_block_index |<br>
> + nir_metadata_dominance);<br>
> +<br>
> + return state.progress;<br>
> +}<br>
> +<br>
> +bool<br>
> +nir_opt_peephole_ffma(nir_shader *shader)<br>
> +{<br>
> + bool progress = false;<br>
> +<br>
> + nir_foreach_overload(shader, overload) {<br>
> + if (overload->impl)<br>
> + progress |= nir_opt_peephole_ffma_impl(overload->impl);<br>
> + }<br>
> +<br>
> + return progress;<br>
> +}<br>
> diff --git a/src/mesa/drivers/dri/i965/brw_fs_nir.cpp b/src/mesa/drivers/dri/i965/brw_fs_nir.cpp<br>
> index b2c5863..b6720af 100644<br>
> --- a/src/mesa/drivers/dri/i965/brw_fs_nir.cpp<br>
> +++ b/src/mesa/drivers/dri/i965/brw_fs_nir.cpp<br>
> @@ -63,6 +63,8 @@ fs_visitor::emit_nir_code()<br>
> nir_validate_shader(nir);<br>
> nir_opt_peephole_select(nir);<br>
> nir_validate_shader(nir);<br>
> + nir_opt_peephole_ffma(nir);<br>
> + nir_validate_shader(nir);<br>
> nir_print_shader(nir, stderr);<br>
><br>
> nir_convert_from_ssa(nir);<br>
> --<br>
> 2.2.0<br>
><br>
</div></div>> _______________________________________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> <a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div></div></div>