[v2,1/2] nir: Add is_divergent_vector search helper

Submitted by Alyssa Rosenzweig on May 10, 2019, 4:08 p.m.

Details

Message ID 20190510160803.14929-1-alyssa@rosenzweig.io
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in Mesa

Not browsing as part of any series.

Commit Message

Alyssa Rosenzweig May 10, 2019, 4:08 p.m.
This allows algebraic optimizations to check if the argument accesses
multiple distinct components of a vector. So a swizzle like "xyz" will
return true, but "yyy" will return false, as will a scalar. This can be
useful for optimizations on vector processors, where a convergent
swizzle can be done in one clock (replicating as if a scalar) but a
divergent one must be scalarized. In these cases, it is useful to
optimize differently based on whether the swizzle diverges. (Use case is
the "csel" condition on Midgard).

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Cc: Jason Ekstrand <jason@jlekstrand.net>
Cc: Ilia Mirkin <imirkin@alum.mit.edu>
---
 src/compiler/nir/nir_search_helpers.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

Patch hide | download patch | download mbox

diff --git a/src/compiler/nir/nir_search_helpers.h b/src/compiler/nir/nir_search_helpers.h
index 1624508993d..8e26739a3ce 100644
--- a/src/compiler/nir/nir_search_helpers.h
+++ b/src/compiler/nir/nir_search_helpers.h
@@ -143,6 +143,23 @@  is_not_const(nir_alu_instr *instr, unsigned src, UNUSED unsigned num_components,
    return !nir_src_is_const(instr->src[src].src);
 }
 
+/* I.e. the vector's swizzle actually accesses multiple channels. True for
+ * xyzw, false for wwww, false for w */
+
+static inline bool
+is_non_scalar_swizzle(nir_alu_instr *instr, UNUSED unsigned src, 
+             unsigned num_components, const uint8_t *swizzle)
+{
+   unsigned first_component = swizzle[0];
+
+   for (unsigned i = 1; i < num_components; ++i) {
+      if (swizzle[i] != first_component)
+         return true;
+   }
+
+   return false;
+}
+
 static inline bool
 is_used_more_than_once(nir_alu_instr *instr)
 {

Comments

Perhaps change the subject?

On Fri, May 10, 2019 at 12:08 PM Alyssa Rosenzweig <alyssa@rosenzweig.io> wrote:
>
> This allows algebraic optimizations to check if the argument accesses
> multiple distinct components of a vector. So a swizzle like "xyz" will
> return true, but "yyy" will return false, as will a scalar. This can be
> useful for optimizations on vector processors, where a convergent
> swizzle can be done in one clock (replicating as if a scalar) but a
> divergent one must be scalarized. In these cases, it is useful to
> optimize differently based on whether the swizzle diverges. (Use case is
> the "csel" condition on Midgard).

I might rephrase it as

"""
csel on Midgard requires a scalar condition, so add a function to
enable lowering csel for cases where the condition is non-scalar.
"""

Your call. (But either way, get rid of the divergent/convergent references.)




>
> Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
> Cc: Jason Ekstrand <jason@jlekstrand.net>
> Cc: Ilia Mirkin <imirkin@alum.mit.edu>
> ---
>  src/compiler/nir/nir_search_helpers.h | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
>
> diff --git a/src/compiler/nir/nir_search_helpers.h b/src/compiler/nir/nir_search_helpers.h
> index 1624508993d..8e26739a3ce 100644
> --- a/src/compiler/nir/nir_search_helpers.h
> +++ b/src/compiler/nir/nir_search_helpers.h
> @@ -143,6 +143,23 @@ is_not_const(nir_alu_instr *instr, unsigned src, UNUSED unsigned num_components,
>     return !nir_src_is_const(instr->src[src].src);
>  }
>
> +/* I.e. the vector's swizzle actually accesses multiple channels. True for
> + * xyzw, false for wwww, false for w */
> +
> +static inline bool
> +is_non_scalar_swizzle(nir_alu_instr *instr, UNUSED unsigned src,
> +             unsigned num_components, const uint8_t *swizzle)
> +{
> +   unsigned first_component = swizzle[0];
> +
> +   for (unsigned i = 1; i < num_components; ++i) {
> +      if (swizzle[i] != first_component)
> +         return true;
> +   }
> +
> +   return false;
> +}
> +
>  static inline bool
>  is_used_more_than_once(nir_alu_instr *instr)
>  {
> --
> 2.20.1
>
Oh, forgot to amend the message itself, thank you :)