1; RUN: opt -S -mtriple=amdgcn--  -amdgpu-replace-lds-use-with-pointer -amdgpu-enable-lds-replace-with-pointer=true < %s | FileCheck %s
2
3; DESCRIPTION:
4;
5; The lds global @lds_used_within_func is used within non-kernel function @func_uses_lds
6; which is recheable from kernel @kernel_reaches_lds, hence pointer replacement takes place
7; for @lds_used_within_func.
8;
9
10; Original LDS should exist.
11; CHECK: @lds_used_within_func = internal addrspace(3) global [4 x i32] undef, align 4
12@lds_used_within_func = internal addrspace(3) global [4 x i32] undef, align 4
13
14; Pointer should be created.
15; CHECK: @lds_used_within_func.ptr = internal unnamed_addr addrspace(3) global i16 undef, align 2
16
17; Pointer replacement code should be added.
18define internal void @func_uses_lds() {
19; CHECK-LABEL: entry:
20; CHECK:   %0 = load i16, i16 addrspace(3)* @lds_used_within_func.ptr, align 2
21; CHECK:   %1 = getelementptr i8, i8 addrspace(3)* null, i16 %0
22; CHECK:   %2 = bitcast i8 addrspace(3)* %1 to [4 x i32] addrspace(3)*
23; CHECK:   %gep = getelementptr inbounds [4 x i32], [4 x i32] addrspace(3)* %2, i32 0, i32 0
24; CHECK:   ret void
25entry:
26  %gep = getelementptr inbounds [4 x i32], [4 x i32] addrspace(3)* @lds_used_within_func, i32 0, i32 0
27  ret void
28}
29
30; No change
31define internal void @func_does_not_use_lds_3() {
32; CHECK-LABEL: entry:
33; CHECK:   call void @func_uses_lds()
34; CHECK:   ret void
35entry:
36  call void @func_uses_lds()
37  ret void
38}
39
40; No change
41define internal void @func_does_not_use_lds_2() {
42; CHECK-LABEL: entry:
43; CHECK:   call void @func_uses_lds()
44; CHECK:   ret void
45entry:
46  call void @func_uses_lds()
47  ret void
48}
49
50; No change
51define internal void @func_does_not_use_lds_1() {
52; CHECK-LABEL: entry:
53; CHECK:   call void @func_does_not_use_lds_2()
54; CHECK:   call void @func_does_not_use_lds_3()
55; CHECK:   ret void
56entry:
57  call void @func_does_not_use_lds_2()
58  call void @func_does_not_use_lds_3()
59  ret void
60}
61
62; Pointer initialization code shoud be added
63define protected amdgpu_kernel void @kernel_reaches_lds() {
64; CHECK-LABEL: entry:
65; CHECK:   %0 = call i32 @llvm.amdgcn.mbcnt.lo(i32 -1, i32 0)
66; CHECK:   %1 = icmp eq i32 %0, 0
67; CHECK:   br i1 %1, label %2, label %3
68;
69; CHECK-LABEL: 2:
70; CHECK:   store i16 ptrtoint ([4 x i32] addrspace(3)* @lds_used_within_func to i16), i16 addrspace(3)* @lds_used_within_func.ptr, align 2
71; CHECK:   br label %3
72;
73; CHECK-LABEL: 3:
74; CHECK:   call void @llvm.amdgcn.wave.barrier()
75; CHECK:   call void @func_does_not_use_lds_1()
76; CHECK:   ret void
77entry:
78  call void @func_does_not_use_lds_1()
79  ret void
80}
81
82; No change here since this kernel does not reach @func_uses_lds which uses lds.
83define protected amdgpu_kernel void @kernel_does_not_reach_lds() {
84; CHECK-LABEL: entry:
85; CHECK:   ret void
86entry:
87  ret void
88}
89