PetScan::extract_argument: mark non-const pointer arguments as potential writes
[pet.git] / pet_check_code.c
blob5d370215b50e1ed55ff9906aaf04364007632986
1 /*
2 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY ECOLE NORMALE SUPERIEURE ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECOLE NORMALE SUPERIEURE OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Ecole Normale Superieure.
34 #include <assert.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <isl/arg.h>
38 #include <isl/aff.h>
39 #include <isl/options.h>
40 #include <isl/set.h>
41 #include <isl/union_map.h>
42 #include <isl/id_to_pw_aff.h>
43 #include <isl/schedule_node.h>
44 #include <pet.h>
46 struct options {
47 struct isl_options *isl;
48 struct pet_options *pet;
49 char *schedule;
50 char *code;
51 unsigned tree;
54 ISL_ARGS_START(struct options, options_args)
55 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
56 ISL_ARG_CHILD(struct options, pet, NULL, &pet_options_args, "pet options")
57 ISL_ARG_ARG(struct options, schedule, "schedule", NULL)
58 ISL_ARG_ARG(struct options, code, "code", NULL)
59 ISL_ARG_BOOL(struct options, tree, 0, "tree", 0,
60 "input schedule is specified as schedule tree")
61 ISL_ARGS_END
63 ISL_ARG_DEF(options, struct options, options_args)
65 /* Extract an affine expression from "expr" in the form of an isl_map.
67 * The domain of the created expression is that of "pc".
69 static __isl_give isl_map *expr_extract_map(__isl_keep pet_expr *expr,
70 __isl_keep pet_context *pc)
72 isl_pw_aff *pa;
74 pa = pet_expr_extract_affine(expr, pc);
75 return isl_map_from_pw_aff(pa);
78 /* Extract a call from "stmt".
80 * The returned map is of the form
82 * { domain -> function[arguments] }
84 static __isl_give isl_map *stmt_extract_call(struct pet_stmt *stmt)
86 int i, n;
87 isl_set *domain;
88 isl_map *call;
89 const char *name;
90 pet_context *pc;
91 pet_expr *expr;
93 expr = pet_tree_expr_get_expr(stmt->body);
94 if (!expr)
95 return NULL;
96 if (pet_expr_get_type(expr) != pet_expr_call)
97 isl_die(pet_expr_get_ctx(expr),
98 isl_error_invalid, "expecting call statement",
99 goto error);
101 domain = isl_set_copy(stmt->domain);
102 call = isl_map_from_domain(domain);
104 pc = pet_context_alloc(isl_set_copy(stmt->domain));
105 n = pet_expr_get_n_arg(expr);
106 for (i = 0; i < n; ++i) {
107 isl_map *map_i;
108 pet_expr *arg;
110 arg = pet_expr_get_arg(expr, i);
111 map_i = expr_extract_map(arg, pc);
112 pet_expr_free(arg);
113 call = isl_map_flat_range_product(call, map_i);
115 pet_context_free(pc);
117 name = pet_expr_call_get_name(expr);
118 call = isl_map_set_tuple_name(call, isl_dim_out, name);
120 pet_expr_free(expr);
121 return call;
122 error:
123 pet_expr_free(expr);
124 return NULL;
127 /* Extract a mapping from the iterations domains of "scop" to
128 * the calls in the corresponding statements.
130 * We skip assignment and kill statements.
131 * Other than assignments and kill statements, all statements are assumed
132 * to be function calls.
134 static __isl_give isl_union_map *scop_collect_calls(struct pet_scop *scop)
136 int i;
137 isl_ctx *ctx;
138 isl_map *call_i;
139 isl_union_map *call;
141 if (!scop)
142 return NULL;
144 call = isl_union_map_empty(isl_set_get_space(scop->context));
145 ctx = isl_set_get_ctx(scop->context);
147 for (i = 0; i < scop->n_stmt; ++i) {
148 struct pet_stmt *stmt;
150 stmt = scop->stmts[i];
151 if (pet_stmt_is_assign(stmt))
152 continue;
153 if (pet_stmt_is_kill(stmt))
154 continue;
155 call_i = stmt_extract_call(scop->stmts[i]);
156 call = isl_union_map_add_map(call, call_i);
159 return call;
162 /* Extract a schedule on the original domains from "scop".
163 * The original domain elements appear as calls in "scop".
165 * We first extract a schedule on the code iteration domains
166 * and a mapping from the code iteration domains to the calls
167 * (i.e., the original domain) and then combine the two.
169 static __isl_give isl_union_map *extract_code_schedule(struct pet_scop *scop)
171 isl_union_map *schedule;
172 isl_union_map *calls;
174 schedule = isl_schedule_get_map(scop->schedule);
176 calls = scop_collect_calls(scop);
178 schedule = isl_union_map_apply_domain(schedule, calls);
180 return schedule;
183 /* Check that schedule and code_schedule have the same domain,
184 * i.e., that they execute the same statement instances.
186 static int check_domain(__isl_keep isl_union_map *schedule,
187 __isl_keep isl_union_map *code_schedule)
189 isl_union_set *dom1, *dom2;
190 int equal;
191 isl_set *s1, *s2;;
192 isl_id *id1, *id2;
193 int r = 0;
195 dom1 = isl_union_map_domain(isl_union_map_copy(schedule));
196 dom2 = isl_union_map_domain(isl_union_map_copy(code_schedule));
197 equal = isl_union_set_is_equal(dom1, dom2);
199 if (equal < 0)
200 r = -1;
201 else if (!equal) {
202 isl_union_set_dump(dom1);
203 isl_union_set_dump(dom2);
204 isl_die(isl_union_map_get_ctx(schedule), isl_error_unknown,
205 "domains not identical", r = -1);
208 isl_union_set_free(dom1);
209 isl_union_set_free(dom2);
211 return r;
214 /* Check that the relative order specified by the input schedule is respected
215 * by the schedule extracted from the code, in case the original schedule
216 * is single valued.
218 * In particular, check that there is no pair of statement instances
219 * such that the first should be scheduled _before_ the second,
220 * but is actually scheduled _after_ the second in the code.
222 static int check_order_sv(__isl_keep isl_union_map *schedule,
223 __isl_keep isl_union_map *code_schedule)
225 isl_union_map *t1;
226 isl_union_map *t2;
227 int empty;
229 t1 = isl_union_map_lex_lt_union_map(isl_union_map_copy(schedule),
230 isl_union_map_copy(schedule));
231 t2 = isl_union_map_lex_gt_union_map(isl_union_map_copy(code_schedule),
232 isl_union_map_copy(code_schedule));
233 t1 = isl_union_map_intersect(t1, t2);
234 empty = isl_union_map_is_empty(t1);
235 isl_union_map_free(t1);
237 if (empty < 0)
238 return -1;
239 if (!empty)
240 isl_die(isl_union_map_get_ctx(schedule), isl_error_unknown,
241 "order not respected", return -1);
243 return 0;
246 /* Check that the relative order specified by the input schedule is respected
247 * by the schedule extracted from the code, in case the original schedule
248 * is not single valued.
250 * In particular, check that the order imposed by the schedules on pairs
251 * of statement instances is the same.
253 static int check_order_not_sv(__isl_keep isl_union_map *schedule,
254 __isl_keep isl_union_map *code_schedule)
256 isl_union_map *t1;
257 isl_union_map *t2;
258 int equal;
260 t1 = isl_union_map_lex_lt_union_map(isl_union_map_copy(schedule),
261 isl_union_map_copy(schedule));
262 t2 = isl_union_map_lex_lt_union_map(isl_union_map_copy(code_schedule),
263 isl_union_map_copy(code_schedule));
264 equal = isl_union_map_is_equal(t1, t2);
265 isl_union_map_free(t1);
266 isl_union_map_free(t2);
268 if (equal < 0)
269 return -1;
270 if (!equal)
271 isl_die(isl_union_map_get_ctx(schedule), isl_error_unknown,
272 "order not respected", return -1);
274 return 0;
277 /* Check that the relative order specified by the input schedule is respected
278 * by the schedule extracted from the code.
280 * "sv" indicated whether the original schedule is single valued.
281 * If so, we use a cheaper test. Otherwise, we fall back on a more
282 * expensive test.
284 static int check_order(__isl_keep isl_union_map *schedule,
285 __isl_keep isl_union_map *code_schedule, int sv)
287 if (sv)
288 return check_order_sv(schedule, code_schedule);
289 else
290 return check_order_not_sv(schedule, code_schedule);
293 /* If the original schedule was single valued ("sv" is set),
294 * then the schedule extracted from the code should be single valued as well.
296 static int check_single_valued(__isl_keep isl_union_map *code_schedule, int sv)
298 if (!sv)
299 return 0;
301 sv = isl_union_map_is_single_valued(code_schedule);
302 if (sv < 0)
303 return -1;
305 if (!sv)
306 isl_die(isl_union_map_get_ctx(code_schedule), isl_error_unknown,
307 "schedule not single valued", return -1);
309 return 0;
312 /* Read a schedule and a context from the first argument and
313 * C code from the second argument and check that the C code
314 * corresponds to the schedule on the context.
316 * In particular, check that
317 * - the domains are identical, i.e., the calls in the C code
318 * correspond to the domain elements of the schedule
319 * - no function is called twice with the same arguments, provided
320 * the schedule is single-valued
321 * - the calls are performed in an order that is compatible
322 * with the schedule
324 * If the schedule is not single-valued then we would have to check
325 * that each function with a given set of arguments is called
326 * the same number of times as there are images in the schedule,
327 * but this is considerably more difficult.
329 int main(int argc, char **argv)
331 isl_ctx *ctx;
332 isl_set *context;
333 isl_union_map *input_schedule, *code_schedule;
334 struct pet_scop *scop;
335 struct options *options;
336 FILE *file;
337 int r;
338 int sv;
340 options = options_new_with_defaults();
341 assert(options);
342 ctx = isl_ctx_alloc_with_options(&options_args, options);
343 pet_options_set_signed_overflow(ctx, PET_OVERFLOW_IGNORE);
344 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
346 file = fopen(options->schedule, "r");
347 assert(file);
348 if (options->tree) {
349 isl_schedule *schedule;
350 isl_schedule_node *node;
351 enum isl_schedule_node_type type;
353 schedule = isl_schedule_read_from_file(ctx, file);
354 node = isl_schedule_get_root(schedule);
355 isl_options_set_schedule_separate_components(ctx, 0);
356 input_schedule =
357 isl_schedule_node_get_subtree_schedule_union_map(node);
358 node = isl_schedule_node_child(node, 0);
359 type = isl_schedule_node_get_type(node);
360 if (type == isl_schedule_node_context) {
361 context = isl_schedule_node_context_get_context(node);
362 } else {
363 isl_space *space;
364 space = isl_union_map_get_space(input_schedule);
365 context = isl_set_universe(space);
367 isl_schedule_node_free(node);
368 isl_schedule_free(schedule);
369 } else {
370 input_schedule = isl_union_map_read_from_file(ctx, file);
371 context = isl_set_read_from_file(ctx, file);
373 fclose(file);
375 scop = pet_scop_extract_from_C_source(ctx, options->code, NULL);
377 input_schedule = isl_union_map_intersect_params(input_schedule,
378 isl_set_copy(context));
379 code_schedule = extract_code_schedule(scop);
380 code_schedule = isl_union_map_intersect_params(code_schedule, context);
382 sv = isl_union_map_is_single_valued(input_schedule);
383 r = sv < 0 ||
384 check_domain(input_schedule, code_schedule) ||
385 check_single_valued(code_schedule, sv) ||
386 check_order(input_schedule, code_schedule, sv);
388 pet_scop_free(scop);
389 isl_union_map_free(input_schedule);
390 isl_union_map_free(code_schedule);
391 isl_ctx_free(ctx);
393 return r;