-
Notifications
You must be signed in to change notification settings - Fork 0
/
isl_ast_build.c
2566 lines (2201 loc) · 71.5 KB
/
isl_ast_build.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012-2013 Ecole Normale Superieure
* Copyright 2014 INRIA Rocquencourt
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege,
* Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
* and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
* B.P. 105 - 78153 Le Chesnay, France
*/
#include <isl/map.h>
#include <isl/aff.h>
#include <isl/constraint.h>
#include <isl/map.h>
#include <isl/union_set.h>
#include <isl/union_map.h>
#include <isl_ast_build_private.h>
#include <isl_ast_private.h>
#include <isl_config.h>
/* Construct a map that isolates the current dimension.
*
* Essentially, the current dimension of "set" is moved to the single output
* dimension in the result, with the current dimension in the domain replaced
* by an unconstrained variable.
*/
__isl_give isl_map *isl_ast_build_map_to_iterator(
__isl_keep isl_ast_build *build, __isl_take isl_set *set)
{
isl_map *map;
map = isl_map_from_domain(set);
map = isl_map_add_dims(map, isl_dim_out, 1);
if (!build)
return isl_map_free(map);
map = isl_map_equate(map, isl_dim_in, build->depth, isl_dim_out, 0);
map = isl_map_eliminate(map, isl_dim_in, build->depth, 1);
return map;
}
/* Initialize the information derived during the AST generation to default
* values for a schedule domain in "space".
*
* We also check that the remaining fields are not NULL so that
* the calling functions don't have to perform this test.
*/
static __isl_give isl_ast_build *isl_ast_build_init_derived(
__isl_take isl_ast_build *build, __isl_take isl_space *space)
{
isl_ctx *ctx;
isl_vec *strides;
build = isl_ast_build_cow(build);
if (!build || !build->domain)
goto error;
ctx = isl_ast_build_get_ctx(build);
strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
strides = isl_vec_set_si(strides, 1);
isl_vec_free(build->strides);
build->strides = strides;
space = isl_space_map_from_set(space);
isl_multi_aff_free(build->offsets);
build->offsets = isl_multi_aff_zero(isl_space_copy(space));
isl_multi_aff_free(build->values);
build->values = isl_multi_aff_identity(isl_space_copy(space));
isl_multi_aff_free(build->internal2input);
build->internal2input = isl_multi_aff_identity(space);
if (!build->iterators || !build->domain || !build->generated ||
!build->pending || !build->values || !build->internal2input ||
!build->strides || !build->offsets || !build->options)
return isl_ast_build_free(build);
return build;
error:
isl_space_free(space);
return isl_ast_build_free(build);
}
/* Return an isl_id called "c%d", with "%d" set to "i".
* If an isl_id with such a name already appears among the parameters
* in build->domain, then adjust the name to "c%d_%d".
*/
static __isl_give isl_id *generate_name(isl_ctx *ctx, int i,
__isl_keep isl_ast_build *build)
{
int j;
char name[16];
isl_set *dom = build->domain;
snprintf(name, sizeof(name), "c%d", i);
j = 0;
while (isl_set_find_dim_by_name(dom, isl_dim_param, name) >= 0)
snprintf(name, sizeof(name), "c%d_%d", i, j++);
return isl_id_alloc(ctx, name, NULL);
}
/* Create an isl_ast_build with "set" as domain.
*
* The input set is usually a parameter domain, but we currently allow it to
* be any kind of set. We set the domain of the returned isl_ast_build
* to "set" and initialize all the other fields to default values.
*/
__isl_give isl_ast_build *isl_ast_build_from_context(__isl_take isl_set *set)
{
int i, n;
isl_ctx *ctx;
isl_space *space;
isl_ast_build *build;
set = isl_set_compute_divs(set);
if (!set)
return NULL;
ctx = isl_set_get_ctx(set);
build = isl_calloc_type(ctx, isl_ast_build);
if (!build)
goto error;
build->ref = 1;
build->domain = set;
build->generated = isl_set_copy(build->domain);
build->pending = isl_set_universe(isl_set_get_space(build->domain));
build->options = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
n = isl_set_dim(set, isl_dim_set);
build->depth = n;
build->iterators = isl_id_list_alloc(ctx, n);
for (i = 0; i < n; ++i) {
isl_id *id;
if (isl_set_has_dim_id(set, isl_dim_set, i))
id = isl_set_get_dim_id(set, isl_dim_set, i);
else
id = generate_name(ctx, i, build);
build->iterators = isl_id_list_add(build->iterators, id);
}
space = isl_set_get_space(set);
if (isl_space_is_params(space))
space = isl_space_set_from_params(space);
return isl_ast_build_init_derived(build, space);
error:
isl_set_free(set);
return NULL;
}
/* Create an isl_ast_build with a universe (parametric) context.
*/
__isl_give isl_ast_build *isl_ast_build_alloc(isl_ctx *ctx)
{
isl_space *space;
isl_set *context;
space = isl_space_params_alloc(ctx, 0);
context = isl_set_universe(space);
return isl_ast_build_from_context(context);
}
__isl_give isl_ast_build *isl_ast_build_copy(__isl_keep isl_ast_build *build)
{
if (!build)
return NULL;
build->ref++;
return build;
}
__isl_give isl_ast_build *isl_ast_build_dup(__isl_keep isl_ast_build *build)
{
isl_ctx *ctx;
isl_ast_build *dup;
if (!build)
return NULL;
ctx = isl_ast_build_get_ctx(build);
dup = isl_calloc_type(ctx, isl_ast_build);
if (!dup)
return NULL;
dup->ref = 1;
dup->outer_pos = build->outer_pos;
dup->depth = build->depth;
dup->iterators = isl_id_list_copy(build->iterators);
dup->domain = isl_set_copy(build->domain);
dup->generated = isl_set_copy(build->generated);
dup->pending = isl_set_copy(build->pending);
dup->values = isl_multi_aff_copy(build->values);
dup->internal2input = isl_multi_aff_copy(build->internal2input);
dup->value = isl_pw_aff_copy(build->value);
dup->strides = isl_vec_copy(build->strides);
dup->offsets = isl_multi_aff_copy(build->offsets);
dup->executed = isl_union_map_copy(build->executed);
dup->single_valued = build->single_valued;
dup->options = isl_union_map_copy(build->options);
dup->at_each_domain = build->at_each_domain;
dup->at_each_domain_user = build->at_each_domain_user;
dup->before_each_for = build->before_each_for;
dup->before_each_for_user = build->before_each_for_user;
dup->after_each_for = build->after_each_for;
dup->after_each_for_user = build->after_each_for_user;
dup->before_each_mark = build->before_each_mark;
dup->before_each_mark_user = build->before_each_mark_user;
dup->after_each_mark = build->after_each_mark;
dup->after_each_mark_user = build->after_each_mark_user;
dup->create_leaf = build->create_leaf;
dup->create_leaf_user = build->create_leaf_user;
dup->node = isl_schedule_node_copy(build->node);
if (build->loop_type) {
int i;
dup->n = build->n;
dup->loop_type = isl_alloc_array(ctx,
enum isl_ast_loop_type, dup->n);
if (dup->n && !dup->loop_type)
return isl_ast_build_free(dup);
for (i = 0; i < dup->n; ++i)
dup->loop_type[i] = build->loop_type[i];
}
if (!dup->iterators || !dup->domain || !dup->generated ||
!dup->pending || !dup->values ||
!dup->strides || !dup->offsets || !dup->options ||
(build->internal2input && !dup->internal2input) ||
(build->executed && !dup->executed) ||
(build->value && !dup->value) ||
(build->node && !dup->node))
return isl_ast_build_free(dup);
return dup;
}
/* Align the parameters of "build" to those of "model", introducing
* additional parameters if needed.
*/
__isl_give isl_ast_build *isl_ast_build_align_params(
__isl_take isl_ast_build *build, __isl_take isl_space *model)
{
build = isl_ast_build_cow(build);
if (!build)
goto error;
build->domain = isl_set_align_params(build->domain,
isl_space_copy(model));
build->generated = isl_set_align_params(build->generated,
isl_space_copy(model));
build->pending = isl_set_align_params(build->pending,
isl_space_copy(model));
build->values = isl_multi_aff_align_params(build->values,
isl_space_copy(model));
build->offsets = isl_multi_aff_align_params(build->offsets,
isl_space_copy(model));
build->options = isl_union_map_align_params(build->options,
isl_space_copy(model));
if (build->internal2input) {
build->internal2input =
isl_multi_aff_align_params(build->internal2input,
model);
if (!build->internal2input)
return isl_ast_build_free(build);
} else {
isl_space_free(model);
}
if (!build->domain || !build->values || !build->offsets ||
!build->options)
return isl_ast_build_free(build);
return build;
error:
isl_space_free(model);
return NULL;
}
__isl_give isl_ast_build *isl_ast_build_cow(__isl_take isl_ast_build *build)
{
if (!build)
return NULL;
if (build->ref == 1)
return build;
build->ref--;
return isl_ast_build_dup(build);
}
__isl_null isl_ast_build *isl_ast_build_free(
__isl_take isl_ast_build *build)
{
if (!build)
return NULL;
if (--build->ref > 0)
return NULL;
isl_id_list_free(build->iterators);
isl_set_free(build->domain);
isl_set_free(build->generated);
isl_set_free(build->pending);
isl_multi_aff_free(build->values);
isl_multi_aff_free(build->internal2input);
isl_pw_aff_free(build->value);
isl_vec_free(build->strides);
isl_multi_aff_free(build->offsets);
isl_multi_aff_free(build->schedule_map);
isl_union_map_free(build->executed);
isl_union_map_free(build->options);
isl_schedule_node_free(build->node);
free(build->loop_type);
isl_set_free(build->isolated);
free(build);
return NULL;
}
isl_ctx *isl_ast_build_get_ctx(__isl_keep isl_ast_build *build)
{
return build ? isl_set_get_ctx(build->domain) : NULL;
}
/* Replace build->options by "options".
*/
__isl_give isl_ast_build *isl_ast_build_set_options(
__isl_take isl_ast_build *build, __isl_take isl_union_map *options)
{
build = isl_ast_build_cow(build);
if (!build || !options)
goto error;
isl_union_map_free(build->options);
build->options = options;
return build;
error:
isl_union_map_free(options);
return isl_ast_build_free(build);
}
/* Set the iterators for the next code generation.
*
* If we still have some iterators left from the previous code generation
* (if any) or if iterators have already been set by a previous
* call to this function, then we remove them first.
*/
__isl_give isl_ast_build *isl_ast_build_set_iterators(
__isl_take isl_ast_build *build, __isl_take isl_id_list *iterators)
{
int dim, n_it;
build = isl_ast_build_cow(build);
if (!build)
goto error;
dim = isl_set_dim(build->domain, isl_dim_set);
n_it = isl_id_list_n_id(build->iterators);
if (n_it < dim)
isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
"isl_ast_build in inconsistent state", goto error);
if (n_it > dim)
build->iterators = isl_id_list_drop(build->iterators,
dim, n_it - dim);
build->iterators = isl_id_list_concat(build->iterators, iterators);
if (!build->iterators)
return isl_ast_build_free(build);
return build;
error:
isl_id_list_free(iterators);
return isl_ast_build_free(build);
}
/* Set the "at_each_domain" callback of "build" to "fn".
*/
__isl_give isl_ast_build *isl_ast_build_set_at_each_domain(
__isl_take isl_ast_build *build,
__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
__isl_keep isl_ast_build *build, void *user), void *user)
{
build = isl_ast_build_cow(build);
if (!build)
return NULL;
build->at_each_domain = fn;
build->at_each_domain_user = user;
return build;
}
/* Set the "before_each_for" callback of "build" to "fn".
*/
__isl_give isl_ast_build *isl_ast_build_set_before_each_for(
__isl_take isl_ast_build *build,
__isl_give isl_id *(*fn)(__isl_keep isl_ast_build *build,
void *user), void *user)
{
build = isl_ast_build_cow(build);
if (!build)
return NULL;
build->before_each_for = fn;
build->before_each_for_user = user;
return build;
}
/* Set the "after_each_for" callback of "build" to "fn".
*/
__isl_give isl_ast_build *isl_ast_build_set_after_each_for(
__isl_take isl_ast_build *build,
__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
__isl_keep isl_ast_build *build, void *user), void *user)
{
build = isl_ast_build_cow(build);
if (!build)
return NULL;
build->after_each_for = fn;
build->after_each_for_user = user;
return build;
}
/* Set the "before_each_mark" callback of "build" to "fn".
*/
__isl_give isl_ast_build *isl_ast_build_set_before_each_mark(
__isl_take isl_ast_build *build,
isl_stat (*fn)(__isl_keep isl_id *mark, __isl_keep isl_ast_build *build,
void *user), void *user)
{
build = isl_ast_build_cow(build);
if (!build)
return NULL;
build->before_each_mark = fn;
build->before_each_mark_user = user;
return build;
}
/* Set the "after_each_mark" callback of "build" to "fn".
*/
__isl_give isl_ast_build *isl_ast_build_set_after_each_mark(
__isl_take isl_ast_build *build,
__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
__isl_keep isl_ast_build *build, void *user), void *user)
{
build = isl_ast_build_cow(build);
if (!build)
return NULL;
build->after_each_mark = fn;
build->after_each_mark_user = user;
return build;
}
/* Set the "create_leaf" callback of "build" to "fn".
*/
__isl_give isl_ast_build *isl_ast_build_set_create_leaf(
__isl_take isl_ast_build *build,
__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_build *build,
void *user), void *user)
{
build = isl_ast_build_cow(build);
if (!build)
return NULL;
build->create_leaf = fn;
build->create_leaf_user = user;
return build;
}
/* Clear all information that is specific to this code generation
* and that is (probably) not meaningful to any nested code generation.
*/
__isl_give isl_ast_build *isl_ast_build_clear_local_info(
__isl_take isl_ast_build *build)
{
isl_space *space;
build = isl_ast_build_cow(build);
if (!build)
return NULL;
space = isl_union_map_get_space(build->options);
isl_union_map_free(build->options);
build->options = isl_union_map_empty(space);
build->at_each_domain = NULL;
build->at_each_domain_user = NULL;
build->before_each_for = NULL;
build->before_each_for_user = NULL;
build->after_each_for = NULL;
build->after_each_for_user = NULL;
build->before_each_mark = NULL;
build->before_each_mark_user = NULL;
build->after_each_mark = NULL;
build->after_each_mark_user = NULL;
build->create_leaf = NULL;
build->create_leaf_user = NULL;
if (!build->options)
return isl_ast_build_free(build);
return build;
}
/* Have any loops been eliminated?
* That is, do any of the original schedule dimensions have a fixed
* value that has been substituted?
*/
static int any_eliminated(isl_ast_build *build)
{
int i;
for (i = 0; i < build->depth; ++i)
if (isl_ast_build_has_affine_value(build, i))
return 1;
return 0;
}
/* Clear build->schedule_map.
* This function should be called whenever anything that might affect
* the result of isl_ast_build_get_schedule_map_multi_aff changes.
* In particular, it should be called when the depth is changed or
* when an iterator is determined to have a fixed value.
*/
static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build *build)
{
if (!build)
return;
isl_multi_aff_free(build->schedule_map);
build->schedule_map = NULL;
}
/* Do we need a (non-trivial) schedule map?
* That is, is the internal schedule space different from
* the external schedule space?
*
* The internal and external schedule spaces are only the same
* if code has been generated for the entire schedule and if none
* of the loops have been eliminated.
*/
__isl_give int isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
{
int dim;
if (!build)
return -1;
dim = isl_set_dim(build->domain, isl_dim_set);
return build->depth != dim || any_eliminated(build);
}
/* Return a mapping from the internal schedule space to the external
* schedule space in the form of an isl_multi_aff.
* The internal schedule space originally corresponds to that of the
* input schedule. This may change during the code generation if
* if isl_ast_build_insert_dim is ever called.
* The external schedule space corresponds to the
* loops that have been generated.
*
* Currently, the only difference between the internal schedule domain
* and the external schedule domain is that some dimensions are projected
* out in the external schedule domain. In particular, the dimensions
* for which no code has been generated yet and the dimensions that correspond
* to eliminated loops.
*
* We cache a copy of the schedule_map in build->schedule_map.
* The cache is cleared through isl_ast_build_reset_schedule_map
* whenever anything changes that might affect the result of this function.
*/
__isl_give isl_multi_aff *isl_ast_build_get_schedule_map_multi_aff(
__isl_keep isl_ast_build *build)
{
isl_space *space;
isl_multi_aff *ma;
if (!build)
return NULL;
if (build->schedule_map)
return isl_multi_aff_copy(build->schedule_map);
space = isl_ast_build_get_space(build, 1);
space = isl_space_map_from_set(space);
ma = isl_multi_aff_identity(space);
if (isl_ast_build_need_schedule_map(build)) {
int i;
int dim = isl_set_dim(build->domain, isl_dim_set);
ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
build->depth, dim - build->depth);
for (i = build->depth - 1; i >= 0; --i)
if (isl_ast_build_has_affine_value(build, i))
ma = isl_multi_aff_drop_dims(ma,
isl_dim_out, i, 1);
}
build->schedule_map = ma;
return isl_multi_aff_copy(build->schedule_map);
}
/* Return a mapping from the internal schedule space to the external
* schedule space in the form of an isl_map.
*/
__isl_give isl_map *isl_ast_build_get_schedule_map(
__isl_keep isl_ast_build *build)
{
isl_multi_aff *ma;
ma = isl_ast_build_get_schedule_map_multi_aff(build);
return isl_map_from_multi_aff(ma);
}
/* Return the position of the dimension in build->domain for which
* an AST node is currently being generated.
*/
int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
{
return build ? build->depth : -1;
}
/* Prepare for generating code for the next level.
* In particular, increase the depth and reset any information
* that is local to the current depth.
*/
__isl_give isl_ast_build *isl_ast_build_increase_depth(
__isl_take isl_ast_build *build)
{
build = isl_ast_build_cow(build);
if (!build)
return NULL;
build->depth++;
isl_ast_build_reset_schedule_map(build);
build->value = isl_pw_aff_free(build->value);
return build;
}
void isl_ast_build_dump(__isl_keep isl_ast_build *build)
{
if (!build)
return;
fprintf(stderr, "domain: ");
isl_set_dump(build->domain);
fprintf(stderr, "generated: ");
isl_set_dump(build->generated);
fprintf(stderr, "pending: ");
isl_set_dump(build->pending);
fprintf(stderr, "iterators: ");
isl_id_list_dump(build->iterators);
fprintf(stderr, "values: ");
isl_multi_aff_dump(build->values);
if (build->value) {
fprintf(stderr, "value: ");
isl_pw_aff_dump(build->value);
}
fprintf(stderr, "strides: ");
isl_vec_dump(build->strides);
fprintf(stderr, "offsets: ");
isl_multi_aff_dump(build->offsets);
fprintf(stderr, "internal2input: ");
isl_multi_aff_dump(build->internal2input);
}
/* Initialize "build" for AST construction in schedule space "space"
* in the case that build->domain is a parameter set.
*
* build->iterators is assumed to have been updated already.
*/
static __isl_give isl_ast_build *isl_ast_build_init(
__isl_take isl_ast_build *build, __isl_take isl_space *space)
{
isl_set *set;
build = isl_ast_build_cow(build);
if (!build)
goto error;
set = isl_set_universe(isl_space_copy(space));
build->domain = isl_set_intersect_params(isl_set_copy(set),
build->domain);
build->pending = isl_set_intersect_params(isl_set_copy(set),
build->pending);
build->generated = isl_set_intersect_params(set, build->generated);
return isl_ast_build_init_derived(build, space);
error:
isl_ast_build_free(build);
isl_space_free(space);
return NULL;
}
/* Assign "aff" to *user and return -1, effectively extracting
* the first (and presumably only) affine expression in the isl_pw_aff
* on which this function is used.
*/
static isl_stat extract_single_piece(__isl_take isl_set *set,
__isl_take isl_aff *aff, void *user)
{
isl_aff **p = user;
*p = aff;
isl_set_free(set);
return isl_stat_error;
}
/* Intersect "set" with the stride constraint of "build", if any.
*/
static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
__isl_keep isl_ast_build *build)
{
isl_set *stride;
if (!build)
return isl_set_free(set);
if (!isl_ast_build_has_stride(build, build->depth))
return set;
stride = isl_ast_build_get_stride_constraint(build);
return isl_set_intersect(set, stride);
}
/* Check if the given bounds on the current dimension (together with
* the stride constraint, if any) imply that
* this current dimension attains only a single value (in terms of
* parameters and outer dimensions).
* If so, we record it in build->value.
* If, moreover, this value can be represented as a single affine expression,
* then we also update build->values, effectively marking the current
* dimension as "eliminated".
*
* When computing the gist of the fixed value that can be represented
* as a single affine expression, it is important to only take into
* account the domain constraints in the original AST build and
* not the domain of the affine expression itself.
* Otherwise, a [i/3] is changed into a i/3 because we know that i
* is a multiple of 3, but then we end up not expressing anywhere
* in the context that i is a multiple of 3.
*/
static __isl_give isl_ast_build *update_values(
__isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
{
int sv;
isl_pw_multi_aff *pma;
isl_aff *aff = NULL;
isl_map *it_map;
isl_set *set;
set = isl_set_from_basic_set(bounds);
set = isl_set_intersect(set, isl_set_copy(build->domain));
set = intersect_stride_constraint(set, build);
it_map = isl_ast_build_map_to_iterator(build, set);
sv = isl_map_is_single_valued(it_map);
if (sv < 0)
build = isl_ast_build_free(build);
if (!build || !sv) {
isl_map_free(it_map);
return build;
}
pma = isl_pw_multi_aff_from_map(it_map);
build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
build->value = isl_pw_aff_coalesce(build->value);
isl_pw_multi_aff_free(pma);
if (!build->value)
return isl_ast_build_free(build);
if (isl_pw_aff_n_piece(build->value) != 1)
return build;
isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff);
build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
if (!build->values)
return isl_ast_build_free(build);
isl_ast_build_reset_schedule_map(build);
return build;
}
/* Update the AST build based on the given loop bounds for
* the current dimension and the stride information available in the build.
*
* We first make sure that the bounds do not refer to any iterators
* that have already been eliminated.
* Then, we check if the bounds imply that the current iterator
* has a fixed value.
* If they do and if this fixed value can be expressed as a single
* affine expression, we eliminate the iterators from the bounds.
* Note that we cannot simply plug in this single value using
* isl_basic_set_preimage_multi_aff as the single value may only
* be defined on a subset of the domain. Plugging in the value
* would restrict the build domain to this subset, while this
* restriction may not be reflected in the generated code.
* Finally, we intersect build->domain with the updated bounds.
* We also add the stride constraint unless we have been able
* to find a fixed value expressed as a single affine expression.
*
* Note that the check for a fixed value in update_values requires
* us to intersect the bounds with the current build domain.
* When we intersect build->domain with the updated bounds in
* the final step, we make sure that these updated bounds have
* not been intersected with the old build->domain.
* Otherwise, we would indirectly intersect the build domain with itself,
* which can lead to inefficiencies, in particular if the build domain
* contains any unknown divs.
*
* The pending and generated sets are not updated by this function to
* match the updated domain.
* The caller still needs to call isl_ast_build_set_pending_generated.
*/
__isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
__isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
{
isl_set *set;
build = isl_ast_build_cow(build);
if (!build)
goto error;
build = update_values(build, isl_basic_set_copy(bounds));
if (!build)
goto error;
set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
if (isl_ast_build_has_affine_value(build, build->depth)) {
set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
set = isl_set_compute_divs(set);
build->pending = isl_set_intersect(build->pending,
isl_set_copy(set));
build->domain = isl_set_intersect(build->domain, set);
} else {
build->domain = isl_set_intersect(build->domain, set);
build = isl_ast_build_include_stride(build);
if (!build)
goto error;
}
isl_basic_set_free(bounds);
if (!build->domain || !build->pending || !build->generated)
return isl_ast_build_free(build);
return build;
error:
isl_ast_build_free(build);
isl_basic_set_free(bounds);
return NULL;
}
/* Update the pending and generated sets of "build" according to "bounds".
* If the build has an affine value at the current depth,
* then isl_ast_build_set_loop_bounds has already set the pending set.
* Otherwise, do it here.
*/
__isl_give isl_ast_build *isl_ast_build_set_pending_generated(
__isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
{
isl_basic_set *generated, *pending;
if (!build)
goto error;
if (isl_ast_build_has_affine_value(build, build->depth)) {
isl_basic_set_free(bounds);
return build;
}
build = isl_ast_build_cow(build);
if (!build)
goto error;
pending = isl_basic_set_copy(bounds);
pending = isl_basic_set_drop_constraints_involving_dims(pending,
isl_dim_set, build->depth, 1);
build->pending = isl_set_intersect(build->pending,
isl_set_from_basic_set(pending));
generated = bounds;
generated = isl_basic_set_drop_constraints_not_involving_dims(
generated, isl_dim_set, build->depth, 1);
build->generated = isl_set_intersect(build->generated,
isl_set_from_basic_set(generated));
if (!build->pending || !build->generated)
return isl_ast_build_free(build);
return build;
error:
isl_ast_build_free(build);
isl_basic_set_free(bounds);
return NULL;
}
/* Intersect build->domain with "set", where "set" is specified
* in terms of the internal schedule domain.
*/
static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
__isl_take isl_ast_build *build, __isl_take isl_set *set)
{
build = isl_ast_build_cow(build);
if (!build)
goto error;
set = isl_set_compute_divs(set);
build->domain = isl_set_intersect(build->domain, set);
build->domain = isl_set_coalesce(build->domain);
if (!build->domain)
return isl_ast_build_free(build);
return build;
error:
isl_ast_build_free(build);
isl_set_free(set);
return NULL;
}
/* Intersect build->generated and build->domain with "set",
* where "set" is specified in terms of the internal schedule domain.
*/
__isl_give isl_ast_build *isl_ast_build_restrict_generated(
__isl_take isl_ast_build *build, __isl_take isl_set *set)
{
set = isl_set_compute_divs(set);
build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
build = isl_ast_build_cow(build);
if (!build)
goto error;
build->generated = isl_set_intersect(build->generated, set);
build->generated = isl_set_coalesce(build->generated);
if (!build->generated)
return isl_ast_build_free(build);
return build;
error:
isl_ast_build_free(build);
isl_set_free(set);
return NULL;
}
/* Replace the set of pending constraints by "guard", which is then
* no longer considered as pending.
* That is, add "guard" to the generated constraints and clear all pending
* constraints, making the domain equal to the generated constraints.
*/
__isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
__isl_take isl_ast_build *build, __isl_take isl_set *guard)
{
build = isl_ast_build_restrict_generated(build, guard);
build = isl_ast_build_cow(build);
if (!build)
return NULL;
isl_set_free(build->domain);
build->domain = isl_set_copy(build->generated);
isl_set_free(build->pending);
build->pending = isl_set_universe(isl_set_get_space(build->domain));
if (!build->pending)
return isl_ast_build_free(build);
return build;
}
/* Intersect build->domain with "set", where "set" is specified
* in terms of the external schedule domain.
*/
__isl_give isl_ast_build *isl_ast_build_restrict(
__isl_take isl_ast_build *build, __isl_take isl_set *set)
{
if (isl_set_is_params(set))
return isl_ast_build_restrict_generated(build, set);
if (isl_ast_build_need_schedule_map(build)) {
isl_multi_aff *ma;
ma = isl_ast_build_get_schedule_map_multi_aff(build);
set = isl_set_preimage_multi_aff(set, ma);
}
return isl_ast_build_restrict_generated(build, set);