<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Dec 27, 2015 at 10:15 AM, 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"><span class="">On Sat, Dec 26, 2015 at 2:09 PM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> This commit adds a new NIR pass that lowers all function calls away by<br>
> inlining the functions.<br>
> ---<br>
><br>
> There are still two things missing here:<br>
><br>
> 1) We really shouldn't do the inline recursively. We should keep a<br>
> hash-table of inlined versions or something.<br>
<br>
</span>I don't think we need anything so complicated. We're collapsing the<br>
DAG of functions, so we just need a simple DFS: we should inline the<br>
callees before inlining the caller. We could keep a record of the<br>
already-inlined functions, but if we visit an already-inlined function<br>
we wouldn't do anything so it would only save us the cost of walking<br>
over that function.<br></blockquote><div><br></div><div>Yes, that's what we want to do for the "inline the whole shader" case. What if we want only want to inline part of it? Would that ever seriously happen? I don't know. We probably also want to delete all of the "extra" functions while we're at it. How do we specify the unique entrypoint? No, "main" isn't sufficient.<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>
><br>
> 2) It doesn't properly handle things like samplers as parameters. Since<br>
> those can only be "in" parameters, I had thought about treating the<br>
> variable as a "proxy" and going through and rewriting all of the derefs<br>
> that use it. We could do the same thing for regular parameters when a<br>
> shadow isn't needed.<br>
><br>
> Still, I thought it was worth sending out because it does work for most<br>
> cases.<br>
><br>
> src/glsl/Makefile.sources | 1 +<br>
> src/glsl/nir/nir.h | 3 +<br>
> src/glsl/nir/nir_inline_functions.c | 138 ++++++++++++++++++++++++++++++++++++<br>
> 3 files changed, 142 insertions(+)<br>
> create mode 100644 src/glsl/nir/nir_inline_functions.c<br>
><br>
> diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
> index fa3868c..c271afd 100644<br>
> --- a/src/glsl/Makefile.sources<br>
> +++ b/src/glsl/Makefile.sources<br>
> @@ -35,6 +35,7 @@ NIR_FILES = \<br>
> nir/nir_dominance.c \<br>
> nir/nir_from_ssa.c \<br>
> nir/nir_gs_count_vertices.c \<br>
> + nir/nir_inline_functions.c \<br>
> nir/nir_intrinsics.c \<br>
> nir/nir_intrinsics.h \<br>
> nir/nir_instr_set.c \<br>
> diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h<br>
> index 2e29617..437f63a 100644<br>
> --- a/src/glsl/nir/nir.h<br>
> +++ b/src/glsl/nir/nir.h<br>
> @@ -1938,6 +1938,9 @@ bool nir_split_var_copies(nir_shader *shader);<br>
> bool nir_lower_returns_impl(nir_function_impl *impl);<br>
> bool nir_lower_returns(nir_shader *shader);<br>
><br>
> +bool nir_inline_functions_impl(nir_function_impl *impl);<br>
> +bool nir_inline_functions(nir_shader *shader);<br>
> +<br>
> void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, void *mem_ctx);<br>
> void nir_lower_var_copies(nir_shader *shader);<br>
><br>
> diff --git a/src/glsl/nir/nir_inline_functions.c b/src/glsl/nir/nir_inline_functions.c<br>
> new file mode 100644<br>
> index 0000000..e7e17ca<br>
> --- /dev/null<br>
> +++ b/src/glsl/nir/nir_inline_functions.c<br>
> @@ -0,0 +1,138 @@<br>
> +/*<br>
> + * Copyright © 2015 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>
> +<br>
> +#include "nir.h"<br>
> +#include "nir_builder.h"<br>
> +#include "nir_control_flow.h"<br>
> +<br>
> +struct inline_functions_state {<br>
> + nir_function_impl *impl;<br>
> + nir_builder builder;<br>
> + bool progress;<br>
> +};<br>
> +<br>
> +static bool<br>
> +inline_functions_block(nir_block *block, void *void_state)<br>
> +{<br>
> + struct inline_functions_state *state = void_state;<br>
> +<br>
> + nir_builder *b = &state->builder;<br>
> +<br>
> + /* This is tricky. We're iterating over instructions in a block but, as<br>
> + * we go, the block and its instruction list are being split into<br>
> + * pieces. However, this *should* be safe since foreach_safe always<br>
> + * stashes the next thing in the iteration. That next thing will<br>
> + * properly get moved to the next block when it gets split, and we<br>
> + * continue iterating there.<br>
> + */<br>
> + nir_foreach_instr_safe(block, instr) {<br>
> + if (instr->type != nir_instr_type_call)<br>
> + continue;<br>
> +<br>
> + state->progress = true;<br>
> +<br>
> + nir_call_instr *call = nir_instr_as_call(instr);<br>
> + assert(call->callee->impl);<br>
> +<br>
> + nir_function_impl *callee_copy =<br>
> + nir_function_impl_clone(call->callee->impl);<br>
> +<br>
> + exec_list_append(&state->impl->locals, &callee_copy->locals);<br>
> + exec_list_append(&state->impl->registers, &callee_copy->registers);<br>
> +<br>
> + b->cursor = nir_before_instr(&call->instr);<br>
> +<br>
> + /* Add copies of all in parameters */<br>
> + assert(call->num_params == callee_copy->num_params);<br>
> + for (unsigned i = 0; i < callee_copy->num_params; i++) {<br>
> + /* Only in or inout parameters */<br>
> + if (call->callee->params[i].param_type == nir_parameter_out)<br>
> + continue;<br>
> +<br>
> + nir_copy_deref_var(b, nir_deref_var_create(b->shader,<br>
> + callee_copy->params[i]),<br>
> + call->params[i]);<br>
> + }<br>
> +<br>
> + /* Pluck the body out of the function and place it here */<br>
> + nir_cf_list body;<br>
> + nir_cf_list_extract(&body, &callee_copy->body);<br>
> + nir_cf_reinsert(&body, b->cursor);<br>
> +<br>
> + b->cursor = nir_before_instr(&call->instr);<br>
> +<br>
> + /* Add copies of all out parameters and the return */<br>
> + assert(call->num_params == callee_copy->num_params);<br>
> + for (unsigned i = 0; i < callee_copy->num_params; i++) {<br>
> + /* Only out or inout parameters */<br>
> + if (call->callee->params[i].param_type == nir_parameter_in)<br>
> + continue;<br>
> +<br>
> + nir_copy_deref_var(b, call->params[i],<br>
> + nir_deref_var_create(b->shader,<br>
> + callee_copy->params[i]));<br>
> + }<br>
> + if (!glsl_type_is_void(call->callee->return_type)) {<br>
> + nir_copy_deref_var(b, call->return_deref,<br>
> + nir_deref_var_create(b->shader,<br>
> + callee_copy->return_var));<br>
> + }<br>
> +<br>
> + nir_instr_remove(&call->instr);<br>
> + }<br>
> +<br>
> + return true;<br>
> +}<br>
> +<br>
> +bool<br>
> +nir_inline_functions_impl(nir_function_impl *impl)<br>
> +{<br>
> + struct inline_functions_state state;<br>
> +<br>
> + state.progress = false;<br>
> + state.impl = impl;<br>
> + nir_builder_init(&state.builder, impl);<br>
> +<br>
> + nir_foreach_block(impl, inline_functions_block, &state);<br>
> +<br>
> + /* SSA and register indices are completely messed up now */<br>
> + nir_index_ssa_defs(impl);<br>
> + nir_index_local_regs(impl);<br>
> +<br>
> + nir_metadata_preserve(impl, nir_metadata_none);<br>
> +<br>
> + return state.progress;<br>
> +}<br>
> +<br>
> +bool<br>
> +nir_inline_functions(nir_shader *shader)<br>
> +{<br>
> + bool progress = false;<br>
> +<br>
> + nir_foreach_function(shader, function) {<br>
> + if (function->impl)<br>
> + progress = nir_inline_functions_impl(function->impl) || progress;<br>
> + }<br>
> +<br>
> + return progress;<br>
> +}<br>
> --<br>
> 2.5.0.400.gff86faf<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" rel="noreferrer" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div><br></div></div>