Skip to content

Commit e688fa5

Browse files
[SYCL] Cast to correct address space for _alloca (#6050)
Alloca instruction for _alloca is generated in AllocaAddrSpace. Addspace cast is required since pointers have address space 4 in SYCL. Signed-off-by: Elizabeth Andrews <[email protected]>
1 parent f33a719 commit e688fa5

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,10 +2057,9 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
20572057
llvm::Type *SrcTy = Src->getType();
20582058
llvm::Type *DstTy = ConvertType(DestTy);
20592059
if (SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() &&
2060-
SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) {
2061-
llvm_unreachable("wrong cast for pointers in different address spaces"
2062-
"(must be an address space cast)!");
2063-
}
2060+
SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2061+
Src = Builder.CreateAddrSpaceCast(
2062+
Src, llvm::PointerType::get(SrcTy, DstTy->getPointerAddressSpace()));
20642063

20652064
if (CGF.SanOpts.has(SanitizerKind::CFIUnrelatedCast)) {
20662065
if (auto *PT = DestTy->getAs<PointerType>()) {
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 -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)