aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTanya Lattner <tonic@nondot.org>2011-07-15 23:07:01 +0000
committerTanya Lattner <tonic@nondot.org>2011-07-15 23:07:01 +0000
commit61b4bc80e943578ae855810918a1dc9697dbd15b (patch)
treec7dbecf09859eed29a717583c3bce74daf3f690e
parentdeefaf6eac47046f60b059d519585c42618a5291 (diff)
This handles the missing cases of opencl vector literals.
Test cases provided by Anton Lokhmot. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@135322 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Sema/SemaExpr.cpp23
-rw-r--r--lib/Sema/SemaInit.cpp3
-rw-r--r--test/CodeGenOpenCL/vector_literals_valid.cl22
-rw-r--r--test/SemaOpenCL/vector_literals_invalid.cl13
4 files changed, 55 insertions, 6 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index a99a5d2cc4..5efc36559d 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -4222,13 +4222,14 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc,
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 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc,
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,
diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp
index 835d837e4c..9fbcbab0b7 100644
--- a/lib/Sema/SemaInit.cpp
+++ b/lib/Sema/SemaInit.cpp
@@ -769,7 +769,8 @@ void InitListChecker::CheckSubElementType(const InitializedEntity &Entity,
// 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;
diff --git a/test/CodeGenOpenCL/vector_literals_valid.cl b/test/CodeGenOpenCL/vector_literals_valid.cl
new file mode 100644
index 0000000000..bba5b23e23
--- /dev/null
+++ b/test/CodeGenOpenCL/vector_literals_valid.cl
@@ -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);
+}
+
+
diff --git a/test/SemaOpenCL/vector_literals_invalid.cl b/test/SemaOpenCL/vector_literals_invalid.cl
new file mode 100644
index 0000000000..957680f44f
--- /dev/null
+++ b/test/SemaOpenCL/vector_literals_invalid.cl
@@ -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'}}
+}