xref: /aosp_15_r20/prebuilts/sdk/renderscript/clang-include/cuda_builtin_vars.h (revision 344a7f5ef16c479e7a7f54ee6567a9d112f9e72b)
1*344a7f5eSAndroid Build Coastguard Worker /*===---- cuda_builtin_vars.h - CUDA built-in variables ---------------------===
2*344a7f5eSAndroid Build Coastguard Worker  *
3*344a7f5eSAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a copy
4*344a7f5eSAndroid Build Coastguard Worker  * of this software and associated documentation files (the "Software"), to deal
5*344a7f5eSAndroid Build Coastguard Worker  * in the Software without restriction, including without limitation the rights
6*344a7f5eSAndroid Build Coastguard Worker  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*344a7f5eSAndroid Build Coastguard Worker  * copies of the Software, and to permit persons to whom the Software is
8*344a7f5eSAndroid Build Coastguard Worker  * furnished to do so, subject to the following conditions:
9*344a7f5eSAndroid Build Coastguard Worker  *
10*344a7f5eSAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be included in
11*344a7f5eSAndroid Build Coastguard Worker  * all copies or substantial portions of the Software.
12*344a7f5eSAndroid Build Coastguard Worker  *
13*344a7f5eSAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*344a7f5eSAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*344a7f5eSAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*344a7f5eSAndroid Build Coastguard Worker  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*344a7f5eSAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18*344a7f5eSAndroid Build Coastguard Worker  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19*344a7f5eSAndroid Build Coastguard Worker  * THE SOFTWARE.
20*344a7f5eSAndroid Build Coastguard Worker  *
21*344a7f5eSAndroid Build Coastguard Worker  *===-----------------------------------------------------------------------===
22*344a7f5eSAndroid Build Coastguard Worker  */
23*344a7f5eSAndroid Build Coastguard Worker 
24*344a7f5eSAndroid Build Coastguard Worker #ifndef __CUDA_BUILTIN_VARS_H
25*344a7f5eSAndroid Build Coastguard Worker #define __CUDA_BUILTIN_VARS_H
26*344a7f5eSAndroid Build Coastguard Worker 
27*344a7f5eSAndroid Build Coastguard Worker // Forward declares from vector_types.h.
28*344a7f5eSAndroid Build Coastguard Worker struct uint3;
29*344a7f5eSAndroid Build Coastguard Worker struct dim3;
30*344a7f5eSAndroid Build Coastguard Worker 
31*344a7f5eSAndroid Build Coastguard Worker // The file implements built-in CUDA variables using __declspec(property).
32*344a7f5eSAndroid Build Coastguard Worker // https://msdn.microsoft.com/en-us/library/yhfk0thd.aspx
33*344a7f5eSAndroid Build Coastguard Worker // All read accesses of built-in variable fields get converted into calls to a
34*344a7f5eSAndroid Build Coastguard Worker // getter function which in turn calls the appropriate builtin to fetch the
35*344a7f5eSAndroid Build Coastguard Worker // value.
36*344a7f5eSAndroid Build Coastguard Worker //
37*344a7f5eSAndroid Build Coastguard Worker // Example:
38*344a7f5eSAndroid Build Coastguard Worker //    int x = threadIdx.x;
39*344a7f5eSAndroid Build Coastguard Worker // IR output:
40*344a7f5eSAndroid Build Coastguard Worker //  %0 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x() #3
41*344a7f5eSAndroid Build Coastguard Worker // PTX output:
42*344a7f5eSAndroid Build Coastguard Worker //  mov.u32     %r2, %tid.x;
43*344a7f5eSAndroid Build Coastguard Worker 
44*344a7f5eSAndroid Build Coastguard Worker #define __CUDA_DEVICE_BUILTIN(FIELD, INTRINSIC)                                \
45*344a7f5eSAndroid Build Coastguard Worker   __declspec(property(get = __fetch_builtin_##FIELD)) unsigned int FIELD;      \
46*344a7f5eSAndroid Build Coastguard Worker   static inline __attribute__((always_inline))                                 \
47*344a7f5eSAndroid Build Coastguard Worker       __attribute__((device)) unsigned int __fetch_builtin_##FIELD(void) {     \
48*344a7f5eSAndroid Build Coastguard Worker     return INTRINSIC;                                                          \
49*344a7f5eSAndroid Build Coastguard Worker   }
50*344a7f5eSAndroid Build Coastguard Worker 
51*344a7f5eSAndroid Build Coastguard Worker #if __cplusplus >= 201103L
52*344a7f5eSAndroid Build Coastguard Worker #define __DELETE =delete
53*344a7f5eSAndroid Build Coastguard Worker #else
54*344a7f5eSAndroid Build Coastguard Worker #define __DELETE
55*344a7f5eSAndroid Build Coastguard Worker #endif
56*344a7f5eSAndroid Build Coastguard Worker 
57*344a7f5eSAndroid Build Coastguard Worker // Make sure nobody can create instances of the special varible types.  nvcc
58*344a7f5eSAndroid Build Coastguard Worker // also disallows taking address of special variables, so we disable address-of
59*344a7f5eSAndroid Build Coastguard Worker // operator as well.
60*344a7f5eSAndroid Build Coastguard Worker #define __CUDA_DISALLOW_BUILTINVAR_ACCESS(TypeName)                            \
61*344a7f5eSAndroid Build Coastguard Worker   __attribute__((device)) TypeName() __DELETE;                                 \
62*344a7f5eSAndroid Build Coastguard Worker   __attribute__((device)) TypeName(const TypeName &) __DELETE;                 \
63*344a7f5eSAndroid Build Coastguard Worker   __attribute__((device)) void operator=(const TypeName &) const __DELETE;     \
64*344a7f5eSAndroid Build Coastguard Worker   __attribute__((device)) TypeName *operator&() const __DELETE
65*344a7f5eSAndroid Build Coastguard Worker 
66*344a7f5eSAndroid Build Coastguard Worker struct __cuda_builtin_threadIdx_t {
67*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(x,__nvvm_read_ptx_sreg_tid_x());
68*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(y,__nvvm_read_ptx_sreg_tid_y());
69*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(z,__nvvm_read_ptx_sreg_tid_z());
70*344a7f5eSAndroid Build Coastguard Worker   // threadIdx should be convertible to uint3 (in fact in nvcc, it *is* a
71*344a7f5eSAndroid Build Coastguard Worker   // uint3).  This function is defined after we pull in vector_types.h.
72*344a7f5eSAndroid Build Coastguard Worker   __attribute__((device)) operator uint3() const;
73*344a7f5eSAndroid Build Coastguard Worker private:
74*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_threadIdx_t);
75*344a7f5eSAndroid Build Coastguard Worker };
76*344a7f5eSAndroid Build Coastguard Worker 
77*344a7f5eSAndroid Build Coastguard Worker struct __cuda_builtin_blockIdx_t {
78*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(x,__nvvm_read_ptx_sreg_ctaid_x());
79*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(y,__nvvm_read_ptx_sreg_ctaid_y());
80*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(z,__nvvm_read_ptx_sreg_ctaid_z());
81*344a7f5eSAndroid Build Coastguard Worker   // blockIdx should be convertible to uint3 (in fact in nvcc, it *is* a
82*344a7f5eSAndroid Build Coastguard Worker   // uint3).  This function is defined after we pull in vector_types.h.
83*344a7f5eSAndroid Build Coastguard Worker   __attribute__((device)) operator uint3() const;
84*344a7f5eSAndroid Build Coastguard Worker private:
85*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_blockIdx_t);
86*344a7f5eSAndroid Build Coastguard Worker };
87*344a7f5eSAndroid Build Coastguard Worker 
88*344a7f5eSAndroid Build Coastguard Worker struct __cuda_builtin_blockDim_t {
89*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(x,__nvvm_read_ptx_sreg_ntid_x());
90*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(y,__nvvm_read_ptx_sreg_ntid_y());
91*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(z,__nvvm_read_ptx_sreg_ntid_z());
92*344a7f5eSAndroid Build Coastguard Worker   // blockDim should be convertible to dim3 (in fact in nvcc, it *is* a
93*344a7f5eSAndroid Build Coastguard Worker   // dim3).  This function is defined after we pull in vector_types.h.
94*344a7f5eSAndroid Build Coastguard Worker   __attribute__((device)) operator dim3() const;
95*344a7f5eSAndroid Build Coastguard Worker private:
96*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_blockDim_t);
97*344a7f5eSAndroid Build Coastguard Worker };
98*344a7f5eSAndroid Build Coastguard Worker 
99*344a7f5eSAndroid Build Coastguard Worker struct __cuda_builtin_gridDim_t {
100*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(x,__nvvm_read_ptx_sreg_nctaid_x());
101*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(y,__nvvm_read_ptx_sreg_nctaid_y());
102*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DEVICE_BUILTIN(z,__nvvm_read_ptx_sreg_nctaid_z());
103*344a7f5eSAndroid Build Coastguard Worker   // gridDim should be convertible to dim3 (in fact in nvcc, it *is* a
104*344a7f5eSAndroid Build Coastguard Worker   // dim3).  This function is defined after we pull in vector_types.h.
105*344a7f5eSAndroid Build Coastguard Worker   __attribute__((device)) operator dim3() const;
106*344a7f5eSAndroid Build Coastguard Worker private:
107*344a7f5eSAndroid Build Coastguard Worker   __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_gridDim_t);
108*344a7f5eSAndroid Build Coastguard Worker };
109*344a7f5eSAndroid Build Coastguard Worker 
110*344a7f5eSAndroid Build Coastguard Worker #define __CUDA_BUILTIN_VAR                                                     \
111*344a7f5eSAndroid Build Coastguard Worker   extern const __attribute__((device)) __attribute__((weak))
112*344a7f5eSAndroid Build Coastguard Worker __CUDA_BUILTIN_VAR __cuda_builtin_threadIdx_t threadIdx;
113*344a7f5eSAndroid Build Coastguard Worker __CUDA_BUILTIN_VAR __cuda_builtin_blockIdx_t blockIdx;
114*344a7f5eSAndroid Build Coastguard Worker __CUDA_BUILTIN_VAR __cuda_builtin_blockDim_t blockDim;
115*344a7f5eSAndroid Build Coastguard Worker __CUDA_BUILTIN_VAR __cuda_builtin_gridDim_t gridDim;
116*344a7f5eSAndroid Build Coastguard Worker 
117*344a7f5eSAndroid Build Coastguard Worker // warpSize should translate to read of %WARP_SZ but there's currently no
118*344a7f5eSAndroid Build Coastguard Worker // builtin to do so. According to PTX v4.2 docs 'to date, all target
119*344a7f5eSAndroid Build Coastguard Worker // architectures have a WARP_SZ value of 32'.
120*344a7f5eSAndroid Build Coastguard Worker __attribute__((device)) const int warpSize = 32;
121*344a7f5eSAndroid Build Coastguard Worker 
122*344a7f5eSAndroid Build Coastguard Worker #undef __CUDA_DEVICE_BUILTIN
123*344a7f5eSAndroid Build Coastguard Worker #undef __CUDA_BUILTIN_VAR
124*344a7f5eSAndroid Build Coastguard Worker #undef __CUDA_DISALLOW_BUILTINVAR_ACCESS
125*344a7f5eSAndroid Build Coastguard Worker 
126*344a7f5eSAndroid Build Coastguard Worker #endif /* __CUDA_BUILTIN_VARS_H */
127