__clang_cuda_runtime_wrapper.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*===---- __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. * WARNING: This header is intended to be directly -include'd by
  25. * the compiler and is not supposed to be included by users.
  26. *
  27. * CUDA headers are implemented in a way that currently makes it
  28. * impossible for user code to #include directly when compiling with
  29. * Clang. They present different view of CUDA-supplied functions
  30. * depending on where in NVCC's compilation pipeline the headers are
  31. * included. Neither of these modes provides function definitions with
  32. * correct attributes, so we use preprocessor to force the headers
  33. * into a form that Clang can use.
  34. *
  35. * Similarly to NVCC which -include's cuda_runtime.h, Clang -include's
  36. * this file during every CUDA compilation.
  37. */
  38. #ifndef __CLANG_CUDA_RUNTIME_WRAPPER_H__
  39. #define __CLANG_CUDA_RUNTIME_WRAPPER_H__
  40. #if defined(__CUDA__) && defined(__clang__)
  41. // Include some forward declares that must come before cmath.
  42. #include <__clang_cuda_math_forward_declares.h>
  43. // Include some standard headers to avoid CUDA headers including them
  44. // while some required macros (like __THROW) are in a weird state.
  45. #include <cmath>
  46. #include <cstdlib>
  47. #include <stdlib.h>
  48. // Preserve common macros that will be changed below by us or by CUDA
  49. // headers.
  50. #pragma push_macro("__THROW")
  51. #pragma push_macro("__CUDA_ARCH__")
  52. // WARNING: Preprocessor hacks below are based on specific details of
  53. // CUDA-7.x headers and are not expected to work with any other
  54. // version of CUDA headers.
  55. #include "cuda.h"
  56. #if !defined(CUDA_VERSION)
  57. #error "cuda.h did not define CUDA_VERSION"
  58. #elif CUDA_VERSION < 7000 || CUDA_VERSION > 9000
  59. #error "Unsupported CUDA version!"
  60. #endif
  61. // Make largest subset of device functions available during host
  62. // compilation -- SM_35 for the time being.
  63. #ifndef __CUDA_ARCH__
  64. #define __CUDA_ARCH__ 350
  65. #endif
  66. #include "__clang_cuda_builtin_vars.h"
  67. // No need for device_launch_parameters.h as __clang_cuda_builtin_vars.h above
  68. // has taken care of builtin variables declared in the file.
  69. #define __DEVICE_LAUNCH_PARAMETERS_H__
  70. // {math,device}_functions.h only have declarations of the
  71. // functions. We don't need them as we're going to pull in their
  72. // definitions from .hpp files.
  73. #define __DEVICE_FUNCTIONS_H__
  74. #define __MATH_FUNCTIONS_H__
  75. #define __COMMON_FUNCTIONS_H__
  76. #undef __CUDACC__
  77. #if CUDA_VERSION < 9000
  78. #define __CUDABE__
  79. #else
  80. #define __CUDA_LIBDEVICE__
  81. #endif
  82. // Disables definitions of device-side runtime support stubs in
  83. // cuda_device_runtime_api.h
  84. #include "driver_types.h"
  85. #include "host_config.h"
  86. #include "host_defines.h"
  87. #undef __CUDABE__
  88. #undef __CUDA_LIBDEVICE__
  89. #define __CUDACC__
  90. #include "cuda_runtime.h"
  91. #undef __CUDACC__
  92. #define __CUDABE__
  93. // CUDA headers use __nvvm_memcpy and __nvvm_memset which Clang does
  94. // not have at the moment. Emulate them with a builtin memcpy/memset.
  95. #define __nvvm_memcpy(s, d, n, a) __builtin_memcpy(s, d, n)
  96. #define __nvvm_memset(d, c, n, a) __builtin_memset(d, c, n)
  97. #if CUDA_VERSION < 9000
  98. #include "crt/device_runtime.h"
  99. #endif
  100. #include "crt/host_runtime.h"
  101. // device_runtime.h defines __cxa_* macros that will conflict with
  102. // cxxabi.h.
  103. // FIXME: redefine these as __device__ functions.
  104. #undef __cxa_vec_ctor
  105. #undef __cxa_vec_cctor
  106. #undef __cxa_vec_dtor
  107. #undef __cxa_vec_new
  108. #undef __cxa_vec_new2
  109. #undef __cxa_vec_new3
  110. #undef __cxa_vec_delete2
  111. #undef __cxa_vec_delete
  112. #undef __cxa_vec_delete3
  113. #undef __cxa_pure_virtual
  114. // math_functions.hpp expects this host function be defined on MacOS, but it
  115. // ends up not being there because of the games we play here. Just define it
  116. // ourselves; it's simple enough.
  117. #ifdef __APPLE__
  118. inline __host__ double __signbitd(double x) {
  119. return std::signbit(x);
  120. }
  121. #endif
  122. // We need decls for functions in CUDA's libdevice with __device__
  123. // attribute only. Alas they come either as __host__ __device__ or
  124. // with no attributes at all. To work around that, define __CUDA_RTC__
  125. // which produces HD variant and undef __host__ which gives us desided
  126. // decls with __device__ attribute.
  127. #pragma push_macro("__host__")
  128. #define __host__
  129. #define __CUDACC_RTC__
  130. #include "device_functions_decls.h"
  131. #undef __CUDACC_RTC__
  132. // Temporarily poison __host__ macro to ensure it's not used by any of
  133. // the headers we're about to include.
  134. #define __host__ UNEXPECTED_HOST_ATTRIBUTE
  135. // CUDA 8.0.41 relies on __USE_FAST_MATH__ and __CUDA_PREC_DIV's values.
  136. // Previous versions used to check whether they are defined or not.
  137. // CU_DEVICE_INVALID macro is only defined in 8.0.41, so we use it
  138. // here to detect the switch.
  139. #if defined(CU_DEVICE_INVALID)
  140. #if !defined(__USE_FAST_MATH__)
  141. #define __USE_FAST_MATH__ 0
  142. #endif
  143. #if !defined(__CUDA_PREC_DIV)
  144. #define __CUDA_PREC_DIV 0
  145. #endif
  146. #endif
  147. // device_functions.hpp and math_functions*.hpp use 'static
  148. // __forceinline__' (with no __device__) for definitions of device
  149. // functions. Temporarily redefine __forceinline__ to include
  150. // __device__.
  151. #pragma push_macro("__forceinline__")
  152. #define __forceinline__ __device__ __inline__ __attribute__((always_inline))
  153. #pragma push_macro("__float2half_rn")
  154. #if CUDA_VERSION >= 9000
  155. // CUDA-9 has conflicting prototypes for __float2half_rn(float f) in
  156. // cuda_fp16.h[pp] and device_functions.hpp. We need to get the one in
  157. // device_functions.hpp out of the way.
  158. #define __float2half_rn __float2half_rn_disabled
  159. #endif
  160. #include "device_functions.hpp"
  161. #pragma pop_macro("__float2half_rn")
  162. // math_function.hpp uses the __USE_FAST_MATH__ macro to determine whether we
  163. // get the slow-but-accurate or fast-but-inaccurate versions of functions like
  164. // sin and exp. This is controlled in clang by -fcuda-approx-transcendentals.
  165. //
  166. // device_functions.hpp uses __USE_FAST_MATH__ for a different purpose (fast vs.
  167. // slow divides), so we need to scope our define carefully here.
  168. #pragma push_macro("__USE_FAST_MATH__")
  169. #if defined(__CLANG_CUDA_APPROX_TRANSCENDENTALS__)
  170. #define __USE_FAST_MATH__ 1
  171. #endif
  172. #include "math_functions.hpp"
  173. #pragma pop_macro("__USE_FAST_MATH__")
  174. #include "math_functions_dbl_ptx3.hpp"
  175. #pragma pop_macro("__forceinline__")
  176. // Pull in host-only functions that are only available when neither
  177. // __CUDACC__ nor __CUDABE__ are defined.
  178. #undef __MATH_FUNCTIONS_HPP__
  179. #undef __CUDABE__
  180. #include "math_functions.hpp"
  181. // Alas, additional overloads for these functions are hard to get to.
  182. // Considering that we only need these overloads for a few functions,
  183. // we can provide them here.
  184. static inline float rsqrt(float __a) { return rsqrtf(__a); }
  185. static inline float rcbrt(float __a) { return rcbrtf(__a); }
  186. static inline float sinpi(float __a) { return sinpif(__a); }
  187. static inline float cospi(float __a) { return cospif(__a); }
  188. static inline void sincospi(float __a, float *__b, float *__c) {
  189. return sincospif(__a, __b, __c);
  190. }
  191. static inline float erfcinv(float __a) { return erfcinvf(__a); }
  192. static inline float normcdfinv(float __a) { return normcdfinvf(__a); }
  193. static inline float normcdf(float __a) { return normcdff(__a); }
  194. static inline float erfcx(float __a) { return erfcxf(__a); }
  195. // For some reason single-argument variant is not always declared by
  196. // CUDA headers. Alas, device_functions.hpp included below needs it.
  197. static inline __device__ void __brkpt(int __c) { __brkpt(); }
  198. // Now include *.hpp with definitions of various GPU functions. Alas,
  199. // a lot of thins get declared/defined with __host__ attribute which
  200. // we don't want and we have to define it out. We also have to include
  201. // {device,math}_functions.hpp again in order to extract the other
  202. // branch of #if/else inside.
  203. #define __host__
  204. #undef __CUDABE__
  205. #define __CUDACC__
  206. #undef __DEVICE_FUNCTIONS_HPP__
  207. #include "device_atomic_functions.hpp"
  208. #include "device_functions.hpp"
  209. #include "sm_20_atomic_functions.hpp"
  210. #include "sm_20_intrinsics.hpp"
  211. #include "sm_32_atomic_functions.hpp"
  212. // Don't include sm_30_intrinsics.h and sm_32_intrinsics.h. These define the
  213. // __shfl and __ldg intrinsics using inline (volatile) asm, but we want to
  214. // define them using builtins so that the optimizer can reason about and across
  215. // these instructions. In particular, using intrinsics for ldg gets us the
  216. // [addr+imm] addressing mode, which, although it doesn't actually exist in the
  217. // hardware, seems to generate faster machine code because ptxas can more easily
  218. // reason about our code.
  219. #if CUDA_VERSION >= 8000
  220. #include "sm_60_atomic_functions.hpp"
  221. #include "sm_61_intrinsics.hpp"
  222. #endif
  223. #undef __MATH_FUNCTIONS_HPP__
  224. // math_functions.hpp defines ::signbit as a __host__ __device__ function. This
  225. // conflicts with libstdc++'s constexpr ::signbit, so we have to rename
  226. // math_function.hpp's ::signbit. It's guarded by #undef signbit, but that's
  227. // conditional on __GNUC__. :)
  228. #pragma push_macro("signbit")
  229. #pragma push_macro("__GNUC__")
  230. #undef __GNUC__
  231. #define signbit __ignored_cuda_signbit
  232. // CUDA-9 omits device-side definitions of some math functions if it sees
  233. // include guard from math.h wrapper from libstdc++. We have to undo the header
  234. // guard temporarily to get the definitions we need.
  235. #pragma push_macro("_GLIBCXX_MATH_H")
  236. #pragma push_macro("_LIBCPP_VERSION")
  237. #if CUDA_VERSION >= 9000
  238. #undef _GLIBCXX_MATH_H
  239. // We also need to undo another guard that checks for libc++ 3.8+
  240. #ifdef _LIBCPP_VERSION
  241. #define _LIBCPP_VERSION 3700
  242. #endif
  243. #endif
  244. #include "math_functions.hpp"
  245. #pragma pop_macro("_GLIBCXX_MATH_H")
  246. #pragma pop_macro("_LIBCPP_VERSION")
  247. #pragma pop_macro("__GNUC__")
  248. #pragma pop_macro("signbit")
  249. #pragma pop_macro("__host__")
  250. #include "texture_indirect_functions.h"
  251. // Restore state of __CUDA_ARCH__ and __THROW we had on entry.
  252. #pragma pop_macro("__CUDA_ARCH__")
  253. #pragma pop_macro("__THROW")
  254. // Set up compiler macros expected to be seen during compilation.
  255. #undef __CUDABE__
  256. #define __CUDACC__
  257. extern "C" {
  258. // Device-side CUDA system calls.
  259. // http://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/index.html#system-calls
  260. // We need these declarations and wrappers for device-side
  261. // malloc/free/printf calls to work without relying on
  262. // -fcuda-disable-target-call-checks option.
  263. __device__ int vprintf(const char *, const char *);
  264. __device__ void free(void *) __attribute((nothrow));
  265. __device__ void *malloc(size_t) __attribute((nothrow)) __attribute__((malloc));
  266. __device__ void __assertfail(const char *__message, const char *__file,
  267. unsigned __line, const char *__function,
  268. size_t __charSize) __attribute__((noreturn));
  269. // In order for standard assert() macro on linux to work we need to
  270. // provide device-side __assert_fail()
  271. __device__ static inline void __assert_fail(const char *__message,
  272. const char *__file, unsigned __line,
  273. const char *__function) {
  274. __assertfail(__message, __file, __line, __function, sizeof(char));
  275. }
  276. // Clang will convert printf into vprintf, but we still need
  277. // device-side declaration for it.
  278. __device__ int printf(const char *, ...);
  279. } // extern "C"
  280. // We also need device-side std::malloc and std::free.
  281. namespace std {
  282. __device__ static inline void free(void *__ptr) { ::free(__ptr); }
  283. __device__ static inline void *malloc(size_t __size) {
  284. return ::malloc(__size);
  285. }
  286. } // namespace std
  287. // Out-of-line implementations from __clang_cuda_builtin_vars.h. These need to
  288. // come after we've pulled in the definition of uint3 and dim3.
  289. __device__ inline __cuda_builtin_threadIdx_t::operator uint3() const {
  290. uint3 ret;
  291. ret.x = x;
  292. ret.y = y;
  293. ret.z = z;
  294. return ret;
  295. }
  296. __device__ inline __cuda_builtin_blockIdx_t::operator uint3() const {
  297. uint3 ret;
  298. ret.x = x;
  299. ret.y = y;
  300. ret.z = z;
  301. return ret;
  302. }
  303. __device__ inline __cuda_builtin_blockDim_t::operator dim3() const {
  304. return dim3(x, y, z);
  305. }
  306. __device__ inline __cuda_builtin_gridDim_t::operator dim3() const {
  307. return dim3(x, y, z);
  308. }
  309. #include <__clang_cuda_cmath.h>
  310. #include <__clang_cuda_intrinsics.h>
  311. #include <__clang_cuda_complex_builtins.h>
  312. // curand_mtgp32_kernel helpfully redeclares blockDim and threadIdx in host
  313. // mode, giving them their "proper" types of dim3 and uint3. This is
  314. // incompatible with the types we give in __clang_cuda_builtin_vars.h. As as
  315. // hack, force-include the header (nvcc doesn't include it by default) but
  316. // redefine dim3 and uint3 to our builtin types. (Thankfully dim3 and uint3 are
  317. // only used here for the redeclarations of blockDim and threadIdx.)
  318. #pragma push_macro("dim3")
  319. #pragma push_macro("uint3")
  320. #define dim3 __cuda_builtin_blockDim_t
  321. #define uint3 __cuda_builtin_threadIdx_t
  322. #include "curand_mtgp32_kernel.h"
  323. #pragma pop_macro("dim3")
  324. #pragma pop_macro("uint3")
  325. #pragma pop_macro("__USE_FAST_MATH__")
  326. #endif // __CUDA__
  327. #endif // __CLANG_CUDA_RUNTIME_WRAPPER_H__