opencl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /* GCC-StarPU
  2. Copyright (C) 2012 Inria
  3. GCC-StarPU is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. GCC-StarPU is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GCC-StarPU. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <starpu-gcc/config.h>
  14. /* We must include starpu.h here, otherwise gcc will complain about a poisoned
  15. malloc in xmmintrin.h. */
  16. #include <starpu.h>
  17. #include <gcc-plugin.h>
  18. #include <plugin-version.h>
  19. #include <plugin.h>
  20. #include <tree.h>
  21. #include <tree-iterator.h>
  22. #include <gimple.h>
  23. #include <cgraph.h>
  24. #include <toplev.h>
  25. #include <langhooks.h>
  26. #ifdef HAVE_C_FAMILY_C_COMMON_H
  27. # include <c-family/c-common.h>
  28. #elif HAVE_C_COMMON_H
  29. # include <c-common.h>
  30. #endif
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <sys/mman.h>
  34. #include <starpu-gcc/utils.h>
  35. #include <starpu-gcc/tasks.h>
  36. /* Search path for OpenCL source files for the `opencl' pragma, as a
  37. `TREE_LIST'. */
  38. tree opencl_include_dirs = NULL_TREE;
  39. /* Names of data structures defined in <starpu.h>. */
  40. static const char opencl_program_struct_tag[] = "starpu_opencl_program";
  41. /* Return the type corresponding to OPENCL_PROGRAM_STRUCT_TAG. */
  42. static tree
  43. opencl_program_type (void)
  44. {
  45. tree t = TREE_TYPE (type_decl_for_struct_tag (opencl_program_struct_tag));
  46. if (TYPE_SIZE (t) == NULL_TREE)
  47. {
  48. /* Incomplete type definition, for instance because <starpu_opencl.h>
  49. wasn't included. */
  50. error_at (UNKNOWN_LOCATION, "StarPU OpenCL support is lacking");
  51. t = error_mark_node;
  52. }
  53. return t;
  54. }
  55. static tree
  56. opencl_kernel_type (void)
  57. {
  58. tree t = lookup_name (get_identifier ("cl_kernel"));
  59. gcc_assert (t != NULL_TREE);
  60. if (TREE_CODE (t) == TYPE_DECL)
  61. t = TREE_TYPE (t);
  62. gcc_assert (TYPE_P (t));
  63. return t;
  64. }
  65. static tree
  66. opencl_command_queue_type (void)
  67. {
  68. tree t = lookup_name (get_identifier ("cl_command_queue"));
  69. gcc_assert (t != NULL_TREE);
  70. if (TREE_CODE (t) == TYPE_DECL)
  71. t = TREE_TYPE (t);
  72. gcc_assert (TYPE_P (t));
  73. return t;
  74. }
  75. static tree
  76. opencl_event_type (void)
  77. {
  78. tree t = lookup_name (get_identifier ("cl_event"));
  79. gcc_assert (t != NULL_TREE);
  80. if (TREE_CODE (t) == TYPE_DECL)
  81. t = TREE_TYPE (t);
  82. gcc_assert (TYPE_P (t));
  83. return t;
  84. }
  85. /* Return a private global string literal VAR_DECL, whose contents are the
  86. LEN bytes at CONTENTS. */
  87. static tree
  88. build_string_variable (location_t loc, const char *name_seed,
  89. const char *contents, size_t len)
  90. {
  91. tree decl;
  92. decl = build_decl (loc, VAR_DECL, create_tmp_var_name (name_seed),
  93. string_type_node);
  94. TREE_PUBLIC (decl) = false;
  95. TREE_STATIC (decl) = true;
  96. TREE_USED (decl) = true;
  97. DECL_INITIAL (decl) = /* XXX: off-by-one? */
  98. build_string_literal (len + 1, contents);
  99. DECL_ARTIFICIAL (decl) = true;
  100. return decl;
  101. }
  102. /* Return a VAR_DECL for a string variable containing the contents of FILE,
  103. which is looked for in each of the directories listed in SEARCH_PATH. If
  104. FILE could not be found, return NULL_TREE. */
  105. static tree
  106. build_variable_from_file_contents (location_t loc,
  107. const char *name_seed,
  108. const char *file,
  109. const_tree search_path)
  110. {
  111. gcc_assert (search_path != NULL_TREE
  112. && TREE_CODE (search_path) == TREE_LIST);
  113. int err, dir_fd;
  114. struct stat st;
  115. const_tree dirs;
  116. tree var = NULL_TREE;
  117. /* Look for FILE in each directory in SEARCH_PATH, and pick the first one
  118. that matches. */
  119. for (err = ENOENT, dir_fd = -1, dirs = search_path;
  120. (err != 0 || err == ENOENT) && dirs != NULL_TREE;
  121. dirs = TREE_CHAIN (dirs))
  122. {
  123. gcc_assert (TREE_VALUE (dirs) != NULL_TREE
  124. && TREE_CODE (TREE_VALUE (dirs)) == STRING_CST);
  125. dir_fd = open (TREE_STRING_POINTER (TREE_VALUE (dirs)),
  126. O_DIRECTORY | O_RDONLY);
  127. if (dir_fd < 0)
  128. err = ENOENT;
  129. else
  130. {
  131. err = fstatat (dir_fd, file, &st, 0);
  132. if (err != 0)
  133. close (dir_fd);
  134. else
  135. /* Leave DIRS unchanged so it can be referred to in diagnostics
  136. below. */
  137. break;
  138. }
  139. }
  140. if (err != 0 || dir_fd < 0)
  141. error_at (loc, "failed to access %qs: %m", file);
  142. else if (st.st_size == 0)
  143. {
  144. error_at (loc, "source file %qs is empty", file);
  145. close (dir_fd);
  146. }
  147. else
  148. {
  149. if (verbose_output_p)
  150. inform (loc, "found file %qs in %qs",
  151. file, TREE_STRING_POINTER (TREE_VALUE (dirs)));
  152. int fd;
  153. fd = openat (dir_fd, file, O_RDONLY);
  154. close (dir_fd);
  155. if (fd < 0)
  156. error_at (loc, "failed to open %qs: %m", file);
  157. else
  158. {
  159. void *contents;
  160. contents = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  161. if (contents == NULL)
  162. error_at (loc, "failed to map contents of %qs: %m", file);
  163. else
  164. {
  165. var = build_string_variable (loc, name_seed,
  166. (char *) contents, st.st_size);
  167. pushdecl (var);
  168. munmap (contents, st.st_size);
  169. }
  170. close (fd);
  171. }
  172. }
  173. return var;
  174. }
  175. /* Return an expression that, given the OpenCL error code in ERROR_VAR,
  176. returns a string. */
  177. static tree
  178. build_opencl_error_string (tree error_var)
  179. {
  180. static tree clstrerror_fn;
  181. LOOKUP_STARPU_FUNCTION (clstrerror_fn, "starpu_opencl_error_string");
  182. return build_call_expr (clstrerror_fn, 1, error_var);
  183. }
  184. /* Return an error-checking `clSetKernelArg' call for argument ARG, at
  185. index IDX, of KERNEL. */
  186. static tree
  187. build_opencl_set_kernel_arg_call (location_t loc, tree fn,
  188. tree kernel, unsigned int idx,
  189. tree arg)
  190. {
  191. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
  192. && TREE_TYPE (kernel) == opencl_kernel_type ());
  193. static tree setkernarg_fn;
  194. LOOKUP_STARPU_FUNCTION (setkernarg_fn, "clSetKernelArg");
  195. tree call = build_call_expr (setkernarg_fn, 4, kernel,
  196. build_int_cst (integer_type_node, idx),
  197. size_in_bytes (TREE_TYPE (arg)),
  198. build_addr (arg, fn));
  199. tree error_var = build_decl (loc, VAR_DECL,
  200. create_tmp_var_name ("setkernelarg_error"),
  201. integer_type_node);
  202. DECL_ARTIFICIAL (error_var) = true;
  203. DECL_CONTEXT (error_var) = fn;
  204. tree assignment = build2 (INIT_EXPR, TREE_TYPE (error_var),
  205. error_var, call);
  206. /* Build `if (ERROR_VAR != 0) error ();'. */
  207. tree cond;
  208. cond = build3 (COND_EXPR, void_type_node,
  209. build2 (NE_EXPR, boolean_type_node,
  210. error_var, integer_zero_node),
  211. build_error_statements (loc, error_var,
  212. build_opencl_error_string,
  213. "failed to set OpenCL kernel "
  214. "argument %d", idx),
  215. NULL_TREE);
  216. tree stmts = NULL_TREE;
  217. append_to_statement_list (assignment, &stmts);
  218. append_to_statement_list (cond, &stmts);
  219. return build4 (TARGET_EXPR, void_type_node, error_var,
  220. stmts, NULL_TREE, NULL_TREE);
  221. }
  222. /* Return the sequence of `clSetKernelArg' calls for KERNEL. */
  223. static tree
  224. build_opencl_set_kernel_arg_calls (location_t loc, tree task_impl,
  225. tree kernel)
  226. {
  227. gcc_assert (task_implementation_p (task_impl));
  228. size_t n;
  229. tree arg, stmts = NULL_TREE;
  230. for (arg = DECL_ARGUMENTS (task_impl), n = 0;
  231. arg != NULL_TREE;
  232. arg = TREE_CHAIN (arg), n++)
  233. {
  234. tree call = build_opencl_set_kernel_arg_call (loc, task_impl,
  235. kernel, n, arg);
  236. append_to_statement_list (call, &stmts);
  237. }
  238. return stmts;
  239. }
  240. /* Define a body for TASK_IMPL that loads OpenCL source from FILE and calls
  241. KERNEL. */
  242. static void
  243. define_opencl_task_implementation (location_t loc, tree task_impl,
  244. const char *file, const_tree kernel,
  245. tree groupsize)
  246. {
  247. gcc_assert (task_implementation_p (task_impl)
  248. && task_implementation_where (task_impl) == STARPU_OPENCL);
  249. gcc_assert (TREE_CODE (kernel) == STRING_CST);
  250. gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (groupsize)));
  251. local_define (tree, local_var, (tree type))
  252. {
  253. tree var = build_decl (loc, VAR_DECL,
  254. create_tmp_var_name ("opencl_var"),
  255. type);
  256. DECL_ARTIFICIAL (var) = true;
  257. DECL_CONTEXT (var) = task_impl;
  258. return var;
  259. };
  260. if (!verbose_output_p)
  261. /* No further warnings for this node. */
  262. TREE_NO_WARNING (task_impl) = true;
  263. static tree load_fn, load_kern_fn, enqueue_kern_fn, wid_fn, devid_fn, clfinish_fn,
  264. collect_stats_fn, release_ev_fn;
  265. if (load_fn == NULL_TREE)
  266. {
  267. load_fn =
  268. lookup_name (get_identifier ("starpu_opencl_load_opencl_from_string"));
  269. if (load_fn == NULL_TREE)
  270. {
  271. inform (loc, "no OpenCL support, task implementation %qE "
  272. "not generated", DECL_NAME (task_impl));
  273. return;
  274. }
  275. }
  276. LOOKUP_STARPU_FUNCTION (load_kern_fn, "starpu_opencl_load_kernel");
  277. LOOKUP_STARPU_FUNCTION (wid_fn, "starpu_worker_get_id");
  278. LOOKUP_STARPU_FUNCTION (devid_fn, "starpu_worker_get_devid");
  279. LOOKUP_STARPU_FUNCTION (enqueue_kern_fn, "clEnqueueNDRangeKernel");
  280. LOOKUP_STARPU_FUNCTION (clfinish_fn, "clFinish");
  281. LOOKUP_STARPU_FUNCTION (collect_stats_fn, "starpu_opencl_collect_stats");
  282. LOOKUP_STARPU_FUNCTION (release_ev_fn, "clReleaseEvent");
  283. if (verbose_output_p)
  284. inform (loc, "defining %qE, with OpenCL kernel %qs from file %qs",
  285. DECL_NAME (task_impl), TREE_STRING_POINTER (kernel), file);
  286. tree source_var;
  287. source_var = build_variable_from_file_contents (loc, "opencl_source",
  288. file, opencl_include_dirs);
  289. if (source_var != NULL_TREE)
  290. {
  291. /* Give TASK_IMPL an actual argument list. */
  292. DECL_ARGUMENTS (task_impl) = build_function_arguments (task_impl);
  293. tree prog_var, prog_loaded_var;
  294. /* Global variable to hold the `starpu_opencl_program' object. */
  295. prog_var = build_decl (loc, VAR_DECL,
  296. create_tmp_var_name ("opencl_program"),
  297. opencl_program_type ());
  298. TREE_PUBLIC (prog_var) = false;
  299. TREE_STATIC (prog_var) = true;
  300. TREE_USED (prog_var) = true;
  301. DECL_ARTIFICIAL (prog_var) = true;
  302. pushdecl (prog_var);
  303. /* Global variable indicating whether the program has already been
  304. loaded. */
  305. prog_loaded_var = build_decl (loc, VAR_DECL,
  306. create_tmp_var_name ("opencl_prog_loaded"),
  307. boolean_type_node);
  308. TREE_PUBLIC (prog_loaded_var) = false;
  309. TREE_STATIC (prog_loaded_var) = true;
  310. TREE_USED (prog_loaded_var) = true;
  311. DECL_ARTIFICIAL (prog_loaded_var) = true;
  312. DECL_INITIAL (prog_loaded_var) = build_zero_cst (boolean_type_node);
  313. pushdecl (prog_loaded_var);
  314. /* Build `starpu_opencl_load_opencl_from_string (SOURCE_VAR,
  315. &PROG_VAR, "")'. */
  316. tree load = build_call_expr (load_fn, 3, source_var,
  317. build_addr (prog_var, task_impl),
  318. build_string_literal (1, ""));
  319. tree load_stmts = NULL_TREE;
  320. append_to_statement_list (load, &load_stmts);
  321. append_to_statement_list (build2 (MODIFY_EXPR, boolean_type_node,
  322. prog_loaded_var,
  323. build_int_cst (boolean_type_node, 1)),
  324. &load_stmts);
  325. /* Build `if (!PROG_LOADED_VAR) { ...; PROG_LOADED_VAR = true; }'. */
  326. tree load_cond = build3 (COND_EXPR, void_type_node,
  327. prog_loaded_var,
  328. NULL_TREE,
  329. load_stmts);
  330. /* Local variables. */
  331. tree kernel_var, queue_var, event_var, group_size_var, ngroups_var,
  332. error_var;
  333. kernel_var = local_var (opencl_kernel_type ());
  334. queue_var = local_var (opencl_command_queue_type ());
  335. event_var = local_var (opencl_event_type ());
  336. group_size_var = local_var (size_type_node);
  337. ngroups_var = local_var (size_type_node);
  338. error_var = local_var (integer_type_node);
  339. /* Build `starpu_opencl_load_kernel (...)'.
  340. TODO: Check return value. */
  341. tree devid =
  342. build_call_expr (devid_fn, 1, build_call_expr (wid_fn, 0));
  343. tree load_kern = build_call_expr (load_kern_fn, 5,
  344. build_addr (kernel_var, task_impl),
  345. build_addr (queue_var, task_impl),
  346. build_addr (prog_var, task_impl),
  347. build_string_literal
  348. (TREE_STRING_LENGTH (kernel) + 1,
  349. TREE_STRING_POINTER (kernel)),
  350. devid);
  351. tree enqueue_kern =
  352. build_call_expr (enqueue_kern_fn, 9,
  353. queue_var, kernel_var,
  354. build_int_cst (integer_type_node, 1),
  355. null_pointer_node,
  356. build_addr (group_size_var, task_impl),
  357. build_addr (ngroups_var, task_impl),
  358. integer_zero_node,
  359. null_pointer_node,
  360. build_addr (event_var, task_impl));
  361. tree enqueue_err =
  362. build2 (INIT_EXPR, TREE_TYPE (error_var), error_var, enqueue_kern);
  363. tree enqueue_cond =
  364. build3 (COND_EXPR, void_type_node,
  365. build2 (NE_EXPR, boolean_type_node,
  366. error_var, integer_zero_node),
  367. build_error_statements (loc, error_var,
  368. build_opencl_error_string,
  369. "failed to enqueue kernel"),
  370. NULL_TREE);
  371. tree clfinish =
  372. build_call_expr (clfinish_fn, 1, queue_var);
  373. tree collect_stats =
  374. build_call_expr (collect_stats_fn, 1, event_var);
  375. tree release_ev =
  376. build_call_expr (release_ev_fn, 1, event_var);
  377. tree enqueue_stmts = NULL_TREE;
  378. append_to_statement_list (enqueue_err, &enqueue_stmts);
  379. append_to_statement_list (enqueue_cond, &enqueue_stmts);
  380. /* TODO: Build `clFinish', `clReleaseEvent', & co. */
  381. /* Put it all together. */
  382. tree stmts = NULL_TREE;
  383. append_to_statement_list (load_cond, &stmts);
  384. append_to_statement_list (load_kern, &stmts);
  385. append_to_statement_list (build_opencl_set_kernel_arg_calls (loc,
  386. task_impl,
  387. kernel_var),
  388. &stmts);
  389. /* TODO: Support user-provided values. */
  390. append_to_statement_list (build2 (INIT_EXPR, TREE_TYPE (group_size_var),
  391. group_size_var,
  392. fold_convert (TREE_TYPE (group_size_var),
  393. groupsize)),
  394. &stmts);
  395. append_to_statement_list (build2 (INIT_EXPR, TREE_TYPE (ngroups_var),
  396. ngroups_var,
  397. build_int_cst (TREE_TYPE (ngroups_var),
  398. 1)),
  399. &stmts);
  400. append_to_statement_list (build4 (TARGET_EXPR, void_type_node,
  401. error_var, enqueue_stmts,
  402. NULL_TREE, NULL_TREE),
  403. &stmts);
  404. append_to_statement_list (clfinish, &stmts);
  405. append_to_statement_list (collect_stats, &stmts);
  406. append_to_statement_list (release_ev, &stmts);
  407. /* Bind the local vars. */
  408. tree vars = chain_trees (kernel_var, queue_var, event_var,
  409. group_size_var, ngroups_var, NULL_TREE);
  410. tree bind = build3 (BIND_EXPR, void_type_node, vars, stmts,
  411. build_block (vars, NULL_TREE, task_impl, NULL_TREE));
  412. TREE_USED (task_impl) = true;
  413. TREE_STATIC (task_impl) = true;
  414. DECL_EXTERNAL (task_impl) = false;
  415. DECL_ARTIFICIAL (task_impl) = true;
  416. DECL_SAVED_TREE (task_impl) = bind;
  417. DECL_INITIAL (task_impl) = BIND_EXPR_BLOCK (bind);
  418. DECL_RESULT (task_impl) =
  419. build_decl (loc, RESULT_DECL, NULL_TREE, void_type_node);
  420. /* Compile TASK_IMPL. */
  421. rest_of_decl_compilation (task_impl, true, 0);
  422. allocate_struct_function (task_impl, false);
  423. cgraph_finalize_function (task_impl, false);
  424. cgraph_mark_needed_node (cgraph_get_node (task_impl));
  425. /* Generate a wrapper for TASK_IMPL, and possibly the body of its task.
  426. This needs to be done explicitly here, because otherwise
  427. `handle_pre_genericize' would never see TASK_IMPL's task. */
  428. tree task = task_implementation_task (task_impl);
  429. if (!TREE_STATIC (task))
  430. {
  431. declare_codelet (task);
  432. define_task (task);
  433. /* Compile TASK's body. */
  434. rest_of_decl_compilation (task, true, 0);
  435. allocate_struct_function (task, false);
  436. cgraph_finalize_function (task, false);
  437. cgraph_mark_needed_node (cgraph_get_node (task));
  438. }
  439. }
  440. else
  441. DECL_SAVED_TREE (task_impl) = error_mark_node;
  442. return;
  443. }
  444. /* Handle the `opencl' pragma, which defines an OpenCL task
  445. implementation. */
  446. void
  447. handle_pragma_opencl (struct cpp_reader *reader)
  448. {
  449. tree args;
  450. location_t loc;
  451. loc = cpp_peek_token (reader, 0)->src_loc;
  452. if (current_function_decl != NULL_TREE)
  453. {
  454. error_at (loc, "%<starpu opencl%> pragma can only be used "
  455. "at the top-level");
  456. return;
  457. }
  458. args = read_pragma_expressions ("opencl", loc);
  459. if (args == NULL_TREE)
  460. return;
  461. /* TODO: Add "number of groups" arguments. */
  462. if (list_length (args) < 4)
  463. {
  464. error_at (loc, "wrong number of arguments for %<starpu opencl%> pragma");
  465. return;
  466. }
  467. if (task_implementation_p (TREE_VALUE (args)))
  468. {
  469. tree task_impl = TREE_VALUE (args);
  470. if (task_implementation_where (task_impl) == STARPU_OPENCL)
  471. {
  472. args = TREE_CHAIN (args);
  473. if (TREE_CODE (TREE_VALUE (args)) == STRING_CST)
  474. {
  475. tree file = TREE_VALUE (args);
  476. args = TREE_CHAIN (args);
  477. if (TREE_CODE (TREE_VALUE (args)) == STRING_CST)
  478. {
  479. tree kernel = TREE_VALUE (args);
  480. args = TREE_CHAIN (args);
  481. if (TREE_TYPE (TREE_VALUE (args)) != NULL_TREE &&
  482. INTEGRAL_TYPE_P (TREE_TYPE (TREE_VALUE (args))))
  483. {
  484. tree groupsize = TREE_VALUE (args);
  485. if (TREE_CHAIN (args) == NULL_TREE)
  486. define_opencl_task_implementation (loc, task_impl,
  487. TREE_STRING_POINTER (file),
  488. kernel, groupsize);
  489. else
  490. error_at (loc, "junk after %<starpu opencl%> pragma");
  491. }
  492. else
  493. error_at (loc, "%<groupsize%> argument must be an integral type");
  494. }
  495. else
  496. error_at (loc, "%<kernel%> argument must be a string constant");
  497. }
  498. else
  499. error_at (loc, "%<file%> argument must be a string constant");
  500. }
  501. else
  502. error_at (loc, "%qE is not an OpenCL task implementation",
  503. DECL_NAME (task_impl));
  504. }
  505. else
  506. error_at (loc, "%qE is not a task implementation", TREE_VALUE (args));
  507. }
  508. /* Diagnose use of C types that are either nonexistent or different in
  509. OpenCL. */
  510. void
  511. validate_opencl_argument_type (location_t loc, const_tree type)
  512. {
  513. /* When TYPE is a pointer type, get to the base element type. */
  514. for (; POINTER_TYPE_P (type); type = TREE_TYPE (type));
  515. if (!RECORD_OR_UNION_TYPE_P (type) && !VOID_TYPE_P (type))
  516. {
  517. tree decl = TYPE_NAME (type);
  518. if (DECL_P (decl))
  519. {
  520. static const struct { const char *c; const char *cl; }
  521. type_map[] =
  522. {
  523. /* Scalar types defined in OpenCL 1.2. See
  524. <http://www.khronos.org/files/opencl-1-2-quick-reference-card.pdf>. */
  525. { "char", "cl_char" },
  526. { "signed char", "cl_char" },
  527. { "unsigned char", "cl_uchar" },
  528. { "uchar", "cl_uchar" },
  529. { "short int", "cl_short" },
  530. { "unsigned short", "cl_ushort" },
  531. { "int", "cl_int" },
  532. { "unsigned int", "cl_uint" },
  533. { "uint", "cl_uint" },
  534. { "long int", "cl_long" },
  535. { "long unsigned int", "cl_ulong" },
  536. { "ulong", "cl_ulong" },
  537. { "float", "cl_float" },
  538. { "double", "cl_double" },
  539. { NULL, NULL }
  540. };
  541. const char *c_name = IDENTIFIER_POINTER (DECL_NAME (decl));
  542. const char *cl_name =
  543. ({
  544. size_t i;
  545. for (i = 0; type_map[i].c != NULL; i++)
  546. {
  547. if (strcmp (type_map[i].c, c_name) == 0)
  548. break;
  549. }
  550. type_map[i].cl;
  551. });
  552. if (cl_name != NULL)
  553. {
  554. tree cl_type = lookup_name (get_identifier (cl_name));
  555. if (cl_type != NULL_TREE)
  556. {
  557. if (DECL_P (cl_type))
  558. cl_type = TREE_TYPE (cl_type);
  559. if (!lang_hooks.types_compatible_p ((tree) type, cl_type))
  560. {
  561. tree st, sclt;
  562. st = c_common_signed_type ((tree) type);
  563. sclt = c_common_signed_type (cl_type);
  564. if (st == sclt)
  565. warning_at (loc, 0, "C type %qE differs in signedness "
  566. "from the same-named OpenCL type",
  567. DECL_NAME (decl));
  568. else
  569. /* TYPE should be avoided because the it differs from
  570. CL_TYPE, and thus cannot be used safely in
  571. `clSetKernelArg'. */
  572. warning_at (loc, 0, "C type %qE differs from the "
  573. "same-named OpenCL type",
  574. DECL_NAME (decl));
  575. }
  576. }
  577. /* Otherwise we can't conclude. It could be that <CL/cl.h>
  578. wasn't included in the program, for instance. */
  579. }
  580. else
  581. /* Recommend against use of `size_t', etc. */
  582. warning_at (loc, 0, "%qE does not correspond to a known "
  583. "OpenCL type", DECL_NAME (decl));
  584. }
  585. }
  586. }