2 * Copyright 2012 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
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.
39 #include <isl/union_map.h>
43 struct pet_options
*pet
;
48 ISL_ARGS_START(struct options
, options_args
)
49 ISL_ARG_CHILD(struct options
, pet
, NULL
, &pet_options_args
, "pet options")
50 ISL_ARG_ARG(struct options
, schedule
, "schedule", NULL
)
51 ISL_ARG_ARG(struct options
, code
, "code", NULL
)
54 ISL_ARG_DEF(options
, struct options
, options_args
)
56 static __isl_give isl_pw_aff
*expr_extract_pw_aff(struct pet_expr
*expr
,
57 __isl_keep isl_union_map
*assignments
);
59 /* Extract an affine expression from the call to floord in "expr",
60 * possibly exploiting "assignments".
62 static __isl_give isl_pw_aff
*expr_extract_floord(struct pet_expr
*expr
,
63 __isl_keep isl_union_map
*assignments
)
65 isl_pw_aff
*lhs
, *rhs
;
67 lhs
= expr_extract_pw_aff(expr
->args
[0], assignments
);
68 rhs
= expr_extract_pw_aff(expr
->args
[1], assignments
);
69 return isl_pw_aff_floor(isl_pw_aff_div(lhs
, rhs
));
72 /* Extract an affine expression from the call in "expr",
73 * possibly exploiting "assignments".
75 * We only support calls to the "floord" function for now.
77 static __isl_give isl_pw_aff
*call_expr_extract_pw_aff(struct pet_expr
*expr
,
78 __isl_keep isl_union_map
*assignments
)
80 assert(!strcmp(expr
->name
, "floord"));
82 return expr_extract_floord(expr
, assignments
);
85 /* Is the variable accessed by "access" assigned in "assignments"?
87 * The assignments are of the form
89 * { variable -> [domain -> value] }
91 static int is_assigned(__isl_keep isl_map
*access
,
92 __isl_keep isl_union_map
*assignments
)
98 var
= isl_union_set_from_set(isl_map_range(isl_map_copy(access
)));
99 test
= isl_union_map_copy(assignments
);
100 test
= isl_union_map_intersect_domain(test
, var
);
101 empty
= isl_union_map_is_empty(test
);
102 isl_union_map_free(test
);
107 /* Apply the appropriate assignment in "assignments" to the access "map".
109 * "map" is of the form
111 * { access_domain -> variable }
113 * "assignments" are of the form
115 * { variable -> [assignment_domain -> value] }
117 * We assume the assignment precedes the access in the code.
118 * In particular, we assume that the loops around the assignment
119 * are the same as the first loops around the access.
123 * { access_domain -> [assignment_domain -> value] }
125 * equate the iterators of assignment_domain to the corresponding iterators
126 * in access_domain and then project out assignment_domain, obtaining
128 * { access_domain -> value }
130 static __isl_give isl_map
*apply_assignment(__isl_take isl_map
*map
,
131 __isl_keep isl_union_map
*assignments
)
137 umap
= isl_union_map_from_map(map
);
138 umap
= isl_union_map_apply_range(umap
, isl_union_map_copy(assignments
));
139 map
= isl_map_from_union_map(umap
);
140 space
= isl_space_unwrap(isl_space_range(isl_map_get_space(map
)));
141 n
= isl_space_dim(space
, isl_dim_in
);
142 for (i
= 0; i
< n
; ++i
)
143 map
= isl_map_equate(map
, isl_dim_in
, i
, isl_dim_out
, i
);
144 map
= isl_map_apply_range(map
,
145 isl_map_range_map(isl_map_universe(space
)));
150 /* Extract an affine expression from the access to a named space in "access",
151 * possibly exploiting "assignments".
153 * If the variable has been assigned a value, we return the corresponding
154 * assignment. Otherwise, we assume we are accessing a 0D space and
155 * we turn that into an expression equal to a parameter of the same name.
157 static __isl_give isl_map
*resolve_access(__isl_take isl_map
*access
,
158 __isl_keep isl_union_map
*assignments
)
162 if (is_assigned(access
, assignments
))
163 return apply_assignment(access
, assignments
);
165 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
166 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
167 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, id
);
168 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
169 access
= isl_map_equate(access
, isl_dim_param
, 0, isl_dim_out
, 0);
174 /* Extract an affine expression from the access expression "expr",
175 * possibly exploiting "assignments".
177 * If we are accessing a (1D) anonymous space, then we are actually
178 * computing an affine expression and we simply return that expression.
179 * Otherwise, we try and convert the access to an affine expression in
182 static __isl_give isl_pw_aff
*access_expr_extract_pw_aff(struct pet_expr
*expr
,
183 __isl_keep isl_union_map
*assignments
)
187 isl_pw_multi_aff
*pma
;
189 map
= isl_map_copy(expr
->acc
.access
);
190 if (isl_map_has_tuple_id(map
, isl_dim_out
))
191 map
= resolve_access(map
, assignments
);
192 pma
= isl_pw_multi_aff_from_map(map
);
193 pa
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
194 isl_pw_multi_aff_free(pma
);
198 /* Extract an affine expression from "expr", possibly exploiting "assignments",
199 * in the form of an isl_pw_aff.
201 * We only handle the kinds of expressions that we would expect
202 * as arguments to a function call in code generated by isl.
204 static __isl_give isl_pw_aff
*expr_extract_pw_aff(struct pet_expr
*expr
,
205 __isl_keep isl_union_map
*assignments
)
207 isl_pw_aff
*pa
, *pa1
, *pa2
;
209 switch (expr
->type
) {
210 case pet_expr_access
:
211 return access_expr_extract_pw_aff(expr
, assignments
);
213 if (expr
->op
== pet_op_minus
) {
214 pa
= expr_extract_pw_aff(expr
->args
[0], assignments
);
215 return isl_pw_aff_neg(pa
);
218 case pet_expr_binary
:
219 pa1
= expr_extract_pw_aff(expr
->args
[0], assignments
);
220 pa2
= expr_extract_pw_aff(expr
->args
[1], assignments
);
223 pa
= isl_pw_aff_mul(pa1
, pa2
);
226 pa
= isl_pw_aff_add(pa1
, pa2
);
229 pa
= isl_pw_aff_sub(pa1
, pa2
);
232 pa
= isl_pw_aff_tdiv_q(pa1
, pa2
);
235 pa
= isl_pw_aff_tdiv_r(pa1
, pa2
);
242 return call_expr_extract_pw_aff(expr
, assignments
);
243 case pet_expr_ternary
:
244 pa
= expr_extract_pw_aff(expr
->args
[0], assignments
);
245 pa1
= expr_extract_pw_aff(expr
->args
[1], assignments
);
246 pa2
= expr_extract_pw_aff(expr
->args
[2], assignments
);
247 return isl_pw_aff_cond(pa
, pa1
, pa2
);
249 case pet_expr_double
:
254 /* Extract an affine expression from "expr", possibly exploiting "assignments",
255 * in the form of an isl_map.
257 static __isl_give isl_map
*expr_extract_map(struct pet_expr
*expr
,
258 __isl_keep isl_union_map
*assignments
)
260 return isl_map_from_pw_aff(expr_extract_pw_aff(expr
, assignments
));
263 /* Extract a call from "stmt", possibly exploiting "assignments".
265 * The returned map is of the form
267 * { domain -> function[arguments] }
269 static __isl_give isl_map
*stmt_extract_call(struct pet_stmt
*stmt
,
270 __isl_keep isl_union_map
*assignments
)
276 domain
= isl_set_copy(stmt
->domain
);
277 call
= isl_map_from_domain(domain
);
279 assert(stmt
->body
->type
== pet_expr_call
);
281 for (i
= 0; i
< stmt
->body
->n_arg
; ++i
) {
284 arg
= expr_extract_map(stmt
->body
->args
[i
], assignments
);
285 call
= isl_map_flat_range_product(call
, arg
);
288 call
= isl_map_set_tuple_name(call
, isl_dim_out
, stmt
->body
->name
);
293 /* Add the assignment in "stmt" to "assignments".
295 * We extract the variable access
297 * { domain -> variable }
299 * and the assigned value
301 * { domain -> value }
303 * and combined them into
305 * { variable -> [domain -> value] }
307 * We add this to "assignments" after having removed any
308 * previously assigned value to the same variable.
310 static __isl_give isl_union_map
*add_assignment(
311 __isl_take isl_union_map
*assignments
, struct pet_stmt
*stmt
)
317 assert(stmt
->body
->op
== pet_op_assign
);
318 assert(stmt
->body
->args
[0]->type
== pet_expr_access
);
319 var
= isl_map_copy(stmt
->body
->args
[0]->acc
.access
);
320 val
= expr_extract_map(stmt
->body
->args
[1], assignments
);
322 val
= isl_map_range_product(val
, var
);
323 val
= isl_map_uncurry(val
);
324 val
= isl_map_reverse(val
);
326 dom
= isl_set_universe(isl_space_domain(isl_map_get_space(val
)));
327 assignments
= isl_union_map_subtract_domain(assignments
,
328 isl_union_set_from_set(dom
));
329 assignments
= isl_union_map_add_map(assignments
, val
);
334 /* Is "stmt" a kill statement?
336 static int is_kill(struct pet_stmt
*stmt
)
338 if (stmt
->body
->type
!= pet_expr_unary
)
340 return stmt
->body
->op
== pet_op_kill
;
343 /* Extract a mapping from the iterations domains of "scop" to
344 * the calls in the corresponding statements.
346 * While scanning "scop", we keep track of assignments to variables
347 * so that we can plug them in in the arguments of the calls.
348 * Note that we do not perform any dependence analysis on the assigned
349 * variables. In code generated by isl, such assignments should only
350 * appear immediately before they are used.
352 * The assignments are kept in the form
354 * { variable -> [domain -> value] }
356 * We skip kill statements.
357 * Other than assignments and kill statements, all statements are assumed
358 * to be function calls.
360 static __isl_give isl_union_map
*scop_collect_calls(struct pet_scop
*scop
)
364 isl_union_map
*assignments
;
370 call
= isl_union_map_empty(isl_set_get_space(scop
->context
));
371 assignments
= isl_union_map_empty(isl_set_get_space(scop
->context
));
373 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
374 struct pet_stmt
*stmt
;
376 stmt
= scop
->stmts
[i
];
377 if (stmt
->body
->type
== pet_expr_binary
) {
378 assignments
= add_assignment(assignments
, stmt
);
383 call_i
= stmt_extract_call(scop
->stmts
[i
], assignments
);
384 call
= isl_union_map_add_map(call
, call_i
);
387 isl_union_map_free(assignments
);
392 /* Extract a schedule on the original domains from "scop".
393 * The original domain elements appear as calls in "scop".
395 * We first extract a schedule on the code iteration domains
396 * and a mapping from the code iteration domains to the calls
397 * (i.e., the original domain) and then combine the two.
399 static __isl_give isl_union_map
*extract_code_schedule(struct pet_scop
*scop
)
401 isl_union_map
*schedule
;
402 isl_union_map
*calls
;
404 schedule
= pet_scop_collect_schedule(scop
);
406 calls
= scop_collect_calls(scop
);
408 schedule
= isl_union_map_apply_domain(schedule
, calls
);
413 /* Check that schedule and code_schedule have the same domain,
414 * i.e., that they execute the same statement instances.
416 static int check_domain(__isl_keep isl_union_map
*schedule
,
417 __isl_keep isl_union_map
*code_schedule
)
419 isl_union_set
*dom1
, *dom2
;
424 dom1
= isl_union_map_domain(isl_union_map_copy(schedule
));
425 dom2
= isl_union_map_domain(isl_union_map_copy(code_schedule
));
426 equal
= isl_union_set_is_equal(dom1
, dom2
);
427 isl_union_set_free(dom1
);
428 isl_union_set_free(dom2
);
433 isl_die(isl_union_map_get_ctx(schedule
), isl_error_unknown
,
434 "domains not identical", return -1);
439 /* Check that the relative order specified by the input schedule is respected
440 * by the schedule extracted from the code, in case the original schedule
443 * In particular, check that there is no pair of statement instances
444 * such that the first should be scheduled _before_ the second,
445 * but is actually scheduled _after_ the second in the code.
447 static int check_order_sv(__isl_keep isl_union_map
*schedule
,
448 __isl_keep isl_union_map
*code_schedule
)
454 t1
= isl_union_map_lex_lt_union_map(isl_union_map_copy(schedule
),
455 isl_union_map_copy(schedule
));
456 t2
= isl_union_map_lex_gt_union_map(isl_union_map_copy(code_schedule
),
457 isl_union_map_copy(code_schedule
));
458 t1
= isl_union_map_intersect(t1
, t2
);
459 empty
= isl_union_map_is_empty(t1
);
460 isl_union_map_free(t1
);
465 isl_die(isl_union_map_get_ctx(schedule
), isl_error_unknown
,
466 "order not respected", return -1);
471 /* Check that the relative order specified by the input schedule is respected
472 * by the schedule extracted from the code, in case the original schedule
473 * is not single valued.
475 * In particular, check that the order imposed by the schedules on pairs
476 * of statement instances is the same.
478 static int check_order_not_sv(__isl_keep isl_union_map
*schedule
,
479 __isl_keep isl_union_map
*code_schedule
)
485 t1
= isl_union_map_lex_lt_union_map(isl_union_map_copy(schedule
),
486 isl_union_map_copy(schedule
));
487 t2
= isl_union_map_lex_lt_union_map(isl_union_map_copy(code_schedule
),
488 isl_union_map_copy(code_schedule
));
489 equal
= isl_union_map_is_equal(t1
, t2
);
490 isl_union_map_free(t1
);
491 isl_union_map_free(t2
);
496 isl_die(isl_union_map_get_ctx(schedule
), isl_error_unknown
,
497 "order not respected", return -1);
502 /* Check that the relative order specified by the input schedule is respected
503 * by the schedule extracted from the code.
505 * "sv" indicated whether the original schedule is single valued.
506 * If so, we use a cheaper test. Otherwise, we fall back on a more
509 static int check_order(__isl_keep isl_union_map
*schedule
,
510 __isl_keep isl_union_map
*code_schedule
, int sv
)
513 return check_order_sv(schedule
, code_schedule
);
515 return check_order_not_sv(schedule
, code_schedule
);
518 /* If the original schedule was single valued ("sv" is set),
519 * then the schedule extracted from the code should be single valued as well.
521 static int check_single_valued(__isl_keep isl_union_map
*code_schedule
, int sv
)
526 sv
= isl_union_map_is_single_valued(code_schedule
);
531 isl_die(isl_union_map_get_ctx(code_schedule
), isl_error_unknown
,
532 "schedule not single valued", return -1);
537 /* Read a schedule and a context from the first argument and
538 * C code from the second argument and check that the C code
539 * corresponds to the schedule on the context.
541 * In particular, check that
542 * - the domains are identical, i.e., the calls in the C code
543 * correspond to the domain elements of the schedule
544 * - no function is called twice with the same arguments, provided
545 * the schedule is single-valued
546 * - the calls are performed in an order that is compatible
549 * If the schedule is not single-valued then we would have to check
550 * that each function with a given set of arguments is called
551 * the same number of times as there are images in the schedule,
552 * but this is considerably more difficult.
554 int main(int argc
, char **argv
)
558 isl_union_map
*schedule
, *code_schedule
;
559 struct pet_scop
*scop
;
560 struct options
*options
;
565 options
= options_new_with_defaults();
567 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
568 pet_options_set_signed_overflow(ctx
, PET_OVERFLOW_IGNORE
);
569 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
571 file
= fopen(options
->schedule
, "r");
573 schedule
= isl_union_map_read_from_file(ctx
, file
);
574 context
= isl_set_read_from_file(ctx
, file
);
577 scop
= pet_scop_extract_from_C_source(ctx
, options
->code
, NULL
);
579 schedule
= isl_union_map_intersect_params(schedule
,
580 isl_set_copy(context
));
581 code_schedule
= extract_code_schedule(scop
);
582 code_schedule
= isl_union_map_intersect_params(code_schedule
, context
);
584 sv
= isl_union_map_is_single_valued(schedule
);
586 check_domain(schedule
, code_schedule
) ||
587 check_single_valued(code_schedule
, sv
) ||
588 check_order(schedule
, code_schedule
, sv
);
591 isl_union_map_free(schedule
);
592 isl_union_map_free(code_schedule
);