From 7ae3d3eb8d9053abe20a194e66583b41a57f3040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Ondra=C4=8Dka?= Date: Tue, 22 Aug 2023 16:08:01 +0200 Subject: [PATCH] r300: convert x * 2 into x + x for presubtract total instructions in shared programs: 128859 -> 128864 (<.01%) instructions in affected programs: 931 -> 936 (0.54%) helped: 0 HURT: 5 total presub in shared programs: 7635 -> 7682 (0.62%) presub in affected programs: 208 -> 255 (22.60%) helped: 0 HURT: 17 total cycles in shared programs: 194124 -> 194101 (-0.01%) cycles in affected programs: 1671 -> 1648 (-1.38%) helped: 9 HURT: 1 Reviewed-by: Filip Gawin Part-of: --- src/gallium/drivers/r300/compiler/r300_nir.h | 21 +++++++++++++++++++ .../r300/compiler/r300_nir_algebraic.py | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/src/gallium/drivers/r300/compiler/r300_nir.h b/src/gallium/drivers/r300/compiler/r300_nir.h index 951e0a8cdbd..916eb08fd48 100644 --- a/src/gallium/drivers/r300/compiler/r300_nir.h +++ b/src/gallium/drivers/r300/compiler/r300_nir.h @@ -26,6 +26,27 @@ #include "pipe/p_screen.h" #include "compiler/nir/nir.h" +static inline bool +is_ubo_or_input(UNUSED struct hash_table *ht, const nir_alu_instr *instr, + unsigned src, unsigned num_components, + const uint8_t *swizzle) +{ + nir_instr *parent = instr->src[src].src.ssa->parent_instr; + if (parent->type != nir_instr_type_intrinsic) + return false; + + nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(parent); + + switch (intrinsic->intrinsic) { + case nir_intrinsic_load_ubo_vec4: + case nir_intrinsic_load_input: + case nir_intrinsic_load_interpolated_input: + return true; + default: + return false; + } +} + char *r300_finalize_nir(struct pipe_screen *pscreen, void *nir); extern bool r300_transform_vs_trig_input(struct nir_shader *shader); diff --git a/src/gallium/drivers/r300/compiler/r300_nir_algebraic.py b/src/gallium/drivers/r300/compiler/r300_nir_algebraic.py index c27dba90ec4..485943e16f2 100644 --- a/src/gallium/drivers/r300/compiler/r300_nir_algebraic.py +++ b/src/gallium/drivers/r300/compiler/r300_nir_algebraic.py @@ -76,6 +76,10 @@ r300_nir_prepare_presubtract = [ (('ffma', -2.0, a, 1.0), ('fneg', ('ffma', ('fneg', a), 2.0, 1.0))), (('ffma', 2.0, a, -1.0), ('fneg', ('ffma', ('fneg', a), 2.0, 1.0))), (('ffma', a, 2.0, -1.0), ('fneg', ('ffma', ('fneg', a), 2.0, 1.0))), + # x * 2 can be usually folded into output modifier for the previous + # instruction, but that only works if x is a temporary. If it is input or + # constant just convert it to add instead. + (('fmul', 'a(is_ubo_or_input)', 2.0), ('fadd', a, a)), ] # Previous prepare_presubtract pass can sometimes produce double fneg patterns.