Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 38edf4b

Browse files
committed
[Sema] Allow shifting a scalar operand by a vector operand.
r278501 inadvertently introduced a bug in which it disallowed shifting scalar operands by vector operands when not compiling for OpenCL. This commit fixes it. Patch by Vladimir Yakovlev. Differential Revision: https://reviews.llvm.org/D24467 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@281669 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 133fae6 commit 38edf4b

File tree

3 files changed

+233
-5
lines changed

3 files changed

+233
-5
lines changed

lib/Sema/SemaExpr.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8722,7 +8722,8 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
87228722
static QualType checkVectorShift(Sema &S, ExprResult &LHS, ExprResult &RHS,
87238723
SourceLocation Loc, bool IsCompAssign) {
87248724
// OpenCL v1.1 s6.3.j says RHS can be a vector only if LHS is a vector.
8725-
if (!LHS.get()->getType()->isVectorType()) {
8725+
if ((S.LangOpts.OpenCL || S.LangOpts.ZVector) &&
8726+
!LHS.get()->getType()->isVectorType()) {
87268727
S.Diag(Loc, diag::err_shift_rhs_only_vector)
87278728
<< RHS.get()->getType() << LHS.get()->getType()
87288729
<< LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
@@ -8738,15 +8739,17 @@ static QualType checkVectorShift(Sema &S, ExprResult &LHS, ExprResult &RHS,
87388739
if (RHS.isInvalid()) return QualType();
87398740

87408741
QualType LHSType = LHS.get()->getType();
8741-
const VectorType *LHSVecTy = LHSType->castAs<VectorType>();
8742-
QualType LHSEleType = LHSVecTy->getElementType();
8742+
// Note that LHS might be a scalar because the routine calls not only in
8743+
// OpenCL case.
8744+
const VectorType *LHSVecTy = LHSType->getAs<VectorType>();
8745+
QualType LHSEleType = LHSVecTy ? LHSVecTy->getElementType() : LHSType;
87438746

87448747
// Note that RHS might not be a vector.
87458748
QualType RHSType = RHS.get()->getType();
87468749
const VectorType *RHSVecTy = RHSType->getAs<VectorType>();
87478750
QualType RHSEleType = RHSVecTy ? RHSVecTy->getElementType() : RHSType;
87488751

8749-
// OpenCL v1.1 s6.3.j says that the operands need to be integers.
8752+
// The operands need to be integers.
87508753
if (!LHSEleType->isIntegerType()) {
87518754
S.Diag(Loc, diag::err_typecheck_expect_int)
87528755
<< LHS.get()->getType() << LHS.get()->getSourceRange();
@@ -8759,7 +8762,19 @@ static QualType checkVectorShift(Sema &S, ExprResult &LHS, ExprResult &RHS,
87598762
return QualType();
87608763
}
87618764

8762-
if (RHSVecTy) {
8765+
if (!LHSVecTy) {
8766+
assert(RHSVecTy);
8767+
if (IsCompAssign)
8768+
return RHSType;
8769+
if (LHSEleType != RHSEleType) {
8770+
LHS = S.ImpCastExprToType(LHS.get(),RHSEleType, CK_IntegralCast);
8771+
LHSEleType = RHSEleType;
8772+
}
8773+
QualType VecTy =
8774+
S.Context.getExtVectorType(LHSEleType, RHSVecTy->getNumElements());
8775+
LHS = S.ImpCastExprToType(LHS.get(), VecTy, CK_VectorSplat);
8776+
LHSType = VecTy;
8777+
} else if (RHSVecTy) {
87638778
// OpenCL v1.1 s6.3.j says that for vector types, the operators
87648779
// are applied component-wise. So if RHS is a vector, then ensure
87658780
// that the number of elements is the same as LHS...

test/CodeGen/vecshift.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s
2+
3+
typedef __attribute__((__ext_vector_type__(8))) char vector_char8;
4+
typedef __attribute__((__ext_vector_type__(8))) short vector_short8;
5+
typedef __attribute__((__ext_vector_type__(8))) int vector_int8;
6+
typedef __attribute__((__ext_vector_type__(8))) unsigned char vector_uchar8;
7+
typedef __attribute__((__ext_vector_type__(8))) unsigned short vector_ushort8;
8+
typedef __attribute__((__ext_vector_type__(8))) unsigned int vector_uint8;
9+
typedef __attribute__((__ext_vector_type__(4))) char vector_char4;
10+
typedef __attribute__((__ext_vector_type__(4))) short vector_short4;
11+
typedef __attribute__((__ext_vector_type__(4))) int vector_int4;
12+
typedef __attribute__((__ext_vector_type__(4))) unsigned char vector_uchar4;
13+
typedef __attribute__((__ext_vector_type__(4))) unsigned short vector_ushort4;
14+
typedef __attribute__((__ext_vector_type__(4))) unsigned int vector_uint4;
15+
16+
char c;
17+
short s;
18+
int i;
19+
unsigned char uc;
20+
unsigned short us;
21+
unsigned int ui;
22+
vector_char8 vc8;
23+
vector_short8 vs8;
24+
vector_int8 vi8;
25+
vector_uchar8 vuc8;
26+
vector_ushort8 vus8;
27+
vector_uint8 vui8;
28+
vector_char4 vc4;
29+
vector_short4 vs4;
30+
vector_int4 vi4;
31+
vector_uchar4 vuc4;
32+
vector_ushort4 vus4;
33+
vector_uint4 vui4;
34+
35+
void foo() {
36+
vc8 = 1 << vc8;
37+
// CHECK: [[t0:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}},
38+
// CHECK: shl <8 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>, [[t0]]
39+
vuc8 = 1 << vuc8;
40+
// CHECK: [[t1:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}},
41+
// CHECK: shl <8 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>, [[t1]]
42+
vi8 = 1 << vi8;
43+
// CHECK: [[t2:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}},
44+
// CHECK: shl <8 x i32> <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>, [[t2]]
45+
vui8 = 1 << vui8;
46+
// CHECK: [[t3:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}},
47+
// CHECK: shl <8 x i32> <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>, [[t3]]
48+
vs8 = 1 << vs8;
49+
// CHECK: [[t4:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}},
50+
// CHECK: shl <8 x i16> <i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1>, [[t4]]
51+
vus8 = 1 << vus8;
52+
// CHECK: [[t5:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}},
53+
// CHECK: shl <8 x i16> <i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1>, [[t5]]
54+
55+
vc8 = c << vc8;
56+
// CHECK: [[t6:%.+]] = load i8, i8* @c,
57+
// CHECK: [[splat_splatinsert:%.+]] = insertelement <8 x i8> undef, i8 [[t6]], i32 0
58+
// CHECK: [[splat_splat:%.+]] = shufflevector <8 x i8> [[splat_splatinsert]], <8 x i8> undef, <8 x i32> zeroinitializer
59+
// CHECK: [[t7:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}},
60+
// CHECK: shl <8 x i8> [[splat_splat]], [[t7]]
61+
vuc8 = i << vuc8;
62+
// CHECK: [[t8:%.+]] = load i32, i32* @i,
63+
// CHECK: [[tconv:%.+]] = trunc i32 [[t8]] to i8
64+
// CHECK: [[splat_splatinsert7:%.+]] = insertelement <8 x i8> undef, i8 [[tconv]], i32 0
65+
// CHECK: [[splat_splat8:%.+]] = shufflevector <8 x i8> [[splat_splatinsert7]], <8 x i8> undef, <8 x i32> zeroinitializer
66+
// CHECK: [[t9:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}},
67+
// CHECK: shl <8 x i8> [[splat_splat8]], [[t9]]
68+
vi8 = uc << vi8;
69+
// CHECK: [[t10:%.+]] = load i8, i8* @uc,
70+
// CHECK: [[conv10:%.+]] = zext i8 [[t10]] to i32
71+
// CHECK: [[splat_splatinsert11:%.+]] = insertelement <8 x i32> undef, i32 [[conv10]], i32 0
72+
// CHECK: [[splat_splat12:%.+]] = shufflevector <8 x i32> [[splat_splatinsert11]], <8 x i32> undef, <8 x i32> zeroinitializer
73+
// CHECK: [[t11:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}},
74+
// CHECK: shl <8 x i32> [[splat_splat12]], [[t11]]
75+
vui8 = us << vui8;
76+
// CHECK: [[t12:%.+]] = load i16, i16* @us,
77+
// CHECK: [[conv14:%.+]] = zext i16 [[t12]] to i32
78+
// CHECK: [[splat_splatinsert15:%.+]] = insertelement <8 x i32> undef, i32 [[conv14]], i32 0
79+
// CHECK: [[splat_splat16:%.+]] = shufflevector <8 x i32> [[splat_splatinsert15]], <8 x i32> undef, <8 x i32> zeroinitializer
80+
// CHECK: [[t13:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}},
81+
// CHECK: shl <8 x i32> [[splat_splat16]], [[t13]]
82+
vs8 = ui << vs8;
83+
// CHECK: [[t14:%.+]] = load i32, i32* @ui,
84+
// CHECK: [[conv18:%.+]] = trunc i32 [[t14]] to i16
85+
// CHECK: [[splat_splatinsert19:%.+]] = insertelement <8 x i16> undef, i16 [[conv18]], i32 0
86+
// CHECK: [[splat_splat20:%.+]] = shufflevector <8 x i16> [[splat_splatinsert19]], <8 x i16> undef, <8 x i32> zeroinitializer
87+
// CHECK: [[t15:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}},
88+
// CHECK: shl <8 x i16> [[splat_splat20]], [[t15]]
89+
vus8 = 1 << vus8;
90+
// CHECK: [[t16:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}},
91+
// CHECK: [[shl22:%.+]] = shl <8 x i16> <i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1, i16 1>, [[t16]]
92+
93+
vc8 = vc8 << vc8;
94+
// CHECK: [[t17:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}},
95+
// CHECK: [[t18:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}},
96+
// CHECK: shl <8 x i8> [[t17]], [[t18]]
97+
vi8 = vi8 << vuc8;
98+
// CHECK: [[t19:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}},
99+
// CHECK: [[t20:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}},
100+
// CHECK: [[shprom:%.+]] = zext <8 x i8> [[t20]] to <8 x i32>
101+
// CHECK: shl <8 x i32> [[t19]], [[shprom]]
102+
vuc8 = vuc8 << vi8;
103+
// CHECK: [[t21:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}},
104+
// CHECK: [[t22:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}},
105+
// CHECK: [[sh_prom25:%.+]] = trunc <8 x i32> [[t22]] to <8 x i8>
106+
// CHECK: shl <8 x i8> [[t21]], [[sh_prom25]]
107+
vus8 = vus8 << vui8;
108+
// CHECK: [[t23:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}},
109+
// CHECK: [[t24:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}},
110+
// CHECK: [[sh_prom27:%.+]] = trunc <8 x i32> [[t24]] to <8 x i16>
111+
// CHECK: shl <8 x i16> [[t23]], [[sh_prom27]]
112+
vui8 = vui8 << vs8;
113+
// CHECK: [[t25:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}},
114+
// CHECK: [[t26:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}},
115+
// CHECK: [[sh_prom29:%.+]] = zext <8 x i16> [[t26]] to <8 x i32>
116+
// CHECK: shl <8 x i32> [[t25]], [[sh_prom29]]
117+
118+
vui8 <<= s;
119+
// CHECK: [[t27:%.+]] = load i16, i16* @s,
120+
// CHECK: [[conv40:%.+]] = sext i16 [[t27]] to i32
121+
// CHECK: [[splat_splatinsert41:%.+]] = insertelement <8 x i32> undef, i32 [[conv40]], i32 0
122+
// CHECK: [[splat_splat42:%.+]] = shufflevector <8 x i32> [[splat_splatinsert41]], <8 x i32> undef, <8 x i32> zeroinitializer
123+
// CHECK: [[t28:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}},
124+
// CHECK: shl <8 x i32> [[t28]], [[splat_splat42]]
125+
vi8 <<= us;
126+
// CHECK: [[t29:%.+]] = load i16, i16* @us,
127+
// CHECK: [[conv44:%.+]] = zext i16 [[t29]] to i32
128+
// CHECK: [[splat_splatinsert45:%.+]] = insertelement <8 x i32> undef, i32 [[conv44]], i32 0
129+
// CHECK: [[splat_splat46:%.+]] = shufflevector <8 x i32> [[splat_splatinsert45]], <8 x i32> undef, <8 x i32> zeroinitializer
130+
// CHECK: [[t30:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}},
131+
// CHECK: shl <8 x i32> [[t30]], [[splat_splat46]]
132+
vus8 <<= i;
133+
// CHECK: [[t31:%.+]] = load i32, i32* @i,
134+
// CHECK: [[splat_splatinsert48:%.+]] = insertelement <8 x i32> undef, i32 [[t31]], i32 0
135+
// CHECK: [[splat_splat49:%.+]] = shufflevector <8 x i32> [[splat_splatinsert48]], <8 x i32> undef, <8 x i32> zeroinitializer
136+
// CHECK: [[t32:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}},
137+
// CHECK: [[sh_prom50:%.+]] = trunc <8 x i32> [[splat_splat49]] to <8 x i16>
138+
// CHECK: shl <8 x i16> [[t32]], [[sh_prom50]]
139+
vs8 <<= ui;
140+
// CHECK: [[t33:%.+]] = load i32, i32* @ui,
141+
// CHECK: [[splat_splatinsert52:%.+]] = insertelement <8 x i32> undef, i32 [[t33]], i32 0
142+
// CHECK: [[splat_splat53:%.+]] = shufflevector <8 x i32> [[splat_splatinsert52]], <8 x i32> undef, <8 x i32> zeroinitializer
143+
// CHECK: [[t34:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}},
144+
// CHECK: [[sh_prom54:%.+]] = trunc <8 x i32> [[splat_splat53]] to <8 x i16>
145+
// CHECK: shl <8 x i16> [[t34]], [[sh_prom54]]
146+
}

test/Sema/vecshift.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
3+
typedef __attribute__((__ext_vector_type__(8))) char vector_char8;
4+
typedef __attribute__((__ext_vector_type__(8))) short vector_short8;
5+
typedef __attribute__((__ext_vector_type__(8))) int vector_int8;
6+
typedef __attribute__((__ext_vector_type__(8))) unsigned char vector_uchar8;
7+
typedef __attribute__((__ext_vector_type__(8))) unsigned short vector_ushort8;
8+
typedef __attribute__((__ext_vector_type__(8))) unsigned int vector_uint8;
9+
typedef __attribute__((__ext_vector_type__(4))) char vector_char4;
10+
typedef __attribute__((__ext_vector_type__(4))) short vector_short4;
11+
typedef __attribute__((__ext_vector_type__(4))) int vector_int4;
12+
typedef __attribute__((__ext_vector_type__(4))) unsigned char vector_uchar4;
13+
typedef __attribute__((__ext_vector_type__(4))) unsigned short vector_ushort4;
14+
typedef __attribute__((__ext_vector_type__(4))) unsigned int vector_uint4;
15+
16+
char c;
17+
short s;
18+
int i;
19+
unsigned char uc;
20+
unsigned short us;
21+
unsigned int ui;
22+
vector_char8 vc8;
23+
vector_short8 vs8;
24+
vector_int8 vi8;
25+
vector_uchar8 vuc8;
26+
vector_ushort8 vus8;
27+
vector_uint8 vui8;
28+
vector_char4 vc4;
29+
vector_short4 vs4;
30+
vector_int4 vi4;
31+
vector_uchar4 vuc4;
32+
vector_ushort4 vus4;
33+
vector_uint4 vui4;
34+
35+
void foo() {
36+
vc8 = 1 << vc8;
37+
vuc8 = 1 << vuc8;
38+
vi8 = 1 << vi8;
39+
vui8 = 1 << vui8;
40+
vs8 = 1 << vs8;
41+
vus8 = 1 << vus8;
42+
43+
vc8 = c << vc8;
44+
vuc8 = i << vuc8;
45+
vi8 = uc << vi8;
46+
vui8 = us << vui8;
47+
vs8 = ui << vs8;
48+
vus8 = 1 << vus8;
49+
50+
vc8 = vc8 << vc8;
51+
vi8 = vi8 << vuc8;
52+
vuc8 = vuc8 << vi8;
53+
vus8 = vus8 << vui8;
54+
vui8 = vui8 << vs8;
55+
56+
vc8 <<= vc8;
57+
vi8 <<= vuc8;
58+
vuc8 <<= vi8;
59+
vus8 <<= vui8;
60+
vui8 <<= vs8;
61+
62+
c <<= vc8; // expected-error {{assigning to 'char' from incompatible type}}
63+
i <<= vuc8; // expected-error {{assigning to 'int' from incompatible type}}
64+
uc <<= vi8; // expected-error {{assigning to 'unsigned char' from incompatible type}}
65+
us <<= vui8; // expected-error {{assigning to 'unsigned short' from incompatible type}}
66+
ui <<= vs8; // expected-error {{assigning to 'unsigned int' from incompatible type}}
67+
}

0 commit comments

Comments
 (0)