-
Notifications
You must be signed in to change notification settings - Fork 37
/
gpu_group.c
1670 lines (1466 loc) · 50.5 KB
/
gpu_group.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 2010-2011 INRIA Saclay
* Copyright 2012-2014 Ecole Normale Superieure
* Copyright 2015 Sven Verdoolaege
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, 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/aff.h>
#include <isl/map.h>
#include <isl/constraint.h>
#include "gpu_array_tile.h"
#include "gpu_group.h"
#include "gpu_tree.h"
#include "schedule.h"
/* Print the name of the local copy of a given group of array references.
*/
__isl_give isl_printer *gpu_array_ref_group_print_name(
struct gpu_array_ref_group *group, __isl_take isl_printer *p)
{
int global = 0;
enum ppcg_group_access_type type;
type = gpu_array_ref_group_type(group);
if (type == ppcg_access_private)
p = isl_printer_print_str(p, "private_");
else if (type == ppcg_access_shared)
p = isl_printer_print_str(p, "shared_");
else
global = 1;
p = isl_printer_print_str(p, group->array->name);
if (!global && group->local_array->n_group > 1) {
p = isl_printer_print_str(p, "_");
p = isl_printer_print_int(p, group->nr);
}
return p;
}
/* Return the union of all read (read = 1) and/or write (write = 1)
* access relations in the group.
*/
__isl_give isl_union_map *gpu_array_ref_group_access_relation(
struct gpu_array_ref_group *group, int read, int write)
{
int i;
isl_union_map *access;
access = isl_union_map_empty(isl_map_get_space(group->access));
for (i = 0; i < group->n_ref; ++i) {
isl_map *map_i;
if (!((read && group->refs[i]->read) ||
(write && group->refs[i]->write)))
continue;
map_i = isl_map_copy(group->refs[i]->access);
access = isl_union_map_union(access,
isl_union_map_from_map(map_i));
}
return access;
}
/* Should this array reference group be mapped to private, shared or global
* memory?
* If we have computed both a private and a shared tile, then
* the tile with the smallest depth is used. If both have the same depth,
* then the private tile is used.
*/
enum ppcg_group_access_type gpu_array_ref_group_type(
struct gpu_array_ref_group *group)
{
if (group->private_tile && group->shared_tile &&
group->shared_tile->depth < group->private_tile->depth)
return ppcg_access_shared;
if (group->private_tile)
return ppcg_access_private;
if (group->shared_tile)
return ppcg_access_shared;
return ppcg_access_global;
}
/* Return the effective gpu_array_tile associated to "group" or
* NULL if there is no such gpu_array_tile.
*/
struct gpu_array_tile *gpu_array_ref_group_tile(
struct gpu_array_ref_group *group)
{
switch (gpu_array_ref_group_type(group)) {
case ppcg_access_global:
return NULL;
case ppcg_access_shared:
return group->shared_tile;
case ppcg_access_private:
return group->private_tile;
}
}
/* Does the tile associated to "group" require unrolling of the schedule
* dimensions mapped to threads?
* Note that this can only happen for private tiles.
*/
int gpu_array_ref_group_requires_unroll(struct gpu_array_ref_group *group)
{
struct gpu_array_tile *tile;
tile = gpu_array_ref_group_tile(group);
if (!tile)
return 0;
return tile->requires_unroll;
}
/* Given an array access "access", check if for any index i there is
* a shift a(p) and a stride g such that
*
* a(p) + i = 0 mod g
*
* If so, record the information in tile->bound[i]->stride and
* tile->bound[i]->shift.
* Otherwise, set tile->bound[i]->stride to 1 (and tile->bound[i]->shift to 0).
* Return isl_bool_true if any non-trivial stride was found.
*
* Note that the stride info returned by isl_map_get_range_stride_info
* is of the form
*
* i = o(p) + g n
*
* a(p) can therefore be taken to be equal to -o(p).
*/
static isl_bool detect_strides(struct gpu_array_tile *tile,
__isl_keep isl_map *access)
{
int i;
isl_bool has_strides = isl_bool_false;
for (i = 0; i < tile->n; ++i) {
struct gpu_array_bound *bound = &tile->bound[i];
isl_stride_info *si;
si = isl_map_get_range_stride_info(access, i);
bound->stride = isl_stride_info_get_stride(si);
bound->shift = isl_aff_neg(isl_stride_info_get_offset(si));
isl_stride_info_free(si);
if (!has_strides)
has_strides = isl_val_gt_si(bound->stride, 1);
if (has_strides < 0)
return isl_bool_error;
}
return has_strides;
}
/* Given an array access "access", remove the strides based
* on the information in tile->bound[i]->stride and tile->bound[i]->shift.
*
* In particular let the access be A[a] and
* let the shifts s_i(p) and the strides g_i be such that
*
* S(p) + a = 0 mod G
*
* Replace the access by
*
* A[(a + S(p))/G]
*
* First collect the shifts s_i into an isl_multi_aff and
* the strides into the scaling function A[i] -> A[G i].
* Then add the shifts to the original access and
* take the preimage over the scaling.
*/
static __isl_give isl_map *remove_strides(__isl_take isl_map *access,
struct gpu_array_tile *tile)
{
int i;
isl_space *space;
isl_multi_aff *shift, *scale;
isl_multi_val *stride;
space = isl_map_get_space(access);
shift = isl_multi_aff_zero(isl_space_copy(space));
space = isl_space_range(space);
stride = isl_multi_val_zero(isl_space_copy(space));
scale = isl_multi_aff_identity(isl_space_map_from_set(space));
for (i = 0; i < tile->n; ++i) {
struct gpu_array_bound *bound = &tile->bound[i];
isl_aff *shift_i;
isl_val *stride_i;
shift_i = isl_aff_copy(bound->shift);
stride_i = isl_val_copy(bound->stride);
shift = isl_multi_aff_set_aff(shift, i, shift_i);
stride = isl_multi_val_set_val(stride, i, stride_i);
}
scale = isl_multi_aff_scale_multi_val(scale, stride);
access = isl_map_sum(access, isl_map_from_multi_aff(shift));
access = isl_map_preimage_range_multi_aff(access, scale);
return access;
}
/* Check if we can find a memory tile for the given array
* based on the given accesses, and if so, put the results in "tile".
*
* We project the accesses on each index in turn and look for a parametric
* offset such that the size is constant, after removing
* any stride that may appear in the accesses.
*
* tile->depth is initialized to the input dimension of the computed bounds.
*/
static isl_bool can_tile(__isl_keep isl_map *access,
struct gpu_array_tile *tile)
{
int i;
isl_bool has_strides, valid;
isl_fixed_box *box;
isl_multi_aff *offset;
isl_multi_val *size;
if (!tile)
return isl_bool_error;
isl_map_free(isl_map_detect_equalities(isl_map_copy(access)));
has_strides = detect_strides(tile, access);
if (has_strides < 0)
return isl_bool_error;
tile->depth = isl_map_dim(access, isl_dim_in);
access = isl_map_copy(access);
if (has_strides)
access = remove_strides(access, tile);
box = isl_map_get_range_simple_fixed_box_hull(access);
isl_map_free(access);
valid = isl_fixed_box_is_valid(box);
if (valid >= 0 && valid) {
offset = isl_fixed_box_get_offset(box);
size = isl_fixed_box_get_size(box);
for (i = 0; i < tile->n; ++i) {
tile->bound[i].size = isl_multi_val_get_val(size, i);
tile->bound[i].lb = isl_multi_aff_get_aff(offset, i);
}
isl_multi_aff_free(offset);
isl_multi_val_free(size);
}
isl_fixed_box_free(box);
return valid;
}
/* Internal data structure for gpu_group_references.
*
* scop represents the input scop.
* kernel_depth is the schedule depth where the kernel launch will
* be introduced, i.e., it is the depth of the band that is mapped
* to blocks.
* shared_depth is the schedule depth at which the copying to/from
* shared memory is computed. The copy operation may then
* later be hoisted to a higher level.
* thread_depth is the schedule depth where the thread mark is located,
* i.e., it is the depth of the band that is mapped to threads and also
* the schedule depth at which the copying to/from private memory
* is computed. The copy operation may then later be hoisted to
* a higher level.
* n_thread is the number of schedule dimensions in the band that
* is mapped to threads.
* privatization lives in the range of thread_sched (i.e., it is
* of dimension thread_depth + n_thread) and encodes the mapping
* to thread identifiers (as parameters).
* host_sched contains the kernel_depth dimensions of the host schedule.
* shared_sched contains the first shared_depth dimensions of the
* kernel schedule.
* copy_sched contains the first thread_depth dimensions of the
* kernel schedule.
* thread_sched contains the first (thread_depth + n_thread) dimensions
* of the kernel schedule.
* full_sched is a union_map representation of the entire kernel schedule.
* The schedules are all formulated in terms of the original statement
* instances, i.e., those that appear in the domains of the access
* relations.
*/
struct gpu_group_data {
struct ppcg_scop *scop;
int kernel_depth;
int shared_depth;
int thread_depth;
int n_thread;
isl_set *privatization;
isl_union_map *host_sched;
isl_union_map *shared_sched;
isl_union_map *copy_sched;
isl_union_map *thread_sched;
isl_union_map *full_sched;
};
/* Construct a map from domain_space to domain_space that increments
* the dimension at position "pos" and leaves all other dimensions
* constant.
*/
static __isl_give isl_map *next(__isl_take isl_space *domain_space, int pos)
{
isl_space *space;
isl_aff *aff;
isl_multi_aff *next;
space = isl_space_map_from_set(domain_space);
next = isl_multi_aff_identity(space);
aff = isl_multi_aff_get_aff(next, pos);
aff = isl_aff_add_constant_si(aff, 1);
next = isl_multi_aff_set_aff(next, pos, aff);
return isl_map_from_multi_aff(next);
}
/* Check if the given access is coalesced (or if there is no point
* in trying to coalesce the access by mapping the array to shared memory).
* That is, check whether incrementing the dimension that will get
* wrapped over the last thread index results in incrementing
* the last array index.
*
* If no two consecutive array elements are ever accessed by "access",
* then mapping the corresponding array to shared memory will not
* improve coalescing. In fact, the copying will likely be performed
* by a single thread. Consider the access as coalesced such that
* the caller will not try and map the array to shared memory just
* to improve coalescing.
*
* This function is only called for access relations without reuse and
* kernels with at least one thread identifier.
*/
static int access_is_coalesced(struct gpu_group_data *data,
__isl_keep isl_union_map *access)
{
int dim;
isl_space *space;
isl_set *accessed;
isl_map *access_map;
isl_map *next_thread_x;
isl_map *next_element;
isl_map *map;
int coalesced, empty;
access = isl_union_map_copy(access);
access = isl_union_map_apply_domain(access,
isl_union_map_copy(data->full_sched));
access_map = isl_map_from_union_map(access);
space = isl_map_get_space(access_map);
space = isl_space_range(space);
dim = isl_space_dim(space, isl_dim_set);
if (dim == 0)
next_element = isl_map_empty(isl_space_map_from_set(space));
else
next_element = next(space, dim - 1);
accessed = isl_map_range(isl_map_copy(access_map));
map = isl_map_copy(next_element);
map = isl_map_intersect_domain(map, isl_set_copy(accessed));
map = isl_map_intersect_range(map, accessed);
empty = isl_map_is_empty(map);
isl_map_free(map);
if (empty < 0 || empty) {
isl_map_free(next_element);
isl_map_free(access_map);
return empty;
}
space = isl_map_get_space(access_map);
space = isl_space_domain(space);
next_thread_x = next(space, data->thread_depth + data->n_thread - 1);
map = isl_map_apply_domain(next_thread_x, isl_map_copy(access_map));
map = isl_map_apply_range(map, access_map);
coalesced = isl_map_is_subset(map, next_element);
isl_map_free(next_element);
isl_map_free(map);
return coalesced;
}
/* Replace the host schedule dimensions in the access relation "access"
* by parameters, so that they are treated as fixed when checking for reuse
* (within a kernel) or whether two consecutive elements are accessed
* (within a kernel).
*/
static __isl_give isl_union_map *localize_access(struct gpu_group_data *data,
__isl_take isl_union_map *access)
{
int n;
isl_space *space;
isl_set *param;
isl_union_map *umap;
isl_id_list *ids;
umap = isl_union_map_copy(data->host_sched);
space = isl_union_map_get_space(umap);
n = data->kernel_depth;
ids = ppcg_scop_generate_names(data->scop, n, "__ppcg_host_");
param = parametrization(space, n, 0, ids);
isl_id_list_free(ids);
umap = isl_union_map_intersect_range(umap,
isl_union_set_from_set(param));
access = isl_union_map_intersect_domain(access,
isl_union_map_domain(umap));
return access;
}
/* Given an access relation in terms of at least data->thread_depth initial
* dimensions of the computed schedule, check if it is bijective for
* fixed values of the first data->thread_depth dimensions.
* We perform this check by equating these dimensions to parameters.
*/
static int access_is_bijective(struct gpu_group_data *data,
__isl_keep isl_map *access)
{
int res;
int dim;
isl_set *par;
isl_space *space;
isl_id_list *ids;
access = isl_map_copy(access);
space = isl_space_params(isl_map_get_space(access));
ids = ppcg_scop_generate_names(data->scop, data->thread_depth, "s");
dim = isl_map_dim(access, isl_dim_in);
par = parametrization(space, dim, 0, ids);
isl_id_list_free(ids);
access = isl_map_intersect_domain(access, par);
res = isl_map_is_bijective(access);
isl_map_free(access);
return res;
}
/* Compute the number of outer schedule tile dimensions that affect
* the offset of "tile".
* If there is no such dimension, then return the index
* of the first kernel dimension, i.e., data->kernel_depth.
*/
static int compute_tile_depth(struct gpu_group_data *data,
struct gpu_array_tile *tile)
{
int i, j;
for (j = tile->depth - 1; j >= data->kernel_depth; --j) {
for (i = 0; i < tile->n; ++i) {
isl_aff *lb;
isl_aff *shift;
lb = tile->bound[i].lb;
if (isl_aff_involves_dims(lb, isl_dim_in, j, 1))
break;
shift = tile->bound[i].shift;
if (!shift)
continue;
if (isl_aff_involves_dims(shift, isl_dim_in, j, 1))
break;
}
if (i < tile->n)
break;
}
return ++j;
}
/* Return the lowest depth between data->kernel_depth and data->thread_depth
* at which every array element accessed through "acc" is accessed
* by a single thread. The input dimension of "acc" is
* data->thread_depth + data->n_thread, where the final data->n_thread
* dimensions are those that will be mapped to threads.
* If the values for these dimensions are uniquely determined
* by the array index and a given number of outer dimensions, then
* there is only one thread accessing that array element within those
* outer dimensions.
*
* The input space of "acc" is first split up, such that it has the form
*
* [O -> T] -> A
*
* with O the outer dimensions, T the dimensions that will be mapped to threads
* and A the array index.
*
* Then the positions of T and A are interchanged to simplify the test
* whether T uniquely depends on O and A.
* In particular, the above access relation is first combined with
*
* [O -> T] -> T
*
* to form
*
* [O -> T] -> [A -> T]
*
* from which
*
* O -> [A -> T]
*
* is extracted, which is then uncurried to
*
* [O -> A] -> T
*
* Finally, the final dimensions of O are projected out one by one
* until T is no longer uniquely determined by A and the remaining
* dimensions in O. The value returned is that of the last dimension
* that was successfully projected out.
* Note that there is no need to test whether [O -> A] -> T itself
* is single-valued as that was already tested in access_is_bijective.
*/
static int compute_accessed_by_single_thread_depth(struct gpu_group_data *data,
__isl_keep isl_map *acc)
{
int i;
isl_space *space;
isl_map *map;
isl_bool sv;
if (data->thread_depth == data->kernel_depth)
return data->thread_depth;
acc = isl_map_copy(acc);
space = isl_map_get_space(acc);
space = isl_space_params(space);
space = isl_space_set_from_params(space);
space = isl_space_add_dims(space, isl_dim_set, data->thread_depth);
space = isl_space_from_domain(space);
space = isl_space_add_dims(space, isl_dim_out, data->n_thread);
space = isl_space_wrap(space);
map = isl_set_flatten_map(isl_set_universe(space));
acc = isl_map_apply_range(map, acc);
space = isl_space_domain(isl_map_get_space(acc));
map = isl_map_range_map(isl_map_universe(isl_space_unwrap(space)));
acc = isl_map_range_product(acc, map);
acc = isl_map_domain_factor_domain(acc);
acc = isl_map_uncurry(acc);
for (i = data->thread_depth - 1; i >= data->kernel_depth; --i) {
acc = isl_map_project_out(acc, isl_dim_in, i, 1);
sv = isl_map_is_single_valued(acc);
if (sv < 0)
goto error;
if (!sv)
break;
}
isl_map_free(acc);
return ++i;
error:
isl_map_free(acc);
return -1;
}
/* Adjust the fields of "tile" to reflect the new input dimension "depth".
* The dimension beyond "depth" are assumed not to affect the tile,
* so they can simply be dropped.
*/
static int tile_adjust_depth(struct gpu_array_tile *tile, int depth)
{
int i;
if (tile->depth == depth)
return 0;
for (i = 0; i < tile->n; ++i) {
tile->bound[i].lb = isl_aff_drop_dims(tile->bound[i].lb,
isl_dim_in, depth, tile->depth - depth);
if (!tile->bound[i].lb)
return -1;
if (!tile->bound[i].shift)
continue;
tile->bound[i].shift = isl_aff_drop_dims(tile->bound[i].shift,
isl_dim_in, depth, tile->depth - depth);
if (!tile->bound[i].shift)
return -1;
}
tile->depth = depth;
return 0;
}
/* Determine the number of schedule dimensions that affect the offset of the
* shared or private tile "tile" and store the result in tile->depth, with
* a lower bound of data->kernel_depth.
* Also adjust the fields of the tile to only refer to the tile->depth
* outer schedule dimensions.
*/
static isl_stat tile_set_depth(struct gpu_group_data *data,
struct gpu_array_tile *tile)
{
if (tile_adjust_depth(tile, compute_tile_depth(data, tile)) < 0)
return isl_stat_error;
return isl_stat_ok;
}
/* Determine the number of schedule dimensions that affect the offset of the
* shared tile and store the minimum of the private and shared tile depth
* in group->min_depth, with a lower bound of data->kernel_depth.
* If there is no tile defined on the array reference group,
* then set group->min_depth to data->thread_depth.
*/
static int set_depth(struct gpu_group_data *data,
struct gpu_array_ref_group *group)
{
group->min_depth = data->thread_depth;
if (group->private_tile) {
if (group->private_tile->depth < group->min_depth)
group->min_depth = group->private_tile->depth;
}
if (group->shared_tile) {
if (tile_set_depth(data, group->shared_tile) < 0)
return -1;
if (group->shared_tile->depth < group->min_depth)
group->min_depth = group->shared_tile->depth;
}
return 0;
}
/* Fill up the groups array with singleton groups, i.e., one group
* per reference, initializing the array, access, write, n_ref and refs fields.
* In particular the access field is initialized to the scheduled
* access relation of the array reference.
*
* Return the number of elements initialized, i.e., the number of
* active references in the current kernel.
*/
static int populate_array_references(struct gpu_local_array_info *local,
struct gpu_array_ref_group **groups, struct gpu_group_data *data)
{
int i;
int n;
isl_ctx *ctx = isl_union_map_get_ctx(data->copy_sched);
n = 0;
for (i = 0; i < local->array->n_ref; ++i) {
isl_union_map *umap;
isl_map *map;
struct gpu_array_ref_group *group;
struct gpu_stmt_access *access = local->array->refs[i];
map = isl_map_copy(access->access);
umap = isl_union_map_from_map(map);
umap = isl_union_map_apply_domain(umap,
isl_union_map_copy(data->copy_sched));
if (isl_union_map_is_empty(umap)) {
isl_union_map_free(umap);
continue;
}
map = isl_map_from_union_map(umap);
map = isl_map_detect_equalities(map);
group = isl_calloc_type(ctx, struct gpu_array_ref_group);
if (!group) {
isl_map_free(map);
return -1;
}
group->local_array = local;
group->array = local->array;
group->access = map;
group->write = access->write;
group->exact_write = access->exact_write;
group->slice = access->n_index < local->array->n_index;
group->refs = &local->array->refs[i];
group->n_ref = 1;
groups[n++] = group;
}
return n;
}
/* If group->n_ref == 1, then group->refs was set by
* populate_array_references to point directly into
* group->array->refs and should not be freed.
* If group->n_ref > 1, then group->refs was set by join_groups
* to point to a newly allocated array.
*/
struct gpu_array_ref_group *gpu_array_ref_group_free(
struct gpu_array_ref_group *group)
{
if (!group)
return NULL;
gpu_array_tile_free(group->shared_tile);
gpu_array_tile_free(group->private_tile);
isl_map_free(group->access);
if (group->n_ref > 1)
free(group->refs);
free(group);
return NULL;
}
/* Check if the access relations of group1 and group2 overlap within
* copy_sched.
*/
static int accesses_overlap(struct gpu_array_ref_group *group1,
struct gpu_array_ref_group *group2)
{
int disjoint;
disjoint = isl_map_is_disjoint(group1->access, group2->access);
if (disjoint < 0)
return -1;
return !disjoint;
}
/* Combine the given two groups into a single group, containing
* the references of both groups.
*/
static struct gpu_array_ref_group *join_groups(
struct gpu_array_ref_group *group1,
struct gpu_array_ref_group *group2)
{
int i;
isl_ctx *ctx;
struct gpu_array_ref_group *group;
if (!group1 || !group2)
return NULL;
ctx = isl_map_get_ctx(group1->access);
group = isl_calloc_type(ctx, struct gpu_array_ref_group);
if (!group)
return NULL;
group->local_array = group1->local_array;
group->array = group1->array;
group->access = isl_map_union(isl_map_copy(group1->access),
isl_map_copy(group2->access));
group->write = group1->write || group2->write;
group->exact_write = group1->exact_write && group2->exact_write;
group->slice = group1->slice || group2->slice;
group->n_ref = group1->n_ref + group2->n_ref;
group->refs = isl_alloc_array(ctx, struct gpu_stmt_access *,
group->n_ref);
if (!group->refs)
return gpu_array_ref_group_free(group);
for (i = 0; i < group1->n_ref; ++i)
group->refs[i] = group1->refs[i];
for (i = 0; i < group2->n_ref; ++i)
group->refs[group1->n_ref + i] = group2->refs[i];
return group;
}
/* Combine the given two groups into a single group and free
* the original two groups.
*/
static struct gpu_array_ref_group *join_groups_and_free(
struct gpu_array_ref_group *group1,
struct gpu_array_ref_group *group2)
{
struct gpu_array_ref_group *group;
group = join_groups(group1, group2);
gpu_array_ref_group_free(group1);
gpu_array_ref_group_free(group2);
return group;
}
/* Report that the array reference group with the given access relation
* is not mapped to shared memory in the given kernel because
* it does not exhibit any reuse and is considered to be coalesced.
*/
static void report_no_reuse_and_coalesced(struct ppcg_kernel *kernel,
__isl_keep isl_union_map *access)
{
isl_ctx *ctx;
isl_printer *p;
ctx = isl_union_map_get_ctx(access);
p = isl_printer_to_file(ctx, stdout);
p = isl_printer_print_str(p, "Array reference group ");
p = isl_printer_print_union_map(p, access);
p = isl_printer_print_str(p,
" not considered for mapping to shared memory in kernel");
p = isl_printer_print_int(p, kernel->id);
p = isl_printer_print_str(p,
" because it exhibits no reuse and is considered to be coalesced");
p = isl_printer_end_line(p);
isl_printer_free(p);
}
/* Given an access relation in terms of the data->thread_depth initial
* dimensions of the computed schedule and the thread identifiers
* (as parameters), check if the use of the corresponding private tile
* requires unrolling.
*
* If we are creating a private tile because we are forced to,
* then no unrolling is required.
* Otherwise we check if "access" is bijective and unrolling
* is required if it is not. Note that the access relation
* has already been determined to be bijective before the introduction
* of the thread identifiers and the removal of the schedule dimensions
* that are mapped to these threads. If the access relation is no longer
* bijective, then this means that more than one value of one of those
* schedule dimensions is mapped to the same thread and therefore
* unrolling is required.
*/
static int check_requires_unroll(struct gpu_group_data *data,
__isl_keep isl_map *access, int force_private)
{
int bijective;
if (force_private)
return 0;
bijective = access_is_bijective(data, access);
if (bijective < 0)
return -1;
return !bijective;
}
/* Map the domain of "access" to the outer data->shared_depth
* schedule dimensions. When data->shared_depth is equal to
* data->thread_depth, this result is already available in group->access.
*/
static __isl_give isl_map *shared_access(struct gpu_array_ref_group *group,
__isl_keep isl_union_map *access, struct gpu_group_data *data)
{
isl_union_map *shared;
if (data->shared_depth == data->thread_depth)
return isl_map_copy(group->access);
shared = isl_union_map_copy(access);
shared = isl_union_map_apply_domain(shared,
isl_union_map_copy(data->shared_sched));
return isl_map_from_union_map(shared);
}
/* Compute the private and/or shared memory tiles for the array
* reference group "group" of array "array".
* Return isl_stat_ok on success and isl_stat_error on error.
*
* If the array is a read-only scalar or if the user requested
* not to use shared or private memory, then we do not need to do anything.
*
* If any reference in the reference group accesses more than one element,
* then we would have to make sure that the layout in shared memory
* is the same as that in global memory. Since we do not handle this yet
* (and it may not even be possible), we refuse to map to private or
* shared memory in such cases.
*
* If the array group involves any may writes (that are not must writes),
* then we would have to make sure that we load the data into shared/private
* memory first in case the data is not written by the kernel
* (but still written back out to global memory).
* Since we don't have any such mechanism at the moment, we don't
* compute shared/private tiles for groups involving may writes.
*
* We only try to compute a shared memory tile if there is any reuse
* or if the access is not coalesced.
* Reuse and coalescing are checked within the given kernel.
*
* For computing a private memory tile, we also require that there is
* some reuse. Moreover, we require that the access is private
* to the thread. That is, we check that any given array element
* is only accessed by a single thread.
* We compute an access relation that maps the outer
* data->thread_depth + data->n_thread schedule dimensions.
* The latter data->n_thread will be mapped to thread identifiers.
* We actually check that those iterators that will be wrapped
* partition the array space. This check is stricter than necessary
* since several iterations may be mapped onto the same thread
* and then they could be allowed to access the same memory elements,
* but our check does not allow this situation.
*
* For private memory tiles, the number of schedule dimensions that
* affect the offset is computed and stored in tile->depth, with
* a lower bound of data->kernel_depth. If this depth is smaller
* than the minimal depth that still ensures that every element
* is accessed by a single thread, then the depth is raised
* to this minimal depth.
* The fields of the tile are then adjusted to only refer to the tile->depth
* outer schedule dimensions.
*
* We also check that the index expression only depends on parallel
* loops. That way, we can move those loops innermost and unroll them.
* Again, we use a test that is stricter than necessary.
* We actually check whether the index expression only depends
* on the iterators that are wrapped over the threads.
* These are necessarily parallel, but there may be more parallel loops.
*
* Combining the injectivity of the first test with the single-valuedness
* of the second test, we simply test for bijectivity.
*
* If the use of the private tile requires unrolling, but some
* of the other arrays are forcibly mapped to private memory,
* then we do not allow the use of this private tile since
* we cannot move the schedule dimensions that need to be unrolled down
* without performing some kind of expansion on those arrays
* that are forcibly mapped to private memory.
*
* If the array is marked force_private, then we bypass all checks
* and assume we can (and should) use registers only.
*
* If it turns out we can (or have to) use registers, we compute
* the private memory tile size using can_tile, after introducing a dependence
* on the thread indices.
*/
static isl_stat compute_group_bounds_core(struct ppcg_kernel *kernel,
struct gpu_array_ref_group *group, struct gpu_group_data *data)
{
isl_ctx *ctx = isl_space_get_ctx(group->array->space);
isl_union_map *access, *local;
int n_index = group->array->n_index;
int no_reuse, coalesced;
isl_map *acc;
int force_private = group->local_array->force_private;
int use_shared = !force_private && kernel->options->use_shared_memory &&
data->n_thread > 0;
int use_private = force_private || kernel->options->use_private_memory;
isl_stat r = isl_stat_ok;
isl_bool ok;
int requires_unroll;
int unique_depth;
if (!use_shared && !use_private)
return isl_stat_ok;
if (gpu_array_is_read_only_scalar(group->array))
return isl_stat_ok;
if (!force_private && !group->exact_write)
return isl_stat_ok;
if (group->slice)
return isl_stat_ok;
access = gpu_array_ref_group_access_relation(group, 1, 1);
local = localize_access(data, isl_union_map_copy(access));
no_reuse = isl_union_map_is_injective(local);
if (no_reuse < 0)
r = isl_stat_error;
if (use_shared && no_reuse)
coalesced = access_is_coalesced(data, local);
isl_union_map_free(local);
if (r >= 0 && kernel->options->debug->verbose &&
use_shared && no_reuse && coalesced)
report_no_reuse_and_coalesced(kernel, access);
if (use_shared && (!no_reuse || !coalesced)) {
group->shared_tile = gpu_array_tile_create(ctx,
group->array->n_index);
acc = shared_access(group, access, data);
ok = can_tile(acc, group->shared_tile);
if (ok < 0)
r = isl_stat_error;
else if (!ok)
group->shared_tile =
gpu_array_tile_free(group->shared_tile);
isl_map_free(acc);
}
if (r < 0 || (!force_private && (!use_private || no_reuse))) {
isl_union_map_free(access);
return r;
}
access = isl_union_map_apply_domain(access,
isl_union_map_copy(data->thread_sched));
acc = isl_map_from_union_map(access);
if (!force_private && !access_is_bijective(data, acc)) {
isl_map_free(acc);
return isl_stat_ok;
}
unique_depth = compute_accessed_by_single_thread_depth(data, acc);
acc = isl_map_intersect_domain(acc, isl_set_copy(data->privatization));
acc = isl_map_project_out(acc, isl_dim_in, data->thread_depth,
data->n_thread);
requires_unroll = check_requires_unroll(data, acc, force_private);
if (unique_depth < 0 || requires_unroll < 0 ||
(requires_unroll && kernel->any_force_private)) {
isl_map_free(acc);
return requires_unroll < 0 ? isl_stat_error : isl_stat_ok;
}