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,
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
19 #include <isl/options.h>
20 #include <isl/schedule.h>
21 #include <isl/ast_build.h>
22 #include <isl/schedule.h>
25 #include "ppcg_options.h"
31 struct isl_options
*isl
;
32 struct pet_options
*pet
;
33 struct ppcg_options
*ppcg
;
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
)
54 ISL_ARG_DEF(options
, struct options
, options_args
)
56 /* Copy the base name of "input" to "name" and return its length.
57 * "name" is not NULL terminated.
59 * In particular, remove all leading directory components and
60 * the final extension, if any.
62 int ppcg_extract_base_name(char *name
, const char *input
)
68 base
= strrchr(input
, '/');
73 ext
= strrchr(base
, '.');
74 len
= ext
? ext
- base
: strlen(base
);
76 memcpy(name
, base
, len
);
81 /* Is "stmt" not a kill statement?
83 static int is_not_kill(struct pet_stmt
*stmt
)
85 return !pet_stmt_is_kill(stmt
);
88 /* Collect the iteration domains of the statements in "scop" that
91 static __isl_give isl_union_set
*collect_domains(struct pet_scop
*scop
,
92 int (*pred
)(struct pet_stmt
*stmt
))
96 isl_union_set
*domain
;
101 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
103 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
104 struct pet_stmt
*stmt
= scop
->stmts
[i
];
110 isl_die(isl_union_set_get_ctx(domain
),
111 isl_error_unsupported
,
112 "data dependent conditions not supported",
113 return isl_union_set_free(domain
));
115 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
116 domain
= isl_union_set_add_set(domain
, domain_i
);
122 /* Collect the iteration domains of the statements in "scop",
123 * skipping kill statements.
125 static __isl_give isl_union_set
*collect_non_kill_domains(struct pet_scop
*scop
)
127 return collect_domains(scop
, &is_not_kill
);
130 /* Does "expr" contain any call expressions?
132 static int expr_has_call(struct pet_expr
*expr
)
136 if (expr
->type
== pet_expr_call
)
139 for (i
= 0; i
< expr
->n_arg
; ++i
)
140 if (expr_has_call(expr
->args
[i
]))
146 /* Does "stmt" contain any call expressions?
148 static int has_call(struct pet_stmt
*stmt
)
150 return expr_has_call(stmt
->body
);
153 /* Collect the iteration domains of the statements in "scop"
154 * that contain a call expression.
156 static __isl_give isl_union_set
*collect_call_domains(struct pet_scop
*scop
)
158 return collect_domains(scop
, &has_call
);
161 /* Given a union of "tagged" access relations of the form
163 * [S_i[...] -> R_j[]] -> A_k[...]
165 * project out the "tags" (R_j[]).
166 * That is, return a union of relations of the form
168 * S_i[...] -> A_k[...]
170 static __isl_give isl_union_map
*project_out_tags(
171 __isl_take isl_union_map
*umap
)
175 proj
= isl_union_map_universe(isl_union_map_copy(umap
));
176 proj
= isl_union_set_unwrap(isl_union_map_domain(proj
));
177 proj
= isl_union_map_domain_map(proj
);
179 umap
= isl_union_map_apply_domain(umap
, proj
);
184 /* Construct a relation from the iteration domains to tagged iteration
185 * domains with as range the reference tags that appear
186 * in any of the reads, writes or kills.
187 * Store the result in ps->tagger.
189 * For example, if the statement with iteration space S[i,j]
190 * contains two array references R_1[] and R_2[], then ps->tagger will contain
192 * { S[i,j] -> [S[i,j] -> R_1[]]; S[i,j] -> [S[i,j] -> R_2[]] }
194 static void compute_tagger(struct ppcg_scop
*ps
)
196 isl_union_map
*tagged
, *tagger
;
198 tagged
= isl_union_map_copy(ps
->tagged_reads
);
199 tagged
= isl_union_map_union(tagged
,
200 isl_union_map_copy(ps
->tagged_may_writes
));
201 tagged
= isl_union_map_union(tagged
,
202 isl_union_map_copy(ps
->tagged_must_kills
));
204 tagger
= isl_union_map_universe(tagged
);
205 tagger
= isl_union_set_unwrap(isl_union_map_domain(tagger
));
206 tagger
= isl_union_map_reverse(isl_union_map_domain_map(tagger
));
211 /* Compute the live out accesses, i.e., the writes that are
212 * potentially not killed by any kills or any other writes, and
213 * store them in ps->live_out.
215 * We compute the "dependence" of any "kill" (an explicit kill
216 * or a must write) on any may write.
217 * The may writes with a "depending" kill are definitely killed.
218 * The remaining may writes can potentially be live out.
220 static void compute_live_out(struct ppcg_scop
*ps
)
222 isl_union_map
*tagger
;
223 isl_union_map
*schedule
;
224 isl_union_map
*empty
;
225 isl_union_map
*kills
;
226 isl_union_map
*exposed
;
227 isl_union_map
*covering
;
229 tagger
= isl_union_map_copy(ps
->tagger
);
230 schedule
= isl_union_map_copy(ps
->schedule
);
231 schedule
= isl_union_map_apply_domain(schedule
,
232 isl_union_map_copy(tagger
));
233 empty
= isl_union_map_empty(isl_union_set_get_space(ps
->domain
));
234 kills
= isl_union_map_union(isl_union_map_copy(ps
->tagged_must_writes
),
235 isl_union_map_copy(ps
->tagged_must_kills
));
236 isl_union_map_compute_flow(kills
, empty
,
237 isl_union_map_copy(ps
->tagged_may_writes
),
238 schedule
, NULL
, &covering
, NULL
, NULL
);
239 exposed
= isl_union_map_copy(ps
->tagged_may_writes
);
240 exposed
= isl_union_map_subtract_domain(exposed
,
241 isl_union_map_domain(covering
));
242 exposed
= isl_union_map_apply_range(tagger
, exposed
);
243 ps
->live_out
= exposed
;
246 /* Compute the flow dependences and the live_in accesses and store
247 * the results in ps->dep_flow and ps->live_in.
248 * A copy of the flow dependences, tagged with the reference tags
249 * is stored in ps->tagged_dep_flow.
251 * We first compute ps->tagged_dep_flow, i.e., the tagged flow dependences
252 * and then project out the tags.
254 static void compute_tagged_flow_dep(struct ppcg_scop
*ps
)
256 isl_union_map
*tagger
;
257 isl_union_map
*schedule
;
258 isl_union_map
*may_flow
;
259 isl_union_map
*live_in
, *may_live_in
;
261 tagger
= isl_union_map_copy(ps
->tagger
);
262 schedule
= isl_union_map_copy(ps
->schedule
);
263 schedule
= isl_union_map_apply_domain(schedule
, tagger
);
264 isl_union_map_compute_flow(isl_union_map_copy(ps
->tagged_reads
),
265 isl_union_map_copy(ps
->tagged_must_writes
),
266 isl_union_map_copy(ps
->tagged_may_writes
),
267 schedule
, &ps
->tagged_dep_flow
, &may_flow
,
268 &live_in
, &may_live_in
);
269 ps
->tagged_dep_flow
= isl_union_map_union(ps
->tagged_dep_flow
,
271 ps
->dep_flow
= isl_union_map_copy(ps
->tagged_dep_flow
);
272 ps
->dep_flow
= isl_union_map_zip(ps
->dep_flow
);
273 ps
->dep_flow
= isl_union_set_unwrap(isl_union_map_domain(ps
->dep_flow
));
274 live_in
= isl_union_map_union(live_in
, may_live_in
);
275 ps
->live_in
= project_out_tags(live_in
);
278 /* Compute the order dependences that prevent the potential live ranges
280 * "before" contains all pairs of statement iterations where
281 * the first is executed before the second according to the original schedule.
283 * In particular, construct a union of relations
285 * [R[...] -> R_1[]] -> [W[...] -> R_2[]]
287 * where [R[...] -> R_1[]] is the range of one or more live ranges
288 * (i.e., a read) and [W[...] -> R_2[]] is the domain of one or more
289 * live ranges (i.e., a write). Moreover, the read and the write
290 * access the same memory element and the read occurs before the write
291 * in the original schedule.
292 * The scheduler allows some of these dependences to be violated, provided
293 * the adjacent live ranges are all local (i.e., their domain and range
294 * are mapped to the same point by the current schedule band).
296 * Note that if a live range is not local, then we need to make
297 * sure it does not overlap with _any_ other live range, and not
298 * just with the "previous" and/or the "next" live range.
299 * We therefore add order dependences between reads and
300 * _any_ later potential write.
302 * We also need to be careful about writes without a corresponding read.
303 * They are already prevented from moving past non-local preceding
304 * intervals, but we also need to prevent them from moving past non-local
305 * following intervals. We therefore also add order dependences from
306 * potential writes that do not appear in any intervals
307 * to all later potential writes.
308 * Note that dead code elimination should have removed most of these
309 * dead writes, but the dead code elimination may not remove all dead writes,
310 * so we need to consider them to be safe.
312 static void compute_order_dependences(struct ppcg_scop
*ps
,
313 __isl_take isl_union_map
*before
)
315 isl_union_map
*reads
;
316 isl_union_map
*shared_access
;
317 isl_union_set
*matched
;
318 isl_union_map
*unmatched
;
319 isl_union_set
*domain
;
321 reads
= isl_union_map_copy(ps
->tagged_reads
);
322 matched
= isl_union_map_domain(isl_union_map_copy(ps
->tagged_dep_flow
));
323 unmatched
= isl_union_map_copy(ps
->tagged_may_writes
);
324 unmatched
= isl_union_map_subtract_domain(unmatched
, matched
);
325 reads
= isl_union_map_union(reads
, unmatched
);
326 shared_access
= isl_union_map_copy(ps
->tagged_may_writes
);
327 shared_access
= isl_union_map_reverse(shared_access
);
328 shared_access
= isl_union_map_apply_range(reads
, shared_access
);
329 shared_access
= isl_union_map_zip(shared_access
);
330 shared_access
= isl_union_map_intersect_domain(shared_access
,
331 isl_union_map_wrap(before
));
332 domain
= isl_union_map_domain(isl_union_map_copy(shared_access
));
333 shared_access
= isl_union_map_zip(shared_access
);
334 ps
->dep_order
= isl_union_set_unwrap(domain
);
335 ps
->tagged_dep_order
= shared_access
;
338 /* Compute the external false dependences of the program represented by "scop"
339 * in case live range reordering is allowed.
340 * "before" contains all pairs of statement iterations where
341 * the first is executed before the second according to the original schedule.
343 * The anti-dependences are already taken care of by the order dependences.
344 * The external false dependences are only used to ensure that live-in and
345 * live-out data is not overwritten by any writes inside the scop.
347 * In particular, the reads from live-in data need to precede any
348 * later write to the same memory element.
349 * As to live-out data, the last writes need to remain the last writes.
350 * That is, any earlier write in the original schedule needs to precede
351 * the last write to the same memory element in the computed schedule.
352 * The possible last writes have been computed by compute_live_out.
353 * They may include kills, but if the last access is a kill,
354 * then the corresponding dependences will effectively be ignored
355 * since we do not schedule any kill statements.
357 * Note that the set of live-in and live-out accesses may be
358 * an overapproximation. There may therefore be potential writes
359 * before a live-in access and after a live-out access.
361 static void compute_external_false_dependences(struct ppcg_scop
*ps
,
362 __isl_take isl_union_map
*before
)
364 isl_union_map
*shared_access
;
365 isl_union_map
*exposed
;
366 isl_union_map
*live_in
;
368 exposed
= isl_union_map_copy(ps
->live_out
);
370 exposed
= isl_union_map_reverse(exposed
);
371 shared_access
= isl_union_map_copy(ps
->may_writes
);
372 shared_access
= isl_union_map_apply_range(shared_access
, exposed
);
374 ps
->dep_external
= shared_access
;
376 live_in
= isl_union_map_apply_range(isl_union_map_copy(ps
->live_in
),
377 isl_union_map_reverse(isl_union_map_copy(ps
->may_writes
)));
379 ps
->dep_external
= isl_union_map_union(ps
->dep_external
, live_in
);
380 ps
->dep_external
= isl_union_map_intersect(ps
->dep_external
, before
);
383 /* Compute the dependences of the program represented by "scop"
384 * in case live range reordering is allowed.
386 * We compute the actual live ranges and the corresponding order
389 static void compute_live_range_reordering_dependences(struct ppcg_scop
*ps
)
391 isl_union_map
*before
;
393 before
= isl_union_map_lex_lt_union_map(
394 isl_union_map_copy(ps
->schedule
),
395 isl_union_map_copy(ps
->schedule
));
397 compute_tagged_flow_dep(ps
);
398 compute_order_dependences(ps
, isl_union_map_copy(before
));
399 compute_external_false_dependences(ps
, before
);
402 /* Compute the potential flow dependences and the potential live in
405 static void compute_flow_dep(struct ppcg_scop
*ps
)
407 isl_union_map
*may_flow
;
408 isl_union_map
*may_live_in
;
410 isl_union_map_compute_flow(isl_union_map_copy(ps
->reads
),
411 isl_union_map_copy(ps
->must_writes
),
412 isl_union_map_copy(ps
->may_writes
),
413 isl_union_map_copy(ps
->schedule
),
414 &ps
->dep_flow
, &may_flow
,
415 &ps
->live_in
, &may_live_in
);
417 ps
->dep_flow
= isl_union_map_union(ps
->dep_flow
, may_flow
);
418 ps
->live_in
= isl_union_map_union(ps
->live_in
, may_live_in
);
421 /* Compute the dependences of the program represented by "scop".
422 * Store the computed potential flow dependences
423 * in scop->dep_flow and the reads with potentially no corresponding writes in
425 * Store the potential live out accesses in scop->live_out.
426 * Store the potential false (anti and output) dependences in scop->dep_false.
428 * If live range reordering is allowed, then we compute a separate
429 * set of order dependences and a set of external false dependences
430 * in compute_live_range_reordering_dependences.
432 static void compute_dependences(struct ppcg_scop
*scop
)
434 isl_union_map
*dep1
, *dep2
;
435 isl_union_map
*may_source
;
440 compute_live_out(scop
);
442 if (scop
->options
->live_range_reordering
)
443 compute_live_range_reordering_dependences(scop
);
444 else if (scop
->options
->target
!= PPCG_TARGET_C
)
445 compute_tagged_flow_dep(scop
);
447 compute_flow_dep(scop
);
449 may_source
= isl_union_map_union(isl_union_map_copy(scop
->may_writes
),
450 isl_union_map_copy(scop
->reads
));
451 isl_union_map_compute_flow(isl_union_map_copy(scop
->may_writes
),
452 isl_union_map_copy(scop
->must_writes
),
453 may_source
, isl_union_map_copy(scop
->schedule
),
454 &dep1
, &dep2
, NULL
, NULL
);
456 scop
->dep_false
= isl_union_map_union(dep1
, dep2
);
457 scop
->dep_false
= isl_union_map_coalesce(scop
->dep_false
);
460 /* Eliminate dead code from ps->domain.
462 * In particular, intersect ps->domain with the (parts of) iteration
463 * domains that are needed to produce the output or for statement
464 * iterations that call functions.
466 * We start with the iteration domains that call functions
467 * and the set of iterations that last write to an array
468 * (except those that are later killed).
470 * Then we add those statement iterations that produce
471 * something needed by the "live" statements iterations.
472 * We keep doing this until no more statement iterations can be added.
473 * To ensure that the procedure terminates, we compute the affine
474 * hull of the live iterations (bounded to the original iteration
475 * domains) each time we have added extra iterations.
477 static void eliminate_dead_code(struct ppcg_scop
*ps
)
482 live
= isl_union_map_domain(isl_union_map_copy(ps
->live_out
));
483 if (!isl_union_set_is_empty(ps
->call
)) {
484 live
= isl_union_set_union(live
, isl_union_set_copy(ps
->call
));
485 live
= isl_union_set_coalesce(live
);
488 dep
= isl_union_map_copy(ps
->dep_flow
);
489 dep
= isl_union_map_reverse(dep
);
492 isl_union_set
*extra
;
494 extra
= isl_union_set_apply(isl_union_set_copy(live
),
495 isl_union_map_copy(dep
));
496 if (isl_union_set_is_subset(extra
, live
)) {
497 isl_union_set_free(extra
);
501 live
= isl_union_set_union(live
, extra
);
502 live
= isl_union_set_affine_hull(live
);
503 live
= isl_union_set_intersect(live
,
504 isl_union_set_copy(ps
->domain
));
507 isl_union_map_free(dep
);
509 ps
->domain
= isl_union_set_intersect(ps
->domain
, live
);
512 /* Intersect "set" with the set described by "str", taking the NULL
513 * string to represent the universal set.
515 static __isl_give isl_set
*set_intersect_str(__isl_take isl_set
*set
,
524 ctx
= isl_set_get_ctx(set
);
525 set2
= isl_set_read_from_str(ctx
, str
);
526 set
= isl_set_intersect(set
, set2
);
531 static void *ppcg_scop_free(struct ppcg_scop
*ps
)
536 isl_set_free(ps
->context
);
537 isl_union_set_free(ps
->domain
);
538 isl_union_set_free(ps
->call
);
539 isl_union_map_free(ps
->tagged_reads
);
540 isl_union_map_free(ps
->reads
);
541 isl_union_map_free(ps
->live_in
);
542 isl_union_map_free(ps
->tagged_may_writes
);
543 isl_union_map_free(ps
->tagged_must_writes
);
544 isl_union_map_free(ps
->may_writes
);
545 isl_union_map_free(ps
->must_writes
);
546 isl_union_map_free(ps
->live_out
);
547 isl_union_map_free(ps
->tagged_must_kills
);
548 isl_union_map_free(ps
->tagged_dep_flow
);
549 isl_union_map_free(ps
->dep_flow
);
550 isl_union_map_free(ps
->dep_false
);
551 isl_union_map_free(ps
->dep_external
);
552 isl_union_map_free(ps
->tagged_dep_order
);
553 isl_union_map_free(ps
->dep_order
);
554 isl_union_map_free(ps
->schedule
);
555 isl_union_map_free(ps
->tagger
);
562 /* Extract a ppcg_scop from a pet_scop.
564 * The constructed ppcg_scop refers to elements from the pet_scop
565 * so the pet_scop should not be freed before the ppcg_scop.
567 static struct ppcg_scop
*ppcg_scop_from_pet_scop(struct pet_scop
*scop
,
568 struct ppcg_options
*options
)
571 struct ppcg_scop
*ps
;
576 ctx
= isl_set_get_ctx(scop
->context
);
578 ps
= isl_calloc_type(ctx
, struct ppcg_scop
);
582 ps
->options
= options
;
583 ps
->start
= scop
->start
;
585 ps
->context
= isl_set_copy(scop
->context
);
586 ps
->context
= set_intersect_str(ps
->context
, options
->ctx
);
587 ps
->domain
= collect_non_kill_domains(scop
);
588 ps
->call
= collect_call_domains(scop
);
589 ps
->tagged_reads
= pet_scop_collect_tagged_may_reads(scop
);
590 ps
->reads
= pet_scop_collect_may_reads(scop
);
591 ps
->tagged_may_writes
= pet_scop_collect_tagged_may_writes(scop
);
592 ps
->may_writes
= pet_scop_collect_may_writes(scop
);
593 ps
->tagged_must_writes
= pet_scop_collect_tagged_must_writes(scop
);
594 ps
->must_writes
= pet_scop_collect_must_writes(scop
);
595 ps
->tagged_must_kills
= pet_scop_collect_tagged_must_kills(scop
);
596 ps
->schedule
= pet_scop_collect_schedule(scop
);
597 ps
->n_type
= scop
->n_type
;
598 ps
->types
= scop
->types
;
599 ps
->n_array
= scop
->n_array
;
600 ps
->arrays
= scop
->arrays
;
601 ps
->n_stmt
= scop
->n_stmt
;
602 ps
->stmts
= scop
->stmts
;
605 compute_dependences(ps
);
606 eliminate_dead_code(ps
);
608 if (!ps
->context
|| !ps
->domain
|| !ps
->call
|| !ps
->reads
||
609 !ps
->may_writes
|| !ps
->must_writes
|| !ps
->tagged_must_kills
||
611 return ppcg_scop_free(ps
);
616 /* Internal data structure for ppcg_transform.
618 struct ppcg_transform_data
{
619 struct ppcg_options
*options
;
620 __isl_give isl_printer
*(*transform
)(__isl_take isl_printer
*p
,
621 struct ppcg_scop
*scop
, void *user
);
625 /* Callback for pet_transform_C_source that transforms
626 * the given pet_scop to a ppcg_scop before calling the
627 * ppcg_transform callback.
629 static __isl_give isl_printer
*transform(__isl_take isl_printer
*p
,
630 struct pet_scop
*scop
, void *user
)
632 struct ppcg_transform_data
*data
= user
;
633 struct ppcg_scop
*ps
;
635 if (pet_scop_has_data_dependent_conditions(scop
)) {
636 p
= pet_scop_print_original(scop
, p
);
641 scop
= pet_scop_align_params(scop
);
642 ps
= ppcg_scop_from_pet_scop(scop
, data
->options
);
644 p
= data
->transform(p
, ps
, data
->user
);
652 /* Transform the C source file "input" by rewriting each scop
653 * through a call to "transform".
654 * The transformed C code is written to "out".
656 * This is a wrapper around pet_transform_C_source that transforms
657 * the pet_scop to a ppcg_scop before calling "fn".
659 int ppcg_transform(isl_ctx
*ctx
, const char *input
, FILE *out
,
660 struct ppcg_options
*options
,
661 __isl_give isl_printer
*(*fn
)(__isl_take isl_printer
*p
,
662 struct ppcg_scop
*scop
, void *user
), void *user
)
664 struct ppcg_transform_data data
= { options
, fn
, user
};
665 return pet_transform_C_source(ctx
, input
, out
, &transform
, &data
);
668 /* Check consistency of options.
670 * Return -1 on error.
672 static int check_options(isl_ctx
*ctx
)
674 struct options
*options
;
676 options
= isl_ctx_peek_options(ctx
, &options_args
);
678 isl_die(ctx
, isl_error_internal
,
679 "unable to find options", return -1);
681 if (options
->ppcg
->openmp
&&
682 !isl_options_get_ast_build_atomic_upper_bound(ctx
))
683 isl_die(ctx
, isl_error_invalid
,
684 "OpenMP requires atomic bounds", return -1);
689 int main(int argc
, char **argv
)
693 struct options
*options
;
695 options
= options_new_with_defaults();
698 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
699 isl_options_set_schedule_outer_coincidence(ctx
, 1);
700 isl_options_set_schedule_maximize_band_depth(ctx
, 1);
701 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
703 if (check_options(ctx
) < 0)
705 else if (options
->ppcg
->target
== PPCG_TARGET_CUDA
)
706 r
= generate_cuda(ctx
, options
->ppcg
, options
->input
);
707 else if (options
->ppcg
->target
== PPCG_TARGET_OPENCL
)
708 r
= generate_opencl(ctx
, options
->ppcg
, options
->input
,
711 r
= generate_cpu(ctx
, options
->ppcg
, options
->input
,