starpu.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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. #include <c-common.h>
  25. #include <c-pragma.h>
  26. #include <tm.h>
  27. #include <gimple.h>
  28. #include <tree-pass.h>
  29. #include <tree-flow.h>
  30. #include <cgraph.h>
  31. #include <gimple.h>
  32. #include <toplev.h>
  33. #include <stdio.h>
  34. #include <starpu.h> /* for `STARPU_CPU' & co. */
  35. /* The name of this plug-in. */
  36. static const char plugin_name[] = "starpu";
  37. /* Names of public attributes. */
  38. static const char task_attribute_name[] = "task";
  39. static const char task_implementation_attribute_name[] = "task_implementation";
  40. /* Names of attributes used internally. */
  41. static const char task_codelet_attribute_name[] = ".codelet";
  42. static const char task_implementation_list_attribute_name[] =
  43. ".task_implementation_list";
  44. static const char task_implementation_wrapper_attribute_name[] =
  45. ".task_implementation_wrapper";
  46. /* Names of data structures defined in <starpu.h>. */
  47. static const char codelet_struct_name[] = "starpu_codelet";
  48. static const char task_struct_name[] = "starpu_task";
  49. /* The `starpu_insert_task' and `starpu_data_lookup' FUNCTION_DECLs. */
  50. static tree insert_task_fn, data_lookup_fn;
  51. /* Forward declarations. */
  52. static tree build_codelet_declaration (tree task_decl);
  53. /* Useful code backported from GCC 4.6. */
  54. #if !HAVE_DECL_BUILD_CALL_EXPR_LOC_ARRAY
  55. static tree
  56. build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
  57. {
  58. tree fntype = TREE_TYPE (fndecl);
  59. tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
  60. return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
  61. }
  62. #endif
  63. #if !HAVE_DECL_BUILD_CALL_EXPR_LOC_VEC
  64. static tree
  65. build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
  66. {
  67. return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
  68. VEC_address (tree, vec));
  69. }
  70. #endif
  71. /* Helpers. */
  72. /* Build a reference to the INDEXth element of ARRAY. `build_array_ref' is
  73. not exported, so we roll our own. */
  74. static tree
  75. array_ref (tree array, size_t index)
  76. {
  77. gcc_assert (POINTER_TYPE_P (TREE_TYPE (array)));
  78. tree pointer_plus_offset =
  79. index > 0
  80. ? build_binary_op (UNKNOWN_LOCATION, PLUS_EXPR,
  81. array,
  82. build_int_cstu (integer_type_node, index),
  83. 0)
  84. : array;
  85. gcc_assert (POINTER_TYPE_P (TREE_TYPE (pointer_plus_offset)));
  86. return build_indirect_ref (UNKNOWN_LOCATION,
  87. pointer_plus_offset,
  88. RO_ARRAY_INDEXING);
  89. }
  90. /* Like `build_constructor_from_list', but sort VALS according to their
  91. offset in struct TYPE. Inspired by `gnat_build_constructor'. */
  92. static tree
  93. build_constructor_from_unsorted_list (tree type, tree vals)
  94. {
  95. int compare_elmt_bitpos (const void *rt1, const void *rt2)
  96. {
  97. const constructor_elt *elmt1 = (constructor_elt *) rt1;
  98. const constructor_elt *elmt2 = (constructor_elt *) rt2;
  99. const_tree field1 = elmt1->index;
  100. const_tree field2 = elmt2->index;
  101. int ret
  102. = tree_int_cst_compare (bit_position (field1), bit_position (field2));
  103. return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
  104. }
  105. tree t;
  106. VEC(constructor_elt,gc) *v = NULL;
  107. if (vals)
  108. {
  109. v = VEC_alloc (constructor_elt, gc, list_length (vals));
  110. for (t = vals; t; t = TREE_CHAIN (t))
  111. CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
  112. }
  113. /* Sort field initializers by field offset. */
  114. VEC_qsort (constructor_elt, v, compare_elmt_bitpos);
  115. return build_constructor (type, v);
  116. }
  117. /* Debugging helpers. */
  118. static tree build_printf (const char *, ...)
  119. __attribute__ ((format (printf, 1, 2)));
  120. static tree
  121. build_printf (const char *fmt, ...)
  122. {
  123. tree call;
  124. char *str;
  125. va_list args;
  126. va_start (args, fmt);
  127. vasprintf (&str, fmt, args);
  128. call = build_call_expr (built_in_decls[BUILT_IN_PUTS], 1,
  129. build_string_literal (strlen (str) + 1, str));
  130. free (str);
  131. va_end (args);
  132. return call;
  133. }
  134. static tree
  135. build_hello_world (void)
  136. {
  137. return build_printf ("Hello, StarPU!");
  138. }
  139. /* List and vector utilities, à la SRFI-1. */
  140. static tree chain_trees (tree t, ...)
  141. __attribute__ ((sentinel));
  142. static tree
  143. chain_trees (tree t, ...)
  144. {
  145. va_list args;
  146. va_start (args, t);
  147. tree next, prev = t;
  148. for (prev = t, next = va_arg (args, tree);
  149. next != NULL_TREE;
  150. prev = next, next = va_arg (args, tree))
  151. TREE_CHAIN (prev) = next;
  152. va_end (args);
  153. return t;
  154. }
  155. static tree
  156. filter (bool (*pred) (const_tree), tree t)
  157. {
  158. tree result, lst;
  159. gcc_assert (TREE_CODE (t) == TREE_LIST);
  160. result = NULL_TREE;
  161. for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
  162. {
  163. if (pred (lst))
  164. result = tree_cons (TREE_PURPOSE (lst), TREE_VALUE (lst),
  165. result);
  166. }
  167. return nreverse (result);
  168. }
  169. /* Map FUNC over chain T. T does not have to be `TREE_LIST'; it can be a
  170. chain of arbitrary tree objects. */
  171. static tree
  172. map (tree (*func) (const_tree), tree t)
  173. {
  174. tree result, tail, lst;
  175. result = tail = NULL_TREE;
  176. for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
  177. {
  178. tree r = func (lst);
  179. if (tail != NULL_TREE)
  180. TREE_CHAIN (tail) = r;
  181. else
  182. result = r;
  183. tail = r;
  184. }
  185. return result;
  186. }
  187. static void
  188. for_each (void (*func) (tree), tree t)
  189. {
  190. tree lst;
  191. gcc_assert (TREE_CODE (t) == TREE_LIST);
  192. for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
  193. func (TREE_VALUE (lst));
  194. }
  195. /* Pragmas. */
  196. #define STARPU_PRAGMA_NAME_SPACE "starpu"
  197. static void
  198. handle_pragma_hello (struct cpp_reader *reader)
  199. {
  200. add_stmt (build_hello_world ());
  201. }
  202. static void
  203. handle_pragma_wait (struct cpp_reader *reader)
  204. {
  205. tree fndecl;
  206. fndecl = lookup_name (get_identifier ("starpu_task_wait_for_all"));
  207. gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
  208. add_stmt (build_call_expr (fndecl, 0));
  209. }
  210. /* Process `#pragma starpu register VAR [COUNT]' and emit the corresponding
  211. `starpu_vector_data_register' call.
  212. FIXME: Currently processing happens before macro expansion, even though
  213. the docstring of `cpp_get_token' says otherwise. */
  214. static void
  215. handle_pragma_register (struct cpp_reader *reader)
  216. {
  217. const cpp_token *token;
  218. token = cpp_peek_token (reader, 0);
  219. if (token->type == CPP_PRAGMA_EOL)
  220. error_at (token->src_loc, "unterminated %<starpu register%> pragma");
  221. token = cpp_get_token (reader);
  222. if (token->type != CPP_NAME)
  223. error_at (token->src_loc, "identifier expected");
  224. /* Get the variable name. */
  225. tree var_name = get_identifier ((char *) cpp_token_as_text (reader, token));
  226. tree var = lookup_name (var_name);
  227. if (var == NULL_TREE || !DECL_P (var))
  228. error_at (token->src_loc, "unbound variable %qE", var_name);
  229. gcc_assert (POINTER_TYPE_P (TREE_TYPE (var))
  230. || TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE);
  231. if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE
  232. && !DECL_EXTERNAL (var)
  233. && !TREE_STATIC (var)
  234. && !MAIN_NAME_P (DECL_NAME (current_function_decl)))
  235. warning_at (token->src_loc, 0, "using an on-stack array as a task input "
  236. "considered unsafe");
  237. /* Determine the number of elements in the vector. */
  238. tree count = NULL_TREE;
  239. if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
  240. {
  241. tree domain = TYPE_DOMAIN (TREE_TYPE (var));
  242. if (domain != NULL_TREE)
  243. {
  244. count = build_binary_op (token->src_loc, MINUS_EXPR,
  245. TYPE_MAX_VALUE (domain),
  246. TYPE_MIN_VALUE (domain),
  247. false);
  248. count = build_binary_op (token->src_loc, PLUS_EXPR,
  249. count,
  250. build_int_cstu (integer_type_node, 1),
  251. false);
  252. count = fold_convert (size_type_node, count);
  253. }
  254. }
  255. token = cpp_peek_token (reader, 0);
  256. if (token->type == CPP_PRAGMA_EOL)
  257. {
  258. /* End of line reached: don't consume TOKEN and check whether the array
  259. size was determined. */
  260. if (count == NULL_TREE)
  261. error_at (token->src_loc, "cannot determine size of array %qE",
  262. var_name);
  263. }
  264. else if (token->type == CPP_NUMBER)
  265. {
  266. /* TOKEN is a number, consume it. */
  267. token = cpp_get_token (reader);
  268. unsigned int flags = cpp_classify_number (reader, token);
  269. if ((flags & CPP_N_CATEGORY) != CPP_N_INTEGER)
  270. error_at (token->src_loc, "invalid integer");
  271. cpp_num value = cpp_interpret_integer (reader, token, CPP_N_UNSIGNED);
  272. tree count_arg = build_int_cst_wide (size_type_node, value.low, value.high);
  273. if (count != NULL_TREE)
  274. {
  275. /* The number of elements of this array was already determined. */
  276. inform (token->src_loc,
  277. "element count can be omitted for bounded array %qE",
  278. var_name);
  279. if (!tree_int_cst_equal (count, count_arg))
  280. error_at (token->src_loc,
  281. "specified element count differs from actual size of array %qE",
  282. var_name);
  283. }
  284. else
  285. count = count_arg;
  286. if (cpp_peek_token (reader, 0)->type != CPP_PRAGMA_EOL)
  287. error_at (token->src_loc, "junk after %<starpu register%> pragma");
  288. }
  289. else
  290. error_at (token->src_loc, "integer expected");
  291. /* If VAR is an array, take its address. */
  292. tree pointer =
  293. POINTER_TYPE_P (TREE_TYPE (var))
  294. ? var
  295. : build_addr (var, current_function_decl);
  296. /* Introduce a local variable to hold the handle. */
  297. tree handle_var = create_tmp_var (ptr_type_node, ".handle");
  298. tree register_fn =
  299. lookup_name (get_identifier ("starpu_vector_data_register"));
  300. /* Build `starpu_vector_data_register (&HANDLE_VAR, 0, POINTER,
  301. COUNT, sizeof *POINTER)' */
  302. tree call =
  303. build_call_expr (register_fn, 5,
  304. build_addr (handle_var, current_function_decl),
  305. build_zero_cst (uintptr_type_node), /* home node */
  306. pointer, count,
  307. size_in_bytes (TREE_TYPE (TREE_TYPE (var))));
  308. add_stmt (call);
  309. }
  310. static void
  311. register_pragmas (void *gcc_data, void *user_data)
  312. {
  313. c_register_pragma (STARPU_PRAGMA_NAME_SPACE, "hello",
  314. handle_pragma_hello);
  315. c_register_pragma (STARPU_PRAGMA_NAME_SPACE, "wait",
  316. handle_pragma_wait);
  317. c_register_pragma (STARPU_PRAGMA_NAME_SPACE, "register",
  318. handle_pragma_register);
  319. }
  320. /* Attributes. */
  321. /* Handle the `task' function attribute. */
  322. static tree
  323. handle_task_attribute (tree *node, tree name, tree args,
  324. int flags, bool *no_add_attrs)
  325. {
  326. tree fn;
  327. fn = *node;
  328. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
  329. /* This is a function declaration for something local to this
  330. translation unit, so add the `task' attribute to FN. */
  331. *no_add_attrs = false;
  332. /* Add an empty `task_implementation_list' attribute. */
  333. DECL_ATTRIBUTES (fn) =
  334. tree_cons (get_identifier (task_implementation_list_attribute_name),
  335. NULL_TREE,
  336. NULL_TREE);
  337. /* Push a declaration for the corresponding `starpu_codelet' object and add
  338. it as an attribute of FN. */
  339. tree cl = build_codelet_declaration (fn);
  340. DECL_ATTRIBUTES (fn) =
  341. tree_cons (get_identifier (task_codelet_attribute_name), cl,
  342. DECL_ATTRIBUTES (fn));
  343. pushdecl (cl);
  344. TREE_USED (fn) = true;
  345. /* Lookup the useful StarPU functions in the global scope (this can't be
  346. done from `lower_starpu'.)
  347. XXX: Move it in a pass of its own. */
  348. #define LOOKUP_STARPU_FUNCTION(var, name) \
  349. if ((var) == NULL_TREE) \
  350. { \
  351. (var) = lookup_name (get_identifier (name)); \
  352. gcc_assert ((var) != NULL_TREE && TREE_CODE (var) == FUNCTION_DECL); \
  353. }
  354. LOOKUP_STARPU_FUNCTION (insert_task_fn, "starpu_insert_task");
  355. LOOKUP_STARPU_FUNCTION (data_lookup_fn, "starpu_data_lookup");
  356. #undef LOOKUP_STARPU_FUNCTION
  357. return NULL_TREE;
  358. }
  359. /* Handle the `task_implementation (WHERE, TASK)' attribute. WHERE is a
  360. string constant ("cpu", "cuda", etc.), and TASK is the identifier of a
  361. function declared with the `task' attribute. */
  362. static tree
  363. handle_task_implementation_attribute (tree *node, tree name, tree args,
  364. int flags, bool *no_add_attrs)
  365. {
  366. tree fn, where, task_decl;
  367. /* FIXME: Use error nodes instead of `gcc_assert'. */
  368. /* FIXME:TODO: To change the order to (TASK, WHERE):
  369. tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
  370. tree cleanup_decl = lookup_name (cleanup_id);
  371. */
  372. fn = *node;
  373. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
  374. where = TREE_VALUE (args);
  375. gcc_assert (TREE_CODE (where) == STRING_CST);
  376. task_decl = TREE_VALUE (TREE_CHAIN (args));
  377. gcc_assert (TREE_CODE (task_decl) == FUNCTION_DECL);
  378. gcc_assert (lookup_attribute (task_attribute_name,
  379. DECL_ATTRIBUTES (task_decl)));
  380. gcc_assert (TYPE_CANONICAL (TREE_TYPE (fn))
  381. == TYPE_CANONICAL (TREE_TYPE (task_decl))
  382. && TYPE_CANONICAL (TREE_TYPE (fn)) != NULL_TREE);
  383. /* Add FN to the list of implementations of TASK_DECL. */
  384. tree attr, impls;
  385. attr = lookup_attribute (task_implementation_list_attribute_name,
  386. DECL_ATTRIBUTES (task_decl));
  387. impls = tree_cons (NULL_TREE, fn, TREE_VALUE (attr));
  388. DECL_ATTRIBUTES (task_decl) =
  389. tree_cons (get_identifier (task_implementation_list_attribute_name),
  390. impls,
  391. remove_attribute (task_implementation_list_attribute_name,
  392. DECL_ATTRIBUTES (task_decl)));
  393. TREE_USED (fn) = true;
  394. /* Keep the attribute. */
  395. *no_add_attrs = false;
  396. return NULL_TREE;
  397. }
  398. /* Return the declaration of the `starpu_codelet' variable associated with
  399. TASK_DECL. */
  400. static tree
  401. task_codelet_declaration (const_tree task_decl)
  402. {
  403. tree cl_attr;
  404. cl_attr = lookup_attribute (task_codelet_attribute_name,
  405. DECL_ATTRIBUTES (task_decl));
  406. gcc_assert (cl_attr != NULL_TREE);
  407. return TREE_VALUE (cl_attr);
  408. }
  409. /* Return the list of implementations of TASK_DECL. */
  410. static tree
  411. task_implementation_list (const_tree task_decl)
  412. {
  413. tree attr;
  414. attr = lookup_attribute (task_implementation_list_attribute_name,
  415. DECL_ATTRIBUTES (task_decl));
  416. return TREE_VALUE (attr);
  417. }
  418. /* Return the list of pointer parameter types of TASK_DECL. */
  419. static tree
  420. task_pointer_parameter_types (const_tree task_decl)
  421. {
  422. bool is_pointer (const_tree item)
  423. {
  424. return POINTER_TYPE_P (TREE_VALUE (item));
  425. }
  426. return filter (is_pointer, TYPE_ARG_TYPES (TREE_TYPE (task_decl)));
  427. }
  428. /* Return a value indicating where TASK_IMPL should execute (`STARPU_CPU',
  429. `STARPU_CUDA', etc.). */
  430. static int
  431. task_implementation_where (const_tree task_impl)
  432. {
  433. int where_int;
  434. tree impl_attr, args, where;
  435. gcc_assert (TREE_CODE (task_impl) == FUNCTION_DECL);
  436. impl_attr = lookup_attribute (task_implementation_attribute_name,
  437. DECL_ATTRIBUTES (task_impl));
  438. gcc_assert (impl_attr != NULL_TREE);
  439. args = TREE_VALUE (impl_attr);
  440. where = TREE_VALUE (args);
  441. if (!strncmp (TREE_STRING_POINTER (where), "cpu",
  442. TREE_STRING_LENGTH (where)))
  443. where_int = STARPU_CPU;
  444. else if (!strncmp (TREE_STRING_POINTER (where), "opencl",
  445. TREE_STRING_LENGTH (where)))
  446. where_int = STARPU_OPENCL;
  447. else if (!strncmp (TREE_STRING_POINTER (where), "cuda",
  448. TREE_STRING_LENGTH (where)))
  449. where_int = STARPU_CUDA;
  450. else
  451. /* FIXME: Error out? */
  452. where_int = 0;
  453. return where_int;
  454. }
  455. /* Return the task implemented by TASK_IMPL. */
  456. static tree
  457. task_implementation_task (const_tree task_impl)
  458. {
  459. tree impl_attr, args;
  460. gcc_assert (TREE_CODE (task_impl) == FUNCTION_DECL);
  461. impl_attr = lookup_attribute (task_implementation_attribute_name,
  462. DECL_ATTRIBUTES (task_impl));
  463. gcc_assert (impl_attr != NULL_TREE);
  464. args = TREE_VALUE (impl_attr);
  465. return TREE_VALUE (TREE_CHAIN (args));
  466. }
  467. /* Return the FUNCTION_DECL of the wrapper generated for TASK_IMPL. */
  468. static tree
  469. task_implementation_wrapper (const_tree task_impl)
  470. {
  471. tree attr;
  472. gcc_assert (TREE_CODE (task_impl) == FUNCTION_DECL);
  473. attr = lookup_attribute (task_implementation_wrapper_attribute_name,
  474. DECL_ATTRIBUTES (task_impl));
  475. gcc_assert (attr != NULL_TREE);
  476. return TREE_VALUE (attr);
  477. }
  478. static void
  479. register_task_attributes (void *gcc_data, void *user_data)
  480. {
  481. static const struct attribute_spec task_attr =
  482. {
  483. task_attribute_name, 0, 0, true, false, false,
  484. handle_task_attribute
  485. };
  486. static const struct attribute_spec task_implementation_attr =
  487. {
  488. task_implementation_attribute_name, 2, 2, true, false, false,
  489. handle_task_implementation_attribute
  490. };
  491. register_attribute (&task_attr);
  492. register_attribute (&task_implementation_attr);
  493. }
  494. /* Return the type of a codelet function, i.e.,
  495. `void (*) (void **, void *)'. */
  496. static tree
  497. build_codelet_wrapper_type (void)
  498. {
  499. tree void_ptr_ptr;
  500. void_ptr_ptr = build_pointer_type (ptr_type_node);
  501. return build_function_type_list (void_type_node,
  502. void_ptr_ptr, ptr_type_node,
  503. NULL_TREE);
  504. }
  505. /* Return an identifier for the wrapper of TASK_IMPL, a task
  506. implementation. */
  507. static tree
  508. build_codelet_wrapper_identifier (tree task_impl)
  509. {
  510. static const char suffix[] = ".task_implementation_wrapper";
  511. tree id;
  512. char *cl_name;
  513. const char *task_name;
  514. id = DECL_NAME (task_impl);
  515. task_name = IDENTIFIER_POINTER (id);
  516. cl_name = alloca (IDENTIFIER_LENGTH (id) + strlen (suffix) + 1);
  517. memcpy (cl_name, task_name, IDENTIFIER_LENGTH (id));
  518. strcpy (&cl_name[IDENTIFIER_LENGTH (id)], suffix);
  519. return get_identifier (cl_name);
  520. }
  521. /* Return a function of type `void (*) (void **, void *)' that calls function
  522. TASK_IMPL, the FUNCTION_DECL of a task implementation whose prototype may
  523. be arbitrary. */
  524. static tree
  525. build_codelet_wrapper_definition (tree task_impl)
  526. {
  527. location_t loc;
  528. tree task_decl, decl;
  529. loc = DECL_SOURCE_LOCATION (task_impl);
  530. task_decl = task_implementation_task (task_impl);
  531. bool not_void_type_p (const_tree type)
  532. {
  533. return !VOID_TYPE_P (TREE_VALUE (type));
  534. }
  535. tree build_local_var (const_tree type)
  536. {
  537. tree var, t;
  538. const char *seed;
  539. t = TREE_VALUE (type);
  540. seed = POINTER_TYPE_P (t) ? "pointer_arg" : "scalar_arg";
  541. var = build_decl (loc, VAR_DECL, create_tmp_var_name (seed), t);
  542. DECL_CONTEXT (var) = decl;
  543. DECL_ARTIFICIAL (var) = true;
  544. return var;
  545. }
  546. /* Return the body of the wrapper, which unpacks `cl_args' and calls the
  547. user-defined task implementation. */
  548. tree build_body (tree wrapper_decl, tree vars)
  549. {
  550. tree stmts = NULL, call, unpack_fndecl, v;
  551. VEC(tree, gc) *args;
  552. unpack_fndecl = lookup_name (get_identifier ("starpu_unpack_cl_args"));
  553. gcc_assert (unpack_fndecl != NULL_TREE
  554. && TREE_CODE (unpack_fndecl) == FUNCTION_DECL);
  555. /* Build `var0 = STARPU_VECTOR_GET_PTR (buffers[0]); ...'. */
  556. size_t index = 0;
  557. for (v = vars; v != NULL_TREE; v = TREE_CHAIN (v))
  558. {
  559. if (POINTER_TYPE_P (TREE_TYPE (v)))
  560. {
  561. /* Compute `void *VDESC = buffers[0];'. */
  562. tree vdesc = array_ref (DECL_ARGUMENTS (wrapper_decl), index);
  563. /* Below we assume (1) that pointer arguments are registered as
  564. StarPU vector handles, and (2) that the `ptr' field is at
  565. offset 0 of `starpu_vector_interface_s'. The latter allows us
  566. to use a simple pointer dereference instead of expanding
  567. `STARPU_VECTOR_GET_PTR'. */
  568. assert (offsetof (struct starpu_vector_interface_s, ptr) == 0);
  569. /* Compute `type *PTR = *(type **) VDESC;'. */
  570. tree ptr = build1 (INDIRECT_REF,
  571. build_pointer_type (TREE_TYPE (v)),
  572. vdesc);
  573. append_to_statement_list (build2 (MODIFY_EXPR, TREE_TYPE (v),
  574. v, ptr),
  575. &stmts);
  576. index++;
  577. }
  578. }
  579. /* Build `starpu_unpack_cl_args (cl_args, &var1, &var2, ...)'. */
  580. args = NULL;
  581. VEC_safe_push (tree, gc, args, TREE_CHAIN (DECL_ARGUMENTS (wrapper_decl)));
  582. for (v = vars; v != NULL_TREE; v = TREE_CHAIN (v))
  583. {
  584. if (!POINTER_TYPE_P (TREE_TYPE (v)))
  585. VEC_safe_push (tree, gc, args, build_addr (v, wrapper_decl));
  586. }
  587. if (VEC_length (tree, args) > 1)
  588. {
  589. call = build_call_expr_loc_vec (UNKNOWN_LOCATION, unpack_fndecl, args);
  590. TREE_SIDE_EFFECTS (call) = 1;
  591. append_to_statement_list (call, &stmts);
  592. }
  593. /* Build `my_task_impl (var1, var2, ...)'. */
  594. args = NULL;
  595. for (v = vars; v != NULL_TREE; v = TREE_CHAIN (v))
  596. VEC_safe_push (tree, gc, args, v);
  597. call = build_call_expr_loc_vec (UNKNOWN_LOCATION, task_impl, args);
  598. TREE_SIDE_EFFECTS (call) = 1;
  599. append_to_statement_list (call, &stmts);
  600. tree bind;
  601. bind = build3 (BIND_EXPR, void_type_node, vars, stmts,
  602. DECL_INITIAL (wrapper_decl));
  603. TREE_TYPE (bind) = TREE_TYPE (TREE_TYPE (wrapper_decl));
  604. return bind;
  605. }
  606. /* Return the parameter list of the wrapper:
  607. `(void **BUFFERS, void *CL_ARGS)'. */
  608. tree build_parameters (tree wrapper_decl)
  609. {
  610. tree param1, param2;
  611. param1 = build_decl (loc, PARM_DECL,
  612. create_tmp_var_name ("buffers"),
  613. build_pointer_type (ptr_type_node));
  614. DECL_ARG_TYPE (param1) = ptr_type_node;
  615. DECL_CONTEXT (param1) = wrapper_decl;
  616. TREE_USED (param1) = true;
  617. param2 = build_decl (loc, PARM_DECL,
  618. create_tmp_var_name ("cl_args"),
  619. ptr_type_node);
  620. DECL_ARG_TYPE (param2) = ptr_type_node;
  621. DECL_CONTEXT (param2) = wrapper_decl;
  622. TREE_USED (param2) = true;
  623. return chainon (param1, param2);
  624. }
  625. tree wrapper_name, vars, result;
  626. wrapper_name = build_codelet_wrapper_identifier (task_impl);
  627. decl = build_decl (loc, FUNCTION_DECL, wrapper_name,
  628. build_codelet_wrapper_type ());
  629. vars = map (build_local_var,
  630. filter (not_void_type_p,
  631. TYPE_ARG_TYPES (TREE_TYPE (task_decl))));
  632. DECL_CONTEXT (decl) = NULL_TREE;
  633. DECL_ARGUMENTS (decl) = build_parameters (decl);
  634. result = build_decl (loc, RESULT_DECL, NULL_TREE, void_type_node);
  635. DECL_CONTEXT (result) = decl;
  636. DECL_ARTIFICIAL (result) = true;
  637. DECL_IGNORED_P (result) = true;
  638. DECL_RESULT (decl) = result;
  639. DECL_INITIAL (decl) = build_block (vars, NULL_TREE, decl, NULL_TREE);
  640. DECL_SAVED_TREE (decl) = build_body (decl, vars);
  641. TREE_PUBLIC (decl) = TREE_PUBLIC (task_impl);
  642. TREE_STATIC (decl) = true;
  643. TREE_USED (decl) = true;
  644. DECL_ARTIFICIAL (decl) = true;
  645. DECL_EXTERNAL (decl) = false;
  646. DECL_UNINLINABLE (decl) = true;
  647. rest_of_decl_compilation (decl, true, 0);
  648. struct function *prev_cfun = cfun;
  649. set_cfun (NULL);
  650. allocate_struct_function (decl, false);
  651. cfun->function_end_locus = DECL_SOURCE_LOCATION (task_impl);
  652. cgraph_finalize_function (decl, false);
  653. /* Mark DECL as needed so that it doesn't get removed by
  654. `cgraph_remove_unreachable_nodes' when it's not public. */
  655. cgraph_mark_needed_node (cgraph_get_node (decl));
  656. set_cfun (prev_cfun);
  657. return decl;
  658. }
  659. /* Define one wrapper function for each implementation of TASK. TASK should
  660. be the FUNCTION_DECL of a task. */
  661. static void
  662. define_codelet_wrappers (tree task)
  663. {
  664. void define (tree task_impl)
  665. {
  666. tree wrapper_def;
  667. wrapper_def = build_codelet_wrapper_definition (task_impl);
  668. DECL_ATTRIBUTES (task_impl) =
  669. tree_cons (get_identifier (task_implementation_wrapper_attribute_name),
  670. wrapper_def,
  671. DECL_ATTRIBUTES (task_impl));
  672. pushdecl (wrapper_def);
  673. }
  674. for_each (define, task_implementation_list (task));
  675. }
  676. /* Return a NODE_IDENTIFIER for the variable holding the `starpu_codelet'
  677. structure associated with TASK_DECL. */
  678. static tree
  679. build_codelet_identifier (tree task_decl)
  680. {
  681. static const char suffix[] = ".codelet";
  682. tree id;
  683. char *cl_name;
  684. const char *task_name;
  685. id = DECL_NAME (task_decl);
  686. task_name = IDENTIFIER_POINTER (id);
  687. cl_name = alloca (IDENTIFIER_LENGTH (id) + strlen (suffix) + 1);
  688. memcpy (cl_name, task_name, IDENTIFIER_LENGTH (id));
  689. strcpy (&cl_name[IDENTIFIER_LENGTH (id)], suffix);
  690. return get_identifier (cl_name);
  691. }
  692. static tree
  693. codelet_type (void)
  694. {
  695. tree type_decl;
  696. /* Lookup the `starpu_codelet' struct type. This should succeed since we
  697. push <starpu.h> early on. */
  698. type_decl = lookup_name (get_identifier (codelet_struct_name));
  699. gcc_assert (type_decl != NULL_TREE && TREE_CODE (type_decl) == TYPE_DECL);
  700. return TREE_TYPE (type_decl);
  701. }
  702. /* Return a VAR_DECL that declares a `starpu_codelet' structure for
  703. TASK_DECL. */
  704. static tree
  705. build_codelet_declaration (tree task_decl)
  706. {
  707. tree name, cl_decl;
  708. name = build_codelet_identifier (task_decl);
  709. cl_decl = build_decl (DECL_SOURCE_LOCATION (task_decl),
  710. VAR_DECL, name,
  711. /* c_build_qualified_type (type, TYPE_QUAL_CONST) */
  712. codelet_type ());
  713. DECL_ARTIFICIAL (cl_decl) = true;
  714. TREE_PUBLIC (cl_decl) = TREE_PUBLIC (task_decl);
  715. TREE_STATIC (cl_decl) = false;
  716. TREE_USED (cl_decl) = true;
  717. DECL_EXTERNAL (cl_decl) = true;
  718. DECL_CONTEXT (cl_decl) = NULL_TREE;
  719. return cl_decl;
  720. }
  721. /* Return a `starpu_codelet' initializer for TASK_DECL. */
  722. static tree
  723. build_codelet_initializer (tree task_decl)
  724. {
  725. tree fields;
  726. fields = TYPE_FIELDS (codelet_type ());
  727. gcc_assert (TREE_CODE (fields) == FIELD_DECL);
  728. tree lookup_field (const char *name)
  729. {
  730. tree fdecl, fname;
  731. fname = get_identifier (name);
  732. for (fdecl = fields;
  733. fdecl != NULL_TREE;
  734. fdecl = TREE_CHAIN (fdecl))
  735. {
  736. if (DECL_NAME (fdecl) == fname)
  737. return fdecl;
  738. }
  739. /* Field NAME wasn't found. */
  740. gcc_assert (false);
  741. }
  742. tree field_initializer (const char *name, tree value)
  743. {
  744. tree field, init;
  745. field = lookup_field (name);
  746. init = make_node (TREE_LIST);
  747. TREE_PURPOSE (init) = field;
  748. TREE_VALUE (init) = fold_convert (TREE_TYPE (field), value);
  749. TREE_CHAIN (init) = NULL_TREE;
  750. return init;
  751. }
  752. tree where_init (tree impls)
  753. {
  754. tree impl;
  755. int where_int = 0;
  756. for (impl = impls;
  757. impl != NULL_TREE;
  758. impl = TREE_CHAIN (impl))
  759. {
  760. tree impl_decl;
  761. impl_decl = TREE_VALUE (impl);
  762. gcc_assert (TREE_CODE (impl_decl) == FUNCTION_DECL);
  763. printf (" `%s'\n", IDENTIFIER_POINTER (DECL_NAME (impl_decl)));
  764. where_int |= task_implementation_where (impl_decl);
  765. }
  766. return build_int_cstu (integer_type_node, where_int);
  767. }
  768. tree implementation_pointer (tree impls, int where)
  769. {
  770. tree impl;
  771. for (impl = impls;
  772. impl != NULL_TREE;
  773. impl = TREE_CHAIN (impl))
  774. {
  775. tree impl_decl;
  776. impl_decl = TREE_VALUE (impl);
  777. if (task_implementation_where (impl_decl) == where)
  778. {
  779. /* Return a pointer to the wrapper of IMPL_DECL. */
  780. tree addr = build_addr (task_implementation_wrapper (impl_decl),
  781. NULL_TREE);
  782. return addr;
  783. }
  784. }
  785. /* Default to a NULL pointer. */
  786. return build_int_cstu (build_pointer_type (void_type_node), 0);
  787. }
  788. tree pointer_arg_count (void)
  789. {
  790. size_t len;
  791. len = list_length (task_pointer_parameter_types (task_decl));
  792. return build_int_cstu (integer_type_node, len);
  793. }
  794. printf ("implementations for `%s':\n",
  795. IDENTIFIER_POINTER (DECL_NAME (task_decl)));
  796. tree impls, inits;
  797. impls = task_implementation_list (task_decl);
  798. inits =
  799. chain_trees (field_initializer ("where", where_init (impls)),
  800. field_initializer ("nbuffers", pointer_arg_count ()),
  801. field_initializer ("cpu_func",
  802. implementation_pointer (impls, STARPU_CPU)),
  803. field_initializer ("opencl_func",
  804. implementation_pointer (impls,
  805. STARPU_OPENCL)),
  806. field_initializer ("cuda_func",
  807. implementation_pointer (impls,
  808. STARPU_CUDA)),
  809. NULL_TREE);
  810. return build_constructor_from_unsorted_list (codelet_type (), inits);
  811. }
  812. /* Return the VAR_DECL that defines a `starpu_codelet' structure for
  813. TASK_DECL. The VAR_DECL is assumed to already exists, so it must not be
  814. pushed again. */
  815. static tree
  816. define_codelet (tree task_decl)
  817. {
  818. /* Generate a wrapper function for each implementation of TASK_DECL that
  819. does all the packing/unpacking. */
  820. define_codelet_wrappers (task_decl);
  821. /* Retrieve the declaration of the `starpu_codelet' object. */
  822. tree cl_def;
  823. cl_def = lookup_name (build_codelet_identifier (task_decl));
  824. gcc_assert (cl_def != NULL_TREE && TREE_CODE (cl_def) == VAR_DECL);
  825. /* Turn the codelet declaration into a definition. */
  826. TREE_PUBLIC (cl_def) = TREE_PUBLIC (task_decl);
  827. TREE_STATIC (cl_def) = true;
  828. DECL_EXTERNAL (cl_def) = false;
  829. DECL_INITIAL (cl_def) = build_codelet_initializer (task_decl);
  830. return cl_def;
  831. }
  832. /* Define the `starpu_codelet' structure for the task implemented by
  833. IMPL_DECL if we're in the right compilation unit, i.e., is IMPL_DECL is a
  834. "cpu" task implementation. */
  835. static void
  836. maybe_define_codelet (tree impl_decl)
  837. {
  838. if (task_implementation_where (impl_decl) == STARPU_CPU)
  839. /* IMPL_DECL is a "cpu" implementation of some task, so define the
  840. codelet structure in this compilation unit. */
  841. define_codelet (task_implementation_task (impl_decl));
  842. }
  843. static void
  844. handle_pre_genericize (void *gcc_data, void *user_data)
  845. {
  846. tree fndecl = (tree) gcc_data;
  847. gcc_assert (fndecl != NULL_TREE && TREE_CODE (fndecl) == FUNCTION_DECL);
  848. gcc_assert (lookup_name (DECL_NAME (fndecl)) == fndecl);
  849. if (lookup_attribute (task_implementation_attribute_name,
  850. DECL_ATTRIBUTES (fndecl)))
  851. maybe_define_codelet (fndecl);
  852. }
  853. /* Build a "conversion" from a raw C pointer to its data handle. The
  854. assumption is that the programmer should have already registered the
  855. pointer by themselves. */
  856. static tree
  857. build_pointer_lookup (tree pointer, gimple_seq *body)
  858. {
  859. gimple emit_error_message (void)
  860. {
  861. static const char msg[] =
  862. "starpu: task called with unregistered pointer, aborting\n";
  863. return gimple_build_call (built_in_decls[BUILT_IN_PUTS], 1,
  864. build_string_literal (strlen (msg) + 1, msg));
  865. }
  866. tree var;
  867. var = create_tmp_var (ptr_type_node, ".handle-arg");
  868. mark_addressable (var);
  869. /* Initialize VAR with `starpu_data_lookup (POINTER)'. */
  870. gimple_seq init = NULL;
  871. tree modify = build2 (MODIFY_EXPR, ptr_type_node, var,
  872. build_call_expr (data_lookup_fn, 1, pointer));
  873. force_gimple_operand (modify, &init, true, var);
  874. gimple_seq_add_seq (body, init);
  875. /* FIXME: Add `if (VAR == NULL) abort ();'. */
  876. #if 0
  877. tree abort_label = create_artificial_label (UNKNOWN_LOCATION);
  878. tree success_label = create_artificial_label (UNKNOWN_LOCATION);
  879. gimple cond = gimple_build_cond (EQ_EXPR,
  880. var, null_pointer_node,
  881. abort_label, success_label);
  882. gimple_seq_add_stmt (body, cond);
  883. gimplify_seq_add_stmt (body, gimple_build_label (abort_label));
  884. gimple_seq_add_stmt (body, emit_error_message ());
  885. gimple_seq_add_stmt (body,
  886. gimple_build_call (built_in_decls[BUILT_IN_ABORT], 0));
  887. gimplify_seq_add_stmt (body, gimple_build_label (success_label));
  888. rebuild_cgraph_edges ();
  889. #endif
  890. return var;
  891. }
  892. /* Build a call to `starpu_insert_task' for TASK_DECL, which will replace
  893. CALL. */
  894. static gimple_seq
  895. build_task_submission (tree task_decl, gimple call)
  896. {
  897. /* Return a chain of local variables that need to be introduced. Variables
  898. are introduced for each argument that is either a scalar constant or a
  899. non-addressable variable---e.g., a `char' variable allocated in a
  900. register. Populate BODY with the initial assignments to these
  901. variables. */
  902. /* FIXME: We should also introduce local variables to mimic implicit
  903. integer type conversion---e.g., when the formal parameter type is `char'
  904. and the function is called with a `long' variable. */
  905. tree local_vars (gimple_seq *body)
  906. {
  907. size_t n;
  908. tree vars = NULL_TREE;
  909. tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (task_decl));
  910. for (n = 0;
  911. n < gimple_call_num_args (call);
  912. n++, arg_types = TREE_CHAIN (arg_types))
  913. {
  914. tree arg, type;
  915. /* Initially ARG_TYPES should be a list of N type nodes. */
  916. gcc_assert (TREE_CODE (arg_types) == TREE_LIST);
  917. type = TREE_VALUE (arg_types);
  918. gcc_assert (type != NULL_TREE);
  919. arg = gimple_call_arg (call, n);
  920. if (!POINTER_TYPE_P (type)
  921. && ((TREE_CONSTANT (arg)
  922. && TREE_CODE (arg) != VAR_DECL
  923. && TREE_CODE (arg) != ADDR_EXPR)
  924. || is_gimple_non_addressable (arg)))
  925. {
  926. /* ARG is a scalar constant or a non-addressable variable.
  927. Introduce a variable to hold it. */
  928. tree var =
  929. create_tmp_var (type,
  930. is_gimple_non_addressable (arg)
  931. ? ".non-addressable-arg"
  932. : ".literal-arg");
  933. mark_addressable (var);
  934. /* Initialize VAR. */
  935. tree init_value = fold_convert (type, arg);
  936. gimple_seq init = NULL;
  937. tree modify = build2 (MODIFY_EXPR, type, var, init_value);
  938. force_gimple_operand (modify, &init, true, var);
  939. gimple_seq_add_seq (body, init);
  940. if (vars != NULL_TREE)
  941. chainon (vars, var);
  942. else
  943. vars = var;
  944. }
  945. }
  946. return vars;
  947. }
  948. size_t n;
  949. VEC(tree, heap) *args = NULL;
  950. tree vars;
  951. gimple_seq body = NULL;
  952. tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (task_decl));
  953. vars = local_vars (&body);
  954. /* The first argument will be a pointer to the codelet. */
  955. VEC_safe_push (tree, heap, args,
  956. build_addr (task_codelet_declaration (task_decl),
  957. current_function_decl));
  958. for (n = 0;
  959. n < gimple_call_num_args (call);
  960. n++, arg_types = TREE_CHAIN (arg_types))
  961. {
  962. tree arg, type;
  963. arg = gimple_call_arg (call, n);
  964. type = TREE_VALUE (arg_types);
  965. if (POINTER_TYPE_P (type))
  966. {
  967. /* A pointer: the arguments will be:
  968. `STARPU_RW, ptr' or similar. */
  969. gcc_assert (TREE_CODE (arg) == VAR_DECL
  970. || TREE_CODE (arg) == ADDR_EXPR);
  971. /* If TYPE points to a const-qualified type, then mark the data as
  972. read-only; otherwise default to read-write.
  973. FIXME: Add an attribute to specify write-only. */
  974. int mode =
  975. (TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
  976. ? STARPU_R : STARPU_RW;
  977. VEC_safe_push (tree, heap, args,
  978. build_int_cst (integer_type_node, mode));
  979. VEC_safe_push (tree, heap, args, build_pointer_lookup (arg, &body));
  980. }
  981. else
  982. {
  983. /* A scalar: the arguments will be:
  984. `STARPU_VALUE, &scalar, sizeof (scalar)'. */
  985. if (is_gimple_non_addressable (arg) || TREE_CONSTANT (arg))
  986. {
  987. /* Use the local variable we introduced to hold ARG's
  988. value. */
  989. arg = vars;
  990. vars = TREE_CHAIN (vars);
  991. }
  992. gcc_assert (TREE_CODE (arg) == VAR_DECL);
  993. gcc_assert (TREE_ADDRESSABLE (arg));
  994. VEC_safe_push (tree, heap, args,
  995. build_int_cst (integer_type_node, STARPU_VALUE));
  996. VEC_safe_push (tree, heap, args,
  997. build_addr (arg, current_function_decl));
  998. VEC_safe_push (tree, heap, args,
  999. size_in_bytes (TREE_TYPE (arg)));
  1000. }
  1001. }
  1002. /* Push the terminating zero. */
  1003. VEC_safe_push (tree, heap, args,
  1004. build_int_cst (integer_type_node, 0));
  1005. gimple_seq_add_stmt (&body, gimple_build_call_vec (insert_task_fn, args));
  1006. return body;
  1007. }
  1008. static unsigned int
  1009. lower_starpu (void)
  1010. {
  1011. tree fndecl;
  1012. const struct cgraph_node *cgraph;
  1013. const struct cgraph_edge *callee;
  1014. fndecl = current_function_decl;
  1015. gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
  1016. /* This pass should occur after `build_cgraph_edges'. */
  1017. cgraph = cgraph_get_node (fndecl);
  1018. gcc_assert (cgraph != NULL);
  1019. for (callee = cgraph->callees;
  1020. callee != NULL;
  1021. callee = callee->next_callee)
  1022. {
  1023. gcc_assert (callee->callee != NULL);
  1024. tree callee_decl;
  1025. callee_decl = callee->callee->decl;
  1026. if (lookup_attribute (task_attribute_name,
  1027. DECL_ATTRIBUTES (callee_decl)))
  1028. {
  1029. printf ("%s: `%s' calls task `%s'\n", __func__,
  1030. IDENTIFIER_POINTER (DECL_NAME (fndecl)),
  1031. IDENTIFIER_POINTER (DECL_NAME (callee_decl)));
  1032. gimple call_site;
  1033. gimple_seq submission;
  1034. gimple_stmt_iterator gsi;
  1035. call_site = callee->call_stmt;
  1036. gsi = gsi_for_stmt (call_site);
  1037. submission = build_task_submission (callee_decl, call_site);
  1038. gsi_remove (&gsi, true);
  1039. gsi_insert_seq_before (&gsi, submission, GSI_SAME_STMT);
  1040. rebuild_cgraph_edges ();
  1041. }
  1042. }
  1043. return 0;
  1044. }
  1045. static struct opt_pass pass_lower_starpu =
  1046. {
  1047. .type = GIMPLE_PASS,
  1048. .name = "pass_lower_starpu",
  1049. .execute = lower_starpu,
  1050. /* The rest is zeroed. */
  1051. };
  1052. /* Initialization. */
  1053. static void
  1054. define_cpp_macros (void *gcc_data, void *user_data)
  1055. {
  1056. cpp_define (parse_in, "STARPU_GCC_PLUGIN=0");
  1057. cpp_push_include (parse_in, "starpu.h");
  1058. }
  1059. int
  1060. plugin_init (struct plugin_name_args *plugin_info,
  1061. struct plugin_gcc_version *version)
  1062. {
  1063. if (!plugin_default_version_check (version, &gcc_version))
  1064. return 1;
  1065. register_callback (plugin_name, PLUGIN_START_UNIT,
  1066. define_cpp_macros, NULL);
  1067. register_callback (plugin_name, PLUGIN_PRAGMAS,
  1068. register_pragmas, NULL);
  1069. register_callback (plugin_name, PLUGIN_ATTRIBUTES,
  1070. register_task_attributes, NULL);
  1071. register_callback (plugin_name, PLUGIN_PRE_GENERICIZE,
  1072. handle_pre_genericize, NULL);
  1073. /* Register our pass so that it happens after `build_cgraph_edges' has been
  1074. done. */
  1075. struct register_pass_info pass_info =
  1076. {
  1077. .pass = &pass_lower_starpu,
  1078. .reference_pass_name = "*build_cgraph_edges",
  1079. .ref_pass_instance_number = 1,
  1080. .pos_op = PASS_POS_INSERT_AFTER
  1081. };
  1082. register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP,
  1083. NULL, &pass_info);
  1084. return 0;
  1085. }