This shuffles constants down in the reverse of what the previous patch does and applies some simpilifications that may be made possible from doing so.
Shader-db results BDW: total instructions in shared programs: 12978405 -> 12975412 (-0.02%) instructions in affected programs: 279862 -> 276869 (-1.07%) helped: 1230 HURT: 125 total cycles in shared programs: 246622124 -> 246625712 (0.00%) cycles in affected programs: 11311912 -> 11315500 (0.03%) helped: 1666 HURT: 1342 --- src/compiler/nir/nir.h | 1 + src/compiler/nir/nir_opt_algebraic.py | 23 +++++++++++++++++++++++ src/mesa/drivers/dri/i965/brw_nir.c | 6 ++++++ 3 files changed, 30 insertions(+) diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 8bbc41d..8d6f7d1 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -2569,6 +2569,7 @@ bool nir_lower_phis_to_regs_block(nir_block *block); bool nir_lower_ssa_defs_to_regs_block(nir_block *block); bool nir_opt_algebraic(nir_shader *shader); +bool nir_opt_algebraic_before_ffma(nir_shader *shader); bool nir_opt_algebraic_late(nir_shader *shader); bool nir_opt_constant_folding(nir_shader *shader); diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 797fc4b..a1c18cc 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -513,6 +513,27 @@ for op in ['flt', 'fge', 'feq', 'fne', ('bcsel', 'a', (op, 'd', 'b'), (op, 'd', 'c'))), ] +# This section contains "late" optimizations that should be run before +# creating ffmas and calling regular optimizations for the final time. +# Optimizations should go here if they help code generation and conflict +# with the regular optimizations. +before_ffma_optimizations = [ + # Propagate constants down multiplication chains + (('fmul(is_used_once)', ('fmul(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('fmul', ('fmul', a, c), b)), + (('imul(is_used_once)', ('imul(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('imul', ('imul', a, c), b)), + (('fadd(is_used_once)', ('fadd(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('fadd', ('fadd', a, c), b)), + (('iadd(is_used_once)', ('iadd(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('iadd', ('iadd', a, c), b)), + + (('~fadd', ('fmul', a, b), ('fmul', a, c)), ('fmul', a, ('fadd', b, c))), + (('iadd', ('imul', a, b), ('imul', a, c)), ('imul', a, ('iadd', b, c))), + (('~fadd', ('fneg', a), a), 0.0), + (('iadd', ('ineg', a), a), 0), + (('iadd', ('ineg', a), ('iadd', a, b)), b), + (('iadd', a, ('iadd', ('ineg', a), b)), b), + (('~fadd', ('fneg', a), ('fadd', a, b)), b), + (('~fadd', a, ('fadd', ('fneg', a), b)), b), +] + # This section contains "late" optimizations that should be run after the # regular optimizations have finished. Optimizations should go here if # they help code generation but do not necessarily produce code that is @@ -539,5 +560,7 @@ late_optimizations = [ ] print nir_algebraic.AlgebraicPass("nir_opt_algebraic", optimizations).render() +print nir_algebraic.AlgebraicPass("nir_opt_algebraic_before_ffma", + before_ffma_optimizations).render() print nir_algebraic.AlgebraicPass("nir_opt_algebraic_late", late_optimizations).render() diff --git a/src/mesa/drivers/dri/i965/brw_nir.c b/src/mesa/drivers/dri/i965/brw_nir.c index a5912a0..999e1d2 100644 --- a/src/mesa/drivers/dri/i965/brw_nir.c +++ b/src/mesa/drivers/dri/i965/brw_nir.c @@ -604,6 +604,12 @@ brw_postprocess_nir(nir_shader *nir, const struct brw_compiler *compiler, bool progress; /* Written by OPT and OPT_V */ (void)progress; + + do { + progress = false; + OPT(nir_opt_algebraic_before_ffma); + } while (progress); + nir = nir_optimize(nir, compiler, is_scalar); if (devinfo->gen >= 6) { -- 2.9.3 _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev