copy text from input file based on file offsets rather than location of pragmas
[ppcg.git] / ppcg.c
blobe137d575fda3807f22218876cfeab5725e1791a5
1 /*
2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 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 <isl/ctx.h>
14 #include <isl/flow.h>
15 #include <isl/options.h>
16 #include <isl/schedule.h>
17 #include <isl/ast_build.h>
18 #include <pet.h>
19 #include "ppcg.h"
20 #include "ppcg_options.h"
21 #include "cuda.h"
22 #include "cpu.h"
24 struct options {
25 struct isl_options *isl;
26 struct pet_options *pet;
27 struct ppcg_options *ppcg;
28 char *input;
29 char *output;
32 const char *ppcg_version(void);
33 static void print_version(void)
35 printf("%s", ppcg_version());
38 ISL_ARGS_START(struct options, options_args)
39 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
40 ISL_ARG_CHILD(struct options, pet, "pet", &pet_options_args, "pet options")
41 ISL_ARG_CHILD(struct options, ppcg, NULL, &ppcg_options_args, "ppcg options")
42 ISL_ARG_STR(struct options, output, 'o', NULL,
43 "filename", NULL, "output filename (c target)")
44 ISL_ARG_ARG(struct options, input, "input", NULL)
45 ISL_ARG_VERSION(print_version)
46 ISL_ARGS_END
48 ISL_ARG_DEF(options, struct options, options_args)
50 /* Is "stmt" a kill statement?
52 static int is_kill(struct pet_stmt *stmt)
54 if (stmt->body->type != pet_expr_unary)
55 return 0;
56 return stmt->body->op == pet_op_kill;
59 /* Is "stmt" not a kill statement?
61 static int is_not_kill(struct pet_stmt *stmt)
63 return !is_kill(stmt);
66 /* Collect the iteration domains of the statements in "scop" that
67 * satisfy "pred".
69 static __isl_give isl_union_set *collect_domains(struct pet_scop *scop,
70 int (*pred)(struct pet_stmt *stmt))
72 int i;
73 isl_set *domain_i;
74 isl_union_set *domain;
76 if (!scop)
77 return NULL;
79 domain = isl_union_set_empty(isl_set_get_space(scop->context));
81 for (i = 0; i < scop->n_stmt; ++i) {
82 struct pet_stmt *stmt = scop->stmts[i];
84 if (!pred(stmt))
85 continue;
86 domain_i = isl_set_copy(scop->stmts[i]->domain);
87 domain = isl_union_set_add_set(domain, domain_i);
90 return domain;
93 /* Collect the iteration domains of the statements in "scop",
94 * skipping kill statements.
96 static __isl_give isl_union_set *collect_non_kill_domains(struct pet_scop *scop)
98 return collect_domains(scop, &is_not_kill);
101 /* Does "expr" contain any call expressions?
103 static int expr_has_call(struct pet_expr *expr)
105 int i;
107 if (expr->type == pet_expr_call)
108 return 1;
110 for (i = 0; i < expr->n_arg; ++i)
111 if (expr_has_call(expr->args[i]))
112 return 1;
114 return 0;
117 /* Does "stmt" contain any call expressions?
119 static int has_call(struct pet_stmt *stmt)
121 return expr_has_call(stmt->body);
124 /* Collect the iteration domains of the statements in "scop"
125 * that contain a call expression.
127 static __isl_give isl_union_set *collect_call_domains(struct pet_scop *scop)
129 return collect_domains(scop, &has_call);
132 /* Collect all kill accesses in "scop".
134 static __isl_give isl_union_map *collect_kills(struct pet_scop *scop)
136 int i;
137 isl_union_map *kills;
139 if (!scop)
140 return NULL;
142 kills = isl_union_map_empty(isl_set_get_space(scop->context));
144 for (i = 0; i < scop->n_stmt; ++i) {
145 struct pet_stmt *stmt = scop->stmts[i];
146 isl_map *kill_i;
148 if (!is_kill(stmt))
149 continue;
150 kill_i = isl_map_copy(stmt->body->args[0]->acc.access);
151 kill_i = isl_map_intersect_domain(kill_i,
152 isl_set_copy(stmt->domain));
153 kills = isl_union_map_add_map(kills, kill_i);
156 return kills;
159 /* Compute the dependences of the program represented by "scop".
160 * Store the computed flow dependences
161 * in scop->dep_flow and the reads with no corresponding writes in
162 * scop->live_in.
163 * Store the false (anti and output) dependences in scop->dep_false.
165 static void compute_dependences(struct ppcg_scop *scop)
167 isl_union_map *empty;
168 isl_union_map *dep1, *dep2;
170 if (!scop)
171 return;
173 empty = isl_union_map_empty(isl_union_set_get_space(scop->domain));
174 isl_union_map_compute_flow(isl_union_map_copy(scop->reads),
175 isl_union_map_copy(scop->writes), empty,
176 isl_union_map_copy(scop->schedule),
177 &scop->dep_flow, NULL, &scop->live_in, NULL);
179 isl_union_map_compute_flow(isl_union_map_copy(scop->writes),
180 isl_union_map_copy(scop->writes),
181 isl_union_map_copy(scop->reads),
182 isl_union_map_copy(scop->schedule),
183 &dep1, &dep2, NULL, NULL);
185 scop->dep_false = isl_union_map_union(dep1, dep2);
186 scop->dep_false = isl_union_map_coalesce(scop->dep_false);
189 /* Eliminate dead code from ps->domain.
191 * In particular, intersect ps->domain with the (parts of) iteration
192 * domains that are needed to produce the output or for statement
193 * iterations that call functions.
195 * We start with the iteration domains that call functions
196 * and the set of iterations that last write to an array
197 * (except those that are later killed).
199 * Then we add those statement iterations that produce
200 * something needed by the "live" statements iterations.
201 * We keep doing this until no more statement iterations can be added.
202 * To ensure that the procedure terminates, we compute the affine
203 * hull of the live iterations (bounded to the original iteration
204 * domains) each time we have added extra iterations.
206 static void eliminate_dead_code(struct ppcg_scop *ps)
208 isl_union_map *exposed;
209 isl_union_set *live;
210 isl_union_map *dep;
212 exposed = isl_union_map_union(isl_union_map_copy(ps->writes),
213 isl_union_map_copy(ps->kills));
214 exposed = isl_union_map_reverse(exposed);
215 exposed = isl_union_map_apply_range(exposed,
216 isl_union_map_copy(ps->schedule));
217 exposed = isl_union_map_lexmax(exposed);
218 exposed = isl_union_map_apply_range(exposed,
219 isl_union_map_reverse(isl_union_map_copy(ps->schedule)));
221 live = isl_union_map_range(exposed);
222 if (!isl_union_set_is_empty(ps->call)) {
223 live = isl_union_set_union(live, isl_union_set_copy(ps->call));
224 live = isl_union_set_coalesce(live);
227 dep = isl_union_map_copy(ps->dep_flow);
228 dep = isl_union_map_reverse(dep);
230 for (;;) {
231 isl_union_set *extra;
233 extra = isl_union_set_apply(isl_union_set_copy(live),
234 isl_union_map_copy(dep));
235 if (isl_union_set_is_subset(extra, live)) {
236 isl_union_set_free(extra);
237 break;
240 live = isl_union_set_union(live, extra);
241 live = isl_union_set_affine_hull(live);
242 live = isl_union_set_intersect(live,
243 isl_union_set_copy(ps->domain));
246 isl_union_map_free(dep);
248 ps->domain = isl_union_set_intersect(ps->domain, live);
251 /* Intersect "set" with the set described by "str", taking the NULL
252 * string to represent the universal set.
254 static __isl_give isl_set *set_intersect_str(__isl_take isl_set *set,
255 const char *str)
257 isl_ctx *ctx;
258 isl_set *set2;
260 if (!str)
261 return set;
263 ctx = isl_set_get_ctx(set);
264 set2 = isl_set_read_from_str(ctx, str);
265 set = isl_set_intersect(set, set2);
267 return set;
270 /* Extract a ppcg_scop from a pet_scop.
272 * The constructed ppcg_scop refers to elements from the pet_scop
273 * so the pet_scop should not be freed before the ppcg_scop.
275 static struct ppcg_scop *ppcg_scop_from_pet_scop(struct pet_scop *scop,
276 struct ppcg_options *options)
278 isl_ctx *ctx;
279 struct ppcg_scop *ps;
281 if (!scop)
282 return NULL;
284 ctx = isl_set_get_ctx(scop->context);
286 ps = isl_calloc_type(ctx, struct ppcg_scop);
287 if (!ps)
288 return NULL;
290 ps->start = scop->start;
291 ps->end = scop->end;
292 ps->context = isl_set_copy(scop->context);
293 ps->context = set_intersect_str(ps->context, options->ctx);
294 ps->domain = collect_non_kill_domains(scop);
295 ps->call = collect_call_domains(scop);
296 ps->reads = pet_scop_collect_reads(scop);
297 ps->writes = pet_scop_collect_writes(scop);
298 ps->kills = collect_kills(scop);
299 ps->schedule = pet_scop_collect_schedule(scop);
300 ps->n_array = scop->n_array;
301 ps->arrays = scop->arrays;
302 ps->n_stmt = scop->n_stmt;
303 ps->stmts = scop->stmts;
305 compute_dependences(ps);
306 eliminate_dead_code(ps);
308 return ps;
311 static void ppcg_scop_free(struct ppcg_scop *ps)
313 if (!ps)
314 return;
316 isl_set_free(ps->context);
317 isl_union_set_free(ps->domain);
318 isl_union_set_free(ps->call);
319 isl_union_map_free(ps->reads);
320 isl_union_map_free(ps->live_in);
321 isl_union_map_free(ps->writes);
322 isl_union_map_free(ps->kills);
323 isl_union_map_free(ps->dep_flow);
324 isl_union_map_free(ps->dep_false);
325 isl_union_map_free(ps->schedule);
327 free(ps);
330 int main(int argc, char **argv)
332 int r;
333 isl_ctx *ctx;
334 struct options *options;
335 struct pet_scop *scop;
336 struct ppcg_scop *ps;
338 options = options_new_with_defaults();
339 assert(options);
341 ctx = isl_ctx_alloc_with_options(&options_args, options);
342 isl_options_set_schedule_outer_zero_distance(ctx, 1);
343 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
345 if (options->ppcg->openmp)
346 assert(isl_options_get_ast_build_atomic_upper_bound(ctx)
347 && "OpenMP requires atomic bounds");
349 scop = pet_scop_extract_from_C_source(ctx, options->input, NULL);
350 scop = pet_scop_align_params(scop);
351 ps = ppcg_scop_from_pet_scop(scop, options->ppcg);
353 if (options->ppcg->target == PPCG_TARGET_CUDA)
354 r = generate_cuda(ctx, ps, options->ppcg, options->input);
355 else
356 r = generate_cpu(ctx, ps, options->ppcg, options->input,
357 options->output);
359 ppcg_scop_free(ps);
360 pet_scop_free(scop);
362 isl_ctx_free(ctx);
364 return r;