Path: blob/master/tools/android-sdk/renderscript/clang-include/__clang_cuda_runtime_wrapper.h
496 views
/*===---- __clang_cuda_runtime_wrapper.h - CUDA runtime support -------------===1*2* Permission is hereby granted, free of charge, to any person obtaining a copy3* of this software and associated documentation files (the "Software"), to deal4* in the Software without restriction, including without limitation the rights5* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell6* copies of the Software, and to permit persons to whom the Software is7* furnished to do so, subject to the following conditions:8*9* The above copyright notice and this permission notice shall be included in10* all copies or substantial portions of the Software.11*12* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,17* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN18* THE SOFTWARE.19*20*===-----------------------------------------------------------------------===21*/2223/*24* WARNING: This header is intended to be directly -include'd by25* the compiler and is not supposed to be included by users.26*27* CUDA headers are implemented in a way that currently makes it28* impossible for user code to #include directly when compiling with29* Clang. They present different view of CUDA-supplied functions30* depending on where in NVCC's compilation pipeline the headers are31* included. Neither of these modes provides function definitions with32* correct attributes, so we use preprocessor to force the headers33* into a form that Clang can use.34*35* Similarly to NVCC which -include's cuda_runtime.h, Clang -include's36* this file during every CUDA compilation.37*/3839#ifndef __CLANG_CUDA_RUNTIME_WRAPPER_H__40#define __CLANG_CUDA_RUNTIME_WRAPPER_H__4142#if defined(__CUDA__) && defined(__clang__)4344// Include some forward declares that must come before cmath.45#include <__clang_cuda_math_forward_declares.h>4647// Include some standard headers to avoid CUDA headers including them48// while some required macros (like __THROW) are in a weird state.49#include <cmath>50#include <cstdlib>51#include <stdlib.h>5253// Preserve common macros that will be changed below by us or by CUDA54// headers.55#pragma push_macro("__THROW")56#pragma push_macro("__CUDA_ARCH__")5758// WARNING: Preprocessor hacks below are based on specific details of59// CUDA-7.x headers and are not expected to work with any other60// version of CUDA headers.61#include "cuda.h"62#if !defined(CUDA_VERSION)63#error "cuda.h did not define CUDA_VERSION"64#elif CUDA_VERSION < 7000 || CUDA_VERSION > 705065#error "Unsupported CUDA version!"66#endif6768// Make largest subset of device functions available during host69// compilation -- SM_35 for the time being.70#ifndef __CUDA_ARCH__71#define __CUDA_ARCH__ 35072#endif7374#include "cuda_builtin_vars.h"7576// No need for device_launch_parameters.h as cuda_builtin_vars.h above77// has taken care of builtin variables declared in the file.78#define __DEVICE_LAUNCH_PARAMETERS_H__7980// {math,device}_functions.h only have declarations of the81// functions. We don't need them as we're going to pull in their82// definitions from .hpp files.83#define __DEVICE_FUNCTIONS_H__84#define __MATH_FUNCTIONS_H__85#define __COMMON_FUNCTIONS_H__8687#undef __CUDACC__88#define __CUDABE__89// Disables definitions of device-side runtime support stubs in90// cuda_device_runtime_api.h91#include "driver_types.h"92#include "host_config.h"93#include "host_defines.h"9495#undef __CUDABE__96#define __CUDACC__97#include "cuda_runtime.h"9899#undef __CUDACC__100#define __CUDABE__101102// CUDA headers use __nvvm_memcpy and __nvvm_memset which Clang does103// not have at the moment. Emulate them with a builtin memcpy/memset.104#define __nvvm_memcpy(s, d, n, a) __builtin_memcpy(s, d, n)105#define __nvvm_memset(d, c, n, a) __builtin_memset(d, c, n)106107#include "crt/device_runtime.h"108#include "crt/host_runtime.h"109// device_runtime.h defines __cxa_* macros that will conflict with110// cxxabi.h.111// FIXME: redefine these as __device__ functions.112#undef __cxa_vec_ctor113#undef __cxa_vec_cctor114#undef __cxa_vec_dtor115#undef __cxa_vec_new2116#undef __cxa_vec_new3117#undef __cxa_vec_delete2118#undef __cxa_vec_delete119#undef __cxa_vec_delete3120#undef __cxa_pure_virtual121122// We need decls for functions in CUDA's libdevice with __device__123// attribute only. Alas they come either as __host__ __device__ or124// with no attributes at all. To work around that, define __CUDA_RTC__125// which produces HD variant and undef __host__ which gives us desided126// 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__132133// Temporarily poison __host__ macro to ensure it's not used by any of134// the headers we're about to include.135#define __host__ UNEXPECTED_HOST_ATTRIBUTE136137// device_functions.hpp and math_functions*.hpp use 'static138// __forceinline__' (with no __device__) for definitions of device139// functions. Temporarily redefine __forceinline__ to include140// __device__.141#pragma push_macro("__forceinline__")142#define __forceinline__ __device__ __inline__ __attribute__((always_inline))143#include "device_functions.hpp"144145// math_function.hpp uses the __USE_FAST_MATH__ macro to determine whether we146// get the slow-but-accurate or fast-but-inaccurate versions of functions like147// sin and exp. This is controlled in clang by -fcuda-approx-transcendentals.148//149// device_functions.hpp uses __USE_FAST_MATH__ for a different purpose (fast vs.150// slow divides), so we need to scope our define carefully here.151#pragma push_macro("__USE_FAST_MATH__")152#if defined(__CLANG_CUDA_APPROX_TRANSCENDENTALS__)153#define __USE_FAST_MATH__154#endif155#include "math_functions.hpp"156#pragma pop_macro("__USE_FAST_MATH__")157158#include "math_functions_dbl_ptx3.hpp"159#pragma pop_macro("__forceinline__")160161// Pull in host-only functions that are only available when neither162// __CUDACC__ nor __CUDABE__ are defined.163#undef __MATH_FUNCTIONS_HPP__164#undef __CUDABE__165#include "math_functions.hpp"166// Alas, additional overloads for these functions are hard to get to.167// Considering that we only need these overloads for a few functions,168// we can provide them here.169static inline float rsqrt(float __a) { return rsqrtf(__a); }170static inline float rcbrt(float __a) { return rcbrtf(__a); }171static inline float sinpi(float __a) { return sinpif(__a); }172static inline float cospi(float __a) { return cospif(__a); }173static inline void sincospi(float __a, float *__b, float *__c) {174return sincospif(__a, __b, __c);175}176static inline float erfcinv(float __a) { return erfcinvf(__a); }177static inline float normcdfinv(float __a) { return normcdfinvf(__a); }178static inline float normcdf(float __a) { return normcdff(__a); }179static inline float erfcx(float __a) { return erfcxf(__a); }180181// For some reason single-argument variant is not always declared by182// CUDA headers. Alas, device_functions.hpp included below needs it.183static inline __device__ void __brkpt(int __c) { __brkpt(); }184185// Now include *.hpp with definitions of various GPU functions. Alas,186// a lot of thins get declared/defined with __host__ attribute which187// we don't want and we have to define it out. We also have to include188// {device,math}_functions.hpp again in order to extract the other189// branch of #if/else inside.190191#define __host__192#undef __CUDABE__193#define __CUDACC__194#undef __DEVICE_FUNCTIONS_HPP__195#include "device_atomic_functions.hpp"196#include "device_functions.hpp"197#include "sm_20_atomic_functions.hpp"198#include "sm_20_intrinsics.hpp"199#include "sm_32_atomic_functions.hpp"200201// Don't include sm_30_intrinsics.h and sm_32_intrinsics.h. These define the202// __shfl and __ldg intrinsics using inline (volatile) asm, but we want to203// define them using builtins so that the optimizer can reason about and across204// these instructions. In particular, using intrinsics for ldg gets us the205// [addr+imm] addressing mode, which, although it doesn't actually exist in the206// hardware, seems to generate faster machine code because ptxas can more easily207// reason about our code.208209#undef __MATH_FUNCTIONS_HPP__210211// math_functions.hpp defines ::signbit as a __host__ __device__ function. This212// conflicts with libstdc++'s constexpr ::signbit, so we have to rename213// math_function.hpp's ::signbit. It's guarded by #undef signbit, but that's214// conditional on __GNUC__. :)215#pragma push_macro("signbit")216#pragma push_macro("__GNUC__")217#undef __GNUC__218#define signbit __ignored_cuda_signbit219#include "math_functions.hpp"220#pragma pop_macro("__GNUC__")221#pragma pop_macro("signbit")222223#pragma pop_macro("__host__")224225#include "texture_indirect_functions.h"226227// Restore state of __CUDA_ARCH__ and __THROW we had on entry.228#pragma pop_macro("__CUDA_ARCH__")229#pragma pop_macro("__THROW")230231// Set up compiler macros expected to be seen during compilation.232#undef __CUDABE__233#define __CUDACC__234235extern "C" {236// Device-side CUDA system calls.237// http://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/index.html#system-calls238// We need these declarations and wrappers for device-side239// malloc/free/printf calls to work without relying on240// -fcuda-disable-target-call-checks option.241__device__ int vprintf(const char *, const char *);242__device__ void free(void *) __attribute((nothrow));243__device__ void *malloc(size_t) __attribute((nothrow)) __attribute__((malloc));244__device__ void __assertfail(const char *__message, const char *__file,245unsigned __line, const char *__function,246size_t __charSize) __attribute__((noreturn));247248// In order for standard assert() macro on linux to work we need to249// provide device-side __assert_fail()250__device__ static inline void __assert_fail(const char *__message,251const char *__file, unsigned __line,252const char *__function) {253__assertfail(__message, __file, __line, __function, sizeof(char));254}255256// Clang will convert printf into vprintf, but we still need257// device-side declaration for it.258__device__ int printf(const char *, ...);259} // extern "C"260261// We also need device-side std::malloc and std::free.262namespace std {263__device__ static inline void free(void *__ptr) { ::free(__ptr); }264__device__ static inline void *malloc(size_t __size) {265return ::malloc(__size);266}267} // namespace std268269// Out-of-line implementations from cuda_builtin_vars.h. These need to come270// after we've pulled in the definition of uint3 and dim3.271272__device__ inline __cuda_builtin_threadIdx_t::operator uint3() const {273uint3 ret;274ret.x = x;275ret.y = y;276ret.z = z;277return ret;278}279280__device__ inline __cuda_builtin_blockIdx_t::operator uint3() const {281uint3 ret;282ret.x = x;283ret.y = y;284ret.z = z;285return ret;286}287288__device__ inline __cuda_builtin_blockDim_t::operator dim3() const {289return dim3(x, y, z);290}291292__device__ inline __cuda_builtin_gridDim_t::operator dim3() const {293return dim3(x, y, z);294}295296#include <__clang_cuda_cmath.h>297#include <__clang_cuda_intrinsics.h>298299// curand_mtgp32_kernel helpfully redeclares blockDim and threadIdx in host300// mode, giving them their "proper" types of dim3 and uint3. This is301// incompatible with the types we give in cuda_builtin_vars.h. As as hack,302// force-include the header (nvcc doesn't include it by default) but redefine303// dim3 and uint3 to our builtin types. (Thankfully dim3 and uint3 are only304// used here for the redeclarations of blockDim and threadIdx.)305#pragma push_macro("dim3")306#pragma push_macro("uint3")307#define dim3 __cuda_builtin_blockDim_t308#define uint3 __cuda_builtin_threadIdx_t309#include "curand_mtgp32_kernel.h"310#pragma pop_macro("dim3")311#pragma pop_macro("uint3")312#pragma pop_macro("__USE_FAST_MATH__")313314#endif // __CUDA__315#endif // __CLANG_CUDA_RUNTIME_WRAPPER_H__316317318