[RFC] Ternary operator with vector type predicate

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

[RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev
Hi folks,

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:
```
typedef float float4 __attribute__((ext_vector_type(4)));

float4 foo(bool cond, float4 a, float4 b) {
  return cond? a : b;
}
```
However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:
```
float4 foo(float4 a, float4 b) {
  return a > b? a : b;
}
```
Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

Here are the reasons and some advantages:
1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

I’ll try to come up with a patch for preview as soon as possible.
Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

Thanks for the feedbacks in advance.
-Min



_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev

Resending to the list:

 

From: Keane, Erich
Sent: Friday, May 8, 2020 11:04 AM
To: 'Min-Yih Hsu' <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Note that this was implemented for non-ext-vector types here:

https://github.com/llvm/llvm-project/commit/349636d2bfc39a5c81a835a95d203a42d9f9301a

 

Which matches the GCC version. 

The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:

```

typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {

  return cond? a : b;

}

```

However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:

```

float4 foo(float4 a, float4 b) {

  return a > b? a : b;

}

```

Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:

1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.

Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.

-Min

 

 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev
In reply to this post by Fangrui Song via cfe-dev
Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

Thanks for the pointer

-Min

On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

Note that this was implemented for non-ext-vector types here:
 
Which matches the GCC version.  
The attribute is vector_size, not ext_vector_type however.
 
Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).
 
 
 
From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate
 
Hi folks,
 
In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:
```
typedef float float4 __attribute__((ext_vector_type(4)));
 
float4 foo(bool cond, float4 a, float4 b) {
  return cond? a : b;
}
```
However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:
```
float4 foo(float4 a, float4 b) {
  return a > b? a : b;
}
```
Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.
 
I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.
 
On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.
 
Here are the reasons and some advantages:
1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.
 
2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.
 
3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.
 
I’ll try to come up with a patch for preview as soon as possible.
Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.
 
Thanks for the feedbacks in advance.
-Min
 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev
In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

That having been said, is this related to our discussion on bool vectors:


 -Hal

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate
 
Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

Thanks for the pointer

-Min

On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

Note that this was implemented for non-ext-vector types here:
 
Which matches the GCC version.  
The attribute is vector_size, not ext_vector_type however.
 
Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).
 
 
 
From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate
 
Hi folks,
 
In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:
```
typedef float float4 __attribute__((ext_vector_type(4)));
 
float4 foo(bool cond, float4 a, float4 b) {
  return cond? a : b;
}
```
However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:
```
float4 foo(float4 a, float4 b) {
  return a > b? a : b;
}
```
Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.
 
I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.
 
On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.
 
Here are the reasons and some advantages:
1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.
 
2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.
 
3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.
 
I’ll try to come up with a patch for preview as soon as possible.
Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.
 
Thanks for the feedbacks in advance.
-Min
 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev

What is the intent with the ext-vector-types?  It seems to me that it is an incompatible extension without anyone really maintaining them, and slightly different SEMA rules (for seemingly no reason).

 

The conditional operator for the vector_size value was implemented as a GCC compatibility option, but no such motivation existed for ext-vetctor-types.

 

That said, the semantic analysis for this in ext-vector-types is sufficiently different that it wasn’t possible to combine implementations.  The rules had diverged significantly at one point, so there wasn’t really a way to do so compatibly.

 

From: Finkel, Hal J. <[hidden email]>
Sent: Tuesday, May 19, 2020 6:05 AM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

 

That having been said, is this related to our discussion on bool vectors:

 

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

 

Thanks for the pointer

 

-Min



On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

 

Note that this was implemented for non-ext-vector types here:

 

Which matches the GCC version.  

The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:

```

typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {

  return cond? a : b;

}

```

However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:

```

float4 foo(float4 a, float4 b) {

  return a > b? a : b;

}

```

Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:

1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.

Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.

-Min

 

 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev


From: Keane, Erich <[hidden email]>
Sent: Tuesday, May 19, 2020 8:10 AM
To: Finkel, Hal J. <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate
 

What is the intent with the ext-vector-types? 



That's a really good question. I've always thought of them as a slightly-more-convenient version of the vector types, although I suppose that they're really supposed to be OpenCL vectors. The Clang extension docs say, "OpenCL vector types are created using the ext_vector_type attribute. It supports the V.xyzw syntax and other tidbits as seen in OpenCL." Thus, for OpenCL, I suppose that it is supposed to do this: https://www.khronos.org/registry/OpenCL/specs/2.2/html/OpenCL_C.html#operators-ternary-selection


 -Hal







 It seems to me that it is an incompatible extension without anyone really maintaining them, and slightly different SEMA rules (for seemingly no reason).

 

The conditional operator for the vector_size value was implemented as a GCC compatibility option, but no such motivation existed for ext-vetctor-types.

 

That said, the semantic analysis for this in ext-vector-types is sufficiently different that it wasn’t possible to combine implementations.  The rules had diverged significantly at one point, so there wasn’t really a way to do so compatibly.

 

From: Finkel, Hal J. <[hidden email]>
Sent: Tuesday, May 19, 2020 6:05 AM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

 

That having been said, is this related to our discussion on bool vectors:

 

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

 

Thanks for the pointer

 

-Min



On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

 

Note that this was implemented for non-ext-vector types here:

 

Which matches the GCC version.  

The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:

```

typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {

  return cond? a : b;

}

```

However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:

```

float4 foo(float4 a, float4 b) {

  return a > b? a : b;

}

```

Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:

1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.

Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.

-Min

 

 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev
I see ext_vector_type as a Clang extension that implements OpenCL rules under OpenCL mode. I believe there are areas where it behaves slightly differently if it's used in non-OpenCL code. I am not aware of details, but I believe it is used outside of OpenCL mode too.

Anastasia

From: cfe-dev <[hidden email]> on behalf of Finkel, Hal J. via cfe-dev <[hidden email]>
Sent: 19 May 2020 14:22
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate
 


From: Keane, Erich <[hidden email]>
Sent: Tuesday, May 19, 2020 8:10 AM
To: Finkel, Hal J. <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate
 

What is the intent with the ext-vector-types? 



That's a really good question. I've always thought of them as a slightly-more-convenient version of the vector types, although I suppose that they're really supposed to be OpenCL vectors. The Clang extension docs say, "OpenCL vector types are created using the ext_vector_type attribute. It supports the V.xyzw syntax and other tidbits as seen in OpenCL." Thus, for OpenCL, I suppose that it is supposed to do this: https://www.khronos.org/registry/OpenCL/specs/2.2/html/OpenCL_C.html#operators-ternary-selection


 -Hal







 It seems to me that it is an incompatible extension without anyone really maintaining them, and slightly different SEMA rules (for seemingly no reason).

 

The conditional operator for the vector_size value was implemented as a GCC compatibility option, but no such motivation existed for ext-vetctor-types.

 

That said, the semantic analysis for this in ext-vector-types is sufficiently different that it wasn’t possible to combine implementations.  The rules had diverged significantly at one point, so there wasn’t really a way to do so compatibly.

 

From: Finkel, Hal J. <[hidden email]>
Sent: Tuesday, May 19, 2020 6:05 AM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

 

That having been said, is this related to our discussion on bool vectors:

 

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

 

Thanks for the pointer

 

-Min



On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

 

Note that this was implemented for non-ext-vector types here:

 

Which matches the GCC version.  

The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:

```

typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {

  return cond? a : b;

}

```

However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:

```

float4 foo(float4 a, float4 b) {

  return a > b? a : b;

}

```

Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:

1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.

Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.

-Min

 

 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev
Thanks, Anastasia. I have certainly used the ext_vector_type types outside of OpenCL mode in the past. Do we currently implement the OpenCL 2 ternary operator rules for these vectors?

 -Hal

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory


From: Anastasia Stulova <[hidden email]>
Sent: Tuesday, May 19, 2020 12:30 PM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; Finkel, Hal J. <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate
 
I see ext_vector_type as a Clang extension that implements OpenCL rules under OpenCL mode. I believe there are areas where it behaves slightly differently if it's used in non-OpenCL code. I am not aware of details, but I believe it is used outside of OpenCL mode too.

Anastasia

From: cfe-dev <[hidden email]> on behalf of Finkel, Hal J. via cfe-dev <[hidden email]>
Sent: 19 May 2020 14:22
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate
 


From: Keane, Erich <[hidden email]>
Sent: Tuesday, May 19, 2020 8:10 AM
To: Finkel, Hal J. <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate
 

What is the intent with the ext-vector-types? 



That's a really good question. I've always thought of them as a slightly-more-convenient version of the vector types, although I suppose that they're really supposed to be OpenCL vectors. The Clang extension docs say, "OpenCL vector types are created using the ext_vector_type attribute. It supports the V.xyzw syntax and other tidbits as seen in OpenCL." Thus, for OpenCL, I suppose that it is supposed to do this: https://www.khronos.org/registry/OpenCL/specs/2.2/html/OpenCL_C.html#operators-ternary-selection


 -Hal







 It seems to me that it is an incompatible extension without anyone really maintaining them, and slightly different SEMA rules (for seemingly no reason).

 

The conditional operator for the vector_size value was implemented as a GCC compatibility option, but no such motivation existed for ext-vetctor-types.

 

That said, the semantic analysis for this in ext-vector-types is sufficiently different that it wasn’t possible to combine implementations.  The rules had diverged significantly at one point, so there wasn’t really a way to do so compatibly.

 

From: Finkel, Hal J. <[hidden email]>
Sent: Tuesday, May 19, 2020 6:05 AM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

 

That having been said, is this related to our discussion on bool vectors:

 

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

 

Thanks for the pointer

 

-Min



On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

 

Note that this was implemented for non-ext-vector types here:

 

Which matches the GCC version.  

The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:

```

typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {

  return cond? a : b;

}

```

However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:

```

float4 foo(float4 a, float4 b) {

  return a > b? a : b;

}

```

Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:

1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.

Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.

-Min

 

 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev

Yes, we do implement these rules for OpenCL and they are applied to all OpenCL versions.

https://godbolt.org/z/YH6ExF

 

 

From: cfe-dev <[hidden email]> On Behalf Of Finkel, Hal J. via cfe-dev
Sent: Tuesday, May 19, 2020 8:36 PM
To: Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Thanks, Anastasia. I have certainly used the ext_vector_type types outside of OpenCL mode in the past. Do we currently implement the OpenCL 2 ternary operator rules for these vectors?

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: Anastasia Stulova <[hidden email]>
Sent: Tuesday, May 19, 2020 12:30 PM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; Finkel, Hal J. <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

I see ext_vector_type as a Clang extension that implements OpenCL rules under OpenCL mode. I believe there are areas where it behaves slightly differently if it's used in non-OpenCL code. I am not aware of details, but I believe it is used outside of OpenCL mode too.

 

Anastasia


From: cfe-dev <[hidden email]> on behalf of Finkel, Hal J. via cfe-dev <[hidden email]>
Sent: 19 May 2020 14:22
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

 


From: Keane, Erich <[hidden email]>
Sent: Tuesday, May 19, 2020 8:10 AM
To: Finkel, Hal J. <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

What is the intent with the ext-vector-types? 

 

 

That's a really good question. I've always thought of them as a slightly-more-convenient version of the vector types, although I suppose that they're really supposed to be OpenCL vectors. The Clang extension docs say, "OpenCL vector types are created using the ext_vector_type attribute. It supports the V.xyzw syntax and other tidbits as seen in OpenCL." Thus, for OpenCL, I suppose that it is supposed to do this: https://www.khronos.org/registry/OpenCL/specs/2.2/html/OpenCL_C.html#operators-ternary-selection

 

 -Hal

 

 

 

 

 

 

 It seems to me that it is an incompatible extension without anyone really maintaining them, and slightly different SEMA rules (for seemingly no reason).

 

The conditional operator for the vector_size value was implemented as a GCC compatibility option, but no such motivation existed for ext-vetctor-types.

 

That said, the semantic analysis for this in ext-vector-types is sufficiently different that it wasn’t possible to combine implementations.  The rules had diverged significantly at one point, so there wasn’t really a way to do so compatibly.

 

From: Finkel, Hal J. <[hidden email]>
Sent: Tuesday, May 19, 2020 6:05 AM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

 

That having been said, is this related to our discussion on bool vectors:

 

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

 

Thanks for the pointer

 

-Min

 

On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

 

Note that this was implemented for non-ext-vector types here:

 

Which matches the GCC version.  

The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:

```

typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {

  return cond? a : b;

}

```

However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:

```

float4 foo(float4 a, float4 b) {

  return a > b? a : b;

}

```

Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:

1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.

Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.

-Min

 

 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev
Thanks, Alexey. In that case, do you have any thoughts on the original email in this thread?

 -Hal

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory


From: Bader, Alexey <[hidden email]>
Sent: Tuesday, May 19, 2020 1:48 PM
To: Finkel, Hal J. <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate
 

Yes, we do implement these rules for OpenCL and they are applied to all OpenCL versions.

https://godbolt.org/z/YH6ExF

 

 

From: cfe-dev <[hidden email]> On Behalf Of Finkel, Hal J. via cfe-dev
Sent: Tuesday, May 19, 2020 8:36 PM
To: Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Thanks, Anastasia. I have certainly used the ext_vector_type types outside of OpenCL mode in the past. Do we currently implement the OpenCL 2 ternary operator rules for these vectors?

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: Anastasia Stulova <[hidden email]>
Sent: Tuesday, May 19, 2020 12:30 PM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; Finkel, Hal J. <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

I see ext_vector_type as a Clang extension that implements OpenCL rules under OpenCL mode. I believe there are areas where it behaves slightly differently if it's used in non-OpenCL code. I am not aware of details, but I believe it is used outside of OpenCL mode too.

 

Anastasia


From: cfe-dev <[hidden email]> on behalf of Finkel, Hal J. via cfe-dev <[hidden email]>
Sent: 19 May 2020 14:22
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

 


From: Keane, Erich <[hidden email]>
Sent: Tuesday, May 19, 2020 8:10 AM
To: Finkel, Hal J. <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

What is the intent with the ext-vector-types? 

 

 

That's a really good question. I've always thought of them as a slightly-more-convenient version of the vector types, although I suppose that they're really supposed to be OpenCL vectors. The Clang extension docs say, "OpenCL vector types are created using the ext_vector_type attribute. It supports the V.xyzw syntax and other tidbits as seen in OpenCL." Thus, for OpenCL, I suppose that it is supposed to do this: https://www.khronos.org/registry/OpenCL/specs/2.2/html/OpenCL_C.html#operators-ternary-selection

 

 -Hal

 

 

 

 

 

 

 It seems to me that it is an incompatible extension without anyone really maintaining them, and slightly different SEMA rules (for seemingly no reason).

 

The conditional operator for the vector_size value was implemented as a GCC compatibility option, but no such motivation existed for ext-vetctor-types.

 

That said, the semantic analysis for this in ext-vector-types is sufficiently different that it wasn’t possible to combine implementations.  The rules had diverged significantly at one point, so there wasn’t really a way to do so compatibly.

 

From: Finkel, Hal J. <[hidden email]>
Sent: Tuesday, May 19, 2020 6:05 AM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

 

That having been said, is this related to our discussion on bool vectors:

 

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

 

Thanks for the pointer

 

-Min

 

On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

 

Note that this was implemented for non-ext-vector types here:

 

Which matches the GCC version.  

The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:

```

typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {

  return cond? a : b;

}

```

However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:

```

float4 foo(float4 a, float4 b) {

  return a > b? a : b;

}

```

Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:

1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.

Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.

-Min

 

 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev

I never used this extensions in non-OpenCL mode, so I can’t predict what is the impact of the proposed changes.

 

I just note that code snippet from the original email works in OpenCL mode as described in RFC with one very minor note:

 

  • I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

In OpenCL mode the comparison operator returns bitmask to enable bitwise operations (https://godbolt.org/z/F6ynx6):

 

float4 foo(float4 a, float4 b, int4 *c) {

  int4 x = {1234};

  *c = (a < b) & x;

  return a > b? a : b;

}

 

and select is defined as

 

gentype select(gentype a, gentype b, igentype c) gentype select(gentype a, gentype b, ugentype c)

For each component of a vector type,

result[i] = if MSB of c[i] is set ? b[i] : a[i].

For a scalar type, result = c ? b : a.

igentype and ugentype must have the same number of elements and bits as gentype36.

 

I guess, if we convert predicate elements to bool, we should get the same behavior.

 

From: Finkel, Hal J. <[hidden email]>
Sent: Tuesday, May 19, 2020 9:50 PM
To: Bader, Alexey <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Thanks, Alexey. In that case, do you have any thoughts on the original email in this thread?

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: Bader, Alexey <[hidden email]>
Sent: Tuesday, May 19, 2020 1:48 PM
To: Finkel, Hal J. <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Yes, we do implement these rules for OpenCL and they are applied to all OpenCL versions.

https://godbolt.org/z/YH6ExF

 

 

From: cfe-dev <[hidden email]> On Behalf Of Finkel, Hal J. via cfe-dev
Sent: Tuesday, May 19, 2020 8:36 PM
To: Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Thanks, Anastasia. I have certainly used the ext_vector_type types outside of OpenCL mode in the past. Do we currently implement the OpenCL 2 ternary operator rules for these vectors?

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: Anastasia Stulova <[hidden email]>
Sent: Tuesday, May 19, 2020 12:30 PM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; Finkel, Hal J. <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

I see ext_vector_type as a Clang extension that implements OpenCL rules under OpenCL mode. I believe there are areas where it behaves slightly differently if it's used in non-OpenCL code. I am not aware of details, but I believe it is used outside of OpenCL mode too.

 

Anastasia


From: cfe-dev <[hidden email]> on behalf of Finkel, Hal J. via cfe-dev <[hidden email]>
Sent: 19 May 2020 14:22
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

 


From: Keane, Erich <[hidden email]>
Sent: Tuesday, May 19, 2020 8:10 AM
To: Finkel, Hal J. <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

What is the intent with the ext-vector-types? 

 

 

That's a really good question. I've always thought of them as a slightly-more-convenient version of the vector types, although I suppose that they're really supposed to be OpenCL vectors. The Clang extension docs say, "OpenCL vector types are created using the ext_vector_type attribute. It supports the V.xyzw syntax and other tidbits as seen in OpenCL." Thus, for OpenCL, I suppose that it is supposed to do this: https://www.khronos.org/registry/OpenCL/specs/2.2/html/OpenCL_C.html#operators-ternary-selection

 

 -Hal

 

 

 

 

 

 

 It seems to me that it is an incompatible extension without anyone really maintaining them, and slightly different SEMA rules (for seemingly no reason).

 

The conditional operator for the vector_size value was implemented as a GCC compatibility option, but no such motivation existed for ext-vetctor-types.

 

That said, the semantic analysis for this in ext-vector-types is sufficiently different that it wasn’t possible to combine implementations.  The rules had diverged significantly at one point, so there wasn’t really a way to do so compatibly.

 

From: Finkel, Hal J. <[hidden email]>
Sent: Tuesday, May 19, 2020 6:05 AM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

 

That having been said, is this related to our discussion on bool vectors:

 

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

 

Thanks for the pointer

 

-Min

 

On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

 

Note that this was implemented for non-ext-vector types here:

 

Which matches the GCC version.  

The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:

```

typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {

  return cond? a : b;

}

```

However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:

```

float4 foo(float4 a, float4 b) {

  return a > b? a : b;

}

```

Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:

1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.

Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.

-Min

 

 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev
Right, If OpenCL mode compilation flag is removed from the example that Alexey has given, the compilation fails!

FYI, I just came across this old bug discussing similar issue:


From: Bader, Alexey <[hidden email]>
Sent: 19 May 2020 20:31
To: Finkel, Hal J. <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate
 

I never used this extensions in non-OpenCL mode, so I can’t predict what is the impact of the proposed changes.

 

I just note that code snippet from the original email works in OpenCL mode as described in RFC with one very minor note:

 

  • I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

In OpenCL mode the comparison operator returns bitmask to enable bitwise operations (https://godbolt.org/z/F6ynx6):

 

float4 foo(float4 a, float4 b, int4 *c) {

  int4 x = {1234};

  *c = (a < b) & x;

  return a > b? a : b;

}

 

and select is defined as

 

gentype select(gentype a, gentype b, igentype c) gentype select(gentype a, gentype b, ugentype c)

For each component of a vector type,

result[i] = if MSB of c[i] is set ? b[i] : a[i].

For a scalar type, result = c ? b : a.

igentype and ugentype must have the same number of elements and bits as gentype36.

 

I guess, if we convert predicate elements to bool, we should get the same behavior.

 

From: Finkel, Hal J. <[hidden email]>
Sent: Tuesday, May 19, 2020 9:50 PM
To: Bader, Alexey <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Thanks, Alexey. In that case, do you have any thoughts on the original email in this thread?

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: Bader, Alexey <[hidden email]>
Sent: Tuesday, May 19, 2020 1:48 PM
To: Finkel, Hal J. <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Yes, we do implement these rules for OpenCL and they are applied to all OpenCL versions.

https://godbolt.org/z/YH6ExF

 

 

From: cfe-dev <[hidden email]> On Behalf Of Finkel, Hal J. via cfe-dev
Sent: Tuesday, May 19, 2020 8:36 PM
To: Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Thanks, Anastasia. I have certainly used the ext_vector_type types outside of OpenCL mode in the past. Do we currently implement the OpenCL 2 ternary operator rules for these vectors?

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: Anastasia Stulova <[hidden email]>
Sent: Tuesday, May 19, 2020 12:30 PM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; Finkel, Hal J. <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

I see ext_vector_type as a Clang extension that implements OpenCL rules under OpenCL mode. I believe there are areas where it behaves slightly differently if it's used in non-OpenCL code. I am not aware of details, but I believe it is used outside of OpenCL mode too.

 

Anastasia


From: cfe-dev <[hidden email]> on behalf of Finkel, Hal J. via cfe-dev <[hidden email]>
Sent: 19 May 2020 14:22
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

 


From: Keane, Erich <[hidden email]>
Sent: Tuesday, May 19, 2020 8:10 AM
To: Finkel, Hal J. <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

What is the intent with the ext-vector-types? 

 

 

That's a really good question. I've always thought of them as a slightly-more-convenient version of the vector types, although I suppose that they're really supposed to be OpenCL vectors. The Clang extension docs say, "OpenCL vector types are created using the ext_vector_type attribute. It supports the V.xyzw syntax and other tidbits as seen in OpenCL." Thus, for OpenCL, I suppose that it is supposed to do this: https://www.khronos.org/registry/OpenCL/specs/2.2/html/OpenCL_C.html#operators-ternary-selection

 

 -Hal

 

 

 

 

 

 

 It seems to me that it is an incompatible extension without anyone really maintaining them, and slightly different SEMA rules (for seemingly no reason).

 

The conditional operator for the vector_size value was implemented as a GCC compatibility option, but no such motivation existed for ext-vetctor-types.

 

That said, the semantic analysis for this in ext-vector-types is sufficiently different that it wasn’t possible to combine implementations.  The rules had diverged significantly at one point, so there wasn’t really a way to do so compatibly.

 

From: Finkel, Hal J. <[hidden email]>
Sent: Tuesday, May 19, 2020 6:05 AM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

 

That having been said, is this related to our discussion on bool vectors:

 

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

 

Thanks for the pointer

 

-Min

 

On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

 

Note that this was implemented for non-ext-vector types here:

 

Which matches the GCC version.  

The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:

```

typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {

  return cond? a : b;

}

```

However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:

```

float4 foo(float4 a, float4 b) {

  return a > b? a : b;

}

```

Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:

1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.

Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.

-Min

 

 


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev
Thanks for everyone’s feedback.

I think the bottom line is that the originally proposal works under OpenCL and gcc-style vector extension. Should we support it for ext_vector_type under non-OpenCL mode?

Surprisingly the bug Anastasia pointed to was also asking the same question. Most of the comments there agreed with the approach and I didn’t see any comments against it in this thread. So I’ll cook a simple patch to turn on this switch for non-OpenCL mode for everyone’s review.

On May 20, 2020, at 2:49 PM, Anastasia Stulova <[hidden email]> wrote:

Right, If OpenCL mode compilation flag is removed from the example that Alexey has given, the compilation fails!

FYI, I just came across this old bug discussing similar issue:


From: Bader, Alexey <[hidden email]>
Sent: 19 May 2020 20:31
To: Finkel, Hal J. <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate
 
I never used this extensions in non-OpenCL mode, so I can’t predict what is the impact of the proposed changes.

 

I just note that code snippet from the original email works in OpenCL mode as described in RFC with one very minor note:

 

  • I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

In OpenCL mode the comparison operator returns bitmask to enable bitwise operations (https://godbolt.org/z/F6ynx6):

 

float4 foo(float4 a, float4 b, int4 *c) {
  int4 x = {1234};
  *c = (a < b) & x;
  return a > b? a : b;
}

 

and select is defined as 

 

gentype select(gentype a, gentype b, igentype c) gentype select(gentype a, gentype b, ugentype c)

For each component of a vector type,

result[i] = if MSB of c[i] is set ? b[i] : a[i].

For a scalar type, result = c ? b : a.

igentype and ugentype must have the same number of elements and bits as gentype36.

 

I guess, if we convert predicate elements to bool, we should get the same behavior.

 

From: Finkel, Hal J. <[hidden email]> 
Sent: Tuesday, May 19, 2020 9:50 PM
To: Bader, Alexey <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Thanks, Alexey. In that case, do you have any thoughts on the original email in this thread?

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: Bader, Alexey <[hidden email]>
Sent: Tuesday, May 19, 2020 1:48 PM
To: Finkel, Hal J. <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Yes, we do implement these rules for OpenCL and they are applied to all OpenCL versions.

 

 

From: cfe-dev <[hidden email]> On Behalf Of Finkel, Hal J. via cfe-dev
Sent: Tuesday, May 19, 2020 8:36 PM
To: Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Thanks, Anastasia. I have certainly used the ext_vector_type types outside of OpenCL mode in the past. Do we currently implement the OpenCL 2 ternary operator rules for these vectors?

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: Anastasia Stulova <[hidden email]>
Sent: Tuesday, May 19, 2020 12:30 PM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; Finkel, Hal J. <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

I see ext_vector_type as a Clang extension that implements OpenCL rules under OpenCL mode. I believe there are areas where it behaves slightly differently if it's used in non-OpenCL code. I am not aware of details, but I believe it is used outside of OpenCL mode too.

 

Anastasia

From: cfe-dev <[hidden email]> on behalf of Finkel, Hal J. via cfe-dev <[hidden email]>
Sent: 19 May 2020 14:22
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

 


From: Keane, Erich <[hidden email]>
Sent: Tuesday, May 19, 2020 8:10 AM
To: Finkel, Hal J. <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

What is the intent with the ext-vector-types? 

 

 

That's a really good question. I've always thought of them as a slightly-more-convenient version of the vector types, although I suppose that they're really supposed to be OpenCL vectors. The Clang extension docs say, "OpenCL vector types are created using the ext_vector_type attribute. It supports the V.xyzw syntax and other tidbits as seen in OpenCL." Thus, for OpenCL, I suppose that it is supposed to do this: https://www.khronos.org/registry/OpenCL/specs/2.2/html/OpenCL_C.html#operators-ternary-selection

 

 -Hal

 

 

 

 

 

 

 It seems to me that it is an incompatible extension without anyone really maintaining them, and slightly different SEMA rules (for seemingly no reason).

 

The conditional operator for the vector_size value was implemented as a GCC compatibility option, but no such motivation existed for ext-vetctor-types.

 

That said, the semantic analysis for this in ext-vector-types is sufficiently different that it wasn’t possible to combine implementations.  The rules had diverged significantly at one point, so there wasn’t really a way to do so compatibly.

 

From: Finkel, Hal J. <[hidden email]> 
Sent: Tuesday, May 19, 2020 6:05 AM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

 

That having been said, is this related to our discussion on bool vectors:

 

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

 

Thanks for the pointer

 

-Min
 
On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

 

Note that this was implemented for non-ext-vector types here:

 

Which matches the GCC version.  
The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:
```
typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {
  return cond? a : b;
}
```
However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:
```
float4 foo(float4 a, float4 b) {
  return a > b? a : b;
}
```
Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:
1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.
Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.
-Min

 



_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Ternary operator with vector type predicate

Fangrui Song via cfe-dev
Makes sense! The change seems to be straight forward and it brings useful functionality.

Cheers,
Anastasia

From: Min-Yih Hsu <[hidden email]>
Sent: 21 May 2020 17:04
To: Anastasia Stulova <[hidden email]>
Cc: Bader, Alexey <[hidden email]>; Finkel, Hal J. <[hidden email]>; Keane, Erich <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>; nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate
 
Thanks for everyone’s feedback.

I think the bottom line is that the originally proposal works under OpenCL and gcc-style vector extension. Should we support it for ext_vector_type under non-OpenCL mode?

Surprisingly the bug Anastasia pointed to was also asking the same question. Most of the comments there agreed with the approach and I didn’t see any comments against it in this thread. So I’ll cook a simple patch to turn on this switch for non-OpenCL mode for everyone’s review.

On May 20, 2020, at 2:49 PM, Anastasia Stulova <[hidden email]> wrote:

Right, If OpenCL mode compilation flag is removed from the example that Alexey has given, the compilation fails!

FYI, I just came across this old bug discussing similar issue:


From: Bader, Alexey <[hidden email]>
Sent: 19 May 2020 20:31
To: Finkel, Hal J. <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate
 
I never used this extensions in non-OpenCL mode, so I can’t predict what is the impact of the proposed changes.

 

I just note that code snippet from the original email works in OpenCL mode as described in RFC with one very minor note:

 

  • I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

In OpenCL mode the comparison operator returns bitmask to enable bitwise operations (https://godbolt.org/z/F6ynx6):

 

float4 foo(float4 a, float4 b, int4 *c) {
  int4 x = {1234};
  *c = (a < b) & x;
  return a > b? a : b;
}

 

and select is defined as 

 

gentype select(gentype a, gentype b, igentype c) gentype select(gentype a, gentype b, ugentype c)

For each component of a vector type,

result[i] = if MSB of c[i] is set ? b[i] : a[i].

For a scalar type, result = c ? b : a.

igentype and ugentype must have the same number of elements and bits as gentype36.

 

I guess, if we convert predicate elements to bool, we should get the same behavior.

 

From: Finkel, Hal J. <[hidden email]> 
Sent: Tuesday, May 19, 2020 9:50 PM
To: Bader, Alexey <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Thanks, Alexey. In that case, do you have any thoughts on the original email in this thread?

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: Bader, Alexey <[hidden email]>
Sent: Tuesday, May 19, 2020 1:48 PM
To: Finkel, Hal J. <[hidden email]>; Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; cfe-dev ([hidden email]) <[hidden email]>
Cc: nd <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Yes, we do implement these rules for OpenCL and they are applied to all OpenCL versions.

 

 

From: cfe-dev <[hidden email]> On Behalf Of Finkel, Hal J. via cfe-dev
Sent: Tuesday, May 19, 2020 8:36 PM
To: Anastasia Stulova <[hidden email]>; Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Thanks, Anastasia. I have certainly used the ext_vector_type types outside of OpenCL mode in the past. Do we currently implement the OpenCL 2 ternary operator rules for these vectors?

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: Anastasia Stulova <[hidden email]>
Sent: Tuesday, May 19, 2020 12:30 PM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>; Finkel, Hal J. <[hidden email]>; clang developer list <[hidden email]>
Cc: nd <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

I see ext_vector_type as a Clang extension that implements OpenCL rules under OpenCL mode. I believe there are areas where it behaves slightly differently if it's used in non-OpenCL code. I am not aware of details, but I believe it is used outside of OpenCL mode too.

 

Anastasia

From: cfe-dev <[hidden email]> on behalf of Finkel, Hal J. via cfe-dev <[hidden email]>
Sent: 19 May 2020 14:22
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

 


From: Keane, Erich <[hidden email]>
Sent: Tuesday, May 19, 2020 8:10 AM
To: Finkel, Hal J. <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: RE: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

What is the intent with the ext-vector-types? 

 

 

That's a really good question. I've always thought of them as a slightly-more-convenient version of the vector types, although I suppose that they're really supposed to be OpenCL vectors. The Clang extension docs say, "OpenCL vector types are created using the ext_vector_type attribute. It supports the V.xyzw syntax and other tidbits as seen in OpenCL." Thus, for OpenCL, I suppose that it is supposed to do this: https://www.khronos.org/registry/OpenCL/specs/2.2/html/OpenCL_C.html#operators-ternary-selection

 

 -Hal

 

 

 

 

 

 

 It seems to me that it is an incompatible extension without anyone really maintaining them, and slightly different SEMA rules (for seemingly no reason).

 

The conditional operator for the vector_size value was implemented as a GCC compatibility option, but no such motivation existed for ext-vetctor-types.

 

That said, the semantic analysis for this in ext-vector-types is sufficiently different that it wasn’t possible to combine implementations.  The rules had diverged significantly at one point, so there wasn’t really a way to do so compatibly.

 

From: Finkel, Hal J. <[hidden email]> 
Sent: Tuesday, May 19, 2020 6:05 AM
To: Keane, Erich <[hidden email]>; Min-Yih Hsu <[hidden email]>
Cc: [hidden email]
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

In general, I don't like to see features supported by not-ext-vector types that are not supported by ext-vector types.

 

That having been said, is this related to our discussion on bool vectors:

 

 

 -Hal

 

Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

 


From: cfe-dev <[hidden email]> on behalf of Min-Yih Hsu via cfe-dev <[hidden email]>
Sent: Friday, May 8, 2020 1:17 PM
To: Keane, Erich <[hidden email]>
Cc: [hidden email] <[hidden email]>
Subject: Re: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Interesting….then I think the question simply becomes whether we should support it in ext_vector_type as well.

 

Thanks for the pointer

 

-Min
 
On May 8, 2020, at 11:04 AM, Keane, Erich <[hidden email]> wrote:

 

Note that this was implemented for non-ext-vector types here:

 

Which matches the GCC version.  
The attribute is vector_size, not ext_vector_type however.

 

Otherwise, it does everything you’re proposing (like I said, just with a more portable vector-type definition).

 

 

 

From: cfe-dev <[hidden email]> On Behalf Of Min-Yih Hsu via cfe-dev
Sent: Friday, May 8, 2020 10:58 AM
To: [hidden email]
Subject: [cfe-dev] [RFC] Ternary operator with vector type predicate

 

Hi folks,

 

In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:
```
typedef float float4 __attribute__((ext_vector_type(4)));

 

float4 foo(bool cond, float4 a, float4 b) {
  return cond? a : b;
}
```
However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:
```
float4 foo(float4 a, float4 b) {
  return a > b? a : b;
}
```
Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.

 

I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.

 

On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.

 

Here are the reasons and some advantages:
1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.

 

2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.

 

3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.

 

I’ll try to come up with a patch for preview as soon as possible.
Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.

 

Thanks for the feedbacks in advance.
-Min

 



_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev