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" a kill statement?
83 static int is_kill(struct pet_stmt
*stmt
)
85 if (stmt
->body
->type
!= pet_expr_unary
)
87 return stmt
->body
->op
== pet_op_kill
;
90 /* Is "stmt" not a kill statement?
92 static int is_not_kill(struct pet_stmt
*stmt
)
94 return !is_kill(stmt
);
97 /* Collect the iteration domains of the statements in "scop" that
100 static __isl_give isl_union_set
*collect_domains(struct pet_scop
*scop
,
101 int (*pred
)(struct pet_stmt
*stmt
))
105 isl_union_set
*domain
;
110 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
112 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
113 struct pet_stmt
*stmt
= scop
->stmts
[i
];
119 isl_die(isl_union_set_get_ctx(domain
),
120 isl_error_unsupported
,
121 "data dependent conditions not supported",
122 return isl_union_set_free(domain
));
124 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
125 domain
= isl_union_set_add_set(domain
, domain_i
);
131 /* Collect the iteration domains of the statements in "scop",
132 * skipping kill statements.
134 static __isl_give isl_union_set
*collect_non_kill_domains(struct pet_scop
*scop
)
136 return collect_domains(scop
, &is_not_kill
);
139 /* Does "expr" contain any call expressions?
141 static int expr_has_call(struct pet_expr
*expr
)
145 if (expr
->type
== pet_expr_call
)
148 for (i
= 0; i
< expr
->n_arg
; ++i
)
149 if (expr_has_call(expr
->args
[i
]))
155 /* Does "stmt" contain any call expressions?
157 static int has_call(struct pet_stmt
*stmt
)
159 return expr_has_call(stmt
->body
);
162 /* Collect the iteration domains of the statements in "scop"
163 * that contain a call expression.
165 static __isl_give isl_union_set
*collect_call_domains(struct pet_scop
*scop
)
167 return collect_domains(scop
, &has_call
);
170 /* Given a union of "tagged" access relations of the form
172 * [S_i[...] -> R_j[]] -> A_k[...]
174 * project out the "tags" (R_j[]).
175 * That is, return a union of relations of the form
177 * S_i[...] -> A_k[...]
179 static __isl_give isl_union_map
*project_out_tags(
180 __isl_take isl_union_map
*umap
)
184 proj
= isl_union_map_universe(isl_union_map_copy(umap
));
185 proj
= isl_union_set_unwrap(isl_union_map_domain(proj
));
186 proj
= isl_union_map_domain_map(proj
);
188 umap
= isl_union_map_apply_domain(umap
, proj
);
193 /* Construct a relation from the iteration domains to tagged iteration
194 * domains with as range the reference tags that appear
195 * in any of the reads, writes or kills.
196 * Store the result in ps->tagger.
198 * For example, if the statement with iteration space S[i,j]
199 * contains two array references R_1[] and R_2[], then ps->tagger will contain
201 * { S[i,j] -> [S[i,j] -> R_1[]]; S[i,j] -> [S[i,j] -> R_2[]] }
203 static void compute_tagger(struct ppcg_scop
*ps
)
205 isl_union_map
*tagged
, *tagger
;
207 tagged
= isl_union_map_copy(ps
->tagged_reads
);
208 tagged
= isl_union_map_union(tagged
,
209 isl_union_map_copy(ps
->tagged_may_writes
));
210 tagged
= isl_union_map_union(tagged
,
211 isl_union_map_copy(ps
->tagged_must_kills
));
213 tagger
= isl_union_map_universe(tagged
);
214 tagger
= isl_union_set_unwrap(isl_union_map_domain(tagger
));
215 tagger
= isl_union_map_reverse(isl_union_map_domain_map(tagger
));
220 /* Compute the live out accesses, i.e., the writes that are
221 * potentially not killed by any kills or any other writes, and
222 * store them in ps->live_out.
224 * We compute the "dependence" of any "kill" (an explicit kill
225 * or a must write) on any may write.
226 * The may writes with a "depending" kill are definitely killed.
227 * The remaining may writes can potentially be live out.
229 static void compute_live_out(struct ppcg_scop
*ps
)
231 isl_union_map
*tagger
;
232 isl_union_map
*schedule
;
233 isl_union_map
*empty
;
234 isl_union_map
*kills
;
235 isl_union_map
*exposed
;
236 isl_union_map
*covering
;
238 tagger
= isl_union_map_copy(ps
->tagger
);
239 schedule
= isl_union_map_copy(ps
->schedule
);
240 schedule
= isl_union_map_apply_domain(schedule
,
241 isl_union_map_copy(tagger
));
242 empty
= isl_union_map_empty(isl_union_set_get_space(ps
->domain
));
243 kills
= isl_union_map_union(isl_union_map_copy(ps
->tagged_must_writes
),
244 isl_union_map_copy(ps
->tagged_must_kills
));
245 isl_union_map_compute_flow(kills
, empty
,
246 isl_union_map_copy(ps
->tagged_may_writes
),
247 schedule
, NULL
, &covering
, NULL
, NULL
);
248 exposed
= isl_union_map_copy(ps
->tagged_may_writes
);
249 exposed
= isl_union_map_subtract_domain(exposed
,
250 isl_union_map_domain(covering
));
251 exposed
= isl_union_map_apply_range(tagger
, exposed
);
252 ps
->live_out
= exposed
;
255 /* Compute the flow dependences and the live_in accesses and store
256 * the results in ps->dep_flow and ps->live_in.
257 * A copy of the flow dependences, tagged with the reference tags
258 * is stored in ps->tagged_dep_flow.
260 * We first compute ps->tagged_dep_flow, i.e., the tagged flow dependences
261 * and then project out the tags.
263 static void compute_tagged_flow_dep(struct ppcg_scop
*ps
)
265 isl_union_map
*tagger
;
266 isl_union_map
*schedule
;
267 isl_union_map
*may_flow
;
268 isl_union_map
*live_in
, *may_live_in
;
270 tagger
= isl_union_map_copy(ps
->tagger
);
271 schedule
= isl_union_map_copy(ps
->schedule
);
272 schedule
= isl_union_map_apply_domain(schedule
, tagger
);
273 isl_union_map_compute_flow(isl_union_map_copy(ps
->tagged_reads
),
274 isl_union_map_copy(ps
->tagged_must_writes
),
275 isl_union_map_copy(ps
->tagged_may_writes
),
276 schedule
, &ps
->tagged_dep_flow
, &may_flow
,
277 &live_in
, &may_live_in
);
278 ps
->tagged_dep_flow
= isl_union_map_union(ps
->tagged_dep_flow
,
280 ps
->dep_flow
= isl_union_map_copy(ps
->tagged_dep_flow
);
281 ps
->dep_flow
= isl_union_map_zip(ps
->dep_flow
);
282 ps
->dep_flow
= isl_union_set_unwrap(isl_union_map_domain(ps
->dep_flow
));
283 live_in
= isl_union_map_union(live_in
, may_live_in
);
284 ps
->live_in
= project_out_tags(live_in
);
287 /* Compute the order dependences that prevent the potential live ranges
289 * "before" contains all pairs of statement iterations where
290 * the first is executed before the second according to the original schedule.
292 * In particular, construct a union of relations
294 * [R[...] -> R_1[]] -> [W[...] -> R_2[]]
296 * where [R[...] -> R_1[]] is the range of one or more live ranges
297 * (i.e., a read) and [W[...] -> R_2[]] is the domain of one or more
298 * live ranges (i.e., a write). Moreover, the read and the write
299 * access the same memory element and the read occurs before the write
300 * in the original schedule.
301 * The scheduler allows some of these dependences to be violated, provided
302 * the adjacent live ranges are all local (i.e., their domain and range
303 * are mapped to the same point by the current schedule band).
305 * Note that if a live range is not local, then we need to make
306 * sure it does not overlap with _any_ other live range, and not
307 * just with the "previous" and/or the "next" live range.
308 * We therefore add order dependences between reads and
309 * _any_ later potential write.
311 * We also need to be careful about writes without a corresponding read.
312 * They are already prevented from moving past non-local preceding
313 * intervals, but we also need to prevent them from moving past non-local
314 * following intervals. We therefore also add order dependences from
315 * potential writes that do not appear in any intervals
316 * to all later potential writes.
317 * Note that dead code elimination should have removed most of these
318 * dead writes, but the dead code elimination may not remove all dead writes,
319 * so we need to consider them to be safe.
321 static void compute_order_dependences(struct ppcg_scop
*ps
,
322 __isl_take isl_union_map
*before
)
324 isl_union_map
*reads
;
325 isl_union_map
*shared_access
;
326 isl_union_set
*matched
;
327 isl_union_map
*unmatched
;
328 isl_union_set
*domain
;
330 reads
= isl_union_map_copy(ps
->tagged_reads
);
331 matched
= isl_union_map_domain(isl_union_map_copy(ps
->tagged_dep_flow
));
332 unmatched
= isl_union_map_copy(ps
->tagged_may_writes
);
333 unmatched
= isl_union_map_subtract_domain(unmatched
, matched
);
334 reads
= isl_union_map_union(reads
, unmatched
);
335 shared_access
= isl_union_map_copy(ps
->tagged_may_writes
);
336 shared_access
= isl_union_map_reverse(shared_access
);
337 shared_access
= isl_union_map_apply_range(reads
, shared_access
);
338 shared_access
= isl_union_map_zip(shared_access
);
339 shared_access
= isl_union_map_intersect_domain(shared_access
,
340 isl_union_map_wrap(before
));
341 domain
= isl_union_map_domain(isl_union_map_copy(shared_access
));
342 shared_access
= isl_union_map_zip(shared_access
);
343 ps
->dep_order
= isl_union_set_unwrap(domain
);
344 ps
->tagged_dep_order
= shared_access
;
347 /* Compute the external false dependences of the program represented by "scop"
348 * in case live range reordering is allowed.
349 * "before" contains all pairs of statement iterations where
350 * the first is executed before the second according to the original schedule.
352 * The anti-dependences are already taken care of by the order dependences.
353 * The external false dependences are only used to ensure that live-in and
354 * live-out data is not overwritten by any writes inside the scop.
356 * In particular, the reads from live-in data need to precede any
357 * later write to the same memory element.
358 * As to live-out data, the last writes need to remain the last writes.
359 * That is, any earlier write in the original schedule needs to precede
360 * the last write to the same memory element in the computed schedule.
361 * The possible last writes have been computed by compute_live_out.
362 * They may include kills, but if the last access is a kill,
363 * then the corresponding dependences will effectively be ignored
364 * since we do not schedule any kill statements.
366 * Note that the set of live-in and live-out accesses may be
367 * an overapproximation. There may therefore be potential writes
368 * before a live-in access and after a live-out access.
370 static void compute_external_false_dependences(struct ppcg_scop
*ps
,
371 __isl_take isl_union_map
*before
)
373 isl_union_map
*shared_access
;
374 isl_union_map
*exposed
;
375 isl_union_map
*live_in
;
377 exposed
= isl_union_map_copy(ps
->live_out
);
379 exposed
= isl_union_map_reverse(exposed
);
380 shared_access
= isl_union_map_copy(ps
->may_writes
);
381 shared_access
= isl_union_map_apply_range(shared_access
, exposed
);
383 ps
->dep_external
= shared_access
;
385 live_in
= isl_union_map_apply_range(isl_union_map_copy(ps
->live_in
),
386 isl_union_map_reverse(isl_union_map_copy(ps
->may_writes
)));
388 ps
->dep_external
= isl_union_map_union(ps
->dep_external
, live_in
);
389 ps
->dep_external
= isl_union_map_intersect(ps
->dep_external
, before
);
392 /* Compute the dependences of the program represented by "scop"
393 * in case live range reordering is allowed.
395 * We compute the actual live ranges and the corresponding order
398 static void compute_live_range_reordering_dependences(struct ppcg_scop
*ps
)
400 isl_union_map
*before
;
402 before
= isl_union_map_lex_lt_union_map(
403 isl_union_map_copy(ps
->schedule
),
404 isl_union_map_copy(ps
->schedule
));
406 compute_tagged_flow_dep(ps
);
407 compute_order_dependences(ps
, isl_union_map_copy(before
));
408 compute_external_false_dependences(ps
, before
);
411 /* Compute the potential flow dependences and the potential live in
414 static void compute_flow_dep(struct ppcg_scop
*ps
)
416 isl_union_map
*may_flow
;
417 isl_union_map
*may_live_in
;
419 isl_union_map_compute_flow(isl_union_map_copy(ps
->reads
),
420 isl_union_map_copy(ps
->must_writes
),
421 isl_union_map_copy(ps
->may_writes
),
422 isl_union_map_copy(ps
->schedule
),
423 &ps
->dep_flow
, &may_flow
,
424 &ps
->live_in
, &may_live_in
);
426 ps
->dep_flow
= isl_union_map_union(ps
->dep_flow
, may_flow
);
427 ps
->live_in
= isl_union_map_union(ps
->live_in
, may_live_in
);
430 /* Compute the dependences of the program represented by "scop".
431 * Store the computed potential flow dependences
432 * in scop->dep_flow and the reads with potentially no corresponding writes in
434 * Store the potential live out accesses in scop->live_out.
435 * Store the potential false (anti and output) dependences in scop->dep_false.
437 * If live range reordering is allowed, then we compute a separate
438 * set of order dependences and a set of external false dependences
439 * in compute_live_range_reordering_dependences.
441 static void compute_dependences(struct ppcg_scop
*scop
)
443 isl_union_map
*dep1
, *dep2
;
444 isl_union_map
*may_source
;
449 compute_live_out(scop
);
451 if (scop
->options
->live_range_reordering
)
452 compute_live_range_reordering_dependences(scop
);
453 else if (scop
->options
->target
!= PPCG_TARGET_C
)
454 compute_tagged_flow_dep(scop
);
456 compute_flow_dep(scop
);
458 may_source
= isl_union_map_union(isl_union_map_copy(scop
->may_writes
),
459 isl_union_map_copy(scop
->reads
));
460 isl_union_map_compute_flow(isl_union_map_copy(scop
->may_writes
),
461 isl_union_map_copy(scop
->must_writes
),
462 may_source
, isl_union_map_copy(scop
->schedule
),
463 &dep1
, &dep2
, NULL
, NULL
);
465 scop
->dep_false
= isl_union_map_union(dep1
, dep2
);
466 scop
->dep_false
= isl_union_map_coalesce(scop
->dep_false
);
469 /* Eliminate dead code from ps->domain.
471 * In particular, intersect ps->domain with the (parts of) iteration
472 * domains that are needed to produce the output or for statement
473 * iterations that call functions.
475 * We start with the iteration domains that call functions
476 * and the set of iterations that last write to an array
477 * (except those that are later killed).
479 * Then we add those statement iterations that produce
480 * something needed by the "live" statements iterations.
481 * We keep doing this until no more statement iterations can be added.
482 * To ensure that the procedure terminates, we compute the affine
483 * hull of the live iterations (bounded to the original iteration
484 * domains) each time we have added extra iterations.
486 static void eliminate_dead_code(struct ppcg_scop
*ps
)
491 live
= isl_union_map_domain(isl_union_map_copy(ps
->live_out
));
492 if (!isl_union_set_is_empty(ps
->call
)) {
493 live
= isl_union_set_union(live
, isl_union_set_copy(ps
->call
));
494 live
= isl_union_set_coalesce(live
);
497 dep
= isl_union_map_copy(ps
->dep_flow
);
498 dep
= isl_union_map_reverse(dep
);
501 isl_union_set
*extra
;
503 extra
= isl_union_set_apply(isl_union_set_copy(live
),
504 isl_union_map_copy(dep
));
505 if (isl_union_set_is_subset(extra
, live
)) {
506 isl_union_set_free(extra
);
510 live
= isl_union_set_union(live
, extra
);
511 live
= isl_union_set_affine_hull(live
);
512 live
= isl_union_set_intersect(live
,
513 isl_union_set_copy(ps
->domain
));
516 isl_union_map_free(dep
);
518 ps
->domain
= isl_union_set_intersect(ps
->domain
, live
);
521 /* Intersect "set" with the set described by "str", taking the NULL
522 * string to represent the universal set.
524 static __isl_give isl_set
*set_intersect_str(__isl_take isl_set
*set
,
533 ctx
= isl_set_get_ctx(set
);
534 set2
= isl_set_read_from_str(ctx
, str
);
535 set
= isl_set_intersect(set
, set2
);
540 static void *ppcg_scop_free(struct ppcg_scop
*ps
)
545 isl_set_free(ps
->context
);
546 isl_union_set_free(ps
->domain
);
547 isl_union_set_free(ps
->call
);
548 isl_union_map_free(ps
->tagged_reads
);
549 isl_union_map_free(ps
->reads
);
550 isl_union_map_free(ps
->live_in
);
551 isl_union_map_free(ps
->tagged_may_writes
);
552 isl_union_map_free(ps
->tagged_must_writes
);
553 isl_union_map_free(ps
->may_writes
);
554 isl_union_map_free(ps
->must_writes
);
555 isl_union_map_free(ps
->live_out
);
556 isl_union_map_free(ps
->tagged_must_kills
);
557 isl_union_map_free(ps
->tagged_dep_flow
);
558 isl_union_map_free(ps
->dep_flow
);
559 isl_union_map_free(ps
->dep_false
);
560 isl_union_map_free(ps
->dep_external
);
561 isl_union_map_free(ps
->tagged_dep_order
);
562 isl_union_map_free(ps
->dep_order
);
563 isl_union_map_free(ps
->schedule
);
564 isl_union_map_free(ps
->tagger
);
571 /* Extract a ppcg_scop from a pet_scop.
573 * The constructed ppcg_scop refers to elements from the pet_scop
574 * so the pet_scop should not be freed before the ppcg_scop.
576 static struct ppcg_scop
*ppcg_scop_from_pet_scop(struct pet_scop
*scop
,
577 struct ppcg_options
*options
)
580 struct ppcg_scop
*ps
;
585 ctx
= isl_set_get_ctx(scop
->context
);
587 ps
= isl_calloc_type(ctx
, struct ppcg_scop
);
591 ps
->options
= options
;
592 ps
->start
= scop
->start
;
594 ps
->context
= isl_set_copy(scop
->context
);
595 ps
->context
= set_intersect_str(ps
->context
, options
->ctx
);
596 ps
->domain
= collect_non_kill_domains(scop
);
597 ps
->call
= collect_call_domains(scop
);
598 ps
->tagged_reads
= pet_scop_collect_tagged_may_reads(scop
);
599 ps
->reads
= pet_scop_collect_may_reads(scop
);
600 ps
->tagged_may_writes
= pet_scop_collect_tagged_may_writes(scop
);
601 ps
->may_writes
= pet_scop_collect_may_writes(scop
);
602 ps
->tagged_must_writes
= pet_scop_collect_tagged_must_writes(scop
);
603 ps
->must_writes
= pet_scop_collect_must_writes(scop
);
604 ps
->tagged_must_kills
= pet_scop_collect_tagged_must_kills(scop
);
605 ps
->schedule
= pet_scop_collect_schedule(scop
);
606 ps
->n_type
= scop
->n_type
;
607 ps
->types
= scop
->types
;
608 ps
->n_array
= scop
->n_array
;
609 ps
->arrays
= scop
->arrays
;
610 ps
->n_stmt
= scop
->n_stmt
;
611 ps
->stmts
= scop
->stmts
;
614 compute_dependences(ps
);
615 eliminate_dead_code(ps
);
617 if (!ps
->context
|| !ps
->domain
|| !ps
->call
|| !ps
->reads
||
618 !ps
->may_writes
|| !ps
->must_writes
|| !ps
->tagged_must_kills
||
620 return ppcg_scop_free(ps
);
625 /* Internal data structure for ppcg_transform.
627 struct ppcg_transform_data
{
628 struct ppcg_options
*options
;
629 __isl_give isl_printer
*(*transform
)(__isl_take isl_printer
*p
,
630 struct ppcg_scop
*scop
, void *user
);
634 /* Callback for pet_transform_C_source that transforms
635 * the given pet_scop to a ppcg_scop before calling the
636 * ppcg_transform callback.
638 static __isl_give isl_printer
*transform(__isl_take isl_printer
*p
,
639 struct pet_scop
*scop
, void *user
)
641 struct ppcg_transform_data
*data
= user
;
642 struct ppcg_scop
*ps
;
644 if (pet_scop_has_data_dependent_conditions(scop
)) {
645 p
= pet_scop_print_original(scop
, p
);
650 scop
= pet_scop_align_params(scop
);
651 ps
= ppcg_scop_from_pet_scop(scop
, data
->options
);
653 p
= data
->transform(p
, ps
, data
->user
);
661 /* Transform the C source file "input" by rewriting each scop
662 * through a call to "transform".
663 * The transformed C code is written to "out".
665 * This is a wrapper around pet_transform_C_source that transforms
666 * the pet_scop to a ppcg_scop before calling "fn".
668 int ppcg_transform(isl_ctx
*ctx
, const char *input
, FILE *out
,
669 struct ppcg_options
*options
,
670 __isl_give isl_printer
*(*fn
)(__isl_take isl_printer
*p
,
671 struct ppcg_scop
*scop
, void *user
), void *user
)
673 struct ppcg_transform_data data
= { options
, fn
, user
};
674 return pet_transform_C_source(ctx
, input
, out
, &transform
, &data
);
677 /* Check consistency of options.
679 * Return -1 on error.
681 static int check_options(isl_ctx
*ctx
)
683 struct options
*options
;
685 options
= isl_ctx_peek_options(ctx
, &options_args
);
687 isl_die(ctx
, isl_error_internal
,
688 "unable to find options", return -1);
690 if (options
->ppcg
->openmp
&&
691 !isl_options_get_ast_build_atomic_upper_bound(ctx
))
692 isl_die(ctx
, isl_error_invalid
,
693 "OpenMP requires atomic bounds", return -1);
698 int main(int argc
, char **argv
)
702 struct options
*options
;
704 options
= options_new_with_defaults();
707 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
708 isl_options_set_schedule_outer_coincidence(ctx
, 1);
709 isl_options_set_schedule_maximize_band_depth(ctx
, 1);
710 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
712 if (check_options(ctx
) < 0)
714 else if (options
->ppcg
->target
== PPCG_TARGET_CUDA
)
715 r
= generate_cuda(ctx
, options
->ppcg
, options
->input
);
716 else if (options
->ppcg
->target
== PPCG_TARGET_OPENCL
)
717 r
= generate_opencl(ctx
, options
->ppcg
, options
->input
,
720 r
= generate_cpu(ctx
, options
->ppcg
, options
->input
,