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,
16 #include <isl/options.h>
17 #include <isl/schedule.h>
18 #include <isl/ast_build.h>
21 #include "ppcg_options.h"
26 struct isl_options
*isl
;
27 struct pet_options
*pet
;
28 struct ppcg_options
*ppcg
;
33 const char *ppcg_version(void);
34 static void print_version(void)
36 printf("%s", ppcg_version());
39 ISL_ARGS_START(struct options
, options_args
)
40 ISL_ARG_CHILD(struct options
, isl
, "isl", &isl_options_args
, "isl options")
41 ISL_ARG_CHILD(struct options
, pet
, "pet", &pet_options_args
, "pet options")
42 ISL_ARG_CHILD(struct options
, ppcg
, NULL
, &ppcg_options_args
, "ppcg options")
43 ISL_ARG_STR(struct options
, output
, 'o', NULL
,
44 "filename", NULL
, "output filename (c target)")
45 ISL_ARG_ARG(struct options
, input
, "input", NULL
)
46 ISL_ARG_VERSION(print_version
)
49 ISL_ARG_DEF(options
, struct options
, options_args
)
51 /* Is "stmt" a kill statement?
53 static int is_kill(struct pet_stmt
*stmt
)
55 if (stmt
->body
->type
!= pet_expr_unary
)
57 return stmt
->body
->op
== pet_op_kill
;
60 /* Is "stmt" not a kill statement?
62 static int is_not_kill(struct pet_stmt
*stmt
)
64 return !is_kill(stmt
);
67 /* Collect the iteration domains of the statements in "scop" that
70 static __isl_give isl_union_set
*collect_domains(struct pet_scop
*scop
,
71 int (*pred
)(struct pet_stmt
*stmt
))
75 isl_union_set
*domain
;
80 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
82 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
83 struct pet_stmt
*stmt
= scop
->stmts
[i
];
87 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
88 domain
= isl_union_set_add_set(domain
, domain_i
);
94 /* Collect the iteration domains of the statements in "scop",
95 * skipping kill statements.
97 static __isl_give isl_union_set
*collect_non_kill_domains(struct pet_scop
*scop
)
99 return collect_domains(scop
, &is_not_kill
);
102 /* Does "expr" contain any call expressions?
104 static int expr_has_call(struct pet_expr
*expr
)
108 if (expr
->type
== pet_expr_call
)
111 for (i
= 0; i
< expr
->n_arg
; ++i
)
112 if (expr_has_call(expr
->args
[i
]))
118 /* Does "stmt" contain any call expressions?
120 static int has_call(struct pet_stmt
*stmt
)
122 return expr_has_call(stmt
->body
);
125 /* Collect the iteration domains of the statements in "scop"
126 * that contain a call expression.
128 static __isl_give isl_union_set
*collect_call_domains(struct pet_scop
*scop
)
130 return collect_domains(scop
, &has_call
);
133 /* Collect all kill accesses in "scop".
135 static __isl_give isl_union_map
*collect_kills(struct pet_scop
*scop
)
138 isl_union_map
*kills
;
143 kills
= isl_union_map_empty(isl_set_get_space(scop
->context
));
145 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
146 struct pet_stmt
*stmt
= scop
->stmts
[i
];
151 kill_i
= isl_map_copy(stmt
->body
->args
[0]->acc
.access
);
152 kill_i
= isl_map_intersect_domain(kill_i
,
153 isl_set_copy(stmt
->domain
));
154 kills
= isl_union_map_add_map(kills
, kill_i
);
160 /* Compute the dependences of the program represented by "scop".
161 * Store the computed flow dependences
162 * in scop->dep_flow and the reads with no corresponding writes in
164 * Store the false (anti and output) dependences in scop->dep_false.
166 static void compute_dependences(struct ppcg_scop
*scop
)
168 isl_union_map
*empty
;
169 isl_union_map
*dep1
, *dep2
;
174 empty
= isl_union_map_empty(isl_union_set_get_space(scop
->domain
));
175 isl_union_map_compute_flow(isl_union_map_copy(scop
->reads
),
176 isl_union_map_copy(scop
->writes
), empty
,
177 isl_union_map_copy(scop
->schedule
),
178 &scop
->dep_flow
, NULL
, &scop
->live_in
, NULL
);
180 isl_union_map_compute_flow(isl_union_map_copy(scop
->writes
),
181 isl_union_map_copy(scop
->writes
),
182 isl_union_map_copy(scop
->reads
),
183 isl_union_map_copy(scop
->schedule
),
184 &dep1
, &dep2
, NULL
, NULL
);
186 scop
->dep_false
= isl_union_map_union(dep1
, dep2
);
187 scop
->dep_false
= isl_union_map_coalesce(scop
->dep_false
);
190 /* Eliminate dead code from ps->domain.
192 * In particular, intersect ps->domain with the (parts of) iteration
193 * domains that are needed to produce the output or for statement
194 * iterations that call functions.
196 * We start with the iteration domains that call functions
197 * and the set of iterations that last write to an array
198 * (except those that are later killed).
200 * Then we add those statement iterations that produce
201 * something needed by the "live" statements iterations.
202 * We keep doing this until no more statement iterations can be added.
203 * To ensure that the procedure terminates, we compute the affine
204 * hull of the live iterations (bounded to the original iteration
205 * domains) each time we have added extra iterations.
207 static void eliminate_dead_code(struct ppcg_scop
*ps
)
209 isl_union_map
*exposed
;
213 exposed
= isl_union_map_union(isl_union_map_copy(ps
->writes
),
214 isl_union_map_copy(ps
->kills
));
215 exposed
= isl_union_map_reverse(exposed
);
216 exposed
= isl_union_map_apply_range(exposed
,
217 isl_union_map_copy(ps
->schedule
));
218 exposed
= isl_union_map_lexmax(exposed
);
219 exposed
= isl_union_map_apply_range(exposed
,
220 isl_union_map_reverse(isl_union_map_copy(ps
->schedule
)));
222 live
= isl_union_map_range(exposed
);
223 if (!isl_union_set_is_empty(ps
->call
)) {
224 live
= isl_union_set_union(live
, isl_union_set_copy(ps
->call
));
225 live
= isl_union_set_coalesce(live
);
228 dep
= isl_union_map_copy(ps
->dep_flow
);
229 dep
= isl_union_map_reverse(dep
);
232 isl_union_set
*extra
;
234 extra
= isl_union_set_apply(isl_union_set_copy(live
),
235 isl_union_map_copy(dep
));
236 if (isl_union_set_is_subset(extra
, live
)) {
237 isl_union_set_free(extra
);
241 live
= isl_union_set_union(live
, extra
);
242 live
= isl_union_set_affine_hull(live
);
243 live
= isl_union_set_intersect(live
,
244 isl_union_set_copy(ps
->domain
));
247 isl_union_map_free(dep
);
249 ps
->domain
= isl_union_set_intersect(ps
->domain
, live
);
252 /* Intersect "set" with the set described by "str", taking the NULL
253 * string to represent the universal set.
255 static __isl_give isl_set
*set_intersect_str(__isl_take isl_set
*set
,
264 ctx
= isl_set_get_ctx(set
);
265 set2
= isl_set_read_from_str(ctx
, str
);
266 set
= isl_set_intersect(set
, set2
);
271 /* Extract a ppcg_scop from a pet_scop.
273 * The constructed ppcg_scop refers to elements from the pet_scop
274 * so the pet_scop should not be freed before the ppcg_scop.
276 static struct ppcg_scop
*ppcg_scop_from_pet_scop(struct pet_scop
*scop
,
277 struct ppcg_options
*options
)
280 struct ppcg_scop
*ps
;
285 ctx
= isl_set_get_ctx(scop
->context
);
287 ps
= isl_calloc_type(ctx
, struct ppcg_scop
);
291 ps
->start
= scop
->start
;
293 ps
->context
= isl_set_copy(scop
->context
);
294 ps
->context
= set_intersect_str(ps
->context
, options
->ctx
);
295 ps
->domain
= collect_non_kill_domains(scop
);
296 ps
->call
= collect_call_domains(scop
);
297 ps
->reads
= pet_scop_collect_reads(scop
);
298 ps
->writes
= pet_scop_collect_writes(scop
);
299 ps
->kills
= collect_kills(scop
);
300 ps
->schedule
= pet_scop_collect_schedule(scop
);
301 ps
->n_array
= scop
->n_array
;
302 ps
->arrays
= scop
->arrays
;
303 ps
->n_stmt
= scop
->n_stmt
;
304 ps
->stmts
= scop
->stmts
;
306 compute_dependences(ps
);
307 eliminate_dead_code(ps
);
312 static void ppcg_scop_free(struct ppcg_scop
*ps
)
317 isl_set_free(ps
->context
);
318 isl_union_set_free(ps
->domain
);
319 isl_union_set_free(ps
->call
);
320 isl_union_map_free(ps
->reads
);
321 isl_union_map_free(ps
->live_in
);
322 isl_union_map_free(ps
->writes
);
323 isl_union_map_free(ps
->kills
);
324 isl_union_map_free(ps
->dep_flow
);
325 isl_union_map_free(ps
->dep_false
);
326 isl_union_map_free(ps
->schedule
);
331 /* Extract a model from the input file, transform the model and
332 * write out the result to the output file(s).
334 static int transform(isl_ctx
*ctx
)
337 struct pet_scop
*scop
;
338 struct ppcg_scop
*ps
;
339 struct options
*options
;
341 options
= isl_ctx_peek_options(ctx
, &options_args
);
343 isl_die(ctx
, isl_error_internal
,
344 "unable to find options", return EXIT_FAILURE
);
346 scop
= pet_scop_extract_from_C_source(ctx
, options
->input
, NULL
);
347 scop
= pet_scop_align_params(scop
);
348 ps
= ppcg_scop_from_pet_scop(scop
, options
->ppcg
);
350 if (options
->ppcg
->target
== PPCG_TARGET_CUDA
)
351 r
= generate_cuda(ctx
, ps
, options
->ppcg
, options
->input
);
353 r
= generate_cpu(ctx
, ps
, options
->ppcg
, options
->input
,
362 int main(int argc
, char **argv
)
366 struct options
*options
;
368 options
= options_new_with_defaults();
371 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
372 isl_options_set_schedule_outer_zero_distance(ctx
, 1);
373 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
375 if (options
->ppcg
->openmp
)
376 assert(isl_options_get_ast_build_atomic_upper_bound(ctx
)
377 && "OpenMP requires atomic bounds");