[cfe-commits] r135322 - in /cfe/trunk: lib/Sema/SemaExpr.cpp lib/Sema/SemaInit.cpp test/CodeGenOpenCL/vector_literals_valid.cl test/SemaOpenCL/vector_literals_invalid.cl

Tanya Lattner tonic at nondot.org
Fri Jul 15 16:07:01 PDT 2011


Author: tbrethou
Date: Fri Jul 15 18:07:01 2011
New Revision: 135322

URL: http://llvm.org/viewvc/llvm-project?rev=135322&view=rev
Log:
This handles the missing cases of opencl vector literals.
Test cases provided by Anton Lokhmot.

Added:
    cfe/trunk/test/CodeGenOpenCL/vector_literals_valid.cl
    cfe/trunk/test/SemaOpenCL/vector_literals_invalid.cl
Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaInit.cpp

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=135322&r1=135321&r2=135322&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Jul 15 18:07:01 2011
@@ -4222,13 +4222,14 @@
   assert(Ty->isVectorType() && "Expected vector type");
 
   llvm::SmallVector<Expr *, 8> initExprs;
+  const VectorType *VTy = Ty->getAs<VectorType>();
+  unsigned numElems = Ty->getAs<VectorType>()->getNumElements();
+  
   // '(...)' form of vector initialization in AltiVec: the number of
   // initializers must be one or must match the size of the vector.
   // If a single value is specified in the initializer then it will be
   // replicated to all the components of the vector
-  if (Ty->getAs<VectorType>()->getVectorKind() ==
-      VectorType::AltiVecVector) {
-    unsigned numElems = Ty->getAs<VectorType>()->getNumElements();
+  if (VTy->getVectorKind() == VectorType::AltiVecVector) {
     // The number of initializers must be one or must match the size of the
     // vector. If a single value is specified in the initializer then it will
     // be replicated to all the components of the vector
@@ -4248,10 +4249,22 @@
       for (unsigned i = 0, e = numExprs; i != e; ++i)
         initExprs.push_back(exprs[i]);
   }
-  else
+  else {
+    // For OpenCL, when the number of initializers is a single value,
+    // it will be replicated to all components of the vector.
+    if (getLangOptions().OpenCL &&
+        VTy->getVectorKind() == VectorType::GenericVector &&
+        numExprs == 1) {
+        QualType ElemTy = Ty->getAs<VectorType>()->getElementType();
+        ExprResult Literal = Owned(exprs[0]);
+        Literal = ImpCastExprToType(Literal.take(), ElemTy,
+                                    PrepareScalarCast(*this, Literal, ElemTy));
+        return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take());
+    }
+    
     for (unsigned i = 0, e = numExprs; i != e; ++i)
       initExprs.push_back(exprs[i]);
-
+  }
   // FIXME: This means that pretty-printing the final AST will produce curly
   // braces instead of the original commas.
   InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc,

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=135322&r1=135321&r2=135322&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Fri Jul 15 18:07:01 2011
@@ -769,7 +769,8 @@
   //   subaggregate, brace elision is assumed and the initializer is
   //   considered for the initialization of the first member of
   //   the subaggregate.
-  if (ElemType->isAggregateType() || ElemType->isVectorType()) {
+  if (!SemaRef.getLangOptions().OpenCL && 
+      (ElemType->isAggregateType() || ElemType->isVectorType())) {
     CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList,
                           StructuredIndex);
     ++StructuredIndex;

Added: cfe/trunk/test/CodeGenOpenCL/vector_literals_valid.cl
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenOpenCL/vector_literals_valid.cl?rev=135322&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenOpenCL/vector_literals_valid.cl (added)
+++ cfe/trunk/test/CodeGenOpenCL/vector_literals_valid.cl Fri Jul 15 18:07:01 2011
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -emit-llvm %s -o %t
+
+typedef __attribute__(( ext_vector_type(2) ))  int int2;
+typedef __attribute__(( ext_vector_type(3) ))  int int3;
+typedef __attribute__(( ext_vector_type(4) ))  int int4;
+typedef __attribute__(( ext_vector_type(8) ))  int int8;
+typedef __attribute__(( ext_vector_type(4) ))  float float4;
+
+void vector_literals_valid() {
+  int4 a_1_1_1_1 = (int4)(1,2,3,4);
+  int4 a_2_1_1 = (int4)((int2)(1,2),3,4);
+  int4 a_1_2_1 = (int4)(1,(int2)(2,3),4);
+  int4 a_1_1_2 = (int4)(1,2,(int2)(3,4));
+  int4 a_2_2 = (int4)((int2)(1,2),(int2)(3,4));
+  int4 a_3_1 = (int4)((int3)(1,2,3),4);
+  int4 a_1_3 = (int4)(1,(int3)(2,3,4));
+  int4 a = (int4)(1);
+  int8 b = (int8)(1,2,a.xy,a);
+  float4 V2 = (float4) (1);
+}
+
+

Added: cfe/trunk/test/SemaOpenCL/vector_literals_invalid.cl
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaOpenCL/vector_literals_invalid.cl?rev=135322&view=auto
==============================================================================
--- cfe/trunk/test/SemaOpenCL/vector_literals_invalid.cl (added)
+++ cfe/trunk/test/SemaOpenCL/vector_literals_invalid.cl Fri Jul 15 18:07:01 2011
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -verify %s 
+
+typedef __attribute__(( ext_vector_type(4) ))  float float4;
+typedef __attribute__(( ext_vector_type(4) ))  int int4;
+typedef __attribute__(( ext_vector_type(8) ))  int int8;
+
+void vector_literals_invalid()
+{
+  int4 a = (int4)(1,2,3); // expected-error{{too few elements}}
+  int4 b = (int4)(1,2,3,4,5); // expected-error{{excess elements in vector}}
+  ((float4)(1.0f))++; // expected-error{{expression is not assignable}}
+  int8 d = (int8)(a,(float4)(1)); // expected-error{{initializing 'int' with an expression of incompatible type 'float4'}}
+}





More information about the cfe-commits mailing list