From 1749e667ea38ec539d29c92c08f94738afcb91ad Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Fri, 18 Dec 2015 11:16:16 -0800 Subject: [PATCH] nir: Add a stub function inlining pass All it does is remove the return at the end, but it's good enough for simple functions. --- src/glsl/Makefile.sources | 1 + src/glsl/nir/nir.h | 3 ++ src/glsl/nir/nir_lower_returns.c | 78 ++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/glsl/nir/nir_lower_returns.c diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources index e64c31e17c6..32fbf6d1b6c 100644 --- a/src/glsl/Makefile.sources +++ b/src/glsl/Makefile.sources @@ -44,6 +44,7 @@ NIR_FILES = \ nir/nir_lower_alu_to_scalar.c \ nir/nir_lower_atomics.c \ nir/nir_lower_clip.c \ + nir/nir_lower_returns.c \ nir/nir_lower_global_vars_to_local.c \ nir/nir_lower_gs_intrinsics.c \ nir/nir_lower_load_const_to_scalar.c \ diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h index 021c4280557..0cf5f80c591 100644 --- a/src/glsl/nir/nir.h +++ b/src/glsl/nir/nir.h @@ -1998,6 +1998,9 @@ int nir_gs_count_vertices(const nir_shader *shader); bool nir_split_var_copies(nir_shader *shader); +bool nir_lower_returns_impl(nir_function_impl *impl); +bool nir_lower_returns(nir_shader *shader); + void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, void *mem_ctx); void nir_lower_var_copies(nir_shader *shader); diff --git a/src/glsl/nir/nir_lower_returns.c b/src/glsl/nir/nir_lower_returns.c new file mode 100644 index 00000000000..a15d0332ed4 --- /dev/null +++ b/src/glsl/nir/nir_lower_returns.c @@ -0,0 +1,78 @@ +/* + * Copyright © 2015 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "nir.h" + +static bool +assert_no_returns_block(nir_block *block, void *state) +{ + (void)state; + + nir_foreach_instr(block, instr) { + if (instr->type != nir_instr_type_jump) + continue; + + nir_jump_instr *jump = nir_instr_as_jump(instr); + assert(jump->type != nir_jump_return); + } + + return true; +} + +bool +nir_lower_returns_impl(nir_function_impl *impl) +{ + bool progress = false; + + assert(impl->end_block->predecessors->entries == 1); + + struct set_entry *entry = + _mesa_set_next_entry(impl->end_block->predecessors, NULL); + + nir_block *last_block = (nir_block *)entry->key; + + nir_instr *last_instr = nir_block_last_instr(last_block); + if (last_instr && last_instr->type == nir_instr_type_jump) { + nir_jump_instr *jump = nir_instr_as_jump(last_instr); + assert(jump->type == nir_jump_return); + nir_instr_remove(&jump->instr); + progress = true; + } + + nir_foreach_block(impl, assert_no_returns_block, NULL); + + return progress; +} + +bool +nir_lower_returns(nir_shader *shader) +{ + bool progress = false; + + nir_foreach_overload(shader, overload) { + if (overload->impl) + progress = nir_lower_returns_impl(overload->impl) || progress; + } + + return progress; +} -- 2.30.2