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 live out accesses, i.e., the writes that are not killed
168 * by any kills or any other writes, and store them in ps->live_out.
170 * We currently assume that all write access relations are exact.
172 static void compute_live_out(struct ppcg_scop
*ps
)
174 isl_union_map
*exposed
;
176 exposed
= isl_union_map_union(isl_union_map_copy(ps
->writes
),
177 isl_union_map_copy(ps
->kills
));
178 exposed
= isl_union_map_reverse(exposed
);
179 exposed
= isl_union_map_apply_range(exposed
,
180 isl_union_map_copy(ps
->schedule
));
181 exposed
= isl_union_map_lexmax(exposed
);
182 exposed
= isl_union_map_coalesce(exposed
);
183 exposed
= isl_union_map_reverse(exposed
);
184 exposed
= isl_union_map_apply_range(isl_union_map_copy(ps
->schedule
),
186 ps
->live_out
= exposed
;
189 /* Compute the flow dependences and the live in accesses.
191 static void compute_flow_dep(struct ppcg_scop
*ps
)
193 isl_union_map
*empty
;
195 empty
= isl_union_map_empty(isl_union_set_get_space(ps
->domain
));
196 isl_union_map_compute_flow(isl_union_map_copy(ps
->reads
),
197 isl_union_map_copy(ps
->writes
), empty
,
198 isl_union_map_copy(ps
->schedule
),
199 &ps
->dep_flow
, NULL
, &ps
->live_in
, NULL
);
202 /* Compute the dependences of the program represented by "scop".
203 * Store the computed flow dependences
204 * in scop->dep_flow and the reads with no corresponding writes in
206 * Store the live out accesses in scop->live_out.
207 * Store the false (anti and output) dependences in scop->dep_false.
209 static void compute_dependences(struct ppcg_scop
*scop
)
211 isl_union_map
*dep1
, *dep2
;
216 compute_live_out(scop
);
218 compute_flow_dep(scop
);
220 isl_union_map_compute_flow(isl_union_map_copy(scop
->writes
),
221 isl_union_map_copy(scop
->writes
),
222 isl_union_map_copy(scop
->reads
),
223 isl_union_map_copy(scop
->schedule
),
224 &dep1
, &dep2
, NULL
, NULL
);
226 scop
->dep_false
= isl_union_map_union(dep1
, dep2
);
227 scop
->dep_false
= isl_union_map_coalesce(scop
->dep_false
);
230 /* Eliminate dead code from ps->domain.
232 * In particular, intersect ps->domain with the (parts of) iteration
233 * domains that are needed to produce the output or for statement
234 * iterations that call functions.
236 * We start with the iteration domains that call functions
237 * and the set of iterations that last write to an array
238 * (except those that are later killed).
240 * Then we add those statement iterations that produce
241 * something needed by the "live" statements iterations.
242 * We keep doing this until no more statement iterations can be added.
243 * To ensure that the procedure terminates, we compute the affine
244 * hull of the live iterations (bounded to the original iteration
245 * domains) each time we have added extra iterations.
247 static void eliminate_dead_code(struct ppcg_scop
*ps
)
252 live
= isl_union_map_domain(isl_union_map_copy(ps
->live_out
));
253 if (!isl_union_set_is_empty(ps
->call
)) {
254 live
= isl_union_set_union(live
, isl_union_set_copy(ps
->call
));
255 live
= isl_union_set_coalesce(live
);
258 dep
= isl_union_map_copy(ps
->dep_flow
);
259 dep
= isl_union_map_reverse(dep
);
262 isl_union_set
*extra
;
264 extra
= isl_union_set_apply(isl_union_set_copy(live
),
265 isl_union_map_copy(dep
));
266 if (isl_union_set_is_subset(extra
, live
)) {
267 isl_union_set_free(extra
);
271 live
= isl_union_set_union(live
, extra
);
272 live
= isl_union_set_affine_hull(live
);
273 live
= isl_union_set_intersect(live
,
274 isl_union_set_copy(ps
->domain
));
277 isl_union_map_free(dep
);
279 ps
->domain
= isl_union_set_intersect(ps
->domain
, live
);
282 /* Intersect "set" with the set described by "str", taking the NULL
283 * string to represent the universal set.
285 static __isl_give isl_set
*set_intersect_str(__isl_take isl_set
*set
,
294 ctx
= isl_set_get_ctx(set
);
295 set2
= isl_set_read_from_str(ctx
, str
);
296 set
= isl_set_intersect(set
, set2
);
301 /* Does "expr" involve any data dependent accesses?
303 static int expr_has_data_dependent_accesses(struct pet_expr
*expr
)
307 for (i
= 0; i
< expr
->n_arg
; ++i
)
308 if (expr_has_data_dependent_accesses(expr
->args
[i
]))
311 if (expr
->type
== pet_expr_access
&& expr
->n_arg
> 0)
317 /* Does "stmt" contain any data dependent accesses?
319 static int stmt_has_data_dependent_accesses(struct pet_stmt
*stmt
)
321 return expr_has_data_dependent_accesses(stmt
->body
);
324 /* Does "scop" contain any data dependent accesses?
326 static int scop_has_data_dependent_accesses(struct pet_scop
*scop
)
332 for (i
= 0; i
< scop
->n_stmt
; ++i
)
333 if (stmt_has_data_dependent_accesses(scop
->stmts
[i
]))
339 static void *ppcg_scop_free(struct ppcg_scop
*ps
)
344 isl_set_free(ps
->context
);
345 isl_union_set_free(ps
->domain
);
346 isl_union_set_free(ps
->call
);
347 isl_union_map_free(ps
->reads
);
348 isl_union_map_free(ps
->live_in
);
349 isl_union_map_free(ps
->writes
);
350 isl_union_map_free(ps
->live_out
);
351 isl_union_map_free(ps
->kills
);
352 isl_union_map_free(ps
->dep_flow
);
353 isl_union_map_free(ps
->dep_false
);
354 isl_union_map_free(ps
->schedule
);
361 /* Extract a ppcg_scop from a pet_scop.
363 * The constructed ppcg_scop refers to elements from the pet_scop
364 * so the pet_scop should not be freed before the ppcg_scop.
366 static struct ppcg_scop
*ppcg_scop_from_pet_scop(struct pet_scop
*scop
,
367 struct ppcg_options
*options
)
370 struct ppcg_scop
*ps
;
375 ctx
= isl_set_get_ctx(scop
->context
);
377 if (scop_has_data_dependent_accesses(scop
))
378 isl_die(ctx
, isl_error_unsupported
,
379 "data dependent accesses not supported",
382 ps
= isl_calloc_type(ctx
, struct ppcg_scop
);
386 ps
->options
= options
;
387 ps
->start
= scop
->start
;
389 ps
->context
= isl_set_copy(scop
->context
);
390 ps
->context
= set_intersect_str(ps
->context
, options
->ctx
);
391 ps
->domain
= collect_non_kill_domains(scop
);
392 ps
->call
= collect_call_domains(scop
);
393 ps
->reads
= pet_scop_collect_may_reads(scop
);
394 ps
->writes
= pet_scop_collect_may_writes(scop
);
395 ps
->kills
= pet_scop_collect_must_kills(scop
);
396 ps
->schedule
= pet_scop_collect_schedule(scop
);
397 ps
->n_type
= scop
->n_type
;
398 ps
->types
= scop
->types
;
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 /* Internal data structure for ppcg_transform.
416 struct ppcg_transform_data
{
417 struct ppcg_options
*options
;
418 __isl_give isl_printer
*(*transform
)(__isl_take isl_printer
*p
,
419 struct ppcg_scop
*scop
, void *user
);
423 /* Callback for pet_transform_C_source that transforms
424 * the given pet_scop to a ppcg_scop before calling the
425 * ppcg_transform callback.
427 static __isl_give isl_printer
*transform(__isl_take isl_printer
*p
,
428 struct pet_scop
*scop
, void *user
)
430 struct ppcg_transform_data
*data
= user
;
431 struct ppcg_scop
*ps
;
433 if (pet_scop_has_data_dependent_accesses(scop
) ||
434 pet_scop_has_data_dependent_conditions(scop
)) {
435 p
= pet_scop_print_original(scop
, p
);
440 scop
= pet_scop_align_params(scop
);
441 ps
= ppcg_scop_from_pet_scop(scop
, data
->options
);
443 p
= data
->transform(p
, ps
, data
->user
);
451 /* Transform the C source file "input" by rewriting each scop
452 * through a call to "transform".
453 * The transformed C code is written to "out".
455 * This is a wrapper around pet_transform_C_source that transforms
456 * the pet_scop to a ppcg_scop before calling "fn".
458 int ppcg_transform(isl_ctx
*ctx
, const char *input
, FILE *out
,
459 struct ppcg_options
*options
,
460 __isl_give isl_printer
*(*fn
)(__isl_take isl_printer
*p
,
461 struct ppcg_scop
*scop
, void *user
), void *user
)
463 struct ppcg_transform_data data
= { options
, fn
, user
};
464 return pet_transform_C_source(ctx
, input
, out
, &transform
, &data
);
467 /* Check consistency of options.
469 * Return -1 on error.
471 static int check_options(isl_ctx
*ctx
)
473 struct options
*options
;
475 options
= isl_ctx_peek_options(ctx
, &options_args
);
477 isl_die(ctx
, isl_error_internal
,
478 "unable to find options", return -1);
480 if (options
->ppcg
->openmp
&&
481 !isl_options_get_ast_build_atomic_upper_bound(ctx
))
482 isl_die(ctx
, isl_error_invalid
,
483 "OpenMP requires atomic bounds", return -1);
488 int main(int argc
, char **argv
)
492 struct options
*options
;
494 options
= options_new_with_defaults();
497 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
498 isl_options_set_schedule_outer_coincidence(ctx
, 1);
499 isl_options_set_schedule_maximize_band_depth(ctx
, 1);
500 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
502 if (check_options(ctx
) < 0)
504 else if (options
->ppcg
->target
== PPCG_TARGET_CUDA
)
505 r
= generate_cuda(ctx
, options
->ppcg
, options
->input
);
507 r
= generate_cpu(ctx
, options
->ppcg
, options
->input
,