starpu.c 42 KB

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