From: Alyssa Rosenzweig Date: Wed, 29 Apr 2020 21:51:03 +0000 (-0400) Subject: panfrost: Add modifier detection helpers X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=aeb55180ff34c50006dc3ba49097e9834bf32171;p=mesa.git panfrost: Add modifier detection helpers With the goal of removing modifiers from NIR, these helpers let us detect modifier patterns without mutating the underlying NIR. These were intended for upstream, but due to various issues are being (temporarily) vendored. Signed-off-by: Alyssa Rosenzweig Part-of: --- diff --git a/src/panfrost/Makefile.sources b/src/panfrost/Makefile.sources index 06062b16749..2760a5a65bd 100644 --- a/src/panfrost/Makefile.sources +++ b/src/panfrost/Makefile.sources @@ -84,6 +84,7 @@ pandecode_FILES := \ util_FILES := \ util/lcra.c \ util/lcra.h \ + util/nir_mod_helpers.c \ util/pan_ir.c \ util/pan_ir.h \ util/pan_liveness.c \ diff --git a/src/panfrost/util/meson.build b/src/panfrost/util/meson.build index dfc4b3fc6d3..aa3b5decbc8 100644 --- a/src/panfrost/util/meson.build +++ b/src/panfrost/util/meson.build @@ -22,6 +22,7 @@ libpanfrost_util_files = files( 'lcra.c', 'lcra.h', + 'nir_mod_helpers.c', 'pan_ir.c', 'pan_ir.h', 'pan_liveness.c', diff --git a/src/panfrost/util/nir_mod_helpers.c b/src/panfrost/util/nir_mod_helpers.c new file mode 100644 index 00000000000..2fe7b4fabe5 --- /dev/null +++ b/src/panfrost/util/nir_mod_helpers.c @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2020 Collabora, Ltd. + * Copyright (C) 2014 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. + * + * Authors: + * Alyssa Rosenzweig + * Jason Ekstrand (jason@jlekstrand.net) + * + */ + +#include "nir.h" +#include "pan_ir.h" + +/* Check if a given ALU source is the result of a particular componentwise 1-op + * ALU source (principally fneg or fabs). If so, return true and rewrite the + * source to be the argument, respecting swizzles as needed. If not (or it + * cannot be proven), return false and leave the source untouched. +*/ + +bool +pan_has_source_mod(nir_alu_src *src, nir_op op) +{ + if (!src->src.is_ssa || src->src.ssa->parent_instr->type != nir_instr_type_alu) + return false; + + nir_alu_instr *alu = nir_instr_as_alu(src->src.ssa->parent_instr); + + if (alu->op != op) + return false; + + /* This only works for unary ops */ + assert(nir_op_infos[op].num_inputs == 1); + + /* If the copied source is not SSA, moving it might not be valid */ + if (!alu->src[0].src.is_ssa) + return false; + + /* Okay - we've found the modifier we wanted. Let's construct the new ALU + * src. In a scalar world, this is just psrc, but for vector archs we need + * to respect the swizzle, so we compose. + */ + + nir_alu_src nsrc = { + .src = alu->src[0].src, + }; + + for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; ++i) { + /* (a o b)(i) = a(b(i)) ... swizzle composition is intense. */ + nsrc.swizzle[i] = alu->src[0].swizzle[src->swizzle[i]]; + } + + *src = nsrc; + return true; +} + +/* Check if a given instruction's result will be fed into a + * componentwise 1-op ALU instruction (principally fsat without + * swizzles). If so, return true and rewrite the destination. The + * backend will need to track the new destinations to avoid + * incorrect double-emits. */ + +bool +pan_has_dest_mod(nir_dest **odest, nir_op op) +{ + /* This only works for unary ops */ + assert(nir_op_infos[op].num_inputs == 1); + + /* If not SSA, this might not be legal */ + nir_dest *dest = *odest; + if (!dest->is_ssa) + return false; + + /* Check the uses. We want a single use, with the op `op` */ + if (!list_is_empty(&dest->ssa.if_uses)) + return false; + + if (!list_is_singular(&dest->ssa.uses)) + return false; + + nir_src *use = list_first_entry(&dest->ssa.uses, nir_src, use_link); + nir_instr *parent = use->parent_instr; + + /* Check if the op is `op` */ + if (parent->type != nir_instr_type_alu) + return false; + + nir_alu_instr *alu = nir_instr_as_alu(parent); + if (alu->op != op) + return false; + + /* We can't do expansions without a move in the middle */ + unsigned nr_components = nir_dest_num_components(alu->dest.dest); + + if (nir_dest_num_components(*dest) != nr_components) + return false; + + /* We don't handle swizzles here, so check for the identity */ + for (unsigned i = 0; i < nr_components; ++i) { + if (alu->src[0].swizzle[i] != i) + return false; + } + + if (!alu->dest.dest.is_ssa) + return false; + + /* Otherwise, we're good */ + *odest = &alu->dest.dest; + return true; +} diff --git a/src/panfrost/util/pan_ir.h b/src/panfrost/util/pan_ir.h index a45478ae6ec..221967d3b5d 100644 --- a/src/panfrost/util/pan_ir.h +++ b/src/panfrost/util/pan_ir.h @@ -220,4 +220,8 @@ pan_dest_index(nir_dest *dst) /* IR printing helpers */ void pan_print_alu_type(nir_alu_type t, FILE *fp); +/* Until it can be upstreamed.. */ +bool pan_has_source_mod(nir_alu_src *src, nir_op op); +bool pan_has_dest_mod(nir_dest **dest, nir_op op); + #endif