Skip to content

Commit 51a014c

Browse files
committed
[Clang][CodeGen] __builtin_allocas should care about address spaces
`alloca` instructions always return pointers to the `alloca` address space. This composes poorly with most HLLs which are address space agnostic and thus have all pointers point to generic/default. Static `alloca`s were already handled on the AST level, however dynamic `alloca`s were not, which would lead to subtly incorrect IR. This patch addresses that by inserting an address space cast iff the `alloca` address space is different from the default / expected. Reviewed By: rjmccall, arsenm Differential Revision: https://reviews.llvm.org/D156539
1 parent 400da11 commit 51a014c

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3517,6 +3517,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
35173517
return RValue::get(Result);
35183518
}
35193519

3520+
// An alloca will always return a pointer to the alloca (stack) address
3521+
// space. This address space need not be the same as the AST / Language
3522+
// default (e.g. in C / C++ auto vars are in the generic address space). At
3523+
// the AST level this is handled within CreateTempAlloca et al., but for the
3524+
// builtin / dynamic alloca we have to handle it here. We use an explicit cast
3525+
// instead of passing an AS to CreateAlloca so as to not inhibit optimisation.
35203526
case Builtin::BIalloca:
35213527
case Builtin::BI_alloca:
35223528
case Builtin::BI__builtin_alloca_uninitialized:
@@ -3532,6 +3538,13 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
35323538
AI->setAlignment(SuitableAlignmentInBytes);
35333539
if (BuiltinID != Builtin::BI__builtin_alloca_uninitialized)
35343540
initializeAlloca(*this, AI, Size, SuitableAlignmentInBytes);
3541+
LangAS AAS = getASTAllocaAddressSpace();
3542+
LangAS EAS = E->getType()->getPointeeType().getAddressSpace();
3543+
if (AAS != EAS) {
3544+
llvm::Type *Ty = CGM.getTypes().ConvertType(E->getType());
3545+
return RValue::get(getTargetHooks().performAddrSpaceCast(*this, AI, AAS,
3546+
EAS, Ty));
3547+
}
35353548
return RValue::get(AI);
35363549
}
35373550

@@ -3547,6 +3560,13 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
35473560
AI->setAlignment(AlignmentInBytes);
35483561
if (BuiltinID != Builtin::BI__builtin_alloca_with_align_uninitialized)
35493562
initializeAlloca(*this, AI, Size, AlignmentInBytes);
3563+
LangAS AAS = getASTAllocaAddressSpace();
3564+
LangAS EAS = E->getType()->getPointeeType().getAddressSpace();
3565+
if (AAS != EAS) {
3566+
llvm::Type *Ty = CGM.getTypes().ConvertType(E->getType());
3567+
return RValue::get(getTargetHooks().performAddrSpaceCast(*this, AI, AAS,
3568+
EAS, Ty));
3569+
}
35503570
return RValue::get(AI);
35513571
}
35523572

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -emit-llvm %s -o - \
2+
// RUN: | FileCheck %s --check-prefix=CHECK
3+
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -DOCL12 -x cl -std=cl1.2 \
4+
// RUN: -emit-llvm %s -o - | FileCheck %s --check-prefix=CHECK-CL12
5+
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -x cl -std=cl2.0 \
6+
// RUN: -emit-llvm %s -o - | FileCheck %s --check-prefix=CHECK-CL20
7+
8+
#if defined(OCL12)
9+
#define CAST (char *)(unsigned long)
10+
#else
11+
#define CAST (char *)
12+
#endif
13+
14+
void allocas(unsigned long n) {
15+
char *a = CAST __builtin_alloca(n);
16+
char *uninitialized_a = CAST __builtin_alloca_uninitialized(n);
17+
char *aligned_a = CAST __builtin_alloca_with_align(n, 8);
18+
char *aligned_uninitialized_a = CAST __builtin_alloca_with_align_uninitialized(n, 8);
19+
}
20+
21+
// CHECK: @allocas(
22+
// CHECK: store i64 %n, ptr %n.addr.ascast, align 8
23+
// CHECK: %0 = load i64, ptr %n.addr.ascast, align 8
24+
// CHECK: %1 = alloca i8, i64 %0, align 8, addrspace(5)
25+
// CHECK: %2 = addrspacecast ptr addrspace(5) %1 to ptr
26+
// CHECK: store ptr %2, ptr %a.ascast, align 8
27+
// CHECK: %3 = load i64, ptr %n.addr.ascast, align 8
28+
// CHECK: %4 = alloca i8, i64 %3, align 8, addrspace(5)
29+
// CHECK: %5 = addrspacecast ptr addrspace(5) %4 to ptr
30+
// CHECK: store ptr %5, ptr %uninitialized_a.ascast, align 8
31+
// CHECK: %6 = load i64, ptr %n.addr.ascast, align 8
32+
// CHECK: %7 = alloca i8, i64 %6, align 1, addrspace(5)
33+
// CHECK: %8 = addrspacecast ptr addrspace(5) %7 to ptr
34+
// CHECK: store ptr %8, ptr %aligned_a.ascast, align 8
35+
// CHECK: %9 = load i64, ptr %n.addr.ascast, align 8
36+
// CHECK: %10 = alloca i8, i64 %9, align 1, addrspace(5)
37+
// CHECK: %11 = addrspacecast ptr addrspace(5) %10 to ptr
38+
// CHECK: store ptr %11, ptr %aligned_uninitialized_a.ascast, align 8
39+
// CHECK: ret void
40+
// CHECK-CL12-NOT: addrspacecast
41+
// CHECK-CL20-NOT: addrspacecast

0 commit comments

Comments
 (0)