blob: 58ae9c5d2ecce480994951a033d02951f69b2be7 [file] [log] [blame]
Artem Belevich36090852016-03-02 21:03:20 +00001// RUN: echo "GPU binary would be here" > %t
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +00002// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
3// RUN: -fcuda-include-gpubinary %t -o - \
4// RUN: | FileCheck %s --check-prefixes=ALL,NORDC
5// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
6// RUN: -fcuda-include-gpubinary %t -o - -DNOGLOBALS \
Artem Belevich8c1ec1e2016-03-02 18:28:53 +00007// RUN: | FileCheck %s -check-prefix=NOGLOBALS
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +00008// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \
9// RUN: -fcuda-rdc -fcuda-include-gpubinary %t -o - \
10// RUN: | FileCheck %s --check-prefixes=ALL,RDC
11// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s -o - \
12// RUN: | FileCheck %s -check-prefix=NOGPUBIN
Peter Collingbournefa4d6032011-10-06 18:51:56 +000013
Eli Bendersky3468d9d2014-04-28 22:21:28 +000014#include "Inputs/cuda.h"
Peter Collingbournefa4d6032011-10-06 18:51:56 +000015
Artem Belevich8c1ec1e2016-03-02 18:28:53 +000016#ifndef NOGLOBALS
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000017// ALL-DAG: @device_var = internal global i32
Artem Belevich42e19492016-03-02 18:28:50 +000018__device__ int device_var;
19
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000020// ALL-DAG: @constant_var = internal global i32
Artem Belevich42e19492016-03-02 18:28:50 +000021__constant__ int constant_var;
22
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000023// ALL-DAG: @shared_var = internal global i32
Artem Belevich42e19492016-03-02 18:28:50 +000024__shared__ int shared_var;
25
26// Make sure host globals don't get internalized...
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000027// ALL-DAG: @host_var = global i32
Artem Belevich42e19492016-03-02 18:28:50 +000028int host_var;
29// ... and that extern vars remain external.
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000030// ALL-DAG: @ext_host_var = external global i32
Artem Belevich42e19492016-03-02 18:28:50 +000031extern int ext_host_var;
32
33// Shadows for external device-side variables are *definitions* of
34// those variables.
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000035// ALL-DAG: @ext_device_var = internal global i32
Artem Belevich42e19492016-03-02 18:28:50 +000036extern __device__ int ext_device_var;
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000037// ALL-DAG: @ext_device_var = internal global i32
Artem Belevich42e19492016-03-02 18:28:50 +000038extern __constant__ int ext_constant_var;
39
40void use_pointers() {
41 int *p;
42 p = &device_var;
43 p = &constant_var;
44 p = &shared_var;
45 p = &host_var;
46 p = &ext_device_var;
47 p = &ext_constant_var;
48 p = &ext_host_var;
49}
50
Artem Belevich52cc4872015-05-07 19:34:16 +000051// Make sure that all parts of GPU code init/cleanup are there:
52// * constant unnamed string with the kernel name
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000053// ALL: private unnamed_addr constant{{.*}}kernelfunc{{.*}}\00"
Artem Belevich52cc4872015-05-07 19:34:16 +000054// * constant unnamed string with GPU binary
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000055// ALL: private unnamed_addr constant{{.*GPU binary would be here.*}}\00"
56// NORDC-SAME: section ".nv_fatbin", align 8
57// RDC-SAME: section "__nv_relfatbin", align 8
Artem Belevich52cc4872015-05-07 19:34:16 +000058// * constant struct that wraps GPU binary
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000059// ALL: @__cuda_fatbin_wrapper = internal constant { i32, i32, i8*, i8* }
60// ALL-SAME: { i32 1180844977, i32 1, {{.*}}, i8* null }
61// ALL-SAME: section ".nvFatBinSegment"
Artem Belevich52cc4872015-05-07 19:34:16 +000062// * variable to save GPU binary handle after initialization
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000063// NORDC: @__cuda_gpubin_handle = internal global i8** null
64// * constant unnamed string with NVModuleID
65// RDC: [[MODULE_ID_GLOBAL:@.*]] = private unnamed_addr constant
66// RDC-SAME: c"[[MODULE_ID:.+]]\00", section "__nv_module_id", align 32
67// * Make sure our constructor was added to global ctor list.
68// ALL: @llvm.global_ctors = appending global {{.*}}@__cuda_module_ctor
69// * In separate mode we also register a destructor.
70// NORDC: @llvm.global_dtors = appending global {{.*}}@__cuda_module_dtor
71// * Alias to global symbol containing the NVModuleID.
72// RDC: @__fatbinwrap[[MODULE_ID]] = alias { i32, i32, i8*, i8* }
73// RDC-SAME: { i32, i32, i8*, i8* }* @__cuda_fatbin_wrapper
Artem Belevich52cc4872015-05-07 19:34:16 +000074
Peter Collingbournefa4d6032011-10-06 18:51:56 +000075// Test that we build the correct number of calls to cudaSetupArgument followed
76// by a call to cudaLaunch.
77
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000078// ALL: define{{.*}}kernelfunc
79// ALL: call{{.*}}cudaSetupArgument
80// ALL: call{{.*}}cudaSetupArgument
81// ALL: call{{.*}}cudaSetupArgument
82// ALL: call{{.*}}cudaLaunch
Peter Collingbournefa4d6032011-10-06 18:51:56 +000083__global__ void kernelfunc(int i, int j, int k) {}
Artem Belevich52cc4872015-05-07 19:34:16 +000084
85// Test that we've built correct kernel launch sequence.
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000086// ALL: define{{.*}}hostfunc
87// ALL: call{{.*}}cudaConfigureCall
88// ALL: call{{.*}}kernelfunc
Artem Belevich52cc4872015-05-07 19:34:16 +000089void hostfunc(void) { kernelfunc<<<1, 1>>>(1, 1, 1); }
Artem Belevich8c1ec1e2016-03-02 18:28:53 +000090#endif
Artem Belevich52cc4872015-05-07 19:34:16 +000091
Artem Belevich42e19492016-03-02 18:28:50 +000092// Test that we've built a function to register kernels and global vars.
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000093// ALL: define internal void @__cuda_register_globals
94// ALL: call{{.*}}cudaRegisterFunction(i8** %0, {{.*}}kernelfunc
95// ALL-DAG: call{{.*}}cudaRegisterVar(i8** %0, {{.*}}device_var{{.*}}i32 0, i32 4, i32 0, i32 0
96// ALL-DAG: call{{.*}}cudaRegisterVar(i8** %0, {{.*}}constant_var{{.*}}i32 0, i32 4, i32 1, i32 0
97// ALL-DAG: call{{.*}}cudaRegisterVar(i8** %0, {{.*}}ext_device_var{{.*}}i32 1, i32 4, i32 0, i32 0
98// ALL-DAG: call{{.*}}cudaRegisterVar(i8** %0, {{.*}}ext_constant_var{{.*}}i32 1, i32 4, i32 1, i32 0
99// ALL: ret void
Artem Belevich52cc4872015-05-07 19:34:16 +0000100
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000101// Test that we've built a constructor.
102// ALL: define internal void @__cuda_module_ctor
103
104// In separate mode it calls __cudaRegisterFatBinary(&__cuda_fatbin_wrapper)
105// NORDC: call{{.*}}cudaRegisterFatBinary{{.*}}__cuda_fatbin_wrapper
Artem Belevich52cc4872015-05-07 19:34:16 +0000106// .. stores return value in __cuda_gpubin_handle
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000107// NORDC-NEXT: store{{.*}}__cuda_gpubin_handle
Artem Belevich42e19492016-03-02 18:28:50 +0000108// .. and then calls __cuda_register_globals
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000109// NORDC-NEXT: call void @__cuda_register_globals
110
111// With relocatable device code we call __cudaRegisterLinkedBinary%NVModuleID%
112// RDC: call{{.*}}__cudaRegisterLinkedBinary[[MODULE_ID]](
113// RDC-SAME: __cuda_register_globals, {{.*}}__cuda_fatbin_wrapper
114// RDC-SAME: [[MODULE_ID_GLOBAL]]
Artem Belevich52cc4872015-05-07 19:34:16 +0000115
116// Test that we've created destructor.
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000117// NORDC: define internal void @__cuda_module_dtor
118// NORDC: load{{.*}}__cuda_gpubin_handle
119// NORDC-NEXT: call void @__cudaUnregisterFatBinary
Artem Belevich52cc4872015-05-07 19:34:16 +0000120
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000121// There should be no __cuda_register_globals if we have no
122// device-side globals, but we still need to register GPU binary.
123// Skip GPU binary string first.
124// NOGLOBALS: @0 = private unnamed_addr constant{{.*}}
125// NOGLOBALS-NOT: define internal void @__cuda_register_globals
126// NOGLOBALS: define internal void @__cuda_module_ctor
127// NOGLOBALS: call{{.*}}cudaRegisterFatBinary{{.*}}__cuda_fatbin_wrapper
128// NOGLOBALS-NOT: call void @__cuda_register_globals
129// NOGLOBALS: define internal void @__cuda_module_dtor
130// NOGLOBALS: call void @__cudaUnregisterFatBinary
131
132// There should be no constructors/destructors if we have no GPU binary.
133// NOGPUBIN-NOT: define internal void @__cuda_register_globals
134// NOGPUBIN-NOT: define internal void @__cuda_module_ctor
135// NOGPUBIN-NOT: define internal void @__cuda_module_dtor