[clang] 5ea6117 - [PowerPC] Emit error for Altivec vector initializations when -faltivec-src-compat=gcc is specified

Amy Kwan via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 30 07:36:08 PDT 2021


Author: Amy Kwan
Date: 2021-07-30T09:35:43-05:00
New Revision: 5ea6117a9e9eae49ad1295fa422266ef3832e419

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

LOG: [PowerPC] Emit error for Altivec vector initializations when -faltivec-src-compat=gcc is specified

Under the -faltivec-src-compat=gcc option, AltiVec vector initialization should
be treated as if they were compiled with gcc - which is, to emit an error when
the vectors are initialized in the parenthesized or non-parenthesized manner.
This patch implements this behaviour.

Differential Revision: https://reviews.llvm.org/D106410

Added: 
    

Modified: 
    clang/include/clang/Sema/Sema.h
    clang/lib/Sema/SemaCast.cpp
    clang/lib/Sema/SemaExpr.cpp
    clang/test/CodeGen/vector-bool-pixel-altivec-init-no-parentheses.c
    clang/test/CodeGen/vector-bool-pixel-altivec-init.c

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1a696b4938061..07b7e61821f78 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -6100,6 +6100,13 @@ class Sema final {
   // AltiVecPixel and AltiVecBool when -faltivec-src-compat=xl is specified.
   bool ShouldSplatAltivecScalarInCast(const VectorType *VecTy);
 
+  // Checks if the -faltivec-src-compat=gcc option is specified.
+  // If so, AltiVecVector, AltiVecBool and AltiVecPixel types are
+  // treated the same way as they are when trying to initialize
+  // these vectors on gcc (an error is emitted).
+  bool CheckAltivecInitFromScalar(SourceRange R, QualType VecTy,
+                                  QualType SrcTy);
+
   /// ActOnCXXNamedCast - Parse
   /// {dynamic,static,reinterpret,const,addrspace}_cast's.
   ExprResult ActOnCXXNamedCast(SourceLocation OpLoc,

diff  --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp
index cac43075f860c..50edcc1c23020 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -2637,6 +2637,19 @@ bool Sema::ShouldSplatAltivecScalarInCast(const VectorType *VecTy) {
   return false;
 }
 
+bool Sema::CheckAltivecInitFromScalar(SourceRange R, QualType VecTy,
+                                      QualType SrcTy) {
+  bool SrcCompatGCC = this->getLangOpts().getAltivecSrcCompat() ==
+                      LangOptions::AltivecSrcCompatKind::GCC;
+  if (this->getLangOpts().AltiVec && SrcCompatGCC) {
+    this->Diag(R.getBegin(),
+               diag::err_invalid_conversion_between_vector_and_integer)
+        << VecTy << SrcTy << R;
+    return true;
+  }
+  return false;
+}
+
 void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle,
                                        bool ListInitialization) {
   assert(Self.getLangOpts().CPlusPlus);
@@ -2690,7 +2703,12 @@ void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle,
   }
 
   // AltiVec vector initialization with a single literal.
-  if (const VectorType *vecTy = DestType->getAs<VectorType>())
+  if (const VectorType *vecTy = DestType->getAs<VectorType>()) {
+    if (Self.CheckAltivecInitFromScalar(OpRange, DestType,
+                                        SrcExpr.get()->getType())) {
+      SrcExpr = ExprError();
+      return;
+    }
     if (Self.ShouldSplatAltivecScalarInCast(vecTy) &&
         (SrcExpr.get()->getType()->isIntegerType() ||
          SrcExpr.get()->getType()->isFloatingType())) {
@@ -2698,6 +2716,7 @@ void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle,
       SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get());
       return;
     }
+  }
 
   // C++ [expr.cast]p5: The conversions performed by
   //   - a const_cast,
@@ -2976,6 +2995,10 @@ void CastOperation::CheckCStyleCast() {
   }
 
   if (const VectorType *DestVecTy = DestType->getAs<VectorType>()) {
+    if (Self.CheckAltivecInitFromScalar(OpRange, DestType, SrcType)) {
+      SrcExpr = ExprError();
+      return;
+    }
     if (Self.ShouldSplatAltivecScalarInCast(DestVecTy) &&
         (SrcType->isIntegerType() || SrcType->isFloatingType())) {
       Kind = CK_VectorSplat;

diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 5ce5d122c2194..d316687c4cd8e 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7735,6 +7735,9 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc,
   // initializers must be one or must match the size of the vector.
   // If a single value is specified in the initializer then it will be
   // replicated to all the components of the vector
+  if (CheckAltivecInitFromScalar(E->getSourceRange(), Ty,
+                                 VTy->getElementType()))
+    return ExprError();
   if (ShouldSplatAltivecScalarInCast(VTy)) {
     // The number of initializers must be one or must match the size of the
     // vector. If a single value is specified in the initializer then it will

diff  --git a/clang/test/CodeGen/vector-bool-pixel-altivec-init-no-parentheses.c b/clang/test/CodeGen/vector-bool-pixel-altivec-init-no-parentheses.c
index 7a5c20e3d0bc2..db1d9d2e95bc1 100644
--- a/clang/test/CodeGen/vector-bool-pixel-altivec-init-no-parentheses.c
+++ b/clang/test/CodeGen/vector-bool-pixel-altivec-init-no-parentheses.c
@@ -4,6 +4,12 @@
 // RUN: not %clang_cc1 -target-feature +altivec -target-feature +vsx \
 // RUN:   -faltivec-src-compat=mixed -triple powerpc64le-unknown-unknown -S \
 // RUN:   -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=MIXED-ERR
+// RUN: not %clang_cc1 -target-feature +altivec -target-feature +vsx \
+// RUN:   -faltivec-src-compat=gcc -triple powerpc-unknown-unknown -S \
+// RUN:   -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=GCC-ERR
+// RUN: not %clang_cc1 -target-feature +altivec -target-feature +vsx \
+// RUN:   -faltivec-src-compat=gcc -triple powerpc64le-unknown-unknown -S \
+// RUN:   -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=GCC-ERR
 // RUN: %clang_cc1 -target-feature +altivec -target-feature +vsx \
 // RUN:   -faltivec-src-compat=xl -triple powerpc-unknown-unknown -S \
 // RUN:   -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
@@ -14,6 +20,10 @@
 // RUN:   -S -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=MIXED-ERR
 // RUN: not %clang -mcpu=pwr9 -faltivec-src-compat=mixed --target=powerpc-unknown-unknown \
 // RUN:   -S -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=MIXED-ERR
+// RUN: not %clang -mcpu=pwr8 -faltivec-src-compat=gcc --target=powerpc-unknown-unknown \
+// RUN:   -S -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=GCC-ERR
+// RUN: not %clang -mcpu=pwr9 -faltivec-src-compat=gcc --target=powerpc-unknown-unknown \
+// RUN:   -S -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=GCC-ERR
 // RUN: %clang -mcpu=pwr8 -faltivec-src-compat=xl --target=powerpc-unknown-unknown \
 // RUN:   -S -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
 // RUN: %clang -mcpu=pwr9 -faltivec-src-compat=xl --target=powerpc-unknown-unknown \
@@ -40,10 +50,12 @@ void test_vector_bool_pixel_init_no_parentheses() {
   // vector bool char initialization
   vbi8_1 = (vector bool char)'a';
   // MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned char'
+  // GCC-ERR: error: invalid conversion between vector type '__vector __bool unsigned char' (vector of 16 'unsigned char' values) and integer type 'int' of 
diff erent size
   // XL: <i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97>
   char c = 'c';
   vbi8_2 = (vector bool char)c;
   // MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned char'
+  // GCC-ERR: error: invalid conversion between vector type '__vector __bool unsigned char' (vector of 16 'unsigned char' values) and integer type 'char' of 
diff erent size
   // XL: [[INS_ELT:%.*]] = insertelement <16 x i8>
   // XL: [[SHUFF:%.*]] = shufflevector <16 x i8> [[INS_ELT]], <16 x i8> poison, <16 x i32> zeroinitializer
   // XL: store <16 x i8> [[SHUFF]]
@@ -51,10 +63,12 @@ void test_vector_bool_pixel_init_no_parentheses() {
   // vector bool short initialization
   vbi16_1 = (vector bool short)5;
   // MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned short'
+  // GCC-ERR: error: invalid conversion between vector type '__vector __bool unsigned short' (vector of 8 'unsigned short' values) and integer type 'int' of 
diff erent size
   // XL: <i16 5, i16 5, i16 5, i16 5, i16 5, i16 5, i16 5, i16 5>
   short si16 = 55;
   vbi16_2 = (vector bool short)si16;
   // MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned short'
+  // GCC-ERR: error: invalid conversion between vector type '__vector __bool unsigned short' (vector of 8 'unsigned short' values) and integer type 'short' of 
diff erent size
   // XL: [[INS_ELT:%.*]] = insertelement <8 x i16>
   // XL: [[SHUFF:%.*]] = shufflevector <8 x i16> [[INS_ELT]], <8 x i16> poison, <8 x i32> zeroinitializer
   // XL: store <8 x i16> [[SHUFF]]
@@ -62,10 +76,12 @@ void test_vector_bool_pixel_init_no_parentheses() {
   // vector bool int initialization
   vbi32_1 = (vector bool int)9;
   // MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned int'
+  // GCC-ERR: error: invalid conversion between vector type '__vector __bool unsigned int' (vector of 4 'unsigned int' values) and integer type 'int' of 
diff erent size
   // XL: <i32 9, i32 9, i32 9, i32 9>
   int si32 = 99;
   vbi32_2 = (vector bool int)si32;
   // MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned int'
+  // GCC-ERR: error: invalid conversion between vector type '__vector __bool unsigned int' (vector of 4 'unsigned int' values) and integer type 'int' of 
diff erent size
   // XL: [[INS_ELT:%.*]] = insertelement <4 x i32>
   // XL: [[SHUFF:%.*]] = shufflevector <4 x i32> [[INS_ELT]], <4 x i32> poison, <4 x i32> zeroinitializer
   // XL: store <4 x i32> [[SHUFF]]
@@ -73,10 +89,12 @@ void test_vector_bool_pixel_init_no_parentheses() {
   // vector bool long long initialization
   vbi64_1 = (vector bool long long)13;
   // MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned long long'
+  // GCC-ERR: error: invalid conversion between vector type '__vector __bool unsigned long long' (vector of 2 'unsigned long long' values) and integer type 'int' of 
diff erent size
   // XL: <i64 13, i64 13>
   long long si64 = 1313;
   vbi64_2 = (vector bool long long)si64;
   // MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned long long'
+  // GCC-ERR: error: invalid conversion between vector type '__vector __bool unsigned long long' (vector of 2 'unsigned long long' values) and integer type 'long long' of 
diff erent size
   // XL: [[INS_ELT:%.*]] = insertelement <2 x i64>
   // XL: [[SHUFF:%.*]] = shufflevector <2 x i64> [[INS_ELT]], <2 x i64> poison, <2 x i32> zeroinitializer
   // XL: store <2 x i64> [[SHUFF]]
@@ -84,5 +102,6 @@ void test_vector_bool_pixel_init_no_parentheses() {
   // vector pixel initialization
   p1 = (vector pixel)1;
   // MIXED-ERR: error: invalid conversion between vector type '__vector __pixel '
+  // GCC-ERR: error: invalid conversion between vector type '__vector __pixel ' (vector of 8 'unsigned short' values) and integer type 'int' of 
diff erent size
   // XL: <i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1>
 }

diff  --git a/clang/test/CodeGen/vector-bool-pixel-altivec-init.c b/clang/test/CodeGen/vector-bool-pixel-altivec-init.c
index dcaf2d3e5faf3..e7fa10c9a580b 100644
--- a/clang/test/CodeGen/vector-bool-pixel-altivec-init.c
+++ b/clang/test/CodeGen/vector-bool-pixel-altivec-init.c
@@ -10,6 +10,12 @@
 // RUN: %clang_cc1 -target-feature +altivec -target-feature +vsx \
 // RUN:   -faltivec-src-compat=xl -triple powerpc64le-unknown-unknown -S \
 // RUN:   -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
+// RUN: not %clang_cc1 -target-feature +altivec -target-feature +vsx \
+// RUN:   -faltivec-src-compat=gcc -triple powerpc-unknown-unknown -S \
+// RUN:   -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=GCC
+// RUN: not %clang_cc1 -target-feature +altivec -target-feature +vsx \
+// RUN:   -faltivec-src-compat=gcc -triple powerpc64le-unknown-unknown -S \
+// RUN:   -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=GCC
 // RUN: %clang -mcpu=pwr8 -faltivec-src-compat=mixed --target=powerpc-unknown-unknown \
 // RUN:   -S -emit-llvm %s -o - | FileCheck %s --check-prefix=MIXED
 // RUN: %clang -mcpu=pwr9 -faltivec-src-compat=mixed --target=powerpc-unknown-unknown \
@@ -18,6 +24,10 @@
 // RUN:   -S -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
 // RUN: %clang -mcpu=pwr9 -faltivec-src-compat=xl --target=powerpc-unknown-unknown \
 // RUN:   -S -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
+// RUN: not %clang -mcpu=pwr8 -faltivec-src-compat=gcc --target=powerpc-unknown-unknown \
+// RUN:   -S -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=GCC
+// RUN: not %clang -mcpu=pwr9 -faltivec-src-compat=gcc --target=powerpc-unknown-unknown \
+// RUN:   -S -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=GCC
 
 // Vector bool type
 vector bool char vbi8_1;
@@ -41,6 +51,7 @@ void test_vector_bool_pixel_init() {
   vbi8_1 = (vector bool char)('a');
   // MIXED: <i8 97, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>
   // XL: <i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97, i8 97>
+  // GCC: error: invalid conversion between vector type '__vector __bool unsigned char' (vector of 16 'unsigned char' values) and integer type 'unsigned char' of 
diff erent size
   char c = 'c';
   vbi8_2 = (vector bool char)(c);
   // MIXED: [[INS:%.*]] = insertelement <16 x i8>
@@ -48,11 +59,13 @@ void test_vector_bool_pixel_init() {
   // XL: [[INS_ELT:%.*]] = insertelement <16 x i8>
   // XL: [[SHUFF:%.*]] = shufflevector <16 x i8> [[INS_ELT]], <16 x i8> poison, <16 x i32> zeroinitializer
   // XL: store <16 x i8> [[SHUFF]]
+  // GCC: error: invalid conversion between vector type '__vector __bool unsigned char' (vector of 16 'unsigned char' values) and integer type 'unsigned char' of 
diff erent size
 
   // vector bool short initialization
   vbi16_1 = (vector bool short)(5);
   // MIXED: <i16 5, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>
   // XL: <i16 5, i16 5, i16 5, i16 5, i16 5, i16 5, i16 5, i16 5>
+  // GCC: error: invalid conversion between vector type '__vector __bool unsigned short' (vector of 8 'unsigned short' values) and integer type 'unsigned short' of 
diff erent size
   short si16 = 55;
   vbi16_2 = (vector bool short)(si16);
   // MIXED: [[INS:%.*]] = insertelement <8 x i16>
@@ -60,11 +73,13 @@ void test_vector_bool_pixel_init() {
   // XL: [[INS_ELT:%.*]] = insertelement <8 x i16>
   // XL: [[SHUFF:%.*]] = shufflevector <8 x i16> [[INS_ELT]], <8 x i16> poison, <8 x i32> zeroinitializer
   // XL: store <8 x i16> [[SHUFF]]
+  // GCC: error: invalid conversion between vector type '__vector __bool unsigned short' (vector of 8 'unsigned short' values) and integer type 'unsigned short' of 
diff erent size
 
   // vector bool int initialization
   vbi32_1 = (vector bool int)(9);
   // MIXED: <i32 9, i32 0, i32 0, i32 0>
   // XL: <i32 9, i32 9, i32 9, i32 9>
+  // GCC: error: invalid conversion between vector type '__vector __bool unsigned int' (vector of 4 'unsigned int' values) and integer type 'unsigned int' of 
diff erent size
   int si32 = 99;
   vbi32_2 = (vector bool int)(si32);
   // MIXED: [[INS:%.*]] = insertelement <4 x i32>
@@ -72,11 +87,13 @@ void test_vector_bool_pixel_init() {
   // XL: [[INS_ELT:%.*]] = insertelement <4 x i32>
   // XL: [[SHUFF:%.*]] = shufflevector <4 x i32> [[INS_ELT]], <4 x i32> poison, <4 x i32> zeroinitializer
   // XL: store <4 x i32> [[SHUFF]]
+  // GCC: error: invalid conversion between vector type '__vector __bool unsigned int' (vector of 4 'unsigned int' values) and integer type 'unsigned int' of 
diff erent size
 
   // vector bool long long initialization
   vbi64_1 = (vector bool long long)(13);
   // MIXED: <i64 13, i64 0>
   // XL: <i64 13, i64 13>
+  // GCC: error: invalid conversion between vector type '__vector __bool unsigned long long' (vector of 2 'unsigned long long' values) and integer type 'unsigned long long' of 
diff erent size
   long long si64 = 1313;
   vbi64_2 = (vector bool long long)(si64);
   // MIXED: [[INS:%.*]] = insertelement <2 x i64>
@@ -84,9 +101,11 @@ void test_vector_bool_pixel_init() {
   // XL: [[INS_ELT:%.*]] = insertelement <2 x i64>
   // XL: [[SHUFF:%.*]] = shufflevector <2 x i64> [[INS_ELT]], <2 x i64> poison, <2 x i32> zeroinitializer
   // XL: store <2 x i64> [[SHUFF]]
+  // GCC: error: invalid conversion between vector type '__vector __bool unsigned long long' (vector of 2 'unsigned long long' values) and integer type 'unsigned long long' of 
diff erent size
 
   // vector pixel initialization
   p1 = (vector pixel)(1);
   // MIXED: <i16 1, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>
   // XL: <i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1>
+  // GCC: error: invalid conversion between vector type '__vector __pixel ' (vector of 8 'unsigned short' values) and integer type 'unsigned short' of 
diff erent size
 }


        


More information about the cfe-commits mailing list