[cfe-dev] [PATCH] [OpenCL] Conversions for ternary operations between scalar and vector
Colin Riddell
colinriddell at codeplay.com
Fri Dec 5 09:50:31 PST 2014
> float2 = char2 ? float2 : float2
Should result in an error as far as my understanding of the spec' goes.
This is because neither explicit nor implicit conversions between vector
types are allowed in OpenCL 1.2 (page 209 - Section 6.2.2 and page 209
- Section 6.2.1 respectively). Oddly enough, though. For me I don't see
the error..
> seems Clang decided that the ternary expression has type char2. But
> from my understanding of the spec, the type should be float2.
Yes, if anything it should be float2 - irrespective of the rules against
converting vec to vec.
> It further says that the type of the entire expression is derived from
> the second and third expression, and nowhere does it mention the type
> of the first expression.
Yes, the result is derived from either the second or third expression,
however this needs to be implicitly converted to the result type. The
type of the first expression is just used to work out whether the result
should be handled as vector or scalar..
> char2 ? int : float
> char2 ? int : unsigned
> float2 ? char : unsigned
Are valid, as far as I can see, and their results would depend on the
type of result, not the types of the values on either side of the colon.
Expanding your example by providing an assignment would mean that if
assigned to some vector type, the LHS or RHS values would be expanded to
fit the size of that vector and the types would be implicitly converted
to the base type of the result vector.
For example..
float3 = char2 ? int: float would result in a float3 for LHS by
expansion of the scalar values to vec3 and implicit conversion of the
int to float. For RHS, expansion of the scalar to vec3 and no implicit
conversion needed because it's already the same type.
Do you agree with this?
From thinking about this, I now see the two lines I proposed are not
enough in order to implement these rules correctly and fix the current bug.
I see what checkConditionalConvertScalarsToVectors() is trying to do by
converting LHS and RHS to vectors, too. However, subsequent code
checks LHSTy and RHSTy, which are QualType values based on LHS and RHS
before their types were converted and someone has forgotten to update them.
I don't think it's enough to convert these values to vectors and let the
code that follows incorrectly handle the remainder of the conversion and
ternary logic assuming C99.
I think the bug may be caused by trying to convert the LHS and RHS to
CondTy, rather than ResTy.
Colin
On 05/12/2014 07:22, Sahasrabuddhe, Sameer wrote:
> On 12/4/2014 9:38 PM, Colin Riddell wrote:
>> Working with the OpenCL 1.2 CTS I discovered one of the tests fails
>> with the tip revision of clang. Firstly, I wonder if anyone else
>> working with OpenCL has came across an assertion like this:
>>
>> Type.cpp:841: clang::Type::ScalarTypeKind
>> clang::Type::getScalarTypeKind() const: Assertion `isScalarType()'
>> failed.
>>
>> As far as I can tell, non scalar values are trying to be implicitly
>> casted where vector types should be allowed to be arithmetically
>> converted to the element type used by the vector operand as well.
>
> I can see the crash too. The problem is that the call to
> checkConditionalConvertScalarsToVectors() has already implicitly
> casted LHS and RHS to the same vector type as CondTy, but the
> subsequent code still assumes scalar types. But I am confused about
> this conversion. The OpenCL 1.2 spec says on page 220 that "The second
> and third expressions can be any type, as long their types match, or
> there is a conversion ... <snip>". It further says that the type of
> the entire expression is derived from the second and third expression,
> and nowhere does it mention the type of the first expression. Then
> what is the type of the following expression?
>
> float2 = char2 ? float2 : float2
>
> I actually get an error for this: "error: initializing 'float2'
> (vector of 2 'float' values) with an expression of incompatible type
> 'char2' (vector of 2 'char' values)". It seems Clang decided that the
> ternary expression has type char2. But from my understanding of the
> spec, the type should be float2.
>
> How about the following? Are they valid, and what is their return type?
>
> char2 ? int : float
> char2 ? int : unsigned
> float2 ? char : unsigned
>
> Sameer.
>
--
Colin Riddell
Team Lead - GPGPU Software Systems
Codeplay Software Ltd
45 York Place, Edinburgh, EH1 3HP
Tel: 0131 466 0503
Fax: 0131 557 6600
Website:http://www.codeplay.com
This email and any attachments may contain confidential and /or privileged information and is for use by the addressee only. If you are not the intended recipient, please notify Codeplay Software Ltd immediately and delete the message from your computer. You may not copy or forward it,or use or disclose its contents to any other person. Any views or other information in this message which do not relate to our business are not authorized by Codeplay software Ltd, nor does this message form part of any contract unless so stated.
As internet communications are capable of data corruption Codeplay Software Ltd does not accept any responsibility for any changes made to this message after it was sent. Please note that Codeplay Software Ltd does not accept any liability or responsibility for viruses and it is your responsibility to scan any attachments.
Company registered in England and Wales, number: 04567874
Registered office: 81 Linkfield Street, Redhill RH1 6BY
More information about the cfe-dev
mailing list