clang 22.0.0git
__clang_cuda_runtime_wrapper.h
Go to the documentation of this file.
1/*===---- __clang_cuda_runtime_wrapper.h - CUDA runtime support -------------===
2 *
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 *
7 *===-----------------------------------------------------------------------===
8 */
9
10/*
11 * WARNING: This header is intended to be directly -include'd by
12 * the compiler and is not supposed to be included by users.
13 *
14 * CUDA headers are implemented in a way that currently makes it
15 * impossible for user code to #include directly when compiling with
16 * Clang. They present different view of CUDA-supplied functions
17 * depending on where in NVCC's compilation pipeline the headers are
18 * included. Neither of these modes provides function definitions with
19 * correct attributes, so we use preprocessor to force the headers
20 * into a form that Clang can use.
21 *
22 * Similarly to NVCC which -include's cuda_runtime.h, Clang -include's
23 * this file during every CUDA compilation.
24 */
25
26#ifndef __CLANG_CUDA_RUNTIME_WRAPPER_H__
27#define __CLANG_CUDA_RUNTIME_WRAPPER_H__
28
29#if defined(__CUDA__) && defined(__clang__)
30
31// Include some forward declares that must come before cmath.
33
34// Define __CUDACC__ early as libstdc++ standard headers with GNU extensions
35// enabled depend on it to avoid using __float128, which is unsupported in
36// CUDA.
37#define __CUDACC__
38
39// Include some standard headers to avoid CUDA headers including them
40// while some required macros (like __THROW) are in a weird state.
41#include <climits>
42#include <cmath>
43#include <cstdlib>
44#include <stdlib.h>
45#include <string.h>
46#undef __CUDACC__
47
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
53// WARNING: Preprocessor hacks below are based on specific details of
54// CUDA-7.x headers and are not expected to work with any other
55// version of CUDA headers.
56#include "cuda.h"
57#if !defined(CUDA_VERSION)
58#error "cuda.h did not define CUDA_VERSION"
59#elif CUDA_VERSION < 7000
60#error "Unsupported CUDA version!"
61#endif
62
63#pragma push_macro("__CUDA_INCLUDE_COMPILER_INTERNAL_HEADERS__")
64#if CUDA_VERSION >= 10000
65#define __CUDA_INCLUDE_COMPILER_INTERNAL_HEADERS__
66#endif
67
68// Make largest subset of device functions available during host
69// compilation.
70#ifndef __CUDA_ARCH__
71#define __CUDA_ARCH__ 9999
72#endif
73
75
76// No need for device_launch_parameters.h as __clang_cuda_builtin_vars.h above
77// has taken care of builtin variables declared in the file.
78#define __DEVICE_LAUNCH_PARAMETERS_H__
79
80// {math,device}_functions.h only have declarations of the
81// functions. We don't need them as we're going to pull in their
82// definitions from .hpp files.
83#define __DEVICE_FUNCTIONS_H__
84#define __MATH_FUNCTIONS_H__
85#define __COMMON_FUNCTIONS_H__
86// device_functions_decls is replaced by __clang_cuda_device_functions.h
87// included below.
88#define __DEVICE_FUNCTIONS_DECLS_H__
89
90#undef __CUDACC__
91#if CUDA_VERSION < 9000
92#define __CUDABE__
93#else
94#define __CUDACC__
95#define __CUDA_LIBDEVICE__
96#endif
97// Disables definitions of device-side runtime support stubs in
98// cuda_device_runtime_api.h
99#include "host_defines.h"
100#undef __CUDACC__
101#include "driver_types.h"
102#include "host_config.h"
103
104// Temporarily replace "nv_weak" with weak, so __attribute__((nv_weak)) in
105// cuda_device_runtime_api.h ends up being __attribute__((weak)) which is the
106// functional equivalent of what we need.
107#pragma push_macro("nv_weak")
108#define nv_weak weak
109#undef __CUDABE__
110#undef __CUDA_LIBDEVICE__
111#define __CUDACC__
112#include "cuda_runtime.h"
113
114#pragma pop_macro("nv_weak")
115#undef __CUDACC__
116#define __CUDABE__
117
118// CUDA headers use __nvvm_memcpy and __nvvm_memset which Clang does
119// not have at the moment. Emulate them with a builtin memcpy/memset.
120#define __nvvm_memcpy(s, d, n, a) __builtin_memcpy(s, d, n)
121#define __nvvm_memset(d, c, n, a) __builtin_memset(d, c, n)
122
123#if CUDA_VERSION < 9000
124#include "crt/device_runtime.h"
125#endif
126#include "crt/host_runtime.h"
127// device_runtime.h defines __cxa_* macros that will conflict with
128// cxxabi.h.
129// FIXME: redefine these as __device__ functions.
130#undef __cxa_vec_ctor
131#undef __cxa_vec_cctor
132#undef __cxa_vec_dtor
133#undef __cxa_vec_new
134#undef __cxa_vec_new2
135#undef __cxa_vec_new3
136#undef __cxa_vec_delete2
137#undef __cxa_vec_delete
138#undef __cxa_vec_delete3
139#undef __cxa_pure_virtual
140
141// math_functions.hpp expects this host function be defined on MacOS, but it
142// ends up not being there because of the games we play here. Just define it
143// ourselves; it's simple enough.
144#ifdef __APPLE__
145inline __host__ double __signbitd(double x) {
146 return std::signbit(x);
147}
148#endif
149
150// CUDA 9.1 no longer provides declarations for libdevice functions, so we need
151// to provide our own.
153
154// Wrappers for many device-side standard library functions, incl. math
155// functions, became compiler builtins in CUDA-9 and have been removed from the
156// CUDA headers. Clang now provides its own implementation of the wrappers.
157#if CUDA_VERSION >= 9000
159#include <__clang_cuda_math.h>
160#endif
161
162// __THROW is redefined to be empty by device_functions_decls.h in CUDA. Clang's
163// counterpart does not do it, so we need to make it empty here to keep
164// following CUDA includes happy.
165#undef __THROW
166#define __THROW
167
168// CUDA 8.0.41 relies on __USE_FAST_MATH__ and __CUDA_PREC_DIV's values.
169// Previous versions used to check whether they are defined or not.
170// CU_DEVICE_INVALID macro is only defined in 8.0.41, so we use it
171// here to detect the switch.
172
173#if defined(CU_DEVICE_INVALID)
174#if !defined(__USE_FAST_MATH__)
175#define __USE_FAST_MATH__ 0
176#endif
177
178#if !defined(__CUDA_PREC_DIV)
179#define __CUDA_PREC_DIV 0
180#endif
181#endif
182
183// Temporarily poison __host__ macro to ensure it's not used by any of
184// the headers we're about to include.
185#pragma push_macro("__host__")
186#define __host__ UNEXPECTED_HOST_ATTRIBUTE
187
188// device_functions.hpp and math_functions*.hpp use 'static
189// __forceinline__' (with no __device__) for definitions of device
190// functions. Temporarily redefine __forceinline__ to include
191// __device__.
192#pragma push_macro("__forceinline__")
193#define __forceinline__ __device__ __inline__ __attribute__((always_inline))
194#if CUDA_VERSION < 9000
195#include "device_functions.hpp"
196#endif
197
198// math_function.hpp uses the __USE_FAST_MATH__ macro to determine whether we
199// get the slow-but-accurate or fast-but-inaccurate versions of functions like
200// sin and exp. This is controlled in clang by -fgpu-approx-transcendentals.
201//
202// device_functions.hpp uses __USE_FAST_MATH__ for a different purpose (fast vs.
203// slow divides), so we need to scope our define carefully here.
204#pragma push_macro("__USE_FAST_MATH__")
205#if defined(__CLANG_GPU_APPROX_TRANSCENDENTALS__)
206#define __USE_FAST_MATH__ 1
207#endif
208
209#if CUDA_VERSION >= 9000
210#include "crt/math_functions.hpp"
211#else
212#include "math_functions.hpp"
213#endif
214
215#pragma pop_macro("__USE_FAST_MATH__")
216
217#if CUDA_VERSION < 9000
218#include "math_functions_dbl_ptx3.hpp"
219#endif
220#pragma pop_macro("__forceinline__")
221
222// Pull in host-only functions that are only available when neither
223// __CUDACC__ nor __CUDABE__ are defined.
224#undef __MATH_FUNCTIONS_HPP__
225#undef __CUDABE__
226#if CUDA_VERSION < 9000
227#include "math_functions.hpp"
228#endif
229// Alas, additional overloads for these functions are hard to get to.
230// Considering that we only need these overloads for a few functions,
231// we can provide them here.
232static inline float rsqrt(float __a) { return rsqrtf(__a); }
233static inline float rcbrt(float __a) { return rcbrtf(__a); }
234static inline float sinpi(float __a) { return sinpif(__a); }
235static inline float cospi(float __a) { return cospif(__a); }
236static inline void sincospi(float __a, float *__b, float *__c) {
237 return sincospif(__a, __b, __c);
238}
239static inline float erfcinv(float __a) { return erfcinvf(__a); }
240static inline float normcdfinv(float __a) { return normcdfinvf(__a); }
241static inline float normcdf(float __a) { return normcdff(__a); }
242static inline float erfcx(float __a) { return erfcxf(__a); }
243
244#if CUDA_VERSION < 9000
245// For some reason single-argument variant is not always declared by
246// CUDA headers. Alas, device_functions.hpp included below needs it.
247static inline __device__ void __brkpt(int __c) { __brkpt(); }
248#endif
249
250// Now include *.hpp with definitions of various GPU functions. Alas,
251// a lot of thins get declared/defined with __host__ attribute which
252// we don't want and we have to define it out. We also have to include
253// {device,math}_functions.hpp again in order to extract the other
254// branch of #if/else inside.
255#define __host__
256#undef __CUDABE__
257#define __CUDACC__
258#if CUDA_VERSION >= 9000
259// Some atomic functions became compiler builtins in CUDA-9 , so we need their
260// declarations.
261#include "device_atomic_functions.h"
262#endif
263#undef __DEVICE_FUNCTIONS_HPP__
264#include "device_atomic_functions.hpp"
265#if CUDA_VERSION >= 9000
266#include "crt/device_functions.hpp"
267#include "crt/device_double_functions.hpp"
268#else
269#include "device_functions.hpp"
270#define __CUDABE__
271#include "device_double_functions.h"
272#undef __CUDABE__
273#endif
274#include "sm_20_atomic_functions.hpp"
275// Predicate functions used in `__builtin_assume` need to have no side effect.
276// However, sm_20_intrinsics.hpp doesn't define them with neither pure nor
277// const attribute. Rename definitions from sm_20_intrinsics.hpp and re-define
278// them as pure ones.
279#pragma push_macro("__isGlobal")
280#pragma push_macro("__isShared")
281#pragma push_macro("__isConstant")
282#pragma push_macro("__isLocal")
283#define __isGlobal __ignored_cuda___isGlobal
284#define __isShared __ignored_cuda___isShared
285#define __isConstant __ignored_cuda___isConstant
286#define __isLocal __ignored_cuda___isLocal
287#include "sm_20_intrinsics.hpp"
288#pragma pop_macro("__isGlobal")
289#pragma pop_macro("__isShared")
290#pragma pop_macro("__isConstant")
291#pragma pop_macro("__isLocal")
292#pragma push_macro("__DEVICE__")
293#define __DEVICE__ static __device__ __forceinline__ __attribute__((const))
294__DEVICE__ unsigned int __isGlobal(const void *p) {
295 return __nvvm_isspacep_global(p);
296}
297__DEVICE__ unsigned int __isShared(const void *p) {
298 return __nvvm_isspacep_shared(p);
299}
300__DEVICE__ unsigned int __isConstant(const void *p) {
301 return __nvvm_isspacep_const(p);
302}
303__DEVICE__ unsigned int __isLocal(const void *p) {
304 return __nvvm_isspacep_local(p);
305}
306#pragma pop_macro("__DEVICE__")
307#include "sm_32_atomic_functions.hpp"
308
309// Don't include sm_30_intrinsics.h and sm_32_intrinsics.h. These define the
310// __shfl and __ldg intrinsics using inline (volatile) asm, but we want to
311// define them using builtins so that the optimizer can reason about and across
312// these instructions. In particular, using intrinsics for ldg gets us the
313// [addr+imm] addressing mode, which, although it doesn't actually exist in the
314// hardware, seems to generate faster machine code because ptxas can more easily
315// reason about our code.
316
317#if CUDA_VERSION >= 8000
318#pragma push_macro("__CUDA_ARCH__")
319#undef __CUDA_ARCH__
320#include "sm_60_atomic_functions.hpp"
321#include "sm_61_intrinsics.hpp"
322#pragma pop_macro("__CUDA_ARCH__")
323#endif
324
325#undef __MATH_FUNCTIONS_HPP__
326
327// math_functions.hpp defines ::signbit as a __host__ __device__ function. This
328// conflicts with libstdc++'s constexpr ::signbit, so we have to rename
329// math_function.hpp's ::signbit. It's guarded by #undef signbit, but that's
330// conditional on __GNUC__. :)
331#pragma push_macro("signbit")
332#pragma push_macro("__GNUC__")
333#undef __GNUC__
334#define signbit __ignored_cuda_signbit
335
336// CUDA-9 omits device-side definitions of some math functions if it sees
337// include guard from math.h wrapper from libstdc++. We have to undo the header
338// guard temporarily to get the definitions we need.
339#pragma push_macro("_GLIBCXX_MATH_H")
340#pragma push_macro("_LIBCPP_VERSION")
341#if CUDA_VERSION >= 9000
342#undef _GLIBCXX_MATH_H
343// We also need to undo another guard that checks for libc++ 3.8+
344#ifdef _LIBCPP_VERSION
345#define _LIBCPP_VERSION 3700
346#endif
347#endif
348
349#if CUDA_VERSION >= 9000
350#include "crt/math_functions.hpp"
351#else
352#include "math_functions.hpp"
353#endif
354#pragma pop_macro("_GLIBCXX_MATH_H")
355#pragma pop_macro("_LIBCPP_VERSION")
356#pragma pop_macro("__GNUC__")
357#pragma pop_macro("signbit")
358
359#pragma pop_macro("__host__")
360
361// __clang_cuda_texture_intrinsics.h must be included first in order to provide
362// implementation for __nv_tex_surf_handler that CUDA's headers depend on.
363// The implementation requires c++11 and only works with CUDA-9 or newer.
364#if __cplusplus >= 201103L && CUDA_VERSION >= 9000
365// clang-format off
367// clang-format on
368#else
369#if CUDA_VERSION >= 9000
370// Provide a hint that texture support needs C++11.
371template <typename T> struct __nv_tex_needs_cxx11 {
372 const static bool value = false;
373};
374template <class T>
375__host__ __device__ void __nv_tex_surf_handler(const char *name, T *ptr,
376 cudaTextureObject_t obj,
377 float x) {
378 _Static_assert(__nv_tex_needs_cxx11<T>::value,
379 "Texture support requires C++11");
380}
381#else
382// Textures in CUDA-8 and older are not supported by clang.There's no
383// convenient way to intercept texture use in these versions, so we can't
384// produce a meaningful error. The source code that attempts to use textures
385// will continue to fail as it does now.
386#endif // CUDA_VERSION
387#endif // __cplusplus >= 201103L && CUDA_VERSION >= 9000
388#include "surface_indirect_functions.h"
389#if CUDA_VERSION < 13000
390// Direct texture fetch functions had been deprecated since CUDA-11.
391// The file in CUDA-12 only carried unused texture types, and is no longer
392// needed.
393#include "texture_fetch_functions.h"
394#endif // CUDA_VERSION < 13000
395#include "texture_indirect_functions.h"
396
397// Restore state of __CUDA_ARCH__ and __THROW we had on entry.
398#pragma pop_macro("__CUDA_ARCH__")
399#pragma pop_macro("__THROW")
400
401// Set up compiler macros expected to be seen during compilation.
402#undef __CUDABE__
403#define __CUDACC__
404
405extern "C" {
406// Device-side CUDA system calls.
407// http://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/index.html#system-calls
408// We need these declarations and wrappers for device-side
409// malloc/free/printf calls to work without relying on
410// -fcuda-disable-target-call-checks option.
411__device__ int vprintf(const char *, const char *);
412__device__ void free(void *) __attribute((nothrow));
413__device__ void *malloc(size_t) __attribute((nothrow)) __attribute__((malloc));
414
415// __assertfail() used to have a `noreturn` attribute. Unfortunately that
416// contributed to triggering the longstanding bug in ptxas when assert was used
417// in sufficiently convoluted code. See
418// https://bugs.llvm.org/show_bug.cgi?id=27738 for the details.
419__device__ void __assertfail(const char *__message, const char *__file,
420 unsigned __line, const char *__function,
421 size_t __charSize);
422
423// In order for standard assert() macro on linux to work we need to
424// provide device-side __assert_fail()
425__device__ static inline void __assert_fail(const char *__message,
426 const char *__file, unsigned __line,
427 const char *__function) {
428 __assertfail(__message, __file, __line, __function, sizeof(char));
429}
430
431// Clang will convert printf into vprintf, but we still need
432// device-side declaration for it.
433__device__ int printf(const char *, ...);
434} // extern "C"
435
436// We also need device-side std::malloc and std::free.
437namespace std {
438__device__ static inline void free(void *__ptr) { ::free(__ptr); }
439__device__ static inline void *malloc(size_t __size) {
440 return ::malloc(__size);
441}
442} // namespace std
443
444// Out-of-line implementations from __clang_cuda_builtin_vars.h. These need to
445// come after we've pulled in the definition of uint3 and dim3.
446
447__device__ inline __cuda_builtin_threadIdx_t::operator dim3() const {
448 return dim3(x, y, z);
449}
450
451__device__ inline __cuda_builtin_threadIdx_t::operator uint3() const {
452 return {x, y, z};
453}
454
455__device__ inline __cuda_builtin_blockIdx_t::operator dim3() const {
456 return dim3(x, y, z);
457}
458
459__device__ inline __cuda_builtin_blockIdx_t::operator uint3() const {
460 return {x, y, z};
461}
462
463__device__ inline __cuda_builtin_blockDim_t::operator dim3() const {
464 return dim3(x, y, z);
465}
466
467__device__ inline __cuda_builtin_blockDim_t::operator uint3() const {
468 return {x, y, z};
469}
470
471__device__ inline __cuda_builtin_gridDim_t::operator dim3() const {
472 return dim3(x, y, z);
473}
474
475__device__ inline __cuda_builtin_gridDim_t::operator uint3() const {
476 return {x, y, z};
477}
478
479#include <__clang_cuda_cmath.h>
482
483// curand_mtgp32_kernel helpfully redeclares blockDim and threadIdx in host
484// mode, giving them their "proper" types of dim3 and uint3. This is
485// incompatible with the types we give in __clang_cuda_builtin_vars.h. As as
486// hack, force-include the header (nvcc doesn't include it by default) but
487// redefine dim3 and uint3 to our builtin types. (Thankfully dim3 and uint3 are
488// only used here for the redeclarations of blockDim and threadIdx.)
489#pragma push_macro("dim3")
490#pragma push_macro("uint3")
491#define dim3 __cuda_builtin_blockDim_t
492#define uint3 __cuda_builtin_threadIdx_t
493#include "curand_mtgp32_kernel.h"
494#pragma pop_macro("dim3")
495#pragma pop_macro("uint3")
496#pragma pop_macro("__USE_FAST_MATH__")
497#pragma pop_macro("__CUDA_INCLUDE_COMPILER_INTERNAL_HEADERS__")
498
499// CUDA runtime uses this undocumented function to access kernel launch
500// configuration. The declaration is in crt/device_functions.h but that file
501// includes a lot of other stuff we don't want. Instead, we'll provide our own
502// declaration for it here.
503#if CUDA_VERSION >= 9020
504extern "C" unsigned __cudaPushCallConfiguration(dim3 gridDim, dim3 blockDim,
505 size_t sharedMem = 0,
506 void *stream = 0);
507#endif
508
509#endif // __CUDA__
510#endif // __CLANG_CUDA_RUNTIME_WRAPPER_H__
__CUDA_BUILTIN_VAR __cuda_builtin_blockDim_t blockDim
__CUDA_BUILTIN_VAR __cuda_builtin_gridDim_t gridDim
#define __DEVICE__
__DEVICE__ int __signbitd(double __a)
__DEVICE__ float rsqrtf(float __a)
__DEVICE__ double normcdf(double __a)
__DEVICE_VOID__ void sincospi(double __a, double *__s, double *__c)
__DEVICE__ float rcbrtf(float __a)
__DEVICE__ float erfcinvf(float __a)
__DEVICE__ float sinpif(float __a)
__DEVICE_VOID__ void sincospif(float __a, float *__s, float *__c)
__DEVICE__ double rcbrt(double __a)
__DEVICE__ float normcdff(float __a)
__DEVICE__ double cospi(double __a)
__DEVICE__ double sinpi(double __a)
__DEVICE__ float erfcxf(float __a)
__DEVICE__ float normcdfinvf(float __a)
__DEVICE__ double normcdfinv(double __a)
__DEVICE__ float cospif(float __a)
__DEVICE__ double erfcx(double __a)
__DEVICE__ double rsqrt(double __a)
__DEVICE__ double erfcinv(double __a)
#define __nv_tex_surf_handler(__op, __ptr,...)
_Float16 __2f16 __attribute__((ext_vector_type(2)))
Zeroes the upper 128 bits (bits 255:128) of all YMM registers.
#define __host__
#define __device__
struct dim3 dim3
static __inline__ vector float vector float vector float __c
Definition altivec.h:4800
static __inline__ vector float vector float __b
Definition altivec.h:578
static __inline__ void int __a
Definition emmintrin.h:4077
vector< uint, 3 > uint3
__DEVICE__ bool signbit(float __x)
CLINKAGE int printf(__constant const char *st,...) __attribute__((format(printf