[cfe-commits] r90600 - in /cfe/trunk: lib/AST/TypePrinter.cpp lib/Sema/SemaDeclAttr.cpp lib/Sema/SemaType.cpp test/Sema/vector-init.c

John Thompson John.Thompson.JTSoftware at gmail.com
Fri Dec 4 13:51:28 PST 2009


Author: jtsoftware
Date: Fri Dec  4 15:51:28 2009
New Revision: 90600

URL: http://llvm.org/viewvc/llvm-project?rev=90600&view=rev
Log:
Fix for PR5650 - Revised vector_size attribute handling to be done earlier before declaration is finalized.

Modified:
    cfe/trunk/lib/AST/TypePrinter.cpp
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/Sema/vector-init.c

Modified: cfe/trunk/lib/AST/TypePrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/TypePrinter.cpp?rev=90600&r1=90599&r2=90600&view=diff

==============================================================================
--- cfe/trunk/lib/AST/TypePrinter.cpp (original)
+++ cfe/trunk/lib/AST/TypePrinter.cpp Fri Dec  4 15:51:28 2009
@@ -242,12 +242,13 @@
 void TypePrinter::PrintVector(const VectorType *T, std::string &S) { 
   // FIXME: We prefer to print the size directly here, but have no way
   // to get the size of the type.
-  S += " __attribute__((__vector_size__(";
-  S += llvm::utostr_32(T->getNumElements()); // convert back to bytes.
+  Print(T->getElementType(), S);
+  std::string V = "__attribute__((__vector_size__(";
+  V += llvm::utostr_32(T->getNumElements()); // convert back to bytes.
   std::string ET;
   Print(T->getElementType(), ET);
-  S += " * sizeof(" + ET + "))))";
-  Print(T->getElementType(), S);
+  V += " * sizeof(" + ET + ")))) ";
+  S = V + S;
 }
 
 void TypePrinter::PrintExtVector(const ExtVectorType *T, std::string &S) { 

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=90600&r1=90599&r2=90600&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Fri Dec  4 15:51:28 2009
@@ -202,91 +202,6 @@
   }
 }
 
-
-/// HandleVectorSizeAttribute - this attribute is only applicable to integral
-/// and float scalars, although arrays, pointers, and function return values are
-/// allowed in conjunction with this construct. Aggregates with this attribute
-/// are invalid, even if they are of the same size as a corresponding scalar.
-/// The raw attribute should contain precisely 1 argument, the vector size for
-/// the variable, measured in bytes. If curType and rawAttr are well formed,
-/// this routine will return a new vector type.
-static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
-  QualType CurType;
-  if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
-    CurType = VD->getType();
-  else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
-    CurType = TD->getUnderlyingType();
-  else {
-    S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
-      << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
-    return;
-  }
-
-  // Check the attribute arugments.
-  if (Attr.getNumArgs() != 1) {
-    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
-    return;
-  }
-  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
-  llvm::APSInt vecSize(32);
-  if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
-    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
-      << "vector_size" << sizeExpr->getSourceRange();
-    return;
-  }
-  // navigate to the base type - we need to provide for vector pointers, vector
-  // arrays, and functions returning vectors.
-  if (CurType->isPointerType() || CurType->isArrayType() ||
-      CurType->isFunctionType()) {
-    S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
-    return;
-    /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
-     do {
-     if (PointerType *PT = dyn_cast<PointerType>(canonType))
-     canonType = PT->getPointeeType().getTypePtr();
-     else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
-     canonType = AT->getElementType().getTypePtr();
-     else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
-     canonType = FT->getResultType().getTypePtr();
-     } while (canonType->isPointerType() || canonType->isArrayType() ||
-     canonType->isFunctionType());
-     */
-  }
-  // the base type must be integer or float, and can't already be a vector.
-  if (CurType->isVectorType() ||
-      (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
-    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
-    return;
-  }
-  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
-  // vecSize is specified in bytes - convert to bits.
-  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
-
-  // the vector size needs to be an integral multiple of the type size.
-  if (vectorSize % typeSize) {
-    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
-      << sizeExpr->getSourceRange();
-    return;
-  }
-  if (vectorSize == 0) {
-    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
-      << sizeExpr->getSourceRange();
-    return;
-  }
-
-  // Success! Instantiate the vector type, the number of elements is > 0, and
-  // not required to be a power of 2, unlike GCC.
-  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
-
-  if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
-    VD->setType(CurType);
-  else {
-    // FIXME: preserve existing source info.
-    DeclaratorInfo *DInfo = S.Context.getTrivialDeclaratorInfo(CurType);
-    cast<TypedefDecl>(D)->setTypeDeclaratorInfo(DInfo);
-  }
-}
-
 static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
   // check the attribute arguments.
   if (Attr.getNumArgs() > 0) {
@@ -1964,6 +1879,7 @@
   case AttributeList::AT_IBOutlet:    HandleIBOutletAttr  (D, Attr, S); break;
   case AttributeList::AT_address_space:
   case AttributeList::AT_objc_gc:
+  case AttributeList::AT_vector_size:
     // Ignore these, these are type attributes, handled by
     // ProcessTypeAttributes.
     break;
@@ -2013,7 +1929,6 @@
   case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
   case AttributeList::AT_unused:      HandleUnusedAttr      (D, Attr, S); break;
   case AttributeList::AT_used:        HandleUsedAttr        (D, Attr, S); break;
-  case AttributeList::AT_vector_size: HandleVectorSizeAttr  (D, Attr, S); break;
   case AttributeList::AT_visibility:  HandleVisibilityAttr  (D, Attr, S); break;
   case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
     break;

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=90600&r1=90599&r2=90600&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Fri Dec  4 15:51:28 2009
@@ -1643,6 +1643,53 @@
   Type = S.Context.getNoReturnType(Type);
 }
 
+/// HandleVectorSizeAttribute - this attribute is only applicable to integral
+/// and float scalars, although arrays, pointers, and function return values are
+/// allowed in conjunction with this construct. Aggregates with this attribute
+/// are invalid, even if they are of the same size as a corresponding scalar.
+/// The raw attribute should contain precisely 1 argument, the vector size for
+/// the variable, measured in bytes. If curType and rawAttr are well formed,
+/// this routine will return a new vector type.
+static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, Sema &S) {
+  // Check the attribute arugments.
+  if (Attr.getNumArgs() != 1) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
+    return;
+  }
+  Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
+  llvm::APSInt vecSize(32);
+  if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
+      << "vector_size" << sizeExpr->getSourceRange();
+    return;
+  }
+  // the base type must be integer or float, and can't already be a vector.
+  if (CurType->isVectorType() ||
+      (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
+    return;
+  }
+  unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
+  // vecSize is specified in bytes - convert to bits.
+  unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
+
+  // the vector size needs to be an integral multiple of the type size.
+  if (vectorSize % typeSize) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
+      << sizeExpr->getSourceRange();
+    return;
+  }
+  if (vectorSize == 0) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
+      << sizeExpr->getSourceRange();
+    return;
+  }
+
+  // Success! Instantiate the vector type, the number of elements is > 0, and
+  // not required to be a power of 2, unlike GCC.
+  CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
+}
+
 void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
   // Scan through and apply attributes to this type where it makes sense.  Some
   // attributes (such as __address_space__, __vector_size__, etc) apply to the
@@ -1662,6 +1709,9 @@
     case AttributeList::AT_noreturn:
       HandleNoReturnTypeAttribute(Result, *AL, *this);
       break;
+    case AttributeList::AT_vector_size:
+      HandleVectorSizeAttr(Result, *AL, *this);
+      break;
     }
   }
 }

Modified: cfe/trunk/test/Sema/vector-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/vector-init.c?rev=90600&r1=90599&r2=90600&view=diff

==============================================================================
--- cfe/trunk/test/Sema/vector-init.c (original)
+++ cfe/trunk/test/Sema/vector-init.c Fri Dec  4 15:51:28 2009
@@ -16,10 +16,15 @@
 float4 array3[2] = { {1.0, 2.0, 3.0}, 5.0, 6.0, 7.0, 8.0,
                      9.0 }; // expected-warning {{excess elements in array initializer}}
 
+// PR5650
+__attribute__((vector_size(16))) float f1(void) {
+  __attribute__((vector_size(16))) float vec = {0.0f, 0.0f, 0.0f};
+  return(vec);
+}
 
-// rdar://6881069
-__attribute__((vector_size(16))) // expected-error {{unsupported type 'float (void)' for vector_size attribute, please use on typedef}}
-float f1(void) {
+__attribute__((vector_size(16))) float f2(
+    __attribute__((vector_size(16))) float a1) {
+  return(a1);
 }
 
 





More information about the cfe-commits mailing list