<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 5, 2015 at 10:24 PM, Connor Abbott <span dir="ltr"><<a href="mailto:cwabbott0@gmail.com" target="_blank">cwabbott0@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Tue, Dec 16, 2014 at 1:12 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_opt_constant_folding.c | 283 +++++++++++++++++++++++++++++++<br>
> src/mesa/drivers/dri/i965/brw_fs_nir.cpp | 2 +<br>
> 4 files changed, 287 insertions(+)<br>
> create mode 100644 src/glsl/nir/nir_opt_constant_folding.c<br>
><br>
> diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
> index d243f12..7fd650b 100644<br>
> --- a/src/glsl/Makefile.sources<br>
> +++ b/src/glsl/Makefile.sources<br>
> @@ -36,6 +36,7 @@ NIR_FILES = \<br>
> $(GLSL_SRCDIR)/nir/nir_metadata.c \<br>
> $(GLSL_SRCDIR)/nir/nir_opcodes.c \<br>
> $(GLSL_SRCDIR)/nir/nir_opcodes.h \<br>
> + $(GLSL_SRCDIR)/nir/nir_opt_constant_folding.c \<br>
> $(GLSL_SRCDIR)/nir/nir_opt_copy_propagate.c \<br>
> $(GLSL_SRCDIR)/nir/nir_opt_cse.c \<br>
> $(GLSL_SRCDIR)/nir/nir_opt_dce.c \<br>
> diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h<br>
> index 630e841..46f069f 100644<br>
> --- a/src/glsl/nir/nir.h<br>
> +++ b/src/glsl/nir/nir.h<br>
> @@ -1403,6 +1403,7 @@ void nir_convert_to_ssa(nir_shader *shader);<br>
> void nir_convert_from_ssa(nir_shader *shader);<br>
><br>
> bool nir_opt_algebraic(nir_shader *shader);<br>
> +bool nir_opt_constant_folding(nir_shader *shader);<br>
><br>
> bool nir_opt_global_to_local(nir_shader *shader);<br>
><br>
> diff --git a/src/glsl/nir/nir_opt_constant_folding.c b/src/glsl/nir/nir_opt_constant_folding.c<br>
> new file mode 100644<br>
> index 0000000..3523713<br>
> --- /dev/null<br>
> +++ b/src/glsl/nir/nir_opt_constant_folding.c<br>
> @@ -0,0 +1,283 @@<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>
> +#include <math.h><br>
> +<br>
> +/*<br>
> + * Implements SSA-based constant folding.<br>
> + */<br>
> +<br>
> +struct constant_fold_state {<br>
> + void *mem_ctx;<br>
> + nir_function_impl *impl;<br>
> + bool progress;<br>
> +};<br>
> +<br>
> +#define SRC_COMP(T, IDX, CMP) src[IDX]->value.T[instr->src[IDX].swizzle[CMP]]<br>
> +#define SRC(T, IDX) SRC_COMP(T, IDX, i)<br>
> +#define DEST_COMP(T, CMP) dest->value.T[CMP]<br>
> +#define DEST(T) DEST_COMP(T, i)<br>
> +<br>
> +#define FOLD_PER_COMP(EXPR) \<br>
> + for (unsigned i = 0; i < instr->dest.dest.ssa.num_components; i++) { \<br>
> + EXPR; \<br>
> + } \<br>
> +<br>
> +static bool<br>
> +constant_fold_alu_instr(nir_alu_instr *instr, void *void_state)<br>
> +{<br>
> + struct constant_fold_state *state = void_state;<br>
> + nir_load_const_instr *src[4], *dest;<br>
> +<br>
> + if (!instr->dest.dest.is_ssa)<br>
> + return false;<br>
> +<br>
> + for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {<br>
> + if (!instr->src[i].src.is_ssa)<br>
> + return false;<br>
> +<br>
> + if (instr->src[i].src.ssa->parent_instr->type != nir_instr_type_load_const)<br>
> + return false;<br>
> +<br>
> + src[i] = nir_instr_as_load_const(instr->src[i].src.ssa->parent_instr);<br>
> + }<br>
> +<br>
> + dest = nir_load_const_instr_create(state->mem_ctx);<br>
> + dest->array_elems = 0;<br>
> + dest->num_components = instr->dest.dest.ssa.num_components;<br>
> +<br>
> + switch (instr->op) {<br>
> + case nir_op_ineg:<br>
> + FOLD_PER_COMP(DEST(i) = -SRC(i, 0));<br>
> + break;<br>
> + case nir_op_fneg:<br>
> + FOLD_PER_COMP(DEST(f) = -SRC(f, 0));<br>
> + break;<br>
> + case nir_op_inot:<br>
> + FOLD_PER_COMP(DEST(i) = ~SRC(i, 0));<br>
> + break;<br>
> + case nir_op_fnot:<br>
> + FOLD_PER_COMP(DEST(f) = (SRC(f, 0) == 0.0f) ? 1.0f : 0.0f);<br>
> + break;<br>
> + case nir_op_frcp:<br>
> + FOLD_PER_COMP(DEST(f) = 1.0f / SRC(f, 0));<br>
> + break;<br>
> + case nir_op_frsq:<br>
> + FOLD_PER_COMP(DEST(f) = 1.0f / sqrt(SRC(f, 0)));<br>
> + break;<br>
> + case nir_op_fsqrt:<br>
> + FOLD_PER_COMP(DEST(f) = sqrtf(SRC(f, 0)));<br>
> + break;<br>
> + case nir_op_fexp:<br>
> + FOLD_PER_COMP(DEST(f) = expf(SRC(f, 0)));<br>
> + break;<br>
> + case nir_op_flog:<br>
> + FOLD_PER_COMP(DEST(f) = logf(SRC(f, 0)));<br>
> + break;<br>
> + case nir_op_fexp2:<br>
> + FOLD_PER_COMP(DEST(f) = exp2f(SRC(f, 0)));<br>
> + break;<br>
> + case nir_op_flog2:<br>
> + FOLD_PER_COMP(DEST(f) = log2f(SRC(f, 0)));<br>
> + break;<br>
> + case nir_op_f2i:<br>
> + FOLD_PER_COMP(DEST(i) = SRC(f, 0));<br>
> + break;<br>
> + case nir_op_f2u:<br>
> + FOLD_PER_COMP(DEST(u) = SRC(f, 0));<br>
> + break;<br>
> + case nir_op_i2f:<br>
> + FOLD_PER_COMP(DEST(f) = SRC(i, 0));<br>
> + break;<br>
> + case nir_op_f2b:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(i, 0) == 0.0f) ? NIR_FALSE : NIR_TRUE);<br>
> + break;<br>
> + case nir_op_b2f:<br>
> + FOLD_PER_COMP(DEST(f) = SRC(u, 0) ? 1.0f : 0.0f);<br>
> + break;<br>
> + case nir_op_i2b:<br>
> + FOLD_PER_COMP(DEST(u) = SRC(i, 0) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_u2f:<br>
> + FOLD_PER_COMP(DEST(f) = SRC(u, 0));<br>
> + break;<br>
> + case nir_op_bany2:<br>
> + DEST_COMP(u, 0) = (SRC_COMP(u, 0, 0) || SRC_COMP(u, 0, 1)) ?<br>
> + NIR_TRUE : NIR_FALSE;<br>
> + break;<br>
> + case nir_op_fadd:<br>
> + FOLD_PER_COMP(DEST(f) = SRC(f, 0) + SRC(f, 1));<br>
> + break;<br>
> + case nir_op_iadd:<br>
> + FOLD_PER_COMP(DEST(i) = SRC(i, 0) + SRC(i, 1));<br>
> + break;<br>
> + case nir_op_fsub:<br>
> + FOLD_PER_COMP(DEST(f) = SRC(f, 0) - SRC(f, 1));<br>
> + break;<br>
> + case nir_op_isub:<br>
> + FOLD_PER_COMP(DEST(i) = SRC(i, 0) - SRC(i, 1));<br>
> + break;<br>
> + case nir_op_fmul:<br>
> + FOLD_PER_COMP(DEST(f) = SRC(f, 0) * SRC(f, 1));<br>
> + break;<br>
> + case nir_op_imul:<br>
> + FOLD_PER_COMP(DEST(i) = SRC(i, 0) * SRC(i, 1));<br>
> + break;<br>
> + case nir_op_fdiv:<br>
> + FOLD_PER_COMP(DEST(f) = SRC(f, 0) / SRC(f, 1));<br>
> + break;<br>
> + case nir_op_idiv:<br>
> + FOLD_PER_COMP(DEST(i) = SRC(i, 0) / SRC(i, 1));<br>
> + break;<br>
> + case nir_op_udiv:<br>
> + FOLD_PER_COMP(DEST(u) = SRC(u, 0) / SRC(u, 1));<br>
> + break;<br>
> + case nir_op_flt:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(f, 0) < SRC(f, 1)) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_fge:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(f, 0) >= SRC(f, 1)) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_feq:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(f, 0) == SRC(f, 1)) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_fne:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(f, 0) != SRC(f, 1)) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_ilt:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(i, 0) < SRC(i, 1)) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_ige:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(i, 0) >= SRC(i, 1)) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_ieq:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(i, 0) == SRC(i, 1)) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_ine:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(i, 0) != SRC(i, 1)) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_ult:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(u, 0) < SRC(u, 1)) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_uge:<br>
> + FOLD_PER_COMP(DEST(u) = (SRC(u, 0) >= SRC(u, 1)) ? NIR_TRUE : NIR_FALSE);<br>
> + break;<br>
> + case nir_op_ishl:<br>
> + FOLD_PER_COMP(DEST(i) = SRC(i, 0) << SRC(i, 1));<br>
> + break;<br>
> + case nir_op_ishr:<br>
> + FOLD_PER_COMP(DEST(i) = SRC(i, 0) >> SRC(i, 1));<br>
> + break;<br>
> + case nir_op_ushr:<br>
> + FOLD_PER_COMP(DEST(u) = SRC(u, 0) >> SRC(u, 1));<br>
> + break;<br>
> + case nir_op_iand:<br>
> + FOLD_PER_COMP(DEST(i) = SRC(i, 0) & SRC(i, 1));<br>
> + break;<br>
> + case nir_op_ior:<br>
> + FOLD_PER_COMP(DEST(i) = SRC(i, 0) | SRC(i, 1));<br>
> + break;<br>
> + case nir_op_ixor:<br>
> + FOLD_PER_COMP(DEST(i) = SRC(i, 0) ^ SRC(i, 1));<br>
> + break;<br>
> + default:<br>
> + ralloc_free(dest);<br>
> + return false;<br>
> + }<br>
> +<br>
> + if (instr->dest.saturate) {<br>
> + assert(nir_op_infos[instr->op].output_type == nir_type_float);<br>
> + for (int i = 0; i < 4; ++i) {<br>
> + if (dest->value.f[i] > 1.0f)<br>
> + dest->value.f[i] = 1.0f;<br>
> + else if (dest->value.f[i] < 0.0f)<br>
> + dest->value.f[i] = 0.0f;<br>
> + }<br>
> + }<br>
<br>
</div></div>I think this is just dead code... we don't even handle abs and negate.<br></blockquote><div><br></div><div>Yeah, it probably is. We could add abs and negate, but meh. I'll just add asserts.<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>
> +<br>
> + dest->dest.is_ssa = true;<br>
> + nir_ssa_def_init(&dest->instr, &dest->dest.ssa,<br>
> + instr->dest.dest.ssa.num_components,<br>
> + instr-><a href="http://dest.dest.ssa.name" target="_blank">dest.dest.ssa.name</a>);<br>
> +<br>
> + nir_instr_insert_before(&instr->instr, &dest->instr);<br>
> +<br>
> + nir_src new_src = {<br>
> + .is_ssa = true,<br>
> + .ssa = &dest->dest.ssa,<br>
> + };<br>
> +<br>
> + nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, new_src, state->mem_ctx);<br>
> +<br>
> + nir_instr_remove(&instr->instr);<br>
> + ralloc_free(instr);<br>
> +<br>
> + return true;<br>
> +}<br>
> +<br>
> +static bool<br>
> +constant_fold_block(nir_block *block, void *void_state)<br>
> +{<br>
> + struct constant_fold_state *state = void_state;<br>
> +<br>
> + nir_foreach_instr_safe(block, instr) {<br>
> + if (instr->type != nir_instr_type_alu)<br>
> + continue;<br>
> +<br>
> + state->progress |= constant_fold_alu_instr(nir_instr_as_alu(instr), state);<br>
> + }<br>
> +<br>
> + return true;<br>
> +}<br>
> +<br>
> +static bool<br>
> +nir_opt_constant_folding_impl(nir_function_impl *impl)<br>
> +{<br>
> + struct constant_fold_state state;<br>
> +<br>
> + state.mem_ctx = ralloc_parent(impl);<br>
> + state.impl = impl;<br>
> + state.progress = false;<br>
> +<br>
> + nir_foreach_block(impl, constant_fold_block, &state);<br>
> +<br>
> + return state.progress;<br>
> +}<br>
> +<br>
> +bool<br>
> +nir_opt_constant_folding(nir_shader *shader)<br>
> +{<br>
> + bool progress = false;<br>
> +<br>
> + nir_foreach_overload(shader, overload) {<br>
> + if (overload->impl)<br>
> + progress |= nir_opt_constant_folding_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 039dd4d..f719c86 100644<br>
> --- a/src/mesa/drivers/dri/i965/brw_fs_nir.cpp<br>
> +++ b/src/mesa/drivers/dri/i965/brw_fs_nir.cpp<br>
> @@ -55,6 +55,8 @@ fs_visitor::emit_nir_code()<br>
> nir_validate_shader(nir);<br>
> progress |= nir_opt_algebraic(nir);<br>
> nir_validate_shader(nir);<br>
> + progress |= nir_opt_constant_folding(nir);<br>
> + nir_validate_shader(nir);<br>
> } while (progress);<br>
><br>
> /* Lower a bunch of stuff */<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><br></div></div>