starpu.c 43 KB

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