ppcg_scop: rename dep_external to dep_forced
[ppcg.git] / ppcg.c
blob8d91d57f507885fce4df7523ead0551c6a63e71a
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <assert.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl/ctx.h>
18 #include <isl/flow.h>
19 #include <isl/options.h>
20 #include <isl/schedule.h>
21 #include <isl/ast_build.h>
22 #include <isl/schedule.h>
23 #include <pet.h>
24 #include "ppcg.h"
25 #include "ppcg_options.h"
26 #include "cuda.h"
27 #include "opencl.h"
28 #include "cpu.h"
30 struct options {
31 struct isl_options *isl;
32 struct pet_options *pet;
33 struct ppcg_options *ppcg;
34 char *input;
35 char *output;
38 const char *ppcg_version(void);
39 static void print_version(void)
41 printf("%s", ppcg_version());
44 ISL_ARGS_START(struct options, options_args)
45 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
46 ISL_ARG_CHILD(struct options, pet, "pet", &pet_options_args, "pet options")
47 ISL_ARG_CHILD(struct options, ppcg, NULL, &ppcg_options_args, "ppcg options")
48 ISL_ARG_STR(struct options, output, 'o', NULL,
49 "filename", NULL, "output filename (c and opencl targets)")
50 ISL_ARG_ARG(struct options, input, "input", NULL)
51 ISL_ARG_VERSION(print_version)
52 ISL_ARGS_END
54 ISL_ARG_DEF(options, struct options, options_args)
56 /* Return a pointer to the final path component of "filename" or
57 * to "filename" itself if it does not contain any components.
59 const char *ppcg_base_name(const char *filename)
61 const char *base;
63 base = strrchr(filename, '/');
64 if (base)
65 return ++base;
66 else
67 return filename;
70 /* Copy the base name of "input" to "name" and return its length.
71 * "name" is not NULL terminated.
73 * In particular, remove all leading directory components and
74 * the final extension, if any.
76 int ppcg_extract_base_name(char *name, const char *input)
78 const char *base;
79 const char *ext;
80 int len;
82 base = ppcg_base_name(input);
83 ext = strrchr(base, '.');
84 len = ext ? ext - base : strlen(base);
86 memcpy(name, base, len);
88 return len;
91 /* Collect all variable names that are in use in "scop".
92 * In particular, collect all parameters in the context and
93 * all the array names.
94 * Store these names in an isl_id_to_ast_expr by mapping
95 * them to a dummy value (0).
97 static __isl_give isl_id_to_ast_expr *collect_names(struct pet_scop *scop)
99 int i, n;
100 isl_ctx *ctx;
101 isl_ast_expr *zero;
102 isl_id_to_ast_expr *names;
104 ctx = isl_set_get_ctx(scop->context);
106 n = isl_set_dim(scop->context, isl_dim_param);
108 names = isl_id_to_ast_expr_alloc(ctx, n + scop->n_array);
109 zero = isl_ast_expr_from_val(isl_val_zero(ctx));
111 for (i = 0; i < n; ++i) {
112 isl_id *id;
114 id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
115 names = isl_id_to_ast_expr_set(names,
116 id, isl_ast_expr_copy(zero));
119 for (i = 0; i < scop->n_array; ++i) {
120 struct pet_array *array = scop->arrays[i];
121 isl_id *id;
123 id = isl_set_get_tuple_id(array->extent);
124 names = isl_id_to_ast_expr_set(names,
125 id, isl_ast_expr_copy(zero));
128 isl_ast_expr_free(zero);
130 return names;
133 /* Return an isl_id called "prefix%d", with "%d" set to "i".
134 * If an isl_id with such a name already appears among the variable names
135 * of "scop", then adjust the name to "prefix%d_%d".
137 static __isl_give isl_id *generate_name(struct ppcg_scop *scop,
138 const char *prefix, int i)
140 int j;
141 char name[16];
142 isl_ctx *ctx;
143 isl_id *id;
144 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);
150 j = 0;
151 while ((has_name = isl_id_to_ast_expr_has(scop->names, id)) == 1) {
152 isl_id_free(id);
153 snprintf(name, sizeof(name), "%s%d_%d", prefix, i, j++);
154 id = isl_id_alloc(ctx, name, NULL);
157 return has_name < 0 ? isl_id_free(id) : id;
160 /* Return a list of "n" isl_ids of the form "prefix%d".
161 * If an isl_id with such a name already appears among the variable names
162 * of "scop", then adjust the name to "prefix%d_%d".
164 __isl_give isl_id_list *ppcg_scop_generate_names(struct ppcg_scop *scop,
165 int n, const char *prefix)
167 int i;
168 char name[10];
169 isl_ctx *ctx;
170 isl_id_list *names;
172 ctx = isl_set_get_ctx(scop->context);
173 names = isl_id_list_alloc(ctx, n);
174 for (i = 0; i < n; ++i) {
175 isl_id *id;
177 id = generate_name(scop, prefix, i);
178 names = isl_id_list_add(names, id);
181 return names;
184 /* Is "stmt" not a kill statement?
186 static int is_not_kill(struct pet_stmt *stmt)
188 return !pet_stmt_is_kill(stmt);
191 /* Collect the iteration domains of the statements in "scop" that
192 * satisfy "pred".
194 static __isl_give isl_union_set *collect_domains(struct pet_scop *scop,
195 int (*pred)(struct pet_stmt *stmt))
197 int i;
198 isl_set *domain_i;
199 isl_union_set *domain;
201 if (!scop)
202 return NULL;
204 domain = isl_union_set_empty(isl_set_get_space(scop->context));
206 for (i = 0; i < scop->n_stmt; ++i) {
207 struct pet_stmt *stmt = scop->stmts[i];
209 if (!pred(stmt))
210 continue;
212 if (stmt->n_arg > 0)
213 isl_die(isl_union_set_get_ctx(domain),
214 isl_error_unsupported,
215 "data dependent conditions not supported",
216 return isl_union_set_free(domain));
218 domain_i = isl_set_copy(scop->stmts[i]->domain);
219 domain = isl_union_set_add_set(domain, domain_i);
222 return domain;
225 /* Collect the iteration domains of the statements in "scop",
226 * skipping kill statements.
228 static __isl_give isl_union_set *collect_non_kill_domains(struct pet_scop *scop)
230 return collect_domains(scop, &is_not_kill);
233 /* This function is used as a callback to pet_expr_foreach_call_expr
234 * to detect if there is any call expression in the input expression.
235 * Assign the value 1 to the integer that "user" points to and
236 * abort the search since we have found what we were looking for.
238 static int set_has_call(__isl_keep pet_expr *expr, void *user)
240 int *has_call = user;
242 *has_call = 1;
244 return -1;
247 /* Does "expr" contain any call expressions?
249 static int expr_has_call(__isl_keep pet_expr *expr)
251 int has_call = 0;
253 if (pet_expr_foreach_call_expr(expr, &set_has_call, &has_call) < 0 &&
254 !has_call)
255 return -1;
257 return has_call;
260 /* This function is a callback for pet_tree_foreach_expr.
261 * If "expr" contains any call (sub)expressions, then set *has_call
262 * and abort the search.
264 static int check_call(__isl_keep pet_expr *expr, void *user)
266 int *has_call = user;
268 if (expr_has_call(expr))
269 *has_call = 1;
271 return *has_call ? -1 : 0;
274 /* Does "stmt" contain any call expressions?
276 static int has_call(struct pet_stmt *stmt)
278 int has_call = 0;
280 if (pet_tree_foreach_expr(stmt->body, &check_call, &has_call) < 0 &&
281 !has_call)
282 return -1;
284 return has_call;
287 /* Collect the iteration domains of the statements in "scop"
288 * that contain a call expression.
290 static __isl_give isl_union_set *collect_call_domains(struct pet_scop *scop)
292 return collect_domains(scop, &has_call);
295 /* Given a union of "tagged" access relations of the form
297 * [S_i[...] -> R_j[]] -> A_k[...]
299 * project out the "tags" (R_j[]).
300 * That is, return a union of relations of the form
302 * S_i[...] -> A_k[...]
304 static __isl_give isl_union_map *project_out_tags(
305 __isl_take isl_union_map *umap)
307 return isl_union_map_domain_factor_domain(umap);
310 /* Construct a function from tagged iteration domains to the corresponding
311 * untagged iteration domains with as range of the wrapped map in the domain
312 * the reference tags that appear in any of the reads, writes or kills.
313 * Store the result in ps->tagger.
315 * For example, if the statement with iteration space S[i,j]
316 * contains two array references R_1[] and R_2[], then ps->tagger will contain
318 * { [S[i,j] -> R_1[]] -> S[i,j]; [S[i,j] -> R_2[]] -> S[i,j] }
320 static void compute_tagger(struct ppcg_scop *ps)
322 isl_union_map *tagged;
323 isl_union_pw_multi_aff *tagger;
325 tagged = isl_union_map_copy(ps->tagged_reads);
326 tagged = isl_union_map_union(tagged,
327 isl_union_map_copy(ps->tagged_may_writes));
328 tagged = isl_union_map_union(tagged,
329 isl_union_map_copy(ps->tagged_must_kills));
330 tagged = isl_union_map_universe(tagged);
331 tagged = isl_union_set_unwrap(isl_union_map_domain(tagged));
333 tagger = isl_union_map_domain_map_union_pw_multi_aff(tagged);
335 ps->tagger = tagger;
338 /* Compute the live out accesses, i.e., the writes that are
339 * potentially not killed by any kills or any other writes, and
340 * store them in ps->live_out.
342 * We compute the "dependence" of any "kill" (an explicit kill
343 * or a must write) on any may write.
344 * The may writes with a "depending" kill are definitely killed.
345 * The remaining may writes can potentially be live out.
347 static void compute_live_out(struct ppcg_scop *ps)
349 isl_union_pw_multi_aff *tagger;
350 isl_schedule *schedule;
351 isl_union_map *kills;
352 isl_union_map *exposed;
353 isl_union_map *covering;
354 isl_union_access_info *access;
355 isl_union_flow *flow;
357 tagger = isl_union_pw_multi_aff_copy(ps->tagger);
358 schedule = isl_schedule_copy(ps->schedule);
359 schedule = isl_schedule_pullback_union_pw_multi_aff(schedule, tagger);
360 kills = isl_union_map_union(isl_union_map_copy(ps->tagged_must_writes),
361 isl_union_map_copy(ps->tagged_must_kills));
362 access = isl_union_access_info_from_sink(kills);
363 access = isl_union_access_info_set_may_source(access,
364 isl_union_map_copy(ps->tagged_may_writes));
365 access = isl_union_access_info_set_schedule(access, schedule);
366 flow = isl_union_access_info_compute_flow(access);
367 covering = isl_union_flow_get_may_dependence(flow);
368 isl_union_flow_free(flow);
369 exposed = isl_union_map_copy(ps->tagged_may_writes);
370 exposed = isl_union_map_subtract_domain(exposed,
371 isl_union_map_domain(covering));
372 ps->live_out = project_out_tags(exposed);
375 /* Compute the flow dependences and the live_in accesses and store
376 * the results in ps->dep_flow and ps->live_in.
377 * A copy of the flow dependences, tagged with the reference tags
378 * is stored in ps->tagged_dep_flow.
380 * We first compute ps->tagged_dep_flow, i.e., the tagged flow dependences
381 * and then project out the tags.
383 * We allow both the must writes and the must kills to serve as
384 * definite sources such that a subsequent read would not depend
385 * on any earlier write. The resulting flow dependences with
386 * a must kill as source reflect possibly uninitialized reads.
387 * No dependences need to be introduced to protect such reads
388 * (other than those imposed by potential flows from may writes
389 * that follow the kill). We therefore those flow dependences.
390 * This is also useful for the dead code elimination, which assumes
391 * the flow sources are non-kill instances.
393 static void compute_tagged_flow_dep(struct ppcg_scop *ps)
395 isl_union_pw_multi_aff *tagger;
396 isl_schedule *schedule;
397 isl_union_map *live_in;
398 isl_union_access_info *access;
399 isl_union_flow *flow;
400 isl_union_map *must_source;
401 isl_union_map *kills;
402 isl_union_map *tagged_flow;
404 tagger = isl_union_pw_multi_aff_copy(ps->tagger);
405 schedule = isl_schedule_copy(ps->schedule);
406 schedule = isl_schedule_pullback_union_pw_multi_aff(schedule, tagger);
407 kills = isl_union_map_copy(ps->tagged_must_kills);
408 must_source = isl_union_map_copy(ps->tagged_must_writes);
409 must_source = isl_union_map_union(must_source,
410 isl_union_map_copy(kills));
411 access = isl_union_access_info_from_sink(
412 isl_union_map_copy(ps->tagged_reads));
413 access = isl_union_access_info_set_must_source(access, must_source);
414 access = isl_union_access_info_set_may_source(access,
415 isl_union_map_copy(ps->tagged_may_writes));
416 access = isl_union_access_info_set_schedule(access, schedule);
417 flow = isl_union_access_info_compute_flow(access);
418 tagged_flow = isl_union_flow_get_may_dependence(flow);
419 tagged_flow = isl_union_map_subtract_domain(tagged_flow,
420 isl_union_map_domain(kills));
421 ps->tagged_dep_flow = tagged_flow;
422 ps->dep_flow = isl_union_map_copy(ps->tagged_dep_flow);
423 ps->dep_flow = isl_union_map_factor_domain(ps->dep_flow);
424 live_in = isl_union_flow_get_may_no_source(flow);
425 ps->live_in = project_out_tags(live_in);
426 isl_union_flow_free(flow);
429 /* Compute the order dependences that prevent the potential live ranges
430 * from overlapping.
431 * "before" contains all pairs of statement iterations where
432 * the first is executed before the second according to the original schedule.
434 * In particular, construct a union of relations
436 * [R[...] -> R_1[]] -> [W[...] -> R_2[]]
438 * where [R[...] -> R_1[]] is the range of one or more live ranges
439 * (i.e., a read) and [W[...] -> R_2[]] is the domain of one or more
440 * live ranges (i.e., a write). Moreover, the read and the write
441 * access the same memory element and the read occurs before the write
442 * in the original schedule.
443 * The scheduler allows some of these dependences to be violated, provided
444 * the adjacent live ranges are all local (i.e., their domain and range
445 * are mapped to the same point by the current schedule band).
447 * Note that if a live range is not local, then we need to make
448 * sure it does not overlap with _any_ other live range, and not
449 * just with the "previous" and/or the "next" live range.
450 * We therefore add order dependences between reads and
451 * _any_ later potential write.
453 * We also need to be careful about writes without a corresponding read.
454 * They are already prevented from moving past non-local preceding
455 * intervals, but we also need to prevent them from moving past non-local
456 * following intervals. We therefore also add order dependences from
457 * potential writes that do not appear in any intervals
458 * to all later potential writes.
459 * Note that dead code elimination should have removed most of these
460 * dead writes, but the dead code elimination may not remove all dead writes,
461 * so we need to consider them to be safe.
463 static void compute_order_dependences(struct ppcg_scop *ps,
464 __isl_take isl_union_map *before)
466 isl_union_map *reads;
467 isl_union_map *shared_access;
468 isl_union_set *matched;
469 isl_union_map *unmatched;
470 isl_union_set *domain;
472 reads = isl_union_map_copy(ps->tagged_reads);
473 matched = isl_union_map_domain(isl_union_map_copy(ps->tagged_dep_flow));
474 unmatched = isl_union_map_copy(ps->tagged_may_writes);
475 unmatched = isl_union_map_subtract_domain(unmatched, matched);
476 reads = isl_union_map_union(reads, unmatched);
477 shared_access = isl_union_map_copy(ps->tagged_may_writes);
478 shared_access = isl_union_map_reverse(shared_access);
479 shared_access = isl_union_map_apply_range(reads, shared_access);
480 shared_access = isl_union_map_zip(shared_access);
481 shared_access = isl_union_map_intersect_domain(shared_access,
482 isl_union_map_wrap(before));
483 domain = isl_union_map_domain(isl_union_map_copy(shared_access));
484 shared_access = isl_union_map_zip(shared_access);
485 ps->dep_order = isl_union_set_unwrap(domain);
486 ps->tagged_dep_order = shared_access;
489 /* Compute those validity dependences of the program represented by "scop"
490 * that should be unconditionally enforced even when live-range reordering
491 * is used.
492 * "before" contains all pairs of statement iterations where
493 * the first is executed before the second according to the original schedule.
495 * In particular, compute the external false dependences.
496 * The anti-dependences are already taken care of by the order dependences.
497 * The external false dependences are only used to ensure that live-in and
498 * live-out data is not overwritten by any writes inside the scop.
500 * In particular, the reads from live-in data need to precede any
501 * later write to the same memory element.
502 * As to live-out data, the last writes need to remain the last writes.
503 * That is, any earlier write in the original schedule needs to precede
504 * the last write to the same memory element in the computed schedule.
505 * The possible last writes have been computed by compute_live_out.
506 * They may include kills, but if the last access is a kill,
507 * then the corresponding dependences will effectively be ignored
508 * since we do not schedule any kill statements.
510 * Note that the set of live-in and live-out accesses may be
511 * an overapproximation. There may therefore be potential writes
512 * before a live-in access and after a live-out access.
514 static void compute_forced_dependences(struct ppcg_scop *ps,
515 __isl_take isl_union_map *before)
517 isl_union_map *shared_access;
518 isl_union_map *exposed;
519 isl_union_map *live_in;
521 exposed = isl_union_map_copy(ps->live_out);
523 exposed = isl_union_map_reverse(exposed);
524 shared_access = isl_union_map_copy(ps->may_writes);
525 shared_access = isl_union_map_apply_range(shared_access, exposed);
527 ps->dep_forced = shared_access;
529 live_in = isl_union_map_apply_range(isl_union_map_copy(ps->live_in),
530 isl_union_map_reverse(isl_union_map_copy(ps->may_writes)));
532 ps->dep_forced = isl_union_map_union(ps->dep_forced, live_in);
533 ps->dep_forced = isl_union_map_intersect(ps->dep_forced, before);
536 /* Compute the dependences of the program represented by "scop"
537 * in case live range reordering is allowed.
539 * We compute the actual live ranges and the corresponding order
540 * false dependences.
542 static void compute_live_range_reordering_dependences(struct ppcg_scop *ps)
544 isl_union_map *before;
545 isl_union_map *schedule;
547 schedule = isl_schedule_get_map(ps->schedule);
548 before = isl_union_map_lex_lt_union_map(schedule,
549 isl_union_map_copy(schedule));
551 compute_tagged_flow_dep(ps);
552 compute_order_dependences(ps, isl_union_map_copy(before));
553 compute_forced_dependences(ps, before);
556 /* Compute the potential flow dependences and the potential live in
557 * accesses.
559 static void compute_flow_dep(struct ppcg_scop *ps)
561 isl_union_access_info *access;
562 isl_union_flow *flow;
564 access = isl_union_access_info_from_sink(isl_union_map_copy(ps->reads));
565 access = isl_union_access_info_set_must_source(access,
566 isl_union_map_copy(ps->must_writes));
567 access = isl_union_access_info_set_may_source(access,
568 isl_union_map_copy(ps->may_writes));
569 access = isl_union_access_info_set_schedule(access,
570 isl_schedule_copy(ps->schedule));
571 flow = isl_union_access_info_compute_flow(access);
573 ps->dep_flow = isl_union_flow_get_may_dependence(flow);
574 ps->live_in = isl_union_flow_get_may_no_source(flow);
575 isl_union_flow_free(flow);
578 /* Compute the dependences of the program represented by "scop".
579 * Store the computed potential flow dependences
580 * in scop->dep_flow and the reads with potentially no corresponding writes in
581 * scop->live_in.
582 * Store the potential live out accesses in scop->live_out.
583 * Store the potential false (anti and output) dependences in scop->dep_false.
585 * If live range reordering is allowed, then we compute a separate
586 * set of order dependences and a set of external false dependences
587 * in compute_live_range_reordering_dependences.
589 static void compute_dependences(struct ppcg_scop *scop)
591 isl_union_map *may_source;
592 isl_union_access_info *access;
593 isl_union_flow *flow;
595 if (!scop)
596 return;
598 compute_live_out(scop);
600 if (scop->options->live_range_reordering)
601 compute_live_range_reordering_dependences(scop);
602 else if (scop->options->target != PPCG_TARGET_C)
603 compute_tagged_flow_dep(scop);
604 else
605 compute_flow_dep(scop);
607 may_source = isl_union_map_union(isl_union_map_copy(scop->may_writes),
608 isl_union_map_copy(scop->reads));
609 access = isl_union_access_info_from_sink(
610 isl_union_map_copy(scop->may_writes));
611 access = isl_union_access_info_set_must_source(access,
612 isl_union_map_copy(scop->must_writes));
613 access = isl_union_access_info_set_may_source(access, may_source);
614 access = isl_union_access_info_set_schedule(access,
615 isl_schedule_copy(scop->schedule));
616 flow = isl_union_access_info_compute_flow(access);
618 scop->dep_false = isl_union_flow_get_may_dependence(flow);
619 scop->dep_false = isl_union_map_coalesce(scop->dep_false);
620 isl_union_flow_free(flow);
623 /* Eliminate dead code from ps->domain.
625 * In particular, intersect both ps->domain and the domain of
626 * ps->schedule with the (parts of) iteration
627 * domains that are needed to produce the output or for statement
628 * iterations that call functions.
629 * Also intersect the range of the dataflow dependences with
630 * this domain such that the removed instances will no longer
631 * be considered as targets of dataflow.
633 * We start with the iteration domains that call functions
634 * and the set of iterations that last write to an array
635 * (except those that are later killed).
637 * Then we add those statement iterations that produce
638 * something needed by the "live" statements iterations.
639 * We keep doing this until no more statement iterations can be added.
640 * To ensure that the procedure terminates, we compute the affine
641 * hull of the live iterations (bounded to the original iteration
642 * domains) each time we have added extra iterations.
644 static void eliminate_dead_code(struct ppcg_scop *ps)
646 isl_union_set *live;
647 isl_union_map *dep;
648 isl_union_pw_multi_aff *tagger;
650 live = isl_union_map_domain(isl_union_map_copy(ps->live_out));
651 if (!isl_union_set_is_empty(ps->call)) {
652 live = isl_union_set_union(live, isl_union_set_copy(ps->call));
653 live = isl_union_set_coalesce(live);
656 dep = isl_union_map_copy(ps->dep_flow);
657 dep = isl_union_map_reverse(dep);
659 for (;;) {
660 isl_union_set *extra;
662 extra = isl_union_set_apply(isl_union_set_copy(live),
663 isl_union_map_copy(dep));
664 if (isl_union_set_is_subset(extra, live)) {
665 isl_union_set_free(extra);
666 break;
669 live = isl_union_set_union(live, extra);
670 live = isl_union_set_affine_hull(live);
671 live = isl_union_set_intersect(live,
672 isl_union_set_copy(ps->domain));
675 isl_union_map_free(dep);
677 ps->domain = isl_union_set_intersect(ps->domain,
678 isl_union_set_copy(live));
679 ps->schedule = isl_schedule_intersect_domain(ps->schedule,
680 isl_union_set_copy(live));
681 ps->dep_flow = isl_union_map_intersect_range(ps->dep_flow,
682 isl_union_set_copy(live));
683 tagger = isl_union_pw_multi_aff_copy(ps->tagger);
684 live = isl_union_set_preimage_union_pw_multi_aff(live, tagger);
685 ps->tagged_dep_flow = isl_union_map_intersect_range(ps->tagged_dep_flow,
686 live);
689 /* Intersect "set" with the set described by "str", taking the NULL
690 * string to represent the universal set.
692 static __isl_give isl_set *set_intersect_str(__isl_take isl_set *set,
693 const char *str)
695 isl_ctx *ctx;
696 isl_set *set2;
698 if (!str)
699 return set;
701 ctx = isl_set_get_ctx(set);
702 set2 = isl_set_read_from_str(ctx, str);
703 set = isl_set_intersect(set, set2);
705 return set;
708 static void *ppcg_scop_free(struct ppcg_scop *ps)
710 if (!ps)
711 return NULL;
713 isl_set_free(ps->context);
714 isl_union_set_free(ps->domain);
715 isl_union_set_free(ps->call);
716 isl_union_map_free(ps->tagged_reads);
717 isl_union_map_free(ps->reads);
718 isl_union_map_free(ps->live_in);
719 isl_union_map_free(ps->tagged_may_writes);
720 isl_union_map_free(ps->tagged_must_writes);
721 isl_union_map_free(ps->may_writes);
722 isl_union_map_free(ps->must_writes);
723 isl_union_map_free(ps->live_out);
724 isl_union_map_free(ps->tagged_must_kills);
725 isl_union_map_free(ps->tagged_dep_flow);
726 isl_union_map_free(ps->dep_flow);
727 isl_union_map_free(ps->dep_false);
728 isl_union_map_free(ps->dep_forced);
729 isl_union_map_free(ps->tagged_dep_order);
730 isl_union_map_free(ps->dep_order);
731 isl_schedule_free(ps->schedule);
732 isl_union_pw_multi_aff_free(ps->tagger);
733 isl_union_map_free(ps->independence);
734 isl_id_to_ast_expr_free(ps->names);
736 free(ps);
738 return NULL;
741 /* Extract a ppcg_scop from a pet_scop.
743 * The constructed ppcg_scop refers to elements from the pet_scop
744 * so the pet_scop should not be freed before the ppcg_scop.
746 static struct ppcg_scop *ppcg_scop_from_pet_scop(struct pet_scop *scop,
747 struct ppcg_options *options)
749 int i;
750 isl_ctx *ctx;
751 struct ppcg_scop *ps;
753 if (!scop)
754 return NULL;
756 ctx = isl_set_get_ctx(scop->context);
758 ps = isl_calloc_type(ctx, struct ppcg_scop);
759 if (!ps)
760 return NULL;
762 ps->names = collect_names(scop);
763 ps->options = options;
764 ps->start = pet_loc_get_start(scop->loc);
765 ps->end = pet_loc_get_end(scop->loc);
766 ps->context = isl_set_copy(scop->context);
767 ps->context = set_intersect_str(ps->context, options->ctx);
768 ps->domain = collect_non_kill_domains(scop);
769 ps->call = collect_call_domains(scop);
770 ps->tagged_reads = pet_scop_collect_tagged_may_reads(scop);
771 ps->reads = pet_scop_collect_may_reads(scop);
772 ps->tagged_may_writes = pet_scop_collect_tagged_may_writes(scop);
773 ps->may_writes = pet_scop_collect_may_writes(scop);
774 ps->tagged_must_writes = pet_scop_collect_tagged_must_writes(scop);
775 ps->must_writes = pet_scop_collect_must_writes(scop);
776 ps->tagged_must_kills = pet_scop_collect_tagged_must_kills(scop);
777 ps->schedule = isl_schedule_copy(scop->schedule);
778 ps->pet = scop;
779 ps->independence = isl_union_map_empty(isl_set_get_space(ps->context));
780 for (i = 0; i < scop->n_independence; ++i)
781 ps->independence = isl_union_map_union(ps->independence,
782 isl_union_map_copy(scop->independences[i]->filter));
784 compute_tagger(ps);
785 compute_dependences(ps);
786 eliminate_dead_code(ps);
788 if (!ps->context || !ps->domain || !ps->call || !ps->reads ||
789 !ps->may_writes || !ps->must_writes || !ps->tagged_must_kills ||
790 !ps->schedule || !ps->independence || !ps->names)
791 return ppcg_scop_free(ps);
793 return ps;
796 /* Internal data structure for ppcg_transform.
798 struct ppcg_transform_data {
799 struct ppcg_options *options;
800 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
801 struct ppcg_scop *scop, void *user);
802 void *user;
805 /* Should we print the original code?
806 * That is, does "scop" involve any data dependent conditions or
807 * nested expressions that cannot be handled by pet_stmt_build_ast_exprs?
809 static int print_original(struct pet_scop *scop, struct ppcg_options *options)
811 if (!pet_scop_can_build_ast_exprs(scop)) {
812 if (options->debug->verbose)
813 fprintf(stdout, "Printing original code because "
814 "some index expressions cannot currently "
815 "be printed\n");
816 return 1;
819 if (pet_scop_has_data_dependent_conditions(scop)) {
820 if (options->debug->verbose)
821 fprintf(stdout, "Printing original code because "
822 "input involves data dependent conditions\n");
823 return 1;
826 return 0;
829 /* Callback for pet_transform_C_source that transforms
830 * the given pet_scop to a ppcg_scop before calling the
831 * ppcg_transform callback.
833 * If "scop" contains any data dependent conditions or if we may
834 * not be able to print the transformed program, then just print
835 * the original code.
837 static __isl_give isl_printer *transform(__isl_take isl_printer *p,
838 struct pet_scop *scop, void *user)
840 struct ppcg_transform_data *data = user;
841 struct ppcg_scop *ps;
843 if (print_original(scop, data->options)) {
844 p = pet_scop_print_original(scop, p);
845 pet_scop_free(scop);
846 return p;
849 scop = pet_scop_align_params(scop);
850 ps = ppcg_scop_from_pet_scop(scop, data->options);
852 p = data->transform(p, ps, data->user);
854 ppcg_scop_free(ps);
855 pet_scop_free(scop);
857 return p;
860 /* Transform the C source file "input" by rewriting each scop
861 * through a call to "transform".
862 * The transformed C code is written to "out".
864 * This is a wrapper around pet_transform_C_source that transforms
865 * the pet_scop to a ppcg_scop before calling "fn".
867 int ppcg_transform(isl_ctx *ctx, const char *input, FILE *out,
868 struct ppcg_options *options,
869 __isl_give isl_printer *(*fn)(__isl_take isl_printer *p,
870 struct ppcg_scop *scop, void *user), void *user)
872 struct ppcg_transform_data data = { options, fn, user };
873 return pet_transform_C_source(ctx, input, out, &transform, &data);
876 /* Check consistency of options.
878 * Return -1 on error.
880 static int check_options(isl_ctx *ctx)
882 struct options *options;
884 options = isl_ctx_peek_options(ctx, &options_args);
885 if (!options)
886 isl_die(ctx, isl_error_internal,
887 "unable to find options", return -1);
889 if (options->ppcg->openmp &&
890 !isl_options_get_ast_build_atomic_upper_bound(ctx))
891 isl_die(ctx, isl_error_invalid,
892 "OpenMP requires atomic bounds", return -1);
894 return 0;
897 int main(int argc, char **argv)
899 int r;
900 isl_ctx *ctx;
901 struct options *options;
903 options = options_new_with_defaults();
904 assert(options);
906 ctx = isl_ctx_alloc_with_options(&options_args, options);
907 isl_options_set_schedule_outer_coincidence(ctx, 1);
908 isl_options_set_schedule_maximize_band_depth(ctx, 1);
909 pet_options_set_encapsulate_dynamic_control(ctx, 1);
910 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
912 if (check_options(ctx) < 0)
913 r = EXIT_FAILURE;
914 else if (options->ppcg->target == PPCG_TARGET_CUDA)
915 r = generate_cuda(ctx, options->ppcg, options->input);
916 else if (options->ppcg->target == PPCG_TARGET_OPENCL)
917 r = generate_opencl(ctx, options->ppcg, options->input,
918 options->output);
919 else
920 r = generate_cpu(ctx, options->ppcg, options->input,
921 options->output);
923 isl_ctx_free(ctx);
925 return r;