Skip to content

Commit e330f6e

Browse files
committed
follow direct ASTAllocaAddressSpace
1 parent 957d5e9 commit e330f6e

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -993,21 +993,24 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
993993
if (BuiltinID != Builtin::BI__builtin_alloca_uninitialized)
994994
initializeAlloca(*this, AllocaAddr, Size, SuitableAlignmentInBytes);
995995

996+
// Bitcast the alloca to the expected type.
997+
auto resAddr = builder.createBitcast(
998+
AllocaAddr, builder.getVoidPtrTy(CGM.AllocaInt8PtrTy.getAddrSpace()));
999+
9961000
// An alloca will always return a pointer to the alloca (stack) address
9971001
// space. This address space need not be the same as the AST / Language
9981002
// default (e.g. in C / C++ auto vars are in the generic address space). At
9991003
// the AST level this is handled within CreateTempAlloca et al., but for the
10001004
// builtin / dynamic alloca we have to handle it here.
1001-
assert(!MissingFeatures::addressSpace());
10021005
LangAS AAS = getASTAllocaAddressSpace();
10031006
LangAS EAS = E->getType()->getPointeeType().getAddressSpace();
1004-
if (EAS != AAS) {
1005-
assert(false && "Non-default address space for alloca NYI");
1007+
if (AAS != EAS) {
1008+
mlir::Type Ty = CGM.getTypes().ConvertType(E->getType());
1009+
return RValue::get(
1010+
getTargetHooks().performAddrSpaceCast(*this, resAddr, AAS, EAS, Ty));
10061011
}
10071012

1008-
// Bitcast the alloca to the expected type.
1009-
return RValue::get(
1010-
builder.createBitcast(AllocaAddr, builder.getVoidPtrTy()));
1013+
return RValue::get(resAddr);
10111014
}
10121015

10131016
case Builtin::BI__sync_fetch_and_add:

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2945,7 +2945,10 @@ Address CIRGenFunction::CreateTempAlloca(mlir::Type Ty, CharUnits Align,
29452945
// in C++ the auto variables are in the default address space. Therefore
29462946
// cast alloca to the default address space when necessary.
29472947
if (getASTAllocaAddressSpace() != LangAS::Default) {
2948-
llvm_unreachable("Requires address space cast which is NYI");
2948+
auto DestAddrSpace = getContext().getTargetAddressSpace(LangAS::Default);
2949+
V = getTargetHooks().performAddrSpaceCast(
2950+
*this, V, getASTAllocaAddressSpace(), LangAS::Default,
2951+
builder.getPointerTo(Ty, DestAddrSpace), /*non-null*/ true);
29492952
}
29502953
return Address(V, Ty, Align);
29512954
}

clang/lib/CIR/CodeGen/CIRGenTypeCache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ struct CIRGenTypeCache {
128128
// alloca address space can be used for now only for comparison with the
129129
// default address space.
130130
assert(!MissingFeatures::addressSpace());
131-
assert(ASTAllocaAddressSpace == clang::LangAS::Default);
132131
return ASTAllocaAddressSpace;
133132
}
134133
};

clang/lib/CIR/CodeGen/TargetInfo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ class CommonSPIRTargetCIRGenInfo : public TargetCIRGenInfo {
257257
CommonSPIRTargetCIRGenInfo(std::unique_ptr<ABIInfo> ABIInfo)
258258
: TargetCIRGenInfo(std::move(ABIInfo)) {}
259259

260+
LangAS getASTAllocaAddressSpace() const override {
261+
return getLangASFromTargetAS(
262+
getABIInfo().CGT.getModule().getDataLayout().getAllocaMemorySpace());
263+
}
264+
260265
unsigned getOpenCLKernelCallingConv() const override {
261266
return llvm::CallingConv::SPIR_KERNEL;
262267
}

clang/test/CIR/CodeGen/OpenCL/addrspace-alloca.cl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// RUN: %clang_cc1 -cl-std=CL3.0 -O0 -fclangir -S -emit-llvm -triple spirv64-unknown-unknown %s -o %t.ll
44
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM
55

6-
// CIR: cir.func @func(%arg0: !cir.ptr<!s32i, addrspace(3)>
7-
// LLVM: @func(ptr addrspace(3)
8-
kernel void func(local int *p) {
6+
// CIR: cir.func @foo(%arg0: !cir.ptr<!s32i, addrspace(3)>
7+
// LLVM: @foo(ptr addrspace(3)
8+
kernel void foo(local int *p) {
99
// CIR-NEXT: %[[#ALLOCA_P:]] = cir.alloca !cir.ptr<!s32i, addrspace(3)>, !cir.ptr<!cir.ptr<!s32i, addrspace(3)>>, ["p", init] {alignment = 8 : i64}
1010
// LLVM-NEXT: %[[#ALLOCA_P:]] = alloca ptr addrspace(3), i64 1, align 8
1111

@@ -23,3 +23,18 @@ kernel void func(local int *p) {
2323

2424
return;
2525
}
26+
27+
// CIR: cir.func @bar(
28+
// LLVM: @bar(
29+
kernel void bar() {
30+
void * arr = __builtin_alloca(2 * sizeof(int));
31+
// CIR: %[[#ALLOCA:]] = cir.alloca !u8i, !cir.ptr<!u8i>, %{{[0-9]+}} : !u64i, ["bi_alloca"] {alignment = 8 : i64}
32+
// CIR-NEXT: %[[#PRIVATE_VOID_PTR:]] = cir.cast(bitcast, %[[#ALLOCA]] : !cir.ptr<!u8i>), !cir.ptr<!void>
33+
// CIR-NEXT: %[[#GENERIC_VOID_PTR:]] = cir.cast(address_space, %[[#PRIVATE_VOID_PTR:]] : !cir.ptr<!void>), !cir.ptr<!void, addrspace(4)>
34+
// CIR-NEXT: cir.store %[[#GENERIC_VOID_PTR]], %{{[0-9]+}} : !cir.ptr<!void, addrspace(4)>, !cir.ptr<!cir.ptr<!void, addrspace(4)>>
35+
36+
// LLVM: %[[#ALLOCA:]] = alloca i8, i64 8, align 8
37+
// LLVM-NEXT: %[[#GENERIC_VOID_PTR:]] = addrspacecast ptr %[[#ALLOCA]] to ptr addrspace(4)
38+
// LLVM-NEXT: store ptr addrspace(4) %[[#GENERIC_VOID_PTR]], ptr %{{[0-9]+}}, align 8
39+
40+
}

0 commit comments

Comments
 (0)