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,
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
20 #include <isl/options.h>
21 #include <isl/schedule.h>
22 #include <isl/ast_build.h>
23 #include <isl/schedule.h>
26 #include "ppcg_options.h"
32 struct isl_options
*isl
;
33 struct pet_options
*pet
;
34 struct ppcg_options
*ppcg
;
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
)
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
)
64 base
= strrchr(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
)
83 base
= ppcg_base_name(input
);
84 ext
= strrchr(base
, '.');
85 len
= ext
? ext
- base
: strlen(base
);
87 memcpy(name
, base
, 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
)
102 for (i
= 0; i
< scop
->pet
->n_array
; ++i
)
103 if (scop
->pet
->arrays
[i
]->declared
&&
104 !scop
->pet
->arrays
[i
]->exposed
)
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
)
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
) {
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
];
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
);
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
)
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
);
170 while ((has_name
= isl_id_to_ast_expr_has(scop
->names
, id
)) == 1) {
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
)
191 ctx
= isl_set_get_ctx(scop
->context
);
192 names
= isl_id_list_alloc(ctx
, n
);
193 for (i
= 0; i
< n
; ++i
) {
196 id
= generate_name(scop
, prefix
, i
);
197 names
= isl_id_list_add(names
, id
);
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
213 static __isl_give isl_union_set
*collect_domains(struct pet_scop
*scop
,
214 int (*pred
)(struct pet_stmt
*stmt
))
218 isl_union_set
*domain
;
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
];
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
);
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
;
266 /* Does "expr" contain any call expressions?
268 static int expr_has_call(__isl_keep pet_expr
*expr
)
272 if (pet_expr_foreach_call_expr(expr
, &set_has_call
, &has_call
) < 0 &&
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
))
290 return *has_call
? -1 : 0;
293 /* Does "stmt" contain any call expressions?
295 static int has_call(struct pet_stmt
*stmt
)
299 if (pet_tree_foreach_expr(stmt
->body
, &check_call
, &has_call
) < 0 &&
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
);
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 tagged flow dependences and the live_in accesses and store
395 * the results in ps->tagged_dep_flow and ps->live_in.
397 * We allow both the must writes and the must kills to serve as
398 * definite sources such that a subsequent read would not depend
399 * on any earlier write. The resulting flow dependences with
400 * a must kill as source reflect possibly uninitialized reads.
401 * No dependences need to be introduced to protect such reads
402 * (other than those imposed by potential flows from may writes
403 * that follow the kill). We therefore remove those flow dependences.
404 * This is also useful for the dead code elimination, which assumes
405 * the flow sources are non-kill instances.
407 static void compute_tagged_flow_dep_only(struct ppcg_scop
*ps
)
409 isl_union_pw_multi_aff
*tagger
;
410 isl_schedule
*schedule
;
411 isl_union_map
*live_in
;
412 isl_union_access_info
*access
;
413 isl_union_flow
*flow
;
414 isl_union_map
*must_source
;
415 isl_union_map
*kills
;
416 isl_union_map
*tagged_flow
;
418 tagger
= isl_union_pw_multi_aff_copy(ps
->tagger
);
419 schedule
= isl_schedule_copy(ps
->schedule
);
420 schedule
= isl_schedule_pullback_union_pw_multi_aff(schedule
, tagger
);
421 kills
= isl_union_map_copy(ps
->tagged_must_kills
);
422 must_source
= isl_union_map_copy(ps
->tagged_must_writes
);
423 must_source
= isl_union_map_union(must_source
,
424 isl_union_map_copy(kills
));
425 access
= isl_union_access_info_from_sink(
426 isl_union_map_copy(ps
->tagged_reads
));
427 access
= isl_union_access_info_set_must_source(access
, must_source
);
428 access
= isl_union_access_info_set_may_source(access
,
429 isl_union_map_copy(ps
->tagged_may_writes
));
430 access
= isl_union_access_info_set_schedule(access
, schedule
);
431 flow
= isl_union_access_info_compute_flow(access
);
432 tagged_flow
= isl_union_flow_get_may_dependence(flow
);
433 tagged_flow
= isl_union_map_subtract_domain(tagged_flow
,
434 isl_union_map_domain(kills
));
435 ps
->tagged_dep_flow
= tagged_flow
;
436 live_in
= isl_union_flow_get_may_no_source(flow
);
437 ps
->live_in
= project_out_tags(live_in
);
438 isl_union_flow_free(flow
);
441 /* Compute ps->dep_flow from ps->tagged_dep_flow
442 * by projecting out the reference tags.
444 static void derive_flow_dep_from_tagged_flow_dep(struct ppcg_scop
*ps
)
446 ps
->dep_flow
= isl_union_map_copy(ps
->tagged_dep_flow
);
447 ps
->dep_flow
= isl_union_map_factor_domain(ps
->dep_flow
);
450 /* Compute the flow dependences and the live_in accesses and store
451 * the results in ps->dep_flow and ps->live_in.
452 * A copy of the flow dependences, tagged with the reference tags
453 * is stored in ps->tagged_dep_flow.
455 * We first compute ps->tagged_dep_flow, i.e., the tagged flow dependences
456 * and then project out the tags.
458 static void compute_tagged_flow_dep(struct ppcg_scop
*ps
)
460 compute_tagged_flow_dep_only(ps
);
461 derive_flow_dep_from_tagged_flow_dep(ps
);
464 /* Compute the order dependences that prevent the potential live ranges
466 * "before" contains all pairs of statement iterations where
467 * the first is executed before the second according to the original schedule.
469 * In particular, construct a union of relations
471 * [R[...] -> R_1[]] -> [W[...] -> R_2[]]
473 * where [R[...] -> R_1[]] is the range of one or more live ranges
474 * (i.e., a read) and [W[...] -> R_2[]] is the domain of one or more
475 * live ranges (i.e., a write). Moreover, the read and the write
476 * access the same memory element and the read occurs before the write
477 * in the original schedule.
478 * The scheduler allows some of these dependences to be violated, provided
479 * the adjacent live ranges are all local (i.e., their domain and range
480 * are mapped to the same point by the current schedule band).
482 * Note that if a live range is not local, then we need to make
483 * sure it does not overlap with _any_ other live range, and not
484 * just with the "previous" and/or the "next" live range.
485 * We therefore add order dependences between reads and
486 * _any_ later potential write.
488 * We also need to be careful about writes without a corresponding read.
489 * They are already prevented from moving past non-local preceding
490 * intervals, but we also need to prevent them from moving past non-local
491 * following intervals. We therefore also add order dependences from
492 * potential writes that do not appear in any intervals
493 * to all later potential writes.
494 * Note that dead code elimination should have removed most of these
495 * dead writes, but the dead code elimination may not remove all dead writes,
496 * so we need to consider them to be safe.
498 static void compute_order_dependences(struct ppcg_scop
*ps
,
499 __isl_take isl_union_map
*before
)
501 isl_union_map
*reads
;
502 isl_union_map
*shared_access
;
503 isl_union_set
*matched
;
504 isl_union_map
*unmatched
;
505 isl_union_set
*domain
;
507 reads
= isl_union_map_copy(ps
->tagged_reads
);
508 matched
= isl_union_map_domain(isl_union_map_copy(ps
->tagged_dep_flow
));
509 unmatched
= isl_union_map_copy(ps
->tagged_may_writes
);
510 unmatched
= isl_union_map_subtract_domain(unmatched
, matched
);
511 reads
= isl_union_map_union(reads
, unmatched
);
512 shared_access
= isl_union_map_copy(ps
->tagged_may_writes
);
513 shared_access
= isl_union_map_reverse(shared_access
);
514 shared_access
= isl_union_map_apply_range(reads
, shared_access
);
515 shared_access
= isl_union_map_zip(shared_access
);
516 shared_access
= isl_union_map_intersect_domain(shared_access
,
517 isl_union_map_wrap(before
));
518 domain
= isl_union_map_domain(isl_union_map_copy(shared_access
));
519 shared_access
= isl_union_map_zip(shared_access
);
520 ps
->dep_order
= isl_union_set_unwrap(domain
);
521 ps
->tagged_dep_order
= shared_access
;
524 /* Compute those validity dependences of the program represented by "scop"
525 * that should be unconditionally enforced even when live-range reordering
527 * "before" contains all pairs of statement iterations where
528 * the first is executed before the second according to the original schedule.
530 * In particular, compute the external false dependences
531 * as well as order dependences between sources with the same sink.
532 * The anti-dependences are already taken care of by the order dependences.
533 * The external false dependences are only used to ensure that live-in and
534 * live-out data is not overwritten by any writes inside the scop.
536 * In particular, the reads from live-in data need to precede any
537 * later write to the same memory element.
538 * As to live-out data, the last writes need to remain the last writes.
539 * That is, any earlier write in the original schedule needs to precede
540 * the last write to the same memory element in the computed schedule.
541 * The possible last writes have been computed by compute_live_out.
542 * They may include kills, but if the last access is a kill,
543 * then the corresponding dependences will effectively be ignored
544 * since we do not schedule any kill statements.
546 * Note that the set of live-in and live-out accesses may be
547 * an overapproximation. There may therefore be potential writes
548 * before a live-in access and after a live-out access.
550 * In the presence of may-writes, there may be multiple live-ranges
551 * with the same sink, accessing the same memory element.
552 * The sources of these live-ranges need to be executed
553 * in the same relative order as in the original program
554 * since we do not know which of the may-writes will actually
555 * perform a write. Consider all sources that share a sink and
556 * that may write to the same memory element and compute
557 * the order dependences among them.
559 static void compute_forced_dependences(struct ppcg_scop
*ps
,
560 __isl_take isl_union_map
*before
)
562 isl_union_map
*shared_access
;
563 isl_union_map
*exposed
;
564 isl_union_map
*live_in
;
565 isl_union_map
*sink_access
;
566 isl_union_map
*shared_sink
;
567 isl_union_access_info
*access
;
568 isl_union_flow
*flow
;
569 isl_schedule
*schedule
;
571 exposed
= isl_union_map_copy(ps
->live_out
);
573 exposed
= isl_union_map_reverse(exposed
);
574 shared_access
= isl_union_map_copy(ps
->may_writes
);
575 shared_access
= isl_union_map_apply_range(shared_access
, exposed
);
577 ps
->dep_forced
= shared_access
;
579 live_in
= isl_union_map_apply_range(isl_union_map_copy(ps
->live_in
),
580 isl_union_map_reverse(isl_union_map_copy(ps
->may_writes
)));
582 ps
->dep_forced
= isl_union_map_union(ps
->dep_forced
, live_in
);
583 ps
->dep_forced
= isl_union_map_intersect(ps
->dep_forced
, before
);
585 schedule
= isl_schedule_copy(ps
->schedule
);
586 sink_access
= isl_union_map_copy(ps
->tagged_dep_flow
);
587 sink_access
= isl_union_map_range_product(sink_access
,
588 isl_union_map_copy(ps
->tagged_may_writes
));
589 sink_access
= isl_union_map_domain_factor_domain(sink_access
);
590 access
= isl_union_access_info_from_sink(
591 isl_union_map_copy(sink_access
));
592 access
= isl_union_access_info_set_may_source(access
, sink_access
);
593 access
= isl_union_access_info_set_schedule(access
, schedule
);
594 flow
= isl_union_access_info_compute_flow(access
);
595 shared_sink
= isl_union_flow_get_may_dependence(flow
);
596 isl_union_flow_free(flow
);
597 ps
->dep_forced
= isl_union_map_union(ps
->dep_forced
, shared_sink
);
600 /* Remove independence from the tagged flow dependences.
601 * Since the user has guaranteed that source and sink of an independence
602 * can be executed in any order, there cannot be a flow dependence
603 * between them, so they can be removed from the set of flow dependences.
604 * However, if the source of such a flow dependence is a must write,
605 * then it may have killed other potential sources, which would have
606 * to be recovered if we were to remove those flow dependences.
607 * We therefore keep the flow dependences that originate in a must write,
608 * even if it corresponds to a known independence.
610 static void remove_independences_from_tagged_flow(struct ppcg_scop
*ps
)
613 isl_union_set
*indep
;
616 tf
= isl_union_map_copy(ps
->tagged_dep_flow
);
617 tf
= isl_union_map_zip(tf
);
618 indep
= isl_union_map_wrap(isl_union_map_copy(ps
->independence
));
619 tf
= isl_union_map_intersect_domain(tf
, indep
);
620 tf
= isl_union_map_zip(tf
);
621 mw
= isl_union_map_domain(isl_union_map_copy(ps
->tagged_must_writes
));
622 tf
= isl_union_map_subtract_domain(tf
, mw
);
623 ps
->tagged_dep_flow
= isl_union_map_subtract(ps
->tagged_dep_flow
, tf
);
626 /* Compute the dependences of the program represented by "scop"
627 * in case live range reordering is allowed.
629 * We compute the actual live ranges and the corresponding order
632 * The independences are removed from the flow dependences
633 * (provided the source is not a must-write) as well as
634 * from the external false dependences.
636 static void compute_live_range_reordering_dependences(struct ppcg_scop
*ps
)
638 isl_union_map
*before
;
639 isl_union_map
*schedule
;
641 schedule
= isl_schedule_get_map(ps
->schedule
);
642 before
= isl_union_map_lex_lt_union_map(schedule
,
643 isl_union_map_copy(schedule
));
645 compute_tagged_flow_dep_only(ps
);
646 remove_independences_from_tagged_flow(ps
);
647 derive_flow_dep_from_tagged_flow_dep(ps
);
648 compute_order_dependences(ps
, isl_union_map_copy(before
));
649 before
= isl_union_map_subtract(before
,
650 isl_union_map_copy(ps
->independence
));
651 compute_forced_dependences(ps
, before
);
654 /* Compute the potential flow dependences and the potential live in
657 static void compute_flow_dep(struct ppcg_scop
*ps
)
659 isl_union_access_info
*access
;
660 isl_union_flow
*flow
;
662 access
= isl_union_access_info_from_sink(isl_union_map_copy(ps
->reads
));
663 access
= isl_union_access_info_set_must_source(access
,
664 isl_union_map_copy(ps
->must_writes
));
665 access
= isl_union_access_info_set_may_source(access
,
666 isl_union_map_copy(ps
->may_writes
));
667 access
= isl_union_access_info_set_schedule(access
,
668 isl_schedule_copy(ps
->schedule
));
669 flow
= isl_union_access_info_compute_flow(access
);
671 ps
->dep_flow
= isl_union_flow_get_may_dependence(flow
);
672 ps
->live_in
= isl_union_flow_get_may_no_source(flow
);
673 isl_union_flow_free(flow
);
676 /* Compute the dependences of the program represented by "scop".
677 * Store the computed potential flow dependences
678 * in scop->dep_flow and the reads with potentially no corresponding writes in
680 * Store the potential live out accesses in scop->live_out.
681 * Store the potential false (anti and output) dependences in scop->dep_false.
683 * If live range reordering is allowed, then we compute a separate
684 * set of order dependences and a set of external false dependences
685 * in compute_live_range_reordering_dependences.
687 static void compute_dependences(struct ppcg_scop
*scop
)
689 isl_union_map
*may_source
;
690 isl_union_access_info
*access
;
691 isl_union_flow
*flow
;
696 compute_live_out(scop
);
698 if (scop
->options
->live_range_reordering
)
699 compute_live_range_reordering_dependences(scop
);
700 else if (scop
->options
->target
!= PPCG_TARGET_C
)
701 compute_tagged_flow_dep(scop
);
703 compute_flow_dep(scop
);
705 may_source
= isl_union_map_union(isl_union_map_copy(scop
->may_writes
),
706 isl_union_map_copy(scop
->reads
));
707 access
= isl_union_access_info_from_sink(
708 isl_union_map_copy(scop
->may_writes
));
709 access
= isl_union_access_info_set_must_source(access
,
710 isl_union_map_copy(scop
->must_writes
));
711 access
= isl_union_access_info_set_may_source(access
, may_source
);
712 access
= isl_union_access_info_set_schedule(access
,
713 isl_schedule_copy(scop
->schedule
));
714 flow
= isl_union_access_info_compute_flow(access
);
716 scop
->dep_false
= isl_union_flow_get_may_dependence(flow
);
717 scop
->dep_false
= isl_union_map_coalesce(scop
->dep_false
);
718 isl_union_flow_free(flow
);
721 /* Eliminate dead code from ps->domain.
723 * In particular, intersect both ps->domain and the domain of
724 * ps->schedule with the (parts of) iteration
725 * domains that are needed to produce the output or for statement
726 * iterations that call functions.
727 * Also intersect the range of the dataflow dependences with
728 * this domain such that the removed instances will no longer
729 * be considered as targets of dataflow.
731 * We start with the iteration domains that call functions
732 * and the set of iterations that last write to an array
733 * (except those that are later killed).
735 * Then we add those statement iterations that produce
736 * something needed by the "live" statements iterations.
737 * We keep doing this until no more statement iterations can be added.
738 * To ensure that the procedure terminates, we compute the affine
739 * hull of the live iterations (bounded to the original iteration
740 * domains) each time we have added extra iterations.
742 static void eliminate_dead_code(struct ppcg_scop
*ps
)
746 isl_union_pw_multi_aff
*tagger
;
748 live
= isl_union_map_domain(isl_union_map_copy(ps
->live_out
));
749 if (!isl_union_set_is_empty(ps
->call
)) {
750 live
= isl_union_set_union(live
, isl_union_set_copy(ps
->call
));
751 live
= isl_union_set_coalesce(live
);
754 dep
= isl_union_map_copy(ps
->dep_flow
);
755 dep
= isl_union_map_reverse(dep
);
758 isl_union_set
*extra
;
760 extra
= isl_union_set_apply(isl_union_set_copy(live
),
761 isl_union_map_copy(dep
));
762 if (isl_union_set_is_subset(extra
, live
)) {
763 isl_union_set_free(extra
);
767 live
= isl_union_set_union(live
, extra
);
768 live
= isl_union_set_affine_hull(live
);
769 live
= isl_union_set_intersect(live
,
770 isl_union_set_copy(ps
->domain
));
773 isl_union_map_free(dep
);
775 ps
->domain
= isl_union_set_intersect(ps
->domain
,
776 isl_union_set_copy(live
));
777 ps
->schedule
= isl_schedule_intersect_domain(ps
->schedule
,
778 isl_union_set_copy(live
));
779 ps
->dep_flow
= isl_union_map_intersect_range(ps
->dep_flow
,
780 isl_union_set_copy(live
));
781 tagger
= isl_union_pw_multi_aff_copy(ps
->tagger
);
782 live
= isl_union_set_preimage_union_pw_multi_aff(live
, tagger
);
783 ps
->tagged_dep_flow
= isl_union_map_intersect_range(ps
->tagged_dep_flow
,
787 /* Intersect "set" with the set described by "str", taking the NULL
788 * string to represent the universal set.
790 static __isl_give isl_set
*set_intersect_str(__isl_take isl_set
*set
,
799 ctx
= isl_set_get_ctx(set
);
800 set2
= isl_set_read_from_str(ctx
, str
);
801 set
= isl_set_intersect(set
, set2
);
806 static void *ppcg_scop_free(struct ppcg_scop
*ps
)
811 isl_set_free(ps
->context
);
812 isl_union_set_free(ps
->domain
);
813 isl_union_set_free(ps
->call
);
814 isl_union_map_free(ps
->tagged_reads
);
815 isl_union_map_free(ps
->reads
);
816 isl_union_map_free(ps
->live_in
);
817 isl_union_map_free(ps
->tagged_may_writes
);
818 isl_union_map_free(ps
->tagged_must_writes
);
819 isl_union_map_free(ps
->may_writes
);
820 isl_union_map_free(ps
->must_writes
);
821 isl_union_map_free(ps
->live_out
);
822 isl_union_map_free(ps
->tagged_must_kills
);
823 isl_union_map_free(ps
->tagged_dep_flow
);
824 isl_union_map_free(ps
->dep_flow
);
825 isl_union_map_free(ps
->dep_false
);
826 isl_union_map_free(ps
->dep_forced
);
827 isl_union_map_free(ps
->tagged_dep_order
);
828 isl_union_map_free(ps
->dep_order
);
829 isl_schedule_free(ps
->schedule
);
830 isl_union_pw_multi_aff_free(ps
->tagger
);
831 isl_union_map_free(ps
->independence
);
832 isl_id_to_ast_expr_free(ps
->names
);
839 /* Extract a ppcg_scop from a pet_scop.
841 * The constructed ppcg_scop refers to elements from the pet_scop
842 * so the pet_scop should not be freed before the ppcg_scop.
844 static struct ppcg_scop
*ppcg_scop_from_pet_scop(struct pet_scop
*scop
,
845 struct ppcg_options
*options
)
849 struct ppcg_scop
*ps
;
854 ctx
= isl_set_get_ctx(scop
->context
);
856 ps
= isl_calloc_type(ctx
, struct ppcg_scop
);
860 ps
->names
= collect_names(scop
);
861 ps
->options
= options
;
862 ps
->start
= pet_loc_get_start(scop
->loc
);
863 ps
->end
= pet_loc_get_end(scop
->loc
);
864 ps
->context
= isl_set_copy(scop
->context
);
865 ps
->context
= set_intersect_str(ps
->context
, options
->ctx
);
866 if (options
->non_negative_parameters
) {
867 isl_space
*space
= isl_set_get_space(ps
->context
);
868 isl_set
*nn
= isl_set_nat_universe(space
);
869 ps
->context
= isl_set_intersect(ps
->context
, nn
);
871 ps
->domain
= collect_non_kill_domains(scop
);
872 ps
->call
= collect_call_domains(scop
);
873 ps
->tagged_reads
= pet_scop_collect_tagged_may_reads(scop
);
874 ps
->reads
= pet_scop_collect_may_reads(scop
);
875 ps
->tagged_may_writes
= pet_scop_collect_tagged_may_writes(scop
);
876 ps
->may_writes
= pet_scop_collect_may_writes(scop
);
877 ps
->tagged_must_writes
= pet_scop_collect_tagged_must_writes(scop
);
878 ps
->must_writes
= pet_scop_collect_must_writes(scop
);
879 ps
->tagged_must_kills
= pet_scop_collect_tagged_must_kills(scop
);
880 ps
->schedule
= isl_schedule_copy(scop
->schedule
);
882 ps
->independence
= isl_union_map_empty(isl_set_get_space(ps
->context
));
883 for (i
= 0; i
< scop
->n_independence
; ++i
)
884 ps
->independence
= isl_union_map_union(ps
->independence
,
885 isl_union_map_copy(scop
->independences
[i
]->filter
));
888 compute_dependences(ps
);
889 eliminate_dead_code(ps
);
891 if (!ps
->context
|| !ps
->domain
|| !ps
->call
|| !ps
->reads
||
892 !ps
->may_writes
|| !ps
->must_writes
|| !ps
->tagged_must_kills
||
893 !ps
->schedule
|| !ps
->independence
|| !ps
->names
)
894 return ppcg_scop_free(ps
);
899 /* Internal data structure for ppcg_transform.
901 struct ppcg_transform_data
{
902 struct ppcg_options
*options
;
903 __isl_give isl_printer
*(*transform
)(__isl_take isl_printer
*p
,
904 struct ppcg_scop
*scop
, void *user
);
908 /* Should we print the original code?
909 * That is, does "scop" involve any data dependent conditions or
910 * nested expressions that cannot be handled by pet_stmt_build_ast_exprs?
912 static int print_original(struct pet_scop
*scop
, struct ppcg_options
*options
)
914 if (!pet_scop_can_build_ast_exprs(scop
)) {
915 if (options
->debug
->verbose
)
916 fprintf(stdout
, "Printing original code because "
917 "some index expressions cannot currently "
922 if (pet_scop_has_data_dependent_conditions(scop
)) {
923 if (options
->debug
->verbose
)
924 fprintf(stdout
, "Printing original code because "
925 "input involves data dependent conditions\n");
932 /* Callback for pet_transform_C_source that transforms
933 * the given pet_scop to a ppcg_scop before calling the
934 * ppcg_transform callback.
936 * If "scop" contains any data dependent conditions or if we may
937 * not be able to print the transformed program, then just print
940 static __isl_give isl_printer
*transform(__isl_take isl_printer
*p
,
941 struct pet_scop
*scop
, void *user
)
943 struct ppcg_transform_data
*data
= user
;
944 struct ppcg_scop
*ps
;
946 if (print_original(scop
, data
->options
)) {
947 p
= pet_scop_print_original(scop
, p
);
952 scop
= pet_scop_align_params(scop
);
953 ps
= ppcg_scop_from_pet_scop(scop
, data
->options
);
955 p
= data
->transform(p
, ps
, data
->user
);
963 /* Transform the C source file "input" by rewriting each scop
964 * through a call to "transform".
965 * The transformed C code is written to "out".
967 * This is a wrapper around pet_transform_C_source that transforms
968 * the pet_scop to a ppcg_scop before calling "fn".
970 int ppcg_transform(isl_ctx
*ctx
, const char *input
, FILE *out
,
971 struct ppcg_options
*options
,
972 __isl_give isl_printer
*(*fn
)(__isl_take isl_printer
*p
,
973 struct ppcg_scop
*scop
, void *user
), void *user
)
975 struct ppcg_transform_data data
= { options
, fn
, user
};
976 return pet_transform_C_source(ctx
, input
, out
, &transform
, &data
);
979 /* Check consistency of options.
981 * Return -1 on error.
983 static int check_options(isl_ctx
*ctx
)
985 struct options
*options
;
987 options
= isl_ctx_peek_options(ctx
, &options_args
);
989 isl_die(ctx
, isl_error_internal
,
990 "unable to find options", return -1);
992 if (options
->ppcg
->openmp
&&
993 !isl_options_get_ast_build_atomic_upper_bound(ctx
))
994 isl_die(ctx
, isl_error_invalid
,
995 "OpenMP requires atomic bounds", return -1);
1000 int main(int argc
, char **argv
)
1004 struct options
*options
;
1006 options
= options_new_with_defaults();
1009 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
1010 isl_options_set_schedule_outer_coincidence(ctx
, 1);
1011 isl_options_set_schedule_maximize_band_depth(ctx
, 1);
1012 pet_options_set_encapsulate_dynamic_control(ctx
, 1);
1013 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
1015 if (check_options(ctx
) < 0)
1017 else if (options
->ppcg
->target
== PPCG_TARGET_CUDA
)
1018 r
= generate_cuda(ctx
, options
->ppcg
, options
->input
);
1019 else if (options
->ppcg
->target
== PPCG_TARGET_OPENCL
)
1020 r
= generate_opencl(ctx
, options
->ppcg
, options
->input
,
1023 r
= generate_cpu(ctx
, options
->ppcg
, options
->input
,