Skip to content

Commit 5512425

Browse files
[SYCL] Fix addressspace cast for _alloca (#6087)
This is a follow-up to PR#6050. The cast should use element type and not change type. PR#6050 incorrectly change Type* to Type** (when adding the cast) This patch also add opaque pointer version of the test. Signed-off-by: Elizabeth Andrews <[email protected]>
1 parent 082929a commit 5512425

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,10 +2058,17 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
20582058
Value *Src = Visit(const_cast<Expr*>(E));
20592059
llvm::Type *SrcTy = Src->getType();
20602060
llvm::Type *DstTy = ConvertType(DestTy);
2061-
if (SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() &&
2061+
2062+
if (SrcTy->isPointerTy() && DstTy->isPointerTy() &&
20622063
SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
20632064
Src = Builder.CreateAddrSpaceCast(
2064-
Src, llvm::PointerType::get(SrcTy, DstTy->getPointerAddressSpace()));
2065+
Src,
2066+
llvm::PointerType::getWithSamePointeeType(
2067+
cast<llvm::PointerType>(SrcTy), DstTy->getPointerAddressSpace()));
2068+
else if (SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() &&
2069+
SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2070+
llvm_unreachable("wrong cast for pointers in different address spaces"
2071+
"(must be an address space cast)!");
20652072

20662073
if (CGF.SanOpts.has(SanitizerKind::CFIUnrelatedCast)) {
20672074
if (auto *PT = DestTy->getAs<PointerType>()) {
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
// RUN: %clang_cc1 -triple spir64-unknown-linux -fsycl-is-device -disable-llvm-passes -emit-llvm -x c++ %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple spir64-unknown-linux -fsycl-is-device -disable-llvm-passes -opaque-pointers -emit-llvm -x c++ %s -o - | FileCheck %s
22

33
// Test to verify that address space cast is generated correctly for __builtin_alloca
44

55
__attribute__((sycl_device)) void foo() {
6-
// CHECK: %TestVar = alloca i32 addrspace(4)*, align 8
7-
// CHECK: %TestVar.ascast = addrspacecast i32 addrspace(4)** %TestVar to i32 addrspace(4)* addrspace(4)*
6+
// CHECK: %TestVar = alloca ptr addrspace(4), align 8
7+
// CHECK: %TestVar.ascast = addrspacecast ptr %TestVar to ptr addrspace(4)
88
// CHECK: %[[ALLOCA:[0-9]+]] = alloca i8, i64 1, align 8
9-
// CHECK: %[[ADDRSPCAST:[0-9]+]] = addrspacecast i8* %[[ALLOCA]] to i8* addrspace(4)*
10-
// CHECK: %[[BITCAST:[0-9]+]] = bitcast i8* addrspace(4)* %[[ADDRSPCAST]] to i32 addrspace(4)*
11-
// CHECK: store i32 addrspace(4)* %[[BITCAST]], i32 addrspace(4)* addrspace(4)* %TestVar.ascast, align 8
9+
// CHECK: %[[ADDRSPCAST:[0-9]+]] = addrspacecast ptr %[[ALLOCA]] to ptr addrspace(4)
10+
// CHECK: store ptr addrspace(4) %[[ADDRSPCAST]], ptr addrspace(4) %TestVar.ascast, align 8
1211
int *TestVar = (int *)__builtin_alloca(1);
1312
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -triple spir64-unknown-linux -fsycl-is-device -disable-llvm-passes -no-opaque-pointers -emit-llvm -x c++ %s -o - | FileCheck %s
2+
3+
// Test to verify that address space cast is generated correctly for __builtin_alloca
4+
5+
__attribute__((sycl_device)) void foo() {
6+
// CHECK: %TestVar = alloca i32 addrspace(4)*, align 8
7+
// CHECK: %TestVar.ascast = addrspacecast i32 addrspace(4)** %TestVar to i32 addrspace(4)* addrspace(4)*
8+
// CHECK: %[[ALLOCA:[0-9]+]] = alloca i8, i64 1, align 8
9+
// CHECK: %[[ADDRSPCAST:[0-9]+]] = addrspacecast i8* %[[ALLOCA]] to i8 addrspace(4)*
10+
// CHECK: %[[BITCAST:[0-9]+]] = bitcast i8 addrspace(4)* %[[ADDRSPCAST]] to i32 addrspace(4)*
11+
// CHECK: store i32 addrspace(4)* %[[BITCAST]], i32 addrspace(4)* addrspace(4)* %TestVar.ascast, align 8
12+
int *TestVar = (int *)__builtin_alloca(1);
13+
}

0 commit comments

Comments
 (0)