add functions for manipulating the domain of a pet_context
[pet.git] / context.c
blob6eaf621d304e9978cda95ab98aba68be6199ce08
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
35 #include <isl/aff.h>
37 #include "aff.h"
38 #include "context.h"
39 #include "expr.h"
40 #include "expr_arg.h"
41 #include "nest.h"
43 /* A pet_context represents the context in which a pet_expr
44 * in converted to an affine expression.
46 * "domain" prescribes the domain of the affine expressions.
48 * "assignments" maps variable names to their currently known values.
49 * Internally, the domains of the values may be equal to some prefix
50 * of the space of "domain", but the domains are updated to be
51 * equal to the space of "domain" before passing them to the user.
52 * If a variable has been assigned an unknown value (possibly because
53 * it may be assigned a different expression in each iteration) or a value
54 * that is not an affine expression, then the corresponding isl_pw_aff
55 * is set to NaN.
57 * If "allow_nested" is set, then the affine expression created
58 * in this context may involve new parameters that encode a pet_expr.
60 struct pet_context {
61 int ref;
63 isl_set *domain;
64 isl_id_to_pw_aff *assignments;
65 int allow_nested;
68 /* Create a pet_context with the given domain, assignments,
69 * and value for allow_nested.
71 static __isl_give pet_context *context_alloc(__isl_take isl_set *domain,
72 __isl_take isl_id_to_pw_aff *assignments, int allow_nested)
74 pet_context *pc;
76 if (!domain || !assignments)
77 goto error;
79 pc = isl_calloc_type(isl_set_get_ctx(domain), struct pet_context);
80 if (!pc)
81 goto error;
83 pc->ref = 1;
84 pc->domain = domain;
85 pc->assignments = assignments;
86 pc->allow_nested = allow_nested;
88 return pc;
89 error:
90 isl_set_free(domain);
91 isl_id_to_pw_aff_free(assignments);
92 return NULL;
95 /* Create a pet_context with the given domain.
97 * Initially, there are no assigned values and parameters that
98 * encode a pet_expr are not allowed.
100 __isl_give pet_context *pet_context_alloc(__isl_take isl_set *domain)
102 isl_id_to_pw_aff *assignments;
104 if (!domain)
105 return NULL;
107 assignments = isl_id_to_pw_aff_alloc(isl_set_get_ctx(domain), 0);;
108 return context_alloc(domain, assignments, 0);
111 /* Return an independent duplicate of "pc".
113 static __isl_give pet_context *pet_context_dup(__isl_keep pet_context *pc)
115 pet_context *dup;
117 if (!pc)
118 return NULL;
120 dup = context_alloc(isl_set_copy(pc->domain),
121 isl_id_to_pw_aff_copy(pc->assignments),
122 pc->allow_nested);
124 return dup;
127 /* Return a pet_context that is equal to "pc" and that has only one reference.
129 static __isl_give pet_context *pet_context_cow(__isl_take pet_context *pc)
131 if (!pc)
132 return NULL;
134 if (pc->ref == 1)
135 return pc;
136 pc->ref--;
137 return pet_context_dup(pc);
140 /* Return an extra reference to "pc".
142 __isl_give pet_context *pet_context_copy(__isl_keep pet_context *pc)
144 if (!pc)
145 return NULL;
147 pc->ref++;
148 return pc;
151 /* Free a reference to "pc" and return NULL.
153 __isl_null pet_context *pet_context_free(__isl_take pet_context *pc)
155 if (!pc)
156 return NULL;
157 if (--pc->ref > 0)
158 return NULL;
160 isl_set_free(pc->domain);
161 isl_id_to_pw_aff_free(pc->assignments);
162 free(pc);
163 return NULL;
166 /* Return the isl_ctx in which "pc" was created.
168 isl_ctx *pet_context_get_ctx(__isl_keep pet_context *pc)
170 return pc ? isl_set_get_ctx(pc->domain) : NULL;
173 /* Return the domain of "pc".
175 __isl_give isl_set *pet_context_get_domain(__isl_keep pet_context *pc)
177 if (!pc)
178 return NULL;
179 return isl_set_copy(pc->domain);
182 /* Return the domain space of "pc".
184 * The domain of "pc" may have constraints involving parameters
185 * that encode a pet_expr. These parameters are not relevant
186 * outside this domain. Furthermore, they need to be resolved
187 * from the final result, so we do not want to propagate them needlessly.
189 __isl_give isl_space *pet_context_get_space(__isl_keep pet_context *pc)
191 isl_space *space;
193 if (!pc)
194 return NULL;
196 space = isl_set_get_space(pc->domain);
197 space = pet_nested_remove_from_space(space);
198 return space;
201 /* Return the dimension of the domain space of "pc".
203 unsigned pet_context_dim(__isl_keep pet_context *pc)
205 if (!pc)
206 return 0;
207 return isl_set_dim(pc->domain, isl_dim_set);
210 /* Return the assignments of "pc".
212 __isl_give isl_id_to_pw_aff *pet_context_get_assignments(
213 __isl_keep pet_context *pc)
215 if (!pc)
216 return NULL;
217 return isl_id_to_pw_aff_copy(pc->assignments);
220 /* Is "id" assigned any value in "pc"?
222 int pet_context_is_assigned(__isl_keep pet_context *pc, __isl_keep isl_id *id)
224 if (!pc || !id)
225 return -1;
226 return isl_id_to_pw_aff_has(pc->assignments, id);
229 /* Return the value assigned to "id" in "pc".
231 * Some dimensions may have been added to pc->domain after the value
232 * associated to "id" was added. We therefore need to adjust the domain
233 * of the stored value to match pc->domain by adding the missing
234 * dimensions.
236 __isl_give isl_pw_aff *pet_context_get_value(__isl_keep pet_context *pc,
237 __isl_take isl_id *id)
239 int dim;
240 isl_pw_aff *pa;
241 isl_multi_aff *ma;
243 if (!pc || !id)
244 goto error;
246 pa = isl_id_to_pw_aff_get(pc->assignments, id);
247 dim = isl_pw_aff_dim(pa, isl_dim_in);
248 if (dim == isl_set_dim(pc->domain, isl_dim_set))
249 return pa;
251 ma = pet_prefix_projection(pet_context_get_space(pc), dim);
252 pa = isl_pw_aff_pullback_multi_aff(pa, ma);
254 return pa;
255 error:
256 isl_id_free(id);
257 return NULL;
260 /* Assign the value "value" to "id" in "pc", replacing the previously
261 * assigned value, if any.
263 __isl_give pet_context *pet_context_set_value(__isl_take pet_context *pc,
264 __isl_take isl_id *id, isl_pw_aff *value)
266 pc = pet_context_cow(pc);
267 if (!pc)
268 goto error;
269 pc->assignments = isl_id_to_pw_aff_set(pc->assignments, id, value);
270 if (!pc->assignments)
271 return pet_context_free(pc);
272 return pc;
273 error:
274 isl_id_free(id);
275 isl_pw_aff_free(value);
276 return NULL;
279 /* Remove any assignment to "id" in "pc".
281 __isl_give pet_context *pet_context_clear_value(__isl_keep pet_context *pc,
282 __isl_take isl_id *id)
284 pc = pet_context_cow(pc);
285 if (!pc)
286 goto error;
287 pc->assignments = isl_id_to_pw_aff_drop(pc->assignments, id);
288 if (!pc->assignments)
289 return pet_context_free(pc);
290 return pc;
291 error:
292 isl_id_free(id);
293 return NULL;
296 /* Return a piecewise affine expression defined on the specified domain
297 * that represents NaN.
299 static __isl_give isl_pw_aff *nan_on_domain(__isl_take isl_space *space)
301 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space));
304 /* Assign the value NaN to "id" in "pc", marked it as having an unknown
305 * value.
307 __isl_give pet_context *pet_context_mark_unknown(__isl_take pet_context *pc,
308 __isl_take isl_id *id)
310 isl_pw_aff *pa;
312 pa = nan_on_domain(pet_context_get_space(pc));
313 pc = pet_context_set_value(pc, id, pa);
315 return pc;
318 /* Are affine expressions created in this context allowed to involve
319 * parameters that encode a pet_expr?
321 int pet_context_allow_nesting(__isl_keep pet_context *pc)
323 if (!pc)
324 return -1;
325 return pc->allow_nested;
328 /* Allow affine expressions created in this context to involve
329 * parameters that encode a pet_expr based on the value of "allow_nested".
331 __isl_give pet_context *pet_context_set_allow_nested(__isl_take pet_context *pc,
332 int allow_nested)
334 if (!pc)
335 return NULL;
336 if (pc->allow_nested == allow_nested)
337 return pc;
338 pc = pet_context_cow(pc);
339 if (!pc)
340 return NULL;
341 pc->allow_nested = allow_nested;
342 return pc;
345 /* If the access expression "expr" writes to a (non-virtual) scalar,
346 * then mark the scalar as having an unknown value in "pc".
348 static int clear_write(__isl_keep pet_expr *expr, void *user)
350 isl_id *id;
351 pet_context **pc = (pet_context **) user;
353 if (!pet_expr_access_is_write(expr))
354 return 0;
355 if (!pet_expr_is_scalar_access(expr))
356 return 0;
358 id = pet_expr_access_get_id(expr);
359 if (isl_id_get_user(id))
360 *pc = pet_context_mark_unknown(*pc, id);
361 else
362 isl_id_free(id);
364 return 0;
367 /* Look for any writes to scalar variables in "expr" and
368 * mark them as having an unknown value in "pc".
370 __isl_give pet_context *pet_context_clear_writes_in_expr(
371 __isl_take pet_context *pc, __isl_keep pet_expr *expr)
373 if (pet_expr_foreach_access_expr(expr, &clear_write, &pc) < 0)
374 pc = pet_context_free(pc);
376 return pc;
379 /* Look for any writes to scalar variables in "tree" and
380 * mark them as having an unknown value in "pc".
382 __isl_give pet_context *pet_context_clear_writes_in_tree(
383 __isl_take pet_context *pc, __isl_keep pet_tree *tree)
385 if (pet_tree_foreach_access_expr(tree, &clear_write, &pc) < 0)
386 pc = pet_context_free(pc);
388 return pc;
391 /* Given an access expression, check if it reads a scalar variable
392 * that has a known value in "pc".
393 * If so, then replace the access by an access to that value.
395 static __isl_give pet_expr *access_plug_in_affine_read(
396 __isl_take pet_expr *expr, void *user)
398 pet_context *pc = user;
399 isl_pw_aff *pa;
401 if (pet_expr_access_is_write(expr))
402 return expr;
403 if (!pet_expr_is_scalar_access(expr))
404 return expr;
406 pa = pet_expr_extract_affine(expr, pc);
407 if (!pa)
408 return pet_expr_free(expr);
409 if (isl_pw_aff_involves_nan(pa)) {
410 isl_pw_aff_free(pa);
411 return expr;
414 pet_expr_free(expr);
415 expr = pet_expr_from_index(isl_multi_pw_aff_from_pw_aff(pa));
417 return expr;
420 /* Replace every read access in "expr" to a scalar variable
421 * that has a known value in "pc" by that known value.
423 static __isl_give pet_expr *plug_in_affine_read(__isl_take pet_expr *expr,
424 __isl_keep pet_context *pc)
426 return pet_expr_map_access(expr, &access_plug_in_affine_read, pc);
429 /* Evaluate "expr" in the context of "pc".
431 * In particular, we first make sure that all the access expressions
432 * inside "expr" have the same domain as "pc".
433 * Then, we plug in affine expressions for scalar reads and
434 * plug in the arguments of all access expressions in "expr".
436 __isl_give pet_expr *pet_context_evaluate_expr(__isl_keep pet_context *pc,
437 __isl_take pet_expr *expr)
439 expr = pet_expr_insert_domain(expr, pet_context_get_space(pc));
440 expr = plug_in_affine_read(expr, pc);
441 return pet_expr_plug_in_args(expr, pc);
444 /* Add an unbounded inner dimension "id" to pc->domain.
446 * The assignments are not adjusted here and therefore keep
447 * their original domain. These domains need to be adjusted before
448 * these assigned values can be used. This is taken care of by
449 * pet_context_get_value.
451 static __isl_give pet_context *extend_domain(__isl_take pet_context *pc,
452 __isl_take isl_id *id)
454 int pos;
456 pc = pet_context_cow(pc);
457 if (!pc || !id)
458 goto error;
460 pos = pet_context_dim(pc);
461 pc->domain = isl_set_add_dims(pc->domain, isl_dim_set, 1);
462 pc->domain = isl_set_set_dim_id(pc->domain, isl_dim_set, pos, id);
463 if (!pc->domain)
464 return pet_context_free(pc);
466 return pc;
467 error:
468 pet_context_free(pc);
469 isl_id_free(id);
470 return NULL;
473 /* Add an unbounded inner iterator "id" to pc->domain.
474 * Additionally, mark the variable "id" as having the value of this
475 * new inner iterator.
477 __isl_give pet_context *pet_context_add_inner_iterator(
478 __isl_take pet_context *pc, __isl_take isl_id *id)
480 int pos;
481 isl_space *space;
482 isl_local_space *ls;
483 isl_aff *aff;
484 isl_pw_aff *pa;
486 if (!pc || !id)
487 goto error;
489 pos = pet_context_dim(pc);
490 pc = extend_domain(pc, isl_id_copy(id));
491 if (!pc)
492 goto error;
494 space = pet_context_get_space(pc);
495 ls = isl_local_space_from_space(space);
496 aff = isl_aff_var_on_domain(ls, isl_dim_set, pos);
497 pa = isl_pw_aff_from_aff(aff);
499 pc = pet_context_set_value(pc, id, pa);
501 return pc;
502 error:
503 pet_context_free(pc);
504 isl_id_free(id);
505 return NULL;
508 /* Add an inner iterator to pc->domain.
509 * In particular, extend the domain with an inner loop { [t] : t >= 0 }.
511 __isl_give pet_context *pet_context_add_infinite_loop(
512 __isl_take pet_context *pc)
514 int dim;
515 isl_ctx *ctx;
516 isl_id *id;
518 if (!pc)
519 return NULL;
521 dim = pet_context_dim(pc);
522 ctx = pet_context_get_ctx(pc);
523 id = isl_id_alloc(ctx, "t", NULL);
524 pc = extend_domain(pc, id);
525 pc = pet_context_cow(pc);
526 if (!pc)
527 return NULL;
528 pc->domain = isl_set_lower_bound_si(pc->domain, isl_dim_set, dim, 0);
529 if (!pc->domain)
530 return pet_context_free(pc);
532 return pc;
535 /* Internal data structure for preimage_domain.
537 * "ma" is the function under which the preimage should be computed.
538 * "assignments" collects the results.
540 struct pet_preimage_domain_data {
541 isl_multi_aff *ma;
542 isl_id_to_pw_aff *assignments;
545 /* Add the assignment to "key" of the preimage of "val" under data->ma
546 * to data->assignments.
548 * Some dimensions may have been added to the domain of the enclosing
549 * pet_context after the value "val" was added. We therefore need to
550 * adjust the domain of "val" to match the range of data->ma (which
551 * in turn matches the domain of the pet_context), by adding the missing
552 * dimensions.
554 static int preimage_domain_pair(__isl_take isl_id *key,
555 __isl_take isl_pw_aff *val, void *user)
557 struct pet_preimage_domain_data *data = user;
558 int dim;
559 isl_multi_aff *ma;
561 ma = isl_multi_aff_copy(data->ma);
563 dim = isl_pw_aff_dim(val, isl_dim_in);
564 if (dim != isl_multi_aff_dim(data->ma, isl_dim_out)) {
565 isl_space *space;
566 isl_multi_aff *proj;
567 space = isl_multi_aff_get_space(data->ma);
568 space = isl_space_range(space);
569 proj = pet_prefix_projection(space, dim);
570 ma = isl_multi_aff_pullback_multi_aff(proj, ma);
573 val = isl_pw_aff_pullback_multi_aff(val, ma);
574 data->assignments = isl_id_to_pw_aff_set(data->assignments, key, val);
576 return 0;
579 /* Compute the preimage of "assignments" under the function represented by "ma".
580 * In other words, plug in "ma" in the domains of the assigned values.
582 static __isl_give isl_id_to_pw_aff *preimage_domain(
583 __isl_take isl_id_to_pw_aff *assignments, __isl_keep isl_multi_aff *ma)
585 struct pet_preimage_domain_data data = { ma };
586 isl_ctx *ctx;
588 ctx = isl_id_to_pw_aff_get_ctx(assignments);
589 data.assignments = isl_id_to_pw_aff_alloc(ctx, 0);
590 if (isl_id_to_pw_aff_foreach(assignments, &preimage_domain_pair,
591 &data) < 0)
592 data.assignments = isl_id_to_pw_aff_free(data.assignments);
593 isl_id_to_pw_aff_free(assignments);
595 return data.assignments;
598 /* Compute the preimage of "pc" under the function represented by "ma".
599 * In other words, plug in "ma" in the domain of "pc".
601 __isl_give pet_context *pet_context_preimage_domain(__isl_take pet_context *pc,
602 __isl_keep isl_multi_aff *ma)
604 pc = pet_context_cow(pc);
605 if (!pc)
606 return NULL;
607 pc->domain = isl_set_preimage_multi_aff(pc->domain,
608 isl_multi_aff_copy(ma));
609 pc->assignments = preimage_domain(pc->assignments, ma);
610 if (!pc->domain || !pc->assignments)
611 return pet_context_free(pc);
612 return pc;
615 /* Add the constraints of "set" to the domain of "pc".
617 __isl_give pet_context *pet_context_intersect_domain(__isl_take pet_context *pc,
618 __isl_take isl_set *set)
620 pc = pet_context_cow(pc);
621 if (!pc)
622 goto error;
623 pc->domain = isl_set_intersect(pc->domain, set);
624 if (!pc->domain)
625 return pet_context_free(pc);
626 return pc;
627 error:
628 pet_context_free(pc);
629 isl_set_free(set);
630 return NULL;
633 void pet_context_dump(__isl_keep pet_context *pc)
635 if (!pc)
636 return;
637 fprintf(stderr, "domain: ");
638 isl_set_dump(pc->domain);
639 fprintf(stderr, "assignments: ");
640 isl_id_to_pw_aff_dump(pc->assignments);
641 fprintf(stderr, "nesting allowed: %d\n", pc->allow_nested);