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,
17 #include <isl/options.h>
18 #include <isl/schedule.h>
19 #include <isl/ast_build.h>
20 #include <isl/schedule.h>
23 #include "ppcg_options.h"
28 struct isl_options
*isl
;
29 struct pet_options
*pet
;
30 struct ppcg_options
*ppcg
;
35 const char *ppcg_version(void);
36 static void print_version(void)
38 printf("%s", ppcg_version());
41 ISL_ARGS_START(struct options
, options_args
)
42 ISL_ARG_CHILD(struct options
, isl
, "isl", &isl_options_args
, "isl options")
43 ISL_ARG_CHILD(struct options
, pet
, "pet", &pet_options_args
, "pet options")
44 ISL_ARG_CHILD(struct options
, ppcg
, NULL
, &ppcg_options_args
, "ppcg options")
45 ISL_ARG_STR(struct options
, output
, 'o', NULL
,
46 "filename", NULL
, "output filename (c target)")
47 ISL_ARG_ARG(struct options
, input
, "input", NULL
)
48 ISL_ARG_VERSION(print_version
)
51 ISL_ARG_DEF(options
, struct options
, options_args
)
53 /* Copy the base name of "input" to "name" and return its length.
54 * "name" is not NULL terminated.
56 * In particular, remove all leading directory components and
57 * the final extension, if any.
59 int ppcg_extract_base_name(char *name
, const char *input
)
65 base
= strrchr(input
, '/');
70 ext
= strrchr(base
, '.');
71 len
= ext
? ext
- base
: strlen(base
);
73 memcpy(name
, base
, len
);
78 /* Is "stmt" a kill statement?
80 static int is_kill(struct pet_stmt
*stmt
)
82 if (stmt
->body
->type
!= pet_expr_unary
)
84 return stmt
->body
->op
== pet_op_kill
;
87 /* Is "stmt" not a kill statement?
89 static int is_not_kill(struct pet_stmt
*stmt
)
91 return !is_kill(stmt
);
94 /* Collect the iteration domains of the statements in "scop" that
97 static __isl_give isl_union_set
*collect_domains(struct pet_scop
*scop
,
98 int (*pred
)(struct pet_stmt
*stmt
))
102 isl_union_set
*domain
;
107 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
109 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
110 struct pet_stmt
*stmt
= scop
->stmts
[i
];
116 isl_die(isl_union_set_get_ctx(domain
),
117 isl_error_unsupported
,
118 "data dependent conditions not supported",
119 return isl_union_set_free(domain
));
121 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
122 domain
= isl_union_set_add_set(domain
, domain_i
);
128 /* Collect the iteration domains of the statements in "scop",
129 * skipping kill statements.
131 static __isl_give isl_union_set
*collect_non_kill_domains(struct pet_scop
*scop
)
133 return collect_domains(scop
, &is_not_kill
);
136 /* Does "expr" contain any call expressions?
138 static int expr_has_call(struct pet_expr
*expr
)
142 if (expr
->type
== pet_expr_call
)
145 for (i
= 0; i
< expr
->n_arg
; ++i
)
146 if (expr_has_call(expr
->args
[i
]))
152 /* Does "stmt" contain any call expressions?
154 static int has_call(struct pet_stmt
*stmt
)
156 return expr_has_call(stmt
->body
);
159 /* Collect the iteration domains of the statements in "scop"
160 * that contain a call expression.
162 static __isl_give isl_union_set
*collect_call_domains(struct pet_scop
*scop
)
164 return collect_domains(scop
, &has_call
);
167 /* Collect all kill accesses in "scop".
169 static __isl_give isl_union_map
*collect_kills(struct pet_scop
*scop
)
172 isl_union_map
*kills
;
177 kills
= isl_union_map_empty(isl_set_get_space(scop
->context
));
179 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
180 struct pet_stmt
*stmt
= scop
->stmts
[i
];
185 kill_i
= isl_map_copy(stmt
->body
->args
[0]->acc
.access
);
186 kill_i
= isl_map_intersect_domain(kill_i
,
187 isl_set_copy(stmt
->domain
));
188 kills
= isl_union_map_add_map(kills
, kill_i
);
194 /* Compute the dependences of the program represented by "scop".
195 * Store the computed flow dependences
196 * in scop->dep_flow and the reads with no corresponding writes in
198 * Store the false (anti and output) dependences in scop->dep_false.
200 static void compute_dependences(struct ppcg_scop
*scop
)
202 isl_union_map
*empty
;
203 isl_union_map
*dep1
, *dep2
;
208 empty
= isl_union_map_empty(isl_union_set_get_space(scop
->domain
));
209 isl_union_map_compute_flow(isl_union_map_copy(scop
->reads
),
210 isl_union_map_copy(scop
->writes
), empty
,
211 isl_union_map_copy(scop
->schedule
),
212 &scop
->dep_flow
, NULL
, &scop
->live_in
, NULL
);
214 isl_union_map_compute_flow(isl_union_map_copy(scop
->writes
),
215 isl_union_map_copy(scop
->writes
),
216 isl_union_map_copy(scop
->reads
),
217 isl_union_map_copy(scop
->schedule
),
218 &dep1
, &dep2
, NULL
, NULL
);
220 scop
->dep_false
= isl_union_map_union(dep1
, dep2
);
221 scop
->dep_false
= isl_union_map_coalesce(scop
->dep_false
);
224 /* Eliminate dead code from ps->domain.
226 * In particular, intersect ps->domain with the (parts of) iteration
227 * domains that are needed to produce the output or for statement
228 * iterations that call functions.
230 * We start with the iteration domains that call functions
231 * and the set of iterations that last write to an array
232 * (except those that are later killed).
234 * Then we add those statement iterations that produce
235 * something needed by the "live" statements iterations.
236 * We keep doing this until no more statement iterations can be added.
237 * To ensure that the procedure terminates, we compute the affine
238 * hull of the live iterations (bounded to the original iteration
239 * domains) each time we have added extra iterations.
241 static void eliminate_dead_code(struct ppcg_scop
*ps
)
243 isl_union_map
*exposed
;
247 exposed
= isl_union_map_union(isl_union_map_copy(ps
->writes
),
248 isl_union_map_copy(ps
->kills
));
249 exposed
= isl_union_map_reverse(exposed
);
250 exposed
= isl_union_map_apply_range(exposed
,
251 isl_union_map_copy(ps
->schedule
));
252 exposed
= isl_union_map_lexmax(exposed
);
253 exposed
= isl_union_map_apply_range(exposed
,
254 isl_union_map_reverse(isl_union_map_copy(ps
->schedule
)));
256 live
= isl_union_map_range(exposed
);
257 if (!isl_union_set_is_empty(ps
->call
)) {
258 live
= isl_union_set_union(live
, isl_union_set_copy(ps
->call
));
259 live
= isl_union_set_coalesce(live
);
262 dep
= isl_union_map_copy(ps
->dep_flow
);
263 dep
= isl_union_map_reverse(dep
);
266 isl_union_set
*extra
;
268 extra
= isl_union_set_apply(isl_union_set_copy(live
),
269 isl_union_map_copy(dep
));
270 if (isl_union_set_is_subset(extra
, live
)) {
271 isl_union_set_free(extra
);
275 live
= isl_union_set_union(live
, extra
);
276 live
= isl_union_set_affine_hull(live
);
277 live
= isl_union_set_intersect(live
,
278 isl_union_set_copy(ps
->domain
));
281 isl_union_map_free(dep
);
283 ps
->domain
= isl_union_set_intersect(ps
->domain
, live
);
286 /* Intersect "set" with the set described by "str", taking the NULL
287 * string to represent the universal set.
289 static __isl_give isl_set
*set_intersect_str(__isl_take isl_set
*set
,
298 ctx
= isl_set_get_ctx(set
);
299 set2
= isl_set_read_from_str(ctx
, str
);
300 set
= isl_set_intersect(set
, set2
);
305 /* Does "expr" involve any data dependent accesses?
307 static int expr_has_data_dependent_accesses(struct pet_expr
*expr
)
311 for (i
= 0; i
< expr
->n_arg
; ++i
)
312 if (expr_has_data_dependent_accesses(expr
->args
[i
]))
315 if (expr
->type
== pet_expr_access
&& expr
->n_arg
> 0)
321 /* Does "stmt" contain any data dependent accesses?
323 static int stmt_has_data_dependent_accesses(struct pet_stmt
*stmt
)
325 return expr_has_data_dependent_accesses(stmt
->body
);
328 /* Does "scop" contain any data dependent accesses?
330 static int scop_has_data_dependent_accesses(struct pet_scop
*scop
)
336 for (i
= 0; i
< scop
->n_stmt
; ++i
)
337 if (stmt_has_data_dependent_accesses(scop
->stmts
[i
]))
343 static void *ppcg_scop_free(struct ppcg_scop
*ps
)
348 isl_set_free(ps
->context
);
349 isl_union_set_free(ps
->domain
);
350 isl_union_set_free(ps
->call
);
351 isl_union_map_free(ps
->reads
);
352 isl_union_map_free(ps
->live_in
);
353 isl_union_map_free(ps
->writes
);
354 isl_union_map_free(ps
->kills
);
355 isl_union_map_free(ps
->dep_flow
);
356 isl_union_map_free(ps
->dep_false
);
357 isl_union_map_free(ps
->schedule
);
364 /* Extract a ppcg_scop from a pet_scop.
366 * The constructed ppcg_scop refers to elements from the pet_scop
367 * so the pet_scop should not be freed before the ppcg_scop.
369 static struct ppcg_scop
*ppcg_scop_from_pet_scop(struct pet_scop
*scop
,
370 struct ppcg_options
*options
)
373 struct ppcg_scop
*ps
;
378 ctx
= isl_set_get_ctx(scop
->context
);
380 if (scop_has_data_dependent_accesses(scop
))
381 isl_die(ctx
, isl_error_unsupported
,
382 "data dependent accesses not supported",
385 ps
= isl_calloc_type(ctx
, struct ppcg_scop
);
389 ps
->start
= scop
->start
;
391 ps
->context
= isl_set_copy(scop
->context
);
392 ps
->context
= set_intersect_str(ps
->context
, options
->ctx
);
393 ps
->domain
= collect_non_kill_domains(scop
);
394 ps
->call
= collect_call_domains(scop
);
395 ps
->reads
= pet_scop_collect_reads(scop
);
396 ps
->writes
= pet_scop_collect_writes(scop
);
397 ps
->kills
= collect_kills(scop
);
398 ps
->schedule
= pet_scop_collect_schedule(scop
);
399 ps
->n_array
= scop
->n_array
;
400 ps
->arrays
= scop
->arrays
;
401 ps
->n_stmt
= scop
->n_stmt
;
402 ps
->stmts
= scop
->stmts
;
404 compute_dependences(ps
);
405 eliminate_dead_code(ps
);
407 if (!ps
->context
|| !ps
->domain
|| !ps
->call
|| !ps
->reads
||
408 !ps
->writes
|| !ps
->kills
|| !ps
->schedule
)
409 return ppcg_scop_free(ps
);
414 /* Check consistency of options.
416 * Return -1 on error.
418 static int check_options(isl_ctx
*ctx
)
420 struct options
*options
;
422 options
= isl_ctx_peek_options(ctx
, &options_args
);
424 isl_die(ctx
, isl_error_internal
,
425 "unable to find options", return -1);
427 if (options
->ppcg
->openmp
&&
428 !isl_options_get_ast_build_atomic_upper_bound(ctx
))
429 isl_die(ctx
, isl_error_invalid
,
430 "OpenMP requires atomic bounds", return -1);
435 /* Extract a model from the input file, transform the model and
436 * write out the result to the output file(s).
438 static int transform(isl_ctx
*ctx
)
441 struct pet_scop
*scop
;
442 struct ppcg_scop
*ps
;
443 struct options
*options
;
445 options
= isl_ctx_peek_options(ctx
, &options_args
);
447 isl_die(ctx
, isl_error_internal
,
448 "unable to find options", return EXIT_FAILURE
);
450 scop
= pet_scop_extract_from_C_source(ctx
, options
->input
, NULL
);
451 scop
= pet_scop_align_params(scop
);
452 ps
= ppcg_scop_from_pet_scop(scop
, options
->ppcg
);
454 if (options
->ppcg
->target
== PPCG_TARGET_CUDA
)
455 r
= generate_cuda(ctx
, ps
, options
->ppcg
, options
->input
);
457 r
= generate_cpu(ctx
, ps
, options
->ppcg
, options
->input
,
466 int main(int argc
, char **argv
)
470 struct options
*options
;
472 options
= options_new_with_defaults();
475 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
476 isl_options_set_schedule_outer_zero_distance(ctx
, 1);
477 isl_options_set_schedule_maximize_band_depth(ctx
, 1);
478 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
480 if (check_options(ctx
) < 0)