add --assume-non-negative-parameters option
[ppcg.git] / ppcg.c
blob2f69b61edfe0dbbb28e552c8e5844cc66832b4e3
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2013 Ecole Normale Superieure
4 * Copyright 2015 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
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
14 #include <assert.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <isl/ctx.h>
19 #include <isl/flow.h>
20 #include <isl/options.h>
21 #include <isl/schedule.h>
22 #include <isl/ast_build.h>
23 #include <isl/schedule.h>
24 #include <pet.h>
25 #include "ppcg.h"
26 #include "ppcg_options.h"
27 #include "cuda.h"
28 #include "opencl.h"
29 #include "cpu.h"
31 struct options {
32 struct isl_options *isl;
33 struct pet_options *pet;
34 struct ppcg_options *ppcg;
35 char *input;
36 char *output;
39 const char *ppcg_version(void);
40 static void print_version(void)
42 printf("%s", ppcg_version());
45 ISL_ARGS_START(struct options, options_args)
46 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
47 ISL_ARG_CHILD(struct options, pet, "pet", &pet_options_args, "pet options")
48 ISL_ARG_CHILD(struct options, ppcg, NULL, &ppcg_options_args, "ppcg options")
49 ISL_ARG_STR(struct options, output, 'o', NULL,
50 "filename", NULL, "output filename (c and opencl targets)")
51 ISL_ARG_ARG(struct options, input, "input", NULL)
52 ISL_ARG_VERSION(print_version)
53 ISL_ARGS_END
55 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.
60 const char *ppcg_base_name(const char *filename)
62 const char *base;
64 base = strrchr(filename, '/');
65 if (base)
66 return ++base;
67 else
68 return filename;
71 /* Copy the base name of "input" to "name" and return its length.
72 * "name" is not NULL terminated.
74 * In particular, remove all leading directory components and
75 * the final extension, if any.
77 int ppcg_extract_base_name(char *name, const char *input)
79 const char *base;
80 const char *ext;
81 int len;
83 base = ppcg_base_name(input);
84 ext = strrchr(base, '.');
85 len = ext ? ext - base : strlen(base);
87 memcpy(name, base, len);
89 return len;
92 /* Does "scop" refer to any arrays that are declared, but not
93 * exposed to the code after the scop?
95 int ppcg_scop_any_hidden_declarations(struct ppcg_scop *scop)
97 int i;
99 if (!scop)
100 return 0;
102 for (i = 0; i < scop->pet->n_array; ++i)
103 if (scop->pet->arrays[i]->declared &&
104 !scop->pet->arrays[i]->exposed)
105 return 1;
107 return 0;
110 /* Collect all variable names that are in use in "scop".
111 * In particular, collect all parameters in the context and
112 * all the array names.
113 * Store these names in an isl_id_to_ast_expr by mapping
114 * them to a dummy value (0).
116 static __isl_give isl_id_to_ast_expr *collect_names(struct pet_scop *scop)
118 int i, n;
119 isl_ctx *ctx;
120 isl_ast_expr *zero;
121 isl_id_to_ast_expr *names;
123 ctx = isl_set_get_ctx(scop->context);
125 n = isl_set_dim(scop->context, isl_dim_param);
127 names = isl_id_to_ast_expr_alloc(ctx, n + scop->n_array);
128 zero = isl_ast_expr_from_val(isl_val_zero(ctx));
130 for (i = 0; i < n; ++i) {
131 isl_id *id;
133 id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
134 names = isl_id_to_ast_expr_set(names,
135 id, isl_ast_expr_copy(zero));
138 for (i = 0; i < scop->n_array; ++i) {
139 struct pet_array *array = scop->arrays[i];
140 isl_id *id;
142 id = isl_set_get_tuple_id(array->extent);
143 names = isl_id_to_ast_expr_set(names,
144 id, isl_ast_expr_copy(zero));
147 isl_ast_expr_free(zero);
149 return names;
152 /* Return an isl_id called "prefix%d", with "%d" set to "i".
153 * If an isl_id with such a name already appears among the variable names
154 * of "scop", then adjust the name to "prefix%d_%d".
156 static __isl_give isl_id *generate_name(struct ppcg_scop *scop,
157 const char *prefix, int i)
159 int j;
160 char name[16];
161 isl_ctx *ctx;
162 isl_id *id;
163 int has_name;
165 ctx = isl_set_get_ctx(scop->context);
166 snprintf(name, sizeof(name), "%s%d", prefix, i);
167 id = isl_id_alloc(ctx, name, NULL);
169 j = 0;
170 while ((has_name = isl_id_to_ast_expr_has(scop->names, id)) == 1) {
171 isl_id_free(id);
172 snprintf(name, sizeof(name), "%s%d_%d", prefix, i, j++);
173 id = isl_id_alloc(ctx, name, NULL);
176 return has_name < 0 ? isl_id_free(id) : id;
179 /* Return a list of "n" isl_ids of the form "prefix%d".
180 * If an isl_id with such a name already appears among the variable names
181 * of "scop", then adjust the name to "prefix%d_%d".
183 __isl_give isl_id_list *ppcg_scop_generate_names(struct ppcg_scop *scop,
184 int n, const char *prefix)
186 int i;
187 char name[10];
188 isl_ctx *ctx;
189 isl_id_list *names;
191 ctx = isl_set_get_ctx(scop->context);
192 names = isl_id_list_alloc(ctx, n);
193 for (i = 0; i < n; ++i) {
194 isl_id *id;
196 id = generate_name(scop, prefix, i);
197 names = isl_id_list_add(names, id);
200 return names;
203 /* Is "stmt" not a kill statement?
205 static int is_not_kill(struct pet_stmt *stmt)
207 return !pet_stmt_is_kill(stmt);
210 /* Collect the iteration domains of the statements in "scop" that
211 * satisfy "pred".
213 static __isl_give isl_union_set *collect_domains(struct pet_scop *scop,
214 int (*pred)(struct pet_stmt *stmt))
216 int i;
217 isl_set *domain_i;
218 isl_union_set *domain;
220 if (!scop)
221 return NULL;
223 domain = isl_union_set_empty(isl_set_get_space(scop->context));
225 for (i = 0; i < scop->n_stmt; ++i) {
226 struct pet_stmt *stmt = scop->stmts[i];
228 if (!pred(stmt))
229 continue;
231 if (stmt->n_arg > 0)
232 isl_die(isl_union_set_get_ctx(domain),
233 isl_error_unsupported,
234 "data dependent conditions not supported",
235 return isl_union_set_free(domain));
237 domain_i = isl_set_copy(scop->stmts[i]->domain);
238 domain = isl_union_set_add_set(domain, domain_i);
241 return domain;
244 /* Collect the iteration domains of the statements in "scop",
245 * skipping kill statements.
247 static __isl_give isl_union_set *collect_non_kill_domains(struct pet_scop *scop)
249 return collect_domains(scop, &is_not_kill);
252 /* This function is used as a callback to pet_expr_foreach_call_expr
253 * to detect if there is any call expression in the input expression.
254 * Assign the value 1 to the integer that "user" points to and
255 * abort the search since we have found what we were looking for.
257 static int set_has_call(__isl_keep pet_expr *expr, void *user)
259 int *has_call = user;
261 *has_call = 1;
263 return -1;
266 /* Does "expr" contain any call expressions?
268 static int expr_has_call(__isl_keep pet_expr *expr)
270 int has_call = 0;
272 if (pet_expr_foreach_call_expr(expr, &set_has_call, &has_call) < 0 &&
273 !has_call)
274 return -1;
276 return has_call;
279 /* This function is a callback for pet_tree_foreach_expr.
280 * If "expr" contains any call (sub)expressions, then set *has_call
281 * and abort the search.
283 static int check_call(__isl_keep pet_expr *expr, void *user)
285 int *has_call = user;
287 if (expr_has_call(expr))
288 *has_call = 1;
290 return *has_call ? -1 : 0;
293 /* Does "stmt" contain any call expressions?
295 static int has_call(struct pet_stmt *stmt)
297 int has_call = 0;
299 if (pet_tree_foreach_expr(stmt->body, &check_call, &has_call) < 0 &&
300 !has_call)
301 return -1;
303 return has_call;
306 /* Collect the iteration domains of the statements in "scop"
307 * that contain a call expression.
309 static __isl_give isl_union_set *collect_call_domains(struct pet_scop *scop)
311 return collect_domains(scop, &has_call);
314 /* Given a union of "tagged" access relations of the form
316 * [S_i[...] -> R_j[]] -> A_k[...]
318 * project out the "tags" (R_j[]).
319 * That is, return a union of relations of the form
321 * S_i[...] -> A_k[...]
323 static __isl_give isl_union_map *project_out_tags(
324 __isl_take isl_union_map *umap)
326 return isl_union_map_domain_factor_domain(umap);
329 /* Construct a function from tagged iteration domains to the corresponding
330 * untagged iteration domains with as range of the wrapped map in the domain
331 * the reference tags that appear in any of the reads, writes or kills.
332 * Store the result in ps->tagger.
334 * For example, if the statement with iteration space S[i,j]
335 * contains two array references R_1[] and R_2[], then ps->tagger will contain
337 * { [S[i,j] -> R_1[]] -> S[i,j]; [S[i,j] -> R_2[]] -> S[i,j] }
339 static void compute_tagger(struct ppcg_scop *ps)
341 isl_union_map *tagged;
342 isl_union_pw_multi_aff *tagger;
344 tagged = isl_union_map_copy(ps->tagged_reads);
345 tagged = isl_union_map_union(tagged,
346 isl_union_map_copy(ps->tagged_may_writes));
347 tagged = isl_union_map_union(tagged,
348 isl_union_map_copy(ps->tagged_must_kills));
349 tagged = isl_union_map_universe(tagged);
350 tagged = isl_union_set_unwrap(isl_union_map_domain(tagged));
352 tagger = isl_union_map_domain_map_union_pw_multi_aff(tagged);
354 ps->tagger = tagger;
357 /* Compute the live out accesses, i.e., the writes that are
358 * potentially not killed by any kills or any other writes, and
359 * store them in ps->live_out.
361 * We compute the "dependence" of any "kill" (an explicit kill
362 * or a must write) on any may write.
363 * The may writes with a "depending" kill are definitely killed.
364 * The remaining may writes can potentially be live out.
366 static void compute_live_out(struct ppcg_scop *ps)
368 isl_union_pw_multi_aff *tagger;
369 isl_schedule *schedule;
370 isl_union_map *kills;
371 isl_union_map *exposed;
372 isl_union_map *covering;
373 isl_union_access_info *access;
374 isl_union_flow *flow;
376 tagger = isl_union_pw_multi_aff_copy(ps->tagger);
377 schedule = isl_schedule_copy(ps->schedule);
378 schedule = isl_schedule_pullback_union_pw_multi_aff(schedule, tagger);
379 kills = isl_union_map_union(isl_union_map_copy(ps->tagged_must_writes),
380 isl_union_map_copy(ps->tagged_must_kills));
381 access = isl_union_access_info_from_sink(kills);
382 access = isl_union_access_info_set_may_source(access,
383 isl_union_map_copy(ps->tagged_may_writes));
384 access = isl_union_access_info_set_schedule(access, schedule);
385 flow = isl_union_access_info_compute_flow(access);
386 covering = isl_union_flow_get_may_dependence(flow);
387 isl_union_flow_free(flow);
388 exposed = isl_union_map_copy(ps->tagged_may_writes);
389 exposed = isl_union_map_subtract_domain(exposed,
390 isl_union_map_domain(covering));
391 ps->live_out = project_out_tags(exposed);
394 /* Compute the flow dependences and the live_in accesses and store
395 * the results in ps->dep_flow and ps->live_in.
396 * A copy of the flow dependences, tagged with the reference tags
397 * is stored in ps->tagged_dep_flow.
399 * We first compute ps->tagged_dep_flow, i.e., the tagged flow dependences
400 * and then project out the tags.
402 * We allow both the must writes and the must kills to serve as
403 * definite sources such that a subsequent read would not depend
404 * on any earlier write. The resulting flow dependences with
405 * a must kill as source reflect possibly uninitialized reads.
406 * No dependences need to be introduced to protect such reads
407 * (other than those imposed by potential flows from may writes
408 * that follow the kill). We therefore those flow dependences.
409 * This is also useful for the dead code elimination, which assumes
410 * the flow sources are non-kill instances.
412 static void compute_tagged_flow_dep(struct ppcg_scop *ps)
414 isl_union_pw_multi_aff *tagger;
415 isl_schedule *schedule;
416 isl_union_map *live_in;
417 isl_union_access_info *access;
418 isl_union_flow *flow;
419 isl_union_map *must_source;
420 isl_union_map *kills;
421 isl_union_map *tagged_flow;
423 tagger = isl_union_pw_multi_aff_copy(ps->tagger);
424 schedule = isl_schedule_copy(ps->schedule);
425 schedule = isl_schedule_pullback_union_pw_multi_aff(schedule, tagger);
426 kills = isl_union_map_copy(ps->tagged_must_kills);
427 must_source = isl_union_map_copy(ps->tagged_must_writes);
428 must_source = isl_union_map_union(must_source,
429 isl_union_map_copy(kills));
430 access = isl_union_access_info_from_sink(
431 isl_union_map_copy(ps->tagged_reads));
432 access = isl_union_access_info_set_must_source(access, must_source);
433 access = isl_union_access_info_set_may_source(access,
434 isl_union_map_copy(ps->tagged_may_writes));
435 access = isl_union_access_info_set_schedule(access, schedule);
436 flow = isl_union_access_info_compute_flow(access);
437 tagged_flow = isl_union_flow_get_may_dependence(flow);
438 tagged_flow = isl_union_map_subtract_domain(tagged_flow,
439 isl_union_map_domain(kills));
440 ps->tagged_dep_flow = tagged_flow;
441 ps->dep_flow = isl_union_map_copy(ps->tagged_dep_flow);
442 ps->dep_flow = isl_union_map_factor_domain(ps->dep_flow);
443 live_in = isl_union_flow_get_may_no_source(flow);
444 ps->live_in = project_out_tags(live_in);
445 isl_union_flow_free(flow);
448 /* Compute the order dependences that prevent the potential live ranges
449 * from overlapping.
450 * "before" contains all pairs of statement iterations where
451 * the first is executed before the second according to the original schedule.
453 * In particular, construct a union of relations
455 * [R[...] -> R_1[]] -> [W[...] -> R_2[]]
457 * where [R[...] -> R_1[]] is the range of one or more live ranges
458 * (i.e., a read) and [W[...] -> R_2[]] is the domain of one or more
459 * live ranges (i.e., a write). Moreover, the read and the write
460 * access the same memory element and the read occurs before the write
461 * in the original schedule.
462 * The scheduler allows some of these dependences to be violated, provided
463 * the adjacent live ranges are all local (i.e., their domain and range
464 * are mapped to the same point by the current schedule band).
466 * Note that if a live range is not local, then we need to make
467 * sure it does not overlap with _any_ other live range, and not
468 * just with the "previous" and/or the "next" live range.
469 * We therefore add order dependences between reads and
470 * _any_ later potential write.
472 * We also need to be careful about writes without a corresponding read.
473 * They are already prevented from moving past non-local preceding
474 * intervals, but we also need to prevent them from moving past non-local
475 * following intervals. We therefore also add order dependences from
476 * potential writes that do not appear in any intervals
477 * to all later potential writes.
478 * Note that dead code elimination should have removed most of these
479 * dead writes, but the dead code elimination may not remove all dead writes,
480 * so we need to consider them to be safe.
482 static void compute_order_dependences(struct ppcg_scop *ps,
483 __isl_take isl_union_map *before)
485 isl_union_map *reads;
486 isl_union_map *shared_access;
487 isl_union_set *matched;
488 isl_union_map *unmatched;
489 isl_union_set *domain;
491 reads = isl_union_map_copy(ps->tagged_reads);
492 matched = isl_union_map_domain(isl_union_map_copy(ps->tagged_dep_flow));
493 unmatched = isl_union_map_copy(ps->tagged_may_writes);
494 unmatched = isl_union_map_subtract_domain(unmatched, matched);
495 reads = isl_union_map_union(reads, unmatched);
496 shared_access = isl_union_map_copy(ps->tagged_may_writes);
497 shared_access = isl_union_map_reverse(shared_access);
498 shared_access = isl_union_map_apply_range(reads, shared_access);
499 shared_access = isl_union_map_zip(shared_access);
500 shared_access = isl_union_map_intersect_domain(shared_access,
501 isl_union_map_wrap(before));
502 domain = isl_union_map_domain(isl_union_map_copy(shared_access));
503 shared_access = isl_union_map_zip(shared_access);
504 ps->dep_order = isl_union_set_unwrap(domain);
505 ps->tagged_dep_order = shared_access;
508 /* Compute those validity dependences of the program represented by "scop"
509 * that should be unconditionally enforced even when live-range reordering
510 * is used.
511 * "before" contains all pairs of statement iterations where
512 * the first is executed before the second according to the original schedule.
514 * In particular, compute the external false dependences
515 * as well as order dependences between sources with the same sink.
516 * The anti-dependences are already taken care of by the order dependences.
517 * The external false dependences are only used to ensure that live-in and
518 * live-out data is not overwritten by any writes inside the scop.
520 * In particular, the reads from live-in data need to precede any
521 * later write to the same memory element.
522 * As to live-out data, the last writes need to remain the last writes.
523 * That is, any earlier write in the original schedule needs to precede
524 * the last write to the same memory element in the computed schedule.
525 * The possible last writes have been computed by compute_live_out.
526 * They may include kills, but if the last access is a kill,
527 * then the corresponding dependences will effectively be ignored
528 * since we do not schedule any kill statements.
530 * Note that the set of live-in and live-out accesses may be
531 * an overapproximation. There may therefore be potential writes
532 * before a live-in access and after a live-out access.
534 * In the presence of may-writes, there may be multiple live-ranges
535 * with the same sink, accessing the same memory element.
536 * The sources of these live-ranges need to be executed
537 * in the same relative order as in the original program
538 * since we do not know which of the may-writes will actually
539 * perform a write. Consider all sources that share a sink and
540 * that may write to the same memory element and compute
541 * the order dependences among them.
543 static void compute_forced_dependences(struct ppcg_scop *ps,
544 __isl_take isl_union_map *before)
546 isl_union_map *shared_access;
547 isl_union_map *exposed;
548 isl_union_map *live_in;
549 isl_union_map *sink_access;
550 isl_union_map *shared_sink;
551 isl_union_access_info *access;
552 isl_union_flow *flow;
553 isl_schedule *schedule;
555 exposed = isl_union_map_copy(ps->live_out);
557 exposed = isl_union_map_reverse(exposed);
558 shared_access = isl_union_map_copy(ps->may_writes);
559 shared_access = isl_union_map_apply_range(shared_access, exposed);
561 ps->dep_forced = shared_access;
563 live_in = isl_union_map_apply_range(isl_union_map_copy(ps->live_in),
564 isl_union_map_reverse(isl_union_map_copy(ps->may_writes)));
566 ps->dep_forced = isl_union_map_union(ps->dep_forced, live_in);
567 ps->dep_forced = isl_union_map_intersect(ps->dep_forced, before);
569 schedule = isl_schedule_copy(ps->schedule);
570 sink_access = isl_union_map_copy(ps->tagged_dep_flow);
571 sink_access = isl_union_map_range_product(sink_access,
572 isl_union_map_copy(ps->tagged_may_writes));
573 sink_access = isl_union_map_domain_factor_domain(sink_access);
574 access = isl_union_access_info_from_sink(
575 isl_union_map_copy(sink_access));
576 access = isl_union_access_info_set_may_source(access, sink_access);
577 access = isl_union_access_info_set_schedule(access, schedule);
578 flow = isl_union_access_info_compute_flow(access);
579 shared_sink = isl_union_flow_get_may_dependence(flow);
580 isl_union_flow_free(flow);
581 ps->dep_forced = isl_union_map_union(ps->dep_forced, shared_sink);
584 /* Compute the dependences of the program represented by "scop"
585 * in case live range reordering is allowed.
587 * We compute the actual live ranges and the corresponding order
588 * false dependences.
590 static void compute_live_range_reordering_dependences(struct ppcg_scop *ps)
592 isl_union_map *before;
593 isl_union_map *schedule;
595 schedule = isl_schedule_get_map(ps->schedule);
596 before = isl_union_map_lex_lt_union_map(schedule,
597 isl_union_map_copy(schedule));
599 compute_tagged_flow_dep(ps);
600 compute_order_dependences(ps, isl_union_map_copy(before));
601 compute_forced_dependences(ps, before);
604 /* Compute the potential flow dependences and the potential live in
605 * accesses.
607 static void compute_flow_dep(struct ppcg_scop *ps)
609 isl_union_access_info *access;
610 isl_union_flow *flow;
612 access = isl_union_access_info_from_sink(isl_union_map_copy(ps->reads));
613 access = isl_union_access_info_set_must_source(access,
614 isl_union_map_copy(ps->must_writes));
615 access = isl_union_access_info_set_may_source(access,
616 isl_union_map_copy(ps->may_writes));
617 access = isl_union_access_info_set_schedule(access,
618 isl_schedule_copy(ps->schedule));
619 flow = isl_union_access_info_compute_flow(access);
621 ps->dep_flow = isl_union_flow_get_may_dependence(flow);
622 ps->live_in = isl_union_flow_get_may_no_source(flow);
623 isl_union_flow_free(flow);
626 /* Compute the dependences of the program represented by "scop".
627 * Store the computed potential flow dependences
628 * in scop->dep_flow and the reads with potentially no corresponding writes in
629 * scop->live_in.
630 * Store the potential live out accesses in scop->live_out.
631 * Store the potential false (anti and output) dependences in scop->dep_false.
633 * If live range reordering is allowed, then we compute a separate
634 * set of order dependences and a set of external false dependences
635 * in compute_live_range_reordering_dependences.
637 static void compute_dependences(struct ppcg_scop *scop)
639 isl_union_map *may_source;
640 isl_union_access_info *access;
641 isl_union_flow *flow;
643 if (!scop)
644 return;
646 compute_live_out(scop);
648 if (scop->options->live_range_reordering)
649 compute_live_range_reordering_dependences(scop);
650 else if (scop->options->target != PPCG_TARGET_C)
651 compute_tagged_flow_dep(scop);
652 else
653 compute_flow_dep(scop);
655 may_source = isl_union_map_union(isl_union_map_copy(scop->may_writes),
656 isl_union_map_copy(scop->reads));
657 access = isl_union_access_info_from_sink(
658 isl_union_map_copy(scop->may_writes));
659 access = isl_union_access_info_set_must_source(access,
660 isl_union_map_copy(scop->must_writes));
661 access = isl_union_access_info_set_may_source(access, may_source);
662 access = isl_union_access_info_set_schedule(access,
663 isl_schedule_copy(scop->schedule));
664 flow = isl_union_access_info_compute_flow(access);
666 scop->dep_false = isl_union_flow_get_may_dependence(flow);
667 scop->dep_false = isl_union_map_coalesce(scop->dep_false);
668 isl_union_flow_free(flow);
671 /* Eliminate dead code from ps->domain.
673 * In particular, intersect both ps->domain and the domain of
674 * ps->schedule with the (parts of) iteration
675 * domains that are needed to produce the output or for statement
676 * iterations that call functions.
677 * Also intersect the range of the dataflow dependences with
678 * this domain such that the removed instances will no longer
679 * be considered as targets of dataflow.
681 * We start with the iteration domains that call functions
682 * and the set of iterations that last write to an array
683 * (except those that are later killed).
685 * Then we add those statement iterations that produce
686 * something needed by the "live" statements iterations.
687 * We keep doing this until no more statement iterations can be added.
688 * To ensure that the procedure terminates, we compute the affine
689 * hull of the live iterations (bounded to the original iteration
690 * domains) each time we have added extra iterations.
692 static void eliminate_dead_code(struct ppcg_scop *ps)
694 isl_union_set *live;
695 isl_union_map *dep;
696 isl_union_pw_multi_aff *tagger;
698 live = isl_union_map_domain(isl_union_map_copy(ps->live_out));
699 if (!isl_union_set_is_empty(ps->call)) {
700 live = isl_union_set_union(live, isl_union_set_copy(ps->call));
701 live = isl_union_set_coalesce(live);
704 dep = isl_union_map_copy(ps->dep_flow);
705 dep = isl_union_map_reverse(dep);
707 for (;;) {
708 isl_union_set *extra;
710 extra = isl_union_set_apply(isl_union_set_copy(live),
711 isl_union_map_copy(dep));
712 if (isl_union_set_is_subset(extra, live)) {
713 isl_union_set_free(extra);
714 break;
717 live = isl_union_set_union(live, extra);
718 live = isl_union_set_affine_hull(live);
719 live = isl_union_set_intersect(live,
720 isl_union_set_copy(ps->domain));
723 isl_union_map_free(dep);
725 ps->domain = isl_union_set_intersect(ps->domain,
726 isl_union_set_copy(live));
727 ps->schedule = isl_schedule_intersect_domain(ps->schedule,
728 isl_union_set_copy(live));
729 ps->dep_flow = isl_union_map_intersect_range(ps->dep_flow,
730 isl_union_set_copy(live));
731 tagger = isl_union_pw_multi_aff_copy(ps->tagger);
732 live = isl_union_set_preimage_union_pw_multi_aff(live, tagger);
733 ps->tagged_dep_flow = isl_union_map_intersect_range(ps->tagged_dep_flow,
734 live);
737 /* Intersect "set" with the set described by "str", taking the NULL
738 * string to represent the universal set.
740 static __isl_give isl_set *set_intersect_str(__isl_take isl_set *set,
741 const char *str)
743 isl_ctx *ctx;
744 isl_set *set2;
746 if (!str)
747 return set;
749 ctx = isl_set_get_ctx(set);
750 set2 = isl_set_read_from_str(ctx, str);
751 set = isl_set_intersect(set, set2);
753 return set;
756 static void *ppcg_scop_free(struct ppcg_scop *ps)
758 if (!ps)
759 return NULL;
761 isl_set_free(ps->context);
762 isl_union_set_free(ps->domain);
763 isl_union_set_free(ps->call);
764 isl_union_map_free(ps->tagged_reads);
765 isl_union_map_free(ps->reads);
766 isl_union_map_free(ps->live_in);
767 isl_union_map_free(ps->tagged_may_writes);
768 isl_union_map_free(ps->tagged_must_writes);
769 isl_union_map_free(ps->may_writes);
770 isl_union_map_free(ps->must_writes);
771 isl_union_map_free(ps->live_out);
772 isl_union_map_free(ps->tagged_must_kills);
773 isl_union_map_free(ps->tagged_dep_flow);
774 isl_union_map_free(ps->dep_flow);
775 isl_union_map_free(ps->dep_false);
776 isl_union_map_free(ps->dep_forced);
777 isl_union_map_free(ps->tagged_dep_order);
778 isl_union_map_free(ps->dep_order);
779 isl_schedule_free(ps->schedule);
780 isl_union_pw_multi_aff_free(ps->tagger);
781 isl_union_map_free(ps->independence);
782 isl_id_to_ast_expr_free(ps->names);
784 free(ps);
786 return NULL;
789 /* Extract a ppcg_scop from a pet_scop.
791 * The constructed ppcg_scop refers to elements from the pet_scop
792 * so the pet_scop should not be freed before the ppcg_scop.
794 static struct ppcg_scop *ppcg_scop_from_pet_scop(struct pet_scop *scop,
795 struct ppcg_options *options)
797 int i;
798 isl_ctx *ctx;
799 struct ppcg_scop *ps;
801 if (!scop)
802 return NULL;
804 ctx = isl_set_get_ctx(scop->context);
806 ps = isl_calloc_type(ctx, struct ppcg_scop);
807 if (!ps)
808 return NULL;
810 ps->names = collect_names(scop);
811 ps->options = options;
812 ps->start = pet_loc_get_start(scop->loc);
813 ps->end = pet_loc_get_end(scop->loc);
814 ps->context = isl_set_copy(scop->context);
815 ps->context = set_intersect_str(ps->context, options->ctx);
816 if (options->non_negative_parameters) {
817 isl_space *space = isl_set_get_space(ps->context);
818 isl_set *nn = isl_set_nat_universe(space);
819 ps->context = isl_set_intersect(ps->context, nn);
821 ps->domain = collect_non_kill_domains(scop);
822 ps->call = collect_call_domains(scop);
823 ps->tagged_reads = pet_scop_collect_tagged_may_reads(scop);
824 ps->reads = pet_scop_collect_may_reads(scop);
825 ps->tagged_may_writes = pet_scop_collect_tagged_may_writes(scop);
826 ps->may_writes = pet_scop_collect_may_writes(scop);
827 ps->tagged_must_writes = pet_scop_collect_tagged_must_writes(scop);
828 ps->must_writes = pet_scop_collect_must_writes(scop);
829 ps->tagged_must_kills = pet_scop_collect_tagged_must_kills(scop);
830 ps->schedule = isl_schedule_copy(scop->schedule);
831 ps->pet = scop;
832 ps->independence = isl_union_map_empty(isl_set_get_space(ps->context));
833 for (i = 0; i < scop->n_independence; ++i)
834 ps->independence = isl_union_map_union(ps->independence,
835 isl_union_map_copy(scop->independences[i]->filter));
837 compute_tagger(ps);
838 compute_dependences(ps);
839 eliminate_dead_code(ps);
841 if (!ps->context || !ps->domain || !ps->call || !ps->reads ||
842 !ps->may_writes || !ps->must_writes || !ps->tagged_must_kills ||
843 !ps->schedule || !ps->independence || !ps->names)
844 return ppcg_scop_free(ps);
846 return ps;
849 /* Internal data structure for ppcg_transform.
851 struct ppcg_transform_data {
852 struct ppcg_options *options;
853 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
854 struct ppcg_scop *scop, void *user);
855 void *user;
858 /* Should we print the original code?
859 * That is, does "scop" involve any data dependent conditions or
860 * nested expressions that cannot be handled by pet_stmt_build_ast_exprs?
862 static int print_original(struct pet_scop *scop, struct ppcg_options *options)
864 if (!pet_scop_can_build_ast_exprs(scop)) {
865 if (options->debug->verbose)
866 fprintf(stdout, "Printing original code because "
867 "some index expressions cannot currently "
868 "be printed\n");
869 return 1;
872 if (pet_scop_has_data_dependent_conditions(scop)) {
873 if (options->debug->verbose)
874 fprintf(stdout, "Printing original code because "
875 "input involves data dependent conditions\n");
876 return 1;
879 return 0;
882 /* Callback for pet_transform_C_source that transforms
883 * the given pet_scop to a ppcg_scop before calling the
884 * ppcg_transform callback.
886 * If "scop" contains any data dependent conditions or if we may
887 * not be able to print the transformed program, then just print
888 * the original code.
890 static __isl_give isl_printer *transform(__isl_take isl_printer *p,
891 struct pet_scop *scop, void *user)
893 struct ppcg_transform_data *data = user;
894 struct ppcg_scop *ps;
896 if (print_original(scop, data->options)) {
897 p = pet_scop_print_original(scop, p);
898 pet_scop_free(scop);
899 return p;
902 scop = pet_scop_align_params(scop);
903 ps = ppcg_scop_from_pet_scop(scop, data->options);
905 p = data->transform(p, ps, data->user);
907 ppcg_scop_free(ps);
908 pet_scop_free(scop);
910 return p;
913 /* Transform the C source file "input" by rewriting each scop
914 * through a call to "transform".
915 * The transformed C code is written to "out".
917 * This is a wrapper around pet_transform_C_source that transforms
918 * the pet_scop to a ppcg_scop before calling "fn".
920 int ppcg_transform(isl_ctx *ctx, const char *input, FILE *out,
921 struct ppcg_options *options,
922 __isl_give isl_printer *(*fn)(__isl_take isl_printer *p,
923 struct ppcg_scop *scop, void *user), void *user)
925 struct ppcg_transform_data data = { options, fn, user };
926 return pet_transform_C_source(ctx, input, out, &transform, &data);
929 /* Check consistency of options.
931 * Return -1 on error.
933 static int check_options(isl_ctx *ctx)
935 struct options *options;
937 options = isl_ctx_peek_options(ctx, &options_args);
938 if (!options)
939 isl_die(ctx, isl_error_internal,
940 "unable to find options", return -1);
942 if (options->ppcg->openmp &&
943 !isl_options_get_ast_build_atomic_upper_bound(ctx))
944 isl_die(ctx, isl_error_invalid,
945 "OpenMP requires atomic bounds", return -1);
947 return 0;
950 int main(int argc, char **argv)
952 int r;
953 isl_ctx *ctx;
954 struct options *options;
956 options = options_new_with_defaults();
957 assert(options);
959 ctx = isl_ctx_alloc_with_options(&options_args, options);
960 isl_options_set_schedule_outer_coincidence(ctx, 1);
961 isl_options_set_schedule_maximize_band_depth(ctx, 1);
962 pet_options_set_encapsulate_dynamic_control(ctx, 1);
963 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
965 if (check_options(ctx) < 0)
966 r = EXIT_FAILURE;
967 else if (options->ppcg->target == PPCG_TARGET_CUDA)
968 r = generate_cuda(ctx, options->ppcg, options->input);
969 else if (options->ppcg->target == PPCG_TARGET_OPENCL)
970 r = generate_opencl(ctx, options->ppcg, options->input,
971 options->output);
972 else
973 r = generate_cpu(ctx, options->ppcg, options->input,
974 options->output);
976 isl_ctx_free(ctx);
978 return r;