Mesa (main): r600/sfn: Clean up some ALU lowering and move code

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Jun 17 11:05:57 UTC 2021


Module: Mesa
Branch: main
Commit: 8aaa08844fcdefb5d3ca9af871ead70ac7bef282
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=8aaa08844fcdefb5d3ca9af871ead70ac7bef282

Author: Gert Wollny <gert.wollny at collabora.com>
Date:   Sun Jun 13 20:20:08 2021 +0200

r600/sfn: Clean up some ALU lowering and move code

Signed-off-by: Gert Wollny <gert.wollny at collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11348>

---

 src/gallium/drivers/r600/Android.mk                |   9 --
 src/gallium/drivers/r600/Makefile.sources          |   2 +
 src/gallium/drivers/r600/meson.build               |  17 +---
 src/gallium/drivers/r600/sfn/sfn_nir.cpp           |  39 +-------
 src/gallium/drivers/r600/sfn/sfn_nir.h             |   4 -
 src/gallium/drivers/r600/sfn/sfn_nir_algebraic.py  |  49 ----------
 src/gallium/drivers/r600/sfn/sfn_nir_lower_alu.cpp | 103 +++++++++++++++++++++
 src/gallium/drivers/r600/sfn/sfn_nir_lower_alu.h   |  11 +++
 8 files changed, 121 insertions(+), 113 deletions(-)

diff --git a/src/gallium/drivers/r600/Android.mk b/src/gallium/drivers/r600/Android.mk
index b87fc91e60a..296aa3ac9b9 100644
--- a/src/gallium/drivers/r600/Android.mk
+++ b/src/gallium/drivers/r600/Android.mk
@@ -49,15 +49,6 @@ $(intermediates)/egd_tables.h: $(MESA_TOP)/src/gallium/drivers/r600/egd_tables.p
 	@echo "Gen Header: $(PRIVATE_MODULE) <= $(notdir $(@))"
 	$(hide) $(MESA_PYTHON2) $(MESA_TOP)/src/gallium/drivers/r600/egd_tables.py $(MESA_TOP)/src/gallium/drivers/r600/evergreend.h > $@
 
-sfn_nir_algebraic_gen := $(LOCAL_PATH)/sfn/sfn_nir_algebraic.py
-sfn_nir_algebraic_deps := \
-	$(LOCAL_PATH)/sfn/sfn_nir_algebraic.py \
-	$(MESA_TOP)/src/compiler/nir/nir_algebraic.py
-
-$(intermediates)/sfn_nir_algebraic.c: $(sfn_nir_algebraic_deps)
-	@mkdir -p $(dir $@)
-	$(hide) $(MESA_PYTHON2) $(sfn_nir_algebraic_gen) -p $(MESA_TOP)/src/compiler/nir/ > $@
-
 ifeq ($(MESA_ENABLE_LLVM),true)
 $(call mesa-build-with-llvm)
 endif
diff --git a/src/gallium/drivers/r600/Makefile.sources b/src/gallium/drivers/r600/Makefile.sources
index 312497b4918..09b5f5e2c4c 100644
--- a/src/gallium/drivers/r600/Makefile.sources
+++ b/src/gallium/drivers/r600/Makefile.sources
@@ -133,6 +133,8 @@ CXX_SOURCES = \
 	sfn/sfn_nir.cpp \
 	sfn/sfn_nir.h \
 	sfn/sfn_nir_legalize_image_load_store.cpp \
+	sfn/sfn_nir_lower_alu.cpp \
+	sfn/sfn_nir_lower_alu.h \
 	sfn/sfn_nir_lower_64bit.cpp \
 	sfn/sfn_nir_lower_fs_out_to_vector.cpp \
 	sfn/sfn_nir_lower_fs_out_to_vector.h \
diff --git a/src/gallium/drivers/r600/meson.build b/src/gallium/drivers/r600/meson.build
index 2dd39d26420..2c631bbbb54 100644
--- a/src/gallium/drivers/r600/meson.build
+++ b/src/gallium/drivers/r600/meson.build
@@ -151,6 +151,8 @@ files_r600 = files(
   'sfn/sfn_nir.h',
   'sfn/sfn_nir_legalize_image_load_store.cpp',
   'sfn/sfn_nir_lower_64bit.cpp',
+  'sfn/sfn_nir_lower_alu.cpp',
+  'sfn/sfn_nir_lower_alu.h',
   'sfn/sfn_nir_lower_fs_out_to_vector.cpp',
   'sfn/sfn_nir_lower_fs_out_to_vector.h',
   'sfn/sfn_nir_lower_tess_io.cpp',
@@ -189,19 +191,6 @@ egd_tables_h = custom_target(
   capture : true,
 )
 
-sfn_nir_algebraic_c = custom_target(
-  'sfn_nir_algebraic.c',
-  input : 'sfn/sfn_nir_algebraic.py',
-  output : 'sfn_nir_algebraic.c',
-  command : [
-    prog_python, '@INPUT@',
-    '-p', join_paths(meson.source_root(), 'src/compiler/nir/'),
-  ],
-  capture : true,
-  depend_files : nir_algebraic_py,
-)
-
-
 r600_c_args = []
 if with_gallium_opencl
   r600_c_args += '-DHAVE_OPENCL'
@@ -209,7 +198,7 @@ endif
 
 libr600 = static_library(
   'r600',
-  [files_r600, egd_tables_h, sfn_nir_algebraic_c],
+  [files_r600, egd_tables_h],
   c_args : [r600_c_args, '-Wstrict-overflow=0'],
   gnu_symbol_visibility : 'hidden',
   include_directories : [
diff --git a/src/gallium/drivers/r600/sfn/sfn_nir.cpp b/src/gallium/drivers/r600/sfn/sfn_nir.cpp
index e221e969f30..bf8588a6227 100644
--- a/src/gallium/drivers/r600/sfn/sfn_nir.cpp
+++ b/src/gallium/drivers/r600/sfn/sfn_nir.cpp
@@ -40,6 +40,7 @@
 #include "sfn_shader_tess_eval.h"
 #include "sfn_nir_lower_fs_out_to_vector.h"
 #include "sfn_ir_to_assembly.h"
+#include "sfn_nir_lower_alu.h"
 
 #include <vector>
 
@@ -337,41 +338,6 @@ bool AssemblyFromShader::lower(const std::vector<InstructionBlock>& ir)
    return do_lower(ir);
 }
 
-static nir_ssa_def *
-r600_nir_lower_pack_unpack_2x16_impl(nir_builder *b, nir_instr *instr, void *_options)
-{
-   nir_alu_instr *alu = nir_instr_as_alu(instr);
-
-   switch (alu->op) {
-   case nir_op_unpack_half_2x16: {
-      nir_ssa_def *packed = nir_ssa_for_alu_src(b, alu, 0);
-      return  nir_vec2(b, nir_unpack_half_2x16_split_x(b, packed),
-                       nir_unpack_half_2x16_split_y(b, packed));
-
-   }
-   case nir_op_pack_half_2x16: {
-      nir_ssa_def *src_vec2 = nir_ssa_for_alu_src(b, alu, 0);
-      return nir_pack_half_2x16_split(b, nir_channel(b, src_vec2, 0),
-                                      nir_channel(b, src_vec2, 1));
-   }
-   default:
-      return nullptr;
-   }
-}
-
-bool r600_nir_lower_pack_unpack_2x16_filter(const nir_instr *instr, const void *_options)
-{
-   return instr->type == nir_instr_type_alu;
-}
-
-bool r600_nir_lower_pack_unpack_2x16(nir_shader *shader)
-{
-   return nir_shader_lower_instructions(shader,
-                                        r600_nir_lower_pack_unpack_2x16_filter,
-                                        r600_nir_lower_pack_unpack_2x16_impl,
-                                        nullptr);
-};
-
 static void
 r600_nir_lower_scratch_address_impl(nir_builder *b, nir_intrinsic_instr *instr)
 {
@@ -610,7 +576,6 @@ r600_nir_lower_atomics(nir_shader *shader)
    return progress;
 }
 using r600::r600_nir_lower_int_tg4;
-using r600::r600_nir_lower_pack_unpack_2x16;
 using r600::r600_lower_scratch_addresses;
 using r600::r600_lower_fs_out_to_vector;
 using r600::r600_lower_ubo_to_align16;
@@ -876,7 +841,7 @@ int r600_shader_from_nir(struct r600_context *rctx,
    idiv_options.allow_fp16 = true;
 
    NIR_PASS_V(sel->nir, nir_lower_idiv, &idiv_options);
-   NIR_PASS_V(sel->nir, r600_lower_alu);
+   NIR_PASS_V(sel->nir, r600_nir_lower_trigen);
    NIR_PASS_V(sel->nir, nir_lower_phis_to_scalar, false);
 
    if (lower_64bit)
diff --git a/src/gallium/drivers/r600/sfn/sfn_nir.h b/src/gallium/drivers/r600/sfn/sfn_nir.h
index 695be1af162..4cec83314e4 100644
--- a/src/gallium/drivers/r600/sfn/sfn_nir.h
+++ b/src/gallium/drivers/r600/sfn/sfn_nir.h
@@ -54,8 +54,6 @@ protected:
 	nir_builder *b;
 };
 
-bool r600_nir_lower_pack_unpack_2x16(nir_shader *shader);
-
 bool r600_lower_scratch_addresses(nir_shader *shader);
 
 bool r600_lower_ubo_to_align16(nir_shader *shader);
@@ -156,8 +154,6 @@ int r600_shader_from_nir(struct r600_context *rctx,
                          struct r600_pipe_shader *pipeshader,
                          union r600_shader_key *key);
 
-bool r600_lower_alu(nir_shader *sh);
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/gallium/drivers/r600/sfn/sfn_nir_algebraic.py b/src/gallium/drivers/r600/sfn/sfn_nir_algebraic.py
deleted file mode 100644
index 2ef06411172..00000000000
--- a/src/gallium/drivers/r600/sfn/sfn_nir_algebraic.py
+++ /dev/null
@@ -1,49 +0,0 @@
-#
-# Copyright (C) 2021 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.
-
-import argparse
-import sys
-
-lower_alu = [
-   # For chipfamily r600 one must do fma (2*pi ffract() - 0.5)
-   (('fsin', "a at 32"), ('fsin_r600', ('fadd', ('ffract', ('ffma', 'a', 0.15915494, 0.5)), -0.5))),
-   (('fcos', "a at 32"), ('fcos_r600', ('fadd', ('ffract', ('ffma', 'a', 0.15915494, 0.5)), -0.5))),
-]
-
-def main():
-    parser = argparse.ArgumentParser()
-    parser.add_argument('-p', '--import-path', required=True)
-    args = parser.parse_args()
-    sys.path.insert(0, args.import_path)
-    run()
-
-
-def run():
-    import nir_algebraic  # pylint: disable=import-error
-
-    print('#include "sfn/sfn_nir.h"')
-
-    print(nir_algebraic.AlgebraicPass("r600_lower_alu",
-                                      lower_alu).render())
-
-if __name__ == '__main__':
-    main()
diff --git a/src/gallium/drivers/r600/sfn/sfn_nir_lower_alu.cpp b/src/gallium/drivers/r600/sfn/sfn_nir_lower_alu.cpp
new file mode 100644
index 00000000000..561c7468f91
--- /dev/null
+++ b/src/gallium/drivers/r600/sfn/sfn_nir_lower_alu.cpp
@@ -0,0 +1,103 @@
+#include "sfn_nir_lower_alu.h"
+#include "sfn_nir.h"
+
+namespace r600 {
+
+class Lower2x16 : public NirLowerInstruction {
+private:
+   bool filter(const nir_instr *instr) const override;
+   nir_ssa_def *lower(nir_instr *instr) override;
+};
+
+
+bool Lower2x16::filter(const nir_instr *instr) const
+{
+   if (instr->type != nir_instr_type_alu)
+      return false;
+   auto alu = nir_instr_as_alu(instr);
+   switch (alu->op) {
+   case nir_op_unpack_half_2x16:
+   case nir_op_pack_half_2x16:
+      return true;
+   default:
+      return false;
+   }
+}
+
+nir_ssa_def *Lower2x16::lower(nir_instr *instr)
+{
+   nir_alu_instr *alu = nir_instr_as_alu(instr);
+
+   switch (alu->op) {
+   case nir_op_unpack_half_2x16: {
+      nir_ssa_def *packed = nir_ssa_for_alu_src(b, alu, 0);
+      return  nir_vec2(b, nir_unpack_half_2x16_split_x(b, packed),
+                       nir_unpack_half_2x16_split_y(b, packed));
+
+   }
+   case nir_op_pack_half_2x16: {
+      nir_ssa_def *src_vec2 = nir_ssa_for_alu_src(b, alu, 0);
+      return nir_pack_half_2x16_split(b, nir_channel(b, src_vec2, 0),
+                                      nir_channel(b, src_vec2, 1));
+   }
+   default:
+      unreachable("Lower2x16 filter doesn't filter correctly");
+   }
+}
+
+class LowerSinCos : public NirLowerInstruction {
+private:
+   bool filter(const nir_instr *instr) const override;
+   nir_ssa_def *lower(nir_instr *instr) override;
+};
+
+bool LowerSinCos::filter(const nir_instr *instr) const
+{
+   if (instr->type != nir_instr_type_alu)
+      return false;
+
+   auto alu = nir_instr_as_alu(instr);
+   switch (alu->op) {
+   case nir_op_fsin:
+   case nir_op_fcos:
+      return true;
+   default:
+      return false;
+   }
+}
+
+nir_ssa_def *LowerSinCos::lower(nir_instr *instr)
+{
+   auto alu = nir_instr_as_alu(instr);
+
+   assert(alu->op == nir_op_fsin ||
+          alu->op == nir_op_fcos);
+
+   auto normalized =
+         nir_fadd(b,
+                  nir_ffract(b,
+                             nir_ffma(b,
+                                      nir_ssa_for_alu_src(b, alu, 0),
+                                      nir_imm_float(b, 0.15915494),
+                                      nir_imm_float(b, 0.5))),
+                              nir_imm_float(b, -0.5));
+
+   if (alu->op == nir_op_fsin)
+      return nir_fsin_r600(b, normalized);
+   else
+      return nir_fcos_r600(b, normalized);
+}
+
+
+} // namespace r600
+
+
+bool r600_nir_lower_pack_unpack_2x16(nir_shader *shader)
+{
+   return r600::Lower2x16().run(shader);
+}
+
+bool r600_nir_lower_trigen(nir_shader *shader)
+{
+   return r600::LowerSinCos().run(shader);
+}
diff --git a/src/gallium/drivers/r600/sfn/sfn_nir_lower_alu.h b/src/gallium/drivers/r600/sfn/sfn_nir_lower_alu.h
new file mode 100644
index 00000000000..6a75493235d
--- /dev/null
+++ b/src/gallium/drivers/r600/sfn/sfn_nir_lower_alu.h
@@ -0,0 +1,11 @@
+#ifndef SFN_NIR_LOWER_ALU_H
+#define SFN_NIR_LOWER_ALU_H
+
+#include "nir.h"
+
+bool r600_nir_lower_pack_unpack_2x16(nir_shader *shader);
+
+bool r600_nir_lower_trigen(nir_shader *shader);
+
+
+#endif // SFN_NIR_LOWER_ALU_H



More information about the mesa-commit mailing list