update isl for introduction of coincidence schedule constraints
[ppcg.git] / ppcg.c
blobbf04bb59ad79617c4ada1622fb5fd86ef1ca67e2
1 /*
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,
8 * 91893 Orsay, France
9 */
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <isl/ctx.h>
16 #include <isl/flow.h>
17 #include <isl/options.h>
18 #include <isl/schedule.h>
19 #include <isl/ast_build.h>
20 #include <isl/schedule.h>
21 #include <pet.h>
22 #include "ppcg.h"
23 #include "ppcg_options.h"
24 #include "cuda.h"
25 #include "cpu.h"
27 struct options {
28 struct isl_options *isl;
29 struct pet_options *pet;
30 struct ppcg_options *ppcg;
31 char *input;
32 char *output;
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)
49 ISL_ARGS_END
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)
61 const char *base;
62 const char *ext;
63 int len;
65 base = strrchr(input, '/');
66 if (base)
67 base++;
68 else
69 base = input;
70 ext = strrchr(base, '.');
71 len = ext ? ext - base : strlen(base);
73 memcpy(name, base, len);
75 return len;
78 /* Is "stmt" a kill statement?
80 static int is_kill(struct pet_stmt *stmt)
82 if (stmt->body->type != pet_expr_unary)
83 return 0;
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
95 * satisfy "pred".
97 static __isl_give isl_union_set *collect_domains(struct pet_scop *scop,
98 int (*pred)(struct pet_stmt *stmt))
100 int i;
101 isl_set *domain_i;
102 isl_union_set *domain;
104 if (!scop)
105 return NULL;
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];
112 if (!pred(stmt))
113 continue;
115 if (stmt->n_arg > 0)
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);
125 return domain;
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)
140 int i;
142 if (expr->type == pet_expr_call)
143 return 1;
145 for (i = 0; i < expr->n_arg; ++i)
146 if (expr_has_call(expr->args[i]))
147 return 1;
149 return 0;
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 dependences of the program represented by "scop".
168 * Store the computed flow dependences
169 * in scop->dep_flow and the reads with no corresponding writes in
170 * scop->live_in.
171 * Store the false (anti and output) dependences in scop->dep_false.
173 static void compute_dependences(struct ppcg_scop *scop)
175 isl_union_map *empty;
176 isl_union_map *dep1, *dep2;
178 if (!scop)
179 return;
181 empty = isl_union_map_empty(isl_union_set_get_space(scop->domain));
182 isl_union_map_compute_flow(isl_union_map_copy(scop->reads),
183 isl_union_map_copy(scop->writes), empty,
184 isl_union_map_copy(scop->schedule),
185 &scop->dep_flow, NULL, &scop->live_in, NULL);
187 isl_union_map_compute_flow(isl_union_map_copy(scop->writes),
188 isl_union_map_copy(scop->writes),
189 isl_union_map_copy(scop->reads),
190 isl_union_map_copy(scop->schedule),
191 &dep1, &dep2, NULL, NULL);
193 scop->dep_false = isl_union_map_union(dep1, dep2);
194 scop->dep_false = isl_union_map_coalesce(scop->dep_false);
197 /* Eliminate dead code from ps->domain.
199 * In particular, intersect ps->domain with the (parts of) iteration
200 * domains that are needed to produce the output or for statement
201 * iterations that call functions.
203 * We start with the iteration domains that call functions
204 * and the set of iterations that last write to an array
205 * (except those that are later killed).
207 * Then we add those statement iterations that produce
208 * something needed by the "live" statements iterations.
209 * We keep doing this until no more statement iterations can be added.
210 * To ensure that the procedure terminates, we compute the affine
211 * hull of the live iterations (bounded to the original iteration
212 * domains) each time we have added extra iterations.
214 static void eliminate_dead_code(struct ppcg_scop *ps)
216 isl_union_map *exposed;
217 isl_union_set *live;
218 isl_union_map *dep;
220 exposed = isl_union_map_union(isl_union_map_copy(ps->writes),
221 isl_union_map_copy(ps->kills));
222 exposed = isl_union_map_reverse(exposed);
223 exposed = isl_union_map_apply_range(exposed,
224 isl_union_map_copy(ps->schedule));
225 exposed = isl_union_map_lexmax(exposed);
226 exposed = isl_union_map_apply_range(exposed,
227 isl_union_map_reverse(isl_union_map_copy(ps->schedule)));
229 live = isl_union_map_range(exposed);
230 if (!isl_union_set_is_empty(ps->call)) {
231 live = isl_union_set_union(live, isl_union_set_copy(ps->call));
232 live = isl_union_set_coalesce(live);
235 dep = isl_union_map_copy(ps->dep_flow);
236 dep = isl_union_map_reverse(dep);
238 for (;;) {
239 isl_union_set *extra;
241 extra = isl_union_set_apply(isl_union_set_copy(live),
242 isl_union_map_copy(dep));
243 if (isl_union_set_is_subset(extra, live)) {
244 isl_union_set_free(extra);
245 break;
248 live = isl_union_set_union(live, extra);
249 live = isl_union_set_affine_hull(live);
250 live = isl_union_set_intersect(live,
251 isl_union_set_copy(ps->domain));
254 isl_union_map_free(dep);
256 ps->domain = isl_union_set_intersect(ps->domain, live);
259 /* Intersect "set" with the set described by "str", taking the NULL
260 * string to represent the universal set.
262 static __isl_give isl_set *set_intersect_str(__isl_take isl_set *set,
263 const char *str)
265 isl_ctx *ctx;
266 isl_set *set2;
268 if (!str)
269 return set;
271 ctx = isl_set_get_ctx(set);
272 set2 = isl_set_read_from_str(ctx, str);
273 set = isl_set_intersect(set, set2);
275 return set;
278 /* Does "expr" involve any data dependent accesses?
280 static int expr_has_data_dependent_accesses(struct pet_expr *expr)
282 int i;
284 for (i = 0; i < expr->n_arg; ++i)
285 if (expr_has_data_dependent_accesses(expr->args[i]))
286 return 1;
288 if (expr->type == pet_expr_access && expr->n_arg > 0)
289 return 1;
291 return 0;
294 /* Does "stmt" contain any data dependent accesses?
296 static int stmt_has_data_dependent_accesses(struct pet_stmt *stmt)
298 return expr_has_data_dependent_accesses(stmt->body);
301 /* Does "scop" contain any data dependent accesses?
303 static int scop_has_data_dependent_accesses(struct pet_scop *scop)
305 int i;
307 if (!scop)
308 return -1;
309 for (i = 0; i < scop->n_stmt; ++i)
310 if (stmt_has_data_dependent_accesses(scop->stmts[i]))
311 return 1;
313 return 0;
316 static void *ppcg_scop_free(struct ppcg_scop *ps)
318 if (!ps)
319 return NULL;
321 isl_set_free(ps->context);
322 isl_union_set_free(ps->domain);
323 isl_union_set_free(ps->call);
324 isl_union_map_free(ps->reads);
325 isl_union_map_free(ps->live_in);
326 isl_union_map_free(ps->writes);
327 isl_union_map_free(ps->kills);
328 isl_union_map_free(ps->dep_flow);
329 isl_union_map_free(ps->dep_false);
330 isl_union_map_free(ps->schedule);
332 free(ps);
334 return NULL;
337 /* Extract a ppcg_scop from a pet_scop.
339 * The constructed ppcg_scop refers to elements from the pet_scop
340 * so the pet_scop should not be freed before the ppcg_scop.
342 static struct ppcg_scop *ppcg_scop_from_pet_scop(struct pet_scop *scop,
343 struct ppcg_options *options)
345 isl_ctx *ctx;
346 struct ppcg_scop *ps;
348 if (!scop)
349 return NULL;
351 ctx = isl_set_get_ctx(scop->context);
353 if (scop_has_data_dependent_accesses(scop))
354 isl_die(ctx, isl_error_unsupported,
355 "data dependent accesses not supported",
356 return NULL);
358 ps = isl_calloc_type(ctx, struct ppcg_scop);
359 if (!ps)
360 return NULL;
362 ps->options = options;
363 ps->start = scop->start;
364 ps->end = scop->end;
365 ps->context = isl_set_copy(scop->context);
366 ps->context = set_intersect_str(ps->context, options->ctx);
367 ps->domain = collect_non_kill_domains(scop);
368 ps->call = collect_call_domains(scop);
369 ps->reads = pet_scop_collect_may_reads(scop);
370 ps->writes = pet_scop_collect_may_writes(scop);
371 ps->kills = pet_scop_collect_must_kills(scop);
372 ps->schedule = pet_scop_collect_schedule(scop);
373 ps->n_type = scop->n_type;
374 ps->types = scop->types;
375 ps->n_array = scop->n_array;
376 ps->arrays = scop->arrays;
377 ps->n_stmt = scop->n_stmt;
378 ps->stmts = scop->stmts;
380 compute_dependences(ps);
381 eliminate_dead_code(ps);
383 if (!ps->context || !ps->domain || !ps->call || !ps->reads ||
384 !ps->writes || !ps->kills || !ps->schedule)
385 return ppcg_scop_free(ps);
387 return ps;
390 /* Internal data structure for ppcg_transform.
392 struct ppcg_transform_data {
393 struct ppcg_options *options;
394 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
395 struct ppcg_scop *scop, void *user);
396 void *user;
399 /* Callback for pet_transform_C_source that transforms
400 * the given pet_scop to a ppcg_scop before calling the
401 * ppcg_transform callback.
403 static __isl_give isl_printer *transform(__isl_take isl_printer *p,
404 struct pet_scop *scop, void *user)
406 struct ppcg_transform_data *data = user;
407 struct ppcg_scop *ps;
409 if (pet_scop_has_data_dependent_accesses(scop) ||
410 pet_scop_has_data_dependent_conditions(scop)) {
411 p = pet_scop_print_original(scop, p);
412 pet_scop_free(scop);
413 return p;
416 scop = pet_scop_align_params(scop);
417 ps = ppcg_scop_from_pet_scop(scop, data->options);
419 p = data->transform(p, ps, data->user);
421 ppcg_scop_free(ps);
422 pet_scop_free(scop);
424 return p;
427 /* Transform the C source file "input" by rewriting each scop
428 * through a call to "transform".
429 * The transformed C code is written to "out".
431 * This is a wrapper around pet_transform_C_source that transforms
432 * the pet_scop to a ppcg_scop before calling "fn".
434 int ppcg_transform(isl_ctx *ctx, const char *input, FILE *out,
435 struct ppcg_options *options,
436 __isl_give isl_printer *(*fn)(__isl_take isl_printer *p,
437 struct ppcg_scop *scop, void *user), void *user)
439 struct ppcg_transform_data data = { options, fn, user };
440 return pet_transform_C_source(ctx, input, out, &transform, &data);
443 /* Check consistency of options.
445 * Return -1 on error.
447 static int check_options(isl_ctx *ctx)
449 struct options *options;
451 options = isl_ctx_peek_options(ctx, &options_args);
452 if (!options)
453 isl_die(ctx, isl_error_internal,
454 "unable to find options", return -1);
456 if (options->ppcg->openmp &&
457 !isl_options_get_ast_build_atomic_upper_bound(ctx))
458 isl_die(ctx, isl_error_invalid,
459 "OpenMP requires atomic bounds", return -1);
461 return 0;
464 int main(int argc, char **argv)
466 int r;
467 isl_ctx *ctx;
468 struct options *options;
470 options = options_new_with_defaults();
471 assert(options);
473 ctx = isl_ctx_alloc_with_options(&options_args, options);
474 isl_options_set_schedule_outer_coincidence(ctx, 1);
475 isl_options_set_schedule_maximize_band_depth(ctx, 1);
476 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
478 if (check_options(ctx) < 0)
479 r = EXIT_FAILURE;
480 else if (options->ppcg->target == PPCG_TARGET_CUDA)
481 r = generate_cuda(ctx, options->ppcg, options->input);
482 else
483 r = generate_cpu(ctx, options->ppcg, options->input,
484 options->output);
486 isl_ctx_free(ctx);
488 return r;