2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
16 #include <isl/options.h>
17 #include <isl/schedule.h>
18 #include <isl/ast_build.h>
19 #include <isl/schedule.h>
22 #include "ppcg_options.h"
27 struct isl_options
*isl
;
28 struct pet_options
*pet
;
29 struct ppcg_options
*ppcg
;
34 const char *ppcg_version(void);
35 static void print_version(void)
37 printf("%s", ppcg_version());
40 ISL_ARGS_START(struct options
, options_args
)
41 ISL_ARG_CHILD(struct options
, isl
, "isl", &isl_options_args
, "isl options")
42 ISL_ARG_CHILD(struct options
, pet
, "pet", &pet_options_args
, "pet options")
43 ISL_ARG_CHILD(struct options
, ppcg
, NULL
, &ppcg_options_args
, "ppcg options")
44 ISL_ARG_STR(struct options
, output
, 'o', NULL
,
45 "filename", NULL
, "output filename (c target)")
46 ISL_ARG_ARG(struct options
, input
, "input", NULL
)
47 ISL_ARG_VERSION(print_version
)
50 ISL_ARG_DEF(options
, struct options
, options_args
)
52 /* Is "stmt" a kill statement?
54 static int is_kill(struct pet_stmt
*stmt
)
56 if (stmt
->body
->type
!= pet_expr_unary
)
58 return stmt
->body
->op
== pet_op_kill
;
61 /* Is "stmt" not a kill statement?
63 static int is_not_kill(struct pet_stmt
*stmt
)
65 return !is_kill(stmt
);
68 /* Collect the iteration domains of the statements in "scop" that
71 static __isl_give isl_union_set
*collect_domains(struct pet_scop
*scop
,
72 int (*pred
)(struct pet_stmt
*stmt
))
76 isl_union_set
*domain
;
81 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
83 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
84 struct pet_stmt
*stmt
= scop
->stmts
[i
];
88 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
89 domain
= isl_union_set_add_set(domain
, domain_i
);
95 /* Collect the iteration domains of the statements in "scop",
96 * skipping kill statements.
98 static __isl_give isl_union_set
*collect_non_kill_domains(struct pet_scop
*scop
)
100 return collect_domains(scop
, &is_not_kill
);
103 /* Does "expr" contain any call expressions?
105 static int expr_has_call(struct pet_expr
*expr
)
109 if (expr
->type
== pet_expr_call
)
112 for (i
= 0; i
< expr
->n_arg
; ++i
)
113 if (expr_has_call(expr
->args
[i
]))
119 /* Does "stmt" contain any call expressions?
121 static int has_call(struct pet_stmt
*stmt
)
123 return expr_has_call(stmt
->body
);
126 /* Collect the iteration domains of the statements in "scop"
127 * that contain a call expression.
129 static __isl_give isl_union_set
*collect_call_domains(struct pet_scop
*scop
)
131 return collect_domains(scop
, &has_call
);
134 /* Collect all kill accesses in "scop".
136 static __isl_give isl_union_map
*collect_kills(struct pet_scop
*scop
)
139 isl_union_map
*kills
;
144 kills
= isl_union_map_empty(isl_set_get_space(scop
->context
));
146 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
147 struct pet_stmt
*stmt
= scop
->stmts
[i
];
152 kill_i
= isl_map_copy(stmt
->body
->args
[0]->acc
.access
);
153 kill_i
= isl_map_intersect_domain(kill_i
,
154 isl_set_copy(stmt
->domain
));
155 kills
= isl_union_map_add_map(kills
, kill_i
);
161 /* Compute the dependences of the program represented by "scop".
162 * Store the computed flow dependences
163 * in scop->dep_flow and the reads with no corresponding writes in
165 * Store the false (anti and output) dependences in scop->dep_false.
167 static void compute_dependences(struct ppcg_scop
*scop
)
169 isl_union_map
*empty
;
170 isl_union_map
*dep1
, *dep2
;
175 empty
= isl_union_map_empty(isl_union_set_get_space(scop
->domain
));
176 isl_union_map_compute_flow(isl_union_map_copy(scop
->reads
),
177 isl_union_map_copy(scop
->writes
), empty
,
178 isl_union_map_copy(scop
->schedule
),
179 &scop
->dep_flow
, NULL
, &scop
->live_in
, NULL
);
181 isl_union_map_compute_flow(isl_union_map_copy(scop
->writes
),
182 isl_union_map_copy(scop
->writes
),
183 isl_union_map_copy(scop
->reads
),
184 isl_union_map_copy(scop
->schedule
),
185 &dep1
, &dep2
, NULL
, NULL
);
187 scop
->dep_false
= isl_union_map_union(dep1
, dep2
);
188 scop
->dep_false
= isl_union_map_coalesce(scop
->dep_false
);
191 /* Eliminate dead code from ps->domain.
193 * In particular, intersect ps->domain with the (parts of) iteration
194 * domains that are needed to produce the output or for statement
195 * iterations that call functions.
197 * We start with the iteration domains that call functions
198 * and the set of iterations that last write to an array
199 * (except those that are later killed).
201 * Then we add those statement iterations that produce
202 * something needed by the "live" statements iterations.
203 * We keep doing this until no more statement iterations can be added.
204 * To ensure that the procedure terminates, we compute the affine
205 * hull of the live iterations (bounded to the original iteration
206 * domains) each time we have added extra iterations.
208 static void eliminate_dead_code(struct ppcg_scop
*ps
)
210 isl_union_map
*exposed
;
214 exposed
= isl_union_map_union(isl_union_map_copy(ps
->writes
),
215 isl_union_map_copy(ps
->kills
));
216 exposed
= isl_union_map_reverse(exposed
);
217 exposed
= isl_union_map_apply_range(exposed
,
218 isl_union_map_copy(ps
->schedule
));
219 exposed
= isl_union_map_lexmax(exposed
);
220 exposed
= isl_union_map_apply_range(exposed
,
221 isl_union_map_reverse(isl_union_map_copy(ps
->schedule
)));
223 live
= isl_union_map_range(exposed
);
224 if (!isl_union_set_is_empty(ps
->call
)) {
225 live
= isl_union_set_union(live
, isl_union_set_copy(ps
->call
));
226 live
= isl_union_set_coalesce(live
);
229 dep
= isl_union_map_copy(ps
->dep_flow
);
230 dep
= isl_union_map_reverse(dep
);
233 isl_union_set
*extra
;
235 extra
= isl_union_set_apply(isl_union_set_copy(live
),
236 isl_union_map_copy(dep
));
237 if (isl_union_set_is_subset(extra
, live
)) {
238 isl_union_set_free(extra
);
242 live
= isl_union_set_union(live
, extra
);
243 live
= isl_union_set_affine_hull(live
);
244 live
= isl_union_set_intersect(live
,
245 isl_union_set_copy(ps
->domain
));
248 isl_union_map_free(dep
);
250 ps
->domain
= isl_union_set_intersect(ps
->domain
, live
);
253 /* Intersect "set" with the set described by "str", taking the NULL
254 * string to represent the universal set.
256 static __isl_give isl_set
*set_intersect_str(__isl_take isl_set
*set
,
265 ctx
= isl_set_get_ctx(set
);
266 set2
= isl_set_read_from_str(ctx
, str
);
267 set
= isl_set_intersect(set
, set2
);
272 /* Does "expr" involve any data dependent accesses?
274 static int expr_has_data_dependent_accesses(struct pet_expr
*expr
)
278 for (i
= 0; i
< expr
->n_arg
; ++i
)
279 if (expr_has_data_dependent_accesses(expr
->args
[i
]))
282 if (expr
->type
== pet_expr_access
&& expr
->n_arg
> 0)
288 /* Does "stmt" contain any data dependent accesses?
290 static int stmt_has_data_dependent_accesses(struct pet_stmt
*stmt
)
292 return expr_has_data_dependent_accesses(stmt
->body
);
295 /* Does "scop" contain any data dependent accesses?
297 static int scop_has_data_dependent_accesses(struct pet_scop
*scop
)
303 for (i
= 0; i
< scop
->n_stmt
; ++i
)
304 if (stmt_has_data_dependent_accesses(scop
->stmts
[i
]))
310 /* Extract a ppcg_scop from a pet_scop.
312 * The constructed ppcg_scop refers to elements from the pet_scop
313 * so the pet_scop should not be freed before the ppcg_scop.
315 static struct ppcg_scop
*ppcg_scop_from_pet_scop(struct pet_scop
*scop
,
316 struct ppcg_options
*options
)
319 struct ppcg_scop
*ps
;
324 ctx
= isl_set_get_ctx(scop
->context
);
326 if (scop_has_data_dependent_accesses(scop
))
327 isl_die(ctx
, isl_error_unsupported
,
328 "data dependent accesses not supported",
331 ps
= isl_calloc_type(ctx
, struct ppcg_scop
);
335 ps
->start
= scop
->start
;
337 ps
->context
= isl_set_copy(scop
->context
);
338 ps
->context
= set_intersect_str(ps
->context
, options
->ctx
);
339 ps
->domain
= collect_non_kill_domains(scop
);
340 ps
->call
= collect_call_domains(scop
);
341 ps
->reads
= pet_scop_collect_reads(scop
);
342 ps
->writes
= pet_scop_collect_writes(scop
);
343 ps
->kills
= collect_kills(scop
);
344 ps
->schedule
= pet_scop_collect_schedule(scop
);
345 ps
->n_array
= scop
->n_array
;
346 ps
->arrays
= scop
->arrays
;
347 ps
->n_stmt
= scop
->n_stmt
;
348 ps
->stmts
= scop
->stmts
;
350 compute_dependences(ps
);
351 eliminate_dead_code(ps
);
356 static void ppcg_scop_free(struct ppcg_scop
*ps
)
361 isl_set_free(ps
->context
);
362 isl_union_set_free(ps
->domain
);
363 isl_union_set_free(ps
->call
);
364 isl_union_map_free(ps
->reads
);
365 isl_union_map_free(ps
->live_in
);
366 isl_union_map_free(ps
->writes
);
367 isl_union_map_free(ps
->kills
);
368 isl_union_map_free(ps
->dep_flow
);
369 isl_union_map_free(ps
->dep_false
);
370 isl_union_map_free(ps
->schedule
);
375 /* Check consistency of options.
377 * Return -1 on error.
379 static int check_options(isl_ctx
*ctx
)
381 struct options
*options
;
383 options
= isl_ctx_peek_options(ctx
, &options_args
);
385 isl_die(ctx
, isl_error_internal
,
386 "unable to find options", return -1);
388 if (options
->ppcg
->openmp
&&
389 !isl_options_get_ast_build_atomic_upper_bound(ctx
))
390 isl_die(ctx
, isl_error_invalid
,
391 "OpenMP requires atomic bounds", return -1);
396 /* Extract a model from the input file, transform the model and
397 * write out the result to the output file(s).
399 static int transform(isl_ctx
*ctx
)
402 struct pet_scop
*scop
;
403 struct ppcg_scop
*ps
;
404 struct options
*options
;
406 options
= isl_ctx_peek_options(ctx
, &options_args
);
408 isl_die(ctx
, isl_error_internal
,
409 "unable to find options", return EXIT_FAILURE
);
411 scop
= pet_scop_extract_from_C_source(ctx
, options
->input
, NULL
);
412 scop
= pet_scop_align_params(scop
);
413 ps
= ppcg_scop_from_pet_scop(scop
, options
->ppcg
);
415 if (options
->ppcg
->target
== PPCG_TARGET_CUDA
)
416 r
= generate_cuda(ctx
, ps
, options
->ppcg
, options
->input
);
418 r
= generate_cpu(ctx
, ps
, options
->ppcg
, options
->input
,
427 int main(int argc
, char **argv
)
431 struct options
*options
;
433 options
= options_new_with_defaults();
436 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
437 isl_options_set_schedule_outer_zero_distance(ctx
, 1);
438 isl_options_set_schedule_maximize_band_depth(ctx
, 1);
439 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
441 if (check_options(ctx
) < 0)