From 040a350b1eb0be13613c7ddaa8524ac4afc45cfc Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Wed, 10 Feb 2021 11:47:24 -0500 Subject: [PATCH] pan/bi: Add SSA-based scalar copy propagation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a very simple (and slow...) copyprop pass. It's good enough to get rid of redundant moves from FAU, but it doesn't help for vector combines. total instructions in shared programs: 175219 -> 169141 (-3.47%) instructions in affected programs: 91439 -> 85361 (-6.65%) helped: 599 HURT: 0 helped stats (abs) min: 1 max: 112 x̄: 10.15 x̃: 6 helped stats (rel) min: 0.30% max: 33.33% x̄: 8.61% x̃: 8.04% 95% mean confidence interval for instructions value: -11.06 -9.24 95% mean confidence interval for instructions %-change: -9.07% -8.16% Instructions are helped. total nops in shared programs: 120011 -> 121049 (0.86%) nops in affected programs: 47355 -> 48393 (2.19%) helped: 110 HURT: 309 helped stats (abs) min: 1 max: 6 x̄: 2.07 x̃: 2 helped stats (rel) min: 0.44% max: 16.67% x̄: 3.59% x̃: 3.16% HURT stats (abs) min: 1 max: 56 x̄: 4.10 x̃: 2 HURT stats (rel) min: 0.32% max: 80.85% x̄: 6.85% x̃: 3.12% 95% mean confidence interval for nops value: 1.86 3.09 95% mean confidence interval for nops %-change: 3.08% 5.14% Nops are HURT. total clauses in shared programs: 40576 -> 40388 (-0.46%) clauses in affected programs: 3074 -> 2886 (-6.12%) helped: 106 HURT: 0 helped stats (abs) min: 1 max: 4 x̄: 1.77 x̃: 2 helped stats (rel) min: 0.42% max: 22.22% x̄: 7.17% x̃: 6.90% 95% mean confidence interval for clauses value: -1.91 -1.63 95% mean confidence interval for clauses %-change: -7.80% -6.53% Clauses are helped. total quadwords in shared programs: 146590 -> 144937 (-1.13%) quadwords in affected programs: 59475 -> 57822 (-2.78%) helped: 493 HURT: 1 helped stats (abs) min: 1 max: 28 x̄: 3.35 x̃: 2 helped stats (rel) min: 0.28% max: 15.38% x̄: 4.08% x̃: 3.85% HURT stats (abs) min: 1 max: 1 x̄: 1.00 x̃: 1 HURT stats (rel) min: 2.38% max: 2.38% x̄: 2.38% x̃: 2.38% 95% mean confidence interval for quadwords value: -3.61 -3.08 95% mean confidence interval for quadwords %-change: -4.33% -3.81% Quadwords are helped. total spills in shared programs: 1106 -> 1106 (0.00%) spills in affected programs: 0 -> 0 helped: 0 HURT: 0 total fills in shared programs: 2241 -> 2241 (0.00%) fills in affected programs: 0 -> 0 helped: 0 HURT: 0 Signed-off-by: Alyssa Rosenzweig Reviewed-by: Boris Brezillon Part-of: --- src/panfrost/Makefile.sources | 1 + src/panfrost/bifrost/bi_opt_copy_prop.c | 64 +++++++++++++++++++++++++ src/panfrost/bifrost/bifrost_compile.c | 2 + src/panfrost/bifrost/compiler.h | 1 + src/panfrost/bifrost/meson.build | 1 + 5 files changed, 69 insertions(+) create mode 100644 src/panfrost/bifrost/bi_opt_copy_prop.c diff --git a/src/panfrost/Makefile.sources b/src/panfrost/Makefile.sources index bfe4b803144..4ea3ccffeea 100644 --- a/src/panfrost/Makefile.sources +++ b/src/panfrost/Makefile.sources @@ -9,6 +9,7 @@ bifrost_FILES := \ bifrost/bi_print.c \ bifrost/bi_print.h \ bifrost/bi_ra.c \ + bifrost/bi_opt_copy_prop.c \ bifrost/bi_opt_dce.c \ bifrost/bi_quirks.h \ bifrost/bi_test_pack.c \ diff --git a/src/panfrost/bifrost/bi_opt_copy_prop.c b/src/panfrost/bifrost/bi_opt_copy_prop.c new file mode 100644 index 00000000000..48e2ca2bd9e --- /dev/null +++ b/src/panfrost/bifrost/bi_opt_copy_prop.c @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2018 Alyssa Rosenzweig + * Copyright (C) 2019-2020 Collabora, Ltd. + * + * 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 "compiler.h" + +/* A simple scalar-only SSA-based copy-propagation pass. O(N^2) due to the lack + * of use tracking. TODO: better data structures for O(N), TODO: vectors */ + +static bool +bi_rewrite_scalar_uses(bi_context *ctx, bi_index old, bi_index new) +{ + bool progress = false; + + bi_foreach_instr_global(ctx, use) { + bi_foreach_src(use, s) { + bi_index src = use->src[s]; + bool scalar = (bi_count_read_registers(use, s) == 1); + + if (bi_is_word_equiv(src, old) && scalar) { + use->src[s] = bi_replace_index(src, new); + progress = true; + } + } + } + + return progress; +} + +bool +bi_opt_copy_prop(bi_context *ctx) +{ + bool progress = false; + + bi_foreach_instr_global_safe(ctx, ins) { + if (ins->op != BI_OPCODE_MOV_I32) continue; + if (!bi_is_ssa(ins->dest[0])) continue; + if (!(bi_is_ssa(ins->src[0]) || ins->src[0].type == BI_INDEX_FAU)) continue; + + progress |= bi_rewrite_scalar_uses(ctx, ins->dest[0], ins->src[0]); + } + + return progress; +} diff --git a/src/panfrost/bifrost/bifrost_compile.c b/src/panfrost/bifrost/bifrost_compile.c index 8a27549868e..9ad489babea 100644 --- a/src/panfrost/bifrost/bifrost_compile.c +++ b/src/panfrost/bifrost/bifrost_compile.c @@ -2517,6 +2517,8 @@ bifrost_compile_shader_nir(void *mem_ctx, nir_shader *nir, do { progress = false; + progress |= bi_opt_copy_prop(ctx); + bi_foreach_block(ctx, _block) { bi_block *block = (bi_block *) _block; progress |= bi_opt_dead_code_eliminate(ctx, block, false); diff --git a/src/panfrost/bifrost/compiler.h b/src/panfrost/bifrost/compiler.h index ec04f529d0d..e7086611cd5 100644 --- a/src/panfrost/bifrost/compiler.h +++ b/src/panfrost/bifrost/compiler.h @@ -746,6 +746,7 @@ void bi_print_shader(bi_context *ctx, FILE *fp); /* BIR passes */ +bool bi_opt_copy_prop(bi_context *ctx); bool bi_opt_dead_code_eliminate(bi_context *ctx, bi_block *block, bool soft); void bi_schedule(bi_context *ctx); void bi_register_allocate(bi_context *ctx); diff --git a/src/panfrost/bifrost/meson.build b/src/panfrost/bifrost/meson.build index e30f2953f44..dd893854e54 100644 --- a/src/panfrost/bifrost/meson.build +++ b/src/panfrost/bifrost/meson.build @@ -23,6 +23,7 @@ libpanfrost_bifrost_files = files( 'bi_layout.c', 'bi_liveness.c', 'bi_print.c', + 'bi_opt_copy_prop.c', 'bi_opt_dce.c', 'bi_pack.c', 'bi_ra.c',