2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 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 /* Extract a ppcg_scop from a pet_scop.
274 * The constructed ppcg_scop refers to elements from the pet_scop
275 * so the pet_scop should not be freed before the ppcg_scop.
277 static struct ppcg_scop
*ppcg_scop_from_pet_scop(struct pet_scop
*scop
,
278 struct ppcg_options
*options
)
281 struct ppcg_scop
*ps
;
286 ctx
= isl_set_get_ctx(scop
->context
);
288 ps
= isl_calloc_type(ctx
, struct ppcg_scop
);
292 ps
->start
= scop
->start
;
294 ps
->context
= isl_set_copy(scop
->context
);
295 ps
->context
= set_intersect_str(ps
->context
, options
->ctx
);
296 ps
->domain
= collect_non_kill_domains(scop
);
297 ps
->call
= collect_call_domains(scop
);
298 ps
->reads
= pet_scop_collect_reads(scop
);
299 ps
->writes
= pet_scop_collect_writes(scop
);
300 ps
->kills
= collect_kills(scop
);
301 ps
->schedule
= pet_scop_collect_schedule(scop
);
302 ps
->n_array
= scop
->n_array
;
303 ps
->arrays
= scop
->arrays
;
304 ps
->n_stmt
= scop
->n_stmt
;
305 ps
->stmts
= scop
->stmts
;
307 compute_dependences(ps
);
308 eliminate_dead_code(ps
);
313 static void ppcg_scop_free(struct ppcg_scop
*ps
)
318 isl_set_free(ps
->context
);
319 isl_union_set_free(ps
->domain
);
320 isl_union_set_free(ps
->call
);
321 isl_union_map_free(ps
->reads
);
322 isl_union_map_free(ps
->live_in
);
323 isl_union_map_free(ps
->writes
);
324 isl_union_map_free(ps
->kills
);
325 isl_union_map_free(ps
->dep_flow
);
326 isl_union_map_free(ps
->dep_false
);
327 isl_union_map_free(ps
->schedule
);
332 /* Check consistency of options.
334 * Return -1 on error.
336 static int check_options(isl_ctx
*ctx
)
338 struct options
*options
;
340 options
= isl_ctx_peek_options(ctx
, &options_args
);
342 isl_die(ctx
, isl_error_internal
,
343 "unable to find options", return -1);
345 if (options
->ppcg
->openmp
&&
346 !isl_options_get_ast_build_atomic_upper_bound(ctx
))
347 isl_die(ctx
, isl_error_invalid
,
348 "OpenMP requires atomic bounds", return -1);
353 /* Extract a model from the input file, transform the model and
354 * write out the result to the output file(s).
356 static int transform(isl_ctx
*ctx
)
359 struct pet_scop
*scop
;
360 struct ppcg_scop
*ps
;
361 struct options
*options
;
363 options
= isl_ctx_peek_options(ctx
, &options_args
);
365 isl_die(ctx
, isl_error_internal
,
366 "unable to find options", return EXIT_FAILURE
);
368 scop
= pet_scop_extract_from_C_source(ctx
, options
->input
, NULL
);
369 scop
= pet_scop_align_params(scop
);
370 ps
= ppcg_scop_from_pet_scop(scop
, options
->ppcg
);
372 if (options
->ppcg
->target
== PPCG_TARGET_CUDA
)
373 r
= generate_cuda(ctx
, ps
, options
->ppcg
, options
->input
);
375 r
= generate_cpu(ctx
, ps
, options
->ppcg
, options
->input
,
384 int main(int argc
, char **argv
)
388 struct options
*options
;
390 options
= options_new_with_defaults();
393 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
394 isl_options_set_schedule_outer_zero_distance(ctx
, 1);
395 isl_options_set_schedule_maximize_band_depth(ctx
, 1);
396 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
398 if (check_options(ctx
) < 0)