-
Notifications
You must be signed in to change notification settings - Fork 37
/
hybrid.c
2242 lines (1949 loc) · 64.9 KB
/
hybrid.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 2013 Ecole Normale Superieure
* Copyright 2015 Sven Verdoolaege
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege,
* Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
*/
#include <string.h>
#include <isl/space.h>
#include <isl/constraint.h>
#include <isl/val.h>
#include <isl/aff.h>
#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_set.h>
#include <isl/union_map.h>
#include "hybrid.h"
#include "schedule.h"
/* The hybrid tiling implemented in this file is based on
* Grosser et al., "Hybrid Hexagonal/Classical Tiling for GPUs".
*/
/* Bounds on relative dependence distances in input to hybrid tiling.
* upper is an upper bound on the relative dependence distances
* in the first space dimension
* -lower is a lower bound on the relative dependence distances
* in all space dimensions.
*
* In particular,
*
* d_i >= -lower_i d_0
* and
* d_1 <= upper d_0
*
* for each dependence distance vector d, where d_1 is the component
* corresponding to the first space dimension.
*
* upper and lower are always non-negative.
* Some of the values may be NaN if no bound could be found.
*/
struct ppcg_ht_bounds {
isl_val *upper;
isl_multi_val *lower;
};
/* Free "bounds" along with all its fields.
*/
__isl_null ppcg_ht_bounds *ppcg_ht_bounds_free(
__isl_take ppcg_ht_bounds *bounds)
{
if (!bounds)
return NULL;
isl_val_free(bounds->upper);
isl_multi_val_free(bounds->lower);
free(bounds);
return NULL;
}
/* Create a ppcg_ht_bounds object for a band living in "space".
* The bounds are initialized to NaN.
*/
__isl_give ppcg_ht_bounds *ppcg_ht_bounds_alloc(__isl_take isl_space *space)
{
int i, n;
isl_ctx *ctx;
ppcg_ht_bounds *bounds;
if (!space)
return NULL;
ctx = isl_space_get_ctx(space);
bounds = isl_alloc_type(ctx, struct ppcg_ht_bounds);
if (!bounds)
goto error;
bounds->upper = isl_val_nan(ctx);
bounds->lower = isl_multi_val_zero(space);
n = isl_multi_val_dim(bounds->lower, isl_dim_set);
for (i = 0; i < n; ++i) {
isl_val *v = isl_val_copy(bounds->upper);
bounds->lower = isl_multi_val_set_val(bounds->lower, i, v);
}
if (!bounds->lower || !bounds->upper)
return ppcg_ht_bounds_free(bounds);
return bounds;
error:
isl_space_free(space);
return NULL;
}
void ppcg_ht_bounds_dump(__isl_keep ppcg_ht_bounds *bounds)
{
if (!bounds)
return;
fprintf(stderr, "lower: ");
isl_multi_val_dump(bounds->lower);
fprintf(stderr, "upper: ");
isl_val_dump(bounds->upper);
}
/* Return the upper bound on the relative dependence distances
* in the first space dimension.
*/
__isl_give isl_val *ppcg_ht_bounds_get_upper(__isl_keep ppcg_ht_bounds *bounds)
{
if (!bounds)
return NULL;
return isl_val_copy(bounds->upper);
}
/* Replace the upper bound on the relative dependence distances
* in the first space dimension by "upper".
*/
__isl_give ppcg_ht_bounds *ppcg_ht_bounds_set_upper(
__isl_take ppcg_ht_bounds *bounds, __isl_take isl_val *upper)
{
if (!bounds || !upper)
goto error;
isl_val_free(bounds->upper);
bounds->upper = upper;
return bounds;
error:
ppcg_ht_bounds_free(bounds);
isl_val_free(upper);
return NULL;
}
/* Return the lower bound on the relative dependence distances
* in space dimension "pos".
*/
__isl_give isl_val *ppcg_ht_bounds_get_lower(__isl_keep ppcg_ht_bounds *bounds,
int pos)
{
if (!bounds)
return NULL;
return isl_multi_val_get_val(bounds->lower, pos);
}
/* Replace the lower bound on the relative dependence distances
* in space dimension "pos" by "lower".
*/
__isl_give ppcg_ht_bounds *ppcg_ht_bounds_set_lower(
__isl_take ppcg_ht_bounds *bounds, int pos, __isl_take isl_val *lower)
{
if (!bounds || !lower)
goto error;
bounds->lower = isl_multi_val_set_val(bounds->lower, pos, lower);
if (!bounds->lower)
return ppcg_ht_bounds_free(bounds);
return bounds;
error:
ppcg_ht_bounds_free(bounds);
isl_val_free(lower);
return NULL;
}
/* Can the bounds on relative dependence distances recorded in "bounds"
* be used to perform hybrid tiling?
* In particular, have appropriate lower and upper bounds been found?
* Any NaN indicates that no corresponding bound was found.
*/
isl_bool ppcg_ht_bounds_is_valid(__isl_keep ppcg_ht_bounds *bounds)
{
isl_bool is_nan;
int i, n;
if (!bounds)
return isl_bool_error;
is_nan = isl_val_is_nan(bounds->upper);
if (is_nan < 0)
return isl_bool_error;
if (is_nan)
return isl_bool_false;
n = isl_multi_val_dim(bounds->lower, isl_dim_set);
for (i = 0; i < n; ++i) {
isl_val *v;
v = isl_multi_val_get_val(bounds->lower, i);
is_nan = isl_val_is_nan(v);
if (is_nan < 0)
return isl_bool_error;
if (is_nan)
return isl_bool_false;
isl_val_free(v);
}
return isl_bool_true;
}
/* Structure that represents the basic hexagonal tiling,
* along with information that is needed to perform the hybrid tiling.
*
* "bounds" are the bounds on the dependence distances that
* define the hexagonal shape and the required skewing in the remaining
* space dimensions.
*
* "input_node" points to the input pair of band nodes.
* "input_schedule" is the partial schedule of this input pair of band nodes.
* The space of this schedule is [P -> C], where P is the space
* of the parent node and C is the space of the child node.
*
* "space_sizes" represent the total size of a tile for the space
* dimensions, i.e., those corresponding to the child node.
* The space of "space_sizes" is C.
* If S_0 is the original tile size in the first space dimension,
* then the first entry of "space_sizes" is equal to
* W = 2*S_0 + floor(d_l h) + floor(d_u h).
* The remaining entries are the same as in the original tile sizes.
*
* The basic hexagonal tiling "hex" is defined
* in a "ts" (time-space) space and corresponds to the phase-1 tiles.
* "time_tile" maps the "ts" space to outer time tile.
* Is is equal to ts[t, s] -> floor(t/(2 * S_t)), with S_t the original tile
* size corresponding to the parent node.
* "local_time" maps the "ts" space to the time dimension inside each tile.
* It is equal to ts[t, s] -> t mod (2 S_t), with S_t the original tile
* size corresponding to the parent node.
* "shift_space" shifts the tiles at time tile T = floor(t/(2 S_t))
* in the space dimension such that they align to a multiple of W.
* It is equal to ts[t, s] -> s + (-(2 * shift_s)*T) % W,
* with shift_s = S_0 + floor(d_u h).
* "shift_phase" is the shift taken to go from phase 0 to phase 1
* It is equal to ts[t, s] -> ts[t + S_t, s + shift_s],
* with shift_s = S_0 + floor(d_u h).
*
* "project_ts" projects the space of the input schedule to the ts-space.
* It is equal to [P[t] -> C[s_0, ...]] -> ts[t, s_0].
*/
struct ppcg_ht_tiling {
int ref;
ppcg_ht_bounds *bounds;
isl_schedule_node *input_node;
isl_multi_union_pw_aff *input_schedule;
isl_multi_val *space_sizes;
isl_aff *time_tile;
isl_aff *local_time;
isl_aff *shift_space;
isl_multi_aff *shift_phase;
isl_set *hex;
isl_multi_aff *project_ts;
};
typedef struct ppcg_ht_tiling ppcg_ht_tiling;
/* Return the space of the pair of band nodes that form the input
* to the hybrid tiling.
* In particular, return the space [P -> C], where P is the space
* of the parent node and C is the space of the child node.
*/
__isl_give isl_space *ppcg_ht_tiling_get_input_space(
__isl_keep ppcg_ht_tiling *tile)
{
if (!tile)
return NULL;
return isl_multi_union_pw_aff_get_space(tile->input_schedule);
}
/* Remove a reference to "tile" and free "tile" along with all its fields
* as soon as the reference count drops to zero.
*/
static __isl_null ppcg_ht_tiling *ppcg_ht_tiling_free(
__isl_take ppcg_ht_tiling *tiling)
{
if (!tiling)
return NULL;
if (--tiling->ref > 0)
return NULL;
ppcg_ht_bounds_free(tiling->bounds);
isl_schedule_node_free(tiling->input_node);
isl_multi_union_pw_aff_free(tiling->input_schedule);
isl_multi_val_free(tiling->space_sizes);
isl_aff_free(tiling->time_tile);
isl_aff_free(tiling->local_time);
isl_aff_free(tiling->shift_space);
isl_multi_aff_free(tiling->shift_phase);
isl_set_free(tiling->hex);
isl_multi_aff_free(tiling->project_ts);
free(tiling);
return NULL;
}
/* Return a new reference to "tiling".
*/
__isl_give ppcg_ht_tiling *ppcg_ht_tiling_copy(
__isl_keep ppcg_ht_tiling *tiling)
{
if (!tiling)
return NULL;
tiling->ref++;
return tiling;
}
/* Return the isl_ctx to which "tiling" belongs.
*/
isl_ctx *ppcg_ht_tiling_get_ctx(__isl_keep ppcg_ht_tiling *tiling)
{
if (!tiling)
return NULL;
return isl_multi_union_pw_aff_get_ctx(tiling->input_schedule);
}
/* Representation of one of the two phases of hybrid tiling.
*
* "tiling" points to the shared tiling data.
*
* "time_tile", "local_time" and "shift_space" are equal to the corresponding
* fields of "tiling", pulled back to the input space.
* In case of phase 0, these expressions have also been moved
* from phase 1 to phase 0.
*
* "domain" contains the hexagonal tiling of this phase.
*
* "space_shift" is the shift that should be added to the space band
* in order to be able to apply rectangular tiling to the space.
* For phase 1, it is equal to
*
* [P[t] -> C[s_0, s_i]] -> C[(-(2 * shift_s)*T) % W, dl_i * u]
*
* with shift_s = S_0 + floor(d_u h),
* T equal to "time_tile" and u equal to "local_time".
* For phase 0, it is equal to
*
* [P[t] -> C[s_0, s_i]] -> C[shift_s + (-(2 * shift_s)*T) % W, dl_i * u]
*
* "space_tile" is the space tiling. It is equal to
*
* [P[t] -> C[s]] -> C[floor((s + space_shift)/space_size]
*/
struct ppcg_ht_phase {
ppcg_ht_tiling *tiling;
isl_aff *time_tile;
isl_aff *local_time;
isl_aff *shift_space;
isl_set *domain;
isl_multi_aff *space_shift;
isl_multi_aff *space_tile;
};
/* Free "phase" along with all its fields.
*/
static __isl_null ppcg_ht_phase *ppcg_ht_phase_free(
__isl_take ppcg_ht_phase *phase)
{
if (!phase)
return NULL;
ppcg_ht_tiling_free(phase->tiling);
isl_aff_free(phase->time_tile);
isl_aff_free(phase->local_time);
isl_aff_free(phase->shift_space);
isl_set_free(phase->domain);
isl_multi_aff_free(phase->space_shift);
isl_multi_aff_free(phase->space_tile);
free(phase);
return NULL;
}
/* Wrapper around ppcg_ht_phase_free for use as an argument
* to isl_id_set_free_user.
*/
static void ppcg_ht_phase_free_wrap(void *user)
{
ppcg_ht_phase *phase = user;
ppcg_ht_phase_free(phase);
}
/* Return the domain of hybrid tiling phase "phase".
*/
static __isl_give isl_set *ppcg_ht_phase_get_domain(ppcg_ht_phase *phase)
{
if (!phase)
return NULL;
return isl_set_copy(phase->domain);
}
/* Return the space of the pair of band nodes that form the input
* to the hybrid tiling of which "phase" is a phase.
* In particular, return the space [P -> C], where P is the space
* of the parent node and C is the space of the child node.
*/
static __isl_give isl_space *ppcg_ht_phase_get_input_space(
__isl_keep ppcg_ht_phase *phase)
{
if (!phase)
return NULL;
return ppcg_ht_tiling_get_input_space(phase->tiling);
}
/* Construct the lower left constraint of the hexagonal tile, i.e.,
*
* du a - b <= (2h+1) du - duh
* -du a + b + (2h+1) du - duh >= 0
*
* where duh = floor(du * h).
*
* This constraint corresponds to (6) in
* "Hybrid Hexagonal/Classical Tiling for GPUs".
*/
static __isl_give isl_constraint *hex_lower_left(__isl_take isl_local_space *ls,
__isl_keep isl_val *h, __isl_keep isl_val *du, __isl_keep isl_val *duh)
{
isl_val *v;
isl_aff *aff;
v = isl_val_add_ui(isl_val_mul_ui(isl_val_copy(h), 2), 1);
v = isl_val_mul(v, isl_val_copy(du));
v = isl_val_sub(v, isl_val_copy(duh));
aff = isl_aff_val_on_domain(ls, v);
v = isl_val_neg(isl_val_copy(du));
aff = isl_aff_set_coefficient_val(aff, isl_dim_in, 0, v);
aff = isl_aff_set_coefficient_si(aff, isl_dim_in, 1, 1);
return isl_inequality_from_aff(aff);
}
/* Construct the lower constraint of the hexagonal tile, i.e.,
*
* a <= 2h+1
* -a + 2h+1 >= 0
*
* This constraint corresponds to (7) in
* "Hybrid Hexagonal/Classical Tiling for GPUs".
*/
static __isl_give isl_constraint *hex_lower(__isl_take isl_local_space *ls,
__isl_keep isl_val *h)
{
isl_val *v;
isl_aff *aff;
v = isl_val_add_ui(isl_val_mul_ui(isl_val_copy(h), 2), 1);
aff = isl_aff_val_on_domain(ls, v);
aff = isl_aff_set_coefficient_si(aff, isl_dim_in, 0, -1);
return isl_inequality_from_aff(aff);
}
/* Construct the lower right constraint of the hexagonal tile, i.e.,
*
* dl a + b <= (2h+1) dl + duh + (s0-1)
* -dl a - b + (2h+1) dl + duh + (s0-1) >= 0
*
* where duh = floor(du * h).
*
* This constraint corresponds to (8) in
* "Hybrid Hexagonal/Classical Tiling for GPUs".
*/
static __isl_give isl_constraint *hex_lower_right(
__isl_take isl_local_space *ls, __isl_keep isl_val *h,
__isl_keep isl_val *s0, __isl_keep isl_val *dl, __isl_keep isl_val *duh)
{
isl_val *v;
isl_aff *aff;
v = isl_val_add_ui(isl_val_mul_ui(isl_val_copy(h), 2), 1);
v = isl_val_mul(v, isl_val_copy(dl));
v = isl_val_add(v, isl_val_copy(duh));
v = isl_val_add(v, isl_val_copy(s0));
v = isl_val_sub_ui(v, 1);
aff = isl_aff_val_on_domain(ls, v);
v = isl_val_neg(isl_val_copy(dl));
aff = isl_aff_set_coefficient_val(aff, isl_dim_in, 0, v);
aff = isl_aff_set_coefficient_si(aff, isl_dim_in, 1, -1);
return isl_inequality_from_aff(aff);
}
/* Construct the upper left constraint of the hexagonal tile, i.e.,
*
* dl a + b >= h dl - (d - 1)/d with d = den(dl)
* dl a + b - h dl + (d - 1)/d >= 0
*
* This constraint corresponds to (10) in
* "Hybrid Hexagonal/Classical Tiling for GPUs".
*/
static __isl_give isl_constraint *hex_upper_left(__isl_take isl_local_space *ls,
__isl_keep isl_val *h, __isl_keep isl_val *dl)
{
isl_val *v, *d;
isl_aff *aff;
d = isl_val_get_den_val(dl);
v = isl_val_sub_ui(isl_val_copy(d), 1);
v = isl_val_div(v, d);
v = isl_val_sub(v, isl_val_mul(isl_val_copy(h), isl_val_copy(dl)));
aff = isl_aff_val_on_domain(ls, v);
aff = isl_aff_set_coefficient_val(aff, isl_dim_in, 0, isl_val_copy(dl));
aff = isl_aff_set_coefficient_si(aff, isl_dim_in, 1, 1);
return isl_inequality_from_aff(aff);
}
/* Construct the upper right constraint of the hexagonal tile, i.e.,
*
* du a - b >= du h - duh - (s0-1) - dlh - (d - 1)/d with d = den(du)
* du a - b - du h + duh + (s0-1) + dlh + (d - 1)/d >= 0
*
* where dlh = floor(dl * h) and duh = floor(du * h).
*
* This constraint corresponds to (12) in
* "Hybrid Hexagonal/Classical Tiling for GPUs".
*/
static __isl_give isl_constraint *hex_upper_right(
__isl_take isl_local_space *ls, __isl_keep isl_val *h,
__isl_keep isl_val *s0, __isl_keep isl_val *du,
__isl_keep isl_val *dlh, __isl_keep isl_val *duh)
{
isl_val *v, *d;
isl_aff *aff;
d = isl_val_get_den_val(du);
v = isl_val_sub_ui(isl_val_copy(d), 1);
v = isl_val_div(v, d);
v = isl_val_sub(v, isl_val_mul(isl_val_copy(h), isl_val_copy(du)));
v = isl_val_add(v, isl_val_copy(duh));
v = isl_val_add(v, isl_val_copy(dlh));
v = isl_val_add(v, isl_val_copy(s0));
v = isl_val_sub_ui(v, 1);
aff = isl_aff_val_on_domain(ls, v);
aff = isl_aff_set_coefficient_val(aff, isl_dim_in, 0, isl_val_copy(du));
aff = isl_aff_set_coefficient_si(aff, isl_dim_in, 1, -1);
return isl_inequality_from_aff(aff);
}
/* Construct the uppper constraint of the hexagonal tile, i.e.,
*
* a >= 0
*
* This constraint corresponds to (13) in
* "Hybrid Hexagonal/Classical Tiling for GPUs".
*/
static __isl_give isl_constraint *hex_upper(__isl_take isl_local_space *ls)
{
isl_aff *aff;
aff = isl_aff_var_on_domain(ls, isl_dim_set, 0);
return isl_inequality_from_aff(aff);
}
/* Construct the basic hexagonal tile shape.
* "space" is the 2D space in which the hexagon should be constructed.
* h is st-1, with st the tile size in the time dimension
* s0 is the tile size in the space dimension
* dl is a bound on the negative relative dependence distances, i.e.,
*
* d_s >= -dl d_t
*
* du is a bound on the positive relative dependence distances, i.e.,
*
* d_s <= du d_t
*
* with (d_t,d_s) any dependence distance vector.
* dlh = floor(dl * h)
* duh = floor(du * h)
*
* The shape of the hexagon is as follows:
*
* 0 dlh dlh+s0-1
* ______ __
* 0 / \_ /
* / \_ /
* h / \ ______ /
* h+1 \_ // \\_
* \_ // \\_
* 2h+1 \______// \\
* 0 duh duh+s0-1
* duh+s0-1+dlh
* duh+s0-1+dlh+1+s0+1
*
* The next hexagon is shifted by duh + dlh + 2 * s0.
*
* The slope of the "/" constraints is dl.
* The slope of the "\_" constraints is du.
*/
static __isl_give isl_set *compute_hexagon(__isl_take isl_space *space,
__isl_keep isl_val *h, __isl_keep isl_val *s0,
__isl_keep isl_val *dl, __isl_keep isl_val *du,
__isl_keep isl_val *dlh, __isl_keep isl_val *duh)
{
isl_local_space *ls;
isl_constraint *c;
isl_basic_set *bset;
ls = isl_local_space_from_space(space);
c = hex_lower_left(isl_local_space_copy(ls), h, du, duh);
bset = isl_basic_set_from_constraint(c);
c = hex_lower(isl_local_space_copy(ls), h);
bset = isl_basic_set_add_constraint(bset, c);
c = hex_lower_right(isl_local_space_copy(ls), h, s0, dl, duh);
bset = isl_basic_set_add_constraint(bset, c);
c = hex_upper_left(isl_local_space_copy(ls), h, dl);
bset = isl_basic_set_add_constraint(bset, c);
c = hex_upper_right(isl_local_space_copy(ls), h, s0, du, dlh, duh);
bset = isl_basic_set_add_constraint(bset, c);
c = hex_upper(ls);
bset = isl_basic_set_add_constraint(bset, c);
return isl_set_from_basic_set(bset);
}
/* Name of the ts-space.
*/
static const char *ts_space_name = "ts";
/* Construct and return the space ts[t, s].
*/
static __isl_give isl_space *construct_ts_space(isl_ctx *ctx)
{
isl_space *s;
s = isl_space_set_alloc(ctx, 0, 2);
s = isl_space_set_tuple_name(s, isl_dim_set, ts_space_name);
return s;
}
/* Name of the local ts-space.
*/
static const char *local_ts_space_name = "local_ts";
/* Construct and return the space local_ts[t, s].
*/
static __isl_give isl_space *construct_local_ts_space(isl_ctx *ctx)
{
isl_space *s;
s = isl_space_set_alloc(ctx, 0, 2);
s = isl_space_set_tuple_name(s, isl_dim_set, local_ts_space_name);
return s;
}
/* Compute the total size of a tile for the space dimensions,
* i.e., those corresponding to the child node
* of the input pattern.
* If S_0 is the original tile size in the first space dimension,
* then the first entry of "space_sizes" is equal to
* W = 2*S_0 + floor(d_l h) + floor(d_u h).
* The remaining entries are the same as in the original tile sizes.
* "tile_sizes" contains the original tile sizes, including
* the tile size corresponding to the parent node.
* "dlh" is equal to floor(d_l h).
* "duh" is equal to floor(d_u h).
*/
static __isl_give isl_multi_val *compute_space_sizes(
__isl_keep isl_multi_val *tile_sizes,
__isl_keep isl_val *dlh, __isl_keep isl_val *duh)
{
isl_val *size;
isl_multi_val *space_sizes;
space_sizes = isl_multi_val_copy(tile_sizes);
space_sizes = isl_multi_val_factor_range(space_sizes);
size = isl_multi_val_get_val(space_sizes, 0);
size = isl_val_mul_ui(size, 2);
size = isl_val_add(size, isl_val_copy(duh));
size = isl_val_add(size, isl_val_copy(dlh));
space_sizes = isl_multi_val_set_val(space_sizes, 0, size);
return space_sizes;
}
/* Compute the offset of phase 1 with respect to phase 0
* in the ts-space ("space").
* In particular, return
*
* ts[st, s0 + duh]
*/
static __isl_give isl_multi_val *compute_phase_shift(
__isl_keep isl_space *space, __isl_keep isl_val *st,
__isl_keep isl_val *s0, __isl_keep isl_val *duh)
{
isl_val *v;
isl_multi_val *phase_shift;
phase_shift = isl_multi_val_zero(isl_space_copy(space));
phase_shift = isl_multi_val_set_val(phase_shift, 0, isl_val_copy(st));
v = isl_val_add(isl_val_copy(duh), isl_val_copy(s0));
phase_shift = isl_multi_val_set_val(phase_shift, 1, v);
return phase_shift;
}
/* Return the function
*
* ts[t, s] -> floor(t/(2 * st))
*
* representing the time tile.
* "space" is the space ts[t, s].
*/
static __isl_give isl_aff *compute_time_tile(__isl_keep isl_space *space,
__isl_keep isl_val *st)
{
isl_val *v;
isl_aff *t;
isl_local_space *ls;
ls = isl_local_space_from_space(isl_space_copy(space));
t = isl_aff_var_on_domain(ls, isl_dim_set, 0);
v = isl_val_mul_ui(isl_val_copy(st), 2);
t = isl_aff_floor(isl_aff_scale_down_val(t, v));
return t;
}
/* Compute a shift in the space dimension for tiles
* at time tile T = floor(t/(2 * S_t))
* such that they align to a multiple of the total space tile dimension W.
* In particular, compute
*
* ts[t, s] -> s + (-(2 * shift_s)*T) % W
*
* where shift_s is the shift of phase 1 with respect to phase 0
* in the space dimension (the first element of "phase_shift").
* W is stored in the first element of "space_sizes".
* "time_tile" is the function
*
* ts[t, s] -> floor(t/(2 * S_T))
*
* Since phase 1 is shifted by shift_s with respect to phase 0,
* the next line of phase 0 (at T+1) is shifted by 2*shift_s
* with respect to the previous line (at T).
* A shift of -(2 * shift_s)*T therefore allows the basic pattern
* (which starts at 0) to be applied.
* However, this shift will be used to obtain the tile coordinate
* in the first space dimension and if the original values
* in the space dimension are non-negative, then the shift should
* not make them negative. Moreover, the shift should be as minimal
* as possible.
* Since the pattern repeats itself with a period of W in the space
* dimension, the shift can be replaced by (-(2 * shift_s)*T) % W.
*/
static __isl_give isl_aff *compute_shift_space(__isl_keep isl_aff *time_tile,
__isl_keep isl_multi_val *space_sizes,
__isl_keep isl_multi_val *phase_shift)
{
isl_val *v;
isl_aff *s, *t;
isl_local_space *ls;
ls = isl_local_space_from_space(isl_aff_get_domain_space(time_tile));
t = isl_aff_copy(time_tile);
v = isl_val_mul_ui(isl_multi_val_get_val(phase_shift, 1), 2);
v = isl_val_neg(v);
t = isl_aff_scale_val(t, v);
v = isl_multi_val_get_val(space_sizes, 0);
t = isl_aff_mod_val(t, v);
s = isl_aff_var_on_domain(ls, isl_dim_set, 1);
s = isl_aff_add(s, t);
return s;
}
/* Give the phase_shift ts[S_t, S_0 + floor(d_u h)],
* compute a function that applies the shift, i.e.,
*
* ts[t, s] -> ts[t + S_t, s + S_0 + floor(d_u h)],
*/
static __isl_give isl_multi_aff *compute_shift_phase(
__isl_keep isl_multi_val *phase_shift)
{
isl_space *space;
isl_multi_aff *shift;
space = isl_multi_val_get_space(phase_shift);
shift = isl_multi_aff_multi_val_on_space(space,
isl_multi_val_copy(phase_shift));
space = isl_multi_aff_get_space(shift);
shift = isl_multi_aff_add(shift, isl_multi_aff_identity(space));
return shift;
}
/* Compute a mapping from the ts-space to the local coordinates
* within each tile. In particular, compute
*
* ts[t, s] -> local_ts[t % (2 S_t), (s + (-(2 * shift_s)*T) % W) % W]
*
* "ts" is the space ts[t, s]
* "local_ts" is the space local_ts[t, s]
* "shift_space" is equal to ts[t, s] -> s + (-(2 * shift_s)*T) % W
* "st" is the tile size in the time dimension S_t.
* The first element of "space_sizes" is equal to W.
*/
static __isl_give isl_multi_aff *compute_localize(
__isl_keep isl_space *local_ts, __isl_keep isl_aff *shift_space,
__isl_keep isl_val *st, __isl_keep isl_multi_val *space_sizes)
{
isl_val *v;
isl_space *space;
isl_aff *s, *t;
isl_multi_aff *localize;
space = isl_aff_get_domain_space(shift_space);
local_ts = isl_space_copy(local_ts);
space = isl_space_map_from_domain_and_range(space, local_ts);
localize = isl_multi_aff_identity(space);
t = isl_multi_aff_get_aff(localize, 0);
v = isl_val_mul_ui(isl_val_copy(st), 2);
t = isl_aff_mod_val(t, v);
localize = isl_multi_aff_set_aff(localize, 0, t);
s = isl_aff_copy(shift_space);
v = isl_multi_val_get_val(space_sizes, 0);
s = isl_aff_mod_val(s, v);
localize = isl_multi_aff_set_aff(localize, 1, s);
return localize;
}
/* Set the project_ts field of "tiling".
*
* This field projects the space of the input schedule to the ts-space.
* It is equal to [P[t] -> C[s_0, ...]] -> ts[t, s_0].
*/
static __isl_give ppcg_ht_tiling *ppcg_ht_tiling_set_project_ts(
__isl_take ppcg_ht_tiling *tiling)
{
int n;
isl_space *space;
isl_multi_aff *project;
if (!tiling)
return NULL;
space = ppcg_ht_tiling_get_input_space(tiling);
n = isl_space_dim(space, isl_dim_set);
project = isl_multi_aff_project_out_map(space, isl_dim_set, 2, n - 2);
project = isl_multi_aff_set_tuple_name(project,
isl_dim_out, ts_space_name);
if (!project)
return ppcg_ht_tiling_free(tiling);
tiling->project_ts = project;
return tiling;
}
/* Construct a hybrid tiling description from bounds on the dependence
* distances "bounds".
* "input_node" points to the original parent node.
* "input_schedule" is the combined schedule of the parent and child
* node in the input.
* "tile_sizes" are the original, user specified tile sizes.
*/
static __isl_give ppcg_ht_tiling *ppcg_ht_bounds_construct_tiling(
__isl_take ppcg_ht_bounds *bounds,
__isl_keep isl_schedule_node *input_node,
__isl_keep isl_multi_union_pw_aff *input_schedule,
__isl_keep isl_multi_val *tile_sizes)
{
isl_ctx *ctx;
ppcg_ht_tiling *tiling;
isl_multi_val *space_sizes, *phase_shift;
isl_aff *time_tile, *shift_space;
isl_multi_aff *localize;
isl_val *h, *duh, *dlh;
isl_val *st, *s0, *du, *dl;
isl_space *ts, *local_ts;
if (!bounds || !input_node || !input_schedule || !tile_sizes)
goto error;
ctx = isl_multi_union_pw_aff_get_ctx(input_schedule);
tiling = isl_calloc_type(ctx, struct ppcg_ht_tiling);
if (!tiling)
goto error;
tiling->ref = 1;
st = isl_multi_val_get_val(tile_sizes, 0);
h = isl_val_sub_ui(isl_val_copy(st), 1);
s0 = isl_multi_val_get_val(tile_sizes, 1);
du = ppcg_ht_bounds_get_upper(bounds);
dl = ppcg_ht_bounds_get_lower(bounds, 0);
duh = isl_val_floor(isl_val_mul(isl_val_copy(du), isl_val_copy(h)));
dlh = isl_val_floor(isl_val_mul(isl_val_copy(dl), isl_val_copy(h)));
ts = construct_ts_space(ctx);
local_ts = construct_local_ts_space(ctx);
space_sizes = compute_space_sizes(tile_sizes, dlh, duh);
phase_shift = compute_phase_shift(ts, st, s0, duh);
time_tile = compute_time_tile(ts, st);
shift_space = compute_shift_space(time_tile, space_sizes, phase_shift);
localize = compute_localize(local_ts, shift_space, st, space_sizes);
isl_space_free(ts);
tiling->input_node = isl_schedule_node_copy(input_node);
tiling->input_schedule = isl_multi_union_pw_aff_copy(input_schedule);
tiling->space_sizes = space_sizes;
tiling->bounds = bounds;
tiling->local_time = isl_multi_aff_get_aff(localize, 0);
tiling->hex = compute_hexagon(local_ts, h, s0, dl, du, dlh, duh);
tiling->hex = isl_set_preimage_multi_aff(tiling->hex, localize);
tiling->time_tile = time_tile;
tiling->shift_space = shift_space;
tiling->shift_phase = compute_shift_phase(phase_shift);
isl_multi_val_free(phase_shift);
isl_val_free(duh);
isl_val_free(dlh);
isl_val_free(du);
isl_val_free(dl);
isl_val_free(s0);
isl_val_free(st);
isl_val_free(h);
if (!tiling->input_schedule || !tiling->local_time || !tiling->hex ||
!tiling->shift_space || !tiling->shift_phase)
return ppcg_ht_tiling_free(tiling);
tiling = ppcg_ht_tiling_set_project_ts(tiling);
return tiling;
error:
ppcg_ht_bounds_free(bounds);
return NULL;
}
/* Are all members of the band node "node" coincident?
*/
static isl_bool all_coincident(__isl_keep isl_schedule_node *node)
{
int i, n;
n = isl_schedule_node_band_n_member(node);
for (i = 0; i < n; ++i) {
isl_bool c;
c = isl_schedule_node_band_member_get_coincident(node, i);
if (c < 0 || !c)
return c;
}
return isl_bool_true;
}
/* Does "node" satisfy the properties of the inner node in the input
* pattern for hybrid tiling?
* That is, is it a band node with only coincident members, of which
* there is at least one?
*/
static isl_bool has_child_properties(__isl_keep isl_schedule_node *node)
{
if (!node)
return isl_bool_error;
if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
return isl_bool_false;
if (isl_schedule_node_band_n_member(node) < 1)
return isl_bool_false;
return all_coincident(node);
}
/* Does "node" satisfy the properties of the outer node in the input
* pattern for hybrid tiling?
* That is, is it a band node with a single member?
*/
static isl_bool has_parent_properties(__isl_keep isl_schedule_node *node)
{
if (!node)
return isl_bool_error;
if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
return isl_bool_false;
if (isl_schedule_node_band_n_member(node) != 1)
return isl_bool_false;
return isl_bool_true;
}
/* Does the parent of "node" satisfy the input patttern for hybrid tiling?