device-var-init.cu 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // REQUIRES: nvptx-registered-target
  2. // REQUIRES: amdgpu-registered-target
  3. // Make sure we don't allow dynamic initialization for device
  4. // variables, but accept empty constructors allowed by CUDA.
  5. // RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fcuda-is-device -std=c++11 \
  6. // RUN: -fno-threadsafe-statics -emit-llvm -o - %s | FileCheck -check-prefixes=DEVICE,NVPTX %s
  7. // RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -std=c++11 \
  8. // RUN: -fno-threadsafe-statics -emit-llvm -o - %s | FileCheck -check-prefixes=HOST %s
  9. // RUN: %clang_cc1 -triple amdgcn -fcuda-is-device -std=c++11 \
  10. // RUN: -fno-threadsafe-statics -emit-llvm -o - %s | FileCheck -check-prefixes=DEVICE,AMDGCN %s
  11. #ifdef __clang__
  12. #include "Inputs/cuda.h"
  13. #endif
  14. // Use the types we share with Sema tests.
  15. #include "Inputs/cuda-initializers.h"
  16. __device__ int d_v;
  17. // DEVICE: @d_v = addrspace(1) externally_initialized global i32 0,
  18. // HOST: @d_v = internal global i32 undef,
  19. __shared__ int s_v;
  20. // DEVICE: @s_v = addrspace(3) global i32 undef,
  21. // HOST: @s_v = internal global i32 undef,
  22. __constant__ int c_v;
  23. // DEVICE: addrspace(4) externally_initialized global i32 0,
  24. // HOST: @c_v = internal global i32 undef,
  25. __device__ int d_v_i = 1;
  26. // DEVICE: @d_v_i = addrspace(1) externally_initialized global i32 1,
  27. // HOST: @d_v_i = internal global i32 undef,
  28. // For `static` device variables, assume they won't be addressed from the host
  29. // side.
  30. static __device__ int d_s_v_i = 1;
  31. // DEVICE: @_ZL7d_s_v_i = internal addrspace(1) global i32 1,
  32. // Dummy function to keep static variables referenced.
  33. __device__ int foo() {
  34. return d_s_v_i;
  35. }
  36. // trivial constructor -- allowed
  37. __device__ T d_t;
  38. // DEVICE: @d_t = addrspace(1) externally_initialized global %struct.T zeroinitializer
  39. // HOST: @d_t = internal global %struct.T undef,
  40. __shared__ T s_t;
  41. // DEVICE: @s_t = addrspace(3) global %struct.T undef,
  42. // HOST: @s_t = internal global %struct.T undef,
  43. __constant__ T c_t;
  44. // DEVICE: @c_t = addrspace(4) externally_initialized global %struct.T zeroinitializer,
  45. // HOST: @c_t = internal global %struct.T undef,
  46. __device__ T d_t_i = {2};
  47. // DEVICE: @d_t_i = addrspace(1) externally_initialized global %struct.T { i32 2 },
  48. // HOST: @d_t_i = internal global %struct.T undef,
  49. __constant__ T c_t_i = {2};
  50. // DEVICE: @c_t_i = addrspace(4) externally_initialized global %struct.T { i32 2 },
  51. // HOST: @c_t_i = internal global %struct.T undef,
  52. // empty constructor
  53. __device__ EC d_ec;
  54. // DEVICE: @d_ec = addrspace(1) externally_initialized global %struct.EC zeroinitializer,
  55. // HOST: @d_ec = internal global %struct.EC undef,
  56. __shared__ EC s_ec;
  57. // DEVICE: @s_ec = addrspace(3) global %struct.EC undef,
  58. // HOST: @s_ec = internal global %struct.EC undef,
  59. __constant__ EC c_ec;
  60. // DEVICE: @c_ec = addrspace(4) externally_initialized global %struct.EC zeroinitializer,
  61. // HOST: @c_ec = internal global %struct.EC undef
  62. // empty destructor
  63. __device__ ED d_ed;
  64. // DEVICE: @d_ed = addrspace(1) externally_initialized global %struct.ED zeroinitializer,
  65. // HOST: @d_ed = internal global %struct.ED undef,
  66. __shared__ ED s_ed;
  67. // DEVICE: @s_ed = addrspace(3) global %struct.ED undef,
  68. // HOST: @s_ed = internal global %struct.ED undef,
  69. __constant__ ED c_ed;
  70. // DEVICE: @c_ed = addrspace(4) externally_initialized global %struct.ED zeroinitializer,
  71. // HOST: @c_ed = internal global %struct.ED undef,
  72. __device__ ECD d_ecd;
  73. // DEVICE: @d_ecd = addrspace(1) externally_initialized global %struct.ECD zeroinitializer,
  74. // HOST: @d_ecd = internal global %struct.ECD undef,
  75. __shared__ ECD s_ecd;
  76. // DEVICE: @s_ecd = addrspace(3) global %struct.ECD undef,
  77. // HOST: @s_ecd = internal global %struct.ECD undef,
  78. __constant__ ECD c_ecd;
  79. // DEVICE: @c_ecd = addrspace(4) externally_initialized global %struct.ECD zeroinitializer,
  80. // HOST: @c_ecd = internal global %struct.ECD undef,
  81. // empty templated constructor -- allowed with no arguments
  82. __device__ ETC d_etc;
  83. // DEVICE: @d_etc = addrspace(1) externally_initialized global %struct.ETC zeroinitializer,
  84. // HOST: @d_etc = internal global %struct.ETC undef,
  85. __shared__ ETC s_etc;
  86. // DEVICE: @s_etc = addrspace(3) global %struct.ETC undef,
  87. // HOST: @s_etc = internal global %struct.ETC undef,
  88. __constant__ ETC c_etc;
  89. // DEVICE: @c_etc = addrspace(4) externally_initialized global %struct.ETC zeroinitializer,
  90. // HOST: @c_etc = internal global %struct.ETC undef,
  91. __device__ NCFS d_ncfs;
  92. // DEVICE: @d_ncfs = addrspace(1) externally_initialized global %struct.NCFS { i32 3 }
  93. // HOST: @d_ncfs = internal global %struct.NCFS undef,
  94. __constant__ NCFS c_ncfs;
  95. // DEVICE: @c_ncfs = addrspace(4) externally_initialized global %struct.NCFS { i32 3 }
  96. // HOST: @c_ncfs = internal global %struct.NCFS undef,
  97. // Regular base class -- allowed
  98. __device__ T_B_T d_t_b_t;
  99. // DEVICE: @d_t_b_t = addrspace(1) externally_initialized global %struct.T_B_T zeroinitializer,
  100. // HOST: @d_t_b_t = internal global %struct.T_B_T undef,
  101. __shared__ T_B_T s_t_b_t;
  102. // DEVICE: @s_t_b_t = addrspace(3) global %struct.T_B_T undef,
  103. // HOST: @s_t_b_t = internal global %struct.T_B_T undef,
  104. __constant__ T_B_T c_t_b_t;
  105. // DEVICE: @c_t_b_t = addrspace(4) externally_initialized global %struct.T_B_T zeroinitializer,
  106. // HOST: @c_t_b_t = internal global %struct.T_B_T undef,
  107. // Incapsulated object of allowed class -- allowed
  108. __device__ T_F_T d_t_f_t;
  109. // DEVICE: @d_t_f_t = addrspace(1) externally_initialized global %struct.T_F_T zeroinitializer,
  110. // HOST: @d_t_f_t = internal global %struct.T_F_T undef,
  111. __shared__ T_F_T s_t_f_t;
  112. // DEVICE: @s_t_f_t = addrspace(3) global %struct.T_F_T undef,
  113. // HOST: @s_t_f_t = internal global %struct.T_F_T undef,
  114. __constant__ T_F_T c_t_f_t;
  115. // DEVICE: @c_t_f_t = addrspace(4) externally_initialized global %struct.T_F_T zeroinitializer,
  116. // HOST: @c_t_f_t = internal global %struct.T_F_T undef,
  117. // array of allowed objects -- allowed
  118. __device__ T_FA_T d_t_fa_t;
  119. // DEVICE: @d_t_fa_t = addrspace(1) externally_initialized global %struct.T_FA_T zeroinitializer,
  120. // HOST: @d_t_fa_t = internal global %struct.T_FA_T undef,
  121. __shared__ T_FA_T s_t_fa_t;
  122. // DEVICE: @s_t_fa_t = addrspace(3) global %struct.T_FA_T undef,
  123. // HOST: @s_t_fa_t = internal global %struct.T_FA_T undef,
  124. __constant__ T_FA_T c_t_fa_t;
  125. // DEVICE: @c_t_fa_t = addrspace(4) externally_initialized global %struct.T_FA_T zeroinitializer,
  126. // HOST: @c_t_fa_t = internal global %struct.T_FA_T undef,
  127. // Calling empty base class initializer is OK
  128. __device__ EC_I_EC d_ec_i_ec;
  129. // DEVICE: @d_ec_i_ec = addrspace(1) externally_initialized global %struct.EC_I_EC zeroinitializer,
  130. // HOST: @d_ec_i_ec = internal global %struct.EC_I_EC undef,
  131. __shared__ EC_I_EC s_ec_i_ec;
  132. // DEVICE: @s_ec_i_ec = addrspace(3) global %struct.EC_I_EC undef,
  133. // HOST: @s_ec_i_ec = internal global %struct.EC_I_EC undef,
  134. __constant__ EC_I_EC c_ec_i_ec;
  135. // DEVICE: @c_ec_i_ec = addrspace(4) externally_initialized global %struct.EC_I_EC zeroinitializer,
  136. // HOST: @c_ec_i_ec = internal global %struct.EC_I_EC undef,
  137. // DEVICE: @_ZZ2dfvE4s_ec = internal addrspace(3) global %struct.EC undef
  138. // DEVICE: @_ZZ2dfvE5s_etc = internal addrspace(3) global %struct.ETC undef
  139. // DEVICE: @_ZZ2dfvE11const_array = internal addrspace(4) constant [5 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5]
  140. // DEVICE: @_ZZ2dfvE9const_int = internal addrspace(4) constant i32 123
  141. // We should not emit global initializers for device-side variables.
  142. // DEVICE-NOT: @__cxx_global_var_init
  143. // Make sure that initialization restrictions do not apply to local
  144. // variables.
  145. __device__ void df() {
  146. // NVPTX: %[[ec:.*]] = alloca %struct.EC
  147. // NVPTX: %[[ed:.*]] = alloca %struct.ED
  148. // NVPTX: %[[ecd:.*]] = alloca %struct.ECD
  149. // NVPTX: %[[etc:.*]] = alloca %struct.ETC
  150. // NVPTX: %[[uc:.*]] = alloca %struct.UC
  151. // NVPTX: %[[ud:.*]] = alloca %struct.UD
  152. // NVPTX: %[[eci:.*]] = alloca %struct.ECI
  153. // NVPTX: %[[nec:.*]] = alloca %struct.NEC
  154. // NVPTX: %[[ned:.*]] = alloca %struct.NED
  155. // NVPTX: %[[ncv:.*]] = alloca %struct.NCV
  156. // NVPTX: %[[vd:.*]] = alloca %struct.VD
  157. // NVPTX: %[[ncf:.*]] = alloca %struct.NCF
  158. // NVPTX: %[[ncfs:.*]] = alloca %struct.NCFS
  159. // NVPTX: %[[utc:.*]] = alloca %struct.UTC
  160. // NVPTX: %[[netc:.*]] = alloca %struct.NETC
  161. // NVPTX: %[[ec_i_ec:.*]] = alloca %struct.EC_I_EC
  162. // NVPTX: %[[ec_i_ec1:.*]] = alloca %struct.EC_I_EC1
  163. // NVPTX: %[[t_v_t:.*]] = alloca %struct.T_V_T
  164. // NVPTX: %[[t_b_nec:.*]] = alloca %struct.T_B_NEC
  165. // NVPTX: %[[t_f_nec:.*]] = alloca %struct.T_F_NEC
  166. // NVPTX: %[[t_fa_nec:.*]] = alloca %struct.T_FA_NEC
  167. // NVPTX: %[[t_b_ned:.*]] = alloca %struct.T_B_NED
  168. // NVPTX: %[[t_f_ned:.*]] = alloca %struct.T_F_NED
  169. // NVPTX: %[[t_fa_ned:.*]] = alloca %struct.T_FA_NED
  170. // AMDGCN: %[[ec:.*]] = addrspacecast %struct.EC addrspace(5)* %ec to %struct.EC*
  171. // AMDGCN: %[[ed:.*]] = addrspacecast %struct.ED addrspace(5)* %ed to %struct.ED*
  172. // AMDGCN: %[[ecd:.*]] = addrspacecast %struct.ECD addrspace(5)* %ecd to %struct.ECD*
  173. // AMDGCN: %[[etc:.*]] = addrspacecast %struct.ETC addrspace(5)* %etc to %struct.ETC*
  174. // AMDGCN: %[[uc:.*]] = addrspacecast %struct.UC addrspace(5)* %uc to %struct.UC*
  175. // AMDGCN: %[[ud:.*]] = addrspacecast %struct.UD addrspace(5)* %ud to %struct.UD*
  176. // AMDGCN: %[[eci:.*]] = addrspacecast %struct.ECI addrspace(5)* %eci to %struct.ECI*
  177. // AMDGCN: %[[nec:.*]] = addrspacecast %struct.NEC addrspace(5)* %nec to %struct.NEC*
  178. // AMDGCN: %[[ned:.*]] = addrspacecast %struct.NED addrspace(5)* %ned to %struct.NED*
  179. // AMDGCN: %[[ncv:.*]] = addrspacecast %struct.NCV addrspace(5)* %ncv to %struct.NCV*
  180. // AMDGCN: %[[vd:.*]] = addrspacecast %struct.VD addrspace(5)* %vd to %struct.VD*
  181. // AMDGCN: %[[ncf:.*]] = addrspacecast %struct.NCF addrspace(5)* %ncf to %struct.NCF*
  182. // AMDGCN: %[[ncfs:.*]] = addrspacecast %struct.NCFS addrspace(5)* %ncfs to %struct.NCFS*
  183. // AMDGCN: %[[utc:.*]] = addrspacecast %struct.UTC addrspace(5)* %utc to %struct.UTC*
  184. // AMDGCN: %[[netc:.*]] = addrspacecast %struct.NETC addrspace(5)* %netc to %struct.NETC*
  185. // AMDGCN: %[[ec_i_ec:.*]] = addrspacecast %struct.EC_I_EC addrspace(5)* %ec_i_ec to %struct.EC_I_EC*
  186. // AMDGCN: %[[ec_i_ec1:.*]] = addrspacecast %struct.EC_I_EC1 addrspace(5)* %ec_i_ec1 to %struct.EC_I_EC1*
  187. // AMDGCN: %[[t_v_t:.*]] = addrspacecast %struct.T_V_T addrspace(5)* %t_v_t to %struct.T_V_T*
  188. // AMDGCN: %[[t_b_nec:.*]] = addrspacecast %struct.T_B_NEC addrspace(5)* %t_b_nec to %struct.T_B_NEC*
  189. // AMDGCN: %[[t_f_nec:.*]] = addrspacecast %struct.T_F_NEC addrspace(5)* %t_f_nec to %struct.T_F_NEC*
  190. // AMDGCN: %[[t_fa_nec:.*]] = addrspacecast %struct.T_FA_NEC addrspace(5)* %t_fa_nec to %struct.T_FA_NEC*
  191. // AMDGCN: %[[t_b_ned:.*]] = addrspacecast %struct.T_B_NED addrspace(5)* %t_b_ned to %struct.T_B_NED*
  192. // AMDGCN: %[[t_f_ned:.*]] = addrspacecast %struct.T_F_NED addrspace(5)* %t_f_ned to %struct.T_F_NED*
  193. // AMDGCN: %[[t_fa_ned:.*]] = addrspacecast %struct.T_FA_NED addrspace(5)* %t_fa_ned to %struct.T_FA_NED*
  194. T t;
  195. // DEVICE-NOT: call
  196. EC ec;
  197. // DEVICE: call void @_ZN2ECC1Ev(%struct.EC* %[[ec]])
  198. ED ed;
  199. // DEVICE-NOT: call
  200. ECD ecd;
  201. // DEVICE: call void @_ZN3ECDC1Ev(%struct.ECD* %[[ecd]])
  202. ETC etc;
  203. // DEVICE: call void @_ZN3ETCC1IJEEEDpT_(%struct.ETC* %[[etc]])
  204. UC uc;
  205. // undefined constructor -- not allowed
  206. // DEVICE: call void @_ZN2UCC1Ev(%struct.UC* %[[uc]])
  207. UD ud;
  208. // undefined destructor -- not allowed
  209. // DEVICE-NOT: call
  210. ECI eci;
  211. // empty constructor w/ initializer list -- not allowed
  212. // DEVICE: call void @_ZN3ECIC1Ev(%struct.ECI* %[[eci]])
  213. NEC nec;
  214. // non-empty constructor -- not allowed
  215. // DEVICE: call void @_ZN3NECC1Ev(%struct.NEC* %[[nec]])
  216. // non-empty destructor -- not allowed
  217. NED ned;
  218. // no-constructor, virtual method -- not allowed
  219. // DEVICE: call void @_ZN3NCVC1Ev(%struct.NCV* %[[ncv]])
  220. NCV ncv;
  221. // DEVICE-NOT: call
  222. VD vd;
  223. // DEVICE: call void @_ZN2VDC1Ev(%struct.VD* %[[vd]])
  224. NCF ncf;
  225. // DEVICE: call void @_ZN3NCFC1Ev(%struct.NCF* %[[ncf]])
  226. NCFS ncfs;
  227. // DEVICE: call void @_ZN4NCFSC1Ev(%struct.NCFS* %[[ncfs]])
  228. UTC utc;
  229. // DEVICE: call void @_ZN3UTCC1IJEEEDpT_(%struct.UTC* %[[utc]])
  230. NETC netc;
  231. // DEVICE: call void @_ZN4NETCC1IJEEEDpT_(%struct.NETC* %[[netc]])
  232. T_B_T t_b_t;
  233. // DEVICE-NOT: call
  234. T_F_T t_f_t;
  235. // DEVICE-NOT: call
  236. T_FA_T t_fa_t;
  237. // DEVICE-NOT: call
  238. EC_I_EC ec_i_ec;
  239. // DEVICE: call void @_ZN7EC_I_ECC1Ev(%struct.EC_I_EC* %[[ec_i_ec]])
  240. EC_I_EC1 ec_i_ec1;
  241. // DEVICE: call void @_ZN8EC_I_EC1C1Ev(%struct.EC_I_EC1* %[[ec_i_ec1]])
  242. T_V_T t_v_t;
  243. // DEVICE: call void @_ZN5T_V_TC1Ev(%struct.T_V_T* %[[t_v_t]])
  244. T_B_NEC t_b_nec;
  245. // DEVICE: call void @_ZN7T_B_NECC1Ev(%struct.T_B_NEC* %[[t_b_nec]])
  246. T_F_NEC t_f_nec;
  247. // DEVICE: call void @_ZN7T_F_NECC1Ev(%struct.T_F_NEC* %[[t_f_nec]])
  248. T_FA_NEC t_fa_nec;
  249. // DEVICE: call void @_ZN8T_FA_NECC1Ev(%struct.T_FA_NEC* %[[t_fa_nec]])
  250. T_B_NED t_b_ned;
  251. // DEVICE-NOT: call
  252. T_F_NED t_f_ned;
  253. // DEVICE-NOT: call
  254. T_FA_NED t_fa_ned;
  255. // DEVICE-NOT: call
  256. static __shared__ EC s_ec;
  257. // DEVICE-NOT: call void @_ZN2ECC1Ev(%struct.EC* addrspacecast (%struct.EC addrspace(3)* @_ZZ2dfvE4s_ec to %struct.EC*))
  258. static __shared__ ETC s_etc;
  259. // DEVICE-NOT: call void @_ZN3ETCC1IJEEEDpT_(%struct.ETC* addrspacecast (%struct.ETC addrspace(3)* @_ZZ2dfvE5s_etc to %struct.ETC*))
  260. static const int const_array[] = {1, 2, 3, 4, 5};
  261. static const int const_int = 123;
  262. // anchor point separating constructors and destructors
  263. df(); // DEVICE: call void @_Z2dfv()
  264. // Verify that we only call non-empty destructors
  265. // DEVICE-NEXT: call void @_ZN8T_FA_NEDD1Ev(%struct.T_FA_NED* %[[t_fa_ned]])
  266. // DEVICE-NEXT: call void @_ZN7T_F_NEDD1Ev(%struct.T_F_NED* %[[t_f_ned]])
  267. // DEVICE-NEXT: call void @_ZN7T_B_NEDD1Ev(%struct.T_B_NED* %[[t_b_ned]])
  268. // DEVICE-NEXT: call void @_ZN2VDD1Ev(%struct.VD* %[[vd]])
  269. // DEVICE-NEXT: call void @_ZN3NEDD1Ev(%struct.NED* %[[ned]])
  270. // DEVICE-NEXT: call void @_ZN2UDD1Ev(%struct.UD* %[[ud]])
  271. // DEVICE-NEXT: call void @_ZN3ECDD1Ev(%struct.ECD* %[[ecd]])
  272. // DEVICE-NEXT: call void @_ZN2EDD1Ev(%struct.ED* %[[ed]])
  273. // DEVICE-NEXT: ret void
  274. }
  275. // We should not emit global init function.
  276. // DEVICE-NOT: @_GLOBAL__sub_I