cuda_builtin_vars.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*===---- cuda_builtin_vars.h - CUDA built-in variables ---------------------===
  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. #ifndef __CUDA_BUILTIN_VARS_H
  24. #define __CUDA_BUILTIN_VARS_H
  25. // The file implements built-in CUDA variables using __declspec(property).
  26. // https://msdn.microsoft.com/en-us/library/yhfk0thd.aspx
  27. // All read accesses of built-in variable fields get converted into calls to a
  28. // getter function which in turn would call appropriate builtin to fetch the
  29. // value.
  30. //
  31. // Example:
  32. // int x = threadIdx.x;
  33. // IR output:
  34. // %0 = call i32 @llvm.ptx.read.tid.x() #3
  35. // PTX output:
  36. // mov.u32 %r2, %tid.x;
  37. #define __CUDA_DEVICE_BUILTIN(FIELD, INTRINSIC) \
  38. __declspec(property(get = __fetch_builtin_##FIELD)) unsigned int FIELD; \
  39. static inline __attribute__((always_inline)) \
  40. __attribute__((device)) unsigned int __fetch_builtin_##FIELD(void) { \
  41. return INTRINSIC; \
  42. }
  43. #if __cplusplus >= 201103L
  44. #define __DELETE =delete
  45. #else
  46. #define __DELETE
  47. #endif
  48. // Make sure nobody can create instances of the special varible types. nvcc
  49. // also disallows taking address of special variables, so we disable address-of
  50. // operator as well.
  51. #define __CUDA_DISALLOW_BUILTINVAR_ACCESS(TypeName) \
  52. __attribute__((device)) TypeName() __DELETE; \
  53. __attribute__((device)) TypeName(const TypeName &) __DELETE; \
  54. __attribute__((device)) void operator=(const TypeName &) const __DELETE; \
  55. __attribute__((device)) TypeName *operator&() const __DELETE
  56. struct __cuda_builtin_threadIdx_t {
  57. __CUDA_DEVICE_BUILTIN(x,__builtin_ptx_read_tid_x());
  58. __CUDA_DEVICE_BUILTIN(y,__builtin_ptx_read_tid_y());
  59. __CUDA_DEVICE_BUILTIN(z,__builtin_ptx_read_tid_z());
  60. private:
  61. __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_threadIdx_t);
  62. };
  63. struct __cuda_builtin_blockIdx_t {
  64. __CUDA_DEVICE_BUILTIN(x,__builtin_ptx_read_ctaid_x());
  65. __CUDA_DEVICE_BUILTIN(y,__builtin_ptx_read_ctaid_y());
  66. __CUDA_DEVICE_BUILTIN(z,__builtin_ptx_read_ctaid_z());
  67. private:
  68. __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_blockIdx_t);
  69. };
  70. struct __cuda_builtin_blockDim_t {
  71. __CUDA_DEVICE_BUILTIN(x,__builtin_ptx_read_ntid_x());
  72. __CUDA_DEVICE_BUILTIN(y,__builtin_ptx_read_ntid_y());
  73. __CUDA_DEVICE_BUILTIN(z,__builtin_ptx_read_ntid_z());
  74. private:
  75. __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_blockDim_t);
  76. };
  77. struct __cuda_builtin_gridDim_t {
  78. __CUDA_DEVICE_BUILTIN(x,__builtin_ptx_read_nctaid_x());
  79. __CUDA_DEVICE_BUILTIN(y,__builtin_ptx_read_nctaid_y());
  80. __CUDA_DEVICE_BUILTIN(z,__builtin_ptx_read_nctaid_z());
  81. private:
  82. __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_gridDim_t);
  83. };
  84. #define __CUDA_BUILTIN_VAR \
  85. extern const __attribute__((device)) __attribute__((weak))
  86. __CUDA_BUILTIN_VAR __cuda_builtin_threadIdx_t threadIdx;
  87. __CUDA_BUILTIN_VAR __cuda_builtin_blockIdx_t blockIdx;
  88. __CUDA_BUILTIN_VAR __cuda_builtin_blockDim_t blockDim;
  89. __CUDA_BUILTIN_VAR __cuda_builtin_gridDim_t gridDim;
  90. // warpSize should translate to read of %WARP_SZ but there's currently no
  91. // builtin to do so. According to PTX v4.2 docs 'to date, all target
  92. // architectures have a WARP_SZ value of 32'.
  93. __attribute__((device)) const int warpSize = 32;
  94. #undef __CUDA_DEVICE_BUILTIN
  95. #undef __CUDA_BUILTIN_VAR
  96. #undef __CUDA_DISALLOW_BUILTINVAR_ACCESS
  97. #endif /* __CUDA_BUILTIN_VARS_H */