starpu.c 51 KB

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