starpu.c 59 KB

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