Re: [Mesa-dev] [PATCH] nir/builder: Emit better code for iadd/imul_imm

2019-03-07 Thread Caio Marcelo de Oliveira Filho
On Thu, Mar 07, 2019 at 11:34:50AM -0600, Jason Ekstrand wrote:
> Because we already know the immediate right-hand parameter, we can
> potentially save the optimizer a bit of work.
> ---
>  src/compiler/nir/nir_builder.h | 25 +++--
>  1 file changed, 23 insertions(+), 2 deletions(-)

Reviewed-by: Caio Marcelo de Oliveira Filho 


There's a single case in

src/compiler/spirv/vtn_variables.c:^vtn_access_link_as_ssa

that does a check for != 1 before multiply.  Maybe remove it too?




> diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h
> index cd760c8d9ef..56e76ddcb39 100644
> --- a/src/compiler/nir/nir_builder.h
> +++ b/src/compiler/nir/nir_builder.h
> @@ -25,6 +25,7 @@
>  #define NIR_BUILDER_H
>  
>  #include "nir_control_flow.h"
> +#include "util/bitscan.h"
>  #include "util/half_float.h"
>  
>  struct exec_list;
> @@ -601,13 +602,33 @@ nir_u2u(nir_builder *build, nir_ssa_def *x, unsigned 
> dest_bit_size)
>  static inline nir_ssa_def *
>  nir_iadd_imm(nir_builder *build, nir_ssa_def *x, uint64_t y)
>  {
> -   return nir_iadd(build, x, nir_imm_intN_t(build, y, x->bit_size));
> +   assert(x->bit_size <= 64);
> +   if (x->bit_size < 64)
> +  y &= (1ull << x->bit_size) - 1;
> +
> +   if (y == 0) {
> +  return x;
> +   } else {
> +  return nir_iadd(build, x, nir_imm_intN_t(build, y, x->bit_size));
> +   }
>  }
>  
>  static inline nir_ssa_def *
>  nir_imul_imm(nir_builder *build, nir_ssa_def *x, uint64_t y)
>  {
> -   return nir_imul(build, x, nir_imm_intN_t(build, y, x->bit_size));
> +   assert(x->bit_size <= 64);
> +   if (x->bit_size < 64)
> +  y &= (1ull << x->bit_size) - 1;
> +
> +   if (y == 0) {
> +  return nir_imm_intN_t(build, 0, x->bit_size);
> +   } else if (y == 1) {
> +  return x;
> +   } else if (util_is_power_of_two_or_zero64(y)) {
> +  return nir_ishl(build, x, nir_imm_int(build, ffsll(y) - 1));
> +   } else {
> +  return nir_imul(build, x, nir_imm_intN_t(build, y, x->bit_size));
> +   }
>  }
>  
>  static inline nir_ssa_def *
> -- 
> 2.20.1
> 
> ___
> mesa-dev mailing list
> mesa-dev@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Caio
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev

[Mesa-dev] [PATCH] nir/builder: Emit better code for iadd/imul_imm

2019-03-07 Thread Jason Ekstrand
Because we already know the immediate right-hand parameter, we can
potentially save the optimizer a bit of work.
---
 src/compiler/nir/nir_builder.h | 25 +++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h
index cd760c8d9ef..56e76ddcb39 100644
--- a/src/compiler/nir/nir_builder.h
+++ b/src/compiler/nir/nir_builder.h
@@ -25,6 +25,7 @@
 #define NIR_BUILDER_H
 
 #include "nir_control_flow.h"
+#include "util/bitscan.h"
 #include "util/half_float.h"
 
 struct exec_list;
@@ -601,13 +602,33 @@ nir_u2u(nir_builder *build, nir_ssa_def *x, unsigned 
dest_bit_size)
 static inline nir_ssa_def *
 nir_iadd_imm(nir_builder *build, nir_ssa_def *x, uint64_t y)
 {
-   return nir_iadd(build, x, nir_imm_intN_t(build, y, x->bit_size));
+   assert(x->bit_size <= 64);
+   if (x->bit_size < 64)
+  y &= (1ull << x->bit_size) - 1;
+
+   if (y == 0) {
+  return x;
+   } else {
+  return nir_iadd(build, x, nir_imm_intN_t(build, y, x->bit_size));
+   }
 }
 
 static inline nir_ssa_def *
 nir_imul_imm(nir_builder *build, nir_ssa_def *x, uint64_t y)
 {
-   return nir_imul(build, x, nir_imm_intN_t(build, y, x->bit_size));
+   assert(x->bit_size <= 64);
+   if (x->bit_size < 64)
+  y &= (1ull << x->bit_size) - 1;
+
+   if (y == 0) {
+  return nir_imm_intN_t(build, 0, x->bit_size);
+   } else if (y == 1) {
+  return x;
+   } else if (util_is_power_of_two_or_zero64(y)) {
+  return nir_ishl(build, x, nir_imm_int(build, ffsll(y) - 1));
+   } else {
+  return nir_imul(build, x, nir_imm_intN_t(build, y, x->bit_size));
+   }
 }
 
 static inline nir_ssa_def *
-- 
2.20.1

___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev