gpu.c: extract_array_info: reuse ppcg_size_from_extent
[ppcg.git] / ppcg.c
blob71608a5b716fa297c7911aba5e5023635c5d9c6e
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 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, pet, "pet", &pet_options_args, "pet options")
46 ISL_ARG_CHILD(struct options, ppcg, NULL, &ppcg_options_args, "ppcg options")
47 ISL_ARG_STR(struct options, output, 'o', NULL,
48 "filename", NULL, "output filename (c and opencl targets)")
49 ISL_ARG_ARG(struct options, input, "input", NULL)
50 ISL_ARG_VERSION(print_version)
51 ISL_ARGS_END
53 ISL_ARG_DEF(options, struct options, options_args)
55 /* Return a pointer to the final path component of "filename" or
56 * to "filename" itself if it does not contain any components.
58 const char *ppcg_base_name(const char *filename)
60 const char *base;
62 base = strrchr(filename, '/');
63 if (base)
64 return ++base;
65 else
66 return filename;
69 /* Copy the base name of "input" to "name" and return its length.
70 * "name" is not NULL terminated.
72 * In particular, remove all leading directory components and
73 * the final extension, if any.
75 int ppcg_extract_base_name(char *name, const char *input)
77 const char *base;
78 const char *ext;
79 int len;
81 base = ppcg_base_name(input);
82 ext = strrchr(base, '.');
83 len = ext ? ext - base : strlen(base);
85 memcpy(name, base, len);
87 return len;
90 /* Does "scop" refer to any arrays that are declared, but not
91 * exposed to the code after the scop?
93 int ppcg_scop_any_hidden_declarations(struct ppcg_scop *scop)
95 int i;
97 if (!scop)
98 return 0;
100 for (i = 0; i < scop->pet->n_array; ++i)
101 if (scop->pet->arrays[i]->declared &&
102 !scop->pet->arrays[i]->exposed)
103 return 1;
105 return 0;
108 /* Collect all variable names that are in use in "scop".
109 * In particular, collect all parameters in the context and
110 * all the array names.
111 * Store these names in an isl_id_to_ast_expr by mapping
112 * them to a dummy value (0).
114 static __isl_give isl_id_to_ast_expr *collect_names(struct pet_scop *scop)
116 int i, n;
117 isl_ctx *ctx;
118 isl_ast_expr *zero;
119 isl_id_to_ast_expr *names;
121 ctx = isl_set_get_ctx(scop->context);
123 n = isl_set_dim(scop->context, isl_dim_param);
125 names = isl_id_to_ast_expr_alloc(ctx, n + scop->n_array);
126 zero = isl_ast_expr_from_val(isl_val_zero(ctx));
128 for (i = 0; i < n; ++i) {
129 isl_id *id;
131 id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
132 names = isl_id_to_ast_expr_set(names,
133 id, isl_ast_expr_copy(zero));
136 for (i = 0; i < scop->n_array; ++i) {
137 struct pet_array *array = scop->arrays[i];
138 isl_id *id;
140 id = isl_set_get_tuple_id(array->extent);
141 names = isl_id_to_ast_expr_set(names,
142 id, isl_ast_expr_copy(zero));
145 isl_ast_expr_free(zero);
147 return names;
150 /* Return an isl_id called "prefix%d", with "%d" set to "i".
151 * If an isl_id with such a name already appears among the variable names
152 * of "scop", then adjust the name to "prefix%d_%d".
154 static __isl_give isl_id *generate_name(struct ppcg_scop *scop,
155 const char *prefix, int i)
157 int j;
158 char name[16];
159 isl_ctx *ctx;
160 isl_id *id;
161 int has_name;
163 ctx = isl_set_get_ctx(scop->context);
164 snprintf(name, sizeof(name), "%s%d", prefix, i);
165 id = isl_id_alloc(ctx, name, NULL);
167 j = 0;
168 while ((has_name = isl_id_to_ast_expr_has(scop->names, id)) == 1) {
169 isl_id_free(id);
170 snprintf(name, sizeof(name), "%s%d_%d", prefix, i, j++);
171 id = isl_id_alloc(ctx, name, NULL);
174 return has_name < 0 ? isl_id_free(id) : id;
177 /* Return a list of "n" isl_ids of the form "prefix%d".
178 * If an isl_id with such a name already appears among the variable names
179 * of "scop", then adjust the name to "prefix%d_%d".
181 __isl_give isl_id_list *ppcg_scop_generate_names(struct ppcg_scop *scop,
182 int n, const char *prefix)
184 int i;
185 char name[10];
186 isl_ctx *ctx;
187 isl_id_list *names;
189 ctx = isl_set_get_ctx(scop->context);
190 names = isl_id_list_alloc(ctx, n);
191 for (i = 0; i < n; ++i) {
192 isl_id *id;
194 id = generate_name(scop, prefix, i);
195 names = isl_id_list_add(names, id);
198 return names;
201 /* Is "stmt" not a kill statement?
203 static int is_not_kill(struct pet_stmt *stmt)
205 return !pet_stmt_is_kill(stmt);
208 /* Collect the iteration domains of the statements in "scop" that
209 * satisfy "pred".
211 static __isl_give isl_union_set *collect_domains(struct pet_scop *scop,
212 int (*pred)(struct pet_stmt *stmt))
214 int i;
215 isl_set *domain_i;
216 isl_union_set *domain;
218 if (!scop)
219 return NULL;
221 domain = isl_union_set_empty(isl_set_get_space(scop->context));
223 for (i = 0; i < scop->n_stmt; ++i) {
224 struct pet_stmt *stmt = scop->stmts[i];
226 if (!pred(stmt))
227 continue;
229 if (stmt->n_arg > 0)
230 isl_die(isl_union_set_get_ctx(domain),
231 isl_error_unsupported,
232 "data dependent conditions not supported",
233 return isl_union_set_free(domain));
235 domain_i = isl_set_copy(scop->stmts[i]->domain);
236 domain = isl_union_set_add_set(domain, domain_i);
239 return domain;
242 /* Collect the iteration domains of the statements in "scop",
243 * skipping kill statements.
245 static __isl_give isl_union_set *collect_non_kill_domains(struct pet_scop *scop)
247 return collect_domains(scop, &is_not_kill);
250 /* This function is used as a callback to pet_expr_foreach_call_expr
251 * to detect if there is any call expression in the input expression.
252 * Assign the value 1 to the integer that "user" points to and
253 * abort the search since we have found what we were looking for.
255 static int set_has_call(__isl_keep pet_expr *expr, void *user)
257 int *has_call = user;
259 *has_call = 1;
261 return -1;
264 /* Does "expr" contain any call expressions?
266 static int expr_has_call(__isl_keep pet_expr *expr)
268 int has_call = 0;
270 if (pet_expr_foreach_call_expr(expr, &set_has_call, &has_call) < 0 &&
271 !has_call)
272 return -1;
274 return has_call;
277 /* This function is a callback for pet_tree_foreach_expr.
278 * If "expr" contains any call (sub)expressions, then set *has_call
279 * and abort the search.
281 static int check_call(__isl_keep pet_expr *expr, void *user)
283 int *has_call = user;
285 if (expr_has_call(expr))
286 *has_call = 1;
288 return *has_call ? -1 : 0;
291 /* Does "stmt" contain any call expressions?
293 static int has_call(struct pet_stmt *stmt)
295 int has_call = 0;
297 if (pet_tree_foreach_expr(stmt->body, &check_call, &has_call) < 0 &&
298 !has_call)
299 return -1;
301 return has_call;
304 /* Collect the iteration domains of the statements in "scop"
305 * that contain a call expression.
307 static __isl_give isl_union_set *collect_call_domains(struct pet_scop *scop)
309 return collect_domains(scop, &has_call);
312 /* Given a union of "tagged" access relations of the form
314 * [S_i[...] -> R_j[]] -> A_k[...]
316 * project out the "tags" (R_j[]).
317 * That is, return a union of relations of the form
319 * S_i[...] -> A_k[...]
321 static __isl_give isl_union_map *project_out_tags(
322 __isl_take isl_union_map *umap)
324 return isl_union_map_domain_factor_domain(umap);
327 /* Construct a function from tagged iteration domains to the corresponding
328 * untagged iteration domains with as range of the wrapped map in the domain
329 * the reference tags that appear in any of the reads, writes or kills.
330 * Store the result in ps->tagger.
332 * For example, if the statement with iteration space S[i,j]
333 * contains two array references R_1[] and R_2[], then ps->tagger will contain
335 * { [S[i,j] -> R_1[]] -> S[i,j]; [S[i,j] -> R_2[]] -> S[i,j] }
337 static void compute_tagger(struct ppcg_scop *ps)
339 isl_union_map *tagged;
340 isl_union_pw_multi_aff *tagger;
342 tagged = isl_union_map_copy(ps->tagged_reads);
343 tagged = isl_union_map_union(tagged,
344 isl_union_map_copy(ps->tagged_may_writes));
345 tagged = isl_union_map_union(tagged,
346 isl_union_map_copy(ps->tagged_must_kills));
347 tagged = isl_union_map_universe(tagged);
348 tagged = isl_union_set_unwrap(isl_union_map_domain(tagged));
350 tagger = isl_union_map_domain_map_union_pw_multi_aff(tagged);
352 ps->tagger = tagger;
355 /* Compute the live out accesses, i.e., the writes that are
356 * potentially not killed by any kills or any other writes, and
357 * store them in ps->live_out.
359 * We compute the "dependence" of any "kill" (an explicit kill
360 * or a must write) on any may write.
361 * The elements accessed by the may writes with a "depending" kill
362 * also accessing the element are definitely killed.
363 * The remaining may writes can potentially be live out.
365 * The result of the dependence analysis is
367 * { IW -> [IK -> A] }
369 * with IW the instance of the write statement, IK the instance of kill
370 * statement and A the element that was killed.
371 * The range factor range is
373 * { IW -> A }
375 * containing all such pairs for which there is a kill statement instance,
376 * i.e., all pairs that have been killed.
378 static void compute_live_out(struct ppcg_scop *ps)
380 isl_schedule *schedule;
381 isl_union_map *kills;
382 isl_union_map *exposed;
383 isl_union_map *covering;
384 isl_union_set *accessed;
385 isl_union_access_info *access;
386 isl_union_flow *flow;
388 schedule = isl_schedule_copy(ps->schedule);
389 kills = isl_union_map_union(isl_union_map_copy(ps->must_writes),
390 isl_union_map_copy(ps->must_kills));
391 access = isl_union_access_info_from_sink(kills);
392 access = isl_union_access_info_set_may_source(access,
393 isl_union_map_copy(ps->may_writes));
394 access = isl_union_access_info_set_schedule(access, schedule);
395 flow = isl_union_access_info_compute_flow(access);
396 covering = isl_union_flow_get_full_may_dependence(flow);
397 isl_union_flow_free(flow);
399 covering = isl_union_map_range_factor_range(covering);
400 exposed = isl_union_map_copy(ps->may_writes);
401 exposed = isl_union_map_subtract(exposed, covering);
402 ps->live_out = exposed;
405 /* Compute the tagged flow dependences and the live_in accesses and store
406 * the results in ps->tagged_dep_flow and ps->live_in.
408 * We allow both the must writes and the must kills to serve as
409 * definite sources such that a subsequent read would not depend
410 * on any earlier write. The resulting flow dependences with
411 * a must kill as source reflect possibly uninitialized reads.
412 * No dependences need to be introduced to protect such reads
413 * (other than those imposed by potential flows from may writes
414 * that follow the kill). We therefore remove those flow dependences.
415 * This is also useful for the dead code elimination, which assumes
416 * the flow sources are non-kill instances.
418 static void compute_tagged_flow_dep_only(struct ppcg_scop *ps)
420 isl_union_pw_multi_aff *tagger;
421 isl_schedule *schedule;
422 isl_union_map *live_in;
423 isl_union_access_info *access;
424 isl_union_flow *flow;
425 isl_union_map *must_source;
426 isl_union_map *kills;
427 isl_union_map *tagged_flow;
429 tagger = isl_union_pw_multi_aff_copy(ps->tagger);
430 schedule = isl_schedule_copy(ps->schedule);
431 schedule = isl_schedule_pullback_union_pw_multi_aff(schedule, tagger);
432 kills = isl_union_map_copy(ps->tagged_must_kills);
433 must_source = isl_union_map_copy(ps->tagged_must_writes);
434 must_source = isl_union_map_union(must_source,
435 isl_union_map_copy(kills));
436 access = isl_union_access_info_from_sink(
437 isl_union_map_copy(ps->tagged_reads));
438 access = isl_union_access_info_set_must_source(access, must_source);
439 access = isl_union_access_info_set_may_source(access,
440 isl_union_map_copy(ps->tagged_may_writes));
441 access = isl_union_access_info_set_schedule(access, schedule);
442 flow = isl_union_access_info_compute_flow(access);
443 tagged_flow = isl_union_flow_get_may_dependence(flow);
444 tagged_flow = isl_union_map_subtract_domain(tagged_flow,
445 isl_union_map_domain(kills));
446 ps->tagged_dep_flow = tagged_flow;
447 live_in = isl_union_flow_get_may_no_source(flow);
448 ps->live_in = project_out_tags(live_in);
449 isl_union_flow_free(flow);
452 /* Compute ps->dep_flow from ps->tagged_dep_flow
453 * by projecting out the reference tags.
455 static void derive_flow_dep_from_tagged_flow_dep(struct ppcg_scop *ps)
457 ps->dep_flow = isl_union_map_copy(ps->tagged_dep_flow);
458 ps->dep_flow = isl_union_map_factor_domain(ps->dep_flow);
461 /* Compute the flow dependences and the live_in accesses and store
462 * the results in ps->dep_flow and ps->live_in.
463 * A copy of the flow dependences, tagged with the reference tags
464 * is stored in ps->tagged_dep_flow.
466 * We first compute ps->tagged_dep_flow, i.e., the tagged flow dependences
467 * and then project out the tags.
469 static void compute_tagged_flow_dep(struct ppcg_scop *ps)
471 compute_tagged_flow_dep_only(ps);
472 derive_flow_dep_from_tagged_flow_dep(ps);
475 /* Compute the order dependences that prevent the potential live ranges
476 * from overlapping.
478 * In particular, construct a union of relations
480 * [R[...] -> R_1[]] -> [W[...] -> R_2[]]
482 * where [R[...] -> R_1[]] is the range of one or more live ranges
483 * (i.e., a read) and [W[...] -> R_2[]] is the domain of one or more
484 * live ranges (i.e., a write). Moreover, the read and the write
485 * access the same memory element and the read occurs before the write
486 * in the original schedule.
487 * The scheduler allows some of these dependences to be violated, provided
488 * the adjacent live ranges are all local (i.e., their domain and range
489 * are mapped to the same point by the current schedule band).
491 * Note that if a live range is not local, then we need to make
492 * sure it does not overlap with _any_ other live range, and not
493 * just with the "previous" and/or the "next" live range.
494 * We therefore add order dependences between reads and
495 * _any_ later potential write.
497 * We also need to be careful about writes without a corresponding read.
498 * They are already prevented from moving past non-local preceding
499 * intervals, but we also need to prevent them from moving past non-local
500 * following intervals. We therefore also add order dependences from
501 * potential writes that do not appear in any intervals
502 * to all later potential writes.
503 * Note that dead code elimination should have removed most of these
504 * dead writes, but the dead code elimination may not remove all dead writes,
505 * so we need to consider them to be safe.
507 * The order dependences are computed by computing the "dataflow"
508 * from the above unmatched writes and the reads to the may writes.
509 * The unmatched writes and the reads are treated as may sources
510 * such that they would not kill order dependences from earlier
511 * such writes and reads.
513 static void compute_order_dependences(struct ppcg_scop *ps)
515 isl_union_map *reads;
516 isl_union_map *shared_access;
517 isl_union_set *matched;
518 isl_union_map *unmatched;
519 isl_union_pw_multi_aff *tagger;
520 isl_schedule *schedule;
521 isl_union_access_info *access;
522 isl_union_flow *flow;
524 tagger = isl_union_pw_multi_aff_copy(ps->tagger);
525 schedule = isl_schedule_copy(ps->schedule);
526 schedule = isl_schedule_pullback_union_pw_multi_aff(schedule, tagger);
527 reads = isl_union_map_copy(ps->tagged_reads);
528 matched = isl_union_map_domain(isl_union_map_copy(ps->tagged_dep_flow));
529 unmatched = isl_union_map_copy(ps->tagged_may_writes);
530 unmatched = isl_union_map_subtract_domain(unmatched, matched);
531 reads = isl_union_map_union(reads, unmatched);
532 access = isl_union_access_info_from_sink(
533 isl_union_map_copy(ps->tagged_may_writes));
534 access = isl_union_access_info_set_may_source(access, reads);
535 access = isl_union_access_info_set_schedule(access, schedule);
536 flow = isl_union_access_info_compute_flow(access);
537 shared_access = isl_union_flow_get_may_dependence(flow);
538 isl_union_flow_free(flow);
540 ps->tagged_dep_order = isl_union_map_copy(shared_access);
541 ps->dep_order = isl_union_map_factor_domain(shared_access);
544 /* Compute those validity dependences of the program represented by "scop"
545 * that should be unconditionally enforced even when live-range reordering
546 * is used.
548 * In particular, compute the external false dependences
549 * as well as order dependences between sources with the same sink.
550 * The anti-dependences are already taken care of by the order dependences.
551 * The external false dependences are only used to ensure that live-in and
552 * live-out data is not overwritten by any writes inside the scop.
553 * The independences are removed from the external false dependences,
554 * but not from the order dependences between sources with the same sink.
556 * In particular, the reads from live-in data need to precede any
557 * later write to the same memory element.
558 * As to live-out data, the last writes need to remain the last writes.
559 * That is, any earlier write in the original schedule needs to precede
560 * the last write to the same memory element in the computed schedule.
561 * The possible last writes have been computed by compute_live_out.
562 * They may include kills, but if the last access is a kill,
563 * then the corresponding dependences will effectively be ignored
564 * since we do not schedule any kill statements.
566 * Note that the set of live-in and live-out accesses may be
567 * an overapproximation. There may therefore be potential writes
568 * before a live-in access and after a live-out access.
570 * In the presence of may-writes, there may be multiple live-ranges
571 * with the same sink, accessing the same memory element.
572 * The sources of these live-ranges need to be executed
573 * in the same relative order as in the original program
574 * since we do not know which of the may-writes will actually
575 * perform a write. Consider all sources that share a sink and
576 * that may write to the same memory element and compute
577 * the order dependences among them.
579 static void compute_forced_dependences(struct ppcg_scop *ps)
581 isl_union_map *shared_access;
582 isl_union_map *exposed;
583 isl_union_map *live_in;
584 isl_union_map *sink_access;
585 isl_union_map *shared_sink;
586 isl_union_access_info *access;
587 isl_union_flow *flow;
588 isl_schedule *schedule;
590 exposed = isl_union_map_copy(ps->live_out);
591 schedule = isl_schedule_copy(ps->schedule);
592 access = isl_union_access_info_from_sink(exposed);
593 access = isl_union_access_info_set_may_source(access,
594 isl_union_map_copy(ps->may_writes));
595 access = isl_union_access_info_set_schedule(access, schedule);
596 flow = isl_union_access_info_compute_flow(access);
597 shared_access = isl_union_flow_get_may_dependence(flow);
598 isl_union_flow_free(flow);
599 ps->dep_forced = shared_access;
601 schedule = isl_schedule_copy(ps->schedule);
602 access = isl_union_access_info_from_sink(
603 isl_union_map_copy(ps->may_writes));
604 access = isl_union_access_info_set_may_source(access,
605 isl_union_map_copy(ps->live_in));
606 access = isl_union_access_info_set_schedule(access, schedule);
607 flow = isl_union_access_info_compute_flow(access);
608 live_in = isl_union_flow_get_may_dependence(flow);
609 isl_union_flow_free(flow);
611 ps->dep_forced = isl_union_map_union(ps->dep_forced, live_in);
612 ps->dep_forced = isl_union_map_subtract(ps->dep_forced,
613 isl_union_map_copy(ps->independence));
615 schedule = isl_schedule_copy(ps->schedule);
616 sink_access = isl_union_map_copy(ps->tagged_dep_flow);
617 sink_access = isl_union_map_range_product(sink_access,
618 isl_union_map_copy(ps->tagged_may_writes));
619 sink_access = isl_union_map_domain_factor_domain(sink_access);
620 access = isl_union_access_info_from_sink(
621 isl_union_map_copy(sink_access));
622 access = isl_union_access_info_set_may_source(access, sink_access);
623 access = isl_union_access_info_set_schedule(access, schedule);
624 flow = isl_union_access_info_compute_flow(access);
625 shared_sink = isl_union_flow_get_may_dependence(flow);
626 isl_union_flow_free(flow);
627 ps->dep_forced = isl_union_map_union(ps->dep_forced, shared_sink);
630 /* Remove independence from the tagged flow dependences.
631 * Since the user has guaranteed that source and sink of an independence
632 * can be executed in any order, there cannot be a flow dependence
633 * between them, so they can be removed from the set of flow dependences.
634 * However, if the source of such a flow dependence is a must write,
635 * then it may have killed other potential sources, which would have
636 * to be recovered if we were to remove those flow dependences.
637 * We therefore keep the flow dependences that originate in a must write,
638 * even if it corresponds to a known independence.
640 static void remove_independences_from_tagged_flow(struct ppcg_scop *ps)
642 isl_union_map *tf;
643 isl_union_set *indep;
644 isl_union_set *mw;
646 tf = isl_union_map_copy(ps->tagged_dep_flow);
647 tf = isl_union_map_zip(tf);
648 indep = isl_union_map_wrap(isl_union_map_copy(ps->independence));
649 tf = isl_union_map_intersect_domain(tf, indep);
650 tf = isl_union_map_zip(tf);
651 mw = isl_union_map_domain(isl_union_map_copy(ps->tagged_must_writes));
652 tf = isl_union_map_subtract_domain(tf, mw);
653 ps->tagged_dep_flow = isl_union_map_subtract(ps->tagged_dep_flow, tf);
656 /* Compute the dependences of the program represented by "scop"
657 * in case live range reordering is allowed.
659 * We compute the actual live ranges and the corresponding order
660 * false dependences.
662 * The independences are removed from the flow dependences
663 * (provided the source is not a must-write) as well as
664 * from the external false dependences (by compute_forced_dependences).
666 static void compute_live_range_reordering_dependences(struct ppcg_scop *ps)
668 compute_tagged_flow_dep_only(ps);
669 remove_independences_from_tagged_flow(ps);
670 derive_flow_dep_from_tagged_flow_dep(ps);
671 compute_order_dependences(ps);
672 compute_forced_dependences(ps);
675 /* Compute the potential flow dependences and the potential live in
676 * accesses.
678 static void compute_flow_dep(struct ppcg_scop *ps)
680 isl_union_access_info *access;
681 isl_union_flow *flow;
683 access = isl_union_access_info_from_sink(isl_union_map_copy(ps->reads));
684 access = isl_union_access_info_set_must_source(access,
685 isl_union_map_copy(ps->must_writes));
686 access = isl_union_access_info_set_may_source(access,
687 isl_union_map_copy(ps->may_writes));
688 access = isl_union_access_info_set_schedule(access,
689 isl_schedule_copy(ps->schedule));
690 flow = isl_union_access_info_compute_flow(access);
692 ps->dep_flow = isl_union_flow_get_may_dependence(flow);
693 ps->live_in = isl_union_flow_get_may_no_source(flow);
694 isl_union_flow_free(flow);
697 /* Compute the dependences of the program represented by "scop".
698 * Store the computed potential flow dependences
699 * in scop->dep_flow and the reads with potentially no corresponding writes in
700 * scop->live_in.
701 * Store the potential live out accesses in scop->live_out.
702 * Store the potential false (anti and output) dependences in scop->dep_false.
704 * If live range reordering is allowed, then we compute a separate
705 * set of order dependences and a set of external false dependences
706 * in compute_live_range_reordering_dependences.
708 static void compute_dependences(struct ppcg_scop *scop)
710 isl_union_map *may_source;
711 isl_union_access_info *access;
712 isl_union_flow *flow;
714 if (!scop)
715 return;
717 compute_live_out(scop);
719 if (scop->options->live_range_reordering)
720 compute_live_range_reordering_dependences(scop);
721 else if (scop->options->target != PPCG_TARGET_C)
722 compute_tagged_flow_dep(scop);
723 else
724 compute_flow_dep(scop);
726 may_source = isl_union_map_union(isl_union_map_copy(scop->may_writes),
727 isl_union_map_copy(scop->reads));
728 access = isl_union_access_info_from_sink(
729 isl_union_map_copy(scop->may_writes));
730 access = isl_union_access_info_set_must_source(access,
731 isl_union_map_copy(scop->must_writes));
732 access = isl_union_access_info_set_may_source(access, may_source);
733 access = isl_union_access_info_set_schedule(access,
734 isl_schedule_copy(scop->schedule));
735 flow = isl_union_access_info_compute_flow(access);
737 scop->dep_false = isl_union_flow_get_may_dependence(flow);
738 scop->dep_false = isl_union_map_coalesce(scop->dep_false);
739 isl_union_flow_free(flow);
742 /* Eliminate dead code from ps->domain.
744 * In particular, intersect both ps->domain and the domain of
745 * ps->schedule with the (parts of) iteration
746 * domains that are needed to produce the output or for statement
747 * iterations that call functions.
748 * Also intersect the range of the dataflow dependences with
749 * this domain such that the removed instances will no longer
750 * be considered as targets of dataflow.
752 * We start with the iteration domains that call functions
753 * and the set of iterations that last write to an array
754 * (except those that are later killed).
756 * Then we add those statement iterations that produce
757 * something needed by the "live" statements iterations.
758 * We keep doing this until no more statement iterations can be added.
759 * To ensure that the procedure terminates, we compute the affine
760 * hull of the live iterations (bounded to the original iteration
761 * domains) each time we have added extra iterations.
763 static void eliminate_dead_code(struct ppcg_scop *ps)
765 isl_union_set *live;
766 isl_union_map *dep;
767 isl_union_pw_multi_aff *tagger;
769 live = isl_union_map_domain(isl_union_map_copy(ps->live_out));
770 if (!isl_union_set_is_empty(ps->call)) {
771 live = isl_union_set_union(live, isl_union_set_copy(ps->call));
772 live = isl_union_set_coalesce(live);
775 dep = isl_union_map_copy(ps->dep_flow);
776 dep = isl_union_map_reverse(dep);
778 for (;;) {
779 isl_union_set *extra;
781 extra = isl_union_set_apply(isl_union_set_copy(live),
782 isl_union_map_copy(dep));
783 if (isl_union_set_is_subset(extra, live)) {
784 isl_union_set_free(extra);
785 break;
788 live = isl_union_set_union(live, extra);
789 live = isl_union_set_affine_hull(live);
790 live = isl_union_set_intersect(live,
791 isl_union_set_copy(ps->domain));
794 isl_union_map_free(dep);
796 ps->domain = isl_union_set_intersect(ps->domain,
797 isl_union_set_copy(live));
798 ps->schedule = isl_schedule_intersect_domain(ps->schedule,
799 isl_union_set_copy(live));
800 ps->dep_flow = isl_union_map_intersect_range(ps->dep_flow,
801 isl_union_set_copy(live));
802 tagger = isl_union_pw_multi_aff_copy(ps->tagger);
803 live = isl_union_set_preimage_union_pw_multi_aff(live, tagger);
804 ps->tagged_dep_flow = isl_union_map_intersect_range(ps->tagged_dep_flow,
805 live);
808 /* Intersect "set" with the set described by "str", taking the NULL
809 * string to represent the universal set.
811 static __isl_give isl_set *set_intersect_str(__isl_take isl_set *set,
812 const char *str)
814 isl_ctx *ctx;
815 isl_set *set2;
817 if (!str)
818 return set;
820 ctx = isl_set_get_ctx(set);
821 set2 = isl_set_read_from_str(ctx, str);
822 set = isl_set_intersect(set, set2);
824 return set;
827 static void *ppcg_scop_free(struct ppcg_scop *ps)
829 if (!ps)
830 return NULL;
832 isl_set_free(ps->context);
833 isl_union_set_free(ps->domain);
834 isl_union_set_free(ps->call);
835 isl_union_map_free(ps->tagged_reads);
836 isl_union_map_free(ps->reads);
837 isl_union_map_free(ps->live_in);
838 isl_union_map_free(ps->tagged_may_writes);
839 isl_union_map_free(ps->tagged_must_writes);
840 isl_union_map_free(ps->may_writes);
841 isl_union_map_free(ps->must_writes);
842 isl_union_map_free(ps->live_out);
843 isl_union_map_free(ps->tagged_must_kills);
844 isl_union_map_free(ps->must_kills);
845 isl_union_map_free(ps->tagged_dep_flow);
846 isl_union_map_free(ps->dep_flow);
847 isl_union_map_free(ps->dep_false);
848 isl_union_map_free(ps->dep_forced);
849 isl_union_map_free(ps->tagged_dep_order);
850 isl_union_map_free(ps->dep_order);
851 isl_schedule_free(ps->schedule);
852 isl_union_pw_multi_aff_free(ps->tagger);
853 isl_union_map_free(ps->independence);
854 isl_id_to_ast_expr_free(ps->names);
856 free(ps);
858 return NULL;
861 /* Extract a ppcg_scop from a pet_scop.
863 * The constructed ppcg_scop refers to elements from the pet_scop
864 * so the pet_scop should not be freed before the ppcg_scop.
866 static struct ppcg_scop *ppcg_scop_from_pet_scop(struct pet_scop *scop,
867 struct ppcg_options *options)
869 int i;
870 isl_ctx *ctx;
871 struct ppcg_scop *ps;
873 if (!scop)
874 return NULL;
876 ctx = isl_set_get_ctx(scop->context);
878 ps = isl_calloc_type(ctx, struct ppcg_scop);
879 if (!ps)
880 return NULL;
882 ps->names = collect_names(scop);
883 ps->options = options;
884 ps->start = pet_loc_get_start(scop->loc);
885 ps->end = pet_loc_get_end(scop->loc);
886 ps->context = isl_set_copy(scop->context);
887 ps->context = set_intersect_str(ps->context, options->ctx);
888 if (options->non_negative_parameters) {
889 isl_space *space = isl_set_get_space(ps->context);
890 isl_set *nn = isl_set_nat_universe(space);
891 ps->context = isl_set_intersect(ps->context, nn);
893 ps->domain = collect_non_kill_domains(scop);
894 ps->call = collect_call_domains(scop);
895 ps->tagged_reads = pet_scop_get_tagged_may_reads(scop);
896 ps->reads = pet_scop_get_may_reads(scop);
897 ps->tagged_may_writes = pet_scop_get_tagged_may_writes(scop);
898 ps->may_writes = pet_scop_get_may_writes(scop);
899 ps->tagged_must_writes = pet_scop_get_tagged_must_writes(scop);
900 ps->must_writes = pet_scop_get_must_writes(scop);
901 ps->tagged_must_kills = pet_scop_get_tagged_must_kills(scop);
902 ps->must_kills = pet_scop_get_must_kills(scop);
903 ps->schedule = isl_schedule_copy(scop->schedule);
904 ps->pet = scop;
905 ps->independence = isl_union_map_empty(isl_set_get_space(ps->context));
906 for (i = 0; i < scop->n_independence; ++i)
907 ps->independence = isl_union_map_union(ps->independence,
908 isl_union_map_copy(scop->independences[i]->filter));
910 compute_tagger(ps);
911 compute_dependences(ps);
912 eliminate_dead_code(ps);
914 if (!ps->context || !ps->domain || !ps->call || !ps->reads ||
915 !ps->may_writes || !ps->must_writes || !ps->tagged_must_kills ||
916 !ps->must_kills || !ps->schedule || !ps->independence || !ps->names)
917 return ppcg_scop_free(ps);
919 return ps;
922 /* Internal data structure for ppcg_transform.
924 struct ppcg_transform_data {
925 struct ppcg_options *options;
926 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
927 struct ppcg_scop *scop, void *user);
928 void *user;
931 /* Should we print the original code?
932 * That is, does "scop" involve any data dependent conditions or
933 * nested expressions that cannot be handled by pet_stmt_build_ast_exprs?
935 static int print_original(struct pet_scop *scop, struct ppcg_options *options)
937 if (!pet_scop_can_build_ast_exprs(scop)) {
938 if (options->debug->verbose)
939 fprintf(stdout, "Printing original code because "
940 "some index expressions cannot currently "
941 "be printed\n");
942 return 1;
945 if (pet_scop_has_data_dependent_conditions(scop)) {
946 if (options->debug->verbose)
947 fprintf(stdout, "Printing original code because "
948 "input involves data dependent conditions\n");
949 return 1;
952 return 0;
955 /* Callback for pet_transform_C_source that transforms
956 * the given pet_scop to a ppcg_scop before calling the
957 * ppcg_transform callback.
959 * If "scop" contains any data dependent conditions or if we may
960 * not be able to print the transformed program, then just print
961 * the original code.
963 static __isl_give isl_printer *transform(__isl_take isl_printer *p,
964 struct pet_scop *scop, void *user)
966 struct ppcg_transform_data *data = user;
967 struct ppcg_scop *ps;
969 if (print_original(scop, data->options)) {
970 p = pet_scop_print_original(scop, p);
971 pet_scop_free(scop);
972 return p;
975 scop = pet_scop_align_params(scop);
976 ps = ppcg_scop_from_pet_scop(scop, data->options);
978 p = data->transform(p, ps, data->user);
980 ppcg_scop_free(ps);
981 pet_scop_free(scop);
983 return p;
986 /* Transform the C source file "input" by rewriting each scop
987 * through a call to "transform".
988 * The transformed C code is written to "out".
990 * This is a wrapper around pet_transform_C_source that transforms
991 * the pet_scop to a ppcg_scop before calling "fn".
993 int ppcg_transform(isl_ctx *ctx, const char *input, FILE *out,
994 struct ppcg_options *options,
995 __isl_give isl_printer *(*fn)(__isl_take isl_printer *p,
996 struct ppcg_scop *scop, void *user), void *user)
998 struct ppcg_transform_data data = { options, fn, user };
999 return pet_transform_C_source(ctx, input, out, &transform, &data);
1002 /* Check consistency of options.
1004 * Return -1 on error.
1006 static int check_options(isl_ctx *ctx)
1008 struct options *options;
1010 options = isl_ctx_peek_options(ctx, &options_args);
1011 if (!options)
1012 isl_die(ctx, isl_error_internal,
1013 "unable to find options", return -1);
1015 if (options->ppcg->openmp &&
1016 !isl_options_get_ast_build_atomic_upper_bound(ctx))
1017 isl_die(ctx, isl_error_invalid,
1018 "OpenMP requires atomic bounds", return -1);
1020 return 0;
1023 int main(int argc, char **argv)
1025 int r;
1026 isl_ctx *ctx;
1027 struct options *options;
1029 options = options_new_with_defaults();
1030 assert(options);
1032 ctx = isl_ctx_alloc_with_options(&options_args, options);
1033 ppcg_options_set_target_defaults(options->ppcg);
1034 isl_options_set_schedule_maximize_band_depth(ctx, 1);
1035 pet_options_set_encapsulate_dynamic_control(ctx, 1);
1036 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
1038 if (check_options(ctx) < 0)
1039 r = EXIT_FAILURE;
1040 else if (options->ppcg->target == PPCG_TARGET_CUDA)
1041 r = generate_cuda(ctx, options->ppcg, options->input);
1042 else if (options->ppcg->target == PPCG_TARGET_OPENCL)
1043 r = generate_opencl(ctx, options->ppcg, options->input,
1044 options->output);
1045 else
1046 r = generate_cpu(ctx, options->ppcg, options->input,
1047 options->output);
1049 isl_ctx_free(ctx);
1051 return r;