[cfe-commits] r74247 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/Sema.h lib/Sema/SemaExpr.cpp test/Sema/ext_vector_casts.c test/Sema/vector-cast.c

Nate Begeman natebegeman at mac.com
Thu Jun 25 17:50:28 PDT 2009


Author: sampo
Date: Thu Jun 25 19:50:28 2009
New Revision: 74247

URL: http://llvm.org/viewvc/llvm-project?rev=74247&view=rev
Log:
OpenCL 1.0 support: explicit casts to ext-vector types

Added:
    cfe/trunk/test/Sema/ext_vector_casts.c
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/vector-cast.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=74247&r1=74246&r2=74247&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Jun 25 19:50:28 2009
@@ -1860,6 +1860,9 @@
 def err_collection_expr_type : Error<
   "collection expression type %0 is not a valid object">;
 
+def err_invalid_conversion_between_ext_vectors : Error<
+  "invalid conversion between ext-vector type %0 and %1">;
+
 // Type
 def ext_invalid_sign_spec : Extension<"'%0' cannot be signed or unsigned">;
 def warn_receiver_forward_class : Warning<

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=74247&r1=74246&r2=74247&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu Jun 25 19:50:28 2009
@@ -3063,6 +3063,13 @@
   // returns true if the cast is invalid
   bool CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty);
   
+  // CheckExtVectorCast - check type constraints for extended vectors. 
+  // Since vectors are an extension, there are no C standard reference for this.
+  // We allow casting between vectors and integer datatypes of the same size,
+  // or vectors and the element type of that vector.
+  // returns true if the cast is invalid
+  bool CheckExtVectorCast(SourceRange R, QualType VectorTy, QualType Ty);
+  
   /// CheckMessageArgumentTypes - Check types in an Obj-C message send. 
   /// \param Method - May be null.
   /// \param [out] ReturnType - The return type of the send.

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Jun 25 19:50:28 2009
@@ -2876,12 +2876,15 @@
     return Diag(castExpr->getLocStart(),
                 diag::err_typecheck_expect_scalar_operand)
       << castExpr->getType() << castExpr->getSourceRange();
-  } else if (castExpr->getType()->isVectorType()) {
-    if (CheckVectorCast(TyR, castExpr->getType(), castType))
+  } else if (castType->isExtVectorType()) {
+    if (CheckExtVectorCast(TyR, castType, castExpr->getType()))
       return true;
   } else if (castType->isVectorType()) {
     if (CheckVectorCast(TyR, castType, castExpr->getType()))
       return true;
+  } else if (castExpr->getType()->isVectorType()) {
+    if (CheckVectorCast(TyR, castExpr->getType(), castType))
+      return true;
   } else if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) {
     return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR;
   } else if (!castType->isArithmeticType()) {
@@ -2919,6 +2922,35 @@
   return false;
 }
 
+bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, QualType SrcTy) {
+  assert(DestTy->isExtVectorType() && "Not an extended vector type!");
+  
+  // If SrcTy is also an ExtVectorType, the types must be identical unless 
+  // lax vector conversions is enabled.
+  if (SrcTy->isExtVectorType()) {
+    if (getLangOptions().LaxVectorConversions &&
+        Context.getTypeSize(DestTy) == Context.getTypeSize(SrcTy))
+      return false;
+    if (DestTy != SrcTy)
+      return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors)
+      << DestTy << SrcTy << R;
+    return false;
+  }
+  
+  // If SrcTy is a VectorType, then only the total size must match.
+  if (SrcTy->isVectorType()) {
+    if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy))
+      return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors)
+        << DestTy << SrcTy << R;
+    return false;
+  }
+
+  // All scalar -> ext vector "c-style" casts are legal; the appropriate
+  // conversion will take place first from scalar to elt type, and then
+  // splat from elt type to vector.
+  return false;
+}
+
 Action::OwningExprResult
 Sema::ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
                     SourceLocation RParenLoc, ExprArg Op) {

Added: cfe/trunk/test/Sema/ext_vector_casts.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/ext_vector_casts.c?rev=74247&view=auto

==============================================================================
--- cfe/trunk/test/Sema/ext_vector_casts.c (added)
+++ cfe/trunk/test/Sema/ext_vector_casts.c Thu Jun 25 19:50:28 2009
@@ -0,0 +1,23 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+typedef __attribute__(( ext_vector_type(2) )) float float2;
+typedef __attribute__(( ext_vector_type(4) )) int int4;
+typedef __attribute__(( ext_vector_type(4) )) float float4;
+typedef float t3 __attribute__ ((vector_size (16)));
+
+static void test() {
+    float2 vec2;
+    float4 vec4, vec4_2;
+    int4 ivec4;
+    t3 vec4_3;
+    
+    vec4 = (float4)5.0f;
+    vec4 = (float4)5;
+    vec4 = (float4)vec4_3;
+    
+    ivec4 = (int4)5.0f;
+    ivec4 = (int4)5;
+    ivec4 = (int4)vec4_3;
+    
+    vec4 = (float4)vec2; // expected-error {{invalid conversion between ext-vector type 'float4' and 'float2'}}
+}

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

==============================================================================
--- cfe/trunk/test/Sema/vector-cast.c (original)
+++ cfe/trunk/test/Sema/vector-cast.c Thu Jun 25 19:50:28 2009
@@ -11,9 +11,9 @@
   t3 v3;
   
   v2 = (t2)v1; // -expected-error {{invalid conversion between vector type \
-'t1' and 't2' of different size}}
-  v1 = (t1)v2; // -expected-error {{invalid conversion between vector type \
 't2' and 't1' of different size}}
+  v1 = (t1)v2; // -expected-error {{invalid conversion between vector type \
+'t1' and 't2' of different size}}
   v3 = (t3)v2;
   
   v1 = (t1)(char *)10; // -expected-error {{invalid conversion between vector \





More information about the cfe-commits mailing list