-
Notifications
You must be signed in to change notification settings - Fork 25
/
isl_ast_codegen.c
5948 lines (5248 loc) · 186 KB
/
isl_ast_codegen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012-2014 Ecole Normale Superieure
* Copyright 2014 INRIA Rocquencourt
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege,
* Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
* and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
* B.P. 105 - 78153 Le Chesnay, France
*/
#include <limits.h>
#include <isl/id.h>
#include <isl/val.h>
#include <isl/space.h>
#include <isl/aff.h>
#include <isl/constraint.h>
#include <isl/set.h>
#include <isl/ilp.h>
#include <isl/union_set.h>
#include <isl/union_map.h>
#include <isl/schedule_node.h>
#include <isl/options.h>
#include <isl_sort.h>
#include <isl_tarjan.h>
#include <isl_ast_private.h>
#include <isl_ast_build_expr.h>
#include <isl_ast_build_private.h>
#include <isl_ast_graft_private.h>
/* Try and reduce the number of disjuncts in the representation of "set",
* without dropping explicit representations of local variables.
*/
static __isl_give isl_set *isl_set_coalesce_preserve(__isl_take isl_set *set)
{
isl_ctx *ctx;
int save_preserve;
if (!set)
return NULL;
ctx = isl_set_get_ctx(set);
save_preserve = isl_options_get_coalesce_preserve_locals(ctx);
isl_options_set_coalesce_preserve_locals(ctx, 1);
set = isl_set_coalesce(set);
isl_options_set_coalesce_preserve_locals(ctx, save_preserve);
return set;
}
/* Data used in generate_domain.
*
* "build" is the input build.
* "list" collects the results.
*/
struct isl_generate_domain_data {
isl_ast_build *build;
isl_ast_graft_list *list;
};
static __isl_give isl_ast_graft_list *generate_next_level(
__isl_take isl_union_map *executed,
__isl_take isl_ast_build *build);
static __isl_give isl_ast_graft_list *generate_code(
__isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
int internal);
/* Generate an AST for a single domain based on
* the (non single valued) inverse schedule "executed".
*
* We extend the schedule with the iteration domain
* and continue generating through a call to generate_code.
*
* In particular, if executed has the form
*
* S -> D
*
* then we continue generating code on
*
* [S -> D] -> D
*
* The extended inverse schedule is clearly single valued
* ensuring that the nested generate_code will not reach this function,
* but will instead create calls to all elements of D that need
* to be executed from the current schedule domain.
*/
static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
struct isl_generate_domain_data *data)
{
isl_map *identity;
isl_ast_build *build;
isl_ast_graft_list *list;
build = isl_ast_build_copy(data->build);
identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
executed = isl_map_domain_product(executed, identity);
build = isl_ast_build_set_single_valued(build, 1);
list = generate_code(isl_union_map_from_map(executed), build, 1);
data->list = isl_ast_graft_list_concat(data->list, list);
return isl_stat_ok;
}
/* Call the at_each_domain callback, if requested by the user,
* after recording the current inverse schedule in the build.
*/
static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
__isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
{
if (!graft || !build)
return isl_ast_graft_free(graft);
if (!build->at_each_domain)
return graft;
build = isl_ast_build_copy(build);
build = isl_ast_build_set_executed(build,
isl_union_map_from_map(isl_map_copy(executed)));
if (!build)
return isl_ast_graft_free(graft);
graft->node = build->at_each_domain(graft->node,
build, build->at_each_domain_user);
isl_ast_build_free(build);
if (!graft->node)
graft = isl_ast_graft_free(graft);
return graft;
}
/* Generate a call expression for the single executed
* domain element "map" and put a guard around it based its (simplified)
* domain. "executed" is the original inverse schedule from which "map"
* has been derived. In particular, "map" is either identical to "executed"
* or it is the result of gisting "executed" with respect to the build domain.
* "executed" is only used if there is an at_each_domain callback.
*
* At this stage, any pending constraints in the build can no longer
* be simplified with respect to any enforced constraints since
* the call node does not have any enforced constraints.
* Since all pending constraints not covered by any enforced constraints
* will be added as a guard to the graft in create_node_scaled,
* even in the eliminated case, the pending constraints
* can be considered to have been generated by outer constructs.
*
* If the user has set an at_each_domain callback, it is called
* on the constructed call expression node.
*/
static isl_stat add_domain(__isl_take isl_map *executed,
__isl_take isl_map *map, struct isl_generate_domain_data *data)
{
isl_ast_build *build;
isl_ast_graft *graft;
isl_ast_graft_list *list;
isl_set *guard, *pending;
build = isl_ast_build_copy(data->build);
pending = isl_ast_build_get_pending(build);
build = isl_ast_build_replace_pending_by_guard(build, pending);
guard = isl_map_domain(isl_map_copy(map));
guard = isl_set_compute_divs(guard);
guard = isl_set_coalesce_preserve(guard);
guard = isl_set_gist(guard, isl_ast_build_get_generated(build));
guard = isl_ast_build_specialize(build, guard);
graft = isl_ast_graft_alloc_domain(map, build);
graft = at_each_domain(graft, executed, build);
isl_ast_build_free(build);
isl_map_free(executed);
graft = isl_ast_graft_add_guard(graft, guard, data->build);
list = isl_ast_graft_list_from_ast_graft(graft);
data->list = isl_ast_graft_list_concat(data->list, list);
return isl_stat_ok;
}
/* Generate an AST for a single domain based on
* the inverse schedule "executed" and add it to data->list.
*
* If there is more than one domain element associated to the current
* schedule "time", then we need to continue the generation process
* in generate_non_single_valued.
* Note that the inverse schedule being single-valued may depend
* on constraints that are only available in the original context
* domain specified by the user. We therefore first introduce
* some of the constraints of data->build->domain. In particular,
* we intersect with a single-disjunct approximation of this set.
* We perform this approximation to avoid further splitting up
* the executed relation, possibly introducing a disjunctive guard
* on the statement.
*
* On the other hand, we only perform the test after having taken the gist
* of the domain as the resulting map is the one from which the call
* expression is constructed. Using this map to construct the call
* expression usually yields simpler results in cases where the original
* map is not obviously single-valued.
* If the original map is obviously single-valued, then the gist
* operation is skipped.
*
* Because we perform the single-valuedness test on the gisted map,
* we may in rare cases fail to recognize that the inverse schedule
* is single-valued. This becomes problematic if this happens
* from the recursive call through generate_non_single_valued
* as we would then end up in an infinite recursion.
* We therefore check if we are inside a call to generate_non_single_valued
* and revert to the ungisted map if the gisted map turns out not to be
* single-valued.
*
* Otherwise, call add_domain to generate a call expression (with guard) and
* to call the at_each_domain callback, if any.
*/
static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
{
struct isl_generate_domain_data *data = user;
isl_set *domain;
isl_map *map = NULL;
int empty, sv;
domain = isl_ast_build_get_domain(data->build);
domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
executed = isl_map_intersect_domain(executed, domain);
empty = isl_map_is_empty(executed);
if (empty < 0)
goto error;
if (empty) {
isl_map_free(executed);
return isl_stat_ok;
}
sv = isl_map_plain_is_single_valued(executed);
if (sv < 0)
goto error;
if (sv)
return add_domain(executed, isl_map_copy(executed), data);
executed = isl_map_coalesce(executed);
map = isl_map_copy(executed);
map = isl_ast_build_compute_gist_map_domain(data->build, map);
sv = isl_map_is_single_valued(map);
if (sv < 0)
goto error;
if (!sv) {
isl_map_free(map);
if (data->build->single_valued)
map = isl_map_copy(executed);
else
return generate_non_single_valued(executed, data);
}
return add_domain(executed, map, data);
error:
isl_map_free(map);
isl_map_free(executed);
return isl_stat_error;
}
/* Call build->create_leaf to a create "leaf" node in the AST,
* encapsulate the result in an isl_ast_graft and return the result
* as a 1-element list.
*
* Note that the node returned by the user may be an entire tree.
*
* Since the node itself cannot enforce any constraints, we turn
* all pending constraints into guards and add them to the resulting
* graft to ensure that they will be generated.
*
* Before we pass control to the user, we first clear some information
* from the build that is (presumbably) only meaningful
* for the current code generation.
* This includes the create_leaf callback itself, so we make a copy
* of the build first.
*/
static __isl_give isl_ast_graft_list *call_create_leaf(
__isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
{
isl_set *guard;
isl_ast_node *node;
isl_ast_graft *graft;
isl_ast_build *user_build;
guard = isl_ast_build_get_pending(build);
user_build = isl_ast_build_copy(build);
user_build = isl_ast_build_replace_pending_by_guard(user_build,
isl_set_copy(guard));
user_build = isl_ast_build_set_executed(user_build, executed);
user_build = isl_ast_build_clear_local_info(user_build);
if (!user_build)
node = NULL;
else
node = build->create_leaf(user_build, build->create_leaf_user);
graft = isl_ast_graft_alloc(node, build);
graft = isl_ast_graft_add_guard(graft, guard, build);
isl_ast_build_free(build);
return isl_ast_graft_list_from_ast_graft(graft);
}
static __isl_give isl_ast_graft_list *build_ast_from_child(
__isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
__isl_take isl_union_map *executed);
/* Generate an AST after having handled the complete schedule
* of this call to the code generator or the complete band
* if we are generating an AST from a schedule tree.
*
* If we are inside a band node, then move on to the child of the band.
*
* If the user has specified a create_leaf callback, control
* is passed to the user in call_create_leaf.
*
* Otherwise, we generate one or more calls for each individual
* domain in generate_domain.
*/
static __isl_give isl_ast_graft_list *generate_inner_level(
__isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
{
isl_ctx *ctx;
struct isl_generate_domain_data data = { build };
if (!build || !executed)
goto error;
if (isl_ast_build_has_schedule_node(build)) {
isl_schedule_node *node;
node = isl_ast_build_get_schedule_node(build);
build = isl_ast_build_reset_schedule_node(build);
return build_ast_from_child(build, node, executed);
}
if (build->create_leaf)
return call_create_leaf(executed, build);
ctx = isl_union_map_get_ctx(executed);
data.list = isl_ast_graft_list_alloc(ctx, 0);
if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
data.list = isl_ast_graft_list_free(data.list);
if (0)
error: data.list = NULL;
isl_ast_build_free(build);
isl_union_map_free(executed);
return data.list;
}
/* Call the before_each_for callback, if requested by the user.
*/
static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
__isl_keep isl_ast_build *build)
{
isl_id *id;
if (!node || !build)
return isl_ast_node_free(node);
if (!build->before_each_for)
return node;
id = build->before_each_for(build, build->before_each_for_user);
node = isl_ast_node_set_annotation(node, id);
return node;
}
/* Call the after_each_for callback, if requested by the user.
*/
static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
__isl_keep isl_ast_build *build)
{
if (!graft || !build)
return isl_ast_graft_free(graft);
if (!build->after_each_for)
return graft;
graft->node = build->after_each_for(graft->node, build,
build->after_each_for_user);
if (!graft->node)
return isl_ast_graft_free(graft);
return graft;
}
/* Plug in all the know values of the current and outer dimensions
* in the domain of "executed". In principle, we only need to plug
* in the known value of the current dimension since the values of
* outer dimensions have been plugged in already.
* However, it turns out to be easier to just plug in all known values.
*/
static __isl_give isl_union_map *plug_in_values(
__isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
{
return isl_ast_build_substitute_values_union_map_domain(build,
executed);
}
/* Check if the constraint "c" is a lower bound on dimension "pos",
* an upper bound, or independent of dimension "pos".
*/
static int constraint_type(isl_constraint *c, int pos)
{
if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
return 1;
if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
return 2;
return 0;
}
/* Compare the types of the constraints "a" and "b",
* resulting in constraints that are independent of "depth"
* to be sorted before the lower bounds on "depth", which in
* turn are sorted before the upper bounds on "depth".
*/
static int cmp_constraint(__isl_keep isl_constraint *a,
__isl_keep isl_constraint *b, void *user)
{
int *depth = user;
int t1 = constraint_type(a, *depth);
int t2 = constraint_type(b, *depth);
return t1 - t2;
}
/* Extract a lower bound on dimension "pos" from constraint "c".
*
* If the constraint is of the form
*
* a x + f(...) >= 0
*
* then we essentially return
*
* l = ceil(-f(...)/a)
*
* However, if the current dimension is strided, then we need to make
* sure that the lower bound we construct is of the form
*
* f + s a
*
* with f the offset and s the stride.
* We therefore compute
*
* f + s * ceil((l - f)/s)
*/
static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
int pos, __isl_keep isl_ast_build *build)
{
isl_aff *aff;
aff = isl_constraint_get_bound(c, isl_dim_set, pos);
aff = isl_aff_ceil(aff);
if (isl_ast_build_has_stride(build, pos)) {
isl_aff *offset;
isl_val *stride;
offset = isl_ast_build_get_offset(build, pos);
stride = isl_ast_build_get_stride(build, pos);
aff = isl_aff_sub(aff, isl_aff_copy(offset));
aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
aff = isl_aff_ceil(aff);
aff = isl_aff_scale_val(aff, stride);
aff = isl_aff_add(aff, offset);
}
aff = isl_ast_build_compute_gist_aff(build, aff);
return aff;
}
/* Return the exact lower bound (or upper bound if "upper" is set)
* of "domain" as a piecewise affine expression.
*
* If we are computing a lower bound (of a strided dimension), then
* we need to make sure it is of the form
*
* f + s a
*
* where f is the offset and s is the stride.
* We therefore need to include the stride constraint before computing
* the minimum.
*/
static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
__isl_keep isl_ast_build *build, int upper)
{
isl_set *stride;
isl_map *it_map;
isl_pw_aff *pa;
isl_pw_multi_aff *pma;
domain = isl_set_copy(domain);
if (!upper) {
stride = isl_ast_build_get_stride_constraint(build);
domain = isl_set_intersect(domain, stride);
}
it_map = isl_ast_build_map_to_iterator(build, domain);
if (upper)
pma = isl_map_lexmax_pw_multi_aff(it_map);
else
pma = isl_map_lexmin_pw_multi_aff(it_map);
pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
isl_pw_multi_aff_free(pma);
pa = isl_ast_build_compute_gist_pw_aff(build, pa);
pa = isl_pw_aff_coalesce(pa);
return pa;
}
/* Callback for sorting the isl_pw_aff_list passed to reduce_list and
* remove_redundant_lower_bounds.
*/
static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
void *user)
{
return isl_pw_aff_plain_cmp(a, b);
}
/* Given a list of lower bounds "list", remove those that are redundant
* with respect to the other bounds in "list" and the domain of "build".
*
* We first sort the bounds in the same way as they would be sorted
* by set_for_node_expressions so that we can try and remove the last
* bounds first.
*
* For a lower bound to be effective, there needs to be at least
* one domain element for which it is larger than all other lower bounds.
* For each lower bound we therefore intersect the domain with
* the conditions that it is larger than all other bounds and
* check whether the result is empty. If so, the bound can be removed.
*/
static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
__isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
{
int i, j;
isl_size n;
isl_set *domain;
list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
n = isl_pw_aff_list_n_pw_aff(list);
if (n < 0)
return isl_pw_aff_list_free(list);
if (n <= 1)
return list;
domain = isl_ast_build_get_domain(build);
for (i = n - 1; i >= 0; --i) {
isl_pw_aff *pa_i;
isl_set *domain_i;
int empty;
domain_i = isl_set_copy(domain);
pa_i = isl_pw_aff_list_get_pw_aff(list, i);
for (j = 0; j < n; ++j) {
isl_pw_aff *pa_j;
isl_set *better;
if (j == i)
continue;
pa_j = isl_pw_aff_list_get_pw_aff(list, j);
better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
domain_i = isl_set_intersect(domain_i, better);
}
empty = isl_set_is_empty(domain_i);
isl_set_free(domain_i);
isl_pw_aff_free(pa_i);
if (empty < 0)
goto error;
if (!empty)
continue;
list = isl_pw_aff_list_drop(list, i, 1);
n--;
}
isl_set_free(domain);
return list;
error:
isl_set_free(domain);
return isl_pw_aff_list_free(list);
}
/* Extract a lower bound on dimension "pos" from each constraint
* in "constraints" and return the list of lower bounds.
* If "constraints" has zero elements, then we extract a lower bound
* from "domain" instead.
*
* If the current dimension is strided, then the lower bound
* is adjusted by lower_bound to match the stride information.
* This modification may make one or more lower bounds redundant
* with respect to the other lower bounds. We therefore check
* for this condition and remove the redundant lower bounds.
*/
static __isl_give isl_pw_aff_list *lower_bounds(
__isl_keep isl_constraint_list *constraints, int pos,
__isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
{
isl_ctx *ctx;
isl_pw_aff_list *list;
int i;
isl_size n;
if (!build)
return NULL;
n = isl_constraint_list_n_constraint(constraints);
if (n < 0)
return NULL;
if (n == 0) {
isl_pw_aff *pa;
pa = exact_bound(domain, build, 0);
return isl_pw_aff_list_from_pw_aff(pa);
}
ctx = isl_ast_build_get_ctx(build);
list = isl_pw_aff_list_alloc(ctx,n);
for (i = 0; i < n; ++i) {
isl_aff *aff;
isl_constraint *c;
c = isl_constraint_list_get_constraint(constraints, i);
aff = lower_bound(c, pos, build);
isl_constraint_free(c);
list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
}
if (isl_ast_build_has_stride(build, pos))
list = remove_redundant_lower_bounds(list, build);
return list;
}
/* Extract an upper bound on dimension "pos" from each constraint
* in "constraints" and return the list of upper bounds.
* If "constraints" has zero elements, then we extract an upper bound
* from "domain" instead.
*/
static __isl_give isl_pw_aff_list *upper_bounds(
__isl_keep isl_constraint_list *constraints, int pos,
__isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
{
isl_ctx *ctx;
isl_pw_aff_list *list;
int i;
isl_size n;
n = isl_constraint_list_n_constraint(constraints);
if (n < 0)
return NULL;
if (n == 0) {
isl_pw_aff *pa;
pa = exact_bound(domain, build, 1);
return isl_pw_aff_list_from_pw_aff(pa);
}
ctx = isl_ast_build_get_ctx(build);
list = isl_pw_aff_list_alloc(ctx,n);
for (i = 0; i < n; ++i) {
isl_aff *aff;
isl_constraint *c;
c = isl_constraint_list_get_constraint(constraints, i);
aff = isl_constraint_get_bound(c, isl_dim_set, pos);
isl_constraint_free(c);
aff = isl_aff_floor(aff);
list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
}
return list;
}
/* Return an isl_ast_expr that performs the reduction of type "type"
* on AST expressions corresponding to the elements in "list".
*
* The list is assumed to contain at least one element.
* If the list contains exactly one element, then the returned isl_ast_expr
* simply computes that affine expression.
* If the list contains more than one element, then we sort it
* using a fairly arbitrary but hopefully reasonably stable order.
*/
static __isl_give isl_ast_expr *reduce_list(enum isl_ast_expr_op_type type,
__isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
{
int i;
isl_size n;
isl_ctx *ctx;
isl_ast_expr *expr;
n = isl_pw_aff_list_n_pw_aff(list);
if (n < 0)
return NULL;
if (n == 1)
return isl_ast_build_expr_from_pw_aff_internal(build,
isl_pw_aff_list_get_pw_aff(list, 0));
ctx = isl_pw_aff_list_get_ctx(list);
expr = isl_ast_expr_alloc_op(ctx, type, n);
if (!expr)
return NULL;
list = isl_pw_aff_list_copy(list);
list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
if (!list)
return isl_ast_expr_free(expr);
for (i = 0; i < n; ++i) {
isl_ast_expr *expr_i;
expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
isl_pw_aff_list_get_pw_aff(list, i));
if (!expr_i)
goto error;
expr->u.op.args[i] = expr_i;
}
isl_pw_aff_list_free(list);
return expr;
error:
isl_pw_aff_list_free(list);
isl_ast_expr_free(expr);
return NULL;
}
/* Add guards implied by the "generated constraints",
* but not (necessarily) enforced by the generated AST to "guard".
* In particular, if there is any stride constraints,
* then add the guard implied by those constraints.
* If we have generated a degenerate loop, then add the guard
* implied by "bounds" on the outer dimensions, i.e., the guard
* that ensures that the single value actually exists.
* Since there may also be guards implied by a combination
* of these constraints, we first combine them before
* deriving the implied constraints.
*/
static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
int degenerate, __isl_keep isl_basic_set *bounds,
__isl_keep isl_ast_build *build)
{
isl_size depth;
isl_bool has_stride;
isl_space *space;
isl_set *dom, *set;
depth = isl_ast_build_get_depth(build);
has_stride = isl_ast_build_has_stride(build, depth);
if (depth < 0 || has_stride < 0)
return isl_set_free(guard);
if (!has_stride && !degenerate)
return guard;
space = isl_basic_set_get_space(bounds);
dom = isl_set_universe(space);
if (degenerate) {
bounds = isl_basic_set_copy(bounds);
bounds = isl_basic_set_drop_constraints_not_involving_dims(
bounds, isl_dim_set, depth, 1);
set = isl_set_from_basic_set(bounds);
dom = isl_set_intersect(dom, set);
}
if (has_stride) {
set = isl_ast_build_get_stride_constraint(build);
dom = isl_set_intersect(dom, set);
}
dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
dom = isl_ast_build_compute_gist(build, dom);
guard = isl_set_intersect(guard, dom);
return guard;
}
/* Update "graft" based on "sub_build" for the degenerate case.
*
* "build" is the build in which graft->node was created
* "sub_build" contains information about the current level itself,
* including the single value attained.
*
* We set the initialization part of the for loop to the single
* value attained by the current dimension.
* The increment and condition are not strictly needed as they are known
* to be "1" and "iterator <= value" respectively.
*/
static __isl_give isl_ast_graft *refine_degenerate(
__isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
__isl_keep isl_ast_build *sub_build)
{
isl_pw_aff *value;
if (!graft || !sub_build)
return isl_ast_graft_free(graft);
value = isl_pw_aff_copy(sub_build->value);
graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
value);
if (!graft->node->u.f.init)
return isl_ast_graft_free(graft);
return graft;
}
/* Return the intersection of constraints in "list" as a set.
*/
static __isl_give isl_set *intersect_constraints(
__isl_keep isl_constraint_list *list)
{
int i;
isl_size n;
isl_basic_set *bset;
n = isl_constraint_list_n_constraint(list);
if (n < 0)
return NULL;
if (n < 1)
isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
"expecting at least one constraint", return NULL);
bset = isl_basic_set_from_constraint(
isl_constraint_list_get_constraint(list, 0));
for (i = 1; i < n; ++i) {
isl_basic_set *bset_i;
bset_i = isl_basic_set_from_constraint(
isl_constraint_list_get_constraint(list, i));
bset = isl_basic_set_intersect(bset, bset_i);
}
return isl_set_from_basic_set(bset);
}
/* Compute the constraints on the outer dimensions enforced by
* graft->node and add those constraints to graft->enforced,
* in case the upper bound is expressed as a set "upper".
*
* In particular, if l(...) is a lower bound in "lower", and
*
* -a i + f(...) >= 0 or a i <= f(...)
*
* is an upper bound ocnstraint on the current dimension i,
* then the for loop enforces the constraint
*
* -a l(...) + f(...) >= 0 or a l(...) <= f(...)
*
* We therefore simply take each lower bound in turn, plug it into
* the upper bounds and compute the intersection over all lower bounds.
*
* If a lower bound is a rational expression, then
* isl_basic_set_preimage_multi_aff will force this rational
* expression to have only integer values. However, the loop
* itself does not enforce this integrality constraint. We therefore
* use the ceil of the lower bounds instead of the lower bounds themselves.
* Other constraints will make sure that the for loop is only executed
* when each of the lower bounds attains an integral value.
* In particular, potentially rational values only occur in
* lower_bound if the offset is a (seemingly) rational expression,
* but then outer conditions will make sure that this rational expression
* only attains integer values.
*/
static __isl_give isl_ast_graft *set_enforced_from_set(
__isl_take isl_ast_graft *graft,
__isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
{
isl_space *space;
isl_basic_set *enforced;
isl_pw_multi_aff *pma;
int i;
isl_size n;
n = isl_pw_aff_list_n_pw_aff(lower);
if (!graft || n < 0)
return isl_ast_graft_free(graft);
space = isl_set_get_space(upper);
enforced = isl_basic_set_universe(isl_space_copy(space));
space = isl_space_map_from_set(space);
pma = isl_pw_multi_aff_identity(space);
for (i = 0; i < n; ++i) {
isl_pw_aff *pa;
isl_set *enforced_i;
isl_basic_set *hull;
isl_pw_multi_aff *pma_i;
pa = isl_pw_aff_list_get_pw_aff(lower, i);
pa = isl_pw_aff_ceil(pa);
pma_i = isl_pw_multi_aff_copy(pma);
pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
enforced_i = isl_set_copy(upper);
enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
hull = isl_set_simple_hull(enforced_i);
enforced = isl_basic_set_intersect(enforced, hull);
}
isl_pw_multi_aff_free(pma);
graft = isl_ast_graft_enforce(graft, enforced);
return graft;
}
/* Compute the constraints on the outer dimensions enforced by
* graft->node and add those constraints to graft->enforced,
* in case the upper bound is expressed as
* a list of affine expressions "upper".
*
* The enforced condition is that each lower bound expression is less
* than or equal to each upper bound expression.
*/
static __isl_give isl_ast_graft *set_enforced_from_list(
__isl_take isl_ast_graft *graft,
__isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
{
isl_set *cond;
isl_basic_set *enforced;
lower = isl_pw_aff_list_copy(lower);
upper = isl_pw_aff_list_copy(upper);
cond = isl_pw_aff_list_le_set(lower, upper);
enforced = isl_set_simple_hull(cond);
graft = isl_ast_graft_enforce(graft, enforced);
return graft;
}
/* Does "aff" have a negative constant term?
*/
static isl_bool aff_constant_is_negative(__isl_keep isl_set *set,
__isl_keep isl_aff *aff, void *user)
{
isl_bool is_neg;
isl_val *v;
v = isl_aff_get_constant_val(aff);
is_neg = isl_val_is_neg(v);
isl_val_free(v);
return is_neg;
}
/* Does "pa" have a negative constant term over its entire domain?
*/
static isl_bool pw_aff_constant_is_negative(__isl_keep isl_pw_aff *pa,
void *user)
{
return isl_pw_aff_every_piece(pa, &aff_constant_is_negative, NULL);
}
/* Does each element in "list" have a negative constant term?
*/
static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
{
return isl_pw_aff_list_every(list, &pw_aff_constant_is_negative, NULL);
}
/* Add 1 to each of the elements in "list", where each of these elements
* is defined over the internal schedule space of "build".
*/
static __isl_give isl_pw_aff_list *list_add_one(
__isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
{
int i;
isl_size n;
isl_space *space;
isl_aff *aff;
isl_pw_aff *one;
n = isl_pw_aff_list_n_pw_aff(list);
if (n < 0)
return isl_pw_aff_list_free(list);
space = isl_ast_build_get_space(build, 1);
aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
aff = isl_aff_add_constant_si(aff, 1);
one = isl_pw_aff_from_aff(aff);
for (i = 0; i < n; ++i) {
isl_pw_aff *pa;
pa = isl_pw_aff_list_get_pw_aff(list, i);
pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
list = isl_pw_aff_list_set_pw_aff(list, i, pa);
}
isl_pw_aff_free(one);
return list;
}
/* Set the condition part of the for node graft->node in case
* the upper bound is represented as a list of piecewise affine expressions.
*