starpu.c 56 KB

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