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
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/options.h>
41 #include <isl/union_map.h>
42 #include <isl/id_to_pw_aff.h>
48 struct isl_options
*isl
;
49 struct pet_options
*pet
;
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
)
61 ISL_ARG_DEF(options
, struct options
, options_args
)
63 static __isl_give isl_pw_aff
*expr_extract_pw_aff(__isl_keep pet_expr
*expr
,
64 __isl_keep isl_space
*space
, __isl_keep isl_id_to_pw_aff
*assignments
);
66 /* Extract an affine expression from the call to floord in "expr",
67 * possibly exploiting "assignments".
69 * "space" is the iteration space of the statement containing the expression.
71 static __isl_give isl_pw_aff
*expr_extract_floord(__isl_keep pet_expr
*expr
,
72 __isl_keep isl_space
*space
, __isl_keep isl_id_to_pw_aff
*assignments
)
74 isl_pw_aff
*lhs
, *rhs
;
77 arg
= pet_expr_get_arg(expr
, 0);
78 lhs
= expr_extract_pw_aff(arg
, space
, assignments
);
80 arg
= pet_expr_get_arg(expr
, 1);
81 rhs
= expr_extract_pw_aff(arg
, space
, assignments
);
83 return isl_pw_aff_floor(isl_pw_aff_div(lhs
, rhs
));
86 /* Extract an affine expression from the call in "expr",
87 * possibly exploiting "assignments".
89 * "space" is the iteration space of the statement containing the expression.
91 * We only support calls to the "floord" function for now.
93 static __isl_give isl_pw_aff
*call_expr_extract_pw_aff(
94 __isl_keep pet_expr
*expr
, __isl_keep isl_space
*space
,
95 __isl_keep isl_id_to_pw_aff
*assignments
)
99 name
= pet_expr_call_get_name(expr
);
102 if (!strcmp(name
, "floord"))
103 return expr_extract_floord(expr
, space
, assignments
);
105 isl_die(isl_id_to_pw_aff_get_ctx(assignments
), isl_error_unsupported
,
106 "unsupported expression", return NULL
);
109 /* Is the variable accessed by "index" assigned in "assignments"?
111 * The assignments map variable identifiers to functions of the form
113 * { domain -> value }
115 static int is_assigned(__isl_keep isl_multi_pw_aff
*index
,
116 __isl_keep isl_id_to_pw_aff
*assignments
)
121 var
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
122 assigned
= isl_id_to_pw_aff_has(assignments
, var
);
128 /* Apply the appropriate assignment in "assignments"
129 * to the index expression "index".
131 * "index" is of the form
133 * { access_domain -> variable }
135 * "assignments" maps variable identifiers to functions of the form
137 * { assignment_domain -> value }
139 * We assume the assignment precedes the access in the code.
140 * In particular, we assume that the loops around the assignment
141 * are the same as the first loops around the access.
145 * { access_domain -> assignment_domain }
147 * equating the iterators of assignment_domain to the corresponding iterators
148 * in access_domain and then plug that into the assigned value, obtaining
150 * { access_domain -> value }
152 static __isl_give isl_pw_aff
*apply_assignment(
153 __isl_take isl_multi_pw_aff
*index
,
154 __isl_keep isl_id_to_pw_aff
*assignments
)
160 isl_space
*space
, *dom_space
;
164 id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
165 dom
= isl_multi_pw_aff_domain(index
);
166 val
= isl_id_to_pw_aff_get(assignments
, id
);
167 space
= isl_pw_aff_get_domain_space(val
);
168 dom_space
= isl_set_get_space(dom
);
169 space
= isl_space_map_from_domain_and_range(dom_space
, space
);
170 ma
= isl_multi_aff_zero(space
);
171 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
172 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
173 for (i
= 0; i
< n
; ++i
) {
176 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
178 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
180 isl_local_space_free(ls
);
182 val
= isl_pw_aff_pullback_multi_aff(val
, ma
);
183 val
= isl_pw_aff_intersect_domain(val
, dom
);
188 /* Extract an affine expression from the access to a named space in "index",
189 * possibly exploiting "assignments".
191 * If the variable has been assigned a value, we return the corresponding
192 * assignment. Otherwise, we assume we are accessing a 0D space and
193 * we turn that into an expression equal to a parameter of the same name.
195 static __isl_give isl_pw_aff
*resolve_access(__isl_take isl_multi_pw_aff
*index
,
196 __isl_keep isl_id_to_pw_aff
*assignments
)
204 if (is_assigned(index
, assignments
))
205 return apply_assignment(index
, assignments
);
207 id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
208 dom
= isl_multi_pw_aff_domain(index
);
209 dom
= isl_set_insert_dims(dom
, isl_dim_param
, 0, 1);
210 dom
= isl_set_set_dim_id(dom
, isl_dim_param
, 0, id
);
211 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
212 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, 0);
213 pa
= isl_pw_aff_alloc(dom
, aff
);
218 /* Extract an affine expression from the access expression "expr",
219 * possibly exploiting "assignments".
221 * If we are accessing a (1D) anonymous space, then we are actually
222 * computing an affine expression and we simply return that expression.
223 * Otherwise, we try and convert the access to an affine expression in
226 static __isl_give isl_pw_aff
*access_expr_extract_pw_aff(
227 __isl_keep pet_expr
*expr
, __isl_keep isl_id_to_pw_aff
*assignments
)
230 isl_multi_pw_aff
*index
;
232 index
= pet_expr_access_get_index(expr
);
233 if (isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
)) {
234 pa
= resolve_access(index
, assignments
);
236 pa
= isl_multi_pw_aff_get_pw_aff(index
, 0);
237 isl_multi_pw_aff_free(index
);
242 /* Extract an affine expression defined over iteration space "space"
243 * from the integer expression "expr".
245 static __isl_give isl_pw_aff
*int_expr_extract_pw_aff(
246 __isl_keep pet_expr
*expr
, __isl_keep isl_space
*space
)
251 ls
= isl_local_space_from_space(isl_space_copy(space
));
252 aff
= isl_aff_zero_on_domain(ls
);
253 aff
= isl_aff_add_constant_val(aff
, pet_expr_int_get_val(expr
));
254 return isl_pw_aff_from_aff(aff
);
257 /* Extract an affine expression from the operation in "expr",
258 * possibly exploiting "assignments".
260 * "space" is the iteration space of the statement containing the expression.
262 * We only handle the kinds of expressions that we would expect
263 * as arguments to a function call in code generated by isl.
265 static __isl_give isl_pw_aff
*op_expr_extract_pw_aff(__isl_keep pet_expr
*expr
,
266 __isl_keep isl_space
*space
, __isl_keep isl_id_to_pw_aff
*assignments
)
268 isl_pw_aff
*pa
, *pa1
, *pa2
;
270 enum pet_op_type type
;
272 switch (pet_expr_get_n_arg(expr
)) {
274 if (pet_expr_op_get_type(expr
) == pet_op_minus
) {
275 arg
= pet_expr_get_arg(expr
, 0);
276 pa
= expr_extract_pw_aff(arg
, space
, assignments
);
278 return isl_pw_aff_neg(pa
);
282 arg
= pet_expr_get_arg(expr
, 0);
283 pa1
= expr_extract_pw_aff(arg
, space
, assignments
);
285 arg
= pet_expr_get_arg(expr
, 1);
286 pa2
= expr_extract_pw_aff(arg
, space
, assignments
);
288 type
= pet_expr_op_get_type(expr
);
291 pa
= isl_pw_aff_mul(pa1
, pa2
);
294 pa
= isl_pw_aff_add(pa1
, pa2
);
297 pa
= isl_pw_aff_sub(pa1
, pa2
);
300 pa
= isl_pw_aff_tdiv_q(pa1
, pa2
);
303 pa
= isl_pw_aff_tdiv_r(pa1
, pa2
);
311 pa
= pet_comparison(type
, pa1
, pa2
);
315 pa
= pet_boolean(type
, pa1
, pa2
);
322 arg
= pet_expr_get_arg(expr
, 0);
323 pa
= expr_extract_pw_aff(arg
, space
, assignments
);
325 arg
= pet_expr_get_arg(expr
, 1);
326 pa1
= expr_extract_pw_aff(arg
, space
, assignments
);
328 arg
= pet_expr_get_arg(expr
, 2);
329 pa2
= expr_extract_pw_aff(arg
, space
, assignments
);
331 return isl_pw_aff_cond(pa
, pa1
, pa2
);
337 /* Extract an affine expression from "expr", possibly exploiting "assignments",
338 * in the form of an isl_pw_aff.
340 * "space" is the iteration space of the statement containing the expression.
342 * We only handle the kinds of expressions that we would expect
343 * as arguments to a function call in code generated by isl.
345 static __isl_give isl_pw_aff
*expr_extract_pw_aff(__isl_keep pet_expr
*expr
,
346 __isl_keep isl_space
*space
, __isl_keep isl_id_to_pw_aff
*assignments
)
348 switch (pet_expr_get_type(expr
)) {
350 return int_expr_extract_pw_aff(expr
, space
);
351 case pet_expr_access
:
352 return access_expr_extract_pw_aff(expr
, assignments
);
354 return op_expr_extract_pw_aff(expr
, space
, assignments
);
356 return call_expr_extract_pw_aff(expr
, space
, assignments
);
358 case pet_expr_double
:
364 /* Extract an affine expression from "expr", possibly exploiting "assignments",
365 * in the form of an isl_map.
367 * "space" is the iteration space of the statement containing the expression.
369 static __isl_give isl_map
*expr_extract_map(__isl_keep pet_expr
*expr
,
370 __isl_keep isl_space
*space
, __isl_keep isl_id_to_pw_aff
*assignments
)
374 pa
= expr_extract_pw_aff(expr
, space
, assignments
);
375 return isl_map_from_pw_aff(pa
);
378 /* Extract a call from "stmt", possibly exploiting "assignments".
380 * The returned map is of the form
382 * { domain -> function[arguments] }
384 static __isl_give isl_map
*stmt_extract_call(struct pet_stmt
*stmt
,
385 __isl_keep isl_id_to_pw_aff
*assignments
)
392 domain
= isl_set_copy(stmt
->domain
);
393 call
= isl_map_from_domain(domain
);
395 assert(pet_expr_get_type(stmt
->body
) == pet_expr_call
);
397 n
= pet_expr_get_n_arg(stmt
->body
);
398 for (i
= 0; i
< n
; ++i
) {
403 arg
= pet_expr_get_arg(stmt
->body
, i
);
404 space
= pet_stmt_get_space(stmt
);
405 map_i
= expr_extract_map(arg
, space
, assignments
);
406 isl_space_free(space
);
408 call
= isl_map_flat_range_product(call
, map_i
);
411 name
= pet_expr_call_get_name(stmt
->body
);
412 call
= isl_map_set_tuple_name(call
, isl_dim_out
, name
);
417 /* Add the assignment in "stmt" to "assignments".
419 * We extract the accessed variable identifier "var"
420 * and the assigned value
422 * { domain -> value }
424 * and map "var" to this value in "assignments", replacing
425 * any possible previously assigned value to the same variable.
427 static __isl_give isl_id_to_pw_aff
*add_assignment(
428 __isl_take isl_id_to_pw_aff
*assignments
, struct pet_stmt
*stmt
)
435 assert(pet_stmt_is_assign(stmt
));
436 arg
= pet_expr_get_arg(stmt
->body
, 0);
437 assert(pet_expr_get_type(arg
) == pet_expr_access
);
438 var
= pet_expr_access_get_id(arg
);
440 arg
= pet_expr_get_arg(stmt
->body
, 1);
441 space
= pet_stmt_get_space(stmt
);
442 val
= expr_extract_pw_aff(arg
, space
, assignments
);
443 isl_space_free(space
);
446 assignments
= isl_id_to_pw_aff_set(assignments
, var
, val
);
451 /* Extract a mapping from the iterations domains of "scop" to
452 * the calls in the corresponding statements.
454 * While scanning "scop", we keep track of assignments to variables
455 * so that we can plug them in in the arguments of the calls.
456 * Note that we do not perform any dependence analysis on the assigned
457 * variables. In code generated by isl, such assignments should only
458 * appear immediately before they are used.
460 * The assignments are kept as an associative array between
461 * variable identifiers and assignments of the form
463 * { domain -> value }
465 * We skip kill statements.
466 * Other than assignments and kill statements, all statements are assumed
467 * to be function calls.
469 static __isl_give isl_union_map
*scop_collect_calls(struct pet_scop
*scop
)
474 isl_id_to_pw_aff
*assignments
;
480 call
= isl_union_map_empty(isl_set_get_space(scop
->context
));
481 ctx
= isl_set_get_ctx(scop
->context
);
482 assignments
= isl_id_to_pw_aff_alloc(ctx
, 0);
484 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
485 struct pet_stmt
*stmt
;
487 stmt
= scop
->stmts
[i
];
488 if (pet_stmt_is_assign(stmt
)) {
489 assignments
= add_assignment(assignments
, stmt
);
492 if (pet_stmt_is_kill(stmt
))
494 call_i
= stmt_extract_call(scop
->stmts
[i
], assignments
);
495 call
= isl_union_map_add_map(call
, call_i
);
498 isl_id_to_pw_aff_free(assignments
);
503 /* Extract a schedule on the original domains from "scop".
504 * The original domain elements appear as calls in "scop".
506 * We first extract a schedule on the code iteration domains
507 * and a mapping from the code iteration domains to the calls
508 * (i.e., the original domain) and then combine the two.
510 static __isl_give isl_union_map
*extract_code_schedule(struct pet_scop
*scop
)
512 isl_union_map
*schedule
;
513 isl_union_map
*calls
;
515 schedule
= pet_scop_collect_schedule(scop
);
517 calls
= scop_collect_calls(scop
);
519 schedule
= isl_union_map_apply_domain(schedule
, calls
);
524 /* Check that schedule and code_schedule have the same domain,
525 * i.e., that they execute the same statement instances.
527 static int check_domain(__isl_keep isl_union_map
*schedule
,
528 __isl_keep isl_union_map
*code_schedule
)
530 isl_union_set
*dom1
, *dom2
;
536 dom1
= isl_union_map_domain(isl_union_map_copy(schedule
));
537 dom2
= isl_union_map_domain(isl_union_map_copy(code_schedule
));
538 equal
= isl_union_set_is_equal(dom1
, dom2
);
543 isl_union_set_dump(dom1
);
544 isl_union_set_dump(dom2
);
545 isl_die(isl_union_map_get_ctx(schedule
), isl_error_unknown
,
546 "domains not identical", r
= -1);
549 isl_union_set_free(dom1
);
550 isl_union_set_free(dom2
);
555 /* Check that the relative order specified by the input schedule is respected
556 * by the schedule extracted from the code, in case the original schedule
559 * In particular, check that there is no pair of statement instances
560 * such that the first should be scheduled _before_ the second,
561 * but is actually scheduled _after_ the second in the code.
563 static int check_order_sv(__isl_keep isl_union_map
*schedule
,
564 __isl_keep isl_union_map
*code_schedule
)
570 t1
= isl_union_map_lex_lt_union_map(isl_union_map_copy(schedule
),
571 isl_union_map_copy(schedule
));
572 t2
= isl_union_map_lex_gt_union_map(isl_union_map_copy(code_schedule
),
573 isl_union_map_copy(code_schedule
));
574 t1
= isl_union_map_intersect(t1
, t2
);
575 empty
= isl_union_map_is_empty(t1
);
576 isl_union_map_free(t1
);
581 isl_die(isl_union_map_get_ctx(schedule
), isl_error_unknown
,
582 "order not respected", return -1);
587 /* Check that the relative order specified by the input schedule is respected
588 * by the schedule extracted from the code, in case the original schedule
589 * is not single valued.
591 * In particular, check that the order imposed by the schedules on pairs
592 * of statement instances is the same.
594 static int check_order_not_sv(__isl_keep isl_union_map
*schedule
,
595 __isl_keep isl_union_map
*code_schedule
)
601 t1
= isl_union_map_lex_lt_union_map(isl_union_map_copy(schedule
),
602 isl_union_map_copy(schedule
));
603 t2
= isl_union_map_lex_lt_union_map(isl_union_map_copy(code_schedule
),
604 isl_union_map_copy(code_schedule
));
605 equal
= isl_union_map_is_equal(t1
, t2
);
606 isl_union_map_free(t1
);
607 isl_union_map_free(t2
);
612 isl_die(isl_union_map_get_ctx(schedule
), isl_error_unknown
,
613 "order not respected", return -1);
618 /* Check that the relative order specified by the input schedule is respected
619 * by the schedule extracted from the code.
621 * "sv" indicated whether the original schedule is single valued.
622 * If so, we use a cheaper test. Otherwise, we fall back on a more
625 static int check_order(__isl_keep isl_union_map
*schedule
,
626 __isl_keep isl_union_map
*code_schedule
, int sv
)
629 return check_order_sv(schedule
, code_schedule
);
631 return check_order_not_sv(schedule
, code_schedule
);
634 /* If the original schedule was single valued ("sv" is set),
635 * then the schedule extracted from the code should be single valued as well.
637 static int check_single_valued(__isl_keep isl_union_map
*code_schedule
, int sv
)
642 sv
= isl_union_map_is_single_valued(code_schedule
);
647 isl_die(isl_union_map_get_ctx(code_schedule
), isl_error_unknown
,
648 "schedule not single valued", return -1);
653 /* Read a schedule and a context from the first argument and
654 * C code from the second argument and check that the C code
655 * corresponds to the schedule on the context.
657 * In particular, check that
658 * - the domains are identical, i.e., the calls in the C code
659 * correspond to the domain elements of the schedule
660 * - no function is called twice with the same arguments, provided
661 * the schedule is single-valued
662 * - the calls are performed in an order that is compatible
665 * If the schedule is not single-valued then we would have to check
666 * that each function with a given set of arguments is called
667 * the same number of times as there are images in the schedule,
668 * but this is considerably more difficult.
670 int main(int argc
, char **argv
)
674 isl_union_map
*schedule
, *code_schedule
;
675 struct pet_scop
*scop
;
676 struct options
*options
;
681 options
= options_new_with_defaults();
683 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
684 pet_options_set_signed_overflow(ctx
, PET_OVERFLOW_IGNORE
);
685 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
687 file
= fopen(options
->schedule
, "r");
689 schedule
= isl_union_map_read_from_file(ctx
, file
);
690 context
= isl_set_read_from_file(ctx
, file
);
693 scop
= pet_scop_extract_from_C_source(ctx
, options
->code
, NULL
);
695 schedule
= isl_union_map_intersect_params(schedule
,
696 isl_set_copy(context
));
697 code_schedule
= extract_code_schedule(scop
);
698 code_schedule
= isl_union_map_intersect_params(code_schedule
, context
);
700 sv
= isl_union_map_is_single_valued(schedule
);
702 check_domain(schedule
, code_schedule
) ||
703 check_single_valued(code_schedule
, sv
) ||
704 check_order(schedule
, code_schedule
, sv
);
707 isl_union_map_free(schedule
);
708 isl_union_map_free(code_schedule
);