starpu.c 56 KB

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