[clang] dd0d956 - [clang][bytecode] Support vector-to-vector bitcasts (#118230)

via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 1 23:20:45 PST 2024


Author: Timm Baeder
Date: 2024-12-02T08:20:41+01:00
New Revision: dd0d9561b8945d4128814bd7eda1bc470a1552f7

URL: https://github.com/llvm/llvm-project/commit/dd0d9561b8945d4128814bd7eda1bc470a1552f7
DIFF: https://github.com/llvm/llvm-project/commit/dd0d9561b8945d4128814bd7eda1bc470a1552f7.diff

LOG: [clang][bytecode] Support vector-to-vector bitcasts (#118230)

Added: 
    clang/test/AST/ByteCode/altivec.c

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/test/AST/ByteCode/vectors.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index f4cc284dfb6abf..754affffb8f04a 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -448,8 +448,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
 
     QualType SubExprTy = SubExpr->getType();
     std::optional<PrimType> FromT = classify(SubExprTy);
-    // Casts from integer to vectors in C.
-    if (FromT && CE->getType()->isVectorType())
+    // Casts from integer/vector to vector.
+    if (CE->getType()->isVectorType())
       return this->emitBuiltinBitCast(CE);
 
     std::optional<PrimType> ToT = classify(CE->getType());
@@ -6502,7 +6502,7 @@ bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
   // we later assume it to be one (i.e. a PT_Ptr). However,
   // we call this function for other utility methods where
   // a bitcast might be useful, so convert it to a PT_Ptr in that case.
-  if (SubExpr->isGLValue()) {
+  if (SubExpr->isGLValue() || FromType->isVectorType()) {
     if (!this->visit(SubExpr))
       return false;
   } else if (std::optional<PrimType> FromT = classify(SubExpr)) {

diff  --git a/clang/test/AST/ByteCode/altivec.c b/clang/test/AST/ByteCode/altivec.c
new file mode 100644
index 00000000000000..39caedc9949d20
--- /dev/null
+++ b/clang/test/AST/ByteCode/altivec.c
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -triple=powerpc64-unknown-linux-gnu -target-feature +altivec -target-feature +vsx -fsyntax-only -verify=expected,both %s
+// RUN: %clang_cc1 -triple=powerpc64le-unknown-linux-gnu -target-feature +altivec -target-feature -vsx -fsyntax-only -verify=expected,both %s
+// RUN: %clang_cc1 -triple=powerpc-ibm-aix -target-feature +altivec -fsyntax-only -verify=expected,both %s
+// RUN: %clang_cc1 -triple=powerpc64-ibm-aix -target-feature +altivec -target-feature -vsx -fsyntax-only -verify=expected,both %s
+
+// RUN: %clang_cc1 -triple=powerpc64-unknown-linux-gnu -target-feature +altivec -target-feature +vsx -fsyntax-only -verify=expected,both -fexperimental-new-constant-interpreter %s
+// RUN: %clang_cc1 -triple=powerpc64le-unknown-linux-gnu -target-feature +altivec -target-feature -vsx -fsyntax-only -verify=expected,both -fexperimental-new-constant-interpreter %s
+// RUN: %clang_cc1 -triple=powerpc-ibm-aix -target-feature +altivec -fsyntax-only -verify=expected,both -fexperimental-new-constant-interpreter %s
+// RUN: %clang_cc1 -triple=powerpc64-ibm-aix -target-feature +altivec -target-feature -vsx -fsyntax-only -verify=expected,both -fexperimental-new-constant-interpreter %s
+
+// both-no-diagnostics.
+
+/// From test/Parser/altivec.c
+vector char v1 = (vector char)((vector int)(1, 2, 3, 4));
+vector char v2 = (vector char)((vector float)(1.0f, 2.0f, 3.0f, 4.0f));
+vector char v3 = (vector char)((vector int)('a', 'b', 'c', 'd'));
+vector int v4 = (vector int)(1, 2, 3, 4);
+vector float v5 = (vector float)(1.0f, 2.0f, 3.0f, 4.0f);
+vector char v6 = (vector char)((vector int)(1+2, -2, (int)(2.0 * 3), -(5-3)));

diff  --git a/clang/test/AST/ByteCode/vectors.cpp b/clang/test/AST/ByteCode/vectors.cpp
index a0aace44f3c981..08e2ca2adbf5cd 100644
--- a/clang/test/AST/ByteCode/vectors.cpp
+++ b/clang/test/AST/ByteCode/vectors.cpp
@@ -55,10 +55,9 @@ namespace Vector {
   static_assert(__builtin_vectorelements(v2) == (32 / sizeof(double)), "");
 }
 
-/// FIXME: We need to support BitCasts between vector types.
 namespace {
   typedef float __attribute__((vector_size(16))) VI42;
-  constexpr VI42 A2 = A; // expected-error {{must be initialized by a constant expression}}
+  constexpr VI42 A2 = A;
 }
 
 namespace BoolToSignedIntegralCast{


        


More information about the cfe-commits mailing list