starpu.c 60 KB

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