1 // RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-apple-darwin -emit-llvm < %s | FileCheck -enable-var-scope -check-prefixes=CHECK,X86 %s
2 // RUN: %clang_cc1 -no-opaque-pointers -triple amdgcn -emit-llvm < %s | FileCheck -enable-var-scope -check-prefixes=CHECK,AMDGCN %s
3 
4 // CHECK: @foo ={{.*}} addrspace(1) global
5 int foo __attribute__((address_space(1)));
6 
7 // CHECK: @ban ={{.*}} addrspace(1) global
8 int ban[10] __attribute__((address_space(1)));
9 
10 // CHECK: @a ={{.*}} global
11 int a __attribute__((address_space(0)));
12 
13 // CHECK-LABEL: define{{.*}} i32 @test1()
14 // CHECK: load i32, i32 addrspace(1)* @foo
test1(void)15 int test1(void) { return foo; }
16 
17 // CHECK-LABEL: define{{.*}} i32 @test2(i32 noundef %i)
18 // CHECK: load i32, i32 addrspace(1)*
19 // CHECK-NEXT: ret i32
test2(int i)20 int test2(int i) { return ban[i]; }
21 
22 // Both A and B point into addrspace(2).
23 __attribute__((address_space(2))) int *A, *B;
24 
25 // CHECK-LABEL: define{{.*}} void @test3()
26 // X86: load i32 addrspace(2)*, i32 addrspace(2)** @B
27 // AMDGCN: load i32 addrspace(2)*, i32 addrspace(2)** addrspacecast (i32 addrspace(2)* addrspace(1)* @B to i32 addrspace(2)**)
28 // CHECK: load i32, i32 addrspace(2)*
29 // X86: load i32 addrspace(2)*, i32 addrspace(2)** @A
30 // AMDGCN: load i32 addrspace(2)*, i32 addrspace(2)** addrspacecast (i32 addrspace(2)* addrspace(1)* @A to i32 addrspace(2)**)
31 // CHECK: store i32 {{.*}}, i32 addrspace(2)*
test3(void)32 void test3(void) {
33   *A = *B;
34 }
35 
36 // PR7437
37 typedef struct {
38   float aData[1];
39 } MyStruct;
40 
41 // CHECK-LABEL: define{{.*}} void @test4(
42 // CHECK: call void @llvm.memcpy.p0i8.p2i8
43 // CHECK: call void @llvm.memcpy.p2i8.p0i8
test4(MyStruct * pPtr)44 void test4(MyStruct __attribute__((address_space(2))) *pPtr) {
45   MyStruct s = pPtr[0];
46   pPtr[0] = s;
47 }
48 
49 // Make sure the right address space is used when doing arithmetic on a void
50 // pointer. Make sure no invalid bitcast is introduced.
51 
52 // CHECK-LABEL: @void_ptr_arithmetic_test(
53 // X86: [[ALLOCA:%.*]] = alloca i8 addrspace(1)*
54 // X86-NEXT: store i8 addrspace(1)* %arg, i8 addrspace(1)** [[ALLOCA]]
55 // X86-NEXT: load i8 addrspace(1)*, i8 addrspace(1)** [[ALLOCA]]
56 // X86-NEXT: getelementptr i8, i8 addrspace(1)*
57 // X86-NEXT: ret i8 addrspace(1)*
58 void __attribute__((address_space(1)))*
void_ptr_arithmetic_test(void * arg)59 void_ptr_arithmetic_test(void __attribute__((address_space(1))) *arg) {
60     return arg + 4;
61 }
62 
63 // CHECK-LABEL: define{{.*}} i32* @test5(
test5(void)64 const unsigned *test5(void) {
65   // Intentionally leave a part of an array uninitialized. This triggers a
66   // different code path contrary to a fully initialized array.
67   // CHECK: ret i32* getelementptr inbounds ([256 x i32]
68   static const unsigned bars[256] = {
69       0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
70       11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
71   return &bars[0];
72 }
73