xref: /llvm-project/clang/test/CodeGen/address-space.c (revision 39d55321bd0b8ce436d6fcd8e5ba51b8bf535559)
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm < %s | FileCheck -enable-var-scope -check-prefixes=CHECK,X86 %s
2 // RUN: %clang_cc1 -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, ptr 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, ptr 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 ptr addrspace(2), ptr @B
27 // AMDGCN: load ptr addrspace(2), ptr addrspacecast (ptr addrspace(1) @B to ptr)
28 // CHECK: load i32, ptr addrspace(2)
29 // X86: load ptr addrspace(2), ptr @A
30 // AMDGCN: load ptr addrspace(2), ptr addrspacecast (ptr addrspace(1) @A to ptr)
31 // CHECK: store i32 {{.*}}, ptr 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.p0.p2
43 // CHECK: call void @llvm.memcpy.p2.p0
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 ptr addrspace(1)
54 // X86-NEXT: store ptr addrspace(1) %arg, ptr [[ALLOCA]]
55 // X86-NEXT: load ptr addrspace(1), ptr [[ALLOCA]]
56 // X86-NEXT: getelementptr inbounds i8, ptr addrspace(1)
57 // X86-NEXT: ret ptr 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{{.*}} ptr @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   // X86: ret ptr @test5.bars
68   // AMDGCN: ret ptr addrspacecast (ptr addrspace(4) @test5.bars to ptr)
69   static const unsigned bars[256] = {
70       0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
71       11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
72   return &bars[0];
73 }
74