starpu.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /* GCC-StarPU
  2. Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  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. /* Use extensions of the GNU C Library. */
  14. #define _GNU_SOURCE 1
  15. #include <starpu-gcc-config.h>
  16. int plugin_is_GPL_compatible;
  17. /* #define ENABLE_TREE_CHECKING 1 */
  18. #include <gcc-plugin.h>
  19. #include <plugin-version.h>
  20. #include <plugin.h>
  21. #include <cpplib.h>
  22. #include <tree.h>
  23. #include <tree-iterator.h>
  24. #ifdef HAVE_C_FAMILY_C_COMMON_H
  25. # include <c-family/c-common.h>
  26. #elif HAVE_C_COMMON_H
  27. # include <c-common.h>
  28. #endif
  29. #ifdef HAVE_C_FAMILY_C_PRAGMA_H
  30. # include <c-family/c-pragma.h>
  31. #elif HAVE_C_PRAGMA_H
  32. # include <c-pragma.h>
  33. #endif
  34. #include <tm.h>
  35. #include <gimple.h>
  36. #include <tree-pass.h>
  37. #include <tree-flow.h>
  38. #include <cgraph.h>
  39. #include <gimple.h>
  40. #include <toplev.h>
  41. #include <stdio.h>
  42. /* Don't include the dreaded proprietary headers that we don't need anyway.
  43. In particular, this waives the obligation to reproduce their silly
  44. disclaimer. */
  45. #define STARPU_DONT_INCLUDE_CUDA_HEADERS
  46. #include <starpu.h> /* for `STARPU_CPU' & co. */
  47. /* The name of this plug-in. */
  48. static const char plugin_name[] = "starpu";
  49. /* Names of public attributes. */
  50. static const char task_attribute_name[] = "task";
  51. static const char task_implementation_attribute_name[] = "task_implementation";
  52. static const char output_attribute_name[] = "output";
  53. static const char heap_allocated_attribute_name[] = "heap_allocated";
  54. /* Names of attributes used internally. */
  55. static const char task_codelet_attribute_name[] = ".codelet";
  56. static const char task_implementation_list_attribute_name[] =
  57. ".task_implementation_list";
  58. static const char task_implementation_wrapper_attribute_name[] =
  59. ".task_implementation_wrapper";
  60. /* Names of data structures defined in <starpu.h>. */
  61. static const char codelet_struct_name[] = "starpu_codelet_gcc";
  62. /* Cached function declarations. */
  63. static tree unpack_fn;
  64. /* Forward declarations. */
  65. static tree build_codelet_declaration (tree task_decl);
  66. static tree build_task_body (const_tree task_decl);
  67. static tree build_pointer_lookup (tree pointer);
  68. static bool task_p (const_tree decl);
  69. static bool task_implementation_p (const_tree decl);
  70. static int task_implementation_target_to_int (const_tree target);
  71. /* Lookup the StarPU function NAME in the global scope and store the result
  72. in VAR (this can't be done from `lower_starpu'.) */
  73. #define LOOKUP_STARPU_FUNCTION(var, name) \
  74. if ((var) == NULL_TREE) \
  75. { \
  76. (var) = lookup_name (get_identifier (name)); \
  77. gcc_assert ((var) != NULL_TREE && TREE_CODE (var) == FUNCTION_DECL); \
  78. }
  79. /* Useful code backported from GCC 4.6. */
  80. #if !HAVE_DECL_BUILD_CALL_EXPR_LOC_ARRAY
  81. static tree
  82. build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
  83. {
  84. tree fntype = TREE_TYPE (fndecl);
  85. tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
  86. return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
  87. }
  88. #endif
  89. #if !HAVE_DECL_BUILD_CALL_EXPR_LOC_VEC
  90. static tree
  91. build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
  92. {
  93. return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
  94. VEC_address (tree, vec));
  95. }
  96. #endif
  97. /* Helpers. */
  98. /* Build a reference to the INDEXth element of ARRAY. `build_array_ref' is
  99. not exported, so we roll our own.
  100. FIXME: This version may not work for array types and doesn't do as much
  101. type-checking as `build_array_ref'. */
  102. static tree
  103. array_ref (tree array, size_t index)
  104. {
  105. gcc_assert (POINTER_TYPE_P (TREE_TYPE (array)));
  106. tree pointer_plus_offset =
  107. index > 0
  108. ? build_binary_op (UNKNOWN_LOCATION, PLUS_EXPR,
  109. array,
  110. build_int_cstu (integer_type_node, index),
  111. 0)
  112. : array;
  113. gcc_assert (POINTER_TYPE_P (TREE_TYPE (pointer_plus_offset)));
  114. return build_indirect_ref (UNKNOWN_LOCATION,
  115. pointer_plus_offset,
  116. RO_ARRAY_INDEXING);
  117. }
  118. /* Like `build_constructor_from_list', but sort VALS according to their
  119. offset in struct TYPE. Inspired by `gnat_build_constructor'. */
  120. static tree
  121. build_constructor_from_unsorted_list (tree type, tree vals)
  122. {
  123. int compare_elmt_bitpos (const void *rt1, const void *rt2)
  124. {
  125. const constructor_elt *elmt1 = (constructor_elt *) rt1;
  126. const constructor_elt *elmt2 = (constructor_elt *) rt2;
  127. const_tree field1 = elmt1->index;
  128. const_tree field2 = elmt2->index;
  129. int ret
  130. = tree_int_cst_compare (bit_position (field1), bit_position (field2));
  131. return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
  132. }
  133. tree t;
  134. VEC(constructor_elt,gc) *v = NULL;
  135. if (vals)
  136. {
  137. v = VEC_alloc (constructor_elt, gc, list_length (vals));
  138. for (t = vals; t; t = TREE_CHAIN (t))
  139. CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
  140. }
  141. /* Sort field initializers by field offset. */
  142. VEC_qsort (constructor_elt, v, compare_elmt_bitpos);
  143. return build_constructor (type, v);
  144. }
  145. /* Return true if LST holds the void type. */
  146. bool
  147. void_type_p (const_tree lst)
  148. {
  149. gcc_assert (TREE_CODE (lst) == TREE_LIST);
  150. return VOID_TYPE_P (TREE_VALUE (lst));
  151. }
  152. /* Debugging helpers. */
  153. static tree build_printf (const char *, ...)
  154. __attribute__ ((format (printf, 1, 2)));
  155. static tree
  156. build_printf (const char *fmt, ...)
  157. {
  158. tree call;
  159. char *str;
  160. va_list args;
  161. va_start (args, fmt);
  162. vasprintf (&str, fmt, args);
  163. call = build_call_expr (built_in_decls[BUILT_IN_PUTS], 1,
  164. build_string_literal (strlen (str) + 1, str));
  165. free (str);
  166. va_end (args);
  167. return call;
  168. }
  169. static tree
  170. build_hello_world (void)
  171. {
  172. return build_printf ("Hello, StarPU!");
  173. }
  174. /* List and vector utilities, à la SRFI-1. */
  175. static tree chain_trees (tree t, ...)
  176. __attribute__ ((sentinel));
  177. static tree
  178. chain_trees (tree t, ...)
  179. {
  180. va_list args;
  181. va_start (args, t);
  182. tree next, prev = t;
  183. for (prev = t, next = va_arg (args, tree);
  184. next != NULL_TREE;
  185. prev = next, next = va_arg (args, tree))
  186. TREE_CHAIN (prev) = next;
  187. va_end (args);
  188. return t;
  189. }
  190. static tree
  191. filter (bool (*pred) (const_tree), tree t)
  192. {
  193. tree result, lst;
  194. gcc_assert (TREE_CODE (t) == TREE_LIST);
  195. result = NULL_TREE;
  196. for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
  197. {
  198. if (pred (lst))
  199. result = tree_cons (TREE_PURPOSE (lst), TREE_VALUE (lst),
  200. result);
  201. }
  202. return nreverse (result);
  203. }
  204. static tree
  205. list_remove (bool (*pred) (const_tree), tree t)
  206. {
  207. bool opposite (const_tree t)
  208. {
  209. return !pred (t);
  210. }
  211. return filter (opposite, t);
  212. }
  213. /* Map FUNC over chain T. T does not have to be `TREE_LIST'; it can be a
  214. chain of arbitrary tree objects. */
  215. static tree
  216. map (tree (*func) (const_tree), tree t)
  217. {
  218. tree result, tail, lst;
  219. result = tail = NULL_TREE;
  220. for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
  221. {
  222. tree r = func (lst);
  223. if (tail != NULL_TREE)
  224. TREE_CHAIN (tail) = r;
  225. else
  226. result = r;
  227. tail = r;
  228. }
  229. return result;
  230. }
  231. static void
  232. for_each (void (*func) (tree), tree t)
  233. {
  234. tree lst;
  235. gcc_assert (TREE_CODE (t) == TREE_LIST);
  236. for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
  237. func (TREE_VALUE (lst));
  238. }
  239. /* Pragmas. */
  240. #define STARPU_PRAGMA_NAME_SPACE "starpu"
  241. static void
  242. handle_pragma_hello (struct cpp_reader *reader)
  243. {
  244. add_stmt (build_hello_world ());
  245. }
  246. /* Process `#pragma starpu initialize'.
  247. TODO: Parse and initialize some of the fields of `starpu_conf'. */
  248. static void
  249. handle_pragma_initialize (struct cpp_reader *reader)
  250. {
  251. static tree init_fn;
  252. LOOKUP_STARPU_FUNCTION (init_fn, "starpu_init");
  253. /* Call `starpu_init (NULL)'. */
  254. tree init = build_call_expr (init_fn, 1, build_zero_cst (ptr_type_node));
  255. add_stmt (init);
  256. }
  257. /* Process `#pragma starpu shutdown'. */
  258. static void
  259. handle_pragma_shutdown (struct cpp_reader *reader)
  260. {
  261. static tree shutdown_fn;
  262. LOOKUP_STARPU_FUNCTION (shutdown_fn, "starpu_shutdown");
  263. tree token;
  264. if (pragma_lex (&token) != CPP_EOF)
  265. error_at (cpp_peek_token (reader, 0)->src_loc,
  266. "junk after %<starpu shutdown%> pragma");
  267. else
  268. /* Call `starpu_shutdown ()'. */
  269. add_stmt (build_call_expr (shutdown_fn, 0));
  270. }
  271. static void
  272. handle_pragma_wait (struct cpp_reader *reader)
  273. {
  274. if (task_implementation_p (current_function_decl))
  275. {
  276. location_t loc;
  277. loc = cpp_peek_token (reader, 0)->src_loc;
  278. /* TODO: In the future we could generate a task for the continuation
  279. and have it depend on what's before here. */
  280. error_at (loc, "task implementation is not allowed to wait");
  281. }
  282. else
  283. {
  284. tree fndecl;
  285. fndecl = lookup_name (get_identifier ("starpu_task_wait_for_all"));
  286. gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
  287. add_stmt (build_call_expr (fndecl, 0));
  288. }
  289. }
  290. /* The minimal C expression parser. */
  291. extern int yyparse (location_t, const char *, tree *);
  292. extern int yydebug;
  293. /* Parse expressions from the CPP reader for PRAGMA, which is located at LOC.
  294. Return a TREE_LIST of C expressions. */
  295. static tree
  296. read_pragma_expressions (const char *pragma, location_t loc)
  297. {
  298. tree expr = NULL_TREE;
  299. if (yyparse (loc, pragma, &expr))
  300. /* Parse error or memory exhaustion. */
  301. expr = NULL_TREE;
  302. return expr;
  303. }
  304. /* Process `#pragma starpu register VAR [COUNT]' and emit the corresponding
  305. `starpu_vector_data_register' call. */
  306. static void
  307. handle_pragma_register (struct cpp_reader *reader)
  308. {
  309. tree args, ptr, count_arg;
  310. location_t loc;
  311. loc = cpp_peek_token (reader, 0)->src_loc;
  312. args = read_pragma_expressions ("register", loc);
  313. if (args == NULL_TREE)
  314. /* Parse error, presumably already handled by the parser. */
  315. return;
  316. /* First argument should be a pointer expression. */
  317. ptr = TREE_VALUE (args);
  318. args = TREE_CHAIN (args);
  319. if (ptr == error_mark_node)
  320. return;
  321. if (!POINTER_TYPE_P (TREE_TYPE (ptr))
  322. && TREE_CODE (TREE_TYPE (ptr)) != ARRAY_TYPE)
  323. {
  324. error_at (loc, "%qE is neither a pointer nor an array", ptr);
  325. return;
  326. }
  327. TREE_USED (ptr) = true;
  328. if (DECL_P (ptr))
  329. DECL_READ_P (ptr) = true;
  330. if (TREE_CODE (TREE_TYPE (ptr)) == ARRAY_TYPE
  331. && !DECL_EXTERNAL (ptr)
  332. && !TREE_STATIC (ptr)
  333. && !MAIN_NAME_P (DECL_NAME (current_function_decl)))
  334. warning_at (loc, 0, "using an on-stack array as a task input "
  335. "considered unsafe");
  336. /* Determine the number of elements in the vector. */
  337. tree count = NULL_TREE;
  338. if (TREE_CODE (TREE_TYPE (ptr)) == ARRAY_TYPE)
  339. {
  340. tree domain = TYPE_DOMAIN (TREE_TYPE (ptr));
  341. if (domain != NULL_TREE)
  342. {
  343. count = build_binary_op (loc, MINUS_EXPR,
  344. TYPE_MAX_VALUE (domain),
  345. TYPE_MIN_VALUE (domain),
  346. false);
  347. count = build_binary_op (loc, PLUS_EXPR,
  348. count,
  349. build_int_cstu (integer_type_node, 1),
  350. false);
  351. count = fold_convert (size_type_node, count);
  352. }
  353. }
  354. /* Second argument is optional but should be an integer. */
  355. count_arg = (args == NULL_TREE) ? NULL_TREE : TREE_VALUE (args);
  356. if (args != NULL_TREE)
  357. {
  358. args = TREE_CHAIN (args);
  359. TREE_CHAIN (count_arg) = NULL_TREE;
  360. }
  361. if (count_arg == NULL_TREE)
  362. {
  363. /* End of line reached: check whether the array size was
  364. determined. */
  365. if (count == NULL_TREE)
  366. {
  367. error_at (loc, "cannot determine size of array %qE", ptr);
  368. return;
  369. }
  370. }
  371. else if (count_arg == error_mark_node)
  372. /* COUNT_ARG could not be parsed and an error was already reported. */
  373. return;
  374. else if (!INTEGRAL_TYPE_P (TREE_TYPE (count_arg)))
  375. {
  376. error_at (loc, "%qE is not an integer", count_arg);
  377. return;
  378. }
  379. else
  380. {
  381. TREE_USED (count_arg) = true;
  382. if (DECL_P (count_arg))
  383. DECL_READ_P (count_arg) = true;
  384. if (count != NULL_TREE)
  385. {
  386. /* The number of elements of this array was already determined. */
  387. inform (loc,
  388. "element count can be omitted for bounded array %qE",
  389. ptr);
  390. if (count_arg != NULL_TREE)
  391. {
  392. if (TREE_CODE (count_arg) == INTEGER_CST)
  393. {
  394. if (!tree_int_cst_equal (count, count_arg))
  395. error_at (loc, "specified element count differs "
  396. "from actual size of array %qE",
  397. ptr);
  398. }
  399. else
  400. /* Using a variable to determine the array size whereas the
  401. array size is actually known statically. This looks like
  402. unreasonable code, so error out. */
  403. error_at (loc, "determining array size at run-time "
  404. "although array size is known at compile-time");
  405. }
  406. }
  407. else
  408. count = count_arg;
  409. }
  410. /* Any remaining args? */
  411. if (args != NULL_TREE)
  412. error_at (loc, "junk after %<starpu register%> pragma");
  413. /* If PTR is an array, take its address. */
  414. tree pointer =
  415. POINTER_TYPE_P (TREE_TYPE (ptr))
  416. ? ptr
  417. : build_addr (ptr, current_function_decl);
  418. /* Introduce a local variable to hold the handle. */
  419. tree handle_var = build_decl (loc, VAR_DECL, create_tmp_var_name (".handle"),
  420. ptr_type_node);
  421. DECL_CONTEXT (handle_var) = current_function_decl;
  422. DECL_ARTIFICIAL (handle_var) = true;
  423. DECL_INITIAL (handle_var) = NULL_TREE;
  424. tree register_fn =
  425. lookup_name (get_identifier ("starpu_vector_data_register"));
  426. /* Build `starpu_vector_data_register (&HANDLE_VAR, 0, POINTER,
  427. COUNT, sizeof *POINTER)' */
  428. tree call =
  429. build_call_expr (register_fn, 5,
  430. build_addr (handle_var, current_function_decl),
  431. build_zero_cst (uintptr_type_node), /* home node */
  432. pointer, count,
  433. size_in_bytes (TREE_TYPE (TREE_TYPE (ptr))));
  434. tree bind;
  435. bind = build3 (BIND_EXPR, void_type_node, handle_var, call,
  436. NULL_TREE);
  437. add_stmt (bind);
  438. }
  439. /* Process `#pragma starpu acquire VAR' and emit the corresponding
  440. `starpu_data_acquire' call. */
  441. static void
  442. handle_pragma_acquire (struct cpp_reader *reader)
  443. {
  444. static tree acquire_fn;
  445. LOOKUP_STARPU_FUNCTION (acquire_fn, "starpu_data_acquire");
  446. tree args, var;
  447. location_t loc;
  448. loc = cpp_peek_token (reader, 0)->src_loc;
  449. args = read_pragma_expressions ("acquire", loc);
  450. if (args == NULL_TREE)
  451. return;
  452. var = TREE_VALUE (args);
  453. if (var == error_mark_node)
  454. return;
  455. else if (TREE_CODE (TREE_TYPE (var)) != POINTER_TYPE
  456. && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
  457. {
  458. error_at (loc, "%qE is neither a pointer nor an array", var);
  459. return;
  460. }
  461. else if (TREE_CHAIN (var) != NULL_TREE)
  462. error_at (loc, "junk after %<starpu acquire%> pragma");
  463. /* If VAR is an array, take its address. */
  464. tree pointer =
  465. POINTER_TYPE_P (TREE_TYPE (var))
  466. ? var
  467. : build_addr (var, current_function_decl);
  468. /* Call `starpu_data_acquire (starpu_data_lookup (ptr), STARPU_RW)'.
  469. TODO: Support modes other than RW. */
  470. add_stmt (build_call_expr (acquire_fn, 2,
  471. build_pointer_lookup (pointer),
  472. build_int_cst (integer_type_node, STARPU_RW)));
  473. }
  474. /* Process `#pragma starpu unregister VAR' and emit the corresponding
  475. `starpu_data_unregister' call. */
  476. static void
  477. handle_pragma_unregister (struct cpp_reader *reader)
  478. {
  479. static tree unregister_fn;
  480. LOOKUP_STARPU_FUNCTION (unregister_fn, "starpu_data_unregister");
  481. tree args, var;
  482. location_t loc;
  483. loc = cpp_peek_token (reader, 0)->src_loc;
  484. args = read_pragma_expressions ("unregister", loc);
  485. if (args == NULL_TREE)
  486. return;
  487. var = TREE_VALUE (args);
  488. if (var == error_mark_node)
  489. return;
  490. else if (TREE_CODE (TREE_TYPE (var)) != POINTER_TYPE
  491. && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
  492. {
  493. error_at (loc, "%qE is neither a pointer nor an array", var);
  494. return;
  495. }
  496. else if (TREE_CHAIN (args) != NULL_TREE)
  497. error_at (loc, "junk after %<starpu unregister%> pragma");
  498. /* If VAR is an array, take its address. */
  499. tree pointer =
  500. POINTER_TYPE_P (TREE_TYPE (var))
  501. ? var
  502. : build_addr (var, current_function_decl);
  503. /* Call `starpu_data_unregister (starpu_data_lookup (ptr))'. */
  504. add_stmt (build_call_expr (unregister_fn, 1,
  505. build_pointer_lookup (pointer)));
  506. }
  507. static void
  508. register_pragmas (void *gcc_data, void *user_data)
  509. {
  510. c_register_pragma (STARPU_PRAGMA_NAME_SPACE, "hello",
  511. handle_pragma_hello);
  512. c_register_pragma_with_expansion (STARPU_PRAGMA_NAME_SPACE, "initialize",
  513. handle_pragma_initialize);
  514. c_register_pragma (STARPU_PRAGMA_NAME_SPACE, "wait",
  515. handle_pragma_wait);
  516. c_register_pragma_with_expansion (STARPU_PRAGMA_NAME_SPACE, "register",
  517. handle_pragma_register);
  518. c_register_pragma_with_expansion (STARPU_PRAGMA_NAME_SPACE, "acquire",
  519. handle_pragma_acquire);
  520. c_register_pragma_with_expansion (STARPU_PRAGMA_NAME_SPACE, "unregister",
  521. handle_pragma_unregister);
  522. c_register_pragma (STARPU_PRAGMA_NAME_SPACE, "shutdown",
  523. handle_pragma_shutdown);
  524. }
  525. /* Attributes. */
  526. /* Handle the `task' function attribute. */
  527. static tree
  528. handle_task_attribute (tree *node, tree name, tree args,
  529. int flags, bool *no_add_attrs)
  530. {
  531. tree fn;
  532. fn = *node;
  533. /* Get rid of the `task' attribute by default so that FN isn't further
  534. processed when it's erroneous. */
  535. *no_add_attrs = true;
  536. if (TREE_CODE (fn) != FUNCTION_DECL)
  537. error_at (DECL_SOURCE_LOCATION (fn),
  538. "%<task%> attribute only applies to functions");
  539. else
  540. {
  541. if (!VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn))))
  542. /* Raise an error but keep going to avoid spitting out too many
  543. errors at the user's face. */
  544. error_at (DECL_SOURCE_LOCATION (fn),
  545. "task return type must be %<void%>");
  546. /* This is a function declaration for something local to this
  547. translation unit, so add the `task' attribute to FN. */
  548. *no_add_attrs = false;
  549. /* Add an empty `task_implementation_list' attribute. */
  550. DECL_ATTRIBUTES (fn) =
  551. tree_cons (get_identifier (task_implementation_list_attribute_name),
  552. NULL_TREE,
  553. NULL_TREE);
  554. /* Push a declaration for the corresponding `struct starpu_codelet' object and
  555. add it as an attribute of FN. */
  556. tree cl = build_codelet_declaration (fn);
  557. DECL_ATTRIBUTES (fn) =
  558. tree_cons (get_identifier (task_codelet_attribute_name), cl,
  559. DECL_ATTRIBUTES (fn));
  560. pushdecl (cl);
  561. }
  562. /* Lookup & cache function declarations for later reuse. */
  563. LOOKUP_STARPU_FUNCTION (unpack_fn, "starpu_unpack_cl_args");
  564. return NULL_TREE;
  565. }
  566. /* Handle the `task_implementation (WHERE, TASK)' attribute. WHERE is a
  567. string constant ("cpu", "cuda", etc.), and TASK is the identifier of a
  568. function declared with the `task' attribute. */
  569. static tree
  570. handle_task_implementation_attribute (tree *node, tree name, tree args,
  571. int flags, bool *no_add_attrs)
  572. {
  573. location_t loc;
  574. tree fn, where, task_decl;
  575. /* FIXME:TODO: To change the order to (TASK, WHERE):
  576. tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
  577. tree cleanup_decl = lookup_name (cleanup_id);
  578. */
  579. fn = *node;
  580. where = TREE_VALUE (args);
  581. task_decl = TREE_VALUE (TREE_CHAIN (args));
  582. loc = DECL_SOURCE_LOCATION (fn);
  583. /* Get rid of the `task_implementation' attribute by default so that FN
  584. isn't further processed when it's erroneous. */
  585. *no_add_attrs = true;
  586. /* Mark FN as used to placate `-Wunused-function' when FN is erroneous
  587. anyway. */
  588. TREE_USED (fn) = true;
  589. if (TREE_CODE (fn) != FUNCTION_DECL)
  590. error_at (loc,
  591. "%<task_implementation%> attribute only applies to functions");
  592. else if (TREE_CODE (where) != STRING_CST)
  593. error_at (loc, "string constant expected "
  594. "as the first %<task_implementation%> argument");
  595. else if (TREE_CODE (task_decl) != FUNCTION_DECL)
  596. error_at (loc, "%qE is not a function", task_decl);
  597. else if (lookup_attribute (task_attribute_name,
  598. DECL_ATTRIBUTES (task_decl)) == NULL_TREE)
  599. error_at (loc, "function %qE lacks the %<task%> attribute",
  600. DECL_NAME (task_decl));
  601. else if (TYPE_CANONICAL (TREE_TYPE (fn))
  602. != TYPE_CANONICAL (TREE_TYPE (task_decl)))
  603. error_at (loc, "type differs from that of task %qE",
  604. DECL_NAME (task_decl));
  605. else
  606. {
  607. /* Add FN to the list of implementations of TASK_DECL. */
  608. tree attr, impls;
  609. attr = lookup_attribute (task_implementation_list_attribute_name,
  610. DECL_ATTRIBUTES (task_decl));
  611. impls = tree_cons (NULL_TREE, fn, TREE_VALUE (attr));
  612. TREE_VALUE (attr) = impls;
  613. TREE_USED (fn) = TREE_USED (task_decl);
  614. /* Check the `where' argument to raise a warning if needed. */
  615. if (task_implementation_target_to_int (where) == STARPU_NONE)
  616. warning_at (loc, 0,
  617. "unsupported target %E; task implementation won't be used",
  618. where);
  619. /* Keep the attribute. */
  620. *no_add_attrs = false;
  621. }
  622. return NULL_TREE;
  623. }
  624. /* Handle the `heap_allocated' attribute on variable *NODE. */
  625. static tree
  626. handle_heap_allocated_attribute (tree *node, tree name, tree args,
  627. int flags, bool *no_add_attrs)
  628. {
  629. location_t loc;
  630. tree var = *node;
  631. loc = DECL_SOURCE_LOCATION (var);
  632. if (DECL_EXTERNAL (var))
  633. error_at (loc, "attribute %<heap_allocated%> cannot be used "
  634. "on external declarations");
  635. else if (TREE_PUBLIC (var) || TREE_STATIC (var))
  636. {
  637. error_at (loc, "attribute %<heap_allocated%> cannot be used "
  638. "on global variables");
  639. TREE_TYPE (var) = error_mark_node;
  640. }
  641. else if (TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
  642. {
  643. error_at (loc, "variable %qE must have an array type",
  644. DECL_NAME (var));
  645. TREE_TYPE (var) = error_mark_node;
  646. }
  647. else if (TYPE_SIZE (TREE_TYPE (var)) == NULL_TREE)
  648. {
  649. error_at (loc, "variable %qE has an incomplete array type",
  650. DECL_NAME (var));
  651. TREE_TYPE (var) = error_mark_node;
  652. }
  653. else
  654. {
  655. tree array_type = TREE_TYPE (var);
  656. tree pointer_type = build_pointer_type (strip_array_types (array_type));
  657. /* We want VAR to feel like an array, but to really be a pointer. So
  658. the hack consists in keeping its array type, but giving it the
  659. storage of a pointer. (XXX) */
  660. DECL_SIZE (var) = TYPE_SIZE (pointer_type);
  661. DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (pointer_type);
  662. DECL_ALIGN (var) = TYPE_ALIGN (pointer_type);
  663. DECL_USER_ALIGN (var) = false;
  664. DECL_MODE (var) = TYPE_MODE (pointer_type);
  665. tree malloc_fn = lookup_name (get_identifier ("starpu_malloc"));
  666. gcc_assert (malloc_fn != NULL_TREE);
  667. tree alloc = build_call_expr (malloc_fn, 2,
  668. build_addr (var, current_function_decl),
  669. TYPE_SIZE_UNIT (array_type));
  670. TREE_SIDE_EFFECTS (alloc) = true;
  671. add_stmt (alloc);
  672. /* Add a destructor for VAR.
  673. TODO: Provide a way to disable this. */
  674. DECL_ATTRIBUTES (var) =
  675. tree_cons (get_identifier ("cleanup"),
  676. lookup_name (get_identifier ("starpu_free")),
  677. DECL_ATTRIBUTES (var));
  678. }
  679. return NULL_TREE;
  680. }
  681. /* Handle the `output' attribute on type *NODE, which should be the type of a
  682. PARM_DECL of a task or task implementation. */
  683. static tree
  684. handle_output_attribute (tree *node, tree name, tree args,
  685. int flags, bool *no_add_attrs)
  686. {
  687. tree type = *node;
  688. gcc_assert (TYPE_P (type));
  689. if (!POINTER_TYPE_P (type) && TREE_CODE (type) != ARRAY_TYPE)
  690. error ("%<output%> attribute not allowed for non-pointer types");
  691. else
  692. /* Keep the attribute. */
  693. *no_add_attrs = false;
  694. return NULL_TREE;
  695. }
  696. /* Return the declaration of the `struct starpu_codelet' variable associated with
  697. TASK_DECL. */
  698. static tree
  699. task_codelet_declaration (const_tree task_decl)
  700. {
  701. tree cl_attr;
  702. cl_attr = lookup_attribute (task_codelet_attribute_name,
  703. DECL_ATTRIBUTES (task_decl));
  704. gcc_assert (cl_attr != NULL_TREE);
  705. return TREE_VALUE (cl_attr);
  706. }
  707. /* Return true if DECL is a task. */
  708. static bool
  709. task_p (const_tree decl)
  710. {
  711. return (TREE_CODE (decl) == FUNCTION_DECL &&
  712. lookup_attribute (task_attribute_name,
  713. DECL_ATTRIBUTES (decl)) != NULL_TREE);
  714. }
  715. /* Return true if DECL is a task implementation. */
  716. static bool
  717. task_implementation_p (const_tree decl)
  718. {
  719. return (TREE_CODE (decl) == FUNCTION_DECL &&
  720. lookup_attribute (task_implementation_attribute_name,
  721. DECL_ATTRIBUTES (decl)) != NULL_TREE);
  722. }
  723. /* Return the list of implementations of TASK_DECL. */
  724. static tree
  725. task_implementation_list (const_tree task_decl)
  726. {
  727. tree attr;
  728. attr = lookup_attribute (task_implementation_list_attribute_name,
  729. DECL_ATTRIBUTES (task_decl));
  730. return TREE_VALUE (attr);
  731. }
  732. /* Return the list of pointer parameter types of TASK_DECL. */
  733. static tree
  734. task_pointer_parameter_types (const_tree task_decl)
  735. {
  736. bool is_pointer (const_tree item)
  737. {
  738. return POINTER_TYPE_P (TREE_VALUE (item));
  739. }
  740. return filter (is_pointer, TYPE_ARG_TYPES (TREE_TYPE (task_decl)));
  741. }
  742. /* Return the StarPU integer constant corresponding to string TARGET. */
  743. static int
  744. task_implementation_target_to_int (const_tree target)
  745. {
  746. gcc_assert (TREE_CODE (target) == STRING_CST);
  747. int where_int;
  748. if (!strncmp (TREE_STRING_POINTER (target), "cpu",
  749. TREE_STRING_LENGTH (target)))
  750. where_int = STARPU_CPU;
  751. else if (!strncmp (TREE_STRING_POINTER (target), "opencl",
  752. TREE_STRING_LENGTH (target)))
  753. where_int = STARPU_OPENCL;
  754. else if (!strncmp (TREE_STRING_POINTER (target), "cuda",
  755. TREE_STRING_LENGTH (target)))
  756. where_int = STARPU_CUDA;
  757. else
  758. where_int = STARPU_NONE;
  759. return where_int;
  760. }
  761. /* Return a value indicating where TASK_IMPL should execute (`STARPU_CPU',
  762. `STARPU_CUDA', etc.). */
  763. static int
  764. task_implementation_where (const_tree task_impl)
  765. {
  766. tree impl_attr, args, where;
  767. gcc_assert (TREE_CODE (task_impl) == FUNCTION_DECL);
  768. impl_attr = lookup_attribute (task_implementation_attribute_name,
  769. DECL_ATTRIBUTES (task_impl));
  770. gcc_assert (impl_attr != NULL_TREE);
  771. args = TREE_VALUE (impl_attr);
  772. where = TREE_VALUE (args);
  773. return task_implementation_target_to_int (where);
  774. }
  775. /* Return the task implemented by TASK_IMPL. */
  776. static tree
  777. task_implementation_task (const_tree task_impl)
  778. {
  779. tree impl_attr, args;
  780. gcc_assert (TREE_CODE (task_impl) == FUNCTION_DECL);
  781. impl_attr = lookup_attribute (task_implementation_attribute_name,
  782. DECL_ATTRIBUTES (task_impl));
  783. gcc_assert (impl_attr != NULL_TREE);
  784. args = TREE_VALUE (impl_attr);
  785. return TREE_VALUE (TREE_CHAIN (args));
  786. }
  787. /* Return the FUNCTION_DECL of the wrapper generated for TASK_IMPL. */
  788. static tree
  789. task_implementation_wrapper (const_tree task_impl)
  790. {
  791. tree attr;
  792. gcc_assert (TREE_CODE (task_impl) == FUNCTION_DECL);
  793. attr = lookup_attribute (task_implementation_wrapper_attribute_name,
  794. DECL_ATTRIBUTES (task_impl));
  795. gcc_assert (attr != NULL_TREE);
  796. return TREE_VALUE (attr);
  797. }
  798. /* Return true if DECL is a parameter whose type is `output'-qualified. */
  799. static bool
  800. output_parameter_p (const_tree decl)
  801. {
  802. return (TREE_CODE (decl) == PARM_DECL &&
  803. lookup_attribute (output_attribute_name,
  804. TYPE_ATTRIBUTES (TREE_TYPE (decl))) != NULL_TREE);
  805. }
  806. static void
  807. register_task_attributes (void *gcc_data, void *user_data)
  808. {
  809. static const struct attribute_spec task_attr =
  810. {
  811. task_attribute_name, 0, 0, true, false, false,
  812. handle_task_attribute
  813. };
  814. static const struct attribute_spec task_implementation_attr =
  815. {
  816. task_implementation_attribute_name, 2, 2, true, false, false,
  817. handle_task_implementation_attribute
  818. };
  819. static const struct attribute_spec heap_allocated_attr =
  820. {
  821. heap_allocated_attribute_name, 0, 0, true, false, false,
  822. handle_heap_allocated_attribute
  823. };
  824. static const struct attribute_spec output_attr =
  825. {
  826. output_attribute_name, 0, 0, true, true, false,
  827. handle_output_attribute,
  828. #if 0 /* FIXME: Check whether the `affects_type_identity' field is
  829. present. */
  830. true /* affects type identity */
  831. #endif
  832. };
  833. register_attribute (&task_attr);
  834. register_attribute (&task_implementation_attr);
  835. register_attribute (&heap_allocated_attr);
  836. register_attribute (&output_attr);
  837. }
  838. /* Return the type of a codelet function, i.e.,
  839. `void (*) (void **, void *)'. */
  840. static tree
  841. build_codelet_wrapper_type (void)
  842. {
  843. tree void_ptr_ptr;
  844. void_ptr_ptr = build_pointer_type (ptr_type_node);
  845. return build_function_type_list (void_type_node,
  846. void_ptr_ptr, ptr_type_node,
  847. NULL_TREE);
  848. }
  849. /* Return an identifier for the wrapper of TASK_IMPL, a task
  850. implementation. */
  851. static tree
  852. build_codelet_wrapper_identifier (tree task_impl)
  853. {
  854. static const char suffix[] = ".task_implementation_wrapper";
  855. tree id;
  856. char *cl_name;
  857. const char *task_name;
  858. id = DECL_NAME (task_impl);
  859. task_name = IDENTIFIER_POINTER (id);
  860. cl_name = alloca (IDENTIFIER_LENGTH (id) + strlen (suffix) + 1);
  861. memcpy (cl_name, task_name, IDENTIFIER_LENGTH (id));
  862. strcpy (&cl_name[IDENTIFIER_LENGTH (id)], suffix);
  863. return get_identifier (cl_name);
  864. }
  865. /* Return a function of type `void (*) (void **, void *)' that calls function
  866. TASK_IMPL, the FUNCTION_DECL of a task implementation whose prototype may
  867. be arbitrary. */
  868. static tree
  869. build_codelet_wrapper_definition (tree task_impl)
  870. {
  871. location_t loc;
  872. tree task_decl, decl;
  873. loc = DECL_SOURCE_LOCATION (task_impl);
  874. task_decl = task_implementation_task (task_impl);
  875. tree build_local_var (const_tree type)
  876. {
  877. tree var, t;
  878. const char *seed;
  879. t = TREE_VALUE (type);
  880. seed = POINTER_TYPE_P (t) ? "pointer_arg" : "scalar_arg";
  881. var = build_decl (loc, VAR_DECL, create_tmp_var_name (seed), t);
  882. DECL_CONTEXT (var) = decl;
  883. DECL_ARTIFICIAL (var) = true;
  884. return var;
  885. }
  886. /* Return the body of the wrapper, which unpacks `cl_args' and calls the
  887. user-defined task implementation. */
  888. tree build_body (tree wrapper_decl, tree vars)
  889. {
  890. tree stmts = NULL, call, v;
  891. VEC(tree, gc) *args;
  892. /* Build `var0 = STARPU_VECTOR_GET_PTR (buffers[0]); ...'. */
  893. size_t index = 0;
  894. for (v = vars; v != NULL_TREE; v = TREE_CHAIN (v))
  895. {
  896. if (POINTER_TYPE_P (TREE_TYPE (v)))
  897. {
  898. /* Compute `void *VDESC = buffers[0];'. */
  899. tree vdesc = array_ref (DECL_ARGUMENTS (wrapper_decl), index);
  900. /* Below we assume (1) that pointer arguments are registered as
  901. StarPU vector handles, and (2) that the `ptr' field is at
  902. offset 0 of `struct starpu_vector_interface'. The latter allows us
  903. to use a simple pointer dereference instead of expanding
  904. `STARPU_VECTOR_GET_PTR'. */
  905. assert (offsetof (struct starpu_vector_interface, ptr) == 0);
  906. /* Compute `type *PTR = *(type **) VDESC;'. */
  907. tree ptr = build1 (INDIRECT_REF,
  908. build_pointer_type (TREE_TYPE (v)),
  909. vdesc);
  910. append_to_statement_list (build2 (MODIFY_EXPR, TREE_TYPE (v),
  911. v, ptr),
  912. &stmts);
  913. index++;
  914. }
  915. }
  916. /* Build `starpu_unpack_cl_args (cl_args, &var1, &var2, ...)'. */
  917. args = NULL;
  918. VEC_safe_push (tree, gc, args, TREE_CHAIN (DECL_ARGUMENTS (wrapper_decl)));
  919. for (v = vars; v != NULL_TREE; v = TREE_CHAIN (v))
  920. {
  921. if (!POINTER_TYPE_P (TREE_TYPE (v)))
  922. VEC_safe_push (tree, gc, args, build_addr (v, wrapper_decl));
  923. }
  924. if (VEC_length (tree, args) > 1)
  925. {
  926. call = build_call_expr_loc_vec (UNKNOWN_LOCATION, unpack_fn, args);
  927. TREE_SIDE_EFFECTS (call) = 1;
  928. append_to_statement_list (call, &stmts);
  929. }
  930. /* Build `my_task_impl (var1, var2, ...)'. */
  931. args = NULL;
  932. for (v = vars; v != NULL_TREE; v = TREE_CHAIN (v))
  933. VEC_safe_push (tree, gc, args, v);
  934. call = build_call_expr_loc_vec (UNKNOWN_LOCATION, task_impl, args);
  935. TREE_SIDE_EFFECTS (call) = 1;
  936. append_to_statement_list (call, &stmts);
  937. tree bind;
  938. bind = build3 (BIND_EXPR, void_type_node, vars, stmts,
  939. DECL_INITIAL (wrapper_decl));
  940. TREE_TYPE (bind) = TREE_TYPE (TREE_TYPE (wrapper_decl));
  941. return bind;
  942. }
  943. /* Return the parameter list of the wrapper:
  944. `(void **BUFFERS, void *CL_ARGS)'. */
  945. tree build_parameters (tree wrapper_decl)
  946. {
  947. tree param1, param2;
  948. param1 = build_decl (loc, PARM_DECL,
  949. create_tmp_var_name ("buffers"),
  950. build_pointer_type (ptr_type_node));
  951. DECL_ARG_TYPE (param1) = ptr_type_node;
  952. DECL_CONTEXT (param1) = wrapper_decl;
  953. TREE_USED (param1) = true;
  954. param2 = build_decl (loc, PARM_DECL,
  955. create_tmp_var_name ("cl_args"),
  956. ptr_type_node);
  957. DECL_ARG_TYPE (param2) = ptr_type_node;
  958. DECL_CONTEXT (param2) = wrapper_decl;
  959. TREE_USED (param2) = true;
  960. return chainon (param1, param2);
  961. }
  962. tree wrapper_name, vars, result;
  963. wrapper_name = build_codelet_wrapper_identifier (task_impl);
  964. decl = build_decl (loc, FUNCTION_DECL, wrapper_name,
  965. build_codelet_wrapper_type ());
  966. vars = map (build_local_var,
  967. list_remove (void_type_p,
  968. TYPE_ARG_TYPES (TREE_TYPE (task_decl))));
  969. DECL_CONTEXT (decl) = NULL_TREE;
  970. DECL_ARGUMENTS (decl) = build_parameters (decl);
  971. result = build_decl (loc, RESULT_DECL, NULL_TREE, void_type_node);
  972. DECL_CONTEXT (result) = decl;
  973. DECL_ARTIFICIAL (result) = true;
  974. DECL_IGNORED_P (result) = true;
  975. DECL_RESULT (decl) = result;
  976. DECL_INITIAL (decl) = build_block (vars, NULL_TREE, decl, NULL_TREE);
  977. DECL_SAVED_TREE (decl) = build_body (decl, vars);
  978. TREE_PUBLIC (decl) = TREE_PUBLIC (task_impl);
  979. TREE_STATIC (decl) = true;
  980. TREE_USED (decl) = true;
  981. DECL_ARTIFICIAL (decl) = true;
  982. DECL_EXTERNAL (decl) = false;
  983. DECL_UNINLINABLE (decl) = true;
  984. rest_of_decl_compilation (decl, true, 0);
  985. struct function *prev_cfun = cfun;
  986. set_cfun (NULL);
  987. allocate_struct_function (decl, false);
  988. cfun->function_end_locus = DECL_SOURCE_LOCATION (task_impl);
  989. cgraph_finalize_function (decl, false);
  990. /* Mark DECL as needed so that it doesn't get removed by
  991. `cgraph_remove_unreachable_nodes' when it's not public. */
  992. cgraph_mark_needed_node (cgraph_get_node (decl));
  993. set_cfun (prev_cfun);
  994. return decl;
  995. }
  996. /* Define one wrapper function for each implementation of TASK. TASK should
  997. be the FUNCTION_DECL of a task. */
  998. static void
  999. define_codelet_wrappers (tree task)
  1000. {
  1001. void define (tree task_impl)
  1002. {
  1003. tree wrapper_def;
  1004. wrapper_def = build_codelet_wrapper_definition (task_impl);
  1005. DECL_ATTRIBUTES (task_impl) =
  1006. tree_cons (get_identifier (task_implementation_wrapper_attribute_name),
  1007. wrapper_def,
  1008. DECL_ATTRIBUTES (task_impl));
  1009. }
  1010. for_each (define, task_implementation_list (task));
  1011. }
  1012. /* Return a NODE_IDENTIFIER for the variable holding the `struct starpu_codelet'
  1013. structure associated with TASK_DECL. */
  1014. static tree
  1015. build_codelet_identifier (tree task_decl)
  1016. {
  1017. static const char suffix[] = ".codelet";
  1018. tree id;
  1019. char *cl_name;
  1020. const char *task_name;
  1021. id = DECL_NAME (task_decl);
  1022. task_name = IDENTIFIER_POINTER (id);
  1023. cl_name = alloca (IDENTIFIER_LENGTH (id) + strlen (suffix) + 1);
  1024. memcpy (cl_name, task_name, IDENTIFIER_LENGTH (id));
  1025. strcpy (&cl_name[IDENTIFIER_LENGTH (id)], suffix);
  1026. return get_identifier (cl_name);
  1027. }
  1028. static tree
  1029. codelet_type (void)
  1030. {
  1031. /* XXX: Hack to allow the type declaration to be accessible at lower
  1032. time. */
  1033. static tree type_decl = NULL_TREE;
  1034. if (type_decl == NULL_TREE)
  1035. {
  1036. /* Lookup the `struct starpu_codelet' struct type. This should succeed since
  1037. we push <starpu.h> early on. */
  1038. type_decl = lookup_name (get_identifier (codelet_struct_name));
  1039. gcc_assert (type_decl != NULL_TREE && TREE_CODE (type_decl) == TYPE_DECL);
  1040. }
  1041. return TREE_TYPE (type_decl);
  1042. }
  1043. /* Return a VAR_DECL that declares a `struct starpu_codelet' structure for
  1044. TASK_DECL. */
  1045. static tree
  1046. build_codelet_declaration (tree task_decl)
  1047. {
  1048. tree name, cl_decl;
  1049. name = build_codelet_identifier (task_decl);
  1050. cl_decl = build_decl (DECL_SOURCE_LOCATION (task_decl),
  1051. VAR_DECL, name,
  1052. /* c_build_qualified_type (type, TYPE_QUAL_CONST) */
  1053. codelet_type ());
  1054. DECL_ARTIFICIAL (cl_decl) = true;
  1055. TREE_PUBLIC (cl_decl) = TREE_PUBLIC (task_decl);
  1056. TREE_STATIC (cl_decl) = false;
  1057. TREE_USED (cl_decl) = true;
  1058. DECL_EXTERNAL (cl_decl) = true;
  1059. DECL_CONTEXT (cl_decl) = NULL_TREE;
  1060. return cl_decl;
  1061. }
  1062. /* Return a `struct starpu_codelet' initializer for TASK_DECL. */
  1063. static tree
  1064. build_codelet_initializer (tree task_decl)
  1065. {
  1066. tree fields;
  1067. fields = TYPE_FIELDS (codelet_type ());
  1068. gcc_assert (TREE_CODE (fields) == FIELD_DECL);
  1069. tree lookup_field (const char *name)
  1070. {
  1071. tree fdecl, fname;
  1072. fname = get_identifier (name);
  1073. for (fdecl = fields;
  1074. fdecl != NULL_TREE;
  1075. fdecl = TREE_CHAIN (fdecl))
  1076. {
  1077. if (DECL_NAME (fdecl) == fname)
  1078. return fdecl;
  1079. }
  1080. /* Field NAME wasn't found. */
  1081. gcc_assert (false);
  1082. }
  1083. tree field_initializer (const char *name, tree value)
  1084. {
  1085. tree field, init;
  1086. field = lookup_field (name);
  1087. init = make_node (TREE_LIST);
  1088. TREE_PURPOSE (init) = field;
  1089. TREE_CHAIN (init) = NULL_TREE;
  1090. if (TREE_CODE (TREE_TYPE (value)) != ARRAY_TYPE)
  1091. TREE_VALUE (init) = fold_convert (TREE_TYPE (field), value);
  1092. else
  1093. TREE_VALUE (init) = value;
  1094. return init;
  1095. }
  1096. tree where_init (tree impls)
  1097. {
  1098. tree impl;
  1099. int where_int = STARPU_NONE;
  1100. for (impl = impls;
  1101. impl != NULL_TREE;
  1102. impl = TREE_CHAIN (impl))
  1103. {
  1104. tree impl_decl;
  1105. impl_decl = TREE_VALUE (impl);
  1106. gcc_assert (TREE_CODE (impl_decl) == FUNCTION_DECL);
  1107. printf (" `%s'\n", IDENTIFIER_POINTER (DECL_NAME (impl_decl)));
  1108. where_int |= task_implementation_where (impl_decl);
  1109. }
  1110. return build_int_cstu (integer_type_node, where_int);
  1111. }
  1112. tree implementation_pointers (tree impls, int where)
  1113. {
  1114. size_t len;
  1115. tree impl, pointers;
  1116. for (impl = impls, pointers = NULL_TREE, len = 0;
  1117. impl != NULL_TREE;
  1118. impl = TREE_CHAIN (impl))
  1119. {
  1120. tree impl_decl;
  1121. impl_decl = TREE_VALUE (impl);
  1122. if (task_implementation_where (impl_decl) == where)
  1123. {
  1124. /* Return a pointer to the wrapper of IMPL_DECL. */
  1125. tree addr = build_addr (task_implementation_wrapper (impl_decl),
  1126. NULL_TREE);
  1127. pointers = tree_cons (size_int (len), addr, pointers);
  1128. len++;
  1129. if (len > STARPU_MAXIMPLEMENTATIONS)
  1130. error_at (DECL_SOURCE_LOCATION (impl_decl),
  1131. "maximum number of per-target task implementations "
  1132. "exceeded");
  1133. }
  1134. }
  1135. /* POINTERS must be null-terminated. */
  1136. pointers = tree_cons (size_int (len), build_zero_cst (ptr_type_node),
  1137. pointers);
  1138. len++;
  1139. /* Return an array initializer. */
  1140. tree index_type = build_index_type (size_int (list_length (pointers)));
  1141. return build_constructor_from_list (build_array_type (ptr_type_node,
  1142. index_type),
  1143. nreverse (pointers));
  1144. }
  1145. tree pointer_arg_count (void)
  1146. {
  1147. size_t len;
  1148. len = list_length (task_pointer_parameter_types (task_decl));
  1149. return build_int_cstu (integer_type_node, len);
  1150. }
  1151. printf ("implementations for `%s':\n",
  1152. IDENTIFIER_POINTER (DECL_NAME (task_decl)));
  1153. tree impls, inits;
  1154. impls = task_implementation_list (task_decl);
  1155. inits =
  1156. chain_trees (field_initializer ("where", where_init (impls)),
  1157. field_initializer ("nbuffers", pointer_arg_count ()),
  1158. field_initializer ("cpu_funcs",
  1159. implementation_pointers (impls,
  1160. STARPU_CPU)),
  1161. field_initializer ("opencl_funcs",
  1162. implementation_pointers (impls,
  1163. STARPU_OPENCL)),
  1164. field_initializer ("cuda_funcs",
  1165. implementation_pointers (impls,
  1166. STARPU_CUDA)),
  1167. NULL_TREE);
  1168. return build_constructor_from_unsorted_list (codelet_type (), inits);
  1169. }
  1170. /* Return the VAR_DECL that defines a `struct starpu_codelet' structure for
  1171. TASK_DECL. The VAR_DECL is assumed to already exists, so it must not be
  1172. pushed again. */
  1173. static tree
  1174. declare_codelet (tree task_decl)
  1175. {
  1176. /* Retrieve the declaration of the `struct starpu_codelet' object. */
  1177. tree cl_decl;
  1178. cl_decl = lookup_name (build_codelet_identifier (task_decl));
  1179. gcc_assert (cl_decl != NULL_TREE && TREE_CODE (cl_decl) == VAR_DECL);
  1180. /* Turn the codelet declaration into a definition. */
  1181. TREE_TYPE (cl_decl) = codelet_type ();
  1182. TREE_PUBLIC (cl_decl) = TREE_PUBLIC (task_decl);
  1183. return cl_decl;
  1184. }
  1185. static void
  1186. handle_pre_genericize (void *gcc_data, void *user_data)
  1187. {
  1188. tree fn = (tree) gcc_data;
  1189. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
  1190. if (task_p (fn) && TREE_STATIC (fn))
  1191. /* The user defined a body for task FN, which is forbidden. */
  1192. error_at (DECL_SOURCE_LOCATION (fn),
  1193. "task %qE must not have a body", DECL_NAME (fn));
  1194. else if (task_implementation_p (fn))
  1195. {
  1196. tree task = task_implementation_task (fn);
  1197. if (!TREE_STATIC (task))
  1198. {
  1199. /* TASK lacks a body. Declare its codelet, intantiate its codelet
  1200. wrappers, and its body in this compilation unit. */
  1201. tree build_parameter (const_tree lst)
  1202. {
  1203. tree param, type;
  1204. type = TREE_VALUE (lst);
  1205. param = build_decl (DECL_SOURCE_LOCATION (task), PARM_DECL,
  1206. create_tmp_var_name ("parameter"),
  1207. type);
  1208. DECL_ARG_TYPE (param) = type;
  1209. DECL_CONTEXT (param) = task;
  1210. return param;
  1211. }
  1212. /* Declare TASK's codelet. It cannot be defined yet because the
  1213. complete list of tasks isn't available at this point. */
  1214. declare_codelet (task);
  1215. /* Set the task's parameter list. */
  1216. DECL_ARGUMENTS (task) =
  1217. map (build_parameter,
  1218. list_remove (void_type_p,
  1219. TYPE_ARG_TYPES (TREE_TYPE (task))));
  1220. /* Build its body. */
  1221. DECL_SAVED_TREE (task) = build_task_body (task);
  1222. TREE_STATIC (task) = true;
  1223. DECL_EXTERNAL (task) = false;
  1224. DECL_INITIAL (task) = build_block (NULL_TREE, NULL_TREE, task, NULL_TREE);
  1225. DECL_RESULT (task) =
  1226. build_decl (DECL_SOURCE_LOCATION (task), RESULT_DECL,
  1227. NULL_TREE, void_type_node);
  1228. DECL_CONTEXT (DECL_RESULT (task)) = task;
  1229. /* Compile TASK's body. */
  1230. rest_of_decl_compilation (task, true, 0);
  1231. allocate_struct_function (task, false);
  1232. cgraph_finalize_function (task, false);
  1233. }
  1234. }
  1235. }
  1236. /* Build a "conversion" from a raw C pointer to its data handle. The
  1237. assumption is that the programmer should have already registered the
  1238. pointer by themselves. */
  1239. static tree
  1240. build_pointer_lookup (tree pointer)
  1241. {
  1242. #if 0
  1243. gimple emit_error_message (void)
  1244. {
  1245. static const char msg[] =
  1246. "starpu: task called with unregistered pointer, aborting\n";
  1247. return gimple_build_call (built_in_decls[BUILT_IN_PUTS], 1,
  1248. build_string_literal (strlen (msg) + 1, msg));
  1249. }
  1250. #endif
  1251. static tree data_lookup_fn;
  1252. LOOKUP_STARPU_FUNCTION (data_lookup_fn, "starpu_data_lookup");
  1253. return build_call_expr (data_lookup_fn, 1, pointer);
  1254. /* FIXME: Add `if (VAR == NULL) abort ();'. */
  1255. }
  1256. /* Build the body of TASK_DECL, which will call `starpu_insert_task'. */
  1257. static tree
  1258. build_task_body (const_tree task_decl)
  1259. {
  1260. VEC(tree, gc) *args = NULL;
  1261. tree p, params = DECL_ARGUMENTS (task_decl);
  1262. /* The first argument will be a pointer to the codelet. */
  1263. VEC_safe_push (tree, gc, args,
  1264. build_addr (task_codelet_declaration (task_decl),
  1265. current_function_decl));
  1266. for (p = params; p != NULL_TREE; p = TREE_CHAIN (p))
  1267. {
  1268. gcc_assert (TREE_CODE (p) == PARM_DECL);
  1269. tree type = TREE_TYPE (p);
  1270. if (POINTER_TYPE_P (type))
  1271. {
  1272. /* A pointer: the arguments will be:
  1273. `STARPU_RW, ptr' or similar. */
  1274. /* If TYPE points to a const-qualified type, then mark the data as
  1275. read-only; if is has the `output' attribute, then mark it as
  1276. write-only; otherwise default to read-write. */
  1277. int mode =
  1278. (TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
  1279. ? STARPU_R
  1280. : (output_parameter_p (p) ? STARPU_W : STARPU_RW);
  1281. VEC_safe_push (tree, gc, args,
  1282. build_int_cst (integer_type_node, mode));
  1283. VEC_safe_push (tree, gc, args, build_pointer_lookup (p));
  1284. }
  1285. else
  1286. {
  1287. /* A scalar: the arguments will be:
  1288. `STARPU_VALUE, &scalar, sizeof (scalar)'. */
  1289. mark_addressable (p);
  1290. VEC_safe_push (tree, gc, args,
  1291. build_int_cst (integer_type_node, STARPU_VALUE));
  1292. VEC_safe_push (tree, gc, args,
  1293. build_addr (p, current_function_decl));
  1294. VEC_safe_push (tree, gc, args,
  1295. size_in_bytes (type));
  1296. }
  1297. }
  1298. /* Push the terminating zero. */
  1299. VEC_safe_push (tree, gc, args,
  1300. build_int_cst (integer_type_node, 0));
  1301. static tree insert_task_fn;
  1302. LOOKUP_STARPU_FUNCTION (insert_task_fn, "starpu_insert_task");
  1303. return build_call_expr_loc_vec (DECL_SOURCE_LOCATION (task_decl),
  1304. insert_task_fn, args);
  1305. }
  1306. /* Raise an error when IMPL doesn't satisfy the constraints of a task
  1307. implementations, such as not invoking another task. */
  1308. static void
  1309. validate_task_implementation (tree impl)
  1310. {
  1311. gcc_assert (task_implementation_p (impl));
  1312. const struct cgraph_node *cgraph;
  1313. const struct cgraph_edge *callee;
  1314. cgraph = cgraph_get_node (impl);
  1315. for (callee = cgraph->callees;
  1316. callee != NULL;
  1317. callee = callee->next_callee)
  1318. {
  1319. if (task_p (callee->callee->decl))
  1320. {
  1321. location_t loc;
  1322. loc = gimple_location (callee->call_stmt);
  1323. error_at (loc, "task %qE cannot be invoked from task implementation %qE",
  1324. DECL_NAME (callee->callee->decl),
  1325. DECL_NAME (impl));
  1326. }
  1327. }
  1328. }
  1329. static unsigned int
  1330. lower_starpu (void)
  1331. {
  1332. tree fndecl;
  1333. const struct cgraph_node *cgraph;
  1334. const struct cgraph_edge *callee;
  1335. fndecl = current_function_decl;
  1336. gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
  1337. if (task_p (fndecl))
  1338. {
  1339. /* Make sure the task implementations are valid. */
  1340. for_each (validate_task_implementation,
  1341. task_implementation_list (fndecl));
  1342. /* Generate a `struct starpu_codelet' structure and a wrapper function for
  1343. each implementation of TASK_DECL. This cannot be done earlier
  1344. because we need to have a complete list of task implementations. */
  1345. define_codelet_wrappers (fndecl);
  1346. tree cl_def = task_codelet_declaration (fndecl);
  1347. DECL_INITIAL (cl_def) = build_codelet_initializer (fndecl);
  1348. TREE_STATIC (cl_def) = true;
  1349. DECL_EXTERNAL (cl_def) = false;
  1350. varpool_finalize_decl (cl_def);
  1351. }
  1352. /* This pass should occur after `build_cgraph_edges'. */
  1353. cgraph = cgraph_get_node (fndecl);
  1354. gcc_assert (cgraph != NULL);
  1355. if (MAIN_NAME_P (DECL_NAME (fndecl)))
  1356. {
  1357. /* Check whether FNDECL initializes StarPU and emit a warning if it
  1358. doesn't. */
  1359. bool initialized;
  1360. for (initialized = false, callee = cgraph->callees;
  1361. !initialized && callee != NULL;
  1362. callee = callee->next_callee)
  1363. {
  1364. initialized =
  1365. DECL_NAME (callee->callee->decl) == get_identifier ("starpu_init");
  1366. }
  1367. if (!initialized)
  1368. warning_at (DECL_SOURCE_LOCATION (fndecl), 0,
  1369. "%qE does not initialize StarPU", DECL_NAME (fndecl));
  1370. }
  1371. for (callee = cgraph->callees;
  1372. callee != NULL;
  1373. callee = callee->next_callee)
  1374. {
  1375. gcc_assert (callee->callee != NULL);
  1376. tree callee_decl;
  1377. callee_decl = callee->callee->decl;
  1378. if (lookup_attribute (task_attribute_name,
  1379. DECL_ATTRIBUTES (callee_decl)))
  1380. {
  1381. printf ("%s: `%s' calls task `%s'\n", __func__,
  1382. IDENTIFIER_POINTER (DECL_NAME (fndecl)),
  1383. IDENTIFIER_POINTER (DECL_NAME (callee_decl)));
  1384. /* TODO: Insert analysis to check whether the pointer arguments
  1385. need to be registered. */
  1386. }
  1387. }
  1388. return 0;
  1389. }
  1390. static struct opt_pass pass_lower_starpu =
  1391. {
  1392. .type = GIMPLE_PASS,
  1393. .name = "pass_lower_starpu",
  1394. .execute = lower_starpu,
  1395. /* The rest is zeroed. */
  1396. };
  1397. /* Initialization. */
  1398. static void
  1399. define_cpp_macros (void *gcc_data, void *user_data)
  1400. {
  1401. cpp_define (parse_in, "STARPU_GCC_PLUGIN=0");
  1402. cpp_push_include (parse_in, "starpu.h");
  1403. }
  1404. int
  1405. plugin_init (struct plugin_name_args *plugin_info,
  1406. struct plugin_gcc_version *version)
  1407. {
  1408. if (!plugin_default_version_check (version, &gcc_version))
  1409. return 1;
  1410. register_callback (plugin_name, PLUGIN_START_UNIT,
  1411. define_cpp_macros, NULL);
  1412. register_callback (plugin_name, PLUGIN_PRAGMAS,
  1413. register_pragmas, NULL);
  1414. register_callback (plugin_name, PLUGIN_ATTRIBUTES,
  1415. register_task_attributes, NULL);
  1416. register_callback (plugin_name, PLUGIN_PRE_GENERICIZE,
  1417. handle_pre_genericize, NULL);
  1418. /* Register our pass so that it happens after `build_cgraph_edges' has been
  1419. done. */
  1420. struct register_pass_info pass_info =
  1421. {
  1422. .pass = &pass_lower_starpu,
  1423. .reference_pass_name = "*build_cgraph_edges",
  1424. .ref_pass_instance_number = 1,
  1425. .pos_op = PASS_POS_INSERT_AFTER
  1426. };
  1427. register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP,
  1428. NULL, &pass_info);
  1429. return 0;
  1430. }