starpu.c 55 KB

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