Skip to content

Commit dd5aa65

Browse files
committed
[PowerPC] Implement vector bool/pixel initialization under -faltivec-src-compat=xl
This patch implements the initialization of vectors under the -faltivec-src-compat=xl option introduced in https://reviews.llvm.org/D103615. Under this option, the initialization of scalar vectors, vector bool, and vector pixel are treated the same, where the initialization value is splatted across the whole vector. This patch does not change the behaviour of the -faltivec-src-compat=mixed option, which is the current default for Clang. Differential Revision: https://reviews.llvm.org/D106120
1 parent f463212 commit dd5aa65

File tree

5 files changed

+205
-6
lines changed

5 files changed

+205
-6
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6090,6 +6090,12 @@ class Sema final {
60906090
void CheckCompatibleReinterpretCast(QualType SrcType, QualType DestType,
60916091
bool IsDereference, SourceRange Range);
60926092

6093+
// Checks that the vector type should be initialized from a scalar
6094+
// by splatting the value rather than populating a single element.
6095+
// This is the case for AltiVecVector types as well as with
6096+
// AltiVecPixel and AltiVecBool when -faltivec-src-compat=xl is specified.
6097+
bool ShouldSplatAltivecScalarInCast(const VectorType *VecTy);
6098+
60936099
/// ActOnCXXNamedCast - Parse
60946100
/// {dynamic,static,reinterpret,const,addrspace}_cast's.
60956101
ExprResult ActOnCXXNamedCast(SourceLocation OpLoc,

clang/lib/Sema/SemaCast.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,19 @@ void CastOperation::checkAddressSpaceCast(QualType SrcType, QualType DestType) {
26242624
}
26252625
}
26262626

2627+
bool Sema::ShouldSplatAltivecScalarInCast(const VectorType *VecTy) {
2628+
bool SrcCompatXL = this->getLangOpts().getAltivecSrcCompat() ==
2629+
LangOptions::AltivecSrcCompatKind::XL;
2630+
VectorType::VectorKind VKind = VecTy->getVectorKind();
2631+
2632+
if ((VKind == VectorType::AltiVecVector) ||
2633+
(SrcCompatXL && ((VKind == VectorType::AltiVecBool) ||
2634+
(VKind == VectorType::AltiVecPixel)))) {
2635+
return true;
2636+
}
2637+
return false;
2638+
}
2639+
26272640
void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle,
26282641
bool ListInitialization) {
26292642
assert(Self.getLangOpts().CPlusPlus);
@@ -2678,9 +2691,9 @@ void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle,
26782691

26792692
// AltiVec vector initialization with a single literal.
26802693
if (const VectorType *vecTy = DestType->getAs<VectorType>())
2681-
if (vecTy->getVectorKind() == VectorType::AltiVecVector
2682-
&& (SrcExpr.get()->getType()->isIntegerType()
2683-
|| SrcExpr.get()->getType()->isFloatingType())) {
2694+
if (Self.ShouldSplatAltivecScalarInCast(vecTy) &&
2695+
(SrcExpr.get()->getType()->isIntegerType() ||
2696+
SrcExpr.get()->getType()->isFloatingType())) {
26842697
Kind = CK_VectorSplat;
26852698
SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get());
26862699
return;
@@ -2963,8 +2976,8 @@ void CastOperation::CheckCStyleCast() {
29632976
}
29642977

29652978
if (const VectorType *DestVecTy = DestType->getAs<VectorType>()) {
2966-
if (DestVecTy->getVectorKind() == VectorType::AltiVecVector &&
2967-
(SrcType->isIntegerType() || SrcType->isFloatingType())) {
2979+
if (Self.ShouldSplatAltivecScalarInCast(DestVecTy) &&
2980+
(SrcType->isIntegerType() || SrcType->isFloatingType())) {
29682981
Kind = CK_VectorSplat;
29692982
SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get());
29702983
} else if (Self.CheckVectorCast(OpRange, DestType, SrcType, Kind)) {

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7707,7 +7707,7 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc,
77077707
// initializers must be one or must match the size of the vector.
77087708
// If a single value is specified in the initializer then it will be
77097709
// replicated to all the components of the vector
7710-
if (VTy->getVectorKind() == VectorType::AltiVecVector) {
7710+
if (ShouldSplatAltivecScalarInCast(VTy)) {
77117711
// The number of initializers must be one or must match the size of the
77127712
// vector. If a single value is specified in the initializer then it will
77137713
// be replicated to all the components of the vector
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// RUN: not %clang_cc1 -target-feature +altivec -target-feature +vsx \
2+
// RUN: -faltivec-src-compat=mixed -triple powerpc-unknown-unknown -S \
3+
// RUN: -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=MIXED-ERR
4+
// RUN: not %clang_cc1 -target-feature +altivec -target-feature +vsx \
5+
// RUN: -faltivec-src-compat=mixed -triple powerpc64le-unknown-unknown -S \
6+
// RUN: -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=MIXED-ERR
7+
// RUN: %clang_cc1 -target-feature +altivec -target-feature +vsx \
8+
// RUN: -faltivec-src-compat=xl -triple powerpc-unknown-unknown -S \
9+
// RUN: -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
10+
// RUN: %clang_cc1 -target-feature +altivec -target-feature +vsx \
11+
// RUN: -faltivec-src-compat=xl -triple powerpc64le-unknown-unknown -S \
12+
// RUN: -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
13+
// RUN: not %clang -mcpu=pwr8 -faltivec-src-compat=mixed --target=powerpc-unknown-unknown \
14+
// RUN: -S -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=MIXED-ERR
15+
// RUN: not %clang -mcpu=pwr9 -faltivec-src-compat=mixed --target=powerpc-unknown-unknown \
16+
// RUN: -S -emit-llvm %s -o - 2>&1 | FileCheck %s --check-prefix=MIXED-ERR
17+
// RUN: %clang -mcpu=pwr8 -faltivec-src-compat=xl --target=powerpc-unknown-unknown \
18+
// RUN: -S -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
19+
// RUN: %clang -mcpu=pwr9 -faltivec-src-compat=xl --target=powerpc-unknown-unknown \
20+
// RUN: -S -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
21+
22+
// Vector bool type
23+
vector bool char vbi8_1;
24+
vector bool char vbi8_2;
25+
26+
vector bool short vbi16_1;
27+
vector bool short vbi16_2;
28+
29+
vector bool int vbi32_1;
30+
vector bool int vbi32_2;
31+
32+
vector bool long long vbi64_1;
33+
vector bool long long vbi64_2;
34+
35+
// Vector pixel type
36+
vector pixel p1;
37+
38+
////////////////////////////////////////////////////////////////////////////////
39+
void test_vector_bool_pixel_init_no_parentheses() {
40+
// vector bool char initialization
41+
vbi8_1 = (vector bool char)'a';
42+
// MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned char'
43+
// 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>
44+
char c = 'c';
45+
vbi8_2 = (vector bool char)c;
46+
// MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned char'
47+
// XL: [[INS_ELT:%.*]] = insertelement <16 x i8>
48+
// XL: [[SHUFF:%.*]] = shufflevector <16 x i8> [[INS_ELT]], <16 x i8> poison, <16 x i32> zeroinitializer
49+
// XL: store <16 x i8> [[SHUFF]]
50+
51+
// vector bool short initialization
52+
vbi16_1 = (vector bool short)5;
53+
// MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned short'
54+
// XL: <i16 5, i16 5, i16 5, i16 5, i16 5, i16 5, i16 5, i16 5>
55+
short si16 = 55;
56+
vbi16_2 = (vector bool short)si16;
57+
// MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned short'
58+
// XL: [[INS_ELT:%.*]] = insertelement <8 x i16>
59+
// XL: [[SHUFF:%.*]] = shufflevector <8 x i16> [[INS_ELT]], <8 x i16> poison, <8 x i32> zeroinitializer
60+
// XL: store <8 x i16> [[SHUFF]]
61+
62+
// vector bool int initialization
63+
vbi32_1 = (vector bool int)9;
64+
// MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned int'
65+
// XL: <i32 9, i32 9, i32 9, i32 9>
66+
int si32 = 99;
67+
vbi32_2 = (vector bool int)si32;
68+
// MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned int'
69+
// XL: [[INS_ELT:%.*]] = insertelement <4 x i32>
70+
// XL: [[SHUFF:%.*]] = shufflevector <4 x i32> [[INS_ELT]], <4 x i32> poison, <4 x i32> zeroinitializer
71+
// XL: store <4 x i32> [[SHUFF]]
72+
73+
// vector bool long long initialization
74+
vbi64_1 = (vector bool long long)13;
75+
// MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned long long'
76+
// XL: <i64 13, i64 13>
77+
long long si64 = 1313;
78+
vbi64_2 = (vector bool long long)si64;
79+
// MIXED-ERR: error: invalid conversion between vector type '__vector __bool unsigned long long'
80+
// XL: [[INS_ELT:%.*]] = insertelement <2 x i64>
81+
// XL: [[SHUFF:%.*]] = shufflevector <2 x i64> [[INS_ELT]], <2 x i64> poison, <2 x i32> zeroinitializer
82+
// XL: store <2 x i64> [[SHUFF]]
83+
84+
// vector pixel initialization
85+
p1 = (vector pixel)1;
86+
// MIXED-ERR: error: invalid conversion between vector type '__vector __pixel '
87+
// XL: <i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1>
88+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// RUN: %clang_cc1 -target-feature +altivec -target-feature +vsx \
2+
// RUN: -faltivec-src-compat=mixed -triple powerpc-unknown-unknown -S \
3+
// RUN: -emit-llvm %s -o - | FileCheck %s --check-prefix=MIXED
4+
// RUN: %clang_cc1 -target-feature +altivec -target-feature +vsx \
5+
// RUN: -faltivec-src-compat=mixed -triple powerpc64le-unknown-unknown -S \
6+
// RUN: -emit-llvm %s -o - | FileCheck %s --check-prefix=MIXED
7+
// RUN: %clang_cc1 -target-feature +altivec -target-feature +vsx \
8+
// RUN: -faltivec-src-compat=xl -triple powerpc-unknown-unknown -S \
9+
// RUN: -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
10+
// RUN: %clang_cc1 -target-feature +altivec -target-feature +vsx \
11+
// RUN: -faltivec-src-compat=xl -triple powerpc64le-unknown-unknown -S \
12+
// RUN: -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
13+
// RUN: %clang -mcpu=pwr8 -faltivec-src-compat=mixed --target=powerpc-unknown-unknown \
14+
// RUN: -S -emit-llvm %s -o - | FileCheck %s --check-prefix=MIXED
15+
// RUN: %clang -mcpu=pwr9 -faltivec-src-compat=mixed --target=powerpc-unknown-unknown \
16+
// RUN: -S -emit-llvm %s -o - | FileCheck %s --check-prefix=MIXED
17+
// RUN: %clang -mcpu=pwr8 -faltivec-src-compat=xl --target=powerpc-unknown-unknown \
18+
// RUN: -S -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
19+
// RUN: %clang -mcpu=pwr9 -faltivec-src-compat=xl --target=powerpc-unknown-unknown \
20+
// RUN: -S -emit-llvm %s -o - | FileCheck %s --check-prefix=XL
21+
22+
// Vector bool type
23+
vector bool char vbi8_1;
24+
vector bool char vbi8_2;
25+
26+
vector bool short vbi16_1;
27+
vector bool short vbi16_2;
28+
29+
vector bool int vbi32_1;
30+
vector bool int vbi32_2;
31+
32+
vector bool long long vbi64_1;
33+
vector bool long long vbi64_2;
34+
35+
// Vector pixel type
36+
vector pixel p1;
37+
38+
////////////////////////////////////////////////////////////////////////////////
39+
void test_vector_bool_pixel_init() {
40+
// vector bool char initialization
41+
vbi8_1 = (vector bool char)('a');
42+
// 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>
43+
// 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>
44+
char c = 'c';
45+
vbi8_2 = (vector bool char)(c);
46+
// MIXED: [[INS:%.*]] = insertelement <16 x i8>
47+
// MIXED: store <16 x i8> [[INS:%.*]]
48+
// XL: [[INS_ELT:%.*]] = insertelement <16 x i8>
49+
// XL: [[SHUFF:%.*]] = shufflevector <16 x i8> [[INS_ELT]], <16 x i8> poison, <16 x i32> zeroinitializer
50+
// XL: store <16 x i8> [[SHUFF]]
51+
52+
// vector bool short initialization
53+
vbi16_1 = (vector bool short)(5);
54+
// MIXED: <i16 5, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>
55+
// XL: <i16 5, i16 5, i16 5, i16 5, i16 5, i16 5, i16 5, i16 5>
56+
short si16 = 55;
57+
vbi16_2 = (vector bool short)(si16);
58+
// MIXED: [[INS:%.*]] = insertelement <8 x i16>
59+
// MIXED: store <8 x i16> [[INS:%.*]]
60+
// XL: [[INS_ELT:%.*]] = insertelement <8 x i16>
61+
// XL: [[SHUFF:%.*]] = shufflevector <8 x i16> [[INS_ELT]], <8 x i16> poison, <8 x i32> zeroinitializer
62+
// XL: store <8 x i16> [[SHUFF]]
63+
64+
// vector bool int initialization
65+
vbi32_1 = (vector bool int)(9);
66+
// MIXED: <i32 9, i32 0, i32 0, i32 0>
67+
// XL: <i32 9, i32 9, i32 9, i32 9>
68+
int si32 = 99;
69+
vbi32_2 = (vector bool int)(si32);
70+
// MIXED: [[INS:%.*]] = insertelement <4 x i32>
71+
// MIXED: store <4 x i32> [[INS:%.*]]
72+
// XL: [[INS_ELT:%.*]] = insertelement <4 x i32>
73+
// XL: [[SHUFF:%.*]] = shufflevector <4 x i32> [[INS_ELT]], <4 x i32> poison, <4 x i32> zeroinitializer
74+
// XL: store <4 x i32> [[SHUFF]]
75+
76+
// vector bool long long initialization
77+
vbi64_1 = (vector bool long long)(13);
78+
// MIXED: <i64 13, i64 0>
79+
// XL: <i64 13, i64 13>
80+
long long si64 = 1313;
81+
vbi64_2 = (vector bool long long)(si64);
82+
// MIXED: [[INS:%.*]] = insertelement <2 x i64>
83+
// MIXED: store <2 x i64> [[INS:%.*]]
84+
// XL: [[INS_ELT:%.*]] = insertelement <2 x i64>
85+
// XL: [[SHUFF:%.*]] = shufflevector <2 x i64> [[INS_ELT]], <2 x i64> poison, <2 x i32> zeroinitializer
86+
// XL: store <2 x i64> [[SHUFF]]
87+
88+
// vector pixel initialization
89+
p1 = (vector pixel)(1);
90+
// MIXED: <i16 1, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>
91+
// XL: <i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1>
92+
}

0 commit comments

Comments
 (0)