1 // RUN: echo -n "GPU binary would be here." > %t
2 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
3 // RUN: -target-sdk-version=8.0 -fcuda-include-gpubinary %t -o - \
4 // RUN: | FileCheck -allow-deprecated-dag-overlap %s \
5 // RUN: --check-prefixes=ALL,LNX,NORDC,CUDA,CUDANORDC,CUDA-OLD
6 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
7 // RUN: -target-sdk-version=8.0 -fcuda-include-gpubinary %t \
8 // RUN: -o - -DNOGLOBALS \
9 // RUN: | FileCheck -allow-deprecated-dag-overlap %s \
10 // RUN: -check-prefixes=NOGLOBALS,CUDANOGLOBALS
11 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
12 // RUN: -target-sdk-version=8.0 -fgpu-rdc -fcuda-include-gpubinary %t \
14 // RUN: | FileCheck -allow-deprecated-dag-overlap %s \
15 // RUN: --check-prefixes=ALL,LNX,RDC,CUDA,CUDARDC,CUDA-OLD
16 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
17 // RUN: -target-sdk-version=8.0 -o - \
18 // RUN: | FileCheck -allow-deprecated-dag-overlap %s -check-prefix=NOGPUBIN
20 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
21 // RUN: -target-sdk-version=9.2 -fcuda-include-gpubinary %t -o - \
22 // RUN: | FileCheck %s -allow-deprecated-dag-overlap \
23 // RUN: --check-prefixes=ALL,LNX,NORDC,CUDA,CUDANORDC,CUDA-NEW
24 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
25 // RUN: -target-sdk-version=9.2 -fcuda-include-gpubinary %t -o - -DNOGLOBALS \
26 // RUN: | FileCheck -allow-deprecated-dag-overlap %s \
27 // RUN: --check-prefixes=NOGLOBALS,CUDANOGLOBALS
28 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
29 // RUN: -target-sdk-version=9.2 -fgpu-rdc -fcuda-include-gpubinary %t -o - \
30 // RUN: | FileCheck %s -allow-deprecated-dag-overlap \
31 // RUN: --check-prefixes=ALL,LNX,RDC,CUDA,CUDARDC,CUDA-NEW
32 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s -std=c++17 \
33 // RUN: -target-sdk-version=9.2 -fcuda-include-gpubinary %t -o - \
34 // RUN: | FileCheck %s -allow-deprecated-dag-overlap \
35 // RUN: --check-prefixes=ALL,LNX,NORDC,CUDA,CUDANORDC,CUDA-NEW,LNX_17,NORDC17
36 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s -std=c++17 \
37 // RUN: -target-sdk-version=9.2 -fgpu-rdc -fcuda-include-gpubinary %t -o - \
38 // RUN: | FileCheck %s -allow-deprecated-dag-overlap \
39 // RUN: --check-prefixes=ALL,LNX,RDC,CUDA,CUDARDC,CUDA-NEW,LNX_17,RDC17
40 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
41 // RUN: -target-sdk-version=9.2 -o - \
42 // RUN: | FileCheck -allow-deprecated-dag-overlap %s -check-prefix=NOGPUBIN
44 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
45 // RUN: -fcuda-include-gpubinary %t -o - -x hip\
46 // RUN: | FileCheck -allow-deprecated-dag-overlap %s --check-prefixes=ALL,LNX,NORDC,HIP,HIPEF
47 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
48 // RUN: -fcuda-include-gpubinary %t -o - -DNOGLOBALS -x hip \
49 // RUN: | FileCheck -allow-deprecated-dag-overlap %s -check-prefixes=NOGLOBALS,HIPNOGLOBALS
50 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
51 // RUN: -fgpu-rdc -fcuda-include-gpubinary %t -o - -x hip \
52 // RUN: | FileCheck -allow-deprecated-dag-overlap %s --check-prefixes=ALL,LNX,RDC,HIP,HIPEF
53 // RUN: %clang_cc1 -cuid=123 -triple x86_64-linux-gnu -emit-llvm %s -o - -x hip\
54 // RUN: | FileCheck -allow-deprecated-dag-overlap %s -check-prefixes=ALL,LNX,NORDC,HIP,HIPNEF
56 // RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -aux-triple amdgcn -emit-llvm %s \
57 // RUN: -fcuda-include-gpubinary %t -o - -x hip\
58 // RUN: | FileCheck -allow-deprecated-dag-overlap %s --check-prefixes=ALL,WIN
60 // RUN: %clang_cc1 -cuid=123 -triple x86_64-pc-windows-msvc -aux-triple amdgcn -emit-llvm %s \
62 // RUN: | FileCheck -allow-deprecated-dag-overlap %s --check-prefixes=ALL,WIN,HIP,HIPNEF
64 #include "Inputs/cuda.h"
67 // NORDC-DAG: @device_var = internal global i32
68 // RDC-DAG: @device_var = global i32
69 // WIN-DAG: @"?device_var@@3HA" = internal global i32
70 __device__ int device_var;
72 // NORDC-DAG: @constant_var = internal global i32
73 // RDC-DAG: @constant_var = global i32
74 // WIN-DAG: @"?constant_var@@3HA" = internal global i32
75 __constant__ int constant_var;
77 // NORDC-DAG: @shared_var = internal global i32
78 // RDC-DAG: @shared_var = global i32
79 // WIN-DAG: @"?shared_var@@3HA" = internal global i32
80 __shared__ int shared_var;
82 // Make sure host globals don't get internalized...
83 // LNX-DAG: @host_var ={{.*}} global i32
84 // WIN-DAG: @"?host_var@@3HA" = dso_local global i32
86 // ... and that extern vars remain external.
87 // LNX-DAG: @ext_host_var = external global i32
88 // WIN-DAG: @"?ext_host_var@@3HA" = external dso_local global i32
89 extern int ext_host_var;
91 // external device-side variables -> extern references to their shadows.
92 // LNX-DAG: @ext_device_var = external global i32
93 // WIN-DAG: @"?ext_device_var@@3HA" = external dso_local global i32
94 extern __device__ int ext_device_var;
95 // LNX-DAG: @ext_device_var = external global i32
96 // WIN-DAG: @"?ext_constant_var@@3HA" = external dso_local global i32
97 extern __constant__ int ext_constant_var;
99 // external device-side variables with definitions should generate
100 // definitions for the shadows.
101 // NORDC-DAG: @ext_device_var_def = internal global i32 undef,
102 // RDC-DAG: @ext_device_var_def = global i32 undef,
103 // WIN-DAG: @"?ext_device_var_def@@3HA" = internal global i32 undef
104 extern __device__ int ext_device_var_def;
105 __device__ int ext_device_var_def = 1;
106 // NORDC-DAG: @ext_device_var_def = internal global i32 undef,
107 // RDC-DAG: @ext_device_var_def = global i32 undef,
108 // WIN-DAG: @"?ext_constant_var_def@@3HA" = internal global i32 undef
109 __constant__ int ext_constant_var_def = 2;
111 #if __cplusplus > 201402L
112 // NORDC17: @inline_var = internal global i32 undef, comdat, align 4{{$}}
113 // RDC17: @inline_var = linkonce_odr global i32 undef, comdat, align 4{{$}}
114 // NORDC17-NOT: @inline_var2 =
115 // RDC17-NOT: @inline_var2 =
116 // NORDC17: @_ZN1C17member_inline_varE = internal constant i32 undef, comdat, align 4{{$}}
117 // RDC17: @_ZN1C17member_inline_varE = linkonce_odr constant i32 undef, comdat, align 4{{$}}
118 // Check inline variable ODR-used by host is emitted on host and registered.
119 __device__ inline int inline_var = 3;
120 // Check inline variable not ODR-used by host is not emitted on host or registered.
121 __device__ inline int inline_var2 = 5;
123 __device__ static constexpr int member_inline_var = 4;
127 void use_pointers() {
134 p = &ext_constant_var;
136 #if __cplusplus > 201402L
138 decltype(inline_var2) tmp;
139 p = &C::member_inline_var;
143 __device__ void device_use() {
144 #if __cplusplus > 201402L
145 const int *p = &inline_var2;
149 // Make sure that all parts of GPU code init/cleanup are there:
150 // * constant unnamed string with the device-side kernel name to be passed to
151 // __hipRegisterFunction/__cudaRegisterFunction.
152 // ALL: @0 = private unnamed_addr constant [18 x i8] c"_Z10kernelfunciii\00"
153 // * constant unnamed string with the device-side kernel name to be passed to
154 // __hipRegisterVar/__cudaRegisterVar.
155 // ALL: @1 = private unnamed_addr constant [11 x i8] c"device_var\00"
156 // ALL: @2 = private unnamed_addr constant [13 x i8] c"constant_var\00"
157 // ALL: @3 = private unnamed_addr constant [19 x i8] c"ext_device_var_def\00"
158 // ALL: @4 = private unnamed_addr constant [21 x i8] c"ext_constant_var_def\00"
159 // * constant unnamed string with GPU binary
160 // CUDA: @[[FATBIN:.*]] = private constant{{.*}} c"GPU binary would be here.",
161 // HIPEF: @[[FATBIN:.*]] = private constant{{.*}} c"GPU binary would be here.",{{.*}}align 4096
162 // HIPNEF: @[[FATBIN:__hip_fatbin_[0-9a-f]+]] = external constant i8, section ".hip_fatbin"
163 // CUDANORDC-SAME: section ".nv_fatbin", align 8
164 // CUDARDC-SAME: section "__nv_relfatbin", align 8
165 // * constant struct that wraps GPU binary
166 // ALL: @__[[PREFIX:cuda|hip]]_fatbin_wrapper = internal constant
167 // LNX-SAME: { i32, i32, ptr, ptr }
168 // CUDA-SAME: { i32 1180844977, i32 1,
169 // HIP-SAME: { i32 1212764230, i32 1,
170 // CUDA-SAME: ptr @[[FATBIN]],
171 // HIPEF-SAME: ptr @[[FATBIN]],
172 // HIPNEF-SAME: ptr @[[FATBIN]],
173 // LNX-SAME: ptr null }
174 // CUDA-SAME: section ".nvFatBinSegment"
175 // HIP-SAME: section ".hipFatBinSegment"
176 // * variable to save GPU binary handle after initialization
177 // CUDANORDC: @__[[PREFIX]]_gpubin_handle = internal global ptr null
178 // HIPNEF: @__[[PREFIX]]_gpubin_handle_{{[0-9a-f]+}} = internal global ptr null, align 8
179 // * constant unnamed string with NVModuleID
180 // CUDARDC: [[MODULE_ID_GLOBAL:@.*]] = private constant
181 // CUDARDC-SAME: c"[[MODULE_ID:.+]]\00", section "__nv_module_id", align 32
182 // * Make sure our constructor was added to global ctor list.
183 // LNX: @llvm.global_ctors = appending global {{.*}}@__[[PREFIX]]_module_ctor
184 // * Alias to global symbol containing the NVModuleID.
185 // CUDARDC: @__fatbinwrap[[MODULE_ID]] ={{.*}} alias { i32, i32, ptr, ptr }
186 // CUDARDC-SAME: ptr @__[[PREFIX]]_fatbin_wrapper
188 // Test that we build the correct number of calls to cudaSetupArgument followed
189 // by a call to cudaLaunch.
191 // LNX: define{{.*}}kernelfunc
193 // New launch sequence stores arguments into local buffer and passes array of
194 // pointers to them directly to cudaLaunchKernel
199 // CUDA-NEW: call{{.*}}__cudaPopCallConfiguration
200 // CUDA-NEW: call{{.*}}cudaLaunchKernel
202 // Legacy style launch sequence sets up arguments by passing them to
203 // [cuda|hip]SetupArgument.
204 // CUDA-OLD: call{{.*}}[[PREFIX]]SetupArgument
205 // CUDA-OLD: call{{.*}}[[PREFIX]]SetupArgument
206 // CUDA-OLD: call{{.*}}[[PREFIX]]SetupArgument
207 // CUDA-OLD: call{{.*}}[[PREFIX]]Launch
209 // HIP: call{{.*}}[[PREFIX]]SetupArgument
210 // HIP: call{{.*}}[[PREFIX]]SetupArgument
211 // HIP: call{{.*}}[[PREFIX]]SetupArgument
212 // HIP: call{{.*}}[[PREFIX]]Launch
213 __global__ void kernelfunc(int i, int j, int k) {}
215 // Test that we've built correct kernel launch sequence.
216 // LNX: define{{.*}}hostfunc
217 // CUDA-OLD: call{{.*}}[[PREFIX]]ConfigureCall
218 // CUDA-NEW: call{{.*}}__cudaPushCallConfiguration
219 // HIP: call{{.*}}[[PREFIX]]ConfigureCall
220 // LNX: call{{.*}}kernelfunc
221 void hostfunc(void) { kernelfunc<<<1, 1>>>(1, 1, 1); }
224 // Test that we've built a function to register kernels and global vars.
225 // ALL: define internal void @__[[PREFIX]]_register_globals
226 // ALL: call{{.*}}[[PREFIX]]RegisterFunction(ptr %0, {{.*}}kernelfunc{{[^,]*}}, {{[^@]*}}@0
227 // ALL-DAG: call void {{.*}}[[PREFIX]]RegisterVar(ptr %0, {{.*}}device_var{{[^,]*}}, {{[^@]*}}@1, {{.*}}i32 0, {{i32|i64}} 4, i32 0, i32 0
228 // ALL-DAG: call void {{.*}}[[PREFIX]]RegisterVar(ptr %0, {{.*}}constant_var{{[^,]*}}, {{[^@]*}}@2, {{.*}}i32 0, {{i32|i64}} 4, i32 1, i32 0
229 // ALL-DAG: call void {{.*}}[[PREFIX]]RegisterVar(ptr %0, {{.*}}ext_device_var_def{{[^,]*}}, {{[^@]*}}@3, {{.*}}i32 0, {{i32|i64}} 4, i32 0, i32 0
230 // ALL-DAG: call void {{.*}}[[PREFIX]]RegisterVar(ptr %0, {{.*}}ext_constant_var_def{{[^,]*}}, {{[^@]*}}@4, {{.*}}i32 0, {{i32|i64}} 4, i32 1, i32 0
231 // LNX_17-DAG: [[PREFIX]]RegisterVar(ptr %0, {{.*}}inline_var
232 // LNX_17-NOT: [[PREFIX]]RegisterVar(ptr %0, {{.*}}inline_var2
235 // Test that we've built a constructor.
236 // LNX: define internal void @__[[PREFIX]]_module_ctor
238 // In separate mode it calls __[[PREFIX]]RegisterFatBinary(&__[[PREFIX]]_fatbin_wrapper)
239 // HIP only register fat binary once.
240 // HIP: load ptr, ptr @__hip_gpubin_handle
241 // HIP-NEXT: icmp eq ptr {{.*}}, null
242 // HIP-NEXT: br i1 {{.*}}, label %if, label %exit
244 // CUDANORDC: call{{.*}}[[PREFIX]]RegisterFatBinary{{.*}}__[[PREFIX]]_fatbin_wrapper
245 // .. stores return value in __[[PREFIX]]_gpubin_handle
246 // CUDANORDC-NEXT: store{{.*}}__[[PREFIX]]_gpubin_handle
247 // .. and then calls __[[PREFIX]]_register_globals
248 // HIP: call{{.*}}[[PREFIX]]RegisterFatBinary{{.*}}__[[PREFIX]]_fatbin_wrapper
249 // .. stores return value in __[[PREFIX]]_gpubin_handle
250 // HIP-NEXT: store{{.*}}__[[PREFIX]]_gpubin_handle
251 // .. and then calls __[[PREFIX]]_register_globals
252 // HIP-NEXT: br label %exit
254 // HIP-NEXT: load ptr, ptr @__hip_gpubin_handle
255 // CUDANORDC-NEXT: call void @__[[PREFIX]]_register_globals
256 // HIP-NEXT: call void @__[[PREFIX]]_register_globals
257 // * In separate mode we also register a destructor.
258 // CUDANORDC-NEXT: call i32 @atexit(ptr @__[[PREFIX]]_module_dtor)
259 // HIP-NEXT: call i32 @atexit(ptr @__[[PREFIX]]_module_dtor)
261 // With relocatable device code we call __[[PREFIX]]RegisterLinkedBinary%NVModuleID%
262 // CUDARDC: call{{.*}}__[[PREFIX]]RegisterLinkedBinary[[MODULE_ID]](
263 // CUDARDC-SAME: __[[PREFIX]]_register_globals, {{.*}}__[[PREFIX]]_fatbin_wrapper
264 // CUDARDC-SAME: [[MODULE_ID_GLOBAL]]
266 // Test that we've created destructor.
267 // CUDANORDC: define internal void @__[[PREFIX]]_module_dtor
268 // HIP: define internal void @__[[PREFIX]]_module_dtor
269 // CUDANORDC: load{{.*}}__[[PREFIX]]_gpubin_handle
270 // HIP: load{{.*}}__[[PREFIX]]_gpubin_handle
271 // CUDANORDC-NEXT: call void @__[[PREFIX]]UnregisterFatBinary
272 // HIP-NEXT: icmp ne ptr {{.*}}, null
273 // HIP-NEXT: br i1 {{.*}}, label %if, label %exit
275 // HIP-NEXT: call void @__[[PREFIX]]UnregisterFatBinary
276 // HIP-NEXT: store ptr null, ptr @__hip_gpubin_handle
277 // HIP-NEXT: br label %exit
280 // There should be no __[[PREFIX]]_register_globals if we have no
281 // device-side globals, but we still need to register GPU binary.
282 // Skip GPU binary string first.
283 // CUDANOGLOBALS-NOT: @{{.*}} = private constant{{.*}}
284 // HIPNOGLOBALS-NOT: @{{.*}} = internal constant{{.*}}
285 // NOGLOBALS-NOT: define internal void @__{{.*}}_register_globals
286 // NOGLOBALS-NOT: define internal void @__{{cuda|hip}}_module_ctor
287 // NOGLOBALS-NOT: call{{.*}}{{cuda|hip}}RegisterFatBinary{{.*}}__{{cuda|hip}}_fatbin_wrapper
288 // NOGLOBALS-NOT: call void @__{{cuda|hip}}_register_globals
289 // NOGLOBALS-NOT: define internal void @__{{cuda|hip}}_module_dtor
290 // NOGLOBALS-NOT: call void @__{{cuda|hip}}UnregisterFatBinary
292 // There should be no constructors/destructors if we have no GPU binary.
293 // NOGPUBIN-NOT: define internal void @__{{cuda|hip}}_register_globals
294 // NOGPUBIN-NOT: define internal void @__{{cuda|hip}}_module_ctor
295 // NOGPUBIN-NOT: define internal void @__{{cuda|hip}}_module_dtor