From: Matt Turner Date: Thu, 6 Jul 2017 04:57:44 +0000 (-0700) Subject: nir: Add pass to scalarize read_invocation/read_first_invocation X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=3e7b8f6cd40810601982d55d6e8d6935f0fde78c;p=mesa.git nir: Add pass to scalarize read_invocation/read_first_invocation i965 will want these to be scalar operations. Reviewed-by: Connor Abbott Reviewed-by: Kenneth Graunke --- diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index c3a5454e06b..6e08dfb8448 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -229,6 +229,7 @@ NIR_FILES = \ nir/nir_lower_passthrough_edgeflags.c \ nir/nir_lower_patch_vertices.c \ nir/nir_lower_phis_to_scalar.c \ + nir/nir_lower_read_invocation_to_scalar.c \ nir/nir_lower_regs_to_ssa.c \ nir/nir_lower_returns.c \ nir/nir_lower_samplers.c \ diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 86090069520..5ddab57776f 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -2432,7 +2432,7 @@ bool nir_move_vec_src_uses_to_dest(nir_shader *shader); bool nir_lower_vec_to_movs(nir_shader *shader); bool nir_lower_alu_to_scalar(nir_shader *shader); bool nir_lower_load_const_to_scalar(nir_shader *shader); - +bool nir_lower_read_invocation_to_scalar(nir_shader *shader); bool nir_lower_phis_to_scalar(nir_shader *shader); void nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask); diff --git a/src/compiler/nir/nir_lower_read_invocation_to_scalar.c b/src/compiler/nir/nir_lower_read_invocation_to_scalar.c new file mode 100644 index 00000000000..69e7c0a73a6 --- /dev/null +++ b/src/compiler/nir/nir_lower_read_invocation_to_scalar.c @@ -0,0 +1,112 @@ +/* + * Copyright © 2017 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 "nir_builder.h" + +/** @file nir_lower_read_invocation_to_scalar.c + * + * Replaces nir_intrinsic_read_invocation/nir_intrinsic_read_first_invocation + * operations with num_components != 1 with individual per-channel operations. + */ + +static void +lower_read_invocation_to_scalar(nir_builder *b, nir_intrinsic_instr *intrin) +{ + b->cursor = nir_before_instr(&intrin->instr); + + nir_ssa_def *value = nir_ssa_for_src(b, intrin->src[0], intrin->num_components); + nir_ssa_def *reads[4]; + + for (unsigned i = 0; i < intrin->num_components; i++) { + nir_intrinsic_instr *chan_intrin = + nir_intrinsic_instr_create(b->shader, intrin->intrinsic); + nir_ssa_dest_init(&chan_intrin->instr, &chan_intrin->dest, + 1, intrin->dest.ssa.bit_size, NULL); + chan_intrin->num_components = 1; + + /* value */ + chan_intrin->src[0] = nir_src_for_ssa(nir_channel(b, value, i)); + /* invocation */ + if (intrin->intrinsic == nir_intrinsic_read_invocation) + nir_src_copy(&chan_intrin->src[1], &intrin->src[1], chan_intrin); + + nir_builder_instr_insert(b, &chan_intrin->instr); + + reads[i] = &chan_intrin->dest.ssa; + } + + nir_ssa_def_rewrite_uses(&intrin->dest.ssa, + nir_src_for_ssa(nir_vec(b, reads, + intrin->num_components))); + nir_instr_remove(&intrin->instr); +} + +static bool +nir_lower_read_invocation_to_scalar_impl(nir_function_impl *impl) +{ + bool progress = false; + nir_builder b; + nir_builder_init(&b, impl); + + nir_foreach_block(block, impl) { + nir_foreach_instr_safe(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + + nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); + + if (intrin->num_components == 1) + continue; + + switch (intrin->intrinsic) { + case nir_intrinsic_read_invocation: + case nir_intrinsic_read_first_invocation: + lower_read_invocation_to_scalar(&b, intrin); + progress = true; + break; + default: + break; + } + } + } + + if (progress) { + nir_metadata_preserve(impl, nir_metadata_block_index | + nir_metadata_dominance); + } + return progress; +} + +bool +nir_lower_read_invocation_to_scalar(nir_shader *shader) +{ + bool progress = false; + + nir_foreach_function(function, shader) { + if (function->impl) + progress |= nir_lower_read_invocation_to_scalar_impl(function->impl); + } + + return progress; +}