-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu.c
5816 lines (5082 loc) · 183 KB
/
gpu.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-2013 Ecole Normale Superieure
* Copyright 2015-2016 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 <assert.h>
#include <stdlib.h>
#include <string.h>
#include <isl/polynomial.h>
#include <isl/union_set.h>
#include <isl/aff.h>
#include <isl/ilp.h>
#include <isl/flow.h>
#include <isl/schedule.h>
#include <isl/schedule_node.h>
#include <isl/options.h>
#include <isl/ast_build.h>
#include "cpu.h"
#include "gpu.h"
#include "gpu_array_tile.h"
#include "gpu_group.h"
#include "gpu_hybrid.h"
#include "gpu_tree.h"
#include "hybrid.h"
#include "schedule.h"
#include "ppcg_options.h"
#include "print.h"
#include "util.h"
struct gpu_array_info;
/* Return the name of the outer array (of structs) accessed by "access".
*/
static const char *get_outer_array_name(__isl_keep isl_map *access)
{
isl_space *space;
const char *name;
space = isl_space_range(isl_map_get_space(access));
while (space && isl_space_is_wrapping(space))
space = isl_space_domain(isl_space_unwrap(space));
name = isl_space_get_tuple_name(space, isl_dim_set);
isl_space_free(space);
return name;
}
/* Collect all references to the given array and store pointers to them
* in array->refs.
*/
static void collect_references(struct gpu_prog *prog,
struct gpu_array_info *array)
{
int i;
int n;
n = 0;
for (i = 0; i < prog->n_stmts; ++i) {
struct gpu_stmt *stmt = &prog->stmts[i];
struct gpu_stmt_access *access;
for (access = stmt->accesses; access; access = access->next) {
const char *name;
name = get_outer_array_name(access->access);
if (name && !strcmp(array->name, name))
n++;
}
}
array->n_ref = n;
array->refs = isl_alloc_array(prog->ctx, struct gpu_stmt_access *, n);
assert(array->refs);
n = 0;
for (i = 0; i < prog->n_stmts; ++i) {
struct gpu_stmt *stmt = &prog->stmts[i];
struct gpu_stmt_access *access;
for (access = stmt->accesses; access; access = access->next) {
const char *name;
name = get_outer_array_name(access->access);
if (!name || strcmp(array->name, name))
continue;
array->refs[n++] = access;
}
}
}
/* Compute and return the extent of "array", taking into account the set of
* accessed elements.
*
* In particular, the extent in the outer dimension is taken
* from "accessed", while the extents in the remaining dimensions
* are taken from array->extent.
*
* The extent in the outer dimension cannot be taken from array->extent
* because that may be unbounded. Furthermore, even if it is bounded,
* it may be larger than the piece of the array that is being accessed.
*/
static __isl_give isl_set *compute_extent(struct pet_array *array,
__isl_keep isl_set *accessed)
{
int n_index;
isl_id *id;
isl_set *outer;
isl_set *extent;
extent = isl_set_copy(array->extent);
n_index = isl_set_dim(accessed, isl_dim_set);
if (n_index == 0)
return extent;
extent = isl_set_project_out(extent, isl_dim_set, 0, 1);
outer = isl_set_copy(accessed);
outer = isl_set_project_out(outer, isl_dim_set, 1, n_index - 1);
extent = isl_set_flat_product(outer, extent);
id = isl_set_get_tuple_id(accessed);
extent = isl_set_set_tuple_id(extent, id);
return extent;
}
/* Is the array "array" being extracted a read-only scalar?
*
* That is, is "array" a scalar that is never possibly written to.
* An array containing structures is never considered to be a scalar.
*/
static int is_read_only_scalar(struct gpu_array_info *array,
struct gpu_prog *prog)
{
isl_set *space;
isl_union_map *write;
int empty;
if (array->has_compound_element)
return 0;
if (array->n_index != 0)
return 0;
write = isl_union_map_copy(prog->may_write);
space = isl_set_universe(isl_space_copy(array->space));
write = isl_union_map_intersect_range(write,
isl_union_set_from_set(space));
empty = isl_union_map_is_empty(write);
isl_union_map_free(write);
return empty;
}
/* Is "array" only accessed as individual, fixed elements?
* That is, does each access to "array" access a single, fixed element?
*/
static isl_bool only_fixed_element_accessed(struct gpu_array_info *array)
{
int i;
for (i = 0; i < array->n_ref; ++i)
if (!array->refs[i]->fixed_element)
return isl_bool_false;
return isl_bool_true;
}
/* Compute bounds on the host array "pa" based on the corresponding
* accessed elements in "arrays"
* and collect all references to the array.
* Store the results in "info".
*
* If the array is zero-dimensional and does not contain structures,
* i.e., if the array is a scalar, we check whether it is read-only.
* We also check whether the array is accessed at all.
*/
static int extract_array_info(struct gpu_prog *prog,
struct gpu_array_info *info, struct pet_array *pa,
__isl_keep isl_union_set *arrays)
{
int empty;
const char *name;
int n_index;
isl_multi_pw_aff *bounds;
isl_set *accessed, *extent;
n_index = isl_set_dim(pa->extent, isl_dim_set);
name = isl_set_get_tuple_name(pa->extent);
info->space = isl_set_get_space(pa->extent);
info->name = strdup(name);
info->n_index = n_index;
info->linearize = prog->scop->options->linearize_device_arrays;
info->type = strdup(pa->element_type);
info->size = pa->element_size;
info->local = pa->declared && !pa->exposed;
info->has_compound_element = pa->element_is_record;
info->read_only_scalar = is_read_only_scalar(info, prog);
info->declared_extent = isl_set_copy(pa->extent);
accessed = isl_union_set_extract_set(arrays,
isl_space_copy(info->space));
empty = isl_set_is_empty(accessed);
extent = compute_extent(pa, accessed);
isl_set_free(accessed);
info->extent = extent;
if (empty < 0)
return -1;
info->accessed = !empty;
bounds = ppcg_size_from_extent(isl_set_copy(extent));
bounds = isl_multi_pw_aff_gist(bounds, isl_set_copy(prog->context));
if (!bounds)
return -1;
if (!isl_multi_pw_aff_is_cst(bounds))
info->linearize = 1;
info->bound = bounds;
collect_references(prog, info);
info->only_fixed_element = only_fixed_element_accessed(info);
return 0;
}
/* Remove independence from the order constraints "order" on array "array".
* Since the pairs of iterations in the filter relation of an independence
* are guaranteed to be completely independent by the user, there is
* no need to ensure that live ranges are ordered along those pairs.
* We make an exception for local variables, though, as the independence
* guarantee does not apply to those.
*
* The order constraints are used in two places.
* Those on scalars are used in check_scalar_live_ranges to check if
* we need to force the scalar to be private. Any non-local scalar
* should not be forced scalar if it only appears in independent loops.
* Those on non-scalars are added to the coincidence constraints
* in compute_schedule because we do not support any array expansion.
* Accesses to non-local arrays should not prevent a loop from being
* considered coincident so we should indeed remove those constraints
* from the order constraints.
*/
static __isl_give isl_union_map *remove_independences(struct gpu_prog *prog,
struct gpu_array_info *array, __isl_take isl_union_map *order)
{
int i;
for (i = 0; i < prog->scop->pet->n_independence; ++i) {
struct pet_independence *pi = prog->scop->pet->independences[i];
if (isl_union_set_contains(pi->local, array->space))
continue;
order = isl_union_map_subtract(order,
isl_union_map_copy(pi->filter));
}
return order;
}
/* For each array in "prog", store the (untagged) order dependences
* derived from the array in array->dep_order.
* In particular, consider all references that access the given array
* and take the order dependences that have one of these references
* as source. (Since an order dependence relates two references to
* the same array, the target of these order dependences will also
* be one of these references.)
* Additionally, store the union of these array->dep_order relations
* for all arrays that cannot be mapped to private memory in prog->array_order.
*/
void collect_order_dependences(struct gpu_prog *prog)
{
int i;
isl_space *space;
isl_union_map *accesses;
space = isl_union_map_get_space(prog->read);
prog->array_order = isl_union_map_empty(space);
accesses = isl_union_map_copy(prog->scop->tagged_reads);
accesses = isl_union_map_union(accesses,
isl_union_map_copy(prog->scop->tagged_may_writes));
accesses = isl_union_map_universe(accesses);
accesses = isl_union_map_apply_range(accesses,
isl_union_map_copy(prog->to_outer));
for (i = 0; i < prog->n_array; ++i) {
struct gpu_array_info *array = &prog->array[i];
isl_set *set;
isl_union_set *uset;
isl_union_map *order;
set = isl_set_universe(isl_space_copy(array->space));
uset = isl_union_set_from_set(set);
uset = isl_union_map_domain(
isl_union_map_intersect_range(isl_union_map_copy(accesses),
uset));
order = isl_union_map_copy(prog->scop->tagged_dep_order);
order = isl_union_map_intersect_domain(order, uset);
order = isl_union_map_zip(order);
order = isl_union_set_unwrap(isl_union_map_domain(order));
order = remove_independences(prog, array, order);
array->dep_order = order;
if (gpu_array_can_be_private(array))
continue;
prog->array_order = isl_union_map_union(prog->array_order,
isl_union_map_copy(array->dep_order));
}
isl_union_map_free(accesses);
}
/* Construct a gpu_array_info for each array referenced by prog->scop and
* collect them in prog->array.
*
* The sizes are based on the extents and the set of possibly accessed
* elements by "prog".
* If there are any member accesses involved, then they are first mapped
* to the outer arrays of structs.
* Only extract gpu_array_info entries for these outer arrays.
*
* If we are allowing live range reordering, then also set
* the dep_order field. Otherwise leave it NULL.
*/
static int collect_array_info(struct gpu_prog *prog)
{
int i;
int r = 0;
isl_union_set *arrays;
arrays = isl_union_map_range(isl_union_map_copy(prog->read));
arrays = isl_union_set_union(arrays,
isl_union_map_range(isl_union_map_copy(prog->may_write)));
arrays = isl_union_set_apply(arrays,
isl_union_map_copy(prog->to_outer));
arrays = isl_union_set_coalesce(arrays);
prog->n_array = prog->scop->pet->n_array;
prog->array = isl_calloc_array(prog->ctx,
struct gpu_array_info, prog->n_array);
assert(prog->array);
prog->n_array = 0;
for (i = 0; i < prog->scop->pet->n_array; ++i) {
isl_bool field;
field = isl_set_is_wrapping(prog->scop->pet->arrays[i]->extent);
if (field < 0)
break;
if (field)
continue;
if (extract_array_info(prog, &prog->array[prog->n_array++],
prog->scop->pet->arrays[i], arrays) < 0)
r = -1;
}
if (i < prog->scop->pet->n_array)
r = -1;
isl_union_set_free(arrays);
if (prog->scop->options->live_range_reordering)
collect_order_dependences(prog);
return r;
}
static void free_array_info(struct gpu_prog *prog)
{
int i;
for (i = 0; i < prog->n_array; ++i) {
free(prog->array[i].type);
free(prog->array[i].name);
isl_multi_pw_aff_free(prog->array[i].bound);
isl_ast_expr_free(prog->array[i].bound_expr);
isl_space_free(prog->array[i].space);
isl_set_free(prog->array[i].declared_extent);
isl_set_free(prog->array[i].extent);
isl_ast_expr_free(prog->array[i].declared_size);
free(prog->array[i].refs);
isl_union_map_free(prog->array[i].dep_order);
}
free(prog->array);
}
/* Check if a gpu array is a scalar. A scalar is a value that is not stored
* as an array or through a pointer reference, but as a single data element.
* At the moment, scalars are represented as zero-dimensional arrays.
* Note that the single data element may be an entire structure.
*/
int gpu_array_is_scalar(struct gpu_array_info *array)
{
return array->n_index == 0;
}
/* Can "array" be mapped to private memory?
* That is, is it only accessed as individual elements with
* constant index expressions?
*/
isl_bool gpu_array_can_be_private(struct gpu_array_info *array)
{
if (!array)
return isl_bool_error;
return array->only_fixed_element;
}
/* Is "array" a read-only scalar?
*/
int gpu_array_is_read_only_scalar(struct gpu_array_info *array)
{
return array->read_only_scalar;
}
/* Does "array" need to be allocated on the device?
* If it is a read-only scalar, then it will be passed as an argument
* to the kernel and therefore does not require any allocation.
* If this device memory is not accessed at all, then it does not
* need to be allocated either.
*/
int gpu_array_requires_device_allocation(struct gpu_array_info *array)
{
if (gpu_array_is_read_only_scalar(array))
return 0;
if (!array->global)
return 0;
return 1;
}
/* Return the set of parameter values for which the array has a positive
* size in all dimensions.
* If the sizes are only valid for some parameter values, then those
* constraints are also taken into account.
*/
__isl_give isl_set *gpu_array_positive_size_guard(struct gpu_array_info *array)
{
int i;
isl_space *space;
isl_set *guard;
if (!array)
return NULL;
space = isl_space_params(isl_space_copy(array->space));
guard = isl_set_universe(space);
for (i = 0; i < array->n_index; ++i) {
isl_pw_aff *bound;
isl_set *guard_i, *zero;
bound = isl_multi_pw_aff_get_pw_aff(array->bound, i);
guard_i = isl_pw_aff_nonneg_set(isl_pw_aff_copy(bound));
zero = isl_pw_aff_zero_set(bound);
guard_i = isl_set_subtract(guard_i, zero);
guard = isl_set_intersect(guard, guard_i);
}
return guard;
}
/* Internal data structure for extract_size_of_type.
* "type" specifies the name of the space that we want to extract.
* "res" is used to store the subset of that space.
*/
struct ppcg_extract_size_data {
const char *type;
isl_set *res;
};
/* This function is called for each set in a union_set.
* If the name of the set matches data->type, we store the
* set in data->res.
*/
static isl_stat extract_size_of_type(__isl_take isl_set *size, void *user)
{
struct ppcg_extract_size_data *data = user;
const char *name;
name = isl_set_get_tuple_name(size);
if (name && !strcmp(name, data->type)) {
data->res = size;
return isl_stat_error;
}
isl_set_free(size);
return isl_stat_ok;
}
/* Given a union map { kernel[i] -> *[...] },
* return the range in the space called "type" for the kernel with
* sequence number "id".
*/
static __isl_give isl_set *extract_sizes(__isl_keep isl_union_map *sizes,
const char *type, int id)
{
isl_space *space;
isl_set *dom;
isl_union_set *local_sizes;
struct ppcg_extract_size_data data = { type, NULL };
if (!sizes)
return NULL;
space = isl_union_map_get_space(sizes);
space = isl_space_set_from_params(space);
space = isl_space_add_dims(space, isl_dim_set, 1);
space = isl_space_set_tuple_name(space, isl_dim_set, "kernel");
dom = isl_set_universe(space);
dom = isl_set_fix_si(dom, isl_dim_set, 0, id);
local_sizes = isl_union_set_apply(isl_union_set_from_set(dom),
isl_union_map_copy(sizes));
isl_union_set_foreach_set(local_sizes, &extract_size_of_type, &data);
isl_union_set_free(local_sizes);
return data.res;
}
/* Given a singleton set, extract the first (at most *len) elements
* of the single integer tuple into *sizes and update *len if needed.
*/
static void read_sizes_from_set(__isl_take isl_set *set, int *sizes, int *len)
{
int i;
int dim;
if (!set)
return;
dim = isl_set_dim(set, isl_dim_set);
if (dim < *len)
*len = dim;
for (i = 0; i < *len; ++i) {
isl_val *v;
v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
assert(v);
sizes[i] = isl_val_get_num_si(v);
isl_val_free(v);
}
isl_set_free(set);
}
/* Add the map { kernel[id] -> type[sizes] } to gen->used_sizes,
* if the option debug->dump_sizes is set.
*/
static void set_used_sizes(struct gpu_gen *gen, const char *type, int id,
int *sizes, int len)
{
int i;
isl_space *space;
isl_map *map;
if (!gen->options->debug->dump_sizes)
return;
space = isl_union_map_get_space(gen->used_sizes);
space = isl_space_set_from_params(space);
space = isl_space_add_dims(space, isl_dim_set, 1);
space = isl_space_set_tuple_name(space, isl_dim_set, "kernel");
space = isl_space_from_domain(space);
space = isl_space_add_dims(space, isl_dim_out, len);
space = isl_space_set_tuple_name(space, isl_dim_out, type);
map = isl_map_universe(space);
map = isl_map_fix_si(map, isl_dim_in, 0, id);
for (i = 0; i < len; ++i)
map = isl_map_fix_si(map, isl_dim_out, i, sizes[i]);
gen->used_sizes = isl_union_map_add_map(gen->used_sizes, map);
}
/* Extract user specified "tile" sizes from the "sizes" command line option,
* defaulting to option->tile_size in each dimension.
* *tile_len contains the maximum number of tile sizes needed.
* Update *tile_len to the number of specified tile sizes, if any, and
* return a pointer to the tile sizes (or NULL on error).
* Add the effectively used sizes to gen->used_sizes.
*/
static int *read_tile_sizes(struct gpu_gen *gen, int *tile_len)
{
int n;
int *tile_size;
isl_set *size;
tile_size = isl_alloc_array(gen->ctx, int, *tile_len);
if (!tile_size)
return NULL;
for (n = 0; n < *tile_len; ++n)
tile_size[n] = gen->options->tile_size;
size = extract_sizes(gen->sizes, "tile", gen->kernel_id);
read_sizes_from_set(size, tile_size, tile_len);
set_used_sizes(gen, "tile", gen->kernel_id, tile_size, *tile_len);
return tile_size;
}
/* Extract user specified "block" sizes from the "sizes" command line option,
* after filling in some potentially useful defaults.
*/
static void read_block_sizes(struct ppcg_kernel *kernel,
__isl_keep isl_union_map *sizes)
{
isl_set *size;
if (kernel->n_block > 3)
kernel->n_block = 3;
switch (kernel->n_block) {
case 1:
kernel->block_dim[0] = 512;
break;
case 2:
kernel->block_dim[0] = 32;
kernel->block_dim[1] = 16;
break;
default:
kernel->block_dim[0] = 32;
kernel->block_dim[1] = 4;
kernel->block_dim[2] = 4;
break;
}
size = extract_sizes(sizes, "block", kernel->id);
read_sizes_from_set(size, kernel->block_dim, &kernel->n_block);
}
/* Extract user specified "grid" sizes from the "sizes" command line option,
* after filling in some potentially useful defaults.
*/
static void read_grid_sizes(struct ppcg_kernel *kernel,
__isl_keep isl_union_map *sizes)
{
isl_set *size;
if (kernel->n_grid > 2)
kernel->n_grid = 2;
switch (kernel->n_grid) {
case 1:
kernel->grid_dim[0] = 32768;
break;
default:
kernel->grid_dim[0] = 256;
kernel->grid_dim[1] = 256;
break;
}
size = extract_sizes(sizes, "grid", kernel->id);
read_sizes_from_set(size, kernel->grid_dim, &kernel->n_grid);
}
/* Extract user specified grid and block sizes from the gen->sizes
* command line option after filling in some potentially useful defaults.
* Store the extracted sizes in "kernel".
* Add the effectively used sizes to gen->used_sizes.
*/
static void read_grid_and_block_sizes(struct ppcg_kernel *kernel,
struct gpu_gen *gen)
{
read_block_sizes(kernel, gen->sizes);
read_grid_sizes(kernel, gen->sizes);
set_used_sizes(gen, "block", kernel->id,
kernel->block_dim, kernel->n_block);
set_used_sizes(gen, "grid", kernel->id,
kernel->grid_dim, kernel->n_grid);
}
static void *free_stmts(struct gpu_stmt *stmts, int n)
{
int i;
if (!stmts)
return NULL;
for (i = 0; i < n; ++i) {
struct gpu_stmt_access *access, *next;
for (access = stmts[i].accesses; access; access = next) {
next = access->next;
isl_id_free(access->ref_id);
isl_map_free(access->access);
isl_map_free(access->tagged_access);
free(access);
}
isl_id_free(stmts[i].id);
}
free(stmts);
return NULL;
}
/* Add parameters p[i] with identifiers "ids" to "set",
* with bounds to 0 <= p[i] < size[i].
*/
__isl_give isl_set *add_bounded_parameters(__isl_take isl_set *set,
int *size, __isl_keep isl_id_list *ids)
{
int i, len;
unsigned nparam;
len = isl_id_list_n_id(ids);
nparam = isl_set_dim(set, isl_dim_param);
set = isl_set_add_dims(set, isl_dim_param, len);
for (i = 0; i < len; ++i) {
isl_id *id;
id = isl_id_list_get_id(ids, i);
set = isl_set_set_dim_id(set, isl_dim_param, nparam + i, id);
set = isl_set_lower_bound_si(set, isl_dim_param, nparam + i, 0);
set = isl_set_upper_bound_si(set, isl_dim_param,
nparam + i, size[i] - 1);
}
return set;
}
/* Add "len" parameters p[i] with identifiers "ids" and intersect "set"
* with
*
* { : 0 <= p[i] < size[i] }
*
* or an overapproximation.
*/
static __isl_give isl_set *add_bounded_parameters_dynamic(
__isl_take isl_set *set, __isl_keep isl_multi_pw_aff *size,
__isl_keep isl_id_list *ids)
{
int i, len;
unsigned nparam;
isl_space *space;
isl_local_space *ls;
len = isl_multi_pw_aff_dim(size, isl_dim_out);
nparam = isl_set_dim(set, isl_dim_param);
set = isl_set_add_dims(set, isl_dim_param, len);
for (i = 0; i < len; ++i) {
isl_id *id;
id = isl_id_list_get_id(ids, i);
set = isl_set_set_dim_id(set, isl_dim_param, nparam + i, id);
}
space = isl_space_params(isl_set_get_space(set));
ls = isl_local_space_from_space(space);
for (i = 0; i < len; ++i) {
isl_pw_aff *param, *size_i, *zero;
isl_set *bound;
param = isl_pw_aff_var_on_domain(isl_local_space_copy(ls),
isl_dim_param, nparam + i);
size_i = isl_multi_pw_aff_get_pw_aff(size, i);
bound = isl_pw_aff_lt_set(isl_pw_aff_copy(param), size_i);
bound = isl_set_from_basic_set(isl_set_simple_hull(bound));
set = isl_set_intersect_params(set, bound);
zero = isl_pw_aff_zero_on_domain(isl_local_space_copy(ls));
bound = isl_pw_aff_ge_set(param, zero);
set = isl_set_intersect_params(set, bound);
}
isl_local_space_free(ls);
return set;
}
/* Return the union of all tagged access relations in the group.
*/
static __isl_give isl_union_map *group_tagged_access_relation(
struct gpu_array_ref_group *group)
{
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;
map_i = isl_map_copy(group->refs[i]->tagged_access);
access = isl_union_map_union(access,
isl_union_map_from_map(map_i));
}
return access;
}
/* Return the extent of "array", recomputed from the bounds.
* The recomputed extent may be simpler than the original extent.
*/
static __isl_give isl_set *array_extent(struct gpu_array_info *array)
{
int i;
isl_id *id;
isl_space *space;
isl_local_space *ls;
isl_set *extent;
id = isl_set_get_tuple_id(array->extent);
space = isl_set_get_space(array->extent);
extent = isl_set_universe(isl_space_copy(space));
ls = isl_local_space_from_space(space);
for (i = 0; i < array->n_index; ++i) {
isl_pw_aff *bound;
isl_aff *aff;
isl_pw_aff *index;
isl_set *lt;
extent = isl_set_lower_bound_si(extent, isl_dim_set, i, 0);
aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
isl_dim_set, i);
index = isl_pw_aff_from_aff(aff);
bound = isl_multi_pw_aff_get_pw_aff(array->bound, i);
bound = isl_pw_aff_from_range(bound);
bound = isl_pw_aff_add_dims(bound, isl_dim_in, array->n_index);
bound = isl_pw_aff_set_tuple_id(bound, isl_dim_in,
isl_id_copy(id));
lt = isl_pw_aff_lt_set(index, bound);
extent = isl_set_intersect(extent, lt);
}
isl_local_space_free(ls);
isl_id_free(id);
return extent;
}
/* Return a map from the first group->shared_tile->depth dimensions
* of the computed schedule to the array tile in
* global memory that corresponds to the shared memory copy.
*
* In particular, return a map
*
* { D[i] -> A[a] }
*
* with constraints
*
* tile_offset(i) <= a <= tile_offset(i) + tile_size - 1 (1)
*
* and
*
* 0 <= a <= array_size - 1 (2)
*
* Note that if some stride has been detected (i.e., when
* group->shared_tile->bound[i].shift is set), then a in (1) refers
* to the shifted and scaled down version.
*
* Constraints (1) are obtained by mapping the size constraints on the
* shared/private memory tile back to the access relation.
* Constraints (2) are obtained from the (recomputed) extent.
*/
static __isl_give isl_map *group_tile(struct gpu_array_ref_group *group)
{
int i;
int n_index = group->array->n_index;
isl_map *tile;
isl_space *space;
isl_set *local;
isl_set *extent;
space = isl_multi_aff_get_space(group->shared_tile->tiling);
space = isl_space_range(space);
local = isl_set_universe(space);
for (i = 0; i < n_index; ++i) {
isl_val *bound;
local = isl_set_lower_bound_si(local, isl_dim_set, i, 0);
bound = isl_val_copy(group->shared_tile->bound[i].size);
bound = isl_val_sub_ui(bound, 1);
local = isl_set_upper_bound_val(local, isl_dim_set, i, bound);
}
local = isl_set_preimage_multi_aff(local,
isl_multi_aff_copy(group->shared_tile->tiling));
tile = isl_set_unwrap(local);
extent = array_extent(group->array);
tile = isl_map_intersect_range(tile, extent);
return tile;
}
/* Given a mapping "iterator_map" from the AST schedule to a domain,
* return the corresponding mapping from the AST schedule to
* to the outer kernel->copy_schedule_dim dimensions of
* the schedule computed by PPCG for this kernel.
*
* Note that kernel->copy_schedule_dim is at least as large as
* the largest depth of any array reference group associated to the kernel.
* This is needed as the returned schedule is used to extract a mapping
* to the outer tile->depth dimensions in transform_index.
*/
static __isl_give isl_pw_multi_aff *compute_sched_to_copy(
struct ppcg_kernel *kernel, __isl_take isl_pw_multi_aff *iterator_map)
{
isl_union_pw_multi_aff *upma;
isl_pw_multi_aff *pma;
isl_space *space;
space = isl_space_range(isl_pw_multi_aff_get_space(iterator_map));
space = isl_space_from_domain(space);
space = isl_space_add_dims(space, isl_dim_out,
kernel->copy_schedule_dim);
upma = isl_union_pw_multi_aff_copy(kernel->copy_schedule);
pma = isl_union_pw_multi_aff_extract_pw_multi_aff(upma, space);
isl_union_pw_multi_aff_free(upma);
return isl_pw_multi_aff_pullback_pw_multi_aff(pma, iterator_map);
}
/* If max_shared_memory is not set to infinity (-1), then make
* sure that the total amount of shared memory required by the
* array reference groups mapped to shared memory by "kernel"
* is no larger than this maximum.
*
* We apply a greedy approach and discard (keep in global memory)
* those groups that would result in a total memory size that
* is larger than the maximum.
*
* This function should be called after any function that may
* affect the decision on whether to place a reference group
* in private, shared or global memory.
*/
static void check_shared_memory_bound(struct ppcg_kernel *kernel)
{
int i, j;
isl_val *left, *size;
if (kernel->options->max_shared_memory < 0)
return;
left = isl_val_int_from_si(kernel->ctx,
kernel->options->max_shared_memory);
for (i = 0; i < kernel->n_array; ++i) {
struct gpu_local_array_info *local = &kernel->array[i];
for (j = 0; j < local->n_group; ++j) {
struct gpu_array_ref_group *group;
enum ppcg_group_access_type type;
group = local->groups[j];
type = gpu_array_ref_group_type(group);
if (type != ppcg_access_shared)
continue;
size = gpu_array_tile_size(group->shared_tile);
size = isl_val_mul_ui(size, local->array->size);
if (isl_val_le(size, left)) {
left = isl_val_sub(left, size);
continue;
}
isl_val_free(size);
group->shared_tile =
gpu_array_tile_free(group->shared_tile);
}
}
isl_val_free(left);
}
/* Mark all arrays of "kernel" that have an array reference group
* that is not mapped to private or shared memory as
* accessing the corresponding global device memory.
*/
static void mark_global_arrays(struct ppcg_kernel *kernel)
{
int i, j;
for (i = 0; i < kernel->n_array; ++i) {
struct gpu_local_array_info *local = &kernel->array[i];
if (local->global)
continue;
for (j = 0; j < local->n_group; ++j) {
if (gpu_array_ref_group_tile(local->groups[j]))
continue;
local->global = 1;
local->array->global = 1;
break;
}
}
}
/* Compute a tiling for all the array reference groups in "kernel".
*/
static void compute_group_tilings(struct ppcg_kernel *kernel)