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 - \ |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 4 | // RUN: | FileCheck %s --check-prefixes=ALL,NORDC,CUDA,CUDANORDC |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 5 | // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \ |
| 6 | // RUN: -fcuda-include-gpubinary %t -o - -DNOGLOBALS \ |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 7 | // RUN: | FileCheck %s -check-prefixes=NOGLOBALS,CUDANOGLOBALS |
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 - \ |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 10 | // RUN: | FileCheck %s --check-prefixes=ALL,RDC,CUDA,CUDARDC |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 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 | |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 14 | // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \ |
| 15 | // RUN: -fcuda-include-gpubinary %t -o - -x hip\ |
| 16 | // RUN: | FileCheck %s --check-prefixes=ALL,NORDC,HIP |
| 17 | // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \ |
| 18 | // RUN: -fcuda-include-gpubinary %t -o - -DNOGLOBALS -x hip \ |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 19 | // RUN: | FileCheck %s -check-prefixes=NOGLOBALS,HIPNOGLOBALS |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 20 | // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s \ |
| 21 | // RUN: -fcuda-rdc -fcuda-include-gpubinary %t -o - -x hip \ |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 22 | // RUN: | FileCheck %s --check-prefixes=ALL,RDC,HIP,HIPRDC |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 23 | // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s -o - -x hip\ |
| 24 | // RUN: | FileCheck %s -check-prefix=NOGPUBIN |
| 25 | |
Eli Bendersky | 3468d9d | 2014-04-28 22:21:28 +0000 | [diff] [blame] | 26 | #include "Inputs/cuda.h" |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 27 | |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 28 | #ifndef NOGLOBALS |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 29 | // ALL-DAG: @device_var = internal global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 30 | __device__ int device_var; |
| 31 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 32 | // ALL-DAG: @constant_var = internal global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 33 | __constant__ int constant_var; |
| 34 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 35 | // ALL-DAG: @shared_var = internal global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 36 | __shared__ int shared_var; |
| 37 | |
| 38 | // Make sure host globals don't get internalized... |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 39 | // ALL-DAG: @host_var = global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 40 | int host_var; |
| 41 | // ... and that extern vars remain external. |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 42 | // ALL-DAG: @ext_host_var = external global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 43 | extern int ext_host_var; |
| 44 | |
| 45 | // Shadows for external device-side variables are *definitions* of |
| 46 | // those variables. |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 47 | // ALL-DAG: @ext_device_var = internal global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 48 | extern __device__ int ext_device_var; |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 49 | // ALL-DAG: @ext_device_var = internal global i32 |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 50 | extern __constant__ int ext_constant_var; |
| 51 | |
| 52 | void use_pointers() { |
| 53 | int *p; |
| 54 | p = &device_var; |
| 55 | p = &constant_var; |
| 56 | p = &shared_var; |
| 57 | p = &host_var; |
| 58 | p = &ext_device_var; |
| 59 | p = &ext_constant_var; |
| 60 | p = &ext_host_var; |
| 61 | } |
| 62 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 63 | // Make sure that all parts of GPU code init/cleanup are there: |
| 64 | // * constant unnamed string with the kernel name |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 65 | // ALL: private unnamed_addr constant{{.*}}kernelfunc{{.*}}\00" |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 66 | // * constant unnamed string with GPU binary |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 67 | // HIP: @[[FATBIN:__hip_fatbin]] = external constant i8, section ".hip_fatbin" |
Jonas Hahnfeld | 3b9cbba9 | 2018-06-08 11:17:08 +0000 | [diff] [blame] | 68 | // CUDA: @[[FATBIN:.*]] = private constant{{.*GPU binary would be here.*}}\00", |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 69 | // CUDANORDC-SAME: section ".nv_fatbin", align 8 |
| 70 | // CUDARDC-SAME: section "__nv_relfatbin", align 8 |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 71 | // * constant struct that wraps GPU binary |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 72 | // ALL: @__[[PREFIX:cuda|hip]]_fatbin_wrapper = internal constant |
| 73 | // ALL-SAME: { i32, i32, i8*, i8* } |
| 74 | // CUDA-SAME: { i32 1180844977, i32 1, |
| 75 | // HIP-SAME: { i32 1212764230, i32 1, |
| 76 | // CUDA-SAME: i8* getelementptr inbounds ({{.*}}@[[FATBIN]], i64 0, i64 0), |
| 77 | // HIP-SAME: i8* @[[FATBIN]], |
| 78 | // ALL-SAME: i8* null } |
| 79 | // CUDA-SAME: section ".nvFatBinSegment" |
| 80 | // HIP-SAME: section ".hipFatBinSegment" |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 81 | // * variable to save GPU binary handle after initialization |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 82 | // NORDC: @__[[PREFIX]]_gpubin_handle = internal global i8** null |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 83 | // * constant unnamed string with NVModuleID |
Jonas Hahnfeld | 3b9cbba9 | 2018-06-08 11:17:08 +0000 | [diff] [blame] | 84 | // RDC: [[MODULE_ID_GLOBAL:@.*]] = private constant |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 85 | // CUDARDC-SAME: c"[[MODULE_ID:.+]]\00", section "__nv_module_id", align 32 |
| 86 | // HIPRDC-SAME: c"[[MODULE_ID:.+]]\00", section "__hip_module_id", align 32 |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 87 | // * Make sure our constructor was added to global ctor list. |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 88 | // ALL: @llvm.global_ctors = appending global {{.*}}@__[[PREFIX]]_module_ctor |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 89 | // * In separate mode we also register a destructor. |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 90 | // NORDC: @llvm.global_dtors = appending global {{.*}}@__[[PREFIX]]_module_dtor |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 91 | // * Alias to global symbol containing the NVModuleID. |
| 92 | // RDC: @__fatbinwrap[[MODULE_ID]] = alias { i32, i32, i8*, i8* } |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 93 | // RDC-SAME: { i32, i32, i8*, i8* }* @__[[PREFIX]]_fatbin_wrapper |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 94 | |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 95 | // Test that we build the correct number of calls to cudaSetupArgument followed |
| 96 | // by a call to cudaLaunch. |
| 97 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 98 | // ALL: define{{.*}}kernelfunc |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 99 | // ALL: call{{.*}}[[PREFIX]]SetupArgument |
| 100 | // ALL: call{{.*}}[[PREFIX]]SetupArgument |
| 101 | // ALL: call{{.*}}[[PREFIX]]SetupArgument |
| 102 | // ALL: call{{.*}}[[PREFIX]]Launch |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 103 | __global__ void kernelfunc(int i, int j, int k) {} |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 104 | |
| 105 | // Test that we've built correct kernel launch sequence. |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 106 | // ALL: define{{.*}}hostfunc |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 107 | // ALL: call{{.*}}[[PREFIX]]ConfigureCall |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 108 | // ALL: call{{.*}}kernelfunc |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 109 | void hostfunc(void) { kernelfunc<<<1, 1>>>(1, 1, 1); } |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 110 | #endif |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 111 | |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 112 | // Test that we've built a function to register kernels and global vars. |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 113 | // ALL: define internal void @__[[PREFIX]]_register_globals |
| 114 | // ALL: call{{.*}}[[PREFIX]]RegisterFunction(i8** %0, {{.*}}kernelfunc |
| 115 | // ALL-DAG: call{{.*}}[[PREFIX]]RegisterVar(i8** %0, {{.*}}device_var{{.*}}i32 0, i32 4, i32 0, i32 0 |
| 116 | // ALL-DAG: call{{.*}}[[PREFIX]]RegisterVar(i8** %0, {{.*}}constant_var{{.*}}i32 0, i32 4, i32 1, i32 0 |
| 117 | // ALL-DAG: call{{.*}}[[PREFIX]]RegisterVar(i8** %0, {{.*}}ext_device_var{{.*}}i32 1, i32 4, i32 0, i32 0 |
| 118 | // ALL-DAG: call{{.*}}[[PREFIX]]RegisterVar(i8** %0, {{.*}}ext_constant_var{{.*}}i32 1, i32 4, i32 1, i32 0 |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 119 | // ALL: ret void |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 120 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 121 | // Test that we've built a constructor. |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 122 | // ALL: define internal void @__[[PREFIX]]_module_ctor |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 123 | |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 124 | // In separate mode it calls __[[PREFIX]]RegisterFatBinary(&__[[PREFIX]]_fatbin_wrapper) |
| 125 | // NORDC: call{{.*}}[[PREFIX]]RegisterFatBinary{{.*}}__[[PREFIX]]_fatbin_wrapper |
| 126 | // .. stores return value in __[[PREFIX]]_gpubin_handle |
| 127 | // NORDC-NEXT: store{{.*}}__[[PREFIX]]_gpubin_handle |
| 128 | // .. and then calls __[[PREFIX]]_register_globals |
| 129 | // NORDC-NEXT: call void @__[[PREFIX]]_register_globals |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 130 | |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 131 | // With relocatable device code we call __[[PREFIX]]RegisterLinkedBinary%NVModuleID% |
| 132 | // RDC: call{{.*}}__[[PREFIX]]RegisterLinkedBinary[[MODULE_ID]]( |
| 133 | // RDC-SAME: __[[PREFIX]]_register_globals, {{.*}}__[[PREFIX]]_fatbin_wrapper |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 134 | // RDC-SAME: [[MODULE_ID_GLOBAL]] |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 135 | |
| 136 | // Test that we've created destructor. |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 137 | // NORDC: define internal void @__[[PREFIX]]_module_dtor |
| 138 | // NORDC: load{{.*}}__[[PREFIX]]_gpubin_handle |
| 139 | // NORDC-NEXT: call void @__[[PREFIX]]UnregisterFatBinary |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 140 | |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 141 | // There should be no __[[PREFIX]]_register_globals if we have no |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 142 | // device-side globals, but we still need to register GPU binary. |
| 143 | // Skip GPU binary string first. |
Jonas Hahnfeld | 3b9cbba9 | 2018-06-08 11:17:08 +0000 | [diff] [blame] | 144 | // CUDANOGLOBALS: @{{.*}} = private constant{{.*}} |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 145 | // HIPNOGLOBALS: @{{.*}} = external constant{{.*}} |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 146 | // NOGLOBALS-NOT: define internal void @__{{.*}}_register_globals |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 147 | // NOGLOBALS: define internal void @__[[PREFIX:cuda|hip]]_module_ctor |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 148 | // NOGLOBALS: call{{.*}}[[PREFIX]]RegisterFatBinary{{.*}}__[[PREFIX]]_fatbin_wrapper |
| 149 | // NOGLOBALS-NOT: call void @__[[PREFIX]]_register_globals |
| 150 | // NOGLOBALS: define internal void @__[[PREFIX]]_module_dtor |
| 151 | // NOGLOBALS: call void @__[[PREFIX]]UnregisterFatBinary |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 152 | |
| 153 | // There should be no constructors/destructors if we have no GPU binary. |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 154 | // NOGPUBIN-NOT: define internal void @__[[PREFIX]]_register_globals |
| 155 | // NOGPUBIN-NOT: define internal void @__[[PREFIX]]_module_ctor |
| 156 | // NOGPUBIN-NOT: define internal void @__[[PREFIX]]_module_dtor |