From 819867191f95b7e83cbd42cd1c6a6a5d36c71bd4 Mon Sep 17 00:00:00 2001 From: Akira Hatanaka Date: Thu, 15 Sep 2016 22:19:25 +0000 Subject: [PATCH] [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 llvm-svn: 281669 --- clang/lib/Sema/SemaExpr.cpp | 25 ++++++-- clang/test/CodeGen/vecshift.c | 146 ++++++++++++++++++++++++++++++++++++++++++ clang/test/Sema/vecshift.c | 67 +++++++++++++++++++ 3 files changed, 233 insertions(+), 5 deletions(-) create mode 100644 clang/test/CodeGen/vecshift.c create mode 100644 clang/test/Sema/vecshift.c diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 31db29a..72a3586 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -8722,7 +8722,8 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, static QualType checkVectorShift(Sema &S, ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { // OpenCL v1.1 s6.3.j says RHS can be a vector only if LHS is a vector. - if (!LHS.get()->getType()->isVectorType()) { + if ((S.LangOpts.OpenCL || S.LangOpts.ZVector) && + !LHS.get()->getType()->isVectorType()) { S.Diag(Loc, diag::err_shift_rhs_only_vector) << RHS.get()->getType() << LHS.get()->getType() << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); @@ -8738,15 +8739,17 @@ static QualType checkVectorShift(Sema &S, ExprResult &LHS, ExprResult &RHS, if (RHS.isInvalid()) return QualType(); QualType LHSType = LHS.get()->getType(); - const VectorType *LHSVecTy = LHSType->castAs(); - QualType LHSEleType = LHSVecTy->getElementType(); + // Note that LHS might be a scalar because the routine calls not only in + // OpenCL case. + const VectorType *LHSVecTy = LHSType->getAs(); + QualType LHSEleType = LHSVecTy ? LHSVecTy->getElementType() : LHSType; // Note that RHS might not be a vector. QualType RHSType = RHS.get()->getType(); const VectorType *RHSVecTy = RHSType->getAs(); QualType RHSEleType = RHSVecTy ? RHSVecTy->getElementType() : RHSType; - // OpenCL v1.1 s6.3.j says that the operands need to be integers. + // The operands need to be integers. if (!LHSEleType->isIntegerType()) { S.Diag(Loc, diag::err_typecheck_expect_int) << LHS.get()->getType() << LHS.get()->getSourceRange(); @@ -8759,7 +8762,19 @@ static QualType checkVectorShift(Sema &S, ExprResult &LHS, ExprResult &RHS, return QualType(); } - if (RHSVecTy) { + if (!LHSVecTy) { + assert(RHSVecTy); + if (IsCompAssign) + return RHSType; + if (LHSEleType != RHSEleType) { + LHS = S.ImpCastExprToType(LHS.get(),RHSEleType, CK_IntegralCast); + LHSEleType = RHSEleType; + } + QualType VecTy = + S.Context.getExtVectorType(LHSEleType, RHSVecTy->getNumElements()); + LHS = S.ImpCastExprToType(LHS.get(), VecTy, CK_VectorSplat); + LHSType = VecTy; + } else if (RHSVecTy) { // OpenCL v1.1 s6.3.j says that for vector types, the operators // are applied component-wise. So if RHS is a vector, then ensure // that the number of elements is the same as LHS... diff --git a/clang/test/CodeGen/vecshift.c b/clang/test/CodeGen/vecshift.c new file mode 100644 index 0000000..67ab4a5 --- /dev/null +++ b/clang/test/CodeGen/vecshift.c @@ -0,0 +1,146 @@ +// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s + +typedef __attribute__((__ext_vector_type__(8))) char vector_char8; +typedef __attribute__((__ext_vector_type__(8))) short vector_short8; +typedef __attribute__((__ext_vector_type__(8))) int vector_int8; +typedef __attribute__((__ext_vector_type__(8))) unsigned char vector_uchar8; +typedef __attribute__((__ext_vector_type__(8))) unsigned short vector_ushort8; +typedef __attribute__((__ext_vector_type__(8))) unsigned int vector_uint8; +typedef __attribute__((__ext_vector_type__(4))) char vector_char4; +typedef __attribute__((__ext_vector_type__(4))) short vector_short4; +typedef __attribute__((__ext_vector_type__(4))) int vector_int4; +typedef __attribute__((__ext_vector_type__(4))) unsigned char vector_uchar4; +typedef __attribute__((__ext_vector_type__(4))) unsigned short vector_ushort4; +typedef __attribute__((__ext_vector_type__(4))) unsigned int vector_uint4; + +char c; +short s; +int i; +unsigned char uc; +unsigned short us; +unsigned int ui; +vector_char8 vc8; +vector_short8 vs8; +vector_int8 vi8; +vector_uchar8 vuc8; +vector_ushort8 vus8; +vector_uint8 vui8; +vector_char4 vc4; +vector_short4 vs4; +vector_int4 vi4; +vector_uchar4 vuc4; +vector_ushort4 vus4; +vector_uint4 vui4; + +void foo() { + vc8 = 1 << vc8; +// CHECK: [[t0:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}}, +// CHECK: shl <8 x i8> , [[t0]] + vuc8 = 1 << vuc8; +// CHECK: [[t1:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}}, +// CHECK: shl <8 x i8> , [[t1]] + vi8 = 1 << vi8; +// CHECK: [[t2:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}}, +// CHECK: shl <8 x i32> , [[t2]] + vui8 = 1 << vui8; +// CHECK: [[t3:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}}, +// CHECK: shl <8 x i32> , [[t3]] + vs8 = 1 << vs8; +// CHECK: [[t4:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}}, +// CHECK: shl <8 x i16> , [[t4]] + vus8 = 1 << vus8; +// CHECK: [[t5:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}}, +// CHECK: shl <8 x i16> , [[t5]] + + vc8 = c << vc8; +// CHECK: [[t6:%.+]] = load i8, i8* @c, +// CHECK: [[splat_splatinsert:%.+]] = insertelement <8 x i8> undef, i8 [[t6]], i32 0 +// CHECK: [[splat_splat:%.+]] = shufflevector <8 x i8> [[splat_splatinsert]], <8 x i8> undef, <8 x i32> zeroinitializer +// CHECK: [[t7:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}}, +// CHECK: shl <8 x i8> [[splat_splat]], [[t7]] + vuc8 = i << vuc8; +// CHECK: [[t8:%.+]] = load i32, i32* @i, +// CHECK: [[tconv:%.+]] = trunc i32 [[t8]] to i8 +// CHECK: [[splat_splatinsert7:%.+]] = insertelement <8 x i8> undef, i8 [[tconv]], i32 0 +// CHECK: [[splat_splat8:%.+]] = shufflevector <8 x i8> [[splat_splatinsert7]], <8 x i8> undef, <8 x i32> zeroinitializer +// CHECK: [[t9:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}}, +// CHECK: shl <8 x i8> [[splat_splat8]], [[t9]] + vi8 = uc << vi8; +// CHECK: [[t10:%.+]] = load i8, i8* @uc, +// CHECK: [[conv10:%.+]] = zext i8 [[t10]] to i32 +// CHECK: [[splat_splatinsert11:%.+]] = insertelement <8 x i32> undef, i32 [[conv10]], i32 0 +// CHECK: [[splat_splat12:%.+]] = shufflevector <8 x i32> [[splat_splatinsert11]], <8 x i32> undef, <8 x i32> zeroinitializer +// CHECK: [[t11:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}}, +// CHECK: shl <8 x i32> [[splat_splat12]], [[t11]] + vui8 = us << vui8; +// CHECK: [[t12:%.+]] = load i16, i16* @us, +// CHECK: [[conv14:%.+]] = zext i16 [[t12]] to i32 +// CHECK: [[splat_splatinsert15:%.+]] = insertelement <8 x i32> undef, i32 [[conv14]], i32 0 +// CHECK: [[splat_splat16:%.+]] = shufflevector <8 x i32> [[splat_splatinsert15]], <8 x i32> undef, <8 x i32> zeroinitializer +// CHECK: [[t13:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}}, +// CHECK: shl <8 x i32> [[splat_splat16]], [[t13]] + vs8 = ui << vs8; +// CHECK: [[t14:%.+]] = load i32, i32* @ui, +// CHECK: [[conv18:%.+]] = trunc i32 [[t14]] to i16 +// CHECK: [[splat_splatinsert19:%.+]] = insertelement <8 x i16> undef, i16 [[conv18]], i32 0 +// CHECK: [[splat_splat20:%.+]] = shufflevector <8 x i16> [[splat_splatinsert19]], <8 x i16> undef, <8 x i32> zeroinitializer +// CHECK: [[t15:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}}, +// CHECK: shl <8 x i16> [[splat_splat20]], [[t15]] + vus8 = 1 << vus8; +// CHECK: [[t16:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}}, +// CHECK: [[shl22:%.+]] = shl <8 x i16> , [[t16]] + + vc8 = vc8 << vc8; +// CHECK: [[t17:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}}, +// CHECK: [[t18:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}}, +// CHECK: shl <8 x i8> [[t17]], [[t18]] + vi8 = vi8 << vuc8; +// CHECK: [[t19:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}}, +// CHECK: [[t20:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}}, +// CHECK: [[shprom:%.+]] = zext <8 x i8> [[t20]] to <8 x i32> +// CHECK: shl <8 x i32> [[t19]], [[shprom]] + vuc8 = vuc8 << vi8; +// CHECK: [[t21:%.+]] = load <8 x i8>, <8 x i8>* {{@.+}}, +// CHECK: [[t22:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}}, +// CHECK: [[sh_prom25:%.+]] = trunc <8 x i32> [[t22]] to <8 x i8> +// CHECK: shl <8 x i8> [[t21]], [[sh_prom25]] + vus8 = vus8 << vui8; +// CHECK: [[t23:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}}, +// CHECK: [[t24:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}}, +// CHECK: [[sh_prom27:%.+]] = trunc <8 x i32> [[t24]] to <8 x i16> +// CHECK: shl <8 x i16> [[t23]], [[sh_prom27]] + vui8 = vui8 << vs8; +// CHECK: [[t25:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}}, +// CHECK: [[t26:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}}, +// CHECK: [[sh_prom29:%.+]] = zext <8 x i16> [[t26]] to <8 x i32> +// CHECK: shl <8 x i32> [[t25]], [[sh_prom29]] + + vui8 <<= s; +// CHECK: [[t27:%.+]] = load i16, i16* @s, +// CHECK: [[conv40:%.+]] = sext i16 [[t27]] to i32 +// CHECK: [[splat_splatinsert41:%.+]] = insertelement <8 x i32> undef, i32 [[conv40]], i32 0 +// CHECK: [[splat_splat42:%.+]] = shufflevector <8 x i32> [[splat_splatinsert41]], <8 x i32> undef, <8 x i32> zeroinitializer +// CHECK: [[t28:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}}, +// CHECK: shl <8 x i32> [[t28]], [[splat_splat42]] + vi8 <<= us; +// CHECK: [[t29:%.+]] = load i16, i16* @us, +// CHECK: [[conv44:%.+]] = zext i16 [[t29]] to i32 +// CHECK: [[splat_splatinsert45:%.+]] = insertelement <8 x i32> undef, i32 [[conv44]], i32 0 +// CHECK: [[splat_splat46:%.+]] = shufflevector <8 x i32> [[splat_splatinsert45]], <8 x i32> undef, <8 x i32> zeroinitializer +// CHECK: [[t30:%.+]] = load <8 x i32>, <8 x i32>* {{@.+}}, +// CHECK: shl <8 x i32> [[t30]], [[splat_splat46]] + vus8 <<= i; +// CHECK: [[t31:%.+]] = load i32, i32* @i, +// CHECK: [[splat_splatinsert48:%.+]] = insertelement <8 x i32> undef, i32 [[t31]], i32 0 +// CHECK: [[splat_splat49:%.+]] = shufflevector <8 x i32> [[splat_splatinsert48]], <8 x i32> undef, <8 x i32> zeroinitializer +// CHECK: [[t32:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}}, +// CHECK: [[sh_prom50:%.+]] = trunc <8 x i32> [[splat_splat49]] to <8 x i16> +// CHECK: shl <8 x i16> [[t32]], [[sh_prom50]] + vs8 <<= ui; +// CHECK: [[t33:%.+]] = load i32, i32* @ui, +// CHECK: [[splat_splatinsert52:%.+]] = insertelement <8 x i32> undef, i32 [[t33]], i32 0 +// CHECK: [[splat_splat53:%.+]] = shufflevector <8 x i32> [[splat_splatinsert52]], <8 x i32> undef, <8 x i32> zeroinitializer +// CHECK: [[t34:%.+]] = load <8 x i16>, <8 x i16>* {{@.+}}, +// CHECK: [[sh_prom54:%.+]] = trunc <8 x i32> [[splat_splat53]] to <8 x i16> +// CHECK: shl <8 x i16> [[t34]], [[sh_prom54]] +} diff --git a/clang/test/Sema/vecshift.c b/clang/test/Sema/vecshift.c new file mode 100644 index 0000000..59a599f --- /dev/null +++ b/clang/test/Sema/vecshift.c @@ -0,0 +1,67 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +typedef __attribute__((__ext_vector_type__(8))) char vector_char8; +typedef __attribute__((__ext_vector_type__(8))) short vector_short8; +typedef __attribute__((__ext_vector_type__(8))) int vector_int8; +typedef __attribute__((__ext_vector_type__(8))) unsigned char vector_uchar8; +typedef __attribute__((__ext_vector_type__(8))) unsigned short vector_ushort8; +typedef __attribute__((__ext_vector_type__(8))) unsigned int vector_uint8; +typedef __attribute__((__ext_vector_type__(4))) char vector_char4; +typedef __attribute__((__ext_vector_type__(4))) short vector_short4; +typedef __attribute__((__ext_vector_type__(4))) int vector_int4; +typedef __attribute__((__ext_vector_type__(4))) unsigned char vector_uchar4; +typedef __attribute__((__ext_vector_type__(4))) unsigned short vector_ushort4; +typedef __attribute__((__ext_vector_type__(4))) unsigned int vector_uint4; + +char c; +short s; +int i; +unsigned char uc; +unsigned short us; +unsigned int ui; +vector_char8 vc8; +vector_short8 vs8; +vector_int8 vi8; +vector_uchar8 vuc8; +vector_ushort8 vus8; +vector_uint8 vui8; +vector_char4 vc4; +vector_short4 vs4; +vector_int4 vi4; +vector_uchar4 vuc4; +vector_ushort4 vus4; +vector_uint4 vui4; + +void foo() { + vc8 = 1 << vc8; + vuc8 = 1 << vuc8; + vi8 = 1 << vi8; + vui8 = 1 << vui8; + vs8 = 1 << vs8; + vus8 = 1 << vus8; + + vc8 = c << vc8; + vuc8 = i << vuc8; + vi8 = uc << vi8; + vui8 = us << vui8; + vs8 = ui << vs8; + vus8 = 1 << vus8; + + vc8 = vc8 << vc8; + vi8 = vi8 << vuc8; + vuc8 = vuc8 << vi8; + vus8 = vus8 << vui8; + vui8 = vui8 << vs8; + + vc8 <<= vc8; + vi8 <<= vuc8; + vuc8 <<= vi8; + vus8 <<= vui8; + vui8 <<= vs8; + + c <<= vc8; // expected-error {{assigning to 'char' from incompatible type}} + i <<= vuc8; // expected-error {{assigning to 'int' from incompatible type}} + uc <<= vi8; // expected-error {{assigning to 'unsigned char' from incompatible type}} + us <<= vui8; // expected-error {{assigning to 'unsigned short' from incompatible type}} + ui <<= vs8; // expected-error {{assigning to 'unsigned int' from incompatible type}} +} -- 2.7.4