1 // REQUIRES: amdgpu-registered-target
2 // RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fcuda-is-device -x hip %s \
3 // RUN: -std=c++17 -O3 -mllvm -amdgpu-internalize-symbols -emit-llvm -o - \
4 // RUN: -cuid=123 | FileCheck -check-prefix=DEV %s
5 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -x hip %s \
6 // RUN: -std=c++17 -O3 -emit-llvm -o - -cuid=123 | FileCheck -check-prefix=HOST %s
10 // RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fcuda-is-device -x hip %s \
11 // RUN: -std=c++17 -O3 -mllvm -amdgpu-internalize-symbols -emit-llvm -o - \
12 // RUN: | FileCheck -check-prefix=DEV-NEG %s
13 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -x hip %s \
14 // RUN: -std=c++17 -O3 -emit-llvm -o - | FileCheck -check-prefix=HOST-NEG %s
16 #include "Inputs/cuda.h"
24 // Check device variables used by neither host nor device functioins are not kept.
26 // DEV-NEG-NOT: @_ZL2v3
27 static __device__ int v3;
29 // Check device variables used by host functions are kept.
37 // Check host-used static device var is in llvm.compiler.used.
39 static __device__ int u3;
41 // Check device-used static device var is emitted but is not in llvm.compiler.used.
43 static __device__ int u4;
45 // Check device variables with used attribute are always kept.
47 __device__ __attribute__((used)) int u5;
49 // Test external device variable ODR-used by host code is not emitted or registered.
50 // DEV-NEG-NOT: @ext_var
51 extern __device__ int ext_var;
53 // DEV-DAG: @inline_var = linkonce_odr addrspace(1) externally_initialized global i32 0
54 __device__ inline int inline_var;
57 using func_t = T (*) (T, T);
60 __device__ T add_func (T x, T y)
65 // DEV-DAG: @_Z10p_add_funcIiE = linkonce_odr addrspace(1) externally_initialized global ptr @_Z8add_funcIiET_S0_S0_
67 __device__ func_t<T> p_add_func = add_func<T>;
69 // Check non-constant constexpr variables ODR-used by host code only is not emitted.
70 // DEV-NEG-NOT: constexpr_var1a
71 // DEV-NEG-NOT: constexpr_var1b
72 constexpr int constexpr_var1a = 1;
73 inline constexpr int constexpr_var1b = 1;
75 // Check constant constexpr variables ODR-used by host code only.
76 // Device-side constexpr variables accessed by host code should be externalized and kept.
77 // DEV-DAG: @_ZL15constexpr_var2a = addrspace(4) externally_initialized constant i32 2
78 // DEV-DAG: @constexpr_var2b = linkonce_odr addrspace(4) externally_initialized constant i32 2
79 __constant__ constexpr int constexpr_var2a = 2;
80 inline __constant__ constexpr int constexpr_var2b = 2;
82 void use(func_t<int> p);
83 __host__ __device__ void use(const int *p);
85 // Check static device variable in host function.
86 // DEV-DAG: @_ZZ4fun1vE11static_var1 = addrspace(1) externally_initialized global i32 3
88 static __device__ int static_var1 = 3;
95 use(&constexpr_var1a);
96 use(&constexpr_var1b);
97 use(&constexpr_var2a);
98 use(&constexpr_var2b);
102 // Check static variable in host device function.
103 // DEV-DAG: @_ZZ4fun2vE11static_var2 = internal addrspace(1) global i32 4
104 // DEV-DAG: @_ZZ4fun2vE11static_var3 = addrspace(1) global i32 4
105 __host__ __device__ void fun2() {
106 static int static_var2 = 4;
107 static __device__ int static_var3 = 4;
112 __global__ void kern1(int **x) {
117 // Check static variables of lambda functions.
119 // Lambda functions are implicit host device functions.
120 // Default static variables in lambda functions should be treated
121 // as host variables on host side, therefore should not be forced
122 // to be emitted on device.
124 // DEV-DAG: @_ZZZN21TestStaticVarInLambda3funEvENKUlPcE_clES0_E4var2 = addrspace(1) externally_initialized global i32 5
125 // DEV-NEG-NOT: @_ZZZN21TestStaticVarInLambda3funEvENKUlPcE_clES0_E4var1
126 namespace TestStaticVarInLambda {
134 static __device__ int var2 = 5;
141 // Check implicit constant variable ODR-used by host code is not emitted.
143 // AST contains instantiation of al<ar>, which triggers AST instantiation
144 // of x::al<ar>::am, which triggers AST instatiation of x::ap<ar>,
145 // which triggers AST instantiation of aw<ar>::c, which has type
146 // ar. ar has base class x which has member ah. x::ah is initialized
147 // with function pointer pointing to ar:as, which returns an object
148 // of type ou. The constexpr aw<ar>::c is an implicit constant variable
149 // which is ODR-used by host function x::ap<ar>. An incorrect implementation
150 // will force aw<ar>::c to be emitted on device side, which will trigger
151 // emit of x::as and further more ctor of ou and variable o.
152 // The ODR-use of aw<ar>::c in x::ap<ar> should be treated as a host variable
153 // instead of device variable.
155 // DEV-NEG-NOT: _ZN16TestConstexprVar1oE
156 namespace TestConstexprVar {
160 ou(char) { __builtin_strlen(&o); }
162 template < typename ao > struct aw { static constexpr ao c; };
165 typedef ou (*y)(const x *);
166 constexpr x(y ag) : ah(ag) {}
167 template < bool * > struct ak;
168 template < typename > struct al {
172 template < typename ao > static x ap() { (void)aw< ao >::c; return x(nullptr); }
175 template < typename ao > bool x::al< ao >::am(&ap< ao >);
178 constexpr ar() : x(as) {}
179 static ou as(const x *) { return 0; }
184 // Check the exact list of variables to ensure @_ZL2u4 is not among them.
185 // DEV: @llvm.compiler.used = {{[^@]*}} @_Z10p_add_funcIiE
186 // DEV-SAME: {{^[^@]*}} @_ZL15constexpr_var2a
187 // DEV-SAME: {{^[^@]*}} @_ZL2u3
188 // DEV-SAME: {{^[^@]*}} @_ZZ4fun1vE11static_var1
189 // DEV-SAME: {{^[^@]*}} @_ZZZN21TestStaticVarInLambda3funEvENKUlPcE_clES0_E4var2
190 // DEV-SAME: {{^[^@]*}} @__hip_cuid_{{[0-9a-f]+}}
191 // DEV-SAME: {{^[^@]*}} @constexpr_var2b
192 // DEV-SAME: {{^[^@]*}} @inline_var
193 // DEV-SAME: {{^[^@]*}} @u1
194 // DEV-SAME: {{^[^@]*}} @u2
195 // DEV-SAME: {{^[^@]*}} @u5
196 // DEV-SAME: {{^[^@]*$}}
198 // HOST-DAG: hipRegisterVar{{.*}}@u1
199 // HOST-DAG: hipRegisterVar{{.*}}@u2
200 // HOST-DAG: hipRegisterVar{{.*}}@_ZL2u3
201 // HOST-DAG: hipRegisterVar{{.*}}@constexpr_var2b
202 // HOST-DAG: hipRegisterVar{{.*}}@u5
203 // HOST-DAG: hipRegisterVar{{.*}}@inline_var
204 // HOST-DAG: hipRegisterVar{{.*}}@_Z10p_add_funcIiE
205 // HOST-NEG-NOT: hipRegisterVar{{.*}}@_ZZ4fun1vE11static_var1
206 // HOST-NEG-NOT: hipRegisterVar{{.*}}@_ZZ4fun2vE11static_var2
207 // HOST-NEG-NOT: hipRegisterVar{{.*}}@_ZZ4fun2vE11static_var3
208 // HOST-NEG-NOT: hipRegisterVar{{.*}}@_ZZZN21TestStaticVarInLambda3funEvENKUlPcE_clES0_E4var2
209 // HOST-NEG-NOT: hipRegisterVar{{.*}}@_ZZZN21TestStaticVarInLambda3funEvENKUlPcE_clES0_E4var1
210 // HOST-NEG-NOT: hipRegisterVar{{.*}}@ext_var
211 // HOST-NEG-NOT: hipRegisterVar{{.*}}@_ZL2u4
212 // HOST-NEG-NOT: hipRegisterVar{{.*}}@constexpr_var1a
213 // HOST-NEG-NOT: hipRegisterVar{{.*}}@constexpr_var1b
214 // HOST-NEG-NOT: hipRegisterVar{{.*}}@constexpr_var2a