Mesa (main): nir: Avoid generating extra ftruncs for array handling.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Sat Apr 16 20:39:41 UTC 2022


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

Author: Emma Anholt <emma at anholt.net>
Date:   Mon Apr 11 17:27:13 2022 -0700

nir: Avoid generating extra ftruncs for array handling.

It's quite likely that the source of the f2i32 was already an integer, in
which case we can skip the ftrunc (particularly useful on the int-to-float
class of hardware that's unlikely to just have a native trunc opcode!).

Reviewed-by: Marek Olšák <marek.olsak at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15870>

---

 src/compiler/nir/nir_lower_int_to_float.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/src/compiler/nir/nir_lower_int_to_float.c b/src/compiler/nir/nir_lower_int_to_float.c
index 91199708db3..f7fa55ac3b2 100644
--- a/src/compiler/nir/nir_lower_int_to_float.c
+++ b/src/compiler/nir/nir_lower_int_to_float.c
@@ -65,7 +65,29 @@ lower_alu_instr(nir_builder *b, nir_alu_instr *alu)
    case nir_op_b2i32: alu->op = nir_op_b2f32; break;
    case nir_op_i2f32: alu->op = nir_op_mov; break;
    case nir_op_u2f32: alu->op = nir_op_mov; break;
-   case nir_op_f2i32: alu->op = nir_op_ftrunc; break;
+
+   case nir_op_f2i32: {
+      alu->op = nir_op_ftrunc;
+
+      /* If the source was already integer, then we did't need to truncate and
+       * can switch it to a mov that can be copy-propagated away.
+       */
+      nir_alu_instr *src_alu = nir_src_as_alu_instr(alu->src[0].src);
+      if (src_alu) {
+         switch (src_alu->op) {
+         case nir_op_fround_even:
+         case nir_op_fceil:
+         case nir_op_ftrunc:
+         case nir_op_ffloor:
+            alu->op = nir_op_mov;
+            break;
+         default:
+            break;
+         }
+      }
+      break;
+   }
+
    case nir_op_f2u32: alu->op = nir_op_ffloor; break;
    case nir_op_i2b1: alu->op = nir_op_f2b1; break;
 



More information about the mesa-commit mailing list