__clang_cuda_builtin_vars.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // Forward declares from vector_types.h.
  26. struct uint3;
  27. struct dim3;
  28. // The file implements built-in CUDA variables using __declspec(property).
  29. // https://msdn.microsoft.com/en-us/library/yhfk0thd.aspx
  30. // All read accesses of built-in variable fields get converted into calls to a
  31. // getter function which in turn calls the appropriate builtin to fetch the
  32. // value.
  33. //
  34. // Example:
  35. // int x = threadIdx.x;
  36. // IR output:
  37. // %0 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x() #3
  38. // PTX output:
  39. // mov.u32 %r2, %tid.x;
  40. #define __CUDA_DEVICE_BUILTIN(FIELD, INTRINSIC) \
  41. __declspec(property(get = __fetch_builtin_##FIELD)) unsigned int FIELD; \
  42. static inline __attribute__((always_inline)) \
  43. __attribute__((device)) unsigned int __fetch_builtin_##FIELD(void) { \
  44. return INTRINSIC; \
  45. }
  46. #if __cplusplus >= 201103L
  47. #define __DELETE =delete
  48. #else
  49. #define __DELETE
  50. #endif
  51. // Make sure nobody can create instances of the special varible types. nvcc
  52. // also disallows taking address of special variables, so we disable address-of
  53. // operator as well.
  54. #define __CUDA_DISALLOW_BUILTINVAR_ACCESS(TypeName) \
  55. __attribute__((device)) TypeName() __DELETE; \
  56. __attribute__((device)) TypeName(const TypeName &) __DELETE; \
  57. __attribute__((device)) void operator=(const TypeName &) const __DELETE; \
  58. __attribute__((device)) TypeName *operator&() const __DELETE
  59. struct __cuda_builtin_threadIdx_t {
  60. __CUDA_DEVICE_BUILTIN(x,__nvvm_read_ptx_sreg_tid_x());
  61. __CUDA_DEVICE_BUILTIN(y,__nvvm_read_ptx_sreg_tid_y());
  62. __CUDA_DEVICE_BUILTIN(z,__nvvm_read_ptx_sreg_tid_z());
  63. // threadIdx should be convertible to uint3 (in fact in nvcc, it *is* a
  64. // uint3). This function is defined after we pull in vector_types.h.
  65. __attribute__((device)) operator uint3() const;
  66. private:
  67. __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_threadIdx_t);
  68. };
  69. struct __cuda_builtin_blockIdx_t {
  70. __CUDA_DEVICE_BUILTIN(x,__nvvm_read_ptx_sreg_ctaid_x());
  71. __CUDA_DEVICE_BUILTIN(y,__nvvm_read_ptx_sreg_ctaid_y());
  72. __CUDA_DEVICE_BUILTIN(z,__nvvm_read_ptx_sreg_ctaid_z());
  73. // blockIdx should be convertible to uint3 (in fact in nvcc, it *is* a
  74. // uint3). This function is defined after we pull in vector_types.h.
  75. __attribute__((device)) operator uint3() const;
  76. private:
  77. __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_blockIdx_t);
  78. };
  79. struct __cuda_builtin_blockDim_t {
  80. __CUDA_DEVICE_BUILTIN(x,__nvvm_read_ptx_sreg_ntid_x());
  81. __CUDA_DEVICE_BUILTIN(y,__nvvm_read_ptx_sreg_ntid_y());
  82. __CUDA_DEVICE_BUILTIN(z,__nvvm_read_ptx_sreg_ntid_z());
  83. // blockDim should be convertible to dim3 (in fact in nvcc, it *is* a
  84. // dim3). This function is defined after we pull in vector_types.h.
  85. __attribute__((device)) operator dim3() const;
  86. private:
  87. __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_blockDim_t);
  88. };
  89. struct __cuda_builtin_gridDim_t {
  90. __CUDA_DEVICE_BUILTIN(x,__nvvm_read_ptx_sreg_nctaid_x());
  91. __CUDA_DEVICE_BUILTIN(y,__nvvm_read_ptx_sreg_nctaid_y());
  92. __CUDA_DEVICE_BUILTIN(z,__nvvm_read_ptx_sreg_nctaid_z());
  93. // gridDim should be convertible to dim3 (in fact in nvcc, it *is* a
  94. // dim3). This function is defined after we pull in vector_types.h.
  95. __attribute__((device)) operator dim3() const;
  96. private:
  97. __CUDA_DISALLOW_BUILTINVAR_ACCESS(__cuda_builtin_gridDim_t);
  98. };
  99. #define __CUDA_BUILTIN_VAR \
  100. extern const __attribute__((device)) __attribute__((weak))
  101. __CUDA_BUILTIN_VAR __cuda_builtin_threadIdx_t threadIdx;
  102. __CUDA_BUILTIN_VAR __cuda_builtin_blockIdx_t blockIdx;
  103. __CUDA_BUILTIN_VAR __cuda_builtin_blockDim_t blockDim;
  104. __CUDA_BUILTIN_VAR __cuda_builtin_gridDim_t gridDim;
  105. // warpSize should translate to read of %WARP_SZ but there's currently no
  106. // builtin to do so. According to PTX v4.2 docs 'to date, all target
  107. // architectures have a WARP_SZ value of 32'.
  108. __attribute__((device)) const int warpSize = 32;
  109. #undef __CUDA_DEVICE_BUILTIN
  110. #undef __CUDA_BUILTIN_VAR
  111. #undef __CUDA_DISALLOW_BUILTINVAR_ACCESS
  112. #endif /* __CUDA_BUILTIN_VARS_H */