ppcg.c: collect_kills: include domain constraints in kill access relation
[ppcg.git] / ppcg.c
blobb2dd4a633f17cab243fcc8e57d82164b2fc5c131
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 <pet.h>
18 #include "ppcg.h"
19 #include "ppcg_options.h"
20 #include "cuda.h"
21 #include "cpu.h"
23 struct options {
24 struct isl_options *isl;
25 struct pet_options *pet;
26 struct ppcg_options *ppcg;
27 char *input;
30 const char *ppcg_version(void);
31 static void print_version(void)
33 printf("%s", ppcg_version());
36 ISL_ARGS_START(struct options, options_args)
37 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
38 ISL_ARG_CHILD(struct options, pet, "pet", &pet_options_args, "pet options")
39 ISL_ARG_CHILD(struct options, ppcg, NULL, &ppcg_options_args, "ppcg options")
40 ISL_ARG_ARG(struct options, input, "input", NULL)
41 ISL_ARG_VERSION(print_version)
42 ISL_ARGS_END
44 ISL_ARG_DEF(options, struct options, options_args)
46 /* Is "stmt" a kill statement?
48 static int is_kill(struct pet_stmt *stmt)
50 if (stmt->body->type != pet_expr_unary)
51 return 0;
52 return stmt->body->op == pet_op_kill;
55 /* Is "stmt" not a kill statement?
57 static int is_not_kill(struct pet_stmt *stmt)
59 return !is_kill(stmt);
62 /* Collect the iteration domains of the statements in "scop" that
63 * satisfy "pred".
65 static __isl_give isl_union_set *collect_domains(struct pet_scop *scop,
66 int (*pred)(struct pet_stmt *stmt))
68 int i;
69 isl_set *domain_i;
70 isl_union_set *domain;
72 if (!scop)
73 return NULL;
75 domain = isl_union_set_empty(isl_set_get_space(scop->context));
77 for (i = 0; i < scop->n_stmt; ++i) {
78 struct pet_stmt *stmt = scop->stmts[i];
80 if (!pred(stmt))
81 continue;
82 domain_i = isl_set_copy(scop->stmts[i]->domain);
83 domain = isl_union_set_add_set(domain, domain_i);
86 return domain;
89 /* Collect the iteration domains of the statements in "scop",
90 * skipping kill statements.
92 static __isl_give isl_union_set *collect_non_kill_domains(struct pet_scop *scop)
94 return collect_domains(scop, &is_not_kill);
97 /* Does "expr" contain any call expressions?
99 static int expr_has_call(struct pet_expr *expr)
101 int i;
103 if (expr->type == pet_expr_call)
104 return 1;
106 for (i = 0; i < expr->n_arg; ++i)
107 if (expr_has_call(expr->args[i]))
108 return 1;
110 return 0;
113 /* Does "stmt" contain any call expressions?
115 static int has_call(struct pet_stmt *stmt)
117 return expr_has_call(stmt->body);
120 /* Collect the iteration domains of the statements in "scop"
121 * that contain a call expression.
123 static __isl_give isl_union_set *collect_call_domains(struct pet_scop *scop)
125 return collect_domains(scop, &has_call);
128 /* Collect all kill accesses in "scop".
130 static __isl_give isl_union_map *collect_kills(struct pet_scop *scop)
132 int i;
133 isl_union_map *kills;
135 if (!scop)
136 return NULL;
138 kills = isl_union_map_empty(isl_set_get_space(scop->context));
140 for (i = 0; i < scop->n_stmt; ++i) {
141 struct pet_stmt *stmt = scop->stmts[i];
142 isl_map *kill_i;
144 if (!is_kill(stmt))
145 continue;
146 kill_i = isl_map_copy(stmt->body->args[0]->acc.access);
147 kill_i = isl_map_intersect_domain(kill_i,
148 isl_set_copy(stmt->domain));
149 kills = isl_union_map_add_map(kills, kill_i);
152 return kills;
155 /* Compute (flow) dependences and store the resulting flow dependences
156 * in scop->dep_flow and the reads with no corresponding writes in
157 * scop->live_in.
159 static void compute_dependences(struct ppcg_scop *scop)
161 isl_union_map *empty;
163 if (!scop)
164 return;
166 empty = isl_union_map_empty(isl_union_set_get_space(scop->domain));
167 isl_union_map_compute_flow(isl_union_map_copy(scop->reads),
168 isl_union_map_copy(scop->writes), empty,
169 isl_union_map_copy(scop->schedule),
170 &scop->dep_flow, NULL, &scop->live_in, NULL);
173 /* Eliminate dead code from ps->domain.
175 * In particular, intersect ps->domain with the (parts of) iteration
176 * domains that are needed to produce the output or for statement
177 * iterations that call functions.
179 * We start with the iteration domains that call functions
180 * and the set of iterations that last write to an array
181 * (except those that are later killed).
183 * Then we add those statement iterations that produce
184 * something needed by the "live" statements iterations.
185 * We keep doing this until no more statement iterations can be added.
186 * To ensure that the procedure terminates, we compute the affine
187 * hull of the live iterations (bounded to the original iteration
188 * domains) each time we have added extra iterations.
190 static void eliminate_dead_code(struct ppcg_scop *ps)
192 isl_union_map *exposed;
193 isl_union_set *live;
194 isl_union_map *dep;
196 exposed = isl_union_map_union(isl_union_map_copy(ps->writes),
197 isl_union_map_copy(ps->kills));
198 exposed = isl_union_map_reverse(exposed);
199 exposed = isl_union_map_apply_range(exposed,
200 isl_union_map_copy(ps->schedule));
201 exposed = isl_union_map_lexmax(exposed);
202 exposed = isl_union_map_apply_range(exposed,
203 isl_union_map_reverse(isl_union_map_copy(ps->schedule)));
205 live = isl_union_map_range(exposed);
206 live = isl_union_set_union(live, isl_union_set_copy(ps->call));
208 dep = isl_union_map_copy(ps->dep_flow);
209 dep = isl_union_map_reverse(dep);
211 for (;;) {
212 isl_union_set *extra;
214 extra = isl_union_set_apply(isl_union_set_copy(live),
215 isl_union_map_copy(dep));
216 if (isl_union_set_is_subset(extra, live)) {
217 isl_union_set_free(extra);
218 break;
221 live = isl_union_set_union(live, extra);
222 live = isl_union_set_affine_hull(live);
223 live = isl_union_set_intersect(live,
224 isl_union_set_copy(ps->domain));
227 isl_union_map_free(dep);
229 ps->domain = isl_union_set_intersect(ps->domain, live);
232 /* Extract a ppcg_scop from a pet_scop.
234 * The constructed ppcg_scop refers to elements from the pet_scop
235 * so the pet_scop should not be freed before the ppcg_scop.
237 static struct ppcg_scop *ppcg_scop_from_pet_scop(struct pet_scop *scop)
239 isl_ctx *ctx;
240 struct ppcg_scop *ps;
242 if (!scop)
243 return NULL;
245 ctx = isl_set_get_ctx(scop->context);
247 ps = isl_calloc_type(ctx, struct ppcg_scop);
248 if (!ps)
249 return NULL;
251 ps->context = isl_set_copy(scop->context);
252 ps->domain = collect_non_kill_domains(scop);
253 ps->call = collect_call_domains(scop);
254 ps->reads = pet_scop_collect_reads(scop);
255 ps->writes = pet_scop_collect_writes(scop);
256 ps->kills = collect_kills(scop);
257 ps->schedule = pet_scop_collect_schedule(scop);
258 ps->n_array = scop->n_array;
259 ps->arrays = scop->arrays;
260 ps->n_stmt = scop->n_stmt;
261 ps->stmts = scop->stmts;
263 compute_dependences(ps);
264 eliminate_dead_code(ps);
266 return ps;
269 static void ppcg_scop_free(struct ppcg_scop *ps)
271 if (!ps)
272 return;
274 isl_set_free(ps->context);
275 isl_union_set_free(ps->domain);
276 isl_union_set_free(ps->call);
277 isl_union_map_free(ps->reads);
278 isl_union_map_free(ps->live_in);
279 isl_union_map_free(ps->writes);
280 isl_union_map_free(ps->kills);
281 isl_union_map_free(ps->dep_flow);
282 isl_union_map_free(ps->schedule);
284 free(ps);
287 int main(int argc, char **argv)
289 int r;
290 isl_ctx *ctx;
291 struct options *options;
292 struct pet_scop *scop;
293 struct ppcg_scop *ps;
295 options = options_new_with_defaults();
296 assert(options);
298 ctx = isl_ctx_alloc_with_options(&options_args, options);
299 isl_options_set_schedule_outer_zero_distance(ctx, 1);
300 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
302 scop = pet_scop_extract_from_C_source(ctx, options->input, NULL);
303 scop = pet_scop_align_params(scop);
304 ps = ppcg_scop_from_pet_scop(scop);
306 if (options->ppcg->target == PPCG_TARGET_CUDA)
307 r = generate_cuda(ctx, ps, options->ppcg, options->input);
308 else
309 r = generate_cpu(ctx, ps, options->ppcg, options->input);
311 ppcg_scop_free(ps);
312 pet_scop_free(scop);
314 isl_ctx_free(ctx);
316 return r;