print original code if scop has any data dependent constructs
[ppcg.git] / ppcg.c
blob5396f80d775bafa1399eaa6b7cbccc5e9d9e2104
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 /* Collect all kill accesses in "scop".
169 static __isl_give isl_union_map *collect_kills(struct pet_scop *scop)
171 int i;
172 isl_union_map *kills;
174 if (!scop)
175 return NULL;
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];
181 isl_map *kill_i;
183 if (!is_kill(stmt))
184 continue;
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);
191 return kills;
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
197 * scop->live_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;
205 if (!scop)
206 return;
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;
244 isl_union_set *live;
245 isl_union_map *dep;
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);
265 for (;;) {
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);
272 break;
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,
290 const char *str)
292 isl_ctx *ctx;
293 isl_set *set2;
295 if (!str)
296 return set;
298 ctx = isl_set_get_ctx(set);
299 set2 = isl_set_read_from_str(ctx, str);
300 set = isl_set_intersect(set, set2);
302 return set;
305 /* Does "expr" involve any data dependent accesses?
307 static int expr_has_data_dependent_accesses(struct pet_expr *expr)
309 int i;
311 for (i = 0; i < expr->n_arg; ++i)
312 if (expr_has_data_dependent_accesses(expr->args[i]))
313 return 1;
315 if (expr->type == pet_expr_access && expr->n_arg > 0)
316 return 1;
318 return 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)
332 int i;
334 if (!scop)
335 return -1;
336 for (i = 0; i < scop->n_stmt; ++i)
337 if (stmt_has_data_dependent_accesses(scop->stmts[i]))
338 return 1;
340 return 0;
343 static void *ppcg_scop_free(struct ppcg_scop *ps)
345 if (!ps)
346 return NULL;
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);
359 free(ps);
361 return NULL;
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)
372 isl_ctx *ctx;
373 struct ppcg_scop *ps;
375 if (!scop)
376 return NULL;
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",
383 return NULL);
385 ps = isl_calloc_type(ctx, struct ppcg_scop);
386 if (!ps)
387 return NULL;
389 ps->start = scop->start;
390 ps->end = scop->end;
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);
411 return 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);
420 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);
436 pet_scop_free(scop);
437 return 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);
445 ppcg_scop_free(ps);
446 pet_scop_free(scop);
448 return p;
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);
476 if (!options)
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);
485 return 0;
488 int main(int argc, char **argv)
490 int r;
491 isl_ctx *ctx;
492 struct options *options;
494 options = options_new_with_defaults();
495 assert(options);
497 ctx = isl_ctx_alloc_with_options(&options_args, options);
498 isl_options_set_schedule_outer_zero_distance(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)
503 r = EXIT_FAILURE;
504 else if (options->ppcg->target == PPCG_TARGET_CUDA)
505 r = generate_cuda(ctx, options->ppcg, options->input);
506 else
507 r = generate_cpu(ctx, options->ppcg, options->input,
508 options->output);
510 isl_ctx_free(ctx);
512 return r;