-
Notifications
You must be signed in to change notification settings - Fork 0
/
isl_flow.c
3349 lines (2918 loc) · 95.8 KB
/
isl_flow.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 2005-2007 Universiteit Leiden
* Copyright 2008-2009 Katholieke Universiteit Leuven
* Copyright 2010 INRIA Saclay
* Copyright 2012 Universiteit Leiden
* Copyright 2014 Ecole Normale Superieure
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
* Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
* and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
* B-3001 Leuven, Belgium
* and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
* ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
* and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
*/
#include <isl/val.h>
#include <isl/space.h>
#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_set.h>
#include <isl/union_map.h>
#include <isl/flow.h>
#include <isl/schedule_node.h>
#include <isl_sort.h>
#include <isl/stream.h>
enum isl_restriction_type {
isl_restriction_type_empty,
isl_restriction_type_none,
isl_restriction_type_input,
isl_restriction_type_output
};
struct isl_restriction {
enum isl_restriction_type type;
isl_set *source;
isl_set *sink;
};
/* Create a restriction of the given type.
*/
static __isl_give isl_restriction *isl_restriction_alloc(
__isl_take isl_map *source_map, enum isl_restriction_type type)
{
isl_ctx *ctx;
isl_restriction *restr;
if (!source_map)
return NULL;
ctx = isl_map_get_ctx(source_map);
restr = isl_calloc_type(ctx, struct isl_restriction);
if (!restr)
goto error;
restr->type = type;
isl_map_free(source_map);
return restr;
error:
isl_map_free(source_map);
return NULL;
}
/* Create a restriction that doesn't restrict anything.
*/
__isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
{
return isl_restriction_alloc(source_map, isl_restriction_type_none);
}
/* Create a restriction that removes everything.
*/
__isl_give isl_restriction *isl_restriction_empty(
__isl_take isl_map *source_map)
{
return isl_restriction_alloc(source_map, isl_restriction_type_empty);
}
/* Create a restriction on the input of the maximization problem
* based on the given source and sink restrictions.
*/
__isl_give isl_restriction *isl_restriction_input(
__isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
{
isl_ctx *ctx;
isl_restriction *restr;
if (!source_restr || !sink_restr)
goto error;
ctx = isl_set_get_ctx(source_restr);
restr = isl_calloc_type(ctx, struct isl_restriction);
if (!restr)
goto error;
restr->type = isl_restriction_type_input;
restr->source = source_restr;
restr->sink = sink_restr;
return restr;
error:
isl_set_free(source_restr);
isl_set_free(sink_restr);
return NULL;
}
/* Create a restriction on the output of the maximization problem
* based on the given source restriction.
*/
__isl_give isl_restriction *isl_restriction_output(
__isl_take isl_set *source_restr)
{
isl_ctx *ctx;
isl_restriction *restr;
if (!source_restr)
return NULL;
ctx = isl_set_get_ctx(source_restr);
restr = isl_calloc_type(ctx, struct isl_restriction);
if (!restr)
goto error;
restr->type = isl_restriction_type_output;
restr->source = source_restr;
return restr;
error:
isl_set_free(source_restr);
return NULL;
}
__isl_null isl_restriction *isl_restriction_free(
__isl_take isl_restriction *restr)
{
if (!restr)
return NULL;
isl_set_free(restr->source);
isl_set_free(restr->sink);
free(restr);
return NULL;
}
isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
{
return restr ? isl_set_get_ctx(restr->source) : NULL;
}
/* A private structure to keep track of a mapping together with
* a user-specified identifier and a boolean indicating whether
* the map represents a must or may access/dependence.
*/
struct isl_labeled_map {
struct isl_map *map;
void *data;
int must;
};
typedef isl_bool (*isl_access_coscheduled)(void *first, void *second);
/* A structure containing the input for dependence analysis:
* - a sink
* - n_must + n_may (<= max_source) sources
* - a function for determining the relative order of sources and sink
* - an optional function "coscheduled" for determining whether sources
* may be coscheduled. If "coscheduled" is NULL, then the sources
* are assumed not to be coscheduled.
* The must sources are placed before the may sources.
*
* domain_map is an auxiliary map that maps the sink access relation
* to the domain of this access relation.
* This field is only needed when restrict_fn is set and
* the field itself is set by isl_access_info_compute_flow.
*
* restrict_fn is a callback that (if not NULL) will be called
* right before any lexicographical maximization.
*/
struct isl_access_info {
isl_map *domain_map;
struct isl_labeled_map sink;
isl_access_level_before level_before;
isl_access_coscheduled coscheduled;
isl_access_restrict restrict_fn;
void *restrict_user;
int max_source;
int n_must;
int n_may;
struct isl_labeled_map source[1];
};
/* A structure containing the output of dependence analysis:
* - n_source dependences
* - a wrapped subset of the sink for which definitely no source could be found
* - a wrapped subset of the sink for which possibly no source could be found
*/
struct isl_flow {
isl_set *must_no_source;
isl_set *may_no_source;
int n_source;
struct isl_labeled_map *dep;
};
/* Construct an isl_access_info structure and fill it up with
* the given data. The number of sources is set to 0.
*/
__isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
void *sink_user, isl_access_level_before fn, int max_source)
{
isl_ctx *ctx;
struct isl_access_info *acc;
if (!sink)
return NULL;
ctx = isl_map_get_ctx(sink);
isl_assert(ctx, max_source >= 0, goto error);
acc = isl_calloc(ctx, struct isl_access_info,
sizeof(struct isl_access_info) +
(max_source - 1) * sizeof(struct isl_labeled_map));
if (!acc)
goto error;
acc->sink.map = sink;
acc->sink.data = sink_user;
acc->level_before = fn;
acc->max_source = max_source;
acc->n_must = 0;
acc->n_may = 0;
return acc;
error:
isl_map_free(sink);
return NULL;
}
/* Free the given isl_access_info structure.
*/
__isl_null isl_access_info *isl_access_info_free(
__isl_take isl_access_info *acc)
{
int i;
if (!acc)
return NULL;
isl_map_free(acc->domain_map);
isl_map_free(acc->sink.map);
for (i = 0; i < acc->n_must + acc->n_may; ++i)
isl_map_free(acc->source[i].map);
free(acc);
return NULL;
}
isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
{
return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
}
__isl_give isl_access_info *isl_access_info_set_restrict(
__isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
{
if (!acc)
return NULL;
acc->restrict_fn = fn;
acc->restrict_user = user;
return acc;
}
/* Add another source to an isl_access_info structure, making
* sure the "must" sources are placed before the "may" sources.
* This function may be called at most max_source times on a
* given isl_access_info structure, with max_source as specified
* in the call to isl_access_info_alloc that constructed the structure.
*/
__isl_give isl_access_info *isl_access_info_add_source(
__isl_take isl_access_info *acc, __isl_take isl_map *source,
int must, void *source_user)
{
isl_ctx *ctx;
if (!acc)
goto error;
ctx = isl_map_get_ctx(acc->sink.map);
isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
if (must) {
if (acc->n_may)
acc->source[acc->n_must + acc->n_may] =
acc->source[acc->n_must];
acc->source[acc->n_must].map = source;
acc->source[acc->n_must].data = source_user;
acc->source[acc->n_must].must = 1;
acc->n_must++;
} else {
acc->source[acc->n_must + acc->n_may].map = source;
acc->source[acc->n_must + acc->n_may].data = source_user;
acc->source[acc->n_must + acc->n_may].must = 0;
acc->n_may++;
}
return acc;
error:
isl_map_free(source);
isl_access_info_free(acc);
return NULL;
}
/* A helper struct carrying the isl_access_info and an error condition.
*/
struct access_sort_info {
isl_access_info *access_info;
int error;
};
/* Return -n, 0 or n (with n a positive value), depending on whether
* the source access identified by p1 should be sorted before, together
* or after that identified by p2.
*
* If p1 appears before p2, then it should be sorted first.
* For more generic initial schedules, it is possible that neither
* p1 nor p2 appears before the other, or at least not in any obvious way.
* We therefore also check if p2 appears before p1, in which case p2
* should be sorted first.
* If not, we try to order the two statements based on the description
* of the iteration domains. This results in an arbitrary, but fairly
* stable ordering.
*
* In case of an error, sort_info.error is set to true and all elements are
* reported to be equal.
*/
static int access_sort_cmp(const void *p1, const void *p2, void *user)
{
struct access_sort_info *sort_info = user;
isl_access_info *acc = sort_info->access_info;
if (sort_info->error)
return 0;
const struct isl_labeled_map *i1, *i2;
int level1, level2;
uint32_t h1, h2;
i1 = (const struct isl_labeled_map *) p1;
i2 = (const struct isl_labeled_map *) p2;
level1 = acc->level_before(i1->data, i2->data);
if (level1 < 0)
goto error;
if (level1 % 2)
return -1;
level2 = acc->level_before(i2->data, i1->data);
if (level2 < 0)
goto error;
if (level2 % 2)
return 1;
h1 = isl_map_get_hash(i1->map);
h2 = isl_map_get_hash(i2->map);
return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
error:
sort_info->error = 1;
return 0;
}
/* Sort the must source accesses in their textual order.
*/
static __isl_give isl_access_info *isl_access_info_sort_sources(
__isl_take isl_access_info *acc)
{
struct access_sort_info sort_info;
sort_info.access_info = acc;
sort_info.error = 0;
if (!acc)
return NULL;
if (acc->n_must <= 1)
return acc;
if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
access_sort_cmp, &sort_info) < 0)
return isl_access_info_free(acc);
if (sort_info.error)
return isl_access_info_free(acc);
return acc;
}
/* Align the parameters of the two spaces if needed and then call
* isl_space_join.
*/
static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
__isl_take isl_space *right)
{
isl_bool equal_params;
equal_params = isl_space_has_equal_params(left, right);
if (equal_params < 0)
goto error;
if (equal_params)
return isl_space_join(left, right);
left = isl_space_align_params(left, isl_space_copy(right));
right = isl_space_align_params(right, isl_space_copy(left));
return isl_space_join(left, right);
error:
isl_space_free(left);
isl_space_free(right);
return NULL;
}
/* Initialize an empty isl_flow structure corresponding to a given
* isl_access_info structure.
* For each must access, two dependences are created (initialized
* to the empty relation), one for the resulting must dependences
* and one for the resulting may dependences. May accesses can
* only lead to may dependences, so only one dependence is created
* for each of them.
* This function is private as isl_flow structures are only supposed
* to be created by isl_access_info_compute_flow.
*/
static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
{
int i, n;
struct isl_ctx *ctx;
struct isl_flow *dep;
if (!acc)
return NULL;
ctx = isl_map_get_ctx(acc->sink.map);
dep = isl_calloc_type(ctx, struct isl_flow);
if (!dep)
return NULL;
n = 2 * acc->n_must + acc->n_may;
dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
if (n && !dep->dep)
goto error;
dep->n_source = n;
for (i = 0; i < acc->n_must; ++i) {
isl_space *dim;
dim = space_align_and_join(
isl_map_get_space(acc->source[i].map),
isl_space_reverse(isl_map_get_space(acc->sink.map)));
dep->dep[2 * i].map = isl_map_empty(dim);
dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
dep->dep[2 * i].data = acc->source[i].data;
dep->dep[2 * i + 1].data = acc->source[i].data;
dep->dep[2 * i].must = 1;
dep->dep[2 * i + 1].must = 0;
if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
goto error;
}
for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
isl_space *dim;
dim = space_align_and_join(
isl_map_get_space(acc->source[i].map),
isl_space_reverse(isl_map_get_space(acc->sink.map)));
dep->dep[acc->n_must + i].map = isl_map_empty(dim);
dep->dep[acc->n_must + i].data = acc->source[i].data;
dep->dep[acc->n_must + i].must = 0;
if (!dep->dep[acc->n_must + i].map)
goto error;
}
return dep;
error:
isl_flow_free(dep);
return NULL;
}
/* Iterate over all sources and for each resulting flow dependence
* that is not empty, call the user specfied function.
* The second argument in this function call identifies the source,
* while the third argument correspond to the final argument of
* the isl_flow_foreach call.
*/
isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
void *user),
void *user)
{
int i;
if (!deps)
return isl_stat_error;
for (i = 0; i < deps->n_source; ++i) {
if (isl_map_plain_is_empty(deps->dep[i].map))
continue;
if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
deps->dep[i].data, user) < 0)
return isl_stat_error;
}
return isl_stat_ok;
}
/* Return a copy of the subset of the sink for which no source could be found.
*/
__isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
{
if (!deps)
return NULL;
if (must)
return isl_set_unwrap(isl_set_copy(deps->must_no_source));
else
return isl_set_unwrap(isl_set_copy(deps->may_no_source));
}
__isl_null isl_flow *isl_flow_free(__isl_take isl_flow *deps)
{
int i;
if (!deps)
return NULL;
isl_set_free(deps->must_no_source);
isl_set_free(deps->may_no_source);
if (deps->dep) {
for (i = 0; i < deps->n_source; ++i)
isl_map_free(deps->dep[i].map);
free(deps->dep);
}
free(deps);
return NULL;
}
isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
{
return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
}
/* Return a map that enforces that the domain iteration occurs after
* the range iteration at the given level.
* If level is odd, then the domain iteration should occur after
* the target iteration in their shared level/2 outermost loops.
* In this case we simply need to enforce that these outermost
* loop iterations are the same.
* If level is even, then the loop iterator of the domain should
* be greater than the loop iterator of the range at the last
* of the level/2 shared loops, i.e., loop level/2 - 1.
*/
static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
{
struct isl_basic_map *bmap;
if (level % 2)
bmap = isl_basic_map_equal(dim, level/2);
else
bmap = isl_basic_map_more_at(dim, level/2 - 1);
return isl_map_from_basic_map(bmap);
}
/* Compute the partial lexicographic maximum of "dep" on domain "sink",
* but first check if the user has set acc->restrict_fn and if so
* update either the input or the output of the maximization problem
* with respect to the resulting restriction.
*
* Since the user expects a mapping from sink iterations to source iterations,
* whereas the domain of "dep" is a wrapped map, mapping sink iterations
* to accessed array elements, we first need to project out the accessed
* sink array elements by applying acc->domain_map.
* Similarly, the sink restriction specified by the user needs to be
* converted back to the wrapped map.
*/
static __isl_give isl_map *restricted_partial_lexmax(
__isl_keep isl_access_info *acc, __isl_take isl_map *dep,
int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
{
isl_map *source_map;
isl_restriction *restr;
isl_set *sink_domain;
isl_set *sink_restr;
isl_map *res;
if (!acc->restrict_fn)
return isl_map_partial_lexmax(dep, sink, empty);
source_map = isl_map_copy(dep);
source_map = isl_map_apply_domain(source_map,
isl_map_copy(acc->domain_map));
sink_domain = isl_set_copy(sink);
sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
restr = acc->restrict_fn(source_map, sink_domain,
acc->source[source].data, acc->restrict_user);
isl_set_free(sink_domain);
isl_map_free(source_map);
if (!restr)
goto error;
if (restr->type == isl_restriction_type_input) {
dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
sink_restr = isl_set_copy(restr->sink);
sink_restr = isl_set_apply(sink_restr,
isl_map_reverse(isl_map_copy(acc->domain_map)));
sink = isl_set_intersect(sink, sink_restr);
} else if (restr->type == isl_restriction_type_empty) {
isl_space *space = isl_map_get_space(dep);
isl_map_free(dep);
dep = isl_map_empty(space);
}
res = isl_map_partial_lexmax(dep, sink, empty);
if (restr->type == isl_restriction_type_output)
res = isl_map_intersect_range(res, isl_set_copy(restr->source));
isl_restriction_free(restr);
return res;
error:
isl_map_free(dep);
isl_set_free(sink);
*empty = NULL;
return NULL;
}
/* Compute the last iteration of must source j that precedes the sink
* at the given level for sink iterations in set_C.
* The subset of set_C for which no such iteration can be found is returned
* in *empty.
*/
static struct isl_map *last_source(struct isl_access_info *acc,
struct isl_set *set_C,
int j, int level, struct isl_set **empty)
{
struct isl_map *read_map;
struct isl_map *write_map;
struct isl_map *dep_map;
struct isl_map *after;
struct isl_map *result;
read_map = isl_map_copy(acc->sink.map);
write_map = isl_map_copy(acc->source[j].map);
write_map = isl_map_reverse(write_map);
dep_map = isl_map_apply_range(read_map, write_map);
after = after_at_level(isl_map_get_space(dep_map), level);
dep_map = isl_map_intersect(dep_map, after);
result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
result = isl_map_reverse(result);
return result;
}
/* For a given mapping between iterations of must source j and iterations
* of the sink, compute the last iteration of must source k preceding
* the sink at level before_level for any of the sink iterations,
* but following the corresponding iteration of must source j at level
* after_level.
*/
static struct isl_map *last_later_source(struct isl_access_info *acc,
struct isl_map *old_map,
int j, int before_level,
int k, int after_level,
struct isl_set **empty)
{
isl_space *dim;
struct isl_set *set_C;
struct isl_map *read_map;
struct isl_map *write_map;
struct isl_map *dep_map;
struct isl_map *after_write;
struct isl_map *before_read;
struct isl_map *result;
set_C = isl_map_range(isl_map_copy(old_map));
read_map = isl_map_copy(acc->sink.map);
write_map = isl_map_copy(acc->source[k].map);
write_map = isl_map_reverse(write_map);
dep_map = isl_map_apply_range(read_map, write_map);
dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
isl_space_reverse(isl_map_get_space(acc->source[j].map)));
after_write = after_at_level(dim, after_level);
after_write = isl_map_apply_range(after_write, old_map);
after_write = isl_map_reverse(after_write);
dep_map = isl_map_intersect(dep_map, after_write);
before_read = after_at_level(isl_map_get_space(dep_map), before_level);
dep_map = isl_map_intersect(dep_map, before_read);
result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
result = isl_map_reverse(result);
return result;
}
/* Given a shared_level between two accesses, return 1 if the
* the first can precede the second at the requested target_level.
* If the target level is odd, i.e., refers to a statement level
* dimension, then first needs to precede second at the requested
* level, i.e., shared_level must be equal to target_level.
* If the target level is odd, then the two loops should share
* at least the requested number of outer loops.
*/
static int can_precede_at_level(int shared_level, int target_level)
{
if (shared_level < target_level)
return 0;
if ((target_level % 2) && shared_level > target_level)
return 0;
return 1;
}
/* Given a possible flow dependence temp_rel[j] between source j and the sink
* at level sink_level, remove those elements for which
* there is an iteration of another source k < j that is closer to the sink.
* The flow dependences temp_rel[k] are updated with the improved sources.
* Any improved source needs to precede the sink at the same level
* and needs to follow source j at the same or a deeper level.
* The lower this level, the later the execution date of source k.
* We therefore consider lower levels first.
*
* If temp_rel[j] is empty, then there can be no improvement and
* we return immediately.
*
* This function returns isl_stat_ok in case it was executed successfully and
* isl_stat_error in case of errors during the execution of this function.
*/
static isl_stat intermediate_sources(__isl_keep isl_access_info *acc,
struct isl_map **temp_rel, int j, int sink_level)
{
int k, level;
isl_size n_in = isl_map_dim(acc->source[j].map, isl_dim_in);
int depth = 2 * n_in + 1;
if (n_in < 0)
return isl_stat_error;
if (isl_map_plain_is_empty(temp_rel[j]))
return isl_stat_ok;
for (k = j - 1; k >= 0; --k) {
int plevel, plevel2;
plevel = acc->level_before(acc->source[k].data, acc->sink.data);
if (plevel < 0)
return isl_stat_error;
if (!can_precede_at_level(plevel, sink_level))
continue;
plevel2 = acc->level_before(acc->source[j].data,
acc->source[k].data);
if (plevel2 < 0)
return isl_stat_error;
for (level = sink_level; level <= depth; ++level) {
struct isl_map *T;
struct isl_set *trest;
struct isl_map *copy;
if (!can_precede_at_level(plevel2, level))
continue;
copy = isl_map_copy(temp_rel[j]);
T = last_later_source(acc, copy, j, sink_level, k,
level, &trest);
if (isl_map_plain_is_empty(T)) {
isl_set_free(trest);
isl_map_free(T);
continue;
}
temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
}
}
return isl_stat_ok;
}
/* Compute all iterations of may source j that precedes the sink at the given
* level for sink iterations in set_C.
*/
static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
__isl_take isl_set *set_C, int j, int level)
{
isl_map *read_map;
isl_map *write_map;
isl_map *dep_map;
isl_map *after;
read_map = isl_map_copy(acc->sink.map);
read_map = isl_map_intersect_domain(read_map, set_C);
write_map = isl_map_copy(acc->source[acc->n_must + j].map);
write_map = isl_map_reverse(write_map);
dep_map = isl_map_apply_range(read_map, write_map);
after = after_at_level(isl_map_get_space(dep_map), level);
dep_map = isl_map_intersect(dep_map, after);
return isl_map_reverse(dep_map);
}
/* For a given mapping between iterations of must source k and iterations
* of the sink, compute all iterations of may source j preceding
* the sink at level before_level for any of the sink iterations,
* but following the corresponding iteration of must source k at level
* after_level.
*/
static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
__isl_take isl_map *old_map,
int j, int before_level, int k, int after_level)
{
isl_space *dim;
isl_set *set_C;
isl_map *read_map;
isl_map *write_map;
isl_map *dep_map;
isl_map *after_write;
isl_map *before_read;
set_C = isl_map_range(isl_map_copy(old_map));
read_map = isl_map_copy(acc->sink.map);
read_map = isl_map_intersect_domain(read_map, set_C);
write_map = isl_map_copy(acc->source[acc->n_must + j].map);
write_map = isl_map_reverse(write_map);
dep_map = isl_map_apply_range(read_map, write_map);
dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
isl_space_reverse(isl_map_get_space(acc->source[k].map)));
after_write = after_at_level(dim, after_level);
after_write = isl_map_apply_range(after_write, old_map);
after_write = isl_map_reverse(after_write);
dep_map = isl_map_intersect(dep_map, after_write);
before_read = after_at_level(isl_map_get_space(dep_map), before_level);
dep_map = isl_map_intersect(dep_map, before_read);
return isl_map_reverse(dep_map);
}
/* Given the must and may dependence relations for the must accesses
* for level sink_level, check if there are any accesses of may access j
* that occur in between and return their union.
* If some of these accesses are intermediate with respect to
* (previously thought to be) must dependences, then these
* must dependences are turned into may dependences.
*/
static __isl_give isl_map *all_intermediate_sources(
__isl_keep isl_access_info *acc, __isl_take isl_map *map,
struct isl_map **must_rel, struct isl_map **may_rel,
int j, int sink_level)
{
int k, level;
isl_size n_in = isl_map_dim(acc->source[acc->n_must + j].map,
isl_dim_in);
int depth = 2 * n_in + 1;
if (n_in < 0)
return isl_map_free(map);
for (k = 0; k < acc->n_must; ++k) {
int plevel;
if (isl_map_plain_is_empty(may_rel[k]) &&
isl_map_plain_is_empty(must_rel[k]))
continue;
plevel = acc->level_before(acc->source[k].data,
acc->source[acc->n_must + j].data);
if (plevel < 0)
return isl_map_free(map);
for (level = sink_level; level <= depth; ++level) {
isl_map *T;
isl_map *copy;
isl_set *ran;
if (!can_precede_at_level(plevel, level))
continue;
copy = isl_map_copy(may_rel[k]);
T = all_later_sources(acc, copy, j, sink_level, k, level);
map = isl_map_union(map, T);
copy = isl_map_copy(must_rel[k]);
T = all_later_sources(acc, copy, j, sink_level, k, level);
ran = isl_map_range(isl_map_copy(T));
map = isl_map_union(map, T);
may_rel[k] = isl_map_union_disjoint(may_rel[k],
isl_map_intersect_range(isl_map_copy(must_rel[k]),
isl_set_copy(ran)));
T = isl_map_from_domain_and_range(
isl_set_universe(
isl_space_domain(isl_map_get_space(must_rel[k]))),
ran);
must_rel[k] = isl_map_subtract(must_rel[k], T);
}
}
return map;
}
/* Given a dependence relation "old_map" between a must-source and the sink,
* return a subset of the dependences, augmented with instances
* of the source at position "pos" in "acc" that are coscheduled
* with the must-source and that access the same element.
* That is, if the input lives in a space T -> K, then the output
* lives in the space [T -> S] -> K, with S the space of source "pos", and
* the domain factor of the domain product is a subset of the input.
* The sources are considered to be coscheduled if they have the same values
* for the initial "depth" coordinates.
*
* First construct a dependence relation S -> K and a mapping
* between coscheduled sources T -> S.
* The second is combined with the original dependence relation T -> K
* to form a relation in T -> [S -> K], which is subsequently
* uncurried to [T -> S] -> K.
* This result is then intersected with the dependence relation S -> K
* to form the output.
*
* In case a negative depth is given, NULL is returned to indicate an error.
*/
static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
__isl_keep isl_map *old_map, int pos, int depth)
{
isl_space *space;
isl_set *set_C;
isl_map *read_map;
isl_map *write_map;
isl_map *dep_map;
isl_map *equal;
isl_map *map;
if (depth < 0)
return NULL;
set_C = isl_map_range(isl_map_copy(old_map));
read_map = isl_map_copy(acc->sink.map);
read_map = isl_map_intersect_domain(read_map, set_C);
write_map = isl_map_copy(acc->source[pos].map);
dep_map = isl_map_domain_product(write_map, read_map);
dep_map = isl_set_unwrap(isl_map_domain(dep_map));
space = isl_space_join(isl_map_get_space(old_map),
isl_space_reverse(isl_map_get_space(dep_map)));
equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
map = isl_map_range_product(equal, isl_map_copy(old_map));
map = isl_map_uncurry(map);
map = isl_map_intersect_domain_factor_range(map, dep_map);
return map;
}
/* After the dependences derived from a must-source have been computed
* at a certain level, check if any of the sources of the must-dependences
* may be coscheduled with other sources.
* If they are any such sources, then there is no way of determining
* which of the sources actually comes last and the must-dependences
* need to be turned into may-dependences, while dependences from
* the other sources need to be added to the may-dependences as well.
* "acc" describes the sources and a callback for checking whether
* two sources may be coscheduled. If acc->coscheduled is NULL then
* the sources are assumed not to be coscheduled.
* "must_rel" and "may_rel" describe the must and may-dependence relations
* computed at the current level for the must-sources. Some of the dependences
* may be moved from "must_rel" to "may_rel".
* "flow" contains all dependences computed so far (apart from those
* in "must_rel" and "may_rel") and may be updated with additional
* dependences derived from may-sources.
*
* In particular, consider all the must-sources with a non-empty
* dependence relation in "must_rel". They are considered in reverse
* order because that is the order in which they are considered in the caller.
* If any of the must-sources are coscheduled, then the last one
* is the one that will have a corresponding dependence relation.
* For each must-source i, consider both all the previous must-sources
* and all the may-sources. If any of those may be coscheduled with
* must-source i, then compute the coscheduled instances that access
* the same memory elements. The result is a relation [T -> S] -> K.
* The projection onto T -> K is a subset of the must-dependence relation
* that needs to be turned into may-dependences.
* The projection onto S -> K needs to be added to the may-dependences
* of source S.
* Since a given must-source instance may be coscheduled with several
* other source instances, the dependences that need to be turned
* into may-dependences are first collected and only actually removed
* from the must-dependences after all other sources have been considered.
*/
static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
__isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
__isl_take isl_flow *flow)
{
int i, j;
if (!acc->coscheduled)
return flow;
for (i = acc->n_must - 1; i >= 0; --i) {
isl_map *move;
if (isl_map_plain_is_empty(must_rel[i]))
continue;
move = isl_map_empty(isl_map_get_space(must_rel[i]));
for (j = i - 1; j >= 0; --j) {
int depth;
isl_bool coscheduled;
isl_map *map, *factor;