starpu.c 56 KB

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