blob: 95d1f5fa0112a34d65da7c51dadafe068c5078c4 [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +01001/*===---- __clang_cuda_runtime_wrapper.h - CUDA runtime support -------------===
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 *
21 *===-----------------------------------------------------------------------===
22 */
23
24/*
25 * WARNING: This header is intended to be directly -include'd by
26 * the compiler and is not supposed to be included by users.
27 *
28 * CUDA headers are implemented in a way that currently makes it
29 * impossible for user code to #include directly when compiling with
30 * Clang. They present different view of CUDA-supplied functions
31 * depending on where in NVCC's compilation pipeline the headers are
32 * included. Neither of these modes provides function definitions with
33 * correct attributes, so we use preprocessor to force the headers
34 * into a form that Clang can use.
35 *
36 * Similarly to NVCC which -include's cuda_runtime.h, Clang -include's
37 * this file during every CUDA compilation.
38 */
39
40#ifndef __CLANG_CUDA_RUNTIME_WRAPPER_H__
41#define __CLANG_CUDA_RUNTIME_WRAPPER_H__
42
43#if defined(__CUDA__) && defined(__clang__)
44
Ben Murdoch097c5b22016-05-18 11:27:45 +010045// Include some standard headers to avoid CUDA headers including them
46// while some required macros (like __THROW) are in a weird state.
47#include <cmath>
48#include <cstdlib>
49#include <stdlib.h>
50
51// Preserve common macros that will be changed below by us or by CUDA
52// headers.
53#pragma push_macro("__THROW")
54#pragma push_macro("__CUDA_ARCH__")
55
56// WARNING: Preprocessor hacks below are based on specific details of
57// CUDA-7.x headers and are not expected to work with any other
58// version of CUDA headers.
59#include "cuda.h"
60#if !defined(CUDA_VERSION)
61#error "cuda.h did not define CUDA_VERSION"
62#elif CUDA_VERSION < 7000 || CUDA_VERSION > 7050
63#error "Unsupported CUDA version!"
64#endif
65
66// Make largest subset of device functions available during host
67// compilation -- SM_35 for the time being.
68#ifndef __CUDA_ARCH__
69#define __CUDA_ARCH__ 350
70#endif
71
72#include "cuda_builtin_vars.h"
73
74// No need for device_launch_parameters.h as cuda_builtin_vars.h above
75// has taken care of builtin variables declared in the file.
76#define __DEVICE_LAUNCH_PARAMETERS_H__
77
78// {math,device}_functions.h only have declarations of the
79// functions. We don't need them as we're going to pull in their
80// definitions from .hpp files.
81#define __DEVICE_FUNCTIONS_H__
82#define __MATH_FUNCTIONS_H__
83#define __COMMON_FUNCTIONS_H__
84
85#undef __CUDACC__
86#define __CUDABE__
87// Disables definitions of device-side runtime support stubs in
88// cuda_device_runtime_api.h
89#include "driver_types.h"
90#include "host_config.h"
91#include "host_defines.h"
92
93#undef __CUDABE__
94#define __CUDACC__
95#include "cuda_runtime.h"
96
97#undef __CUDACC__
98#define __CUDABE__
99
100// CUDA headers use __nvvm_memcpy and __nvvm_memset which Clang does
101// not have at the moment. Emulate them with a builtin memcpy/memset.
102#define __nvvm_memcpy(s, d, n, a) __builtin_memcpy(s, d, n)
103#define __nvvm_memset(d, c, n, a) __builtin_memset(d, c, n)
104
105#include "crt/device_runtime.h"
106#include "crt/host_runtime.h"
107// device_runtime.h defines __cxa_* macros that will conflict with
108// cxxabi.h.
109// FIXME: redefine these as __device__ functions.
110#undef __cxa_vec_ctor
111#undef __cxa_vec_cctor
112#undef __cxa_vec_dtor
113#undef __cxa_vec_new2
114#undef __cxa_vec_new3
115#undef __cxa_vec_delete2
116#undef __cxa_vec_delete
117#undef __cxa_vec_delete3
118#undef __cxa_pure_virtual
119
120// We need decls for functions in CUDA's libdevice with __device__
121// attribute only. Alas they come either as __host__ __device__ or
122// with no attributes at all. To work around that, define __CUDA_RTC__
123// which produces HD variant and undef __host__ which gives us desided
124// decls with __device__ attribute.
125#pragma push_macro("__host__")
126#define __host__
127#define __CUDACC_RTC__
128#include "device_functions_decls.h"
129#undef __CUDACC_RTC__
130
131// Temporarily poison __host__ macro to ensure it's not used by any of
132// the headers we're about to include.
133#define __host__ UNEXPECTED_HOST_ATTRIBUTE
134
135// device_functions.hpp and math_functions*.hpp use 'static
136// __forceinline__' (with no __device__) for definitions of device
137// functions. Temporarily redefine __forceinline__ to include
138// __device__.
139#pragma push_macro("__forceinline__")
140#define __forceinline__ __device__ __inline__ __attribute__((always_inline))
141#include "device_functions.hpp"
142#include "math_functions.hpp"
143#include "math_functions_dbl_ptx3.hpp"
144#pragma pop_macro("__forceinline__")
145
146// Pull in host-only functions that are only available when neither
147// __CUDACC__ nor __CUDABE__ are defined.
148#undef __MATH_FUNCTIONS_HPP__
149#undef __CUDABE__
150#include "math_functions.hpp"
151// Alas, additional overloads for these functions are hard to get to.
152// Considering that we only need these overloads for a few functions,
153// we can provide them here.
154static inline float rsqrt(float __a) { return rsqrtf(__a); }
155static inline float rcbrt(float __a) { return rcbrtf(__a); }
156static inline float sinpi(float __a) { return sinpif(__a); }
157static inline float cospi(float __a) { return cospif(__a); }
158static inline void sincospi(float __a, float *__b, float *__c) {
159 return sincospif(__a, __b, __c);
160}
161static inline float erfcinv(float __a) { return erfcinvf(__a); }
162static inline float normcdfinv(float __a) { return normcdfinvf(__a); }
163static inline float normcdf(float __a) { return normcdff(__a); }
164static inline float erfcx(float __a) { return erfcxf(__a); }
165
166// For some reason single-argument variant is not always declared by
167// CUDA headers. Alas, device_functions.hpp included below needs it.
168static inline __device__ void __brkpt(int __c) { __brkpt(); }
169
170// Now include *.hpp with definitions of various GPU functions. Alas,
171// a lot of thins get declared/defined with __host__ attribute which
172// we don't want and we have to define it out. We also have to include
173// {device,math}_functions.hpp again in order to extract the other
174// branch of #if/else inside.
175
176#define __host__
177#undef __CUDABE__
178#define __CUDACC__
179#undef __DEVICE_FUNCTIONS_HPP__
180#include "device_atomic_functions.hpp"
181#include "device_functions.hpp"
182#include "sm_20_atomic_functions.hpp"
183#include "sm_20_intrinsics.hpp"
184#include "sm_32_atomic_functions.hpp"
185// sm_30_intrinsics.h has declarations that use default argument, so
186// we have to include it and it will in turn include .hpp
187#include "sm_30_intrinsics.h"
188#include "sm_32_intrinsics.hpp"
Ben Murdoch097c5b22016-05-18 11:27:45 +0100189#undef __MATH_FUNCTIONS_HPP__
Ben Murdoch097c5b22016-05-18 11:27:45 +0100190#include "math_functions.hpp"
Ben Murdoch097c5b22016-05-18 11:27:45 +0100191#pragma pop_macro("__host__")
192
193#include "texture_indirect_functions.h"
194
195// Restore state of __CUDA_ARCH__ and __THROW we had on entry.
196#pragma pop_macro("__CUDA_ARCH__")
197#pragma pop_macro("__THROW")
198
199// Set up compiler macros expected to be seen during compilation.
200#undef __CUDABE__
201#define __CUDACC__
202
Ben Murdochc5610432016-08-08 18:44:38 +0100203#if defined(__CUDA_ARCH__)
204// We need to emit IR declaration for non-existing __nvvm_reflect() to
205// let backend know that it should be treated as const nothrow
206// function which is what NVVMReflect pass expects to see.
207extern "C" __device__ __attribute__((const)) int __nvvm_reflect(const void *);
208static __device__ __attribute__((used)) int __nvvm_reflect_anchor() {
209 return __nvvm_reflect("NONE");
210}
211#endif
212
Ben Murdoch097c5b22016-05-18 11:27:45 +0100213extern "C" {
214// Device-side CUDA system calls.
215// http://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/index.html#system-calls
216// We need these declarations and wrappers for device-side
217// malloc/free/printf calls to work without relying on
218// -fcuda-disable-target-call-checks option.
219__device__ int vprintf(const char *, const char *);
220__device__ void free(void *) __attribute((nothrow));
221__device__ void *malloc(size_t) __attribute((nothrow)) __attribute__((malloc));
222__device__ void __assertfail(const char *__message, const char *__file,
223 unsigned __line, const char *__function,
224 size_t __charSize) __attribute__((noreturn));
225
226// In order for standard assert() macro on linux to work we need to
227// provide device-side __assert_fail()
228__device__ static inline void __assert_fail(const char *__message,
229 const char *__file, unsigned __line,
230 const char *__function) {
231 __assertfail(__message, __file, __line, __function, sizeof(char));
232}
233
234// Clang will convert printf into vprintf, but we still need
235// device-side declaration for it.
236__device__ int printf(const char *, ...);
237} // extern "C"
238
239// We also need device-side std::malloc and std::free.
240namespace std {
241__device__ static inline void free(void *__ptr) { ::free(__ptr); }
242__device__ static inline void *malloc(size_t __size) {
243 return ::malloc(__size);
244}
245} // namespace std
246
247// Out-of-line implementations from cuda_builtin_vars.h. These need to come
248// after we've pulled in the definition of uint3 and dim3.
249
250__device__ inline __cuda_builtin_threadIdx_t::operator uint3() const {
251 uint3 ret;
252 ret.x = x;
253 ret.y = y;
254 ret.z = z;
255 return ret;
256}
257
258__device__ inline __cuda_builtin_blockIdx_t::operator uint3() const {
259 uint3 ret;
260 ret.x = x;
261 ret.y = y;
262 ret.z = z;
263 return ret;
264}
265
266__device__ inline __cuda_builtin_blockDim_t::operator dim3() const {
267 return dim3(x, y, z);
268}
269
270__device__ inline __cuda_builtin_gridDim_t::operator dim3() const {
271 return dim3(x, y, z);
272}
273
274#include <__clang_cuda_cmath.h>
275
276// curand_mtgp32_kernel helpfully redeclares blockDim and threadIdx in host
277// mode, giving them their "proper" types of dim3 and uint3. This is
278// incompatible with the types we give in cuda_builtin_vars.h. As as hack,
279// force-include the header (nvcc doesn't include it by default) but redefine
280// dim3 and uint3 to our builtin types. (Thankfully dim3 and uint3 are only
281// used here for the redeclarations of blockDim and threadIdx.)
282#pragma push_macro("dim3")
283#pragma push_macro("uint3")
284#define dim3 __cuda_builtin_blockDim_t
285#define uint3 __cuda_builtin_threadIdx_t
286#include "curand_mtgp32_kernel.h"
287#pragma pop_macro("dim3")
288#pragma pop_macro("uint3")
289
290#endif // __CUDA__
291#endif // __CLANG_CUDA_RUNTIME_WRAPPER_H__