From 5ae3bd616c1b2be7db7f13cfe87496e650264c04 Mon Sep 17 00:00:00 2001 From: Christian Gmeiner Date: Wed, 19 Jul 2023 10:41:16 +0200 Subject: [PATCH] etnaviv: nir: add etna_nir_lower_to_source_mods(..) This is more or less a copy of nir_lower_to_source_mods(..) with the following differences: - we store the source mods in pass_flags - we do not try to saturate the destination Signed-off-by: Christian Gmeiner Acked-by: Alyssa Rosenzweig Acked-by: Yonggang Luo Acked-by: Lucas Stach Part-of: --- src/gallium/drivers/etnaviv/etnaviv_nir.h | 3 + .../etnaviv/etnaviv_nir_lower_source_mods.c | 124 ++++++++++++++++++ src/gallium/drivers/etnaviv/meson.build | 1 + 3 files changed, 128 insertions(+) create mode 100644 src/gallium/drivers/etnaviv/etnaviv_nir_lower_source_mods.c diff --git a/src/gallium/drivers/etnaviv/etnaviv_nir.h b/src/gallium/drivers/etnaviv/etnaviv_nir.h index ddab69beebc..fea22e51733 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_nir.h +++ b/src/gallium/drivers/etnaviv/etnaviv_nir.h @@ -37,6 +37,9 @@ etna_lower_alu(nir_shader *shader, bool has_new_transcendentals); bool etna_nir_lower_texture(nir_shader *s, struct etna_shader_key *key); +bool +etna_nir_lower_to_source_mods(nir_shader *shader); + bool etna_nir_lower_ubo_to_uniform(nir_shader *shader); diff --git a/src/gallium/drivers/etnaviv/etnaviv_nir_lower_source_mods.c b/src/gallium/drivers/etnaviv/etnaviv_nir_lower_source_mods.c new file mode 100644 index 00000000000..1d22bf7a969 --- /dev/null +++ b/src/gallium/drivers/etnaviv/etnaviv_nir_lower_source_mods.c @@ -0,0 +1,124 @@ +/* + * Copyright © 2014 Intel Corporation + * Copyright © 2023 Igalia S.L. + * + * 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 "etnaviv_nir.h" +#include "etnaviv_compiler_nir.h" + +/* + * This pass lowers the neg and abs operations to pass_flags on + * ALU operations. + */ + +static void +alu_src_consume_abs(nir_instr *instr, unsigned idx) +{ + set_src_mod_abs(instr, idx); +} + +static void +alu_src_consume_negate(nir_instr *instr, unsigned idx) +{ + /* If abs is set on the source, the negate goes away */ + if (!is_src_mod_abs(instr, idx)) + toggle_src_mod_neg(instr, idx); +} + +static bool +nir_lower_to_source_mods_instr(nir_builder *b, nir_instr *instr, + void *data) +{ + bool progress = false; + + if (instr->type != nir_instr_type_alu) + return false; + + nir_alu_instr *alu = nir_instr_as_alu(instr); + + for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) { + if (!alu->src[i].src.is_ssa) + continue; + + if (alu->src[i].src.ssa->parent_instr->type != nir_instr_type_alu) + continue; + + nir_alu_instr *parent = nir_instr_as_alu(alu->src[i].src.ssa->parent_instr); + + if (parent->dest.saturate) + continue; + + if (nir_alu_type_get_base_type(nir_op_infos[alu->op].input_types[i]) != nir_type_float) + continue; + + if (!(parent->op == nir_op_fabs) && + !(parent->op == nir_op_fneg)) { + continue; + } + + if (nir_src_bit_size(alu->src[i].src) == 64) + continue; + + /* We can only store up to 3 source modifiers. */ + if (i >= 3) + continue; + + nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src); + + /* Apply any modifiers that come from the parent opcode */ + if (parent->op == nir_op_fneg) + alu_src_consume_negate(instr, i); + if (parent->op == nir_op_fabs) + alu_src_consume_abs(instr, i); + + /* Apply modifiers from the parent source */ + if (parent->src[0].negate || is_src_mod_neg(&parent->instr, 0)) + alu_src_consume_negate(instr, i); + if (parent->src[0].abs || is_src_mod_abs(&parent->instr, 0)) + alu_src_consume_abs(instr, i); + + for (int j = 0; j < 4; ++j) { + if (!nir_alu_instr_channel_used(alu, i, j)) + continue; + alu->src[i].swizzle[j] = parent->src[0].swizzle[alu->src[i].swizzle[j]]; + } + + if (nir_ssa_def_is_unused(&parent->dest.dest.ssa)) + nir_instr_remove(&parent->instr); + + progress = true; + } + + return progress; +} + +bool +etna_nir_lower_to_source_mods(nir_shader *shader) +{ + nir_shader_clear_pass_flags(shader); + + return nir_shader_instructions_pass(shader, + nir_lower_to_source_mods_instr, + nir_metadata_block_index | + nir_metadata_dominance, + NULL); +} diff --git a/src/gallium/drivers/etnaviv/meson.build b/src/gallium/drivers/etnaviv/meson.build index b84d1be315a..331c303f7ec 100644 --- a/src/gallium/drivers/etnaviv/meson.build +++ b/src/gallium/drivers/etnaviv/meson.build @@ -57,6 +57,7 @@ files_etnaviv = files( 'etnaviv_format.c', 'etnaviv_format.h', 'etnaviv_internal.h', + 'etnaviv_nir_lower_source_mods.c', 'etnaviv_nir_lower_texture.c', 'etnaviv_nir_lower_ubo_to_uniform.c', 'etnaviv_nir.c',