[cfe-dev] Question about conditional operator with vector type

JinGu Kang jingu at codeplay.com
Thu Aug 29 10:13:12 PDT 2013


Hi all,

My colleague 'Alistair' tested a simple code and found a error. The 
simplified code and error message are as following:

Source code:
typedef char char16 __attribute__((ext_vector_type(16)));

int main(void) {
   char16 valA;
   char valB;
   char valC;
   char16 destVal = valC ? valA : valB;
}

Error message:
error: can't convert between vector values of different size ('char16' 
and 'int')
   char16 destVal = valC ? valA : valB;
                                         ^ ~~~~   ~~~~

This error message comes from "CheckVectorOperands()" function in 
"CheckConditionalOperands()" function because 
"UsualArithmeticConversions(LHS, RHS)" function is executed before 
"CheckVectorOperands()" function and "UsualArithmeticConversions(LHS, 
RHS)" function changes RHS as follows:
GDB output of original RHS:
DeclRefExpr 0xc4b3050 'char' lvalue Var 0xc4b2f50 'valB' 'char'

GDB output of changed RHS:
ImplicitCastExpr 0xc4b30a8 'int' <IntegralCast>
`-ImplicitCastExpr 0xc4b3098 'char' <LValueToRValue>
   `-DeclRefExpr 0xc4b3050 'char' lvalue Var 0xc4b2f50 'valB' 'char'

So, the error message is generated between 'char16' and 'int'. I am 
wondering whether this error is correct or not. I have checked other 
checking functions executes "CheckVectorOperands()" function before 
"UsualArithmeticConversions(LHS, RHS)" function such as 
"CheckSubtractionOperands()", "CheckAdditionOperands()" functions and 
etc... Could someone let me know about this error? I have attached a 
simple patch to fix it on the assumption that it is not correct.

Thanks,
JinGu Kang

-- 
JinGu Kang
Codeplay Software Ltd
45 York Place, Edinburgh, EH1 3HP
Tel: 0131 466 0503
Fax: 0131 557 6600
Website: http://www.codeplay.com
Twitter: https://twitter.com/codeplaysoft

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

-------------- next part --------------
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp	(revision 189592)
+++ lib/Sema/SemaExpr.cpp	(working copy)
@@ -5444,18 +5444,14 @@
   VK = VK_RValue;
   OK = OK_Ordinary;
 
-  Cond = UsualUnaryConversions(Cond.take());
-  if (Cond.isInvalid())
-    return QualType();
-  UsualArithmeticConversions(LHS, RHS);
-  if (LHS.isInvalid() || RHS.isInvalid())
-    return QualType();
-
   QualType CondTy = Cond.get()->getType();
   QualType LHSTy = LHS.get()->getType();
   QualType RHSTy = RHS.get()->getType();
 
-  // first, check the condition.
+  // check the condition.
+  Cond = UsualUnaryConversions(Cond.take());
+  if (Cond.isInvalid())
+    return QualType();
   if (checkCondition(*this, Cond.get()))
     return QualType();
 
@@ -5463,6 +5459,14 @@
   if (LHSTy->isVectorType() || RHSTy->isVectorType())
     return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false);
 
+  UsualArithmeticConversions(LHS, RHS);
+  if (LHS.isInvalid() || RHS.isInvalid())
+    return QualType();
+
+  CondTy = Cond.get()->getType();
+  LHSTy = LHS.get()->getType();
+  RHSTy = RHS.get()->getType();
+
   // If the condition is a vector, and both operands are scalar,
   // attempt to implicity convert them to the vector type to act like the
   // built in select. (OpenCL v1.1 s6.3.i)


More information about the cfe-dev mailing list