Mesa (master): nir,vc4: Lower fneg to fmul(x, -1.0)

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Jan 11 19:40:40 UTC 2021


Module: Mesa
Branch: master
Commit: b3ce55b4452d120c36063c2265f9fe4c7c07975a
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=b3ce55b4452d120c36063c2265f9fe4c7c07975a

Author: Daniel Schürmann <daniel at schuermann.dev>
Date:   Thu Aug 27 14:35:04 2020 +0100

nir,vc4: Lower fneg to fmul(x, -1.0)

This patch also replaces lower_negate with lower_ineg / lower_fneg.

The fneg semantics have been clarified as of Version 1.5, Revision 1
of the SPIR-V specification, which means that the previous lowering
to fsub is not a viable solution anymore, and is replaced with
lowering to fmul(x, -1.0).

Reviewed-by: Erik Faye-Lund <erik.faye-lund at collabora.com>
Reviewed-by: Eric Anholt <eric at anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6597>

---

 src/compiler/nir/nir.h                                   | 6 ++++--
 src/compiler/nir/nir_opt_algebraic.py                    | 6 +++---
 src/gallium/drivers/nouveau/codegen/nv50_ir_from_nir.cpp | 3 ++-
 src/gallium/drivers/vc4/vc4_program.c                    | 3 ++-
 src/microsoft/compiler/nir_to_dxil.c                     | 3 ++-
 5 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index f4963ef7060..1f2b92fc0d4 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -3133,8 +3133,10 @@ typedef struct nir_shader_compiler_options {
    bool lower_usub_borrow;
    /** Lowers imul_high/umul_high to 16-bit multiplies and carry operations. */
    bool lower_mul_high;
-   /** lowers fneg and ineg to fsub and isub. */
-   bool lower_negate;
+   /** lowers fneg to fmul(x, -1.0). Driver must call nir_opt_algebraic_late() */
+   bool lower_fneg;
+   /** lowers ineg to isub. Driver must call nir_opt_algebraic_late(). */
+   bool lower_ineg;
    /** lowers fsub and isub to fadd+fneg and iadd+ineg. */
    bool lower_sub;
 
diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py
index bd9b786b91c..db4e6f4d487 100644
--- a/src/compiler/nir/nir_opt_algebraic.py
+++ b/src/compiler/nir/nir_opt_algebraic.py
@@ -2107,9 +2107,9 @@ late_optimizations = [
 
    # Subtractions get lowered during optimization, so we need to recombine them
    (('fadd', 'a', ('fneg', 'b')), ('fsub', 'a', 'b'), '!options->lower_sub'),
-   (('iadd', 'a', ('ineg', 'b')), ('isub', 'a', 'b'), '!options->lower_sub'),
-   (('fneg', a), ('fsub', 0.0, a), 'options->lower_negate'),
-   (('ineg', a), ('isub', 0, a), 'options->lower_negate'),
+   (('fneg', a), ('fmul', a, -1.0), 'options->lower_fneg'),
+   (('iadd', a, ('ineg', 'b')), ('isub', 'a', 'b'), '!options->lower_sub || options->lower_ineg'),
+   (('ineg', a), ('isub', 0, a), 'options->lower_ineg'),
    (('iabs', a), ('imax', a, ('ineg', a)), 'options->lower_iabs'),
    (('~fadd at 16', ('fmul', a, b), c), ('ffma', a, b, c), 'options->fuse_ffma16'),
    (('~fadd at 32', ('fmul', a, b), c), ('ffma', a, b, c), 'options->fuse_ffma32'),
diff --git a/src/gallium/drivers/nouveau/codegen/nv50_ir_from_nir.cpp b/src/gallium/drivers/nouveau/codegen/nv50_ir_from_nir.cpp
index dca45e7b73e..f47e0e13c2a 100644
--- a/src/gallium/drivers/nouveau/codegen/nv50_ir_from_nir.cpp
+++ b/src/gallium/drivers/nouveau/codegen/nv50_ir_from_nir.cpp
@@ -3232,7 +3232,8 @@ nvir_nir_shader_compiler_options(int chipset)
    op.lower_uadd_carry = true; // TODO
    op.lower_usub_borrow = true; // TODO
    op.lower_mul_high = false;
-   op.lower_negate = false;
+   op.lower_fneg = false;
+   op.lower_ineg = false;
    op.lower_sub = true;
    op.lower_scmp = true; // TODO: not implemented yet
    op.lower_vector_cmp = false;
diff --git a/src/gallium/drivers/vc4/vc4_program.c b/src/gallium/drivers/vc4/vc4_program.c
index c2ea353166b..f320256da29 100644
--- a/src/gallium/drivers/vc4/vc4_program.c
+++ b/src/gallium/drivers/vc4/vc4_program.c
@@ -2183,7 +2183,8 @@ static const nir_shader_compiler_options nir_options = {
         .lower_fsat = true,
         .lower_fsqrt = true,
         .lower_ldexp = true,
-        .lower_negate = true,
+        .lower_fneg = true,
+        .lower_ineg = true,
         .lower_rotate = true,
         .lower_to_scalar = true,
         .lower_umax = true,
diff --git a/src/microsoft/compiler/nir_to_dxil.c b/src/microsoft/compiler/nir_to_dxil.c
index e072d62425c..39089d2f245 100644
--- a/src/microsoft/compiler/nir_to_dxil.c
+++ b/src/microsoft/compiler/nir_to_dxil.c
@@ -70,7 +70,8 @@ DEBUG_GET_ONCE_FLAGS_OPTION(debug_dxil, "DXIL_DEBUG", dxil_debug_options, 0)
 
 static const nir_shader_compiler_options
 nir_options = {
-   .lower_negate = true,
+   .lower_ineg = true,
+   .lower_fneg = true,
    .lower_ffma16 = true,
    .lower_ffma32 = true,
    .lower_isign = true,



More information about the mesa-commit mailing list