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 /* Compute the flow dependences and the live in accesses.
169 static void compute_flow_dep(struct ppcg_scop
*ps
)
171 isl_union_map
*empty
;
173 empty
= isl_union_map_empty(isl_union_set_get_space(ps
->domain
));
174 isl_union_map_compute_flow(isl_union_map_copy(ps
->reads
),
175 isl_union_map_copy(ps
->writes
), empty
,
176 isl_union_map_copy(ps
->schedule
),
177 &ps
->dep_flow
, NULL
, &ps
->live_in
, NULL
);
180 /* Compute the dependences of the program represented by "scop".
181 * Store the computed flow dependences
182 * in scop->dep_flow and the reads with no corresponding writes in
184 * Store the false (anti and output) dependences in scop->dep_false.
186 static void compute_dependences(struct ppcg_scop
*scop
)
188 isl_union_map
*dep1
, *dep2
;
193 compute_flow_dep(scop
);
195 isl_union_map_compute_flow(isl_union_map_copy(scop
->writes
),
196 isl_union_map_copy(scop
->writes
),
197 isl_union_map_copy(scop
->reads
),
198 isl_union_map_copy(scop
->schedule
),
199 &dep1
, &dep2
, NULL
, NULL
);
201 scop
->dep_false
= isl_union_map_union(dep1
, dep2
);
202 scop
->dep_false
= isl_union_map_coalesce(scop
->dep_false
);
205 /* Eliminate dead code from ps->domain.
207 * In particular, intersect ps->domain with the (parts of) iteration
208 * domains that are needed to produce the output or for statement
209 * iterations that call functions.
211 * We start with the iteration domains that call functions
212 * and the set of iterations that last write to an array
213 * (except those that are later killed).
215 * Then we add those statement iterations that produce
216 * something needed by the "live" statements iterations.
217 * We keep doing this until no more statement iterations can be added.
218 * To ensure that the procedure terminates, we compute the affine
219 * hull of the live iterations (bounded to the original iteration
220 * domains) each time we have added extra iterations.
222 static void eliminate_dead_code(struct ppcg_scop
*ps
)
224 isl_union_map
*exposed
;
228 exposed
= isl_union_map_union(isl_union_map_copy(ps
->writes
),
229 isl_union_map_copy(ps
->kills
));
230 exposed
= isl_union_map_reverse(exposed
);
231 exposed
= isl_union_map_apply_range(exposed
,
232 isl_union_map_copy(ps
->schedule
));
233 exposed
= isl_union_map_lexmax(exposed
);
234 exposed
= isl_union_map_apply_range(exposed
,
235 isl_union_map_reverse(isl_union_map_copy(ps
->schedule
)));
237 live
= isl_union_map_range(exposed
);
238 if (!isl_union_set_is_empty(ps
->call
)) {
239 live
= isl_union_set_union(live
, isl_union_set_copy(ps
->call
));
240 live
= isl_union_set_coalesce(live
);
243 dep
= isl_union_map_copy(ps
->dep_flow
);
244 dep
= isl_union_map_reverse(dep
);
247 isl_union_set
*extra
;
249 extra
= isl_union_set_apply(isl_union_set_copy(live
),
250 isl_union_map_copy(dep
));
251 if (isl_union_set_is_subset(extra
, live
)) {
252 isl_union_set_free(extra
);
256 live
= isl_union_set_union(live
, extra
);
257 live
= isl_union_set_affine_hull(live
);
258 live
= isl_union_set_intersect(live
,
259 isl_union_set_copy(ps
->domain
));
262 isl_union_map_free(dep
);
264 ps
->domain
= isl_union_set_intersect(ps
->domain
, live
);
267 /* Intersect "set" with the set described by "str", taking the NULL
268 * string to represent the universal set.
270 static __isl_give isl_set
*set_intersect_str(__isl_take isl_set
*set
,
279 ctx
= isl_set_get_ctx(set
);
280 set2
= isl_set_read_from_str(ctx
, str
);
281 set
= isl_set_intersect(set
, set2
);
286 /* Does "expr" involve any data dependent accesses?
288 static int expr_has_data_dependent_accesses(struct pet_expr
*expr
)
292 for (i
= 0; i
< expr
->n_arg
; ++i
)
293 if (expr_has_data_dependent_accesses(expr
->args
[i
]))
296 if (expr
->type
== pet_expr_access
&& expr
->n_arg
> 0)
302 /* Does "stmt" contain any data dependent accesses?
304 static int stmt_has_data_dependent_accesses(struct pet_stmt
*stmt
)
306 return expr_has_data_dependent_accesses(stmt
->body
);
309 /* Does "scop" contain any data dependent accesses?
311 static int scop_has_data_dependent_accesses(struct pet_scop
*scop
)
317 for (i
= 0; i
< scop
->n_stmt
; ++i
)
318 if (stmt_has_data_dependent_accesses(scop
->stmts
[i
]))
324 static void *ppcg_scop_free(struct ppcg_scop
*ps
)
329 isl_set_free(ps
->context
);
330 isl_union_set_free(ps
->domain
);
331 isl_union_set_free(ps
->call
);
332 isl_union_map_free(ps
->reads
);
333 isl_union_map_free(ps
->live_in
);
334 isl_union_map_free(ps
->writes
);
335 isl_union_map_free(ps
->kills
);
336 isl_union_map_free(ps
->dep_flow
);
337 isl_union_map_free(ps
->dep_false
);
338 isl_union_map_free(ps
->schedule
);
345 /* Extract a ppcg_scop from a pet_scop.
347 * The constructed ppcg_scop refers to elements from the pet_scop
348 * so the pet_scop should not be freed before the ppcg_scop.
350 static struct ppcg_scop
*ppcg_scop_from_pet_scop(struct pet_scop
*scop
,
351 struct ppcg_options
*options
)
354 struct ppcg_scop
*ps
;
359 ctx
= isl_set_get_ctx(scop
->context
);
361 if (scop_has_data_dependent_accesses(scop
))
362 isl_die(ctx
, isl_error_unsupported
,
363 "data dependent accesses not supported",
366 ps
= isl_calloc_type(ctx
, struct ppcg_scop
);
370 ps
->options
= options
;
371 ps
->start
= scop
->start
;
373 ps
->context
= isl_set_copy(scop
->context
);
374 ps
->context
= set_intersect_str(ps
->context
, options
->ctx
);
375 ps
->domain
= collect_non_kill_domains(scop
);
376 ps
->call
= collect_call_domains(scop
);
377 ps
->reads
= pet_scop_collect_may_reads(scop
);
378 ps
->writes
= pet_scop_collect_may_writes(scop
);
379 ps
->kills
= pet_scop_collect_must_kills(scop
);
380 ps
->schedule
= pet_scop_collect_schedule(scop
);
381 ps
->n_type
= scop
->n_type
;
382 ps
->types
= scop
->types
;
383 ps
->n_array
= scop
->n_array
;
384 ps
->arrays
= scop
->arrays
;
385 ps
->n_stmt
= scop
->n_stmt
;
386 ps
->stmts
= scop
->stmts
;
388 compute_dependences(ps
);
389 eliminate_dead_code(ps
);
391 if (!ps
->context
|| !ps
->domain
|| !ps
->call
|| !ps
->reads
||
392 !ps
->writes
|| !ps
->kills
|| !ps
->schedule
)
393 return ppcg_scop_free(ps
);
398 /* Internal data structure for ppcg_transform.
400 struct ppcg_transform_data
{
401 struct ppcg_options
*options
;
402 __isl_give isl_printer
*(*transform
)(__isl_take isl_printer
*p
,
403 struct ppcg_scop
*scop
, void *user
);
407 /* Callback for pet_transform_C_source that transforms
408 * the given pet_scop to a ppcg_scop before calling the
409 * ppcg_transform callback.
411 static __isl_give isl_printer
*transform(__isl_take isl_printer
*p
,
412 struct pet_scop
*scop
, void *user
)
414 struct ppcg_transform_data
*data
= user
;
415 struct ppcg_scop
*ps
;
417 if (pet_scop_has_data_dependent_accesses(scop
) ||
418 pet_scop_has_data_dependent_conditions(scop
)) {
419 p
= pet_scop_print_original(scop
, p
);
424 scop
= pet_scop_align_params(scop
);
425 ps
= ppcg_scop_from_pet_scop(scop
, data
->options
);
427 p
= data
->transform(p
, ps
, data
->user
);
435 /* Transform the C source file "input" by rewriting each scop
436 * through a call to "transform".
437 * The transformed C code is written to "out".
439 * This is a wrapper around pet_transform_C_source that transforms
440 * the pet_scop to a ppcg_scop before calling "fn".
442 int ppcg_transform(isl_ctx
*ctx
, const char *input
, FILE *out
,
443 struct ppcg_options
*options
,
444 __isl_give isl_printer
*(*fn
)(__isl_take isl_printer
*p
,
445 struct ppcg_scop
*scop
, void *user
), void *user
)
447 struct ppcg_transform_data data
= { options
, fn
, user
};
448 return pet_transform_C_source(ctx
, input
, out
, &transform
, &data
);
451 /* Check consistency of options.
453 * Return -1 on error.
455 static int check_options(isl_ctx
*ctx
)
457 struct options
*options
;
459 options
= isl_ctx_peek_options(ctx
, &options_args
);
461 isl_die(ctx
, isl_error_internal
,
462 "unable to find options", return -1);
464 if (options
->ppcg
->openmp
&&
465 !isl_options_get_ast_build_atomic_upper_bound(ctx
))
466 isl_die(ctx
, isl_error_invalid
,
467 "OpenMP requires atomic bounds", return -1);
472 int main(int argc
, char **argv
)
476 struct options
*options
;
478 options
= options_new_with_defaults();
481 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
482 isl_options_set_schedule_outer_coincidence(ctx
, 1);
483 isl_options_set_schedule_maximize_band_depth(ctx
, 1);
484 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
486 if (check_options(ctx
) < 0)
488 else if (options
->ppcg
->target
== PPCG_TARGET_CUDA
)
489 r
= generate_cuda(ctx
, options
->ppcg
, options
->input
);
491 r
= generate_cpu(ctx
, options
->ppcg
, options
->input
,