Skip to content

Commit 4da744a

Browse files
committed
[OpenCL] Fix as_type3 invalid store creation
With -fpreserve-vec3-type enabled, a cast was not created when converting from a non-vec3 type to a vec3 type, even though a conversion to vec3 was performed. This resulted in creation of invalid store instructions. Differential Revision: https://reviews.llvm.org/D108470
1 parent 60527ce commit 4da744a

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4804,12 +4804,10 @@ Value *ScalarExprEmitter::VisitAsTypeExpr(AsTypeExpr *E) {
48044804
// to vec4 if the original type is not vec4, then a shuffle vector to
48054805
// get a vec3.
48064806
if (NumElementsSrc != 3 && NumElementsDst == 3) {
4807-
if (!CGF.CGM.getCodeGenOpts().PreserveVec3Type) {
4808-
auto *Vec4Ty = llvm::FixedVectorType::get(
4809-
cast<llvm::VectorType>(DstTy)->getElementType(), 4);
4810-
Src = createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(), Src,
4811-
Vec4Ty);
4812-
}
4807+
auto *Vec4Ty = llvm::FixedVectorType::get(
4808+
cast<llvm::VectorType>(DstTy)->getElementType(), 4);
4809+
Src = createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(), Src,
4810+
Vec4Ty);
48134811

48144812
Src = ConvertVec3AndVec4(Builder, CGF, Src, 3);
48154813
Src->setName("astype");

clang/test/CodeGenOpenCL/preserve_vec3.cl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %clang_cc1 %s -emit-llvm -o - -triple spir-unknown-unknown -fpreserve-vec3-type | FileCheck %s
22

33
typedef char char3 __attribute__((ext_vector_type(3)));
4+
typedef char char8 __attribute__((ext_vector_type(8)));
45
typedef short short3 __attribute__((ext_vector_type(3)));
56
typedef double double2 __attribute__((ext_vector_type(2)));
67
typedef float float3 __attribute__((ext_vector_type(3)));
@@ -38,6 +39,15 @@ void kernel float3_to_double2(global float3 *a, global double2 *b) {
3839
*b = __builtin_astype(*a, double2);
3940
}
4041

42+
void kernel char8_to_short3(global short3 *a, global char8 *b) {
43+
// CHECK-LABEL: spir_kernel void @char8_to_short3
44+
// CHECK: %[[IN_BC:.*]] = bitcast <8 x i8> addrspace(1)* %b to <4 x i16> addrspace(1)*
45+
// CHECK: %[[LOAD_B:.*]] = load <4 x i16>, <4 x i16> addrspace(1)* %[[IN_BC]]
46+
// CHECK: %[[ASTYPE:.*]] = shufflevector <4 x i16> %[[LOAD_B]], <4 x i16> poison, <3 x i32> <i32 0, i32 1, i32 2>
47+
// CHECK: store <3 x i16> %[[ASTYPE]], <3 x i16> addrspace(1)* %a, align 8
48+
*a = __builtin_astype(*b, short3);
49+
}
50+
4151
void from_char3(char3 a, global int *out) {
4252
// CHECK-LABEL: void @from_char3
4353
// CHECK: %[[ASTYPE:.*]] = shufflevector <3 x i8> %a, <3 x i8> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
@@ -53,3 +63,19 @@ void from_short3(short3 a, global long *out) {
5363
// CHECK: store <4 x i16> %[[ASTYPE]], <4 x i16> addrspace(1)* %[[OUT_BC]]
5464
*out = __builtin_astype(a, long);
5565
}
66+
67+
void scalar_to_char3(int a, global char3 *out) {
68+
// CHECK-LABEL: void @scalar_to_char3
69+
// CHECK: %[[IN_BC:.*]] = bitcast i32 %a to <4 x i8>
70+
// CHECK: %[[ASTYPE:.*]] = shufflevector <4 x i8> %[[IN_BC]], <4 x i8> poison, <3 x i32> <i32 0, i32 1, i32 2>
71+
// CHECK: store <3 x i8> %[[ASTYPE]], <3 x i8> addrspace(1)* %out
72+
*out = __builtin_astype(a, char3);
73+
}
74+
75+
void scalar_to_short3(long a, global short3 *out) {
76+
// CHECK-LABEL: void @scalar_to_short3
77+
// CHECK: %[[IN_BC:.*]] = bitcast i64 %a to <4 x i16>
78+
// CHECK: %[[ASTYPE:.*]] = shufflevector <4 x i16> %[[IN_BC]], <4 x i16> poison, <3 x i32> <i32 0, i32 1, i32 2>
79+
// CHECK: store <3 x i16> %[[ASTYPE]], <3 x i16> addrspace(1)* %out
80+
*out = __builtin_astype(a, short3);
81+
}

0 commit comments

Comments
 (0)