diff --git a/clang/AST/Expr.cpp b/clang/AST/Expr.cpp index 8aa5eed80b4e..becce2a6222a 100644 --- a/clang/AST/Expr.cpp +++ b/clang/AST/Expr.cpp @@ -487,7 +487,8 @@ bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { case CompoundLiteralExprClass: if (Loc) *Loc = getLocStart(); // Allow "(int []){2,4}", since the array will be converted to a pointer. - return TR->isArrayType(); + // Allow "(vector type){2,4}" since the elements are all constant. + return TR->isArrayType() || TR->isVectorType(); case UnaryOperatorClass: { const UnaryOperator *Exp = cast(this); diff --git a/clang/CodeGen/CodeGenModule.cpp b/clang/CodeGen/CodeGenModule.cpp index bb61dc7cd825..e841ee549267 100644 --- a/clang/CodeGen/CodeGenModule.cpp +++ b/clang/CodeGen/CodeGenModule.cpp @@ -300,7 +300,8 @@ static llvm::Constant *GenerateAggregateInit(const InitListExpr *ILE, return 0; } - assert((ILE->getType()->isArrayType() || ILE->getType()->isStructureType()) && + assert((ILE->getType()->isArrayType() || ILE->getType()->isStructureType() || + ILE->getType()->isVectorType()) && "Bad type for init list!"); CodeGenTypes& Types = CGM.getTypes(); @@ -342,6 +343,9 @@ static llvm::Constant *GenerateAggregateInit(const InitListExpr *ILE, if (ILE->getType()->isStructureType()) return llvm::ConstantStruct::get(cast(CType), Elts); + if (ILE->getType()->isVectorType()) + return llvm::ConstantVector::get(cast(CType), Elts); + // Make sure we have an array at this point assert(AType); @@ -417,6 +421,12 @@ static llvm::Constant *GenerateConstantExpr(const Expr *Expression, return llvm::ConstantArray::get(Str, false); } + // Generate initializer for the CompoundLiteral + case Stmt::CompoundLiteralExprClass: { + const CompoundLiteralExpr *CLE = cast(Expression); + return GenerateConstantExpr(CLE->getInitializer(), CGM); + } + // Elide parenthesis. case Stmt::ParenExprClass: return GenerateConstantExpr(cast(Expression)->getSubExpr(), CGM); diff --git a/clang/test/CodeGen/ocu-vector.c b/clang/test/CodeGen/ocu-vector.c index ee6e737a5853..cffef339ee33 100644 --- a/clang/test/CodeGen/ocu-vector.c +++ b/clang/test/CodeGen/ocu-vector.c @@ -3,6 +3,7 @@ typedef __attribute__(( ocu_vector_type(4) )) float float4; typedef __attribute__(( ocu_vector_type(2) )) float float2; +float4 foo = (float4){ 1.0, 2.0, 3.0, 4.0 }; float4 test1(float4 V) { return V.wzyx+V; diff --git a/clang/test/Sema/vector-init.c b/clang/test/Sema/vector-init.c new file mode 100644 index 000000000000..5436cbac8c6d --- /dev/null +++ b/clang/test/Sema/vector-init.c @@ -0,0 +1,5 @@ +// RUN: clang %s -verify -fsyntax-only + +typedef __attribute__(( ocu_vector_type(4) )) float float4; + +float4 foo = (float4){ 1.0, 2.0, 3.0, 4.0 };