__clang_cuda_runtime_wrapper.h revision 309124
1292920Sdim/*===---- __clang_cuda_runtime_wrapper.h - CUDA runtime support -------------===
2292920Sdim *
3292920Sdim * Permission is hereby granted, free of charge, to any person obtaining a copy
4292920Sdim * of this software and associated documentation files (the "Software"), to deal
5292920Sdim * in the Software without restriction, including without limitation the rights
6292920Sdim * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7292920Sdim * copies of the Software, and to permit persons to whom the Software is
8292920Sdim * furnished to do so, subject to the following conditions:
9292920Sdim *
10292920Sdim * The above copyright notice and this permission notice shall be included in
11292920Sdim * all copies or substantial portions of the Software.
12292920Sdim *
13292920Sdim * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14292920Sdim * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15292920Sdim * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16292920Sdim * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17292920Sdim * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18292920Sdim * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19292920Sdim * THE SOFTWARE.
20292920Sdim *
21292920Sdim *===-----------------------------------------------------------------------===
22292920Sdim */
23292920Sdim
24292920Sdim/*
25292920Sdim * WARNING: This header is intended to be directly -include'd by
26292920Sdim * the compiler and is not supposed to be included by users.
27292920Sdim *
28292920Sdim * CUDA headers are implemented in a way that currently makes it
29292920Sdim * impossible for user code to #include directly when compiling with
30292920Sdim * Clang. They present different view of CUDA-supplied functions
31292920Sdim * depending on where in NVCC's compilation pipeline the headers are
32292920Sdim * included. Neither of these modes provides function definitions with
33292920Sdim * correct attributes, so we use preprocessor to force the headers
34292920Sdim * into a form that Clang can use.
35292920Sdim *
36292920Sdim * Similarly to NVCC which -include's cuda_runtime.h, Clang -include's
37292920Sdim * this file during every CUDA compilation.
38292920Sdim */
39292920Sdim
40292920Sdim#ifndef __CLANG_CUDA_RUNTIME_WRAPPER_H__
41292920Sdim#define __CLANG_CUDA_RUNTIME_WRAPPER_H__
42292920Sdim
43292920Sdim#if defined(__CUDA__) && defined(__clang__)
44292920Sdim
45309124Sdim// Include some forward declares that must come before cmath.
46309124Sdim#include <__clang_cuda_math_forward_declares.h>
47309124Sdim
48292920Sdim// Include some standard headers to avoid CUDA headers including them
49292920Sdim// while some required macros (like __THROW) are in a weird state.
50309124Sdim#include <cmath>
51309124Sdim#include <cstdlib>
52292920Sdim#include <stdlib.h>
53292920Sdim
54292920Sdim// Preserve common macros that will be changed below by us or by CUDA
55292920Sdim// headers.
56292920Sdim#pragma push_macro("__THROW")
57292920Sdim#pragma push_macro("__CUDA_ARCH__")
58292920Sdim
59292920Sdim// WARNING: Preprocessor hacks below are based on specific details of
60292920Sdim// CUDA-7.x headers and are not expected to work with any other
61292920Sdim// version of CUDA headers.
62292920Sdim#include "cuda.h"
63292920Sdim#if !defined(CUDA_VERSION)
64292920Sdim#error "cuda.h did not define CUDA_VERSION"
65292920Sdim#elif CUDA_VERSION < 7000 || CUDA_VERSION > 7050
66292920Sdim#error "Unsupported CUDA version!"
67292920Sdim#endif
68292920Sdim
69292920Sdim// Make largest subset of device functions available during host
70292920Sdim// compilation -- SM_35 for the time being.
71292920Sdim#ifndef __CUDA_ARCH__
72292920Sdim#define __CUDA_ARCH__ 350
73292920Sdim#endif
74292920Sdim
75292920Sdim#include "cuda_builtin_vars.h"
76292920Sdim
77292920Sdim// No need for device_launch_parameters.h as cuda_builtin_vars.h above
78292920Sdim// has taken care of builtin variables declared in the file.
79292920Sdim#define __DEVICE_LAUNCH_PARAMETERS_H__
80292920Sdim
81292920Sdim// {math,device}_functions.h only have declarations of the
82292920Sdim// functions. We don't need them as we're going to pull in their
83292920Sdim// definitions from .hpp files.
84292920Sdim#define __DEVICE_FUNCTIONS_H__
85292920Sdim#define __MATH_FUNCTIONS_H__
86309124Sdim#define __COMMON_FUNCTIONS_H__
87292920Sdim
88292920Sdim#undef __CUDACC__
89292920Sdim#define __CUDABE__
90292920Sdim// Disables definitions of device-side runtime support stubs in
91292920Sdim// cuda_device_runtime_api.h
92309124Sdim#include "driver_types.h"
93292920Sdim#include "host_config.h"
94292920Sdim#include "host_defines.h"
95292920Sdim
96292920Sdim#undef __CUDABE__
97292920Sdim#define __CUDACC__
98292920Sdim#include "cuda_runtime.h"
99292920Sdim
100292920Sdim#undef __CUDACC__
101292920Sdim#define __CUDABE__
102292920Sdim
103292920Sdim// CUDA headers use __nvvm_memcpy and __nvvm_memset which Clang does
104292920Sdim// not have at the moment. Emulate them with a builtin memcpy/memset.
105309124Sdim#define __nvvm_memcpy(s, d, n, a) __builtin_memcpy(s, d, n)
106309124Sdim#define __nvvm_memset(d, c, n, a) __builtin_memset(d, c, n)
107292920Sdim
108309124Sdim#include "crt/device_runtime.h"
109292920Sdim#include "crt/host_runtime.h"
110292920Sdim// device_runtime.h defines __cxa_* macros that will conflict with
111292920Sdim// cxxabi.h.
112292920Sdim// FIXME: redefine these as __device__ functions.
113292920Sdim#undef __cxa_vec_ctor
114292920Sdim#undef __cxa_vec_cctor
115292920Sdim#undef __cxa_vec_dtor
116292920Sdim#undef __cxa_vec_new2
117292920Sdim#undef __cxa_vec_new3
118292920Sdim#undef __cxa_vec_delete2
119292920Sdim#undef __cxa_vec_delete
120292920Sdim#undef __cxa_vec_delete3
121292920Sdim#undef __cxa_pure_virtual
122292920Sdim
123292920Sdim// We need decls for functions in CUDA's libdevice with __device__
124292920Sdim// attribute only. Alas they come either as __host__ __device__ or
125292920Sdim// with no attributes at all. To work around that, define __CUDA_RTC__
126292920Sdim// which produces HD variant and undef __host__ which gives us desided
127292920Sdim// decls with __device__ attribute.
128292920Sdim#pragma push_macro("__host__")
129292920Sdim#define __host__
130292920Sdim#define __CUDACC_RTC__
131292920Sdim#include "device_functions_decls.h"
132292920Sdim#undef __CUDACC_RTC__
133292920Sdim
134292920Sdim// Temporarily poison __host__ macro to ensure it's not used by any of
135292920Sdim// the headers we're about to include.
136292920Sdim#define __host__ UNEXPECTED_HOST_ATTRIBUTE
137292920Sdim
138292920Sdim// device_functions.hpp and math_functions*.hpp use 'static
139292920Sdim// __forceinline__' (with no __device__) for definitions of device
140292920Sdim// functions. Temporarily redefine __forceinline__ to include
141292920Sdim// __device__.
142292920Sdim#pragma push_macro("__forceinline__")
143292920Sdim#define __forceinline__ __device__ __inline__ __attribute__((always_inline))
144292920Sdim#include "device_functions.hpp"
145309124Sdim
146309124Sdim// math_function.hpp uses the __USE_FAST_MATH__ macro to determine whether we
147309124Sdim// get the slow-but-accurate or fast-but-inaccurate versions of functions like
148309124Sdim// sin and exp.  This is controlled in clang by -fcuda-approx-transcendentals.
149309124Sdim//
150309124Sdim// device_functions.hpp uses __USE_FAST_MATH__ for a different purpose (fast vs.
151309124Sdim// slow divides), so we need to scope our define carefully here.
152309124Sdim#pragma push_macro("__USE_FAST_MATH__")
153309124Sdim#if defined(__CLANG_CUDA_APPROX_TRANSCENDENTALS__)
154309124Sdim#define __USE_FAST_MATH__
155309124Sdim#endif
156292920Sdim#include "math_functions.hpp"
157309124Sdim#pragma pop_macro("__USE_FAST_MATH__")
158309124Sdim
159292920Sdim#include "math_functions_dbl_ptx3.hpp"
160292920Sdim#pragma pop_macro("__forceinline__")
161292920Sdim
162292920Sdim// Pull in host-only functions that are only available when neither
163292920Sdim// __CUDACC__ nor __CUDABE__ are defined.
164292920Sdim#undef __MATH_FUNCTIONS_HPP__
165292920Sdim#undef __CUDABE__
166292920Sdim#include "math_functions.hpp"
167292920Sdim// Alas, additional overloads for these functions are hard to get to.
168292920Sdim// Considering that we only need these overloads for a few functions,
169292920Sdim// we can provide them here.
170309124Sdimstatic inline float rsqrt(float __a) { return rsqrtf(__a); }
171309124Sdimstatic inline float rcbrt(float __a) { return rcbrtf(__a); }
172309124Sdimstatic inline float sinpi(float __a) { return sinpif(__a); }
173309124Sdimstatic inline float cospi(float __a) { return cospif(__a); }
174309124Sdimstatic inline void sincospi(float __a, float *__b, float *__c) {
175309124Sdim  return sincospif(__a, __b, __c);
176292920Sdim}
177309124Sdimstatic inline float erfcinv(float __a) { return erfcinvf(__a); }
178309124Sdimstatic inline float normcdfinv(float __a) { return normcdfinvf(__a); }
179309124Sdimstatic inline float normcdf(float __a) { return normcdff(__a); }
180309124Sdimstatic inline float erfcx(float __a) { return erfcxf(__a); }
181292920Sdim
182292920Sdim// For some reason single-argument variant is not always declared by
183292920Sdim// CUDA headers. Alas, device_functions.hpp included below needs it.
184309124Sdimstatic inline __device__ void __brkpt(int __c) { __brkpt(); }
185292920Sdim
186292920Sdim// Now include *.hpp with definitions of various GPU functions.  Alas,
187292920Sdim// a lot of thins get declared/defined with __host__ attribute which
188292920Sdim// we don't want and we have to define it out. We also have to include
189292920Sdim// {device,math}_functions.hpp again in order to extract the other
190292920Sdim// branch of #if/else inside.
191292920Sdim
192292920Sdim#define __host__
193292920Sdim#undef __CUDABE__
194292920Sdim#define __CUDACC__
195292920Sdim#undef __DEVICE_FUNCTIONS_HPP__
196309124Sdim#include "device_atomic_functions.hpp"
197292920Sdim#include "device_functions.hpp"
198292920Sdim#include "sm_20_atomic_functions.hpp"
199309124Sdim#include "sm_20_intrinsics.hpp"
200292920Sdim#include "sm_32_atomic_functions.hpp"
201309124Sdim
202309124Sdim// Don't include sm_30_intrinsics.h and sm_32_intrinsics.h.  These define the
203309124Sdim// __shfl and __ldg intrinsics using inline (volatile) asm, but we want to
204309124Sdim// define them using builtins so that the optimizer can reason about and across
205309124Sdim// these instructions.  In particular, using intrinsics for ldg gets us the
206309124Sdim// [addr+imm] addressing mode, which, although it doesn't actually exist in the
207309124Sdim// hardware, seems to generate faster machine code because ptxas can more easily
208309124Sdim// reason about our code.
209309124Sdim
210292920Sdim#undef __MATH_FUNCTIONS_HPP__
211309124Sdim
212309124Sdim// math_functions.hpp defines ::signbit as a __host__ __device__ function.  This
213309124Sdim// conflicts with libstdc++'s constexpr ::signbit, so we have to rename
214309124Sdim// math_function.hpp's ::signbit.  It's guarded by #undef signbit, but that's
215309124Sdim// conditional on __GNUC__.  :)
216309124Sdim#pragma push_macro("signbit")
217309124Sdim#pragma push_macro("__GNUC__")
218309124Sdim#undef __GNUC__
219309124Sdim#define signbit __ignored_cuda_signbit
220292920Sdim#include "math_functions.hpp"
221309124Sdim#pragma pop_macro("__GNUC__")
222309124Sdim#pragma pop_macro("signbit")
223309124Sdim
224292920Sdim#pragma pop_macro("__host__")
225292920Sdim
226292920Sdim#include "texture_indirect_functions.h"
227292920Sdim
228292920Sdim// Restore state of __CUDA_ARCH__ and __THROW we had on entry.
229292920Sdim#pragma pop_macro("__CUDA_ARCH__")
230292920Sdim#pragma pop_macro("__THROW")
231292920Sdim
232292920Sdim// Set up compiler macros expected to be seen during compilation.
233292920Sdim#undef __CUDABE__
234292920Sdim#define __CUDACC__
235292920Sdim
236309124Sdimextern "C" {
237309124Sdim// Device-side CUDA system calls.
238309124Sdim// http://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/index.html#system-calls
239309124Sdim// We need these declarations and wrappers for device-side
240309124Sdim// malloc/free/printf calls to work without relying on
241309124Sdim// -fcuda-disable-target-call-checks option.
242309124Sdim__device__ int vprintf(const char *, const char *);
243309124Sdim__device__ void free(void *) __attribute((nothrow));
244309124Sdim__device__ void *malloc(size_t) __attribute((nothrow)) __attribute__((malloc));
245309124Sdim__device__ void __assertfail(const char *__message, const char *__file,
246309124Sdim                             unsigned __line, const char *__function,
247309124Sdim                             size_t __charSize) __attribute__((noreturn));
248309124Sdim
249309124Sdim// In order for standard assert() macro on linux to work we need to
250309124Sdim// provide device-side __assert_fail()
251309124Sdim__device__ static inline void __assert_fail(const char *__message,
252309124Sdim                                            const char *__file, unsigned __line,
253309124Sdim                                            const char *__function) {
254309124Sdim  __assertfail(__message, __file, __line, __function, sizeof(char));
255292920Sdim}
256292920Sdim
257309124Sdim// Clang will convert printf into vprintf, but we still need
258309124Sdim// device-side declaration for it.
259309124Sdim__device__ int printf(const char *, ...);
260309124Sdim} // extern "C"
261309124Sdim
262309124Sdim// We also need device-side std::malloc and std::free.
263309124Sdimnamespace std {
264309124Sdim__device__ static inline void free(void *__ptr) { ::free(__ptr); }
265309124Sdim__device__ static inline void *malloc(size_t __size) {
266309124Sdim  return ::malloc(__size);
267309124Sdim}
268309124Sdim} // namespace std
269309124Sdim
270309124Sdim// Out-of-line implementations from cuda_builtin_vars.h.  These need to come
271309124Sdim// after we've pulled in the definition of uint3 and dim3.
272309124Sdim
273309124Sdim__device__ inline __cuda_builtin_threadIdx_t::operator uint3() const {
274309124Sdim  uint3 ret;
275309124Sdim  ret.x = x;
276309124Sdim  ret.y = y;
277309124Sdim  ret.z = z;
278309124Sdim  return ret;
279309124Sdim}
280309124Sdim
281309124Sdim__device__ inline __cuda_builtin_blockIdx_t::operator uint3() const {
282309124Sdim  uint3 ret;
283309124Sdim  ret.x = x;
284309124Sdim  ret.y = y;
285309124Sdim  ret.z = z;
286309124Sdim  return ret;
287309124Sdim}
288309124Sdim
289309124Sdim__device__ inline __cuda_builtin_blockDim_t::operator dim3() const {
290309124Sdim  return dim3(x, y, z);
291309124Sdim}
292309124Sdim
293309124Sdim__device__ inline __cuda_builtin_gridDim_t::operator dim3() const {
294309124Sdim  return dim3(x, y, z);
295309124Sdim}
296309124Sdim
297309124Sdim#include <__clang_cuda_cmath.h>
298309124Sdim#include <__clang_cuda_intrinsics.h>
299309124Sdim
300309124Sdim// curand_mtgp32_kernel helpfully redeclares blockDim and threadIdx in host
301309124Sdim// mode, giving them their "proper" types of dim3 and uint3.  This is
302309124Sdim// incompatible with the types we give in cuda_builtin_vars.h.  As as hack,
303309124Sdim// force-include the header (nvcc doesn't include it by default) but redefine
304309124Sdim// dim3 and uint3 to our builtin types.  (Thankfully dim3 and uint3 are only
305309124Sdim// used here for the redeclarations of blockDim and threadIdx.)
306309124Sdim#pragma push_macro("dim3")
307309124Sdim#pragma push_macro("uint3")
308309124Sdim#define dim3 __cuda_builtin_blockDim_t
309309124Sdim#define uint3 __cuda_builtin_threadIdx_t
310309124Sdim#include "curand_mtgp32_kernel.h"
311309124Sdim#pragma pop_macro("dim3")
312309124Sdim#pragma pop_macro("uint3")
313309124Sdim#pragma pop_macro("__USE_FAST_MATH__")
314309124Sdim
315292920Sdim#endif // __CUDA__
316292920Sdim#endif // __CLANG_CUDA_RUNTIME_WRAPPER_H__
317