ppcg.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /*
  2. * Copyright 2011 INRIA Saclay
  3. * Copyright 2013 Ecole Normale Superieure
  4. * Copyright 2015 Sven Verdoolaege
  5. *
  6. * Use of this software is governed by the MIT license
  7. *
  8. * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  9. * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  10. * 91893 Orsay, France
  11. * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
  12. */
  13. #include <assert.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <isl/ctx.h>
  18. #include <isl/id.h>
  19. #include <isl/val.h>
  20. #include <isl/set.h>
  21. #include <isl/union_set.h>
  22. #include <isl/union_map.h>
  23. #include <isl/aff.h>
  24. #include <isl/flow.h>
  25. #include <isl/options.h>
  26. #include <isl/schedule.h>
  27. #include <isl/ast.h>
  28. #include <isl/id_to_ast_expr.h>
  29. #include <isl/ast_build.h>
  30. #include <isl/schedule.h>
  31. #include <pet.h>
  32. #include "ppcg.h"
  33. #include "ppcg_options.h"
  34. #include "cuda.h"
  35. #include "opencl.h"
  36. #include "cpu.h"
  37. struct options {
  38. struct pet_options *pet;
  39. struct ppcg_options *ppcg;
  40. char *input;
  41. char *output;
  42. };
  43. const char *ppcg_version(void);
  44. static void print_version(void)
  45. {
  46. printf("%s", ppcg_version());
  47. }
  48. ISL_ARGS_START(struct options, options_args)
  49. ISL_ARG_CHILD(struct options, pet, "pet", &pet_options_args, "pet options")
  50. ISL_ARG_CHILD(struct options, ppcg, NULL, &ppcg_options_args, "ppcg options")
  51. ISL_ARG_STR(struct options, output, 'o', NULL,
  52. "filename", NULL, "output filename (c and opencl targets)")
  53. ISL_ARG_ARG(struct options, input, "input", NULL)
  54. ISL_ARG_VERSION(print_version)
  55. ISL_ARGS_END
  56. ISL_ARG_DEF(options, struct options, options_args)
  57. /* Return a pointer to the final path component of "filename" or
  58. * to "filename" itself if it does not contain any components.
  59. */
  60. const char *ppcg_base_name(const char *filename)
  61. {
  62. const char *base;
  63. base = strrchr(filename, '/');
  64. if (base)
  65. return ++base;
  66. else
  67. return filename;
  68. }
  69. /* Copy the base name of "input" to "name" and return its length.
  70. * "name" is not NULL terminated.
  71. *
  72. * In particular, remove all leading directory components and
  73. * the final extension, if any.
  74. */
  75. int ppcg_extract_base_name(char *name, const char *input)
  76. {
  77. const char *base;
  78. const char *ext;
  79. int len;
  80. base = ppcg_base_name(input);
  81. ext = strrchr(base, '.');
  82. len = ext ? ext - base : strlen(base);
  83. memcpy(name, base, len);
  84. return len;
  85. }
  86. /* Does "scop" refer to any arrays that are declared, but not
  87. * exposed to the code after the scop?
  88. */
  89. int ppcg_scop_any_hidden_declarations(struct ppcg_scop *scop)
  90. {
  91. int i;
  92. if (!scop)
  93. return 0;
  94. // This is a pet feature not available in Polly.
  95. return 0;
  96. for (i = 0; i < scop->pet->n_array; ++i)
  97. if (scop->pet->arrays[i]->declared &&
  98. !scop->pet->arrays[i]->exposed)
  99. return 1;
  100. return 0;
  101. }
  102. /* Collect all variable names that are in use in "scop".
  103. * In particular, collect all parameters in the context and
  104. * all the array names.
  105. * Store these names in an isl_id_to_ast_expr by mapping
  106. * them to a dummy value (0).
  107. */
  108. static __isl_give isl_id_to_ast_expr *collect_names(struct pet_scop *scop)
  109. {
  110. int i, n;
  111. isl_ctx *ctx;
  112. isl_ast_expr *zero;
  113. isl_id_to_ast_expr *names;
  114. ctx = isl_set_get_ctx(scop->context);
  115. n = isl_set_dim(scop->context, isl_dim_param);
  116. names = isl_id_to_ast_expr_alloc(ctx, n + scop->n_array);
  117. zero = isl_ast_expr_from_val(isl_val_zero(ctx));
  118. for (i = 0; i < n; ++i) {
  119. isl_id *id;
  120. id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
  121. names = isl_id_to_ast_expr_set(names,
  122. id, isl_ast_expr_copy(zero));
  123. }
  124. for (i = 0; i < scop->n_array; ++i) {
  125. struct pet_array *array = scop->arrays[i];
  126. isl_id *id;
  127. id = isl_set_get_tuple_id(array->extent);
  128. names = isl_id_to_ast_expr_set(names,
  129. id, isl_ast_expr_copy(zero));
  130. }
  131. isl_ast_expr_free(zero);
  132. return names;
  133. }
  134. /* Return an isl_id called "prefix%d", with "%d" set to "i".
  135. * If an isl_id with such a name already appears among the variable names
  136. * of "scop", then adjust the name to "prefix%d_%d".
  137. */
  138. static __isl_give isl_id *generate_name(struct ppcg_scop *scop,
  139. const char *prefix, int i)
  140. {
  141. int j;
  142. char name[16];
  143. isl_ctx *ctx;
  144. isl_id *id;
  145. int has_name;
  146. ctx = isl_set_get_ctx(scop->context);
  147. snprintf(name, sizeof(name), "%s%d", prefix, i);
  148. id = isl_id_alloc(ctx, name, NULL);
  149. j = 0;
  150. while ((has_name = isl_id_to_ast_expr_has(scop->names, id)) == 1) {
  151. isl_id_free(id);
  152. snprintf(name, sizeof(name), "%s%d_%d", prefix, i, j++);
  153. id = isl_id_alloc(ctx, name, NULL);
  154. }
  155. return has_name < 0 ? isl_id_free(id) : id;
  156. }
  157. /* Return a list of "n" isl_ids of the form "prefix%d".
  158. * If an isl_id with such a name already appears among the variable names
  159. * of "scop", then adjust the name to "prefix%d_%d".
  160. */
  161. __isl_give isl_id_list *ppcg_scop_generate_names(struct ppcg_scop *scop,
  162. int n, const char *prefix)
  163. {
  164. int i;
  165. isl_ctx *ctx;
  166. isl_id_list *names;
  167. ctx = isl_set_get_ctx(scop->context);
  168. names = isl_id_list_alloc(ctx, n);
  169. for (i = 0; i < n; ++i) {
  170. isl_id *id;
  171. id = generate_name(scop, prefix, i);
  172. names = isl_id_list_add(names, id);
  173. }
  174. return names;
  175. }
  176. /* Is "stmt" not a kill statement?
  177. */
  178. static int is_not_kill(struct pet_stmt *stmt)
  179. {
  180. return !pet_stmt_is_kill(stmt);
  181. }
  182. /* Collect the iteration domains of the statements in "scop" that
  183. * satisfy "pred".
  184. */
  185. static __isl_give isl_union_set *collect_domains(struct pet_scop *scop,
  186. int (*pred)(struct pet_stmt *stmt))
  187. {
  188. int i;
  189. isl_set *domain_i;
  190. isl_union_set *domain;
  191. if (!scop)
  192. return NULL;
  193. domain = isl_union_set_empty(isl_set_get_space(scop->context));
  194. for (i = 0; i < scop->n_stmt; ++i) {
  195. struct pet_stmt *stmt = scop->stmts[i];
  196. if (!pred(stmt))
  197. continue;
  198. if (stmt->n_arg > 0)
  199. isl_die(isl_union_set_get_ctx(domain),
  200. isl_error_unsupported,
  201. "data dependent conditions not supported",
  202. return isl_union_set_free(domain));
  203. domain_i = isl_set_copy(scop->stmts[i]->domain);
  204. domain = isl_union_set_add_set(domain, domain_i);
  205. }
  206. return domain;
  207. }
  208. /* Collect the iteration domains of the statements in "scop",
  209. * skipping kill statements.
  210. */
  211. static __isl_give isl_union_set *collect_non_kill_domains(struct pet_scop *scop)
  212. {
  213. return collect_domains(scop, &is_not_kill);
  214. }
  215. /* This function is used as a callback to pet_expr_foreach_call_expr
  216. * to detect if there is any call expression in the input expression.
  217. * Assign the value 1 to the integer that "user" points to and
  218. * abort the search since we have found what we were looking for.
  219. */
  220. static int set_has_call(__isl_keep pet_expr *expr, void *user)
  221. {
  222. int *has_call = user;
  223. *has_call = 1;
  224. return -1;
  225. }
  226. /* Does "expr" contain any call expressions?
  227. */
  228. static int expr_has_call(__isl_keep pet_expr *expr)
  229. {
  230. int has_call = 0;
  231. if (pet_expr_foreach_call_expr(expr, &set_has_call, &has_call) < 0 &&
  232. !has_call)
  233. return -1;
  234. return has_call;
  235. }
  236. /* This function is a callback for pet_tree_foreach_expr.
  237. * If "expr" contains any call (sub)expressions, then set *has_call
  238. * and abort the search.
  239. */
  240. static int check_call(__isl_keep pet_expr *expr, void *user)
  241. {
  242. int *has_call = user;
  243. if (expr_has_call(expr))
  244. *has_call = 1;
  245. return *has_call ? -1 : 0;
  246. }
  247. /* Does "stmt" contain any call expressions?
  248. */
  249. static int has_call(struct pet_stmt *stmt)
  250. {
  251. int has_call = 0;
  252. if (pet_tree_foreach_expr(stmt->body, &check_call, &has_call) < 0 &&
  253. !has_call)
  254. return -1;
  255. return has_call;
  256. }
  257. /* Collect the iteration domains of the statements in "scop"
  258. * that contain a call expression.
  259. */
  260. static __isl_give isl_union_set *collect_call_domains(struct pet_scop *scop)
  261. {
  262. return collect_domains(scop, &has_call);
  263. }
  264. /* Given a union of "tagged" access relations of the form
  265. *
  266. * [S_i[...] -> R_j[]] -> A_k[...]
  267. *
  268. * project out the "tags" (R_j[]).
  269. * That is, return a union of relations of the form
  270. *
  271. * S_i[...] -> A_k[...]
  272. */
  273. static __isl_give isl_union_map *project_out_tags(
  274. __isl_take isl_union_map *umap)
  275. {
  276. return isl_union_map_domain_factor_domain(umap);
  277. }
  278. /* Construct a function from tagged iteration domains to the corresponding
  279. * untagged iteration domains with as range of the wrapped map in the domain
  280. * the reference tags that appear in any of the reads, writes or kills.
  281. * Store the result in ps->tagger.
  282. *
  283. * For example, if the statement with iteration space S[i,j]
  284. * contains two array references R_1[] and R_2[], then ps->tagger will contain
  285. *
  286. * { [S[i,j] -> R_1[]] -> S[i,j]; [S[i,j] -> R_2[]] -> S[i,j] }
  287. */
  288. void compute_tagger(struct ppcg_scop *ps)
  289. {
  290. isl_union_map *tagged;
  291. isl_union_pw_multi_aff *tagger;
  292. tagged = isl_union_map_copy(ps->tagged_reads);
  293. tagged = isl_union_map_union(tagged,
  294. isl_union_map_copy(ps->tagged_may_writes));
  295. tagged = isl_union_map_union(tagged,
  296. isl_union_map_copy(ps->tagged_must_kills));
  297. tagged = isl_union_map_universe(tagged);
  298. tagged = isl_union_set_unwrap(isl_union_map_domain(tagged));
  299. tagger = isl_union_map_domain_map_union_pw_multi_aff(tagged);
  300. ps->tagger = tagger;
  301. }
  302. /* Compute the live out accesses, i.e., the writes that are
  303. * potentially not killed by any kills or any other writes, and
  304. * store them in ps->live_out.
  305. *
  306. * We compute the "dependence" of any "kill" (an explicit kill
  307. * or a must write) on any may write.
  308. * The elements accessed by the may writes with a "depending" kill
  309. * also accessing the element are definitely killed.
  310. * The remaining may writes can potentially be live out.
  311. *
  312. * The result of the dependence analysis is
  313. *
  314. * { IW -> [IK -> A] }
  315. *
  316. * with IW the instance of the write statement, IK the instance of kill
  317. * statement and A the element that was killed.
  318. * The range factor range is
  319. *
  320. * { IW -> A }
  321. *
  322. * containing all such pairs for which there is a kill statement instance,
  323. * i.e., all pairs that have been killed.
  324. */
  325. static void compute_live_out(struct ppcg_scop *ps)
  326. {
  327. isl_schedule *schedule;
  328. isl_union_map *kills;
  329. isl_union_map *exposed;
  330. isl_union_map *covering;
  331. isl_union_access_info *access;
  332. isl_union_flow *flow;
  333. schedule = isl_schedule_copy(ps->schedule);
  334. kills = isl_union_map_union(isl_union_map_copy(ps->must_writes),
  335. isl_union_map_copy(ps->must_kills));
  336. access = isl_union_access_info_from_sink(kills);
  337. access = isl_union_access_info_set_may_source(access,
  338. isl_union_map_copy(ps->may_writes));
  339. access = isl_union_access_info_set_schedule(access, schedule);
  340. flow = isl_union_access_info_compute_flow(access);
  341. covering = isl_union_flow_get_full_may_dependence(flow);
  342. isl_union_flow_free(flow);
  343. covering = isl_union_map_range_factor_range(covering);
  344. exposed = isl_union_map_copy(ps->may_writes);
  345. exposed = isl_union_map_subtract(exposed, covering);
  346. ps->live_out = exposed;
  347. }
  348. /* Compute the tagged flow dependences and the live_in accesses and store
  349. * the results in ps->tagged_dep_flow and ps->live_in.
  350. *
  351. * We allow both the must writes and the must kills to serve as
  352. * definite sources such that a subsequent read would not depend
  353. * on any earlier write. The resulting flow dependences with
  354. * a must kill as source reflect possibly uninitialized reads.
  355. * No dependences need to be introduced to protect such reads
  356. * (other than those imposed by potential flows from may writes
  357. * that follow the kill). We therefore remove those flow dependences.
  358. * This is also useful for the dead code elimination, which assumes
  359. * the flow sources are non-kill instances.
  360. */
  361. static void compute_tagged_flow_dep_only(struct ppcg_scop *ps)
  362. {
  363. isl_union_pw_multi_aff *tagger;
  364. isl_schedule *schedule;
  365. isl_union_map *live_in;
  366. isl_union_access_info *access;
  367. isl_union_flow *flow;
  368. isl_union_map *must_source;
  369. isl_union_map *kills;
  370. isl_union_map *tagged_flow;
  371. tagger = isl_union_pw_multi_aff_copy(ps->tagger);
  372. schedule = isl_schedule_copy(ps->schedule);
  373. schedule = isl_schedule_pullback_union_pw_multi_aff(schedule, tagger);
  374. kills = isl_union_map_copy(ps->tagged_must_kills);
  375. must_source = isl_union_map_copy(ps->tagged_must_writes);
  376. must_source = isl_union_map_union(must_source,
  377. isl_union_map_copy(kills));
  378. access = isl_union_access_info_from_sink(
  379. isl_union_map_copy(ps->tagged_reads));
  380. access = isl_union_access_info_set_must_source(access, must_source);
  381. access = isl_union_access_info_set_may_source(access,
  382. isl_union_map_copy(ps->tagged_may_writes));
  383. access = isl_union_access_info_set_schedule(access, schedule);
  384. flow = isl_union_access_info_compute_flow(access);
  385. tagged_flow = isl_union_flow_get_may_dependence(flow);
  386. tagged_flow = isl_union_map_subtract_domain(tagged_flow,
  387. isl_union_map_domain(kills));
  388. ps->tagged_dep_flow = tagged_flow;
  389. live_in = isl_union_flow_get_may_no_source(flow);
  390. ps->live_in = project_out_tags(live_in);
  391. isl_union_flow_free(flow);
  392. }
  393. /* Compute ps->dep_flow from ps->tagged_dep_flow
  394. * by projecting out the reference tags.
  395. */
  396. static void derive_flow_dep_from_tagged_flow_dep(struct ppcg_scop *ps)
  397. {
  398. ps->dep_flow = isl_union_map_copy(ps->tagged_dep_flow);
  399. ps->dep_flow = isl_union_map_factor_domain(ps->dep_flow);
  400. }
  401. /* Compute the flow dependences and the live_in accesses and store
  402. * the results in ps->dep_flow and ps->live_in.
  403. * A copy of the flow dependences, tagged with the reference tags
  404. * is stored in ps->tagged_dep_flow.
  405. *
  406. * We first compute ps->tagged_dep_flow, i.e., the tagged flow dependences
  407. * and then project out the tags.
  408. */
  409. static void compute_tagged_flow_dep(struct ppcg_scop *ps)
  410. {
  411. compute_tagged_flow_dep_only(ps);
  412. derive_flow_dep_from_tagged_flow_dep(ps);
  413. }
  414. /* Compute the order dependences that prevent the potential live ranges
  415. * from overlapping.
  416. *
  417. * In particular, construct a union of relations
  418. *
  419. * [R[...] -> R_1[]] -> [W[...] -> R_2[]]
  420. *
  421. * where [R[...] -> R_1[]] is the range of one or more live ranges
  422. * (i.e., a read) and [W[...] -> R_2[]] is the domain of one or more
  423. * live ranges (i.e., a write). Moreover, the read and the write
  424. * access the same memory element and the read occurs before the write
  425. * in the original schedule.
  426. * The scheduler allows some of these dependences to be violated, provided
  427. * the adjacent live ranges are all local (i.e., their domain and range
  428. * are mapped to the same point by the current schedule band).
  429. *
  430. * Note that if a live range is not local, then we need to make
  431. * sure it does not overlap with _any_ other live range, and not
  432. * just with the "previous" and/or the "next" live range.
  433. * We therefore add order dependences between reads and
  434. * _any_ later potential write.
  435. *
  436. * We also need to be careful about writes without a corresponding read.
  437. * They are already prevented from moving past non-local preceding
  438. * intervals, but we also need to prevent them from moving past non-local
  439. * following intervals. We therefore also add order dependences from
  440. * potential writes that do not appear in any intervals
  441. * to all later potential writes.
  442. * Note that dead code elimination should have removed most of these
  443. * dead writes, but the dead code elimination may not remove all dead writes,
  444. * so we need to consider them to be safe.
  445. *
  446. * The order dependences are computed by computing the "dataflow"
  447. * from the above unmatched writes and the reads to the may writes.
  448. * The unmatched writes and the reads are treated as may sources
  449. * such that they would not kill order dependences from earlier
  450. * such writes and reads.
  451. */
  452. static void compute_order_dependences(struct ppcg_scop *ps)
  453. {
  454. isl_union_map *reads;
  455. isl_union_map *shared_access;
  456. isl_union_set *matched;
  457. isl_union_map *unmatched;
  458. isl_union_pw_multi_aff *tagger;
  459. isl_schedule *schedule;
  460. isl_union_access_info *access;
  461. isl_union_flow *flow;
  462. tagger = isl_union_pw_multi_aff_copy(ps->tagger);
  463. schedule = isl_schedule_copy(ps->schedule);
  464. schedule = isl_schedule_pullback_union_pw_multi_aff(schedule, tagger);
  465. reads = isl_union_map_copy(ps->tagged_reads);
  466. matched = isl_union_map_domain(isl_union_map_copy(ps->tagged_dep_flow));
  467. unmatched = isl_union_map_copy(ps->tagged_may_writes);
  468. unmatched = isl_union_map_subtract_domain(unmatched, matched);
  469. reads = isl_union_map_union(reads, unmatched);
  470. access = isl_union_access_info_from_sink(
  471. isl_union_map_copy(ps->tagged_may_writes));
  472. access = isl_union_access_info_set_may_source(access, reads);
  473. access = isl_union_access_info_set_schedule(access, schedule);
  474. flow = isl_union_access_info_compute_flow(access);
  475. shared_access = isl_union_flow_get_may_dependence(flow);
  476. isl_union_flow_free(flow);
  477. ps->tagged_dep_order = isl_union_map_copy(shared_access);
  478. ps->dep_order = isl_union_map_factor_domain(shared_access);
  479. }
  480. /* Compute those validity dependences of the program represented by "scop"
  481. * that should be unconditionally enforced even when live-range reordering
  482. * is used.
  483. *
  484. * In particular, compute the external false dependences
  485. * as well as order dependences between sources with the same sink.
  486. * The anti-dependences are already taken care of by the order dependences.
  487. * The external false dependences are only used to ensure that live-in and
  488. * live-out data is not overwritten by any writes inside the scop.
  489. * The independences are removed from the external false dependences,
  490. * but not from the order dependences between sources with the same sink.
  491. *
  492. * In particular, the reads from live-in data need to precede any
  493. * later write to the same memory element.
  494. * As to live-out data, the last writes need to remain the last writes.
  495. * That is, any earlier write in the original schedule needs to precede
  496. * the last write to the same memory element in the computed schedule.
  497. * The possible last writes have been computed by compute_live_out.
  498. * They may include kills, but if the last access is a kill,
  499. * then the corresponding dependences will effectively be ignored
  500. * since we do not schedule any kill statements.
  501. *
  502. * Note that the set of live-in and live-out accesses may be
  503. * an overapproximation. There may therefore be potential writes
  504. * before a live-in access and after a live-out access.
  505. *
  506. * In the presence of may-writes, there may be multiple live-ranges
  507. * with the same sink, accessing the same memory element.
  508. * The sources of these live-ranges need to be executed
  509. * in the same relative order as in the original program
  510. * since we do not know which of the may-writes will actually
  511. * perform a write. Consider all sources that share a sink and
  512. * that may write to the same memory element and compute
  513. * the order dependences among them.
  514. */
  515. static void compute_forced_dependences(struct ppcg_scop *ps)
  516. {
  517. isl_union_map *shared_access;
  518. isl_union_map *exposed;
  519. isl_union_map *live_in;
  520. isl_union_map *sink_access;
  521. isl_union_map *shared_sink;
  522. isl_union_access_info *access;
  523. isl_union_flow *flow;
  524. isl_schedule *schedule;
  525. exposed = isl_union_map_copy(ps->live_out);
  526. schedule = isl_schedule_copy(ps->schedule);
  527. access = isl_union_access_info_from_sink(exposed);
  528. access = isl_union_access_info_set_may_source(access,
  529. isl_union_map_copy(ps->may_writes));
  530. access = isl_union_access_info_set_schedule(access, schedule);
  531. flow = isl_union_access_info_compute_flow(access);
  532. shared_access = isl_union_flow_get_may_dependence(flow);
  533. isl_union_flow_free(flow);
  534. ps->dep_forced = shared_access;
  535. schedule = isl_schedule_copy(ps->schedule);
  536. access = isl_union_access_info_from_sink(
  537. isl_union_map_copy(ps->may_writes));
  538. access = isl_union_access_info_set_may_source(access,
  539. isl_union_map_copy(ps->live_in));
  540. access = isl_union_access_info_set_schedule(access, schedule);
  541. flow = isl_union_access_info_compute_flow(access);
  542. live_in = isl_union_flow_get_may_dependence(flow);
  543. isl_union_flow_free(flow);
  544. ps->dep_forced = isl_union_map_union(ps->dep_forced, live_in);
  545. ps->dep_forced = isl_union_map_subtract(ps->dep_forced,
  546. isl_union_map_copy(ps->independence));
  547. schedule = isl_schedule_copy(ps->schedule);
  548. sink_access = isl_union_map_copy(ps->tagged_dep_flow);
  549. sink_access = isl_union_map_range_product(sink_access,
  550. isl_union_map_copy(ps->tagged_may_writes));
  551. sink_access = isl_union_map_domain_factor_domain(sink_access);
  552. access = isl_union_access_info_from_sink(
  553. isl_union_map_copy(sink_access));
  554. access = isl_union_access_info_set_may_source(access, sink_access);
  555. access = isl_union_access_info_set_schedule(access, schedule);
  556. flow = isl_union_access_info_compute_flow(access);
  557. shared_sink = isl_union_flow_get_may_dependence(flow);
  558. isl_union_flow_free(flow);
  559. ps->dep_forced = isl_union_map_union(ps->dep_forced, shared_sink);
  560. }
  561. /* Remove independence from the tagged flow dependences.
  562. * Since the user has guaranteed that source and sink of an independence
  563. * can be executed in any order, there cannot be a flow dependence
  564. * between them, so they can be removed from the set of flow dependences.
  565. * However, if the source of such a flow dependence is a must write,
  566. * then it may have killed other potential sources, which would have
  567. * to be recovered if we were to remove those flow dependences.
  568. * We therefore keep the flow dependences that originate in a must write,
  569. * even if it corresponds to a known independence.
  570. */
  571. static void remove_independences_from_tagged_flow(struct ppcg_scop *ps)
  572. {
  573. isl_union_map *tf;
  574. isl_union_set *indep;
  575. isl_union_set *mw;
  576. tf = isl_union_map_copy(ps->tagged_dep_flow);
  577. tf = isl_union_map_zip(tf);
  578. indep = isl_union_map_wrap(isl_union_map_copy(ps->independence));
  579. tf = isl_union_map_intersect_domain(tf, indep);
  580. tf = isl_union_map_zip(tf);
  581. mw = isl_union_map_domain(isl_union_map_copy(ps->tagged_must_writes));
  582. tf = isl_union_map_subtract_domain(tf, mw);
  583. ps->tagged_dep_flow = isl_union_map_subtract(ps->tagged_dep_flow, tf);
  584. }
  585. /* Compute the dependences of the program represented by "scop"
  586. * in case live range reordering is allowed.
  587. *
  588. * We compute the actual live ranges and the corresponding order
  589. * false dependences.
  590. *
  591. * The independences are removed from the flow dependences
  592. * (provided the source is not a must-write) as well as
  593. * from the external false dependences (by compute_forced_dependences).
  594. */
  595. static void compute_live_range_reordering_dependences(struct ppcg_scop *ps)
  596. {
  597. compute_tagged_flow_dep_only(ps);
  598. remove_independences_from_tagged_flow(ps);
  599. derive_flow_dep_from_tagged_flow_dep(ps);
  600. compute_order_dependences(ps);
  601. compute_forced_dependences(ps);
  602. }
  603. /* Compute the potential flow dependences and the potential live in
  604. * accesses.
  605. */
  606. static void compute_flow_dep(struct ppcg_scop *ps)
  607. {
  608. isl_union_access_info *access;
  609. isl_union_flow *flow;
  610. access = isl_union_access_info_from_sink(isl_union_map_copy(ps->reads));
  611. access = isl_union_access_info_set_must_source(access,
  612. isl_union_map_copy(ps->must_writes));
  613. access = isl_union_access_info_set_may_source(access,
  614. isl_union_map_copy(ps->may_writes));
  615. access = isl_union_access_info_set_schedule(access,
  616. isl_schedule_copy(ps->schedule));
  617. flow = isl_union_access_info_compute_flow(access);
  618. ps->dep_flow = isl_union_flow_get_may_dependence(flow);
  619. ps->live_in = isl_union_flow_get_may_no_source(flow);
  620. isl_union_flow_free(flow);
  621. }
  622. /* Compute the dependences of the program represented by "scop".
  623. * Store the computed potential flow dependences
  624. * in scop->dep_flow and the reads with potentially no corresponding writes in
  625. * scop->live_in.
  626. * Store the potential live out accesses in scop->live_out.
  627. * Store the potential false (anti and output) dependences in scop->dep_false.
  628. *
  629. * If live range reordering is allowed, then we compute a separate
  630. * set of order dependences and a set of external false dependences
  631. * in compute_live_range_reordering_dependences.
  632. */
  633. void compute_dependences(struct ppcg_scop *scop)
  634. {
  635. isl_union_map *may_source;
  636. isl_union_access_info *access;
  637. isl_union_flow *flow;
  638. if (!scop)
  639. return;
  640. compute_live_out(scop);
  641. if (scop->options->live_range_reordering)
  642. compute_live_range_reordering_dependences(scop);
  643. else if (scop->options->target != PPCG_TARGET_C)
  644. compute_tagged_flow_dep(scop);
  645. else
  646. compute_flow_dep(scop);
  647. may_source = isl_union_map_union(isl_union_map_copy(scop->may_writes),
  648. isl_union_map_copy(scop->reads));
  649. access = isl_union_access_info_from_sink(
  650. isl_union_map_copy(scop->may_writes));
  651. access = isl_union_access_info_set_must_source(access,
  652. isl_union_map_copy(scop->must_writes));
  653. access = isl_union_access_info_set_may_source(access, may_source);
  654. access = isl_union_access_info_set_schedule(access,
  655. isl_schedule_copy(scop->schedule));
  656. flow = isl_union_access_info_compute_flow(access);
  657. scop->dep_false = isl_union_flow_get_may_dependence(flow);
  658. scop->dep_false = isl_union_map_coalesce(scop->dep_false);
  659. isl_union_flow_free(flow);
  660. }
  661. /* Eliminate dead code from ps->domain.
  662. *
  663. * In particular, intersect both ps->domain and the domain of
  664. * ps->schedule with the (parts of) iteration
  665. * domains that are needed to produce the output or for statement
  666. * iterations that call functions.
  667. * Also intersect the range of the dataflow dependences with
  668. * this domain such that the removed instances will no longer
  669. * be considered as targets of dataflow.
  670. *
  671. * We start with the iteration domains that call functions
  672. * and the set of iterations that last write to an array
  673. * (except those that are later killed).
  674. *
  675. * Then we add those statement iterations that produce
  676. * something needed by the "live" statements iterations.
  677. * We keep doing this until no more statement iterations can be added.
  678. * To ensure that the procedure terminates, we compute the affine
  679. * hull of the live iterations (bounded to the original iteration
  680. * domains) each time we have added extra iterations.
  681. */
  682. void eliminate_dead_code(struct ppcg_scop *ps)
  683. {
  684. isl_union_set *live;
  685. isl_union_map *dep;
  686. isl_union_pw_multi_aff *tagger;
  687. live = isl_union_map_domain(isl_union_map_copy(ps->live_out));
  688. if (!isl_union_set_is_empty(ps->call)) {
  689. live = isl_union_set_union(live, isl_union_set_copy(ps->call));
  690. live = isl_union_set_coalesce(live);
  691. }
  692. dep = isl_union_map_copy(ps->dep_flow);
  693. dep = isl_union_map_reverse(dep);
  694. for (;;) {
  695. isl_union_set *extra;
  696. extra = isl_union_set_apply(isl_union_set_copy(live),
  697. isl_union_map_copy(dep));
  698. if (isl_union_set_is_subset(extra, live)) {
  699. isl_union_set_free(extra);
  700. break;
  701. }
  702. live = isl_union_set_union(live, extra);
  703. live = isl_union_set_affine_hull(live);
  704. live = isl_union_set_intersect(live,
  705. isl_union_set_copy(ps->domain));
  706. }
  707. isl_union_map_free(dep);
  708. ps->domain = isl_union_set_intersect(ps->domain,
  709. isl_union_set_copy(live));
  710. ps->schedule = isl_schedule_intersect_domain(ps->schedule,
  711. isl_union_set_copy(live));
  712. ps->dep_flow = isl_union_map_intersect_range(ps->dep_flow,
  713. isl_union_set_copy(live));
  714. tagger = isl_union_pw_multi_aff_copy(ps->tagger);
  715. live = isl_union_set_preimage_union_pw_multi_aff(live, tagger);
  716. ps->tagged_dep_flow = isl_union_map_intersect_range(ps->tagged_dep_flow,
  717. live);
  718. }
  719. /* Intersect "set" with the set described by "str", taking the NULL
  720. * string to represent the universal set.
  721. */
  722. static __isl_give isl_set *set_intersect_str(__isl_take isl_set *set,
  723. const char *str)
  724. {
  725. isl_ctx *ctx;
  726. isl_set *set2;
  727. if (!str)
  728. return set;
  729. ctx = isl_set_get_ctx(set);
  730. set2 = isl_set_read_from_str(ctx, str);
  731. set = isl_set_intersect(set, set2);
  732. return set;
  733. }
  734. void *ppcg_scop_free(struct ppcg_scop *ps)
  735. {
  736. if (!ps)
  737. return NULL;
  738. isl_set_free(ps->context);
  739. isl_union_set_free(ps->domain);
  740. isl_union_set_free(ps->call);
  741. isl_union_map_free(ps->tagged_reads);
  742. isl_union_map_free(ps->reads);
  743. isl_union_map_free(ps->live_in);
  744. isl_union_map_free(ps->tagged_may_writes);
  745. isl_union_map_free(ps->tagged_must_writes);
  746. isl_union_map_free(ps->may_writes);
  747. isl_union_map_free(ps->must_writes);
  748. isl_union_map_free(ps->live_out);
  749. isl_union_map_free(ps->tagged_must_kills);
  750. isl_union_map_free(ps->must_kills);
  751. isl_union_map_free(ps->tagged_dep_flow);
  752. isl_union_map_free(ps->dep_flow);
  753. isl_union_map_free(ps->dep_false);
  754. isl_union_map_free(ps->dep_forced);
  755. isl_union_map_free(ps->tagged_dep_order);
  756. isl_union_map_free(ps->dep_order);
  757. isl_schedule_free(ps->schedule);
  758. isl_union_pw_multi_aff_free(ps->tagger);
  759. isl_union_map_free(ps->independence);
  760. isl_id_to_ast_expr_free(ps->names);
  761. free(ps);
  762. return NULL;
  763. }
  764. /* Extract a ppcg_scop from a pet_scop.
  765. *
  766. * The constructed ppcg_scop refers to elements from the pet_scop
  767. * so the pet_scop should not be freed before the ppcg_scop.
  768. */
  769. static struct ppcg_scop *ppcg_scop_from_pet_scop(struct pet_scop *scop,
  770. struct ppcg_options *options)
  771. {
  772. int i;
  773. isl_ctx *ctx;
  774. struct ppcg_scop *ps;
  775. if (!scop)
  776. return NULL;
  777. ctx = isl_set_get_ctx(scop->context);
  778. ps = isl_calloc_type(ctx, struct ppcg_scop);
  779. if (!ps)
  780. return NULL;
  781. ps->names = collect_names(scop);
  782. ps->options = options;
  783. ps->start = pet_loc_get_start(scop->loc);
  784. ps->end = pet_loc_get_end(scop->loc);
  785. ps->context = isl_set_copy(scop->context);
  786. ps->context = set_intersect_str(ps->context, options->ctx);
  787. if (options->non_negative_parameters) {
  788. isl_space *space = isl_set_get_space(ps->context);
  789. isl_set *nn = isl_set_nat_universe(space);
  790. ps->context = isl_set_intersect(ps->context, nn);
  791. }
  792. ps->domain = collect_non_kill_domains(scop);
  793. ps->call = collect_call_domains(scop);
  794. ps->tagged_reads = pet_scop_get_tagged_may_reads(scop);
  795. ps->reads = pet_scop_get_may_reads(scop);
  796. ps->tagged_may_writes = pet_scop_get_tagged_may_writes(scop);
  797. ps->may_writes = pet_scop_get_may_writes(scop);
  798. ps->tagged_must_writes = pet_scop_get_tagged_must_writes(scop);
  799. ps->must_writes = pet_scop_get_must_writes(scop);
  800. ps->tagged_must_kills = pet_scop_get_tagged_must_kills(scop);
  801. ps->must_kills = pet_scop_get_must_kills(scop);
  802. ps->schedule = isl_schedule_copy(scop->schedule);
  803. ps->pet = scop;
  804. ps->independence = isl_union_map_empty(isl_set_get_space(ps->context));
  805. for (i = 0; i < scop->n_independence; ++i)
  806. ps->independence = isl_union_map_union(ps->independence,
  807. isl_union_map_copy(scop->independences[i]->filter));
  808. compute_tagger(ps);
  809. compute_dependences(ps);
  810. eliminate_dead_code(ps);
  811. if (!ps->context || !ps->domain || !ps->call || !ps->reads ||
  812. !ps->may_writes || !ps->must_writes || !ps->tagged_must_kills ||
  813. !ps->must_kills || !ps->schedule || !ps->independence || !ps->names)
  814. return ppcg_scop_free(ps);
  815. return ps;
  816. }
  817. /* Internal data structure for ppcg_transform.
  818. */
  819. struct ppcg_transform_data {
  820. struct ppcg_options *options;
  821. __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
  822. struct ppcg_scop *scop, void *user);
  823. void *user;
  824. };
  825. /* Should we print the original code?
  826. * That is, does "scop" involve any data dependent conditions or
  827. * nested expressions that cannot be handled by pet_stmt_build_ast_exprs?
  828. */
  829. static int print_original(struct pet_scop *scop, struct ppcg_options *options)
  830. {
  831. if (!pet_scop_can_build_ast_exprs(scop)) {
  832. if (options->debug->verbose)
  833. fprintf(stdout, "Printing original code because "
  834. "some index expressions cannot currently "
  835. "be printed\n");
  836. return 1;
  837. }
  838. if (pet_scop_has_data_dependent_conditions(scop)) {
  839. if (options->debug->verbose)
  840. fprintf(stdout, "Printing original code because "
  841. "input involves data dependent conditions\n");
  842. return 1;
  843. }
  844. return 0;
  845. }
  846. /* Callback for pet_transform_C_source that transforms
  847. * the given pet_scop to a ppcg_scop before calling the
  848. * ppcg_transform callback.
  849. *
  850. * If "scop" contains any data dependent conditions or if we may
  851. * not be able to print the transformed program, then just print
  852. * the original code.
  853. */
  854. static __isl_give isl_printer *transform(__isl_take isl_printer *p,
  855. struct pet_scop *scop, void *user)
  856. {
  857. struct ppcg_transform_data *data = user;
  858. struct ppcg_scop *ps;
  859. if (print_original(scop, data->options)) {
  860. p = pet_scop_print_original(scop, p);
  861. pet_scop_free(scop);
  862. return p;
  863. }
  864. scop = pet_scop_align_params(scop);
  865. ps = ppcg_scop_from_pet_scop(scop, data->options);
  866. p = data->transform(p, ps, data->user);
  867. ppcg_scop_free(ps);
  868. pet_scop_free(scop);
  869. return p;
  870. }
  871. /* Transform the C source file "input" by rewriting each scop
  872. * through a call to "transform".
  873. * The transformed C code is written to "out".
  874. *
  875. * This is a wrapper around pet_transform_C_source that transforms
  876. * the pet_scop to a ppcg_scop before calling "fn".
  877. */
  878. int ppcg_transform(isl_ctx *ctx, const char *input, FILE *out,
  879. struct ppcg_options *options,
  880. __isl_give isl_printer *(*fn)(__isl_take isl_printer *p,
  881. struct ppcg_scop *scop, void *user), void *user)
  882. {
  883. struct ppcg_transform_data data = { options, fn, user };
  884. return pet_transform_C_source(ctx, input, out, &transform, &data);
  885. }
  886. /* Check consistency of options.
  887. *
  888. * Return -1 on error.
  889. */
  890. static int check_options(isl_ctx *ctx)
  891. {
  892. struct options *options;
  893. options = isl_ctx_peek_options(ctx, &options_args);
  894. if (!options)
  895. isl_die(ctx, isl_error_internal,
  896. "unable to find options", return -1);
  897. if (options->ppcg->openmp &&
  898. !isl_options_get_ast_build_atomic_upper_bound(ctx))
  899. isl_die(ctx, isl_error_invalid,
  900. "OpenMP requires atomic bounds", return -1);
  901. return 0;
  902. }
  903. #if 0
  904. int main(int argc, char **argv)
  905. {
  906. int r;
  907. isl_ctx *ctx;
  908. struct options *options;
  909. options = options_new_with_defaults();
  910. assert(options);
  911. ctx = isl_ctx_alloc_with_options(&options_args, options);
  912. ppcg_options_set_target_defaults(options->ppcg);
  913. isl_options_set_ast_build_detect_min_max(ctx, 1);
  914. isl_options_set_ast_print_macro_once(ctx, 1);
  915. isl_options_set_schedule_whole_component(ctx, 0);
  916. isl_options_set_schedule_maximize_band_depth(ctx, 1);
  917. isl_options_set_schedule_maximize_coincidence(ctx, 1);
  918. pet_options_set_encapsulate_dynamic_control(ctx, 1);
  919. argc = options_parse(options, argc, argv, ISL_ARG_ALL);
  920. if (check_options(ctx) < 0)
  921. r = EXIT_FAILURE;
  922. else if (options->ppcg->target == PPCG_TARGET_CUDA)
  923. r = generate_cuda(ctx, options->ppcg, options->input);
  924. else if (options->ppcg->target == PPCG_TARGET_OPENCL)
  925. r = generate_opencl(ctx, options->ppcg, options->input,
  926. options->output);
  927. else
  928. r = generate_cpu(ctx, options->ppcg, options->input,
  929. options->output);
  930. isl_ctx_free(ctx);
  931. return r;
  932. }
  933. #endif