Skip to content

Commit d65f037

Browse files
authored
[Clang] Use private address space for builtin_alloca return type for OpenCL (#95750)
The __builtin_alloca was returning a flat pointer with no address space when compiled using openCL1.2 or below but worked fine with openCL2.0 and above. This accounts to the fact that later uses the concept of generic address space which supports cast to other address space(i.e to private address space which is used for stack allocation) . But, in actuality, as it returns pointer to the stack, it should be pointing to private address space irrespective of openCL version becuase builtin_alloca allocates stack memory used for current function in which it is called. Thus,it requires redefintion of the builtin function with appropraite return pointer to private address space.
1 parent 35dfe80 commit d65f037

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,18 @@ static bool BuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall,
14771477
return false;
14781478
}
14791479

1480+
// In OpenCL, __builtin_alloca_* should return a pointer to address space
1481+
// that corresponds to the stack address space i.e private address space.
1482+
static void builtinAllocaAddrSpace(Sema &S, CallExpr *TheCall) {
1483+
QualType RT = TheCall->getType();
1484+
assert((RT->isPointerType() && !(RT->getPointeeType().hasAddressSpace())) &&
1485+
"__builtin_alloca has invalid address space");
1486+
1487+
RT = RT->getPointeeType();
1488+
RT = S.Context.getAddrSpaceQualType(RT, LangAS::opencl_private);
1489+
TheCall->setType(S.Context.getPointerType(RT));
1490+
}
1491+
14801492
namespace {
14811493
enum PointerAuthOpKind {
14821494
PAO_Strip,
@@ -2214,6 +2226,9 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
22142226
case Builtin::BI__builtin_alloca_uninitialized:
22152227
Diag(TheCall->getBeginLoc(), diag::warn_alloca)
22162228
<< TheCall->getDirectCallee();
2229+
if (getLangOpts().OpenCL) {
2230+
builtinAllocaAddrSpace(*this, TheCall);
2231+
}
22172232
break;
22182233
case Builtin::BI__arithmetic_fence:
22192234
if (BuiltinArithmeticFence(TheCall))
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang_cc1 %s -O0 -triple amdgcn-amd-amdhsa -cl-std=CL1.2 \
3+
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=OPENCL %s
4+
// RUN: %clang_cc1 %s -O0 -triple amdgcn-amd-amdhsa -cl-std=CL2.0 \
5+
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=OPENCL %s
6+
// RUN: %clang_cc1 %s -O0 -triple amdgcn-amd-amdhsa -cl-std=CL3.0 \
7+
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=OPENCL %s
8+
// RUN: %clang_cc1 %s -O0 -triple amdgcn-amd-amdhsa -cl-std=CL3.0 -cl-ext=+__opencl_c_generic_address_space \
9+
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=OPENCL %s
10+
11+
// OPENCL-LABEL: define dso_local void @test1_builtin_alloca(
12+
// OPENCL-SAME: i32 noundef [[N:%.*]]) #[[ATTR0:[0-9]+]] {
13+
// OPENCL-NEXT: [[ENTRY:.*:]]
14+
// OPENCL-NEXT: [[N_ADDR:%.*]] = alloca i32, align 4, addrspace(5)
15+
// OPENCL-NEXT: [[ALLOC_PTR:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
16+
// OPENCL-NEXT: store i32 [[N]], ptr addrspace(5) [[N_ADDR]], align 4
17+
// OPENCL-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(5) [[N_ADDR]], align 4
18+
// OPENCL-NEXT: [[CONV:%.*]] = zext i32 [[TMP0]] to i64
19+
// OPENCL-NEXT: [[MUL:%.*]] = mul i64 [[CONV]], 4
20+
// OPENCL-NEXT: [[TMP1:%.*]] = alloca i8, i64 [[MUL]], align 8, addrspace(5)
21+
// OPENCL-NEXT: store ptr addrspace(5) [[TMP1]], ptr addrspace(5) [[ALLOC_PTR]], align 4
22+
// OPENCL-NEXT: ret void
23+
//
24+
void test1_builtin_alloca(unsigned n) {
25+
__private float* alloc_ptr = (__private float*)__builtin_alloca(n*sizeof(int));
26+
}
27+
28+
// OPENCL-LABEL: define dso_local void @test1_builtin_alloca_uninitialized(
29+
// OPENCL-SAME: i32 noundef [[N:%.*]]) #[[ATTR0]] {
30+
// OPENCL-NEXT: [[ENTRY:.*:]]
31+
// OPENCL-NEXT: [[N_ADDR:%.*]] = alloca i32, align 4, addrspace(5)
32+
// OPENCL-NEXT: [[ALLOC_PTR_UNINITIALIZED:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
33+
// OPENCL-NEXT: store i32 [[N]], ptr addrspace(5) [[N_ADDR]], align 4
34+
// OPENCL-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(5) [[N_ADDR]], align 4
35+
// OPENCL-NEXT: [[CONV:%.*]] = zext i32 [[TMP0]] to i64
36+
// OPENCL-NEXT: [[MUL:%.*]] = mul i64 [[CONV]], 4
37+
// OPENCL-NEXT: [[TMP1:%.*]] = alloca i8, i64 [[MUL]], align 8, addrspace(5)
38+
// OPENCL-NEXT: store ptr addrspace(5) [[TMP1]], ptr addrspace(5) [[ALLOC_PTR_UNINITIALIZED]], align 4
39+
// OPENCL-NEXT: ret void
40+
//
41+
void test1_builtin_alloca_uninitialized(unsigned n) {
42+
__private float* alloc_ptr_uninitialized = (__private float*)__builtin_alloca_uninitialized(n*sizeof(int));
43+
}
44+
45+
// OPENCL-LABEL: define dso_local void @test1_builtin_alloca_with_align(
46+
// OPENCL-SAME: i32 noundef [[N:%.*]]) #[[ATTR0]] {
47+
// OPENCL-NEXT: [[ENTRY:.*:]]
48+
// OPENCL-NEXT: [[N_ADDR:%.*]] = alloca i32, align 4, addrspace(5)
49+
// OPENCL-NEXT: [[ALLOC_PTR_ALIGN:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
50+
// OPENCL-NEXT: store i32 [[N]], ptr addrspace(5) [[N_ADDR]], align 4
51+
// OPENCL-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(5) [[N_ADDR]], align 4
52+
// OPENCL-NEXT: [[CONV:%.*]] = zext i32 [[TMP0]] to i64
53+
// OPENCL-NEXT: [[MUL:%.*]] = mul i64 [[CONV]], 4
54+
// OPENCL-NEXT: [[TMP1:%.*]] = alloca i8, i64 [[MUL]], align 1, addrspace(5)
55+
// OPENCL-NEXT: store ptr addrspace(5) [[TMP1]], ptr addrspace(5) [[ALLOC_PTR_ALIGN]], align 4
56+
// OPENCL-NEXT: ret void
57+
//
58+
void test1_builtin_alloca_with_align(unsigned n) {
59+
__private float* alloc_ptr_align = (__private float*)__builtin_alloca_with_align((n*sizeof(int)), 8);
60+
}
61+
62+
// OPENCL-LABEL: define dso_local void @test1_builtin_alloca_with_align_uninitialized(
63+
// OPENCL-SAME: i32 noundef [[N:%.*]]) #[[ATTR0]] {
64+
// OPENCL-NEXT: [[ENTRY:.*:]]
65+
// OPENCL-NEXT: [[N_ADDR:%.*]] = alloca i32, align 4, addrspace(5)
66+
// OPENCL-NEXT: [[ALLOC_PTR_ALIGN_UNINITIALIZED:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
67+
// OPENCL-NEXT: store i32 [[N]], ptr addrspace(5) [[N_ADDR]], align 4
68+
// OPENCL-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(5) [[N_ADDR]], align 4
69+
// OPENCL-NEXT: [[CONV:%.*]] = zext i32 [[TMP0]] to i64
70+
// OPENCL-NEXT: [[MUL:%.*]] = mul i64 [[CONV]], 4
71+
// OPENCL-NEXT: [[TMP1:%.*]] = alloca i8, i64 [[MUL]], align 1, addrspace(5)
72+
// OPENCL-NEXT: store ptr addrspace(5) [[TMP1]], ptr addrspace(5) [[ALLOC_PTR_ALIGN_UNINITIALIZED]], align 4
73+
// OPENCL-NEXT: ret void
74+
//
75+
void test1_builtin_alloca_with_align_uninitialized(unsigned n) {
76+
__private float* alloc_ptr_align_uninitialized = (__private float*)__builtin_alloca_with_align_uninitialized((n*sizeof(int)), 8);
77+
}
78+
79+
// OPENCL-LABEL: define dso_local void @test2_builtin_alloca(
80+
// OPENCL-SAME: i32 noundef [[N:%.*]]) #[[ATTR0]] {
81+
// OPENCL-NEXT: [[ENTRY:.*:]]
82+
// OPENCL-NEXT: [[N_ADDR:%.*]] = alloca i32, align 4, addrspace(5)
83+
// OPENCL-NEXT: [[ALLOC_PTR:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
84+
// OPENCL-NEXT: store i32 [[N]], ptr addrspace(5) [[N_ADDR]], align 4
85+
// OPENCL-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(5) [[N_ADDR]], align 4
86+
// OPENCL-NEXT: [[CONV:%.*]] = zext i32 [[TMP0]] to i64
87+
// OPENCL-NEXT: [[TMP1:%.*]] = alloca i8, i64 [[CONV]], align 8, addrspace(5)
88+
// OPENCL-NEXT: store ptr addrspace(5) [[TMP1]], ptr addrspace(5) [[ALLOC_PTR]], align 4
89+
// OPENCL-NEXT: ret void
90+
//
91+
void test2_builtin_alloca(unsigned n) {
92+
__private void *alloc_ptr = __builtin_alloca(n);
93+
}
94+
95+
// OPENCL-LABEL: define dso_local void @test2_builtin_alloca_uninitialized(
96+
// OPENCL-SAME: i32 noundef [[N:%.*]]) #[[ATTR0]] {
97+
// OPENCL-NEXT: [[ENTRY:.*:]]
98+
// OPENCL-NEXT: [[N_ADDR:%.*]] = alloca i32, align 4, addrspace(5)
99+
// OPENCL-NEXT: [[ALLOC_PTR_UNINITIALIZED:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
100+
// OPENCL-NEXT: store i32 [[N]], ptr addrspace(5) [[N_ADDR]], align 4
101+
// OPENCL-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(5) [[N_ADDR]], align 4
102+
// OPENCL-NEXT: [[CONV:%.*]] = zext i32 [[TMP0]] to i64
103+
// OPENCL-NEXT: [[TMP1:%.*]] = alloca i8, i64 [[CONV]], align 8, addrspace(5)
104+
// OPENCL-NEXT: store ptr addrspace(5) [[TMP1]], ptr addrspace(5) [[ALLOC_PTR_UNINITIALIZED]], align 4
105+
// OPENCL-NEXT: ret void
106+
//
107+
void test2_builtin_alloca_uninitialized(unsigned n) {
108+
__private void *alloc_ptr_uninitialized = __builtin_alloca_uninitialized(n);
109+
}
110+
111+
// OPENCL-LABEL: define dso_local void @test2_builtin_alloca_with_align(
112+
// OPENCL-SAME: i32 noundef [[N:%.*]]) #[[ATTR0]] {
113+
// OPENCL-NEXT: [[ENTRY:.*:]]
114+
// OPENCL-NEXT: [[N_ADDR:%.*]] = alloca i32, align 4, addrspace(5)
115+
// OPENCL-NEXT: [[ALLOC_PTR_ALIGN:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
116+
// OPENCL-NEXT: store i32 [[N]], ptr addrspace(5) [[N_ADDR]], align 4
117+
// OPENCL-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(5) [[N_ADDR]], align 4
118+
// OPENCL-NEXT: [[CONV:%.*]] = zext i32 [[TMP0]] to i64
119+
// OPENCL-NEXT: [[TMP1:%.*]] = alloca i8, i64 [[CONV]], align 1, addrspace(5)
120+
// OPENCL-NEXT: store ptr addrspace(5) [[TMP1]], ptr addrspace(5) [[ALLOC_PTR_ALIGN]], align 4
121+
// OPENCL-NEXT: ret void
122+
//
123+
void test2_builtin_alloca_with_align(unsigned n) {
124+
__private void *alloc_ptr_align = __builtin_alloca_with_align(n, 8);
125+
}
126+
127+
// OPENCL-LABEL: define dso_local void @test2_builtin_alloca_with_align_uninitialized(
128+
// OPENCL-SAME: i32 noundef [[N:%.*]]) #[[ATTR0]] {
129+
// OPENCL-NEXT: [[ENTRY:.*:]]
130+
// OPENCL-NEXT: [[N_ADDR:%.*]] = alloca i32, align 4, addrspace(5)
131+
// OPENCL-NEXT: [[ALLOC_PTR_ALIGN_UNINITIALIZED:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
132+
// OPENCL-NEXT: store i32 [[N]], ptr addrspace(5) [[N_ADDR]], align 4
133+
// OPENCL-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(5) [[N_ADDR]], align 4
134+
// OPENCL-NEXT: [[CONV:%.*]] = zext i32 [[TMP0]] to i64
135+
// OPENCL-NEXT: [[TMP1:%.*]] = alloca i8, i64 [[CONV]], align 1, addrspace(5)
136+
// OPENCL-NEXT: store ptr addrspace(5) [[TMP1]], ptr addrspace(5) [[ALLOC_PTR_ALIGN_UNINITIALIZED]], align 4
137+
// OPENCL-NEXT: ret void
138+
//
139+
void test2_builtin_alloca_with_align_uninitialized(unsigned n) {
140+
__private void *alloc_ptr_align_uninitialized = __builtin_alloca_with_align_uninitialized(n, 8);
141+
}

0 commit comments

Comments
 (0)