From 45ef10c06a3010a0710f9db3e980fde545157b6a Mon Sep 17 00:00:00 2001 From: Timothy Arceri Date: Thu, 7 Sep 2017 23:27:59 +1000 Subject: [PATCH] nir: add some helpers for doing linking The initial helpers add support for removing unused varyings between stages. V2: - Moved the io mask helper function into this file rather than nir.h so it's not used elsewhere considering it doesn't handle all corner cases. - Use bitmask rather than hash table to handle tcs outputs (Ken) Reviewed-by: Kenneth Graunke --- src/compiler/Makefile.sources | 1 + src/compiler/nir/nir.h | 3 + src/compiler/nir/nir_linking_helpers.c | 147 +++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 src/compiler/nir/nir_linking_helpers.c diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index 0e9a84e..36906f4 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -202,6 +202,7 @@ NIR_FILES = \ nir/nir_instr_set.h \ nir/nir_intrinsics.c \ nir/nir_intrinsics.h \ + nir/nir_linking_helpers.c \ nir/nir_liveness.c \ nir/nir_loop_analyze.c \ nir/nir_loop_analyze.h \ diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index fab2110..bb5aba6 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -2417,6 +2417,9 @@ void nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint); void nir_assign_var_locations(struct exec_list *var_list, unsigned *size, int (*type_size)(const struct glsl_type *)); +/* Some helpers to do very simple linking */ +bool nir_remove_unused_varyings(nir_shader *producer, nir_shader *consumer); + typedef enum { /* If set, this forces all non-flat fragment shader inputs to be * interpolated as if with the "sample" qualifier. This requires diff --git a/src/compiler/nir/nir_linking_helpers.c b/src/compiler/nir/nir_linking_helpers.c new file mode 100644 index 0000000..838054e --- /dev/null +++ b/src/compiler/nir/nir_linking_helpers.c @@ -0,0 +1,147 @@ +/* + * 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" +#include "util/set.h" +#include "util/hash_table.h" + +/* This file contains various little helpers for doing simple linking in + * NIR. Eventually, we'll probably want a full-blown varying packing + * implementation in here. Right now, it just deletes unused things. + */ + +/** + * Returns the bits in the inputs_read, outputs_written, or + * system_values_read bitfield corresponding to this variable. + */ +static uint64_t +get_variable_io_mask(nir_variable *var, gl_shader_stage stage) +{ + /* TODO: add support for tess patches */ + if (var->data.patch || var->data.location < 0) + return 0; + + assert(var->data.mode == nir_var_shader_in || + var->data.mode == nir_var_shader_out || + var->data.mode == nir_var_system_value); + assert(var->data.location >= 0); + + const struct glsl_type *type = var->type; + if (nir_is_per_vertex_io(var, stage)) { + assert(glsl_type_is_array(type)); + type = glsl_get_array_element(type); + } + + unsigned slots = glsl_count_attribute_slots(type, false); + return ((1ull << slots) - 1) << var->data.location; +} + +static uint64_t +tcs_output_read_bitmask(nir_shader *shader) +{ + uint64_t read = 0; + + nir_foreach_function(function, shader) { + if (function->impl) { + nir_foreach_block(block, function->impl) { + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + + nir_intrinsic_instr *intrin_instr = + nir_instr_as_intrinsic(instr); + if (intrin_instr->intrinsic == nir_intrinsic_load_var && + intrin_instr->variables[0]->var->data.mode == + nir_var_shader_out) { + read |= get_variable_io_mask(intrin_instr->variables[0]->var, + shader->stage); + } + } + } + } + } + + return read; +} + +static bool +remove_unused_io_vars(nir_shader *shader, struct exec_list *var_list, + uint64_t used_by_other_stage) +{ + bool progress = false; + + nir_foreach_variable_safe(var, var_list) { + /* TODO: add patch support */ + if (var->data.patch) + continue; + + if (var->data.location < VARYING_SLOT_VAR0 && var->data.location >= 0) + continue; + + if (var->data.always_active_io) + continue; + + if (!(used_by_other_stage & get_variable_io_mask(var, shader->stage))) { + /* This one is invalid, make it a global variable instead */ + var->data.location = 0; + var->data.mode = nir_var_global; + + exec_node_remove(&var->node); + exec_list_push_tail(&shader->globals, &var->node); + + progress = true; + } + } + + return progress; +} + +bool +nir_remove_unused_varyings(nir_shader *producer, nir_shader *consumer) +{ + assert(producer->stage != MESA_SHADER_FRAGMENT); + assert(consumer->stage != MESA_SHADER_VERTEX); + + uint64_t read = 0, written = 0; + + nir_foreach_variable(var, &producer->outputs) + written |= get_variable_io_mask(var, producer->stage); + + nir_foreach_variable(var, &consumer->inputs) + read |= get_variable_io_mask(var, consumer->stage); + + /* Each TCS invocation can read data written by other TCS invocations, + * so even if the outputs are not used by the TES we must also make + * sure they are not read by the TCS before demoting them to globals. + */ + if (producer->stage == MESA_SHADER_TESS_CTRL) + read |= tcs_output_read_bitmask(producer); + + bool progress = false; + progress = remove_unused_io_vars(producer, &producer->outputs, read); + + progress = + remove_unused_io_vars(consumer, &consumer->inputs, written) || progress; + + return progress; +} -- 2.7.4