10b57cec5SDimitry Andric /*===---- __clang_cuda_runtime_wrapper.h - CUDA runtime support -------------===
20b57cec5SDimitry Andric  *
30b57cec5SDimitry Andric  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric  * See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric  *
70b57cec5SDimitry Andric  *===-----------------------------------------------------------------------===
80b57cec5SDimitry Andric  */
90b57cec5SDimitry Andric 
100b57cec5SDimitry Andric /*
110b57cec5SDimitry Andric  * WARNING: This header is intended to be directly -include'd by
120b57cec5SDimitry Andric  * the compiler and is not supposed to be included by users.
130b57cec5SDimitry Andric  *
140b57cec5SDimitry Andric  * CUDA headers are implemented in a way that currently makes it
150b57cec5SDimitry Andric  * impossible for user code to #include directly when compiling with
160b57cec5SDimitry Andric  * Clang. They present different view of CUDA-supplied functions
170b57cec5SDimitry Andric  * depending on where in NVCC's compilation pipeline the headers are
180b57cec5SDimitry Andric  * included. Neither of these modes provides function definitions with
190b57cec5SDimitry Andric  * correct attributes, so we use preprocessor to force the headers
200b57cec5SDimitry Andric  * into a form that Clang can use.
210b57cec5SDimitry Andric  *
220b57cec5SDimitry Andric  * Similarly to NVCC which -include's cuda_runtime.h, Clang -include's
230b57cec5SDimitry Andric  * this file during every CUDA compilation.
240b57cec5SDimitry Andric  */
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric #ifndef __CLANG_CUDA_RUNTIME_WRAPPER_H__
270b57cec5SDimitry Andric #define __CLANG_CUDA_RUNTIME_WRAPPER_H__
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric #if defined(__CUDA__) && defined(__clang__)
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric // Include some forward declares that must come before cmath.
320b57cec5SDimitry Andric #include <__clang_cuda_math_forward_declares.h>
330b57cec5SDimitry Andric 
345ffd83dbSDimitry Andric // Define __CUDACC__ early as libstdc++ standard headers with GNU extensions
355ffd83dbSDimitry Andric // enabled depend on it to avoid using __float128, which is unsupported in
365ffd83dbSDimitry Andric // CUDA.
375ffd83dbSDimitry Andric #define __CUDACC__
385ffd83dbSDimitry Andric 
390b57cec5SDimitry Andric // Include some standard headers to avoid CUDA headers including them
400b57cec5SDimitry Andric // while some required macros (like __THROW) are in a weird state.
410b57cec5SDimitry Andric #include <cmath>
420b57cec5SDimitry Andric #include <cstdlib>
430b57cec5SDimitry Andric #include <stdlib.h>
44349cc55cSDimitry Andric #include <string.h>
455ffd83dbSDimitry Andric #undef __CUDACC__
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric // Preserve common macros that will be changed below by us or by CUDA
480b57cec5SDimitry Andric // headers.
490b57cec5SDimitry Andric #pragma push_macro("__THROW")
500b57cec5SDimitry Andric #pragma push_macro("__CUDA_ARCH__")
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric // WARNING: Preprocessor hacks below are based on specific details of
530b57cec5SDimitry Andric // CUDA-7.x headers and are not expected to work with any other
540b57cec5SDimitry Andric // version of CUDA headers.
550b57cec5SDimitry Andric #include "cuda.h"
560b57cec5SDimitry Andric #if !defined(CUDA_VERSION)
570b57cec5SDimitry Andric #error "cuda.h did not define CUDA_VERSION"
5813138422SDimitry Andric #elif CUDA_VERSION < 7000
590b57cec5SDimitry Andric #error "Unsupported CUDA version!"
600b57cec5SDimitry Andric #endif
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric #pragma push_macro("__CUDA_INCLUDE_COMPILER_INTERNAL_HEADERS__")
630b57cec5SDimitry Andric #if CUDA_VERSION >= 10000
640b57cec5SDimitry Andric #define __CUDA_INCLUDE_COMPILER_INTERNAL_HEADERS__
650b57cec5SDimitry Andric #endif
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric // Make largest subset of device functions available during host
68349cc55cSDimitry Andric // compilation.
690b57cec5SDimitry Andric #ifndef __CUDA_ARCH__
70349cc55cSDimitry Andric #define __CUDA_ARCH__ 9999
710b57cec5SDimitry Andric #endif
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric #include "__clang_cuda_builtin_vars.h"
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric // No need for device_launch_parameters.h as __clang_cuda_builtin_vars.h above
760b57cec5SDimitry Andric // has taken care of builtin variables declared in the file.
770b57cec5SDimitry Andric #define __DEVICE_LAUNCH_PARAMETERS_H__
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric // {math,device}_functions.h only have declarations of the
800b57cec5SDimitry Andric // functions. We don't need them as we're going to pull in their
810b57cec5SDimitry Andric // definitions from .hpp files.
820b57cec5SDimitry Andric #define __DEVICE_FUNCTIONS_H__
830b57cec5SDimitry Andric #define __MATH_FUNCTIONS_H__
840b57cec5SDimitry Andric #define __COMMON_FUNCTIONS_H__
850b57cec5SDimitry Andric // device_functions_decls is replaced by __clang_cuda_device_functions.h
860b57cec5SDimitry Andric // included below.
870b57cec5SDimitry Andric #define __DEVICE_FUNCTIONS_DECLS_H__
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric #undef __CUDACC__
900b57cec5SDimitry Andric #if CUDA_VERSION < 9000
910b57cec5SDimitry Andric #define __CUDABE__
920b57cec5SDimitry Andric #else
935ffd83dbSDimitry Andric #define __CUDACC__
940b57cec5SDimitry Andric #define __CUDA_LIBDEVICE__
950b57cec5SDimitry Andric #endif
960b57cec5SDimitry Andric // Disables definitions of device-side runtime support stubs in
970b57cec5SDimitry Andric // cuda_device_runtime_api.h
985ffd83dbSDimitry Andric #include "host_defines.h"
995ffd83dbSDimitry Andric #undef __CUDACC__
1000b57cec5SDimitry Andric #include "driver_types.h"
1010b57cec5SDimitry Andric #include "host_config.h"
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric // Temporarily replace "nv_weak" with weak, so __attribute__((nv_weak)) in
1040b57cec5SDimitry Andric // cuda_device_runtime_api.h ends up being __attribute__((weak)) which is the
1050b57cec5SDimitry Andric // functional equivalent of what we need.
1060b57cec5SDimitry Andric #pragma push_macro("nv_weak")
1070b57cec5SDimitry Andric #define nv_weak weak
1080b57cec5SDimitry Andric #undef __CUDABE__
1090b57cec5SDimitry Andric #undef __CUDA_LIBDEVICE__
1100b57cec5SDimitry Andric #define __CUDACC__
1110b57cec5SDimitry Andric #include "cuda_runtime.h"
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric #pragma pop_macro("nv_weak")
1140b57cec5SDimitry Andric #undef __CUDACC__
1150b57cec5SDimitry Andric #define __CUDABE__
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric // CUDA headers use __nvvm_memcpy and __nvvm_memset which Clang does
1180b57cec5SDimitry Andric // not have at the moment. Emulate them with a builtin memcpy/memset.
1190b57cec5SDimitry Andric #define __nvvm_memcpy(s, d, n, a) __builtin_memcpy(s, d, n)
1200b57cec5SDimitry Andric #define __nvvm_memset(d, c, n, a) __builtin_memset(d, c, n)
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric #if CUDA_VERSION < 9000
1230b57cec5SDimitry Andric #include "crt/device_runtime.h"
1240b57cec5SDimitry Andric #endif
1250b57cec5SDimitry Andric #include "crt/host_runtime.h"
1260b57cec5SDimitry Andric // device_runtime.h defines __cxa_* macros that will conflict with
1270b57cec5SDimitry Andric // cxxabi.h.
1280b57cec5SDimitry Andric // FIXME: redefine these as __device__ functions.
1290b57cec5SDimitry Andric #undef __cxa_vec_ctor
1300b57cec5SDimitry Andric #undef __cxa_vec_cctor
1310b57cec5SDimitry Andric #undef __cxa_vec_dtor
1320b57cec5SDimitry Andric #undef __cxa_vec_new
1330b57cec5SDimitry Andric #undef __cxa_vec_new2
1340b57cec5SDimitry Andric #undef __cxa_vec_new3
1350b57cec5SDimitry Andric #undef __cxa_vec_delete2
1360b57cec5SDimitry Andric #undef __cxa_vec_delete
1370b57cec5SDimitry Andric #undef __cxa_vec_delete3
1380b57cec5SDimitry Andric #undef __cxa_pure_virtual
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric // math_functions.hpp expects this host function be defined on MacOS, but it
1410b57cec5SDimitry Andric // ends up not being there because of the games we play here.  Just define it
1420b57cec5SDimitry Andric // ourselves; it's simple enough.
1430b57cec5SDimitry Andric #ifdef __APPLE__
__signbitd(double x)1440b57cec5SDimitry Andric inline __host__ double __signbitd(double x) {
1450b57cec5SDimitry Andric   return std::signbit(x);
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric #endif
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric // CUDA 9.1 no longer provides declarations for libdevice functions, so we need
1500b57cec5SDimitry Andric // to provide our own.
1510b57cec5SDimitry Andric #include <__clang_cuda_libdevice_declares.h>
1520b57cec5SDimitry Andric 
1535ffd83dbSDimitry Andric // Wrappers for many device-side standard library functions, incl. math
1545ffd83dbSDimitry Andric // functions, became compiler builtins in CUDA-9 and have been removed from the
1555ffd83dbSDimitry Andric // CUDA headers. Clang now provides its own implementation of the wrappers.
1560b57cec5SDimitry Andric #if CUDA_VERSION >= 9000
1570b57cec5SDimitry Andric #include <__clang_cuda_device_functions.h>
1585ffd83dbSDimitry Andric #include <__clang_cuda_math.h>
1590b57cec5SDimitry Andric #endif
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric // __THROW is redefined to be empty by device_functions_decls.h in CUDA. Clang's
1620b57cec5SDimitry Andric // counterpart does not do it, so we need to make it empty here to keep
1630b57cec5SDimitry Andric // following CUDA includes happy.
1640b57cec5SDimitry Andric #undef __THROW
1650b57cec5SDimitry Andric #define __THROW
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric // CUDA 8.0.41 relies on __USE_FAST_MATH__ and __CUDA_PREC_DIV's values.
1680b57cec5SDimitry Andric // Previous versions used to check whether they are defined or not.
1690b57cec5SDimitry Andric // CU_DEVICE_INVALID macro is only defined in 8.0.41, so we use it
1700b57cec5SDimitry Andric // here to detect the switch.
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric #if defined(CU_DEVICE_INVALID)
1730b57cec5SDimitry Andric #if !defined(__USE_FAST_MATH__)
1740b57cec5SDimitry Andric #define __USE_FAST_MATH__ 0
1750b57cec5SDimitry Andric #endif
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric #if !defined(__CUDA_PREC_DIV)
1780b57cec5SDimitry Andric #define __CUDA_PREC_DIV 0
1790b57cec5SDimitry Andric #endif
1800b57cec5SDimitry Andric #endif
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric // Temporarily poison __host__ macro to ensure it's not used by any of
1830b57cec5SDimitry Andric // the headers we're about to include.
1840b57cec5SDimitry Andric #pragma push_macro("__host__")
1850b57cec5SDimitry Andric #define __host__ UNEXPECTED_HOST_ATTRIBUTE
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric // device_functions.hpp and math_functions*.hpp use 'static
1880b57cec5SDimitry Andric // __forceinline__' (with no __device__) for definitions of device
1890b57cec5SDimitry Andric // functions. Temporarily redefine __forceinline__ to include
1900b57cec5SDimitry Andric // __device__.
1910b57cec5SDimitry Andric #pragma push_macro("__forceinline__")
1920b57cec5SDimitry Andric #define __forceinline__ __device__ __inline__ __attribute__((always_inline))
1930b57cec5SDimitry Andric #if CUDA_VERSION < 9000
1940b57cec5SDimitry Andric #include "device_functions.hpp"
1950b57cec5SDimitry Andric #endif
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric // math_function.hpp uses the __USE_FAST_MATH__ macro to determine whether we
1980b57cec5SDimitry Andric // get the slow-but-accurate or fast-but-inaccurate versions of functions like
1995f757f3fSDimitry Andric // sin and exp.  This is controlled in clang by -fgpu-approx-transcendentals.
2000b57cec5SDimitry Andric //
2010b57cec5SDimitry Andric // device_functions.hpp uses __USE_FAST_MATH__ for a different purpose (fast vs.
2020b57cec5SDimitry Andric // slow divides), so we need to scope our define carefully here.
2030b57cec5SDimitry Andric #pragma push_macro("__USE_FAST_MATH__")
2045f757f3fSDimitry Andric #if defined(__CLANG_GPU_APPROX_TRANSCENDENTALS__)
2050b57cec5SDimitry Andric #define __USE_FAST_MATH__ 1
2060b57cec5SDimitry Andric #endif
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric #if CUDA_VERSION >= 9000
2090b57cec5SDimitry Andric #include "crt/math_functions.hpp"
2100b57cec5SDimitry Andric #else
2110b57cec5SDimitry Andric #include "math_functions.hpp"
2120b57cec5SDimitry Andric #endif
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric #pragma pop_macro("__USE_FAST_MATH__")
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric #if CUDA_VERSION < 9000
2170b57cec5SDimitry Andric #include "math_functions_dbl_ptx3.hpp"
2180b57cec5SDimitry Andric #endif
2190b57cec5SDimitry Andric #pragma pop_macro("__forceinline__")
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric // Pull in host-only functions that are only available when neither
2220b57cec5SDimitry Andric // __CUDACC__ nor __CUDABE__ are defined.
2230b57cec5SDimitry Andric #undef __MATH_FUNCTIONS_HPP__
2240b57cec5SDimitry Andric #undef __CUDABE__
2250b57cec5SDimitry Andric #if CUDA_VERSION < 9000
2260b57cec5SDimitry Andric #include "math_functions.hpp"
2270b57cec5SDimitry Andric #endif
2280b57cec5SDimitry Andric // Alas, additional overloads for these functions are hard to get to.
2290b57cec5SDimitry Andric // Considering that we only need these overloads for a few functions,
2300b57cec5SDimitry Andric // we can provide them here.
rsqrt(float __a)2310b57cec5SDimitry Andric static inline float rsqrt(float __a) { return rsqrtf(__a); }
rcbrt(float __a)2320b57cec5SDimitry Andric static inline float rcbrt(float __a) { return rcbrtf(__a); }
sinpi(float __a)2330b57cec5SDimitry Andric static inline float sinpi(float __a) { return sinpif(__a); }
cospi(float __a)2340b57cec5SDimitry Andric static inline float cospi(float __a) { return cospif(__a); }
sincospi(float __a,float * __b,float * __c)2350b57cec5SDimitry Andric static inline void sincospi(float __a, float *__b, float *__c) {
2360b57cec5SDimitry Andric   return sincospif(__a, __b, __c);
2370b57cec5SDimitry Andric }
erfcinv(float __a)2380b57cec5SDimitry Andric static inline float erfcinv(float __a) { return erfcinvf(__a); }
normcdfinv(float __a)2390b57cec5SDimitry Andric static inline float normcdfinv(float __a) { return normcdfinvf(__a); }
normcdf(float __a)2400b57cec5SDimitry Andric static inline float normcdf(float __a) { return normcdff(__a); }
erfcx(float __a)2410b57cec5SDimitry Andric static inline float erfcx(float __a) { return erfcxf(__a); }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric #if CUDA_VERSION < 9000
2440b57cec5SDimitry Andric // For some reason single-argument variant is not always declared by
2450b57cec5SDimitry Andric // CUDA headers. Alas, device_functions.hpp included below needs it.
__brkpt(int __c)2460b57cec5SDimitry Andric static inline __device__ void __brkpt(int __c) { __brkpt(); }
2470b57cec5SDimitry Andric #endif
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric // Now include *.hpp with definitions of various GPU functions.  Alas,
2500b57cec5SDimitry Andric // a lot of thins get declared/defined with __host__ attribute which
2510b57cec5SDimitry Andric // we don't want and we have to define it out. We also have to include
2520b57cec5SDimitry Andric // {device,math}_functions.hpp again in order to extract the other
2530b57cec5SDimitry Andric // branch of #if/else inside.
2540b57cec5SDimitry Andric #define __host__
2550b57cec5SDimitry Andric #undef __CUDABE__
2560b57cec5SDimitry Andric #define __CUDACC__
2570b57cec5SDimitry Andric #if CUDA_VERSION >= 9000
2580b57cec5SDimitry Andric // Some atomic functions became compiler builtins in CUDA-9 , so we need their
2590b57cec5SDimitry Andric // declarations.
2600b57cec5SDimitry Andric #include "device_atomic_functions.h"
2610b57cec5SDimitry Andric #endif
2620b57cec5SDimitry Andric #undef __DEVICE_FUNCTIONS_HPP__
2630b57cec5SDimitry Andric #include "device_atomic_functions.hpp"
2640b57cec5SDimitry Andric #if CUDA_VERSION >= 9000
2650b57cec5SDimitry Andric #include "crt/device_functions.hpp"
2660b57cec5SDimitry Andric #include "crt/device_double_functions.hpp"
2670b57cec5SDimitry Andric #else
2680b57cec5SDimitry Andric #include "device_functions.hpp"
2690b57cec5SDimitry Andric #define __CUDABE__
2700b57cec5SDimitry Andric #include "device_double_functions.h"
2710b57cec5SDimitry Andric #undef __CUDABE__
2720b57cec5SDimitry Andric #endif
2730b57cec5SDimitry Andric #include "sm_20_atomic_functions.hpp"
274349cc55cSDimitry Andric // Predicate functions used in `__builtin_assume` need to have no side effect.
275349cc55cSDimitry Andric // However, sm_20_intrinsics.hpp doesn't define them with neither pure nor
276349cc55cSDimitry Andric // const attribute. Rename definitions from sm_20_intrinsics.hpp and re-define
277349cc55cSDimitry Andric // them as pure ones.
278349cc55cSDimitry Andric #pragma push_macro("__isGlobal")
279349cc55cSDimitry Andric #pragma push_macro("__isShared")
280349cc55cSDimitry Andric #pragma push_macro("__isConstant")
281349cc55cSDimitry Andric #pragma push_macro("__isLocal")
282349cc55cSDimitry Andric #define __isGlobal __ignored_cuda___isGlobal
283349cc55cSDimitry Andric #define __isShared __ignored_cuda___isShared
284349cc55cSDimitry Andric #define __isConstant __ignored_cuda___isConstant
285349cc55cSDimitry Andric #define __isLocal __ignored_cuda___isLocal
2860b57cec5SDimitry Andric #include "sm_20_intrinsics.hpp"
287349cc55cSDimitry Andric #pragma pop_macro("__isGlobal")
288349cc55cSDimitry Andric #pragma pop_macro("__isShared")
289349cc55cSDimitry Andric #pragma pop_macro("__isConstant")
290349cc55cSDimitry Andric #pragma pop_macro("__isLocal")
291349cc55cSDimitry Andric #pragma push_macro("__DEVICE__")
292349cc55cSDimitry Andric #define __DEVICE__ static __device__ __forceinline__ __attribute__((const))
__isGlobal(const void * p)293349cc55cSDimitry Andric __DEVICE__ unsigned int __isGlobal(const void *p) {
294349cc55cSDimitry Andric   return __nvvm_isspacep_global(p);
295349cc55cSDimitry Andric }
__isShared(const void * p)296349cc55cSDimitry Andric __DEVICE__ unsigned int __isShared(const void *p) {
297349cc55cSDimitry Andric   return __nvvm_isspacep_shared(p);
298349cc55cSDimitry Andric }
__isConstant(const void * p)299349cc55cSDimitry Andric __DEVICE__ unsigned int __isConstant(const void *p) {
300349cc55cSDimitry Andric   return __nvvm_isspacep_const(p);
301349cc55cSDimitry Andric }
__isLocal(const void * p)302349cc55cSDimitry Andric __DEVICE__ unsigned int __isLocal(const void *p) {
303349cc55cSDimitry Andric   return __nvvm_isspacep_local(p);
304349cc55cSDimitry Andric }
305349cc55cSDimitry Andric #pragma pop_macro("__DEVICE__")
3060b57cec5SDimitry Andric #include "sm_32_atomic_functions.hpp"
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric // Don't include sm_30_intrinsics.h and sm_32_intrinsics.h.  These define the
3090b57cec5SDimitry Andric // __shfl and __ldg intrinsics using inline (volatile) asm, but we want to
3100b57cec5SDimitry Andric // define them using builtins so that the optimizer can reason about and across
3110b57cec5SDimitry Andric // these instructions.  In particular, using intrinsics for ldg gets us the
3120b57cec5SDimitry Andric // [addr+imm] addressing mode, which, although it doesn't actually exist in the
3130b57cec5SDimitry Andric // hardware, seems to generate faster machine code because ptxas can more easily
3140b57cec5SDimitry Andric // reason about our code.
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric #if CUDA_VERSION >= 8000
3170b57cec5SDimitry Andric #pragma push_macro("__CUDA_ARCH__")
3180b57cec5SDimitry Andric #undef __CUDA_ARCH__
3190b57cec5SDimitry Andric #include "sm_60_atomic_functions.hpp"
3200b57cec5SDimitry Andric #include "sm_61_intrinsics.hpp"
3210b57cec5SDimitry Andric #pragma pop_macro("__CUDA_ARCH__")
3220b57cec5SDimitry Andric #endif
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric #undef __MATH_FUNCTIONS_HPP__
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric // math_functions.hpp defines ::signbit as a __host__ __device__ function.  This
3270b57cec5SDimitry Andric // conflicts with libstdc++'s constexpr ::signbit, so we have to rename
3280b57cec5SDimitry Andric // math_function.hpp's ::signbit.  It's guarded by #undef signbit, but that's
3290b57cec5SDimitry Andric // conditional on __GNUC__.  :)
3300b57cec5SDimitry Andric #pragma push_macro("signbit")
3310b57cec5SDimitry Andric #pragma push_macro("__GNUC__")
3320b57cec5SDimitry Andric #undef __GNUC__
3330b57cec5SDimitry Andric #define signbit __ignored_cuda_signbit
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric // CUDA-9 omits device-side definitions of some math functions if it sees
3360b57cec5SDimitry Andric // include guard from math.h wrapper from libstdc++. We have to undo the header
3370b57cec5SDimitry Andric // guard temporarily to get the definitions we need.
3380b57cec5SDimitry Andric #pragma push_macro("_GLIBCXX_MATH_H")
3390b57cec5SDimitry Andric #pragma push_macro("_LIBCPP_VERSION")
3400b57cec5SDimitry Andric #if CUDA_VERSION >= 9000
3410b57cec5SDimitry Andric #undef _GLIBCXX_MATH_H
3420b57cec5SDimitry Andric // We also need to undo another guard that checks for libc++ 3.8+
3430b57cec5SDimitry Andric #ifdef _LIBCPP_VERSION
3440b57cec5SDimitry Andric #define _LIBCPP_VERSION 3700
3450b57cec5SDimitry Andric #endif
3460b57cec5SDimitry Andric #endif
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric #if CUDA_VERSION >= 9000
3490b57cec5SDimitry Andric #include "crt/math_functions.hpp"
3500b57cec5SDimitry Andric #else
3510b57cec5SDimitry Andric #include "math_functions.hpp"
3520b57cec5SDimitry Andric #endif
3530b57cec5SDimitry Andric #pragma pop_macro("_GLIBCXX_MATH_H")
3540b57cec5SDimitry Andric #pragma pop_macro("_LIBCPP_VERSION")
3550b57cec5SDimitry Andric #pragma pop_macro("__GNUC__")
3560b57cec5SDimitry Andric #pragma pop_macro("signbit")
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric #pragma pop_macro("__host__")
3590b57cec5SDimitry Andric 
360349cc55cSDimitry Andric // __clang_cuda_texture_intrinsics.h must be included first in order to provide
361349cc55cSDimitry Andric // implementation for __nv_tex_surf_handler that CUDA's headers depend on.
362349cc55cSDimitry Andric // The implementation requires c++11 and only works with CUDA-9 or newer.
363349cc55cSDimitry Andric #if __cplusplus >= 201103L && CUDA_VERSION >= 9000
364349cc55cSDimitry Andric // clang-format off
365349cc55cSDimitry Andric #include <__clang_cuda_texture_intrinsics.h>
366349cc55cSDimitry Andric // clang-format on
367349cc55cSDimitry Andric #else
368349cc55cSDimitry Andric #if CUDA_VERSION >= 9000
369349cc55cSDimitry Andric // Provide a hint that texture support needs C++11.
370349cc55cSDimitry Andric template <typename T> struct __nv_tex_needs_cxx11 {
371349cc55cSDimitry Andric   const static bool value = false;
372349cc55cSDimitry Andric };
373349cc55cSDimitry Andric template <class T>
__nv_tex_surf_handler(const char * name,T * ptr,cudaTextureObject_t obj,float x)374349cc55cSDimitry Andric __host__ __device__ void __nv_tex_surf_handler(const char *name, T *ptr,
375349cc55cSDimitry Andric                                                cudaTextureObject_t obj,
376349cc55cSDimitry Andric                                                float x) {
377349cc55cSDimitry Andric   _Static_assert(__nv_tex_needs_cxx11<T>::value,
378349cc55cSDimitry Andric                  "Texture support requires C++11");
379349cc55cSDimitry Andric }
380349cc55cSDimitry Andric #else
381349cc55cSDimitry Andric // Textures in CUDA-8 and older are not supported by clang.There's no
382349cc55cSDimitry Andric // convenient way to intercept texture use in these versions, so we can't
383349cc55cSDimitry Andric // produce a meaningful error. The source code that attempts to use textures
384349cc55cSDimitry Andric // will continue to fail as it does now.
385349cc55cSDimitry Andric #endif // CUDA_VERSION
386349cc55cSDimitry Andric #endif // __cplusplus >= 201103L && CUDA_VERSION >= 9000
387349cc55cSDimitry Andric #include "texture_fetch_functions.h"
3880b57cec5SDimitry Andric #include "texture_indirect_functions.h"
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric // Restore state of __CUDA_ARCH__ and __THROW we had on entry.
3910b57cec5SDimitry Andric #pragma pop_macro("__CUDA_ARCH__")
3920b57cec5SDimitry Andric #pragma pop_macro("__THROW")
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric // Set up compiler macros expected to be seen during compilation.
3950b57cec5SDimitry Andric #undef __CUDABE__
3960b57cec5SDimitry Andric #define __CUDACC__
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric extern "C" {
3990b57cec5SDimitry Andric // Device-side CUDA system calls.
4000b57cec5SDimitry Andric // http://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/index.html#system-calls
4010b57cec5SDimitry Andric // We need these declarations and wrappers for device-side
4020b57cec5SDimitry Andric // malloc/free/printf calls to work without relying on
4030b57cec5SDimitry Andric // -fcuda-disable-target-call-checks option.
4040b57cec5SDimitry Andric __device__ int vprintf(const char *, const char *);
4050b57cec5SDimitry Andric __device__ void free(void *) __attribute((nothrow));
4060b57cec5SDimitry Andric __device__ void *malloc(size_t) __attribute((nothrow)) __attribute__((malloc));
407fe6060f1SDimitry Andric 
408fe6060f1SDimitry Andric // __assertfail() used to have a `noreturn` attribute. Unfortunately that
409fe6060f1SDimitry Andric // contributed to triggering the longstanding bug in ptxas when assert was used
410fe6060f1SDimitry Andric // in sufficiently convoluted code. See
411fe6060f1SDimitry Andric // https://bugs.llvm.org/show_bug.cgi?id=27738 for the details.
4120b57cec5SDimitry Andric __device__ void __assertfail(const char *__message, const char *__file,
4130b57cec5SDimitry Andric                              unsigned __line, const char *__function,
414fe6060f1SDimitry Andric                              size_t __charSize);
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric // In order for standard assert() macro on linux to work we need to
4170b57cec5SDimitry Andric // provide device-side __assert_fail()
__assert_fail(const char * __message,const char * __file,unsigned __line,const char * __function)4180b57cec5SDimitry Andric __device__ static inline void __assert_fail(const char *__message,
4190b57cec5SDimitry Andric                                             const char *__file, unsigned __line,
4200b57cec5SDimitry Andric                                             const char *__function) {
4210b57cec5SDimitry Andric   __assertfail(__message, __file, __line, __function, sizeof(char));
4220b57cec5SDimitry Andric }
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric // Clang will convert printf into vprintf, but we still need
4250b57cec5SDimitry Andric // device-side declaration for it.
4260b57cec5SDimitry Andric __device__ int printf(const char *, ...);
4270b57cec5SDimitry Andric } // extern "C"
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric // We also need device-side std::malloc and std::free.
4300b57cec5SDimitry Andric namespace std {
free(void * __ptr)4310b57cec5SDimitry Andric __device__ static inline void free(void *__ptr) { ::free(__ptr); }
malloc(size_t __size)4320b57cec5SDimitry Andric __device__ static inline void *malloc(size_t __size) {
4330b57cec5SDimitry Andric   return ::malloc(__size);
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric } // namespace std
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric // Out-of-line implementations from __clang_cuda_builtin_vars.h.  These need to
4380b57cec5SDimitry Andric // come after we've pulled in the definition of uint3 and dim3.
4390b57cec5SDimitry Andric 
dim3()440e8d8bef9SDimitry Andric __device__ inline __cuda_builtin_threadIdx_t::operator dim3() const {
441e8d8bef9SDimitry Andric   return dim3(x, y, z);
442e8d8bef9SDimitry Andric }
443e8d8bef9SDimitry Andric 
uint3()4440b57cec5SDimitry Andric __device__ inline __cuda_builtin_threadIdx_t::operator uint3() const {
445e8d8bef9SDimitry Andric   return {x, y, z};
446e8d8bef9SDimitry Andric }
447e8d8bef9SDimitry Andric 
dim3()448e8d8bef9SDimitry Andric __device__ inline __cuda_builtin_blockIdx_t::operator dim3() const {
449e8d8bef9SDimitry Andric   return dim3(x, y, z);
4500b57cec5SDimitry Andric }
4510b57cec5SDimitry Andric 
uint3()4520b57cec5SDimitry Andric __device__ inline __cuda_builtin_blockIdx_t::operator uint3() const {
453e8d8bef9SDimitry Andric   return {x, y, z};
4540b57cec5SDimitry Andric }
4550b57cec5SDimitry Andric 
dim3()4560b57cec5SDimitry Andric __device__ inline __cuda_builtin_blockDim_t::operator dim3() const {
4570b57cec5SDimitry Andric   return dim3(x, y, z);
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric 
uint3()460e8d8bef9SDimitry Andric __device__ inline __cuda_builtin_blockDim_t::operator uint3() const {
461e8d8bef9SDimitry Andric   return {x, y, z};
462e8d8bef9SDimitry Andric }
463e8d8bef9SDimitry Andric 
dim3()4640b57cec5SDimitry Andric __device__ inline __cuda_builtin_gridDim_t::operator dim3() const {
4650b57cec5SDimitry Andric   return dim3(x, y, z);
4660b57cec5SDimitry Andric }
4670b57cec5SDimitry Andric 
uint3()468e8d8bef9SDimitry Andric __device__ inline __cuda_builtin_gridDim_t::operator uint3() const {
469e8d8bef9SDimitry Andric   return {x, y, z};
470e8d8bef9SDimitry Andric }
471e8d8bef9SDimitry Andric 
4720b57cec5SDimitry Andric #include <__clang_cuda_cmath.h>
4730b57cec5SDimitry Andric #include <__clang_cuda_intrinsics.h>
4740b57cec5SDimitry Andric #include <__clang_cuda_complex_builtins.h>
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric // curand_mtgp32_kernel helpfully redeclares blockDim and threadIdx in host
4770b57cec5SDimitry Andric // mode, giving them their "proper" types of dim3 and uint3.  This is
4780b57cec5SDimitry Andric // incompatible with the types we give in __clang_cuda_builtin_vars.h.  As as
4790b57cec5SDimitry Andric // hack, force-include the header (nvcc doesn't include it by default) but
4800b57cec5SDimitry Andric // redefine dim3 and uint3 to our builtin types.  (Thankfully dim3 and uint3 are
4810b57cec5SDimitry Andric // only used here for the redeclarations of blockDim and threadIdx.)
4820b57cec5SDimitry Andric #pragma push_macro("dim3")
4830b57cec5SDimitry Andric #pragma push_macro("uint3")
4840b57cec5SDimitry Andric #define dim3 __cuda_builtin_blockDim_t
4850b57cec5SDimitry Andric #define uint3 __cuda_builtin_threadIdx_t
4860b57cec5SDimitry Andric #include "curand_mtgp32_kernel.h"
4870b57cec5SDimitry Andric #pragma pop_macro("dim3")
4880b57cec5SDimitry Andric #pragma pop_macro("uint3")
4890b57cec5SDimitry Andric #pragma pop_macro("__USE_FAST_MATH__")
4900b57cec5SDimitry Andric #pragma pop_macro("__CUDA_INCLUDE_COMPILER_INTERNAL_HEADERS__")
4910b57cec5SDimitry Andric 
4920b57cec5SDimitry Andric // CUDA runtime uses this undocumented function to access kernel launch
4930b57cec5SDimitry Andric // configuration. The declaration is in crt/device_functions.h but that file
4940b57cec5SDimitry Andric // includes a lot of other stuff we don't want. Instead, we'll provide our own
4950b57cec5SDimitry Andric // declaration for it here.
4960b57cec5SDimitry Andric #if CUDA_VERSION >= 9020
4970b57cec5SDimitry Andric extern "C" unsigned __cudaPushCallConfiguration(dim3 gridDim, dim3 blockDim,
4980b57cec5SDimitry Andric                                                 size_t sharedMem = 0,
4990b57cec5SDimitry Andric                                                 void *stream = 0);
5000b57cec5SDimitry Andric #endif
5010b57cec5SDimitry Andric 
5020b57cec5SDimitry Andric #endif // __CUDA__
5030b57cec5SDimitry Andric #endif // __CLANG_CUDA_RUNTIME_WRAPPER_H__
504