Artem Belevich | 3609085 | 2016-03-02 21:03:20 +0000 | [diff] [blame] | 1 | // RUN: echo "GPU binary would be here" > %t |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 2 | // 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 Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 7 | // RUN: | FileCheck %s -check-prefix=NOGLOBALS |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 8 | // 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 Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 13 | |
Eli Bendersky | 3468d9d | 2014-04-28 22:21:28 +0000 | [diff] [blame] | 14 | #include "Inputs/cuda.h" |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 15 | |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 16 | #ifndef NOGLOBALS |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 17 | // ALL-DAG: @device_var = internal global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 18 | __device__ int device_var; |
| 19 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 20 | // ALL-DAG: @constant_var = internal global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 21 | __constant__ int constant_var; |
| 22 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 23 | // ALL-DAG: @shared_var = internal global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 24 | __shared__ int shared_var; |
| 25 | |
| 26 | // Make sure host globals don't get internalized... |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 27 | // ALL-DAG: @host_var = global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 28 | int host_var; |
| 29 | // ... and that extern vars remain external. |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 30 | // ALL-DAG: @ext_host_var = external global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 31 | extern int ext_host_var; |
| 32 | |
| 33 | // Shadows for external device-side variables are *definitions* of |
| 34 | // those variables. |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 35 | // ALL-DAG: @ext_device_var = internal global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 36 | extern __device__ int ext_device_var; |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 37 | // ALL-DAG: @ext_device_var = internal global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 38 | extern __constant__ int ext_constant_var; |
| 39 | |
| 40 | void 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 Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 51 | // Make sure that all parts of GPU code init/cleanup are there: |
| 52 | // * constant unnamed string with the kernel name |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 53 | // ALL: private unnamed_addr constant{{.*}}kernelfunc{{.*}}\00" |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 54 | // * constant unnamed string with GPU binary |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 55 | // 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 Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 58 | // * constant struct that wraps GPU binary |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 59 | // 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 Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 62 | // * variable to save GPU binary handle after initialization |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 63 | // 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 Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 74 | |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 75 | // Test that we build the correct number of calls to cudaSetupArgument followed |
| 76 | // by a call to cudaLaunch. |
| 77 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 78 | // ALL: define{{.*}}kernelfunc |
| 79 | // ALL: call{{.*}}cudaSetupArgument |
| 80 | // ALL: call{{.*}}cudaSetupArgument |
| 81 | // ALL: call{{.*}}cudaSetupArgument |
| 82 | // ALL: call{{.*}}cudaLaunch |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 83 | __global__ void kernelfunc(int i, int j, int k) {} |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 84 | |
| 85 | // Test that we've built correct kernel launch sequence. |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 86 | // ALL: define{{.*}}hostfunc |
| 87 | // ALL: call{{.*}}cudaConfigureCall |
| 88 | // ALL: call{{.*}}kernelfunc |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 89 | void hostfunc(void) { kernelfunc<<<1, 1>>>(1, 1, 1); } |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 90 | #endif |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 91 | |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 92 | // Test that we've built a function to register kernels and global vars. |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 93 | // 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 Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 100 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 101 | // 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 Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 106 | // .. stores return value in __cuda_gpubin_handle |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 107 | // NORDC-NEXT: store{{.*}}__cuda_gpubin_handle |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 108 | // .. and then calls __cuda_register_globals |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 109 | // 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 Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 115 | |
| 116 | // Test that we've created destructor. |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame^] | 117 | // NORDC: define internal void @__cuda_module_dtor |
| 118 | // NORDC: load{{.*}}__cuda_gpubin_handle |
| 119 | // NORDC-NEXT: call void @__cudaUnregisterFatBinary |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 120 | |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 121 | // 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 |