sammccall updated this revision to Diff 254646.
sammccall added a comment.

Add 1<<100 testcase, and clang-format


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77335/new/

https://reviews.llvm.org/D77335

Files:
  clang/include/clang/AST/Type.h
  clang/lib/AST/Type.cpp
  clang/lib/Sema/SemaType.cpp
  clang/test/Sema/types.c
  clang/test/SemaCXX/vector.cpp

Index: clang/test/SemaCXX/vector.cpp
===================================================================
--- clang/test/SemaCXX/vector.cpp
+++ clang/test/SemaCXX/vector.cpp
@@ -335,7 +335,7 @@
 typedef bool bad __attribute__((__vector_size__(16)));  // expected-error {{invalid vector element type 'bool'}}
 
 namespace Templates {
-template <typename Elt, unsigned Size>
+template <typename Elt, unsigned long long Size>
 struct TemplateVectorType {
   typedef Elt __attribute__((__vector_size__(Size))) type; // #1
 };
@@ -343,7 +343,7 @@
 template <int N, typename T>
 struct PR15730 {
   typedef T __attribute__((vector_size(N * sizeof(T)))) type;
-  typedef T __attribute__((vector_size(8192))) type2; // #2
+  typedef T __attribute__((vector_size(0x1000000000))) type2; // #2
   typedef T __attribute__((vector_size(3))) type3; // #3
 };
 
@@ -352,19 +352,20 @@
   const TemplateVectorType<int, 32>::type Works2 = {};
   // expected-error@#1 {{invalid vector element type 'bool'}}
   // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<bool, 32>' requested here}}
-  const TemplateVectorType<bool, 32>::type NoBool;
+  const TemplateVectorType<bool, 32>::type NoBool = {};
   // expected-error@#1 {{invalid vector element type 'int __attribute__((ext_vector_type(4)))' (vector of 4 'int' values)}}
   // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<int __attribute__((ext_vector_type(4))), 32>' requested here}}
-  const TemplateVectorType<vi4, 32>::type NoComplex;
+  const TemplateVectorType<vi4, 32>::type NoComplex = {};
   // expected-error@#1 {{vector size not an integral multiple of component size}}
   // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<int, 33>' requested here}}
-  const TemplateVectorType<int, 33>::type BadSize;
+  const TemplateVectorType<int, 33>::type BadSize = {};
+  const TemplateVectorType<int, 3200>::type Large = {};
   // expected-error@#1 {{vector size too large}}
-  // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<int, 8192>' requested here}}
-  const TemplateVectorType<int, 8192>::type TooLarge;
+  // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<int, 68719476736>' requested here}}
+  const TemplateVectorType<int, 0x1000000000>::type TooLarge = {};
   // expected-error@#1 {{zero vector size}}
   // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<int, 0>' requested here}}
-  const TemplateVectorType<int, 0>::type Zero;
+  const TemplateVectorType<int, 0>::type Zero = {};
 
   // expected-error@#2 {{vector size too large}}
   // expected-error@#3 {{vector size not an integral multiple of component size}}
Index: clang/test/Sema/types.c
===================================================================
--- clang/test/Sema/types.c
+++ clang/test/Sema/types.c
@@ -69,9 +69,15 @@
   char c = (char __attribute__((may_alias))) i;
 }
 
-// vector size too large
-int __attribute__ ((vector_size(8192))) x1; // expected-error {{vector size too large}}
-typedef int __attribute__ ((ext_vector_type(8192))) x2; // expected-error {{vector size too large}}
+// vector size
+int __attribute__((vector_size(123456))) v1;
+int __attribute__((vector_size(0x1000000000))) v2;         // expected-error {{vector size too large}}
+int __attribute__((vector_size((__int128_t)1 << 100))) v3; // expected-error {{vector size too large}}
+int __attribute__((vector_size(0))) v4;                    // expected-error {{zero vector size}}
+typedef int __attribute__((ext_vector_type(123456))) e1;
+typedef int __attribute__((ext_vector_type(0x100000000))) e2;      // expected-error {{vector size too large}}
+typedef int __attribute__((vector_size((__int128_t)1 << 100))) e3; // expected-error {{vector size too large}}
+typedef int __attribute__((ext_vector_type(0))) e4;                // expected-error {{zero vector size}}
 
 // no support for vector enum type
 enum { e_2 } x3 __attribute__((vector_size(64))); // expected-error {{invalid vector element type}}
Index: clang/lib/Sema/SemaType.cpp
===================================================================
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -2436,28 +2436,34 @@
     return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc,
                                                VectorType::GenericVector);
 
-  unsigned VectorSize = static_cast<unsigned>(VecSize.getZExtValue() * 8);
+  // vecSize is specified in bytes - convert to bits.
+  if (!VecSize.isIntN(61)) {
+    // Bit size will overflow uint64.
+    Diag(AttrLoc, diag::err_attribute_size_too_large)
+        << SizeExpr->getSourceRange();
+    return QualType();
+  }
+  uint64_t VectorSizeBits = VecSize.getZExtValue() * 8;
   unsigned TypeSize = static_cast<unsigned>(Context.getTypeSize(CurType));
 
-  if (VectorSize == 0) {
+  if (VectorSizeBits == 0) {
     Diag(AttrLoc, diag::err_attribute_zero_size) << SizeExpr->getSourceRange();
     return QualType();
   }
 
-  // vecSize is specified in bytes - convert to bits.
-  if (VectorSize % TypeSize) {
+  if (VectorSizeBits % TypeSize) {
     Diag(AttrLoc, diag::err_attribute_invalid_size)
         << SizeExpr->getSourceRange();
     return QualType();
   }
 
-  if (VectorType::isVectorSizeTooLarge(VectorSize / TypeSize)) {
+  if (VectorSizeBits / TypeSize > std::numeric_limits<unsigned>::max()) {
     Diag(AttrLoc, diag::err_attribute_size_too_large)
         << SizeExpr->getSourceRange();
     return QualType();
   }
 
-  return Context.getVectorType(CurType, VectorSize / TypeSize,
+  return Context.getVectorType(CurType, VectorSizeBits / TypeSize,
                                VectorType::GenericVector);
 }
 
@@ -2489,6 +2495,11 @@
       return QualType();
     }
 
+    if (!vecSize.isIntN(32)) {
+      Diag(AttrLoc, diag::err_attribute_size_too_large)
+          << ArraySize->getSourceRange();
+      return QualType();
+    }
     // Unlike gcc's vector_size attribute, the size is specified as the
     // number of elements, not the number of bytes.
     unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
@@ -2499,12 +2510,6 @@
       return QualType();
     }
 
-    if (VectorType::isVectorSizeTooLarge(vectorSize)) {
-      Diag(AttrLoc, diag::err_attribute_size_too_large)
-        << ArraySize->getSourceRange();
-      return QualType();
-    }
-
     return Context.getExtVectorType(T, vectorSize);
   }
 
Index: clang/lib/AST/Type.cpp
===================================================================
--- clang/lib/AST/Type.cpp
+++ clang/lib/AST/Type.cpp
@@ -288,9 +288,9 @@
 
 VectorType::VectorType(TypeClass tc, QualType vecType, unsigned nElements,
                        QualType canonType, VectorKind vecKind)
-    : Type(tc, canonType, vecType->getDependence()), ElementType(vecType) {
+    : Type(tc, canonType, vecType->getDependence()), ElementType(vecType),
+      NumElements(nElements) {
   VectorTypeBits.VecKind = vecKind;
-  VectorTypeBits.NumElements = nElements;
 }
 
 /// getArrayElementTypeNoTypeQual - If this is an array type, return the
Index: clang/include/clang/AST/Type.h
===================================================================
--- clang/include/clang/AST/Type.h
+++ clang/include/clang/AST/Type.h
@@ -1650,11 +1650,6 @@
     /// The kind of vector, either a generic vector type or some
     /// target-specific vector type such as for AltiVec or Neon.
     unsigned VecKind : 3;
-
-    /// The number of elements in the vector.
-    unsigned NumElements : 29 - NumTypeBits;
-
-    enum { MaxNumElements = (1 << (29 - NumTypeBits)) - 1 };
   };
 
   class AttributedTypeBitfields {
@@ -3238,6 +3233,7 @@
 
   /// The element type of the vector.
   QualType ElementType;
+  unsigned NumElements;
 
   VectorType(QualType vecType, unsigned nElements, QualType canonType,
              VectorKind vecKind);
@@ -3247,11 +3243,7 @@
 
 public:
   QualType getElementType() const { return ElementType; }
-  unsigned getNumElements() const { return VectorTypeBits.NumElements; }
-
-  static bool isVectorSizeTooLarge(unsigned NumElements) {
-    return NumElements > VectorTypeBitfields::MaxNumElements;
-  }
+  unsigned getNumElements() const { return NumElements; }
 
   bool isSugared() const { return false; }
   QualType desugar() const { return QualType(this, 0); }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to