Coverage Report

Created: 2018-06-24 14:39

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_ast_codegen.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2012-2014 Ecole Normale Superieure
3
 * Copyright 2014      INRIA Rocquencourt
4
 *
5
 * Use of this software is governed by the MIT license
6
 *
7
 * Written by Sven Verdoolaege,
8
 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9
 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10
 * B.P. 105 - 78153 Le Chesnay, France
11
 */
12
13
#include <limits.h>
14
#include <isl/id.h>
15
#include <isl/val.h>
16
#include <isl/space.h>
17
#include <isl/aff.h>
18
#include <isl/constraint.h>
19
#include <isl/set.h>
20
#include <isl/ilp.h>
21
#include <isl/union_set.h>
22
#include <isl/union_map.h>
23
#include <isl/schedule_node.h>
24
#include <isl_sort.h>
25
#include <isl_tarjan.h>
26
#include <isl_ast_private.h>
27
#include <isl_ast_build_expr.h>
28
#include <isl_ast_build_private.h>
29
#include <isl_ast_graft_private.h>
30
31
/* Data used in generate_domain.
32
 *
33
 * "build" is the input build.
34
 * "list" collects the results.
35
 */
36
struct isl_generate_domain_data {
37
  isl_ast_build *build;
38
39
  isl_ast_graft_list *list;
40
};
41
42
static __isl_give isl_ast_graft_list *generate_next_level(
43
  __isl_take isl_union_map *executed,
44
  __isl_take isl_ast_build *build);
45
static __isl_give isl_ast_graft_list *generate_code(
46
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
47
  int internal);
48
49
/* Generate an AST for a single domain based on
50
 * the (non single valued) inverse schedule "executed".
51
 *
52
 * We extend the schedule with the iteration domain
53
 * and continue generating through a call to generate_code.
54
 *
55
 * In particular, if executed has the form
56
 *
57
 *  S -> D
58
 *
59
 * then we continue generating code on
60
 *
61
 *  [S -> D] -> D
62
 *
63
 * The extended inverse schedule is clearly single valued
64
 * ensuring that the nested generate_code will not reach this function,
65
 * but will instead create calls to all elements of D that need
66
 * to be executed from the current schedule domain.
67
 */
68
static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
69
  struct isl_generate_domain_data *data)
70
22
{
71
22
  isl_map *identity;
72
22
  isl_ast_build *build;
73
22
  isl_ast_graft_list *list;
74
22
75
22
  build = isl_ast_build_copy(data->build);
76
22
77
22
  identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
78
22
  executed = isl_map_domain_product(executed, identity);
79
22
  build = isl_ast_build_set_single_valued(build, 1);
80
22
81
22
  list = generate_code(isl_union_map_from_map(executed), build, 1);
82
22
83
22
  data->list = isl_ast_graft_list_concat(data->list, list);
84
22
85
22
  return isl_stat_ok;
86
22
}
87
88
/* Call the at_each_domain callback, if requested by the user,
89
 * after recording the current inverse schedule in the build.
90
 */
91
static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
92
  __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
93
2.15k
{
94
2.15k
  if (!graft || !build)
95
0
    return isl_ast_graft_free(graft);
96
2.15k
  if (!build->at_each_domain)
97
10
    return graft;
98
2.14k
99
2.14k
  build = isl_ast_build_copy(build);
100
2.14k
  build = isl_ast_build_set_executed(build,
101
2.14k
      isl_union_map_from_map(isl_map_copy(executed)));
102
2.14k
  if (!build)
103
0
    return isl_ast_graft_free(graft);
104
2.14k
105
2.14k
  graft->node = build->at_each_domain(graft->node,
106
2.14k
          build, build->at_each_domain_user);
107
2.14k
  isl_ast_build_free(build);
108
2.14k
109
2.14k
  if (!graft->node)
110
0
    graft = isl_ast_graft_free(graft);
111
2.14k
112
2.14k
  return graft;
113
2.14k
}
114
115
/* Generate a call expression for the single executed
116
 * domain element "map" and put a guard around it based its (simplified)
117
 * domain.  "executed" is the original inverse schedule from which "map"
118
 * has been derived.  In particular, "map" is either identical to "executed"
119
 * or it is the result of gisting "executed" with respect to the build domain.
120
 * "executed" is only used if there is an at_each_domain callback.
121
 *
122
 * At this stage, any pending constraints in the build can no longer
123
 * be simplified with respect to any enforced constraints since
124
 * the call node does not have any enforced constraints.
125
 * Since all pending constraints not covered by any enforced constraints
126
 * will be added as a guard to the graft in create_node_scaled,
127
 * even in the eliminated case, the pending constraints
128
 * can be considered to have been generated by outer constructs.
129
 *
130
 * If the user has set an at_each_domain callback, it is called
131
 * on the constructed call expression node.
132
 */
133
static isl_stat add_domain(__isl_take isl_map *executed,
134
  __isl_take isl_map *map, struct isl_generate_domain_data *data)
135
2.15k
{
136
2.15k
  isl_ast_build *build;
137
2.15k
  isl_ast_graft *graft;
138
2.15k
  isl_ast_graft_list *list;
139
2.15k
  isl_set *guard, *pending;
140
2.15k
141
2.15k
  build = isl_ast_build_copy(data->build);
142
2.15k
  pending = isl_ast_build_get_pending(build);
143
2.15k
  build = isl_ast_build_replace_pending_by_guard(build, pending);
144
2.15k
145
2.15k
  guard = isl_map_domain(isl_map_copy(map));
146
2.15k
  guard = isl_set_compute_divs(guard);
147
2.15k
  guard = isl_set_coalesce(guard);
148
2.15k
  guard = isl_set_gist(guard, isl_ast_build_get_generated(build));
149
2.15k
  guard = isl_ast_build_specialize(build, guard);
150
2.15k
151
2.15k
  graft = isl_ast_graft_alloc_domain(map, build);
152
2.15k
  graft = at_each_domain(graft, executed, build);
153
2.15k
  isl_ast_build_free(build);
154
2.15k
  isl_map_free(executed);
155
2.15k
  graft = isl_ast_graft_add_guard(graft, guard, data->build);
156
2.15k
157
2.15k
  list = isl_ast_graft_list_from_ast_graft(graft);
158
2.15k
  data->list = isl_ast_graft_list_concat(data->list, list);
159
2.15k
160
2.15k
  return isl_stat_ok;
161
2.15k
}
162
163
/* Generate an AST for a single domain based on
164
 * the inverse schedule "executed" and add it to data->list.
165
 *
166
 * If there is more than one domain element associated to the current
167
 * schedule "time", then we need to continue the generation process
168
 * in generate_non_single_valued.
169
 * Note that the inverse schedule being single-valued may depend
170
 * on constraints that are only available in the original context
171
 * domain specified by the user.  We therefore first introduce
172
 * some of the constraints of data->build->domain.  In particular,
173
 * we intersect with a single-disjunct approximation of this set.
174
 * We perform this approximation to avoid further splitting up
175
 * the executed relation, possibly introducing a disjunctive guard
176
 * on the statement.
177
 *
178
 * On the other hand, we only perform the test after having taken the gist
179
 * of the domain as the resulting map is the one from which the call
180
 * expression is constructed.  Using this map to construct the call
181
 * expression usually yields simpler results in cases where the original
182
 * map is not obviously single-valued.
183
 * If the original map is obviously single-valued, then the gist
184
 * operation is skipped.
185
 *
186
 * Because we perform the single-valuedness test on the gisted map,
187
 * we may in rare cases fail to recognize that the inverse schedule
188
 * is single-valued.  This becomes problematic if this happens
189
 * from the recursive call through generate_non_single_valued
190
 * as we would then end up in an infinite recursion.
191
 * We therefore check if we are inside a call to generate_non_single_valued
192
 * and revert to the ungisted map if the gisted map turns out not to be
193
 * single-valued.
194
 *
195
 * Otherwise, call add_domain to generate a call expression (with guard) and
196
 * to call the at_each_domain callback, if any.
197
 */
198
static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
199
2.17k
{
200
2.17k
  struct isl_generate_domain_data *data = user;
201
2.17k
  isl_set *domain;
202
2.17k
  isl_map *map = NULL;
203
2.17k
  int empty, sv;
204
2.17k
205
2.17k
  domain = isl_ast_build_get_domain(data->build);
206
2.17k
  domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
207
2.17k
  executed = isl_map_intersect_domain(executed, domain);
208
2.17k
  empty = isl_map_is_empty(executed);
209
2.17k
  if (empty < 0)
210
0
    goto error;
211
2.17k
  if (empty) {
212
0
    isl_map_free(executed);
213
0
    return isl_stat_ok;
214
0
  }
215
2.17k
216
2.17k
  sv = isl_map_plain_is_single_valued(executed);
217
2.17k
  if (sv < 0)
218
0
    goto error;
219
2.17k
  if (sv)
220
2.12k
    return add_domain(executed, isl_map_copy(executed), data);
221
49
222
49
  executed = isl_map_coalesce(executed);
223
49
  map = isl_map_copy(executed);
224
49
  map = isl_ast_build_compute_gist_map_domain(data->build, map);
225
49
  sv = isl_map_is_single_valued(map);
226
49
  if (sv < 0)
227
0
    goto error;
228
49
  if (!sv) {
229
22
    isl_map_free(map);
230
22
    if (data->build->single_valued)
231
0
      map = isl_map_copy(executed);
232
22
    else
233
22
      return generate_non_single_valued(executed, data);
234
27
  }
235
27
236
27
  return add_domain(executed, map, data);
237
0
error:
238
0
  isl_map_free(map);
239
0
  isl_map_free(executed);
240
0
  return isl_stat_error;
241
27
}
242
243
/* Call build->create_leaf to a create "leaf" node in the AST,
244
 * encapsulate the result in an isl_ast_graft and return the result
245
 * as a 1-element list.
246
 *
247
 * Note that the node returned by the user may be an entire tree.
248
 *
249
 * Since the node itself cannot enforce any constraints, we turn
250
 * all pending constraints into guards and add them to the resulting
251
 * graft to ensure that they will be generated.
252
 *
253
 * Before we pass control to the user, we first clear some information
254
 * from the build that is (presumbably) only meaningful
255
 * for the current code generation.
256
 * This includes the create_leaf callback itself, so we make a copy
257
 * of the build first.
258
 */
259
static __isl_give isl_ast_graft_list *call_create_leaf(
260
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
261
3
{
262
3
  isl_set *guard;
263
3
  isl_ast_node *node;
264
3
  isl_ast_graft *graft;
265
3
  isl_ast_build *user_build;
266
3
267
3
  guard = isl_ast_build_get_pending(build);
268
3
  user_build = isl_ast_build_copy(build);
269
3
  user_build = isl_ast_build_replace_pending_by_guard(user_build,
270
3
              isl_set_copy(guard));
271
3
  user_build = isl_ast_build_set_executed(user_build, executed);
272
3
  user_build = isl_ast_build_clear_local_info(user_build);
273
3
  if (!user_build)
274
0
    node = NULL;
275
3
  else
276
3
    node = build->create_leaf(user_build, build->create_leaf_user);
277
3
  graft = isl_ast_graft_alloc(node, build);
278
3
  graft = isl_ast_graft_add_guard(graft, guard, build);
279
3
  isl_ast_build_free(build);
280
3
  return isl_ast_graft_list_from_ast_graft(graft);
281
3
}
282
283
static __isl_give isl_ast_graft_list *build_ast_from_child(
284
  __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
285
  __isl_take isl_union_map *executed);
286
287
/* Generate an AST after having handled the complete schedule
288
 * of this call to the code generator or the complete band
289
 * if we are generating an AST from a schedule tree.
290
 *
291
 * If we are inside a band node, then move on to the child of the band.
292
 *
293
 * If the user has specified a create_leaf callback, control
294
 * is passed to the user in call_create_leaf.
295
 *
296
 * Otherwise, we generate one or more calls for each individual
297
 * domain in generate_domain.
298
 */
299
static __isl_give isl_ast_graft_list *generate_inner_level(
300
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
301
4.25k
{
302
4.25k
  isl_ctx *ctx;
303
4.25k
  struct isl_generate_domain_data data = { build };
304
4.25k
305
4.25k
  if (!build || !executed)
306
0
    goto error;
307
4.25k
308
4.25k
  if (isl_ast_build_has_schedule_node(build)) {
309
2.07k
    isl_schedule_node *node;
310
2.07k
    node = isl_ast_build_get_schedule_node(build);
311
2.07k
    build = isl_ast_build_reset_schedule_node(build);
312
2.07k
    return build_ast_from_child(build, node, executed);
313
2.07k
  }
314
2.17k
315
2.17k
  if (build->create_leaf)
316
3
    return call_create_leaf(executed, build);
317
2.17k
318
2.17k
  ctx = isl_union_map_get_ctx(executed);
319
2.17k
  data.list = isl_ast_graft_list_alloc(ctx, 0);
320
2.17k
  if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
321
0
    data.list = isl_ast_graft_list_free(data.list);
322
2.17k
323
2.17k
  if (0)
324
0
error:    data.list = NULL;
325
2.17k
  isl_ast_build_free(build);
326
2.17k
  isl_union_map_free(executed);
327
2.17k
  return data.list;
328
2.17k
}
329
330
/* Call the before_each_for callback, if requested by the user.
331
 */
332
static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
333
  __isl_keep isl_ast_build *build)
334
701
{
335
701
  isl_id *id;
336
701
337
701
  if (!node || !build)
338
0
    return isl_ast_node_free(node);
339
701
  if (!build->before_each_for)
340
468
    return node;
341
233
  id = build->before_each_for(build, build->before_each_for_user);
342
233
  node = isl_ast_node_set_annotation(node, id);
343
233
  return node;
344
233
}
345
346
/* Call the after_each_for callback, if requested by the user.
347
 */
348
static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
349
  __isl_keep isl_ast_build *build)
350
701
{
351
701
  if (!graft || !build)
352
0
    return isl_ast_graft_free(graft);
353
701
  if (!build->after_each_for)
354
468
    return graft;
355
233
  graft->node = build->after_each_for(graft->node, build,
356
233
            build->after_each_for_user);
357
233
  if (!graft->node)
358
0
    return isl_ast_graft_free(graft);
359
233
  return graft;
360
233
}
361
362
/* Plug in all the know values of the current and outer dimensions
363
 * in the domain of "executed".  In principle, we only need to plug
364
 * in the known value of the current dimension since the values of
365
 * outer dimensions have been plugged in already.
366
 * However, it turns out to be easier to just plug in all known values.
367
 */
368
static __isl_give isl_union_map *plug_in_values(
369
  __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
370
3.16k
{
371
3.16k
  return isl_ast_build_substitute_values_union_map_domain(build,
372
3.16k
                    executed);
373
3.16k
}
374
375
/* Check if the constraint "c" is a lower bound on dimension "pos",
376
 * an upper bound, or independent of dimension "pos".
377
 */
378
static int constraint_type(isl_constraint *c, int pos)
379
1.85k
{
380
1.85k
  if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
381
809
    return 1;
382
1.04k
  if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
383
934
    return 2;
384
111
  return 0;
385
111
}
386
387
/* Compare the types of the constraints "a" and "b",
388
 * resulting in constraints that are independent of "depth"
389
 * to be sorted before the lower bounds on "depth", which in
390
 * turn are sorted before the upper bounds on "depth".
391
 */
392
static int cmp_constraint(__isl_keep isl_constraint *a,
393
  __isl_keep isl_constraint *b, void *user)
394
927
{
395
927
  int *depth = user;
396
927
  int t1 = constraint_type(a, *depth);
397
927
  int t2 = constraint_type(b, *depth);
398
927
399
927
  return t1 - t2;
400
927
}
401
402
/* Extract a lower bound on dimension "pos" from constraint "c".
403
 *
404
 * If the constraint is of the form
405
 *
406
 *  a x + f(...) >= 0
407
 *
408
 * then we essentially return
409
 *
410
 *  l = ceil(-f(...)/a)
411
 *
412
 * However, if the current dimension is strided, then we need to make
413
 * sure that the lower bound we construct is of the form
414
 *
415
 *  f + s a
416
 *
417
 * with f the offset and s the stride.
418
 * We therefore compute
419
 *
420
 *  f + s * ceil((l - f)/s)
421
 */
422
static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
423
  int pos, __isl_keep isl_ast_build *build)
424
708
{
425
708
  isl_aff *aff;
426
708
427
708
  aff = isl_constraint_get_bound(c, isl_dim_set, pos);
428
708
  aff = isl_aff_ceil(aff);
429
708
430
708
  if (isl_ast_build_has_stride(build, pos)) {
431
11
    isl_aff *offset;
432
11
    isl_val *stride;
433
11
434
11
    offset = isl_ast_build_get_offset(build, pos);
435
11
    stride = isl_ast_build_get_stride(build, pos);
436
11
437
11
    aff = isl_aff_sub(aff, isl_aff_copy(offset));
438
11
    aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
439
11
    aff = isl_aff_ceil(aff);
440
11
    aff = isl_aff_scale_val(aff, stride);
441
11
    aff = isl_aff_add(aff, offset);
442
11
  }
443
708
444
708
  aff = isl_ast_build_compute_gist_aff(build, aff);
445
708
446
708
  return aff;
447
708
}
448
449
/* Return the exact lower bound (or upper bound if "upper" is set)
450
 * of "domain" as a piecewise affine expression.
451
 *
452
 * If we are computing a lower bound (of a strided dimension), then
453
 * we need to make sure it is of the form
454
 *
455
 *  f + s a
456
 *
457
 * where f is the offset and s is the stride.
458
 * We therefore need to include the stride constraint before computing
459
 * the minimum.
460
 */
461
static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
462
  __isl_keep isl_ast_build *build, int upper)
463
0
{
464
0
  isl_set *stride;
465
0
  isl_map *it_map;
466
0
  isl_pw_aff *pa;
467
0
  isl_pw_multi_aff *pma;
468
0
469
0
  domain = isl_set_copy(domain);
470
0
  if (!upper) {
471
0
    stride = isl_ast_build_get_stride_constraint(build);
472
0
    domain = isl_set_intersect(domain, stride);
473
0
  }
474
0
  it_map = isl_ast_build_map_to_iterator(build, domain);
475
0
  if (upper)
476
0
    pma = isl_map_lexmax_pw_multi_aff(it_map);
477
0
  else
478
0
    pma = isl_map_lexmin_pw_multi_aff(it_map);
479
0
  pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
480
0
  isl_pw_multi_aff_free(pma);
481
0
  pa = isl_ast_build_compute_gist_pw_aff(build, pa);
482
0
  pa = isl_pw_aff_coalesce(pa);
483
0
484
0
  return pa;
485
0
}
486
487
/* Callback for sorting the isl_pw_aff_list passed to reduce_list and
488
 * remove_redundant_lower_bounds.
489
 */
490
static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
491
  void *user)
492
75
{
493
75
  return isl_pw_aff_plain_cmp(a, b);
494
75
}
495
496
/* Given a list of lower bounds "list", remove those that are redundant
497
 * with respect to the other bounds in "list" and the domain of "build".
498
 *
499
 * We first sort the bounds in the same way as they would be sorted
500
 * by set_for_node_expressions so that we can try and remove the last
501
 * bounds first.
502
 *
503
 * For a lower bound to be effective, there needs to be at least
504
 * one domain element for which it is larger than all other lower bounds.
505
 * For each lower bound we therefore intersect the domain with
506
 * the conditions that it is larger than all other bounds and
507
 * check whether the result is empty.  If so, the bound can be removed.
508
 */
509
static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
510
  __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
511
11
{
512
11
  int i, j, n;
513
11
  isl_set *domain;
514
11
515
11
  list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
516
11
  if (!list)
517
0
    return NULL;
518
11
519
11
  n = isl_pw_aff_list_n_pw_aff(list);
520
11
  if (n <= 1)
521
11
    return list;
522
0
523
0
  domain = isl_ast_build_get_domain(build);
524
0
525
0
  for (i = n - 1; i >= 0; --i) {
526
0
    isl_pw_aff *pa_i;
527
0
    isl_set *domain_i;
528
0
    int empty;
529
0
530
0
    domain_i = isl_set_copy(domain);
531
0
    pa_i = isl_pw_aff_list_get_pw_aff(list, i);
532
0
533
0
    for (j = 0; j < n; ++j) {
534
0
      isl_pw_aff *pa_j;
535
0
      isl_set *better;
536
0
537
0
      if (j == i)
538
0
        continue;
539
0
540
0
      pa_j = isl_pw_aff_list_get_pw_aff(list, j);
541
0
      better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
542
0
      domain_i = isl_set_intersect(domain_i, better);
543
0
    }
544
0
545
0
    empty = isl_set_is_empty(domain_i);
546
0
547
0
    isl_set_free(domain_i);
548
0
    isl_pw_aff_free(pa_i);
549
0
550
0
    if (empty < 0)
551
0
      goto error;
552
0
    if (!empty)
553
0
      continue;
554
0
    list = isl_pw_aff_list_drop(list, i, 1);
555
0
    n--;
556
0
  }
557
0
558
0
  isl_set_free(domain);
559
0
560
0
  return list;
561
0
error:
562
0
  isl_set_free(domain);
563
0
  return isl_pw_aff_list_free(list);
564
0
}
565
566
/* Extract a lower bound on dimension "pos" from each constraint
567
 * in "constraints" and return the list of lower bounds.
568
 * If "constraints" has zero elements, then we extract a lower bound
569
 * from "domain" instead.
570
 *
571
 * If the current dimension is strided, then the lower bound
572
 * is adjusted by lower_bound to match the stride information.
573
 * This modification may make one or more lower bounds redundant
574
 * with respect to the other lower bounds.  We therefore check
575
 * for this condition and remove the redundant lower bounds.
576
 */
577
static __isl_give isl_pw_aff_list *lower_bounds(
578
  __isl_keep isl_constraint_list *constraints, int pos,
579
  __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
580
701
{
581
701
  isl_ctx *ctx;
582
701
  isl_pw_aff_list *list;
583
701
  int i, n;
584
701
585
701
  if (!build)
586
0
    return NULL;
587
701
588
701
  n = isl_constraint_list_n_constraint(constraints);
589
701
  if (n == 0) {
590
0
    isl_pw_aff *pa;
591
0
    pa = exact_bound(domain, build, 0);
592
0
    return isl_pw_aff_list_from_pw_aff(pa);
593
0
  }
594
701
595
701
  ctx = isl_ast_build_get_ctx(build);
596
701
  list = isl_pw_aff_list_alloc(ctx,n);
597
701
598
1.40k
  for (i = 0; i < n; 
++i708
) {
599
708
    isl_aff *aff;
600
708
    isl_constraint *c;
601
708
602
708
    c = isl_constraint_list_get_constraint(constraints, i);
603
708
    aff = lower_bound(c, pos, build);
604
708
    isl_constraint_free(c);
605
708
    list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
606
708
  }
607
701
608
701
  if (isl_ast_build_has_stride(build, pos))
609
11
    list = remove_redundant_lower_bounds(list, build);
610
701
611
701
  return list;
612
701
}
613
614
/* Extract an upper bound on dimension "pos" from each constraint
615
 * in "constraints" and return the list of upper bounds.
616
 * If "constraints" has zero elements, then we extract an upper bound
617
 * from "domain" instead.
618
 */
619
static __isl_give isl_pw_aff_list *upper_bounds(
620
  __isl_keep isl_constraint_list *constraints, int pos,
621
  __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
622
701
{
623
701
  isl_ctx *ctx;
624
701
  isl_pw_aff_list *list;
625
701
  int i, n;
626
701
627
701
  n = isl_constraint_list_n_constraint(constraints);
628
701
  if (n == 0) {
629
0
    isl_pw_aff *pa;
630
0
    pa = exact_bound(domain, build, 1);
631
0
    return isl_pw_aff_list_from_pw_aff(pa);
632
0
  }
633
701
634
701
  ctx = isl_ast_build_get_ctx(build);
635
701
  list = isl_pw_aff_list_alloc(ctx,n);
636
701
637
1.46k
  for (i = 0; i < n; 
++i767
) {
638
767
    isl_aff *aff;
639
767
    isl_constraint *c;
640
767
641
767
    c = isl_constraint_list_get_constraint(constraints, i);
642
767
    aff = isl_constraint_get_bound(c, isl_dim_set, pos);
643
767
    isl_constraint_free(c);
644
767
    aff = isl_aff_floor(aff);
645
767
    list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
646
767
  }
647
701
648
701
  return list;
649
701
}
650
651
/* Return an isl_ast_expr that performs the reduction of type "type"
652
 * on AST expressions corresponding to the elements in "list".
653
 *
654
 * The list is assumed to contain at least one element.
655
 * If the list contains exactly one element, then the returned isl_ast_expr
656
 * simply computes that affine expression.
657
 * If the list contains more than one element, then we sort it
658
 * using a fairly abitrary but hopefully reasonably stable order.
659
 */
660
static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
661
  __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
662
1.40k
{
663
1.40k
  int i, n;
664
1.40k
  isl_ctx *ctx;
665
1.40k
  isl_ast_expr *expr;
666
1.40k
667
1.40k
  if (!list)
668
0
    return NULL;
669
1.40k
670
1.40k
  n = isl_pw_aff_list_n_pw_aff(list);
671
1.40k
672
1.40k
  if (n == 1)
673
1.33k
    return isl_ast_build_expr_from_pw_aff_internal(build,
674
1.33k
        isl_pw_aff_list_get_pw_aff(list, 0));
675
70
676
70
  ctx = isl_pw_aff_list_get_ctx(list);
677
70
  expr = isl_ast_expr_alloc_op(ctx, type, n);
678
70
  if (!expr)
679
0
    return NULL;
680
70
681
70
  list = isl_pw_aff_list_copy(list);
682
70
  list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
683
70
  if (!list)
684
0
    return isl_ast_expr_free(expr);
685
70
686
213
  
for (i = 0; 70
i < n;
++i143
) {
687
143
    isl_ast_expr *expr_i;
688
143
689
143
    expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
690
143
        isl_pw_aff_list_get_pw_aff(list, i));
691
143
    if (!expr_i)
692
0
      goto error;
693
143
    expr->u.op.args[i] = expr_i;
694
143
  }
695
70
696
70
  isl_pw_aff_list_free(list);
697
70
  return expr;
698
0
error:
699
0
  isl_pw_aff_list_free(list);
700
0
  isl_ast_expr_free(expr);
701
0
  return NULL;
702
70
}
703
704
/* Add guards implied by the "generated constraints",
705
 * but not (necessarily) enforced by the generated AST to "guard".
706
 * In particular, if there is any stride constraints,
707
 * then add the guard implied by those constraints.
708
 * If we have generated a degenerate loop, then add the guard
709
 * implied by "bounds" on the outer dimensions, i.e., the guard
710
 * that ensures that the single value actually exists.
711
 * Since there may also be guards implied by a combination
712
 * of these constraints, we first combine them before
713
 * deriving the implied constraints.
714
 */
715
static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
716
  int degenerate, __isl_keep isl_basic_set *bounds,
717
  __isl_keep isl_ast_build *build)
718
701
{
719
701
  int depth, has_stride;
720
701
  isl_space *space;
721
701
  isl_set *dom, *set;
722
701
723
701
  depth = isl_ast_build_get_depth(build);
724
701
  has_stride = isl_ast_build_has_stride(build, depth);
725
701
  if (!has_stride && 
!degenerate690
)
726
690
    return guard;
727
11
728
11
  space = isl_basic_set_get_space(bounds);
729
11
  dom = isl_set_universe(space);
730
11
731
11
  if (degenerate) {
732
0
    bounds = isl_basic_set_copy(bounds);
733
0
    bounds = isl_basic_set_drop_constraints_not_involving_dims(
734
0
          bounds, isl_dim_set, depth, 1);
735
0
    set = isl_set_from_basic_set(bounds);
736
0
    dom = isl_set_intersect(dom, set);
737
0
  }
738
11
739
11
  if (has_stride) {
740
11
    set = isl_ast_build_get_stride_constraint(build);
741
11
    dom = isl_set_intersect(dom, set);
742
11
  }
743
11
744
11
  dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
745
11
  dom = isl_ast_build_compute_gist(build, dom);
746
11
  guard = isl_set_intersect(guard, dom);
747
11
748
11
  return guard;
749
11
}
750
751
/* Update "graft" based on "sub_build" for the degenerate case.
752
 *
753
 * "build" is the build in which graft->node was created
754
 * "sub_build" contains information about the current level itself,
755
 * including the single value attained.
756
 *
757
 * We set the initialization part of the for loop to the single
758
 * value attained by the current dimension.
759
 * The increment and condition are not strictly needed as the are known
760
 * to be "1" and "iterator <= value" respectively.
761
 */
762
static __isl_give isl_ast_graft *refine_degenerate(
763
  __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
764
  __isl_keep isl_ast_build *sub_build)
765
0
{
766
0
  isl_pw_aff *value;
767
0
768
0
  if (!graft || !sub_build)
769
0
    return isl_ast_graft_free(graft);
770
0
771
0
  value = isl_pw_aff_copy(sub_build->value);
772
0
773
0
  graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
774
0
            value);
775
0
  if (!graft->node->u.f.init)
776
0
    return isl_ast_graft_free(graft);
777
0
778
0
  return graft;
779
0
}
780
781
/* Return the intersection of constraints in "list" as a set.
782
 */
783
static __isl_give isl_set *intersect_constraints(
784
  __isl_keep isl_constraint_list *list)
785
0
{
786
0
  int i, n;
787
0
  isl_basic_set *bset;
788
0
789
0
  n = isl_constraint_list_n_constraint(list);
790
0
  if (n < 1)
791
0
    isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
792
0
      "expecting at least one constraint", return NULL);
793
0
794
0
  bset = isl_basic_set_from_constraint(
795
0
        isl_constraint_list_get_constraint(list, 0));
796
0
  for (i = 1; i < n; ++i) {
797
0
    isl_basic_set *bset_i;
798
0
799
0
    bset_i = isl_basic_set_from_constraint(
800
0
        isl_constraint_list_get_constraint(list, i));
801
0
    bset = isl_basic_set_intersect(bset, bset_i);
802
0
  }
803
0
804
0
  return isl_set_from_basic_set(bset);
805
0
}
806
807
/* Compute the constraints on the outer dimensions enforced by
808
 * graft->node and add those constraints to graft->enforced,
809
 * in case the upper bound is expressed as a set "upper".
810
 *
811
 * In particular, if l(...) is a lower bound in "lower", and
812
 *
813
 *  -a i + f(...) >= 0    or  a i <= f(...)
814
 *
815
 * is an upper bound ocnstraint on the current dimension i,
816
 * then the for loop enforces the constraint
817
 *
818
 *  -a l(...) + f(...) >= 0   or  a l(...) <= f(...)
819
 *
820
 * We therefore simply take each lower bound in turn, plug it into
821
 * the upper bounds and compute the intersection over all lower bounds.
822
 *
823
 * If a lower bound is a rational expression, then
824
 * isl_basic_set_preimage_multi_aff will force this rational
825
 * expression to have only integer values.  However, the loop
826
 * itself does not enforce this integrality constraint.  We therefore
827
 * use the ceil of the lower bounds instead of the lower bounds themselves.
828
 * Other constraints will make sure that the for loop is only executed
829
 * when each of the lower bounds attains an integral value.
830
 * In particular, potentially rational values only occur in
831
 * lower_bound if the offset is a (seemingly) rational expression,
832
 * but then outer conditions will make sure that this rational expression
833
 * only attains integer values.
834
 */
835
static __isl_give isl_ast_graft *set_enforced_from_set(
836
  __isl_take isl_ast_graft *graft,
837
  __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
838
0
{
839
0
  isl_space *space;
840
0
  isl_basic_set *enforced;
841
0
  isl_pw_multi_aff *pma;
842
0
  int i, n;
843
0
844
0
  if (!graft || !lower)
845
0
    return isl_ast_graft_free(graft);
846
0
847
0
  space = isl_set_get_space(upper);
848
0
  enforced = isl_basic_set_universe(isl_space_copy(space));
849
0
850
0
  space = isl_space_map_from_set(space);
851
0
  pma = isl_pw_multi_aff_identity(space);
852
0
853
0
  n = isl_pw_aff_list_n_pw_aff(lower);
854
0
  for (i = 0; i < n; ++i) {
855
0
    isl_pw_aff *pa;
856
0
    isl_set *enforced_i;
857
0
    isl_basic_set *hull;
858
0
    isl_pw_multi_aff *pma_i;
859
0
860
0
    pa = isl_pw_aff_list_get_pw_aff(lower, i);
861
0
    pa = isl_pw_aff_ceil(pa);
862
0
    pma_i = isl_pw_multi_aff_copy(pma);
863
0
    pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
864
0
    enforced_i = isl_set_copy(upper);
865
0
    enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
866
0
    hull = isl_set_simple_hull(enforced_i);
867
0
    enforced = isl_basic_set_intersect(enforced, hull);
868
0
  }
869
0
870
0
  isl_pw_multi_aff_free(pma);
871
0
872
0
  graft = isl_ast_graft_enforce(graft, enforced);
873
0
874
0
  return graft;
875
0
}
876
877
/* Compute the constraints on the outer dimensions enforced by
878
 * graft->node and add those constraints to graft->enforced,
879
 * in case the upper bound is expressed as
880
 * a list of affine expressions "upper".
881
 *
882
 * The enforced condition is that each lower bound expression is less
883
 * than or equal to each upper bound expression.
884
 */
885
static __isl_give isl_ast_graft *set_enforced_from_list(
886
  __isl_take isl_ast_graft *graft,
887
  __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
888
701
{
889
701
  isl_set *cond;
890
701
  isl_basic_set *enforced;
891
701
892
701
  lower = isl_pw_aff_list_copy(lower);
893
701
  upper = isl_pw_aff_list_copy(upper);
894
701
  cond = isl_pw_aff_list_le_set(lower, upper);
895
701
  enforced = isl_set_simple_hull(cond);
896
701
  graft = isl_ast_graft_enforce(graft, enforced);
897
701
898
701
  return graft;
899
701
}
900
901
/* Does "aff" have a negative constant term?
902
 */
903
static isl_stat aff_constant_is_negative(__isl_take isl_set *set,
904
  __isl_take isl_aff *aff, void *user)
905
710
{
906
710
  int *neg = user;
907
710
  isl_val *v;
908
710
909
710
  v = isl_aff_get_constant_val(aff);
910
710
  *neg = isl_val_is_neg(v);
911
710
  isl_val_free(v);
912
710
  isl_set_free(set);
913
710
  isl_aff_free(aff);
914
710
915
710
  return *neg ? 
isl_stat_ok176
:
isl_stat_error534
;
916
710
}
917
918
/* Does "pa" have a negative constant term over its entire domain?
919
 */
920
static isl_stat pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa,
921
  void *user)
922
710
{
923
710
  isl_stat r;
924
710
  int *neg = user;
925
710
926
710
  r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
927
710
  isl_pw_aff_free(pa);
928
710
929
710
  return (*neg && 
r >= 0176
) ?
isl_stat_ok176
:
isl_stat_error534
;
930
710
}
931
932
/* Does each element in "list" have a negative constant term?
933
 *
934
 * The callback terminates the iteration as soon an element has been
935
 * found that does not have a negative constant term.
936
 */
937
static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
938
701
{
939
701
  int neg = 1;
940
701
941
701
  if (isl_pw_aff_list_foreach(list,
942
701
        &pw_aff_constant_is_negative, &neg) < 0 && 
neg534
)
943
0
    return -1;
944
701
945
701
  return neg;
946
701
}
947
948
/* Add 1 to each of the elements in "list", where each of these elements
949
 * is defined over the internal schedule space of "build".
950
 */
951
static __isl_give isl_pw_aff_list *list_add_one(
952
  __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
953
167
{
954
167
  int i, n;
955
167
  isl_space *space;
956
167
  isl_aff *aff;
957
167
  isl_pw_aff *one;
958
167
959
167
  space = isl_ast_build_get_space(build, 1);
960
167
  aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
961
167
  aff = isl_aff_add_constant_si(aff, 1);
962
167
  one = isl_pw_aff_from_aff(aff);
963
167
964
167
  n = isl_pw_aff_list_n_pw_aff(list);
965
334
  for (i = 0; i < n; 
++i167
) {
966
167
    isl_pw_aff *pa;
967
167
    pa = isl_pw_aff_list_get_pw_aff(list, i);
968
167
    pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
969
167
    list = isl_pw_aff_list_set_pw_aff(list, i, pa);
970
167
  }
971
167
972
167
  isl_pw_aff_free(one);
973
167
974
167
  return list;
975
167
}
976
977
/* Set the condition part of the for node graft->node in case
978
 * the upper bound is represented as a list of piecewise affine expressions.
979
 *
980
 * In particular, set the condition to
981
 *
982
 *  iterator <= min(list of upper bounds)
983
 *
984
 * If each of the upper bounds has a negative constant term, then
985
 * set the condition to
986
 *
987
 *  iterator < min(list of (upper bound + 1)s)
988
 *
989
 */
990
static __isl_give isl_ast_graft *set_for_cond_from_list(
991
  __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
992
  __isl_keep isl_ast_build *build)
993
701
{
994
701
  int neg;
995
701
  isl_ast_expr *bound, *iterator, *cond;
996
701
  enum isl_ast_op_type type = isl_ast_op_le;
997
701
998
701
  if (!graft || !list)
999
0
    return isl_ast_graft_free(graft);
1000
701
1001
701
  neg = list_constant_is_negative(list);
1002
701
  if (neg < 0)
1003
0
    return isl_ast_graft_free(graft);
1004
701
  list = isl_pw_aff_list_copy(list);
1005
701
  if (neg) {
1006
167
    list = list_add_one(list, build);
1007
167
    type = isl_ast_op_lt;
1008
167
  }
1009
701
1010
701
  bound = reduce_list(isl_ast_op_min, list, build);
1011
701
  iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
1012
701
  cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1013
701
  graft->node->u.f.cond = cond;
1014
701
1015
701
  isl_pw_aff_list_free(list);
1016
701
  if (!graft->node->u.f.cond)
1017
0
    return isl_ast_graft_free(graft);
1018
701
  return graft;
1019
701
}
1020
1021
/* Set the condition part of the for node graft->node in case
1022
 * the upper bound is represented as a set.
1023
 */
1024
static __isl_give isl_ast_graft *set_for_cond_from_set(
1025
  __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1026
  __isl_keep isl_ast_build *build)
1027
0
{
1028
0
  isl_ast_expr *cond;
1029
0
1030
0
  if (!graft)
1031
0
    return NULL;
1032
0
1033
0
  cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1034
0
  graft->node->u.f.cond = cond;
1035
0
  if (!graft->node->u.f.cond)
1036
0
    return isl_ast_graft_free(graft);
1037
0
  return graft;
1038
0
}
1039
1040
/* Construct an isl_ast_expr for the increment (i.e., stride) of
1041
 * the current dimension.
1042
 */
1043
static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1044
701
{
1045
701
  int depth;
1046
701
  isl_val *v;
1047
701
  isl_ctx *ctx;
1048
701
1049
701
  if (!build)
1050
0
    return NULL;
1051
701
  ctx = isl_ast_build_get_ctx(build);
1052
701
  depth = isl_ast_build_get_depth(build);
1053
701
1054
701
  if (!isl_ast_build_has_stride(build, depth))
1055
690
    return isl_ast_expr_alloc_int_si(ctx, 1);
1056
11
1057
11
  v = isl_ast_build_get_stride(build, depth);
1058
11
  return isl_ast_expr_from_val(v);
1059
11
}
1060
1061
/* Should we express the loop condition as
1062
 *
1063
 *  iterator <= min(list of upper bounds)
1064
 *
1065
 * or as a conjunction of constraints?
1066
 *
1067
 * The first is constructed from a list of upper bounds.
1068
 * The second is constructed from a set.
1069
 *
1070
 * If there are no upper bounds in "constraints", then this could mean
1071
 * that "domain" simply doesn't have an upper bound or that we didn't
1072
 * pick any upper bound.  In the first case, we want to generate the
1073
 * loop condition as a(n empty) conjunction of constraints
1074
 * In the second case, we will compute
1075
 * a single upper bound from "domain" and so we use the list form.
1076
 *
1077
 * If there are upper bounds in "constraints",
1078
 * then we use the list form iff the atomic_upper_bound option is set.
1079
 */
1080
static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1081
  __isl_keep isl_set *domain, int depth)
1082
701
{
1083
701
  if (n_upper > 0)
1084
701
    return isl_options_get_ast_build_atomic_upper_bound(ctx);
1085
0
  else
1086
0
    return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1087
701
}
1088
1089
/* Fill in the expressions of the for node in graft->node.
1090
 *
1091
 * In particular,
1092
 * - set the initialization part of the loop to the maximum of the lower bounds
1093
 * - extract the increment from the stride of the current dimension
1094
 * - construct the for condition either based on a list of upper bounds
1095
 *  or on a set of upper bound constraints.
1096
 */
1097
static __isl_give isl_ast_graft *set_for_node_expressions(
1098
  __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1099
  int use_list, __isl_keep isl_pw_aff_list *upper_list,
1100
  __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1101
701
{
1102
701
  isl_ast_node *node;
1103
701
1104
701
  if (!graft)
1105
0
    return NULL;
1106
701
1107
701
  build = isl_ast_build_copy(build);
1108
701
1109
701
  node = graft->node;
1110
701
  node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1111
701
  node->u.f.inc = for_inc(build);
1112
701
1113
701
  if (!node->u.f.init || !node->u.f.inc)
1114
0
    graft = isl_ast_graft_free(graft);
1115
701
1116
701
  if (use_list)
1117
701
    graft = set_for_cond_from_list(graft, upper_list, build);
1118
0
  else
1119
0
    graft = set_for_cond_from_set(graft, upper_set, build);
1120
701
1121
701
  isl_ast_build_free(build);
1122
701
1123
701
  return graft;
1124
701
}
1125
1126
/* Update "graft" based on "bounds" and "domain" for the generic,
1127
 * non-degenerate, case.
1128
 *
1129
 * "c_lower" and "c_upper" contain the lower and upper bounds
1130
 * that the loop node should express.
1131
 * "domain" is the subset of the intersection of the constraints
1132
 * for which some code is executed.
1133
 *
1134
 * There may be zero lower bounds or zero upper bounds in "constraints"
1135
 * in case the list of constraints was created
1136
 * based on the atomic option or based on separation with explicit bounds.
1137
 * In that case, we use "domain" to derive lower and/or upper bounds.
1138
 *
1139
 * We first compute a list of one or more lower bounds.
1140
 *
1141
 * Then we decide if we want to express the condition as
1142
 *
1143
 *  iterator <= min(list of upper bounds)
1144
 *
1145
 * or as a conjunction of constraints.
1146
 *
1147
 * The set of enforced constraints is then computed either based on
1148
 * a list of upper bounds or on a set of upper bound constraints.
1149
 * We do not compute any enforced constraints if we were forced
1150
 * to compute a lower or upper bound using exact_bound.  The domains
1151
 * of the resulting expressions may imply some bounds on outer dimensions
1152
 * that we do not want to appear in the enforced constraints since
1153
 * they are not actually enforced by the corresponding code.
1154
 *
1155
 * Finally, we fill in the expressions of the for node.
1156
 */
1157
static __isl_give isl_ast_graft *refine_generic_bounds(
1158
  __isl_take isl_ast_graft *graft,
1159
  __isl_take isl_constraint_list *c_lower,
1160
  __isl_take isl_constraint_list *c_upper,
1161
  __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1162
701
{
1163
701
  int depth;
1164
701
  isl_ctx *ctx;
1165
701
  isl_pw_aff_list *lower;
1166
701
  int use_list;
1167
701
  isl_set *upper_set = NULL;
1168
701
  isl_pw_aff_list *upper_list = NULL;
1169
701
  int n_lower, n_upper;
1170
701
1171
701
  if (!graft || !c_lower || !c_upper || !build)
1172
0
    goto error;
1173
701
1174
701
  depth = isl_ast_build_get_depth(build);
1175
701
  ctx = isl_ast_graft_get_ctx(graft);
1176
701
1177
701
  n_lower = isl_constraint_list_n_constraint(c_lower);
1178
701
  n_upper = isl_constraint_list_n_constraint(c_upper);
1179
701
1180
701
  use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1181
701
1182
701
  lower = lower_bounds(c_lower, depth, domain, build);
1183
701
1184
701
  if (use_list)
1185
701
    upper_list = upper_bounds(c_upper, depth, domain, build);
1186
0
  else if (n_upper > 0)
1187
0
    upper_set = intersect_constraints(c_upper);
1188
0
  else
1189
0
    upper_set = isl_set_universe(isl_set_get_space(domain));
1190
701
1191
701
  if (n_lower == 0 || n_upper == 0)
1192
0
    ;
1193
701
  else if (use_list)
1194
701
    graft = set_enforced_from_list(graft, lower, upper_list);
1195
0
  else
1196
0
    graft = set_enforced_from_set(graft, lower, depth, upper_set);
1197
701
1198
701
  graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1199
701
          upper_set, build);
1200
701
1201
701
  isl_pw_aff_list_free(lower);
1202
701
  isl_pw_aff_list_free(upper_list);
1203
701
  isl_set_free(upper_set);
1204
701
  isl_constraint_list_free(c_lower);
1205
701
  isl_constraint_list_free(c_upper);
1206
701
1207
701
  return graft;
1208
0
error:
1209
0
  isl_constraint_list_free(c_lower);
1210
0
  isl_constraint_list_free(c_upper);
1211
0
  return isl_ast_graft_free(graft);
1212
701
}
1213
1214
/* Internal data structure used inside count_constraints to keep
1215
 * track of the number of constraints that are independent of dimension "pos",
1216
 * the lower bounds in "pos" and the upper bounds in "pos".
1217
 */
1218
struct isl_ast_count_constraints_data {
1219
  int pos;
1220
1221
  int n_indep;
1222
  int n_lower;
1223
  int n_upper;
1224
};
1225
1226
/* Increment data->n_indep, data->lower or data->upper depending
1227
 * on whether "c" is independenct of dimensions data->pos,
1228
 * a lower bound or an upper bound.
1229
 */
1230
static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1231
1.52k
{
1232
1.52k
  struct isl_ast_count_constraints_data *data = user;
1233
1.52k
1234
1.52k
  if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1235
708
    data->n_lower++;
1236
820
  else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1237
767
    data->n_upper++;
1238
53
  else
1239
53
    data->n_indep++;
1240
1.52k
1241
1.52k
  isl_constraint_free(c);
1242
1.52k
1243
1.52k
  return isl_stat_ok;
1244
1.52k
}
1245
1246
/* Update "graft" based on "bounds" and "domain" for the generic,
1247
 * non-degenerate, case.
1248
 *
1249
 * "list" respresent the list of bounds that need to be encoded by
1250
 * the for loop.  Only the constraints that involve the iterator
1251
 * are relevant here.  The other constraints are taken care of by
1252
 * the caller and are included in the generated constraints of "build".
1253
 * "domain" is the subset of the intersection of the constraints
1254
 * for which some code is executed.
1255
 * "build" is the build in which graft->node was created.
1256
 *
1257
 * We separate lower bounds, upper bounds and constraints that
1258
 * are independent of the loop iterator.
1259
 *
1260
 * The actual for loop bounds are generated in refine_generic_bounds.
1261
 */
1262
static __isl_give isl_ast_graft *refine_generic_split(
1263
  __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1264
  __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1265
701
{
1266
701
  struct isl_ast_count_constraints_data data;
1267
701
  isl_constraint_list *lower;
1268
701
  isl_constraint_list *upper;
1269
701
1270
701
  if (!list)
1271
0
    return isl_ast_graft_free(graft);
1272
701
1273
701
  data.pos = isl_ast_build_get_depth(build);
1274
701
1275
701
  list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1276
701
  if (!list)
1277
0
    return isl_ast_graft_free(graft);
1278
701
1279
701
  data.n_indep = data.n_lower = data.n_upper = 0;
1280
701
  if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1281
0
    isl_constraint_list_free(list);
1282
0
    return isl_ast_graft_free(graft);
1283
0
  }
1284
701
1285
701
  lower = isl_constraint_list_drop(list, 0, data.n_indep);
1286
701
  upper = isl_constraint_list_copy(lower);
1287
701
  lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1288
701
  upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1289
701
1290
701
  return refine_generic_bounds(graft, lower, upper, domain, build);
1291
701
}
1292
1293
/* Update "graft" based on "bounds" and "domain" for the generic,
1294
 * non-degenerate, case.
1295
 *
1296
 * "bounds" respresent the bounds that need to be encoded by
1297
 * the for loop (or a guard around the for loop).
1298
 * "domain" is the subset of "bounds" for which some code is executed.
1299
 * "build" is the build in which graft->node was created.
1300
 *
1301
 * We break up "bounds" into a list of constraints and continue with
1302
 * refine_generic_split.
1303
 */
1304
static __isl_give isl_ast_graft *refine_generic(
1305
  __isl_take isl_ast_graft *graft,
1306
  __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1307
  __isl_keep isl_ast_build *build)
1308
701
{
1309
701
  isl_constraint_list *list;
1310
701
1311
701
  if (!build || !graft)
1312
0
    return isl_ast_graft_free(graft);
1313
701
1314
701
  list = isl_basic_set_get_constraint_list(bounds);
1315
701
1316
701
  graft = refine_generic_split(graft, list, domain, build);
1317
701
1318
701
  return graft;
1319
701
}
1320
1321
/* Create a for node for the current level.
1322
 *
1323
 * Mark the for node degenerate if "degenerate" is set.
1324
 */
1325
static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1326
  int degenerate)
1327
701
{
1328
701
  int depth;
1329
701
  isl_id *id;
1330
701
  isl_ast_node *node;
1331
701
1332
701
  if (!build)
1333
0
    return NULL;
1334
701
1335
701
  depth = isl_ast_build_get_depth(build);
1336
701
  id = isl_ast_build_get_iterator_id(build, depth);
1337
701
  node = isl_ast_node_alloc_for(id);
1338
701
  if (degenerate)
1339
0
    node = isl_ast_node_for_mark_degenerate(node);
1340
701
1341
701
  return node;
1342
701
}
1343
1344
/* If the ast_build_exploit_nested_bounds option is set, then return
1345
 * the constraints enforced by all elements in "list".
1346
 * Otherwise, return the universe.
1347
 */
1348
static __isl_give isl_basic_set *extract_shared_enforced(
1349
  __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1350
3.86k
{
1351
3.86k
  isl_ctx *ctx;
1352
3.86k
  isl_space *space;
1353
3.86k
1354
3.86k
  if (!list)
1355
0
    return NULL;
1356
3.86k
1357
3.86k
  ctx = isl_ast_graft_list_get_ctx(list);
1358
3.86k
  if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1359
3.86k
    return isl_ast_graft_list_extract_shared_enforced(list, build);
1360
2
1361
2
  space = isl_ast_build_get_space(build, 1);
1362
2
  return isl_basic_set_universe(space);
1363
2
}
1364
1365
/* Return the pending constraints of "build" that are not already taken
1366
 * care of (by a combination of "enforced" and the generated constraints
1367
 * of "build").
1368
 */
1369
static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1370
  __isl_keep isl_basic_set *enforced)
1371
3.86k
{
1372
3.86k
  isl_set *guard, *context;
1373
3.86k
1374
3.86k
  guard = isl_ast_build_get_pending(build);
1375
3.86k
  context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1376
3.86k
  context = isl_set_intersect(context,
1377
3.86k
          isl_ast_build_get_generated(build));
1378
3.86k
  return isl_set_gist(guard, context);
1379
3.86k
}
1380
1381
/* Create an AST node for the current dimension based on
1382
 * the schedule domain "bounds" and return the node encapsulated
1383
 * in an isl_ast_graft.
1384
 *
1385
 * "executed" is the current inverse schedule, taking into account
1386
 * the bounds in "bounds"
1387
 * "domain" is the domain of "executed", with inner dimensions projected out.
1388
 * It may be a strict subset of "bounds" in case "bounds" was created
1389
 * based on the atomic option or based on separation with explicit bounds.
1390
 *
1391
 * "domain" may satisfy additional equalities that result
1392
 * from intersecting "executed" with "bounds" in add_node.
1393
 * It may also satisfy some global constraints that were dropped out because
1394
 * we performed separation with explicit bounds.
1395
 * The very first step is then to copy these constraints to "bounds".
1396
 *
1397
 * Since we may be calling before_each_for and after_each_for
1398
 * callbacks, we record the current inverse schedule in the build.
1399
 *
1400
 * We consider three builds,
1401
 * "build" is the one in which the current level is created,
1402
 * "body_build" is the build in which the next level is created,
1403
 * "sub_build" is essentially the same as "body_build", except that
1404
 * the depth has not been increased yet.
1405
 *
1406
 * "build" already contains information (in strides and offsets)
1407
 * about the strides at the current level, but this information is not
1408
 * reflected in the build->domain.
1409
 * We first add this information and the "bounds" to the sub_build->domain.
1410
 * isl_ast_build_set_loop_bounds adds the stride information and
1411
 * checks whether the current dimension attains
1412
 * only a single value and whether this single value can be represented using
1413
 * a single affine expression.
1414
 * In the first case, the current level is considered "degenerate".
1415
 * In the second, sub-case, the current level is considered "eliminated".
1416
 * Eliminated levels don't need to be reflected in the AST since we can
1417
 * simply plug in the affine expression.  For degenerate, but non-eliminated,
1418
 * levels, we do introduce a for node, but mark is as degenerate so that
1419
 * it can be printed as an assignment of the single value to the loop
1420
 * "iterator".
1421
 *
1422
 * If the current level is eliminated, we explicitly plug in the value
1423
 * for the current level found by isl_ast_build_set_loop_bounds in the
1424
 * inverse schedule.  This ensures that if we are working on a slice
1425
 * of the domain based on information available in the inverse schedule
1426
 * and the build domain, that then this information is also reflected
1427
 * in the inverse schedule.  This operation also eliminates the current
1428
 * dimension from the inverse schedule making sure no inner dimensions depend
1429
 * on the current dimension.  Otherwise, we create a for node, marking
1430
 * it degenerate if appropriate.  The initial for node is still incomplete
1431
 * and will be completed in either refine_degenerate or refine_generic.
1432
 *
1433
 * We then generate a sequence of grafts for the next level,
1434
 * create a surrounding graft for the current level and insert
1435
 * the for node we created (if the current level is not eliminated).
1436
 * Before creating a graft for the current level, we first extract
1437
 * hoistable constraints from the child guards and combine them
1438
 * with the pending constraints in the build.  These constraints
1439
 * are used to simplify the child guards and then added to the guard
1440
 * of the current graft to ensure that they will be generated.
1441
 * If the hoisted guard is a disjunction, then we use it directly
1442
 * to gist the guards on the children before intersect it with the
1443
 * pending constraints.  We do so because this disjunction is typically
1444
 * identical to the guards on the children such that these guards
1445
 * can be effectively removed completely.  After the intersection,
1446
 * the gist operation would have a harder time figuring this out.
1447
 *
1448
 * Finally, we set the bounds of the for loop in either
1449
 * refine_degenerate or refine_generic.
1450
 * We do so in a context where the pending constraints of the build
1451
 * have been replaced by the guard of the current graft.
1452
 */
1453
static __isl_give isl_ast_graft *create_node_scaled(
1454
  __isl_take isl_union_map *executed,
1455
  __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1456
  __isl_take isl_ast_build *build)
1457
3.86k
{
1458
3.86k
  int depth;
1459
3.86k
  int degenerate;
1460
3.86k
  isl_bool eliminated;
1461
3.86k
  isl_basic_set *hull;
1462
3.86k
  isl_basic_set *enforced;
1463
3.86k
  isl_set *guard, *hoisted;
1464
3.86k
  isl_ast_node *node = NULL;
1465
3.86k
  isl_ast_graft *graft;
1466
3.86k
  isl_ast_graft_list *children;
1467
3.86k
  isl_ast_build *sub_build;
1468
3.86k
  isl_ast_build *body_build;
1469
3.86k
1470
3.86k
  domain = isl_ast_build_eliminate_divs(build, domain);
1471
3.86k
  domain = isl_set_detect_equalities(domain);
1472
3.86k
  hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1473
3.86k
  bounds = isl_basic_set_intersect(bounds, hull);
1474
3.86k
  build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1475
3.86k
1476
3.86k
  depth = isl_ast_build_get_depth(build);
1477
3.86k
  sub_build = isl_ast_build_copy(build);
1478
3.86k
  bounds = isl_basic_set_remove_redundancies(bounds);
1479
3.86k
  bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1480
3.86k
  sub_build = isl_ast_build_set_loop_bounds(sub_build,
1481
3.86k
            isl_basic_set_copy(bounds));
1482
3.86k
  degenerate = isl_ast_build_has_value(sub_build);
1483
3.86k
  eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1484
3.86k
  if (degenerate < 0 || eliminated < 0)
1485
0
    executed = isl_union_map_free(executed);
1486
3.86k
  if (!degenerate)
1487
701
    bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1488
3.86k
  sub_build = isl_ast_build_set_pending_generated(sub_build,
1489
3.86k
            isl_basic_set_copy(bounds));
1490
3.86k
  if (eliminated)
1491
3.16k
    executed = plug_in_values(executed, sub_build);
1492
701
  else
1493
701
    node = create_for(build, degenerate);
1494
3.86k
1495
3.86k
  body_build = isl_ast_build_copy(sub_build);
1496
3.86k
  body_build = isl_ast_build_increase_depth(body_build);
1497
3.86k
  if (!eliminated)
1498
701
    node = before_each_for(node, body_build);
1499
3.86k
  children = generate_next_level(executed,
1500
3.86k
            isl_ast_build_copy(body_build));
1501
3.86k
1502
3.86k
  enforced = extract_shared_enforced(children, build);
1503
3.86k
  guard = extract_pending(sub_build, enforced);
1504
3.86k
  hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1505
3.86k
  if (isl_set_n_basic_set(hoisted) > 1)
1506
7
    children = isl_ast_graft_list_gist_guards(children,
1507
7
                isl_set_copy(hoisted));
1508
3.86k
  guard = isl_set_intersect(guard, hoisted);
1509
3.86k
  if (!eliminated)
1510
701
    guard = add_implied_guards(guard, degenerate, bounds, build);
1511
3.86k
1512
3.86k
  graft = isl_ast_graft_alloc_from_children(children,
1513
3.86k
          isl_set_copy(guard), enforced, build, sub_build);
1514
3.86k
1515
3.86k
  if (!eliminated) {
1516
701
    isl_ast_build *for_build;
1517
701
1518
701
    graft = isl_ast_graft_insert_for(graft, node);
1519
701
    for_build = isl_ast_build_copy(build);
1520
701
    for_build = isl_ast_build_replace_pending_by_guard(for_build,
1521
701
              isl_set_copy(guard));
1522
701
    if (degenerate)
1523
0
      graft = refine_degenerate(graft, for_build, sub_build);
1524
701
    else
1525
701
      graft = refine_generic(graft, bounds,
1526
701
          domain, for_build);
1527
701
    isl_ast_build_free(for_build);
1528
701
  }
1529
3.86k
  isl_set_free(guard);
1530
3.86k
  if (!eliminated)
1531
701
    graft = after_each_for(graft, body_build);
1532
3.86k
1533
3.86k
  isl_ast_build_free(body_build);
1534
3.86k
  isl_ast_build_free(sub_build);
1535
3.86k
  isl_ast_build_free(build);
1536
3.86k
  isl_basic_set_free(bounds);
1537
3.86k
  isl_set_free(domain);
1538
3.86k
1539
3.86k
  return graft;
1540
3.86k
}
1541
1542
/* Internal data structure for checking if all constraints involving
1543
 * the input dimension "depth" are such that the other coefficients
1544
 * are multiples of "m", reducing "m" if they are not.
1545
 * If "m" is reduced all the way down to "1", then the check has failed
1546
 * and we break out of the iteration.
1547
 */
1548
struct isl_check_scaled_data {
1549
  int depth;
1550
  isl_val *m;
1551
};
1552
1553
/* If constraint "c" involves the input dimension data->depth,
1554
 * then make sure that all the other coefficients are multiples of data->m,
1555
 * reducing data->m if needed.
1556
 * Break out of the iteration if data->m has become equal to "1".
1557
 */
1558
static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1559
  void *user)
1560
21
{
1561
21
  struct isl_check_scaled_data *data = user;
1562
21
  int i, j, n;
1563
21
  enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1564
21
            isl_dim_div };
1565
21
1566
21
  if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1567
9
    isl_constraint_free(c);
1568
9
    return isl_stat_ok;
1569
9
  }
1570
12
1571
45
  
for (i = 0; 12
i < 4;
++i33
) {
1572
39
    n = isl_constraint_dim(c, t[i]);
1573
80
    for (j = 0; j < n; 
++j41
) {
1574
47
      isl_val *d;
1575
47
1576
47
      if (t[i] == isl_dim_in && 
j == data->depth26
)
1577
12
        continue;
1578
35
      if (!isl_constraint_involves_dims(c, t[i], j, 1))
1579
23
        continue;
1580
12
      d = isl_constraint_get_coefficient_val(c, t[i], j);
1581
12
      data->m = isl_val_gcd(data->m, d);
1582
12
      if (isl_val_is_one(data->m))
1583
6
        break;
1584
12
    }
1585
39
    if (j < n)
1586
6
      break;
1587
39
  }
1588
12
1589
12
  isl_constraint_free(c);
1590
12
1591
12
  return i < 4 ? 
isl_stat_error6
:
isl_stat_ok6
;
1592
12
}
1593
1594
/* For each constraint of "bmap" that involves the input dimension data->depth,
1595
 * make sure that all the other coefficients are multiples of data->m,
1596
 * reducing data->m if needed.
1597
 * Break out of the iteration if data->m has become equal to "1".
1598
 */
1599
static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1600
  void *user)
1601
6
{
1602
6
  isl_stat r;
1603
6
1604
6
  r = isl_basic_map_foreach_constraint(bmap,
1605
6
            &constraint_check_scaled, user);
1606
6
  isl_basic_map_free(bmap);
1607
6
1608
6
  return r;
1609
6
}
1610
1611
/* For each constraint of "map" that involves the input dimension data->depth,
1612
 * make sure that all the other coefficients are multiples of data->m,
1613
 * reducing data->m if needed.
1614
 * Break out of the iteration if data->m has become equal to "1".
1615
 */
1616
static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1617
6
{
1618
6
  isl_stat r;
1619
6
1620
6
  r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1621
6
  isl_map_free(map);
1622
6
1623
6
  return r;
1624
6
}
1625
1626
/* Create an AST node for the current dimension based on
1627
 * the schedule domain "bounds" and return the node encapsulated
1628
 * in an isl_ast_graft.
1629
 *
1630
 * "executed" is the current inverse schedule, taking into account
1631
 * the bounds in "bounds"
1632
 * "domain" is the domain of "executed", with inner dimensions projected out.
1633
 *
1634
 *
1635
 * Before moving on to the actual AST node construction in create_node_scaled,
1636
 * we first check if the current dimension is strided and if we can scale
1637
 * down this stride.  Note that we only do this if the ast_build_scale_strides
1638
 * option is set.
1639
 *
1640
 * In particular, let the current dimension take on values
1641
 *
1642
 *  f + s a
1643
 *
1644
 * with a an integer.  We check if we can find an integer m that (obviously)
1645
 * divides both f and s.
1646
 *
1647
 * If so, we check if the current dimension only appears in constraints
1648
 * where the coefficients of the other variables are multiples of m.
1649
 * We perform this extra check to avoid the risk of introducing
1650
 * divisions by scaling down the current dimension.
1651
 *
1652
 * If so, we scale the current dimension down by a factor of m.
1653
 * That is, we plug in
1654
 *
1655
 *  i = m i'              (1)
1656
 *
1657
 * Note that in principle we could always scale down strided loops
1658
 * by plugging in
1659
 *
1660
 *  i = f + s i'
1661
 *
1662
 * but this may result in i' taking on larger values than the original i,
1663
 * due to the shift by "f".
1664
 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1665
 */
1666
static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1667
  __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1668
  __isl_take isl_ast_build *build)
1669
3.86k
{
1670
3.86k
  struct isl_check_scaled_data data;
1671
3.86k
  isl_ctx *ctx;
1672
3.86k
  isl_aff *offset;
1673
3.86k
  isl_val *d;
1674
3.86k
1675
3.86k
  ctx = isl_ast_build_get_ctx(build);
1676
3.86k
  if (!isl_options_get_ast_build_scale_strides(ctx))
1677
0
    return create_node_scaled(executed, bounds, domain, build);
1678
3.86k
1679
3.86k
  data.depth = isl_ast_build_get_depth(build);
1680
3.86k
  if (!isl_ast_build_has_stride(build, data.depth))
1681
3.85k
    return create_node_scaled(executed, bounds, domain, build);
1682
14
1683
14
  offset = isl_ast_build_get_offset(build, data.depth);
1684
14
  data.m = isl_ast_build_get_stride(build, data.depth);
1685
14
  if (!data.m)
1686
0
    offset = isl_aff_free(offset);
1687
14
  offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1688
14
  d = isl_aff_get_denominator_val(offset);
1689
14
  if (!d)
1690
0
    executed = isl_union_map_free(executed);
1691
14
1692
14
  if (executed && isl_val_is_divisible_by(data.m, d))
1693
12
    data.m = isl_val_div(data.m, d);
1694
2
  else {
1695
2
    data.m = isl_val_set_si(data.m, 1);
1696
2
    isl_val_free(d);
1697
2
  }
1698
14
1699
14
  if (!isl_val_is_one(data.m)) {
1700
6
    if (isl_union_map_foreach_map(executed, &map_check_scaled,
1701
6
            &data) < 0 &&
1702
6
        !isl_val_is_one(data.m))
1703
0
      executed = isl_union_map_free(executed);
1704
6
  }
1705
14
1706
14
  if (!isl_val_is_one(data.m)) {
1707
0
    isl_space *space;
1708
0
    isl_multi_aff *ma;
1709
0
    isl_aff *aff;
1710
0
    isl_map *map;
1711
0
    isl_union_map *umap;
1712
0
1713
0
    space = isl_ast_build_get_space(build, 1);
1714
0
    space = isl_space_map_from_set(space);
1715
0
    ma = isl_multi_aff_identity(space);
1716
0
    aff = isl_multi_aff_get_aff(ma, data.depth);
1717
0
    aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1718
0
    ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1719
0
1720
0
    bounds = isl_basic_set_preimage_multi_aff(bounds,
1721
0
            isl_multi_aff_copy(ma));
1722
0
    domain = isl_set_preimage_multi_aff(domain,
1723
0
            isl_multi_aff_copy(ma));
1724
0
    map = isl_map_reverse(isl_map_from_multi_aff(ma));
1725
0
    umap = isl_union_map_from_map(map);
1726
0
    executed = isl_union_map_apply_domain(executed,
1727
0
            isl_union_map_copy(umap));
1728
0
    build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1729
0
            umap);
1730
0
  }
1731
14
  isl_aff_free(offset);
1732
14
  isl_val_free(data.m);
1733
14
1734
14
  return create_node_scaled(executed, bounds, domain, build);
1735
14
}
1736
1737
/* Add the basic set to the list that "user" points to.
1738
 */
1739
static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1740
945
{
1741
945
  isl_basic_set_list **list = user;
1742
945
1743
945
  *list = isl_basic_set_list_add(*list, bset);
1744
945
1745
945
  return isl_stat_ok;
1746
945
}
1747
1748
/* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1749
 */
1750
static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1751
  __isl_take isl_set *set)
1752
905
{
1753
905
  int n;
1754
905
  isl_ctx *ctx;
1755
905
  isl_basic_set_list *list;
1756
905
1757
905
  if (!set)
1758
0
    return NULL;
1759
905
1760
905
  ctx = isl_set_get_ctx(set);
1761
905
1762
905
  n = isl_set_n_basic_set(set);
1763
905
  list = isl_basic_set_list_alloc(ctx, n);
1764
905
  if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1765
0
    list = isl_basic_set_list_free(list);
1766
905
1767
905
  isl_set_free(set);
1768
905
  return list;
1769
905
}
1770
1771
/* Generate code for the schedule domain "bounds"
1772
 * and add the result to "list".
1773
 *
1774
 * We mainly detect strides here and check if the bounds do not
1775
 * conflict with the current build domain
1776
 * and then pass over control to create_node.
1777
 *
1778
 * "bounds" reflects the bounds on the current dimension and possibly
1779
 * some extra conditions on outer dimensions.
1780
 * It does not, however, include any divs involving the current dimension,
1781
 * so it does not capture any stride constraints.
1782
 * We therefore need to compute that part of the schedule domain that
1783
 * intersects with "bounds" and derive the strides from the result.
1784
 */
1785
static __isl_give isl_ast_graft_list *add_node(
1786
  __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1787
  __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1788
5.36k
{
1789
5.36k
  isl_ast_graft *graft;
1790
5.36k
  isl_set *domain = NULL;
1791
5.36k
  isl_union_set *uset;
1792
5.36k
  int empty, disjoint;
1793
5.36k
1794
5.36k
  uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1795
5.36k
  executed = isl_union_map_intersect_domain(executed, uset);
1796
5.36k
  empty = isl_union_map_is_empty(executed);
1797
5.36k
  if (empty < 0)
1798
0
    goto error;
1799
5.36k
  if (empty)
1800
0
    goto done;
1801
5.36k
1802
5.36k
  uset = isl_union_map_domain(isl_union_map_copy(executed));
1803
5.36k
  domain = isl_set_from_union_set(uset);
1804
5.36k
  domain = isl_ast_build_specialize(build, domain);
1805
5.36k
1806
5.36k
  domain = isl_set_compute_divs(domain);
1807
5.36k
  domain = isl_ast_build_eliminate_inner(build, domain);
1808
5.36k
  disjoint = isl_set_is_disjoint(domain, build->domain);
1809
5.36k
  if (disjoint < 0)
1810
0
    goto error;
1811
5.36k
  if (disjoint)
1812
1.49k
    goto done;
1813
3.86k
1814
3.86k
  build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1815
3.86k
1816
3.86k
  graft = create_node(executed, bounds, domain,
1817
3.86k
        isl_ast_build_copy(build));
1818
3.86k
  list = isl_ast_graft_list_add(list, graft);
1819
3.86k
  isl_ast_build_free(build);
1820
3.86k
  return list;
1821
0
error:
1822
0
  list = isl_ast_graft_list_free(list);
1823
1.49k
done:
1824
1.49k
  isl_set_free(domain);
1825
1.49k
  isl_basic_set_free(bounds);
1826
1.49k
  isl_union_map_free(executed);
1827
1.49k
  isl_ast_build_free(build);
1828
1.49k
  return list;
1829
0
}
1830
1831
/* Does any element of i follow or coincide with any element of j
1832
 * at the current depth for equal values of the outer dimensions?
1833
 */
1834
static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1835
  __isl_keep isl_basic_set *j, void *user)
1836
37
{
1837
37
  int depth = *(int *) user;
1838
37
  isl_basic_map *test;
1839
37
  isl_bool empty;
1840
37
  int l;
1841
37
1842
37
  test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1843
37
                isl_basic_set_copy(j));
1844
55
  for (l = 0; l < depth; 
++l18
)
1845
18
    test = isl_basic_map_equate(test, isl_dim_in, l,
1846
18
            isl_dim_out, l);
1847
37
  test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1848
37
          isl_dim_out, depth);
1849
37
  empty = isl_basic_map_is_empty(test);
1850
37
  isl_basic_map_free(test);
1851
37
1852
37
  return empty < 0 ? 
isl_bool_error0
: !empty;
1853
37
}
1854
1855
/* Split up each element of "list" into a part that is related to "bset"
1856
 * according to "gt" and a part that is not.
1857
 * Return a list that consist of "bset" and all the pieces.
1858
 */
1859
static __isl_give isl_basic_set_list *add_split_on(
1860
  __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1861
  __isl_keep isl_basic_map *gt)
1862
0
{
1863
0
  int i, n;
1864
0
  isl_basic_set_list *res;
1865
0
1866
0
  if (!list)
1867
0
    bset = isl_basic_set_free(bset);
1868
0
1869
0
  gt = isl_basic_map_copy(gt);
1870
0
  gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1871
0
  n = isl_basic_set_list_n_basic_set(list);
1872
0
  res = isl_basic_set_list_from_basic_set(bset);
1873
0
  for (i = 0; res && i < n; ++i) {
1874
0
    isl_basic_set *bset;
1875
0
    isl_set *set1, *set2;
1876
0
    isl_basic_map *bmap;
1877
0
    int empty;
1878
0
1879
0
    bset = isl_basic_set_list_get_basic_set(list, i);
1880
0
    bmap = isl_basic_map_copy(gt);
1881
0
    bmap = isl_basic_map_intersect_range(bmap, bset);
1882
0
    bset = isl_basic_map_range(bmap);
1883
0
    empty = isl_basic_set_is_empty(bset);
1884
0
    if (empty < 0)
1885
0
      res = isl_basic_set_list_free(res);
1886
0
    if (empty)  {
1887
0
      isl_basic_set_free(bset);
1888
0
      bset = isl_basic_set_list_get_basic_set(list, i);
1889
0
      res = isl_basic_set_list_add(res, bset);
1890
0
      continue;
1891
0
    }
1892
0
1893
0
    res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1894
0
    set1 = isl_set_from_basic_set(bset);
1895
0
    bset = isl_basic_set_list_get_basic_set(list, i);
1896
0
    set2 = isl_set_from_basic_set(bset);
1897
0
    set1 = isl_set_subtract(set2, set1);
1898
0
    set1 = isl_set_make_disjoint(set1);
1899
0
1900
0
    res = isl_basic_set_list_concat(res,
1901
0
              isl_basic_set_list_from_set(set1));
1902
0
  }
1903
0
  isl_basic_map_free(gt);
1904
0
  isl_basic_set_list_free(list);
1905
0
  return res;
1906
0
}
1907
1908
static __isl_give isl_ast_graft_list *generate_sorted_domains(
1909
  __isl_keep isl_basic_set_list *domain_list,
1910
  __isl_keep isl_union_map *executed,
1911
  __isl_keep isl_ast_build *build);
1912
1913
/* Internal data structure for add_nodes.
1914
 *
1915
 * "executed" and "build" are extra arguments to be passed to add_node.
1916
 * "list" collects the results.
1917
 */
1918
struct isl_add_nodes_data {
1919
  isl_union_map *executed;
1920
  isl_ast_build *build;
1921
1922
  isl_ast_graft_list *list;
1923
};
1924
1925
/* Generate code for the schedule domains in "scc"
1926
 * and add the results to "list".
1927
 *
1928
 * The domains in "scc" form a strongly connected component in the ordering.
1929
 * If the number of domains in "scc" is larger than 1, then this means
1930
 * that we cannot determine a valid ordering for the domains in the component.
1931
 * This should be fairly rare because the individual domains
1932
 * have been made disjoint first.
1933
 * The problem is that the domains may be integrally disjoint but not
1934
 * rationally disjoint.  For example, we may have domains
1935
 *
1936
 *  { [i,i] : 0 <= i <= 1 }   and { [i,1-i] : 0 <= i <= 1 }
1937
 *
1938
 * These two domains have an empty intersection, but their rational
1939
 * relaxations do intersect.  It is impossible to order these domains
1940
 * in the second dimension because the first should be ordered before
1941
 * the second for outer dimension equal to 0, while it should be ordered
1942
 * after for outer dimension equal to 1.
1943
 *
1944
 * This may happen in particular in case of unrolling since the domain
1945
 * of each slice is replaced by its simple hull.
1946
 *
1947
 * For each basic set i in "scc" and for each of the following basic sets j,
1948
 * we split off that part of the basic set i that shares the outer dimensions
1949
 * with j and lies before j in the current dimension.
1950
 * We collect all the pieces in a new list that replaces "scc".
1951
 *
1952
 * While the elements in "scc" should be disjoint, we double-check
1953
 * this property to avoid running into an infinite recursion in case
1954
 * they intersect due to some internal error.
1955
 */
1956
static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1957
32
{
1958
32
  struct isl_add_nodes_data *data = user;
1959
32
  int i, n, depth;
1960
32
  isl_basic_set *bset, *first;
1961
32
  isl_basic_set_list *list;
1962
32
  isl_space *space;
1963
32
  isl_basic_map *gt;
1964
32
1965
32
  n = isl_basic_set_list_n_basic_set(scc);
1966
32
  bset = isl_basic_set_list_get_basic_set(scc, 0);
1967
32
  if (n == 1) {
1968
32
    isl_basic_set_list_free(scc);
1969
32
    data->list = add_node(data->list,
1970
32
        isl_union_map_copy(data->executed), bset,
1971
32
        isl_ast_build_copy(data->build));
1972
32
    return data->list ? isl_stat_ok : 
isl_stat_error0
;
1973
32
  }
1974
0
1975
0
  depth = isl_ast_build_get_depth(data->build);
1976
0
  space = isl_basic_set_get_space(bset);
1977
0
  space = isl_space_map_from_set(space);
1978
0
  gt = isl_basic_map_universe(space);
1979
0
  for (i = 0; i < depth; ++i)
1980
0
    gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1981
0
  gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1982
0
1983
0
  first = isl_basic_set_copy(bset);
1984
0
  list = isl_basic_set_list_from_basic_set(bset);
1985
0
  for (i = 1; i < n; ++i) {
1986
0
    int disjoint;
1987
0
1988
0
    bset = isl_basic_set_list_get_basic_set(scc, i);
1989
0
1990
0
    disjoint = isl_basic_set_is_disjoint(bset, first);
1991
0
    if (disjoint < 0)
1992
0
      list = isl_basic_set_list_free(list);
1993
0
    else if (!disjoint)
1994
0
      isl_die(isl_basic_set_list_get_ctx(scc),
1995
0
        isl_error_internal,
1996
0
        "basic sets in scc are assumed to be disjoint",
1997
0
        list = isl_basic_set_list_free(list));
1998
0
1999
0
    list = add_split_on(list, bset, gt);
2000
0
  }
2001
0
  isl_basic_set_free(first);
2002
0
  isl_basic_map_free(gt);
2003
0
  isl_basic_set_list_free(scc);
2004
0
  scc = list;
2005
0
  data->list = isl_ast_graft_list_concat(data->list,
2006
0
        generate_sorted_domains(scc, data->executed, data->build));
2007
0
  isl_basic_set_list_free(scc);
2008
0
2009
0
  return data->list ? isl_stat_ok : isl_stat_error;
2010
0
}
2011
2012
/* Sort the domains in "domain_list" according to the execution order
2013
 * at the current depth (for equal values of the outer dimensions),
2014
 * generate code for each of them, collecting the results in a list.
2015
 * If no code is generated (because the intersection of the inverse schedule
2016
 * with the domains turns out to be empty), then an empty list is returned.
2017
 *
2018
 * The caller is responsible for ensuring that the basic sets in "domain_list"
2019
 * are pair-wise disjoint.  It can, however, in principle happen that
2020
 * two basic sets should be ordered one way for one value of the outer
2021
 * dimensions and the other way for some other value of the outer dimensions.
2022
 * We therefore play safe and look for strongly connected components.
2023
 * The function add_nodes takes care of handling non-trivial components.
2024
 */
2025
static __isl_give isl_ast_graft_list *generate_sorted_domains(
2026
  __isl_keep isl_basic_set_list *domain_list,
2027
  __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2028
939
{
2029
939
  isl_ctx *ctx;
2030
939
  struct isl_add_nodes_data data;
2031
939
  int depth;
2032
939
  int n;
2033
939
2034
939
  if (!domain_list)
2035
0
    return NULL;
2036
939
2037
939
  ctx = isl_basic_set_list_get_ctx(domain_list);
2038
939
  n = isl_basic_set_list_n_basic_set(domain_list);
2039
939
  data.list = isl_ast_graft_list_alloc(ctx, n);
2040
939
  if (n == 0)
2041
0
    return data.list;
2042
939
  if (n == 1)
2043
926
    return add_node(data.list, isl_union_map_copy(executed),
2044
926
      isl_basic_set_list_get_basic_set(domain_list, 0),
2045
926
      isl_ast_build_copy(build));
2046
13
2047
13
  depth = isl_ast_build_get_depth(build);
2048
13
  data.executed = executed;
2049
13
  data.build = build;
2050
13
  if (isl_basic_set_list_foreach_scc(domain_list,
2051
13
          &domain_follows_at_depth, &depth,
2052
13
          &add_nodes, &data) < 0)
2053
0
    data.list = isl_ast_graft_list_free(data.list);
2054
13
2055
13
  return data.list;
2056
13
}
2057
2058
/* Do i and j share any values for the outer dimensions?
2059
 */
2060
static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2061
  __isl_keep isl_basic_set *j, void *user)
2062
83
{
2063
83
  int depth = *(int *) user;
2064
83
  isl_basic_map *test;
2065
83
  isl_bool empty;
2066
83
  int l;
2067
83
2068
83
  test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2069
83
                isl_basic_set_copy(j));
2070
155
  for (l = 0; l < depth; 
++l72
)
2071
72
    test = isl_basic_map_equate(test, isl_dim_in, l,
2072
72
            isl_dim_out, l);
2073
83
  empty = isl_basic_map_is_empty(test);
2074
83
  isl_basic_map_free(test);
2075
83
2076
83
  return empty < 0 ? 
isl_bool_error0
: !empty;
2077
83
}
2078
2079
/* Internal data structure for generate_sorted_domains_wrap.
2080
 *
2081
 * "n" is the total number of basic sets
2082
 * "executed" and "build" are extra arguments to be passed
2083
 *  to generate_sorted_domains.
2084
 *
2085
 * "single" is set to 1 by generate_sorted_domains_wrap if there
2086
 * is only a single component.
2087
 * "list" collects the results.
2088
 */
2089
struct isl_ast_generate_parallel_domains_data {
2090
  int n;
2091
  isl_union_map *executed;
2092
  isl_ast_build *build;
2093
2094
  int single;
2095
  isl_ast_graft_list *list;
2096
};
2097
2098
/* Call generate_sorted_domains on "scc", fuse the result into a list
2099
 * with either zero or one graft and collect the these single element
2100
 * lists into data->list.
2101
 *
2102
 * If there is only one component, i.e., if the number of basic sets
2103
 * in the current component is equal to the total number of basic sets,
2104
 * then data->single is set to 1 and the result of generate_sorted_domains
2105
 * is not fused.
2106
 */
2107
static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2108
  void *user)
2109
71
{
2110
71
  struct isl_ast_generate_parallel_domains_data *data = user;
2111
71
  isl_ast_graft_list *list;
2112
71
2113
71
  list = generate_sorted_domains(scc, data->executed, data->build);
2114
71
  data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2115
71
  if (!data->single)
2116
58
    list = isl_ast_graft_list_fuse(list, data->build);
2117
71
  if (!data->list)
2118
41
    data->list = list;
2119
30
  else
2120
30
    data->list = isl_ast_graft_list_concat(data->list, list);
2121
71
2122
71
  isl_basic_set_list_free(scc);
2123
71
  if (!data->list)
2124
0
    return isl_stat_error;
2125
71
2126
71
  return isl_stat_ok;
2127
71
}
2128
2129
/* Look for any (weakly connected) components in the "domain_list"
2130
 * of domains that share some values of the outer dimensions.
2131
 * That is, domains in different components do not share any values
2132
 * of the outer dimensions.  This means that these components
2133
 * can be freely reordered.
2134
 * Within each of the components, we sort the domains according
2135
 * to the execution order at the current depth.
2136
 *
2137
 * If there is more than one component, then generate_sorted_domains_wrap
2138
 * fuses the result of each call to generate_sorted_domains
2139
 * into a list with either zero or one graft and collects these (at most)
2140
 * single element lists into a bigger list. This means that the elements of the
2141
 * final list can be freely reordered.  In particular, we sort them
2142
 * according to an arbitrary but fixed ordering to ease merging of
2143
 * graft lists from different components.
2144
 */
2145
static __isl_give isl_ast_graft_list *generate_parallel_domains(
2146
  __isl_keep isl_basic_set_list *domain_list,
2147
  __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2148
909
{
2149
909
  int depth;
2150
909
  struct isl_ast_generate_parallel_domains_data data;
2151
909
2152
909
  if (!domain_list)
2153
0
    return NULL;
2154
909
2155
909
  data.n = isl_basic_set_list_n_basic_set(domain_list);
2156
909
  if (data.n <= 1)
2157
868
    return generate_sorted_domains(domain_list, executed, build);
2158
41
2159
41
  depth = isl_ast_build_get_depth(build);
2160
41
  data.list = NULL;
2161
41
  data.executed = executed;
2162
41
  data.build = build;
2163
41
  data.single = 0;
2164
41
  if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2165
41
              &generate_sorted_domains_wrap,
2166
41
              &data) < 0)
2167
0
    data.list = isl_ast_graft_list_free(data.list);
2168
41
2169
41
  if (!data.single)
2170
28
    data.list = isl_ast_graft_list_sort_guard(data.list);
2171
41
2172
41
  return data.list;
2173
41
}
2174
2175
/* Internal data for separate_domain.
2176
 *
2177
 * "explicit" is set if we only want to use explicit bounds.
2178
 *
2179
 * "domain" collects the separated domains.
2180
 */
2181
struct isl_separate_domain_data {
2182
  isl_ast_build *build;
2183
  int explicit;
2184
  isl_set *domain;
2185
};
2186
2187
/* Extract implicit bounds on the current dimension for the executed "map".
2188
 *
2189
 * The domain of "map" may involve inner dimensions, so we
2190
 * need to eliminate them.
2191
 */
2192
static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2193
  __isl_keep isl_ast_build *build)
2194
0
{
2195
0
  isl_set *domain;
2196
0
2197
0
  domain = isl_map_domain(map);
2198
0
  domain = isl_ast_build_eliminate(build, domain);
2199
0
2200
0
  return domain;
2201
0
}
2202
2203
/* Extract explicit bounds on the current dimension for the executed "map".
2204
 *
2205
 * Rather than eliminating the inner dimensions as in implicit_bounds,
2206
 * we simply drop any constraints involving those inner dimensions.
2207
 * The idea is that most bounds that are implied by constraints on the
2208
 * inner dimensions will be enforced by for loops and not by explicit guards.
2209
 * There is then no need to separate along those bounds.
2210
 */
2211
static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2212
  __isl_keep isl_ast_build *build)
2213
24
{
2214
24
  isl_set *domain;
2215
24
  int depth, dim;
2216
24
2217
24
  dim = isl_map_dim(map, isl_dim_out);
2218
24
  map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2219
24
2220
24
  domain = isl_map_domain(map);
2221
24
  depth = isl_ast_build_get_depth(build);
2222
24
  dim = isl_set_dim(domain, isl_dim_set);
2223
24
  domain = isl_set_detect_equalities(domain);
2224
24
  domain = isl_set_drop_constraints_involving_dims(domain,
2225
24
        isl_dim_set, depth + 1, dim - (depth + 1));
2226
24
  domain = isl_set_remove_divs_involving_dims(domain,
2227
24
        isl_dim_set, depth, 1);
2228
24
  domain = isl_set_remove_unknown_divs(domain);
2229
24
2230
24
  return domain;
2231
24
}
2232
2233
/* Split data->domain into pieces that intersect with the range of "map"
2234
 * and pieces that do not intersect with the range of "map"
2235
 * and then add that part of the range of "map" that does not intersect
2236
 * with data->domain.
2237
 */
2238
static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2239
24
{
2240
24
  struct isl_separate_domain_data *data = user;
2241
24
  isl_set *domain;
2242
24
  isl_set *d1, *d2;
2243
24
2244
24
  if (data->explicit)
2245
24
    domain = explicit_bounds(map, data->build);
2246
0
  else
2247
0
    domain = implicit_bounds(map, data->build);
2248
24
2249
24
  domain = isl_set_coalesce(domain);
2250
24
  domain = isl_set_make_disjoint(domain);
2251
24
  d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2252
24
  d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2253
24
  data->domain = isl_set_intersect(data->domain, domain);
2254
24
  data->domain = isl_set_union(data->domain, d1);
2255
24
  data->domain = isl_set_union(data->domain, d2);
2256
24
2257
24
  return isl_stat_ok;
2258
24
}
2259
2260
/* Separate the schedule domains of "executed".
2261
 *
2262
 * That is, break up the domain of "executed" into basic sets,
2263
 * such that for each basic set S, every element in S is associated with
2264
 * the same domain spaces.
2265
 *
2266
 * "space" is the (single) domain space of "executed".
2267
 */
2268
static __isl_give isl_set *separate_schedule_domains(
2269
  __isl_take isl_space *space, __isl_take isl_union_map *executed,
2270
  __isl_keep isl_ast_build *build)
2271
24
{
2272
24
  struct isl_separate_domain_data data = { build };
2273
24
  isl_ctx *ctx;
2274
24
2275
24
  ctx = isl_ast_build_get_ctx(build);
2276
24
  data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2277
24
            ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2278
24
  data.domain = isl_set_empty(space);
2279
24
  if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2280
0
    data.domain = isl_set_free(data.domain);
2281
24
2282
24
  isl_union_map_free(executed);
2283
24
  return data.domain;
2284
24
}
2285
2286
/* Temporary data used during the search for a lower bound for unrolling.
2287
 *
2288
 * "build" is the build in which the unrolling will be performed
2289
 * "domain" is the original set for which to find a lower bound
2290
 * "depth" is the dimension for which to find a lower boudn
2291
 * "expansion" is the expansion that needs to be applied to "domain"
2292
 * in the unrolling that will be performed
2293
 *
2294
 * "lower" is the best lower bound found so far.  It is NULL if we have not
2295
 * found any yet.
2296
 * "n" is the corresponding size.  If lower is NULL, then the value of n
2297
 * is undefined.
2298
 * "n_div" is the maximal number of integer divisions in the first
2299
 * unrolled iteration (after expansion).  It is set to -1 if it hasn't
2300
 * been computed yet.
2301
 */
2302
struct isl_find_unroll_data {
2303
  isl_ast_build *build;
2304
  isl_set *domain;
2305
  int depth;
2306
  isl_basic_map *expansion;
2307
2308
  isl_aff *lower;
2309
  int *n;
2310
  int n_div;
2311
};
2312
2313
/* Return the constraint
2314
 *
2315
 *  i_"depth" = aff + offset
2316
 */
2317
static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2318
  int offset)
2319
4.41k
{
2320
4.41k
  aff = isl_aff_copy(aff);
2321
4.41k
  aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2322
4.41k
  aff = isl_aff_add_constant_si(aff, offset);
2323
4.41k
  return isl_equality_from_aff(aff);
2324
4.41k
}
2325
2326
/* Update *user to the number of integer divsions in the first element
2327
 * of "ma", if it is larger than the current value.
2328
 */
2329
static isl_stat update_n_div(__isl_take isl_set *set,
2330
  __isl_take isl_multi_aff *ma, void *user)
2331
2
{
2332
2
  isl_aff *aff;
2333
2
  int *n = user;
2334
2
  int n_div;
2335
2
2336
2
  aff = isl_multi_aff_get_aff(ma, 0);
2337
2
  n_div = isl_aff_dim(aff, isl_dim_div);
2338
2
  isl_aff_free(aff);
2339
2
  isl_multi_aff_free(ma);
2340
2
  isl_set_free(set);
2341
2
2342
2
  if (n_div > *n)
2343
2
    *n = n_div;
2344
2
2345
2
  return aff ? isl_stat_ok : 
isl_stat_error0
;
2346
2
}
2347
2348
/* Get the number of integer divisions in the expression for the iterator
2349
 * value at the first slice in the unrolling based on lower bound "lower",
2350
 * taking into account the expansion that needs to be performed on this slice.
2351
 */
2352
static int get_expanded_n_div(struct isl_find_unroll_data *data,
2353
  __isl_keep isl_aff *lower)
2354
2
{
2355
2
  isl_constraint *c;
2356
2
  isl_set *set;
2357
2
  isl_map *it_map, *expansion;
2358
2
  isl_pw_multi_aff *pma;
2359
2
  int n;
2360
2
2361
2
  c = at_offset(data->depth, lower, 0);
2362
2
  set = isl_set_copy(data->domain);
2363
2
  set = isl_set_add_constraint(set, c);
2364
2
  expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2365
2
  set = isl_set_apply(set, expansion);
2366
2
  it_map = isl_ast_build_map_to_iterator(data->build, set);
2367
2
  pma = isl_pw_multi_aff_from_map(it_map);
2368
2
  n = 0;
2369
2
  if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2370
0
    n = -1;
2371
2
  isl_pw_multi_aff_free(pma);
2372
2
2373
2
  return n;
2374
2
}
2375
2376
/* Is the lower bound "lower" with corresponding iteration count "n"
2377
 * better than the one stored in "data"?
2378
 * If there is no upper bound on the iteration count ("n" is infinity) or
2379
 * if the count is too large, then we cannot use this lower bound.
2380
 * Otherwise, if there was no previous lower bound or
2381
 * if the iteration count of the new lower bound is smaller than
2382
 * the iteration count of the previous lower bound, then we consider
2383
 * the new lower bound to be better.
2384
 * If the iteration count is the same, then compare the number
2385
 * of integer divisions that would be needed to express
2386
 * the iterator value at the first slice in the unrolling
2387
 * according to the lower bound.  If we end up computing this
2388
 * number, then store the lowest value in data->n_div.
2389
 */
2390
static int is_better_lower_bound(struct isl_find_unroll_data *data,
2391
  __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2392
2.41k
{
2393
2.41k
  int cmp;
2394
2.41k
  int n_div;
2395
2.41k
2396
2.41k
  if (!n)
2397
0
    return -1;
2398
2.41k
  if (isl_val_is_infty(n))
2399
0
    return 0;
2400
2.41k
  if (isl_val_cmp_si(n, INT_MAX) > 0)
2401
0
    return 0;
2402
2.41k
  if (!data->lower)
2403
2.31k
    return 1;
2404
101
  cmp = isl_val_cmp_si(n, *data->n);
2405
101
  if (cmp < 0)
2406
91
    return 1;
2407
10
  if (cmp > 0)
2408
9
    return 0;
2409
1
  if (data->n_div < 0)
2410
1
    data->n_div = get_expanded_n_div(data, data->lower);
2411
1
  if (data->n_div < 0)
2412
0
    return -1;
2413
1
  if (data->n_div == 0)
2414
0
    return 0;
2415
1
  n_div = get_expanded_n_div(data, lower);
2416
1
  if (n_div < 0)
2417
0
    return -1;
2418
1
  if (n_div >= data->n_div)
2419
0
    return 0;
2420
1
  data->n_div = n_div;
2421
1
2422
1
  return 1;
2423
1
}
2424
2425
/* Check if we can use "c" as a lower bound and if it is better than
2426
 * any previously found lower bound.
2427
 *
2428
 * If "c" does not involve the dimension at the current depth,
2429
 * then we cannot use it.
2430
 * Otherwise, let "c" be of the form
2431
 *
2432
 *  i >= f(j)/a
2433
 *
2434
 * We compute the maximal value of
2435
 *
2436
 *  -ceil(f(j)/a)) + i + 1
2437
 *
2438
 * over the domain.  If there is such a value "n", then we know
2439
 *
2440
 *  -ceil(f(j)/a)) + i + 1 <= n
2441
 *
2442
 * or
2443
 *
2444
 *  i < ceil(f(j)/a)) + n
2445
 *
2446
 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2447
 * We just need to check if we have found any lower bound before and
2448
 * if the new lower bound is better (smaller n or fewer integer divisions)
2449
 * than the previously found lower bounds.
2450
 */
2451
static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2452
  __isl_keep isl_constraint *c)
2453
31.3k
{
2454
31.3k
  isl_aff *aff, *lower;
2455
31.3k
  isl_val *max;
2456
31.3k
  int better;
2457
31.3k
2458
31.3k
  if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2459
28.9k
    return isl_stat_ok;
2460
2.41k
2461
2.41k
  lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2462
2.41k
  lower = isl_aff_ceil(lower);
2463
2.41k
  aff = isl_aff_copy(lower);
2464
2.41k
  aff = isl_aff_neg(aff);
2465
2.41k
  aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2466
2.41k
  aff = isl_aff_add_constant_si(aff, 1);
2467
2.41k
  max = isl_set_max_val(data->domain, aff);
2468
2.41k
  isl_aff_free(aff);
2469
2.41k
2470
2.41k
  better = is_better_lower_bound(data, lower, max);
2471
2.41k
  if (better < 0 || !better) {
2472
9
    isl_val_free(max);
2473
9
    isl_aff_free(lower);
2474
9
    return better < 0 ? 
isl_stat_error0
: isl_stat_ok;
2475
9
  }
2476
2.40k
2477
2.40k
  isl_aff_free(data->lower);
2478
2.40k
  data->lower = lower;
2479
2.40k
  *data->n = isl_val_get_num_si(max);
2480
2.40k
  isl_val_free(max);
2481
2.40k
2482
2.40k
  return isl_stat_ok;
2483
2.40k
}
2484
2485
/* Check if we can use "c" as a lower bound and if it is better than
2486
 * any previously found lower bound.
2487
 */
2488
static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2489
31.3k
{
2490
31.3k
  struct isl_find_unroll_data *data;
2491
31.3k
  isl_stat r;
2492
31.3k
2493
31.3k
  data = (struct isl_find_unroll_data *) user;
2494
31.3k
  r = update_unrolling_lower_bound(data, c);
2495
31.3k
  isl_constraint_free(c);
2496
31.3k
2497
31.3k
  return r;
2498
31.3k
}
2499
2500
/* Look for a lower bound l(i) on the dimension at "depth"
2501
 * and a size n such that "domain" is a subset of
2502
 *
2503
 *  { [i] : l(i) <= i_d < l(i) + n }
2504
 *
2505
 * where d is "depth" and l(i) depends only on earlier dimensions.
2506
 * Furthermore, try and find a lower bound such that n is as small as possible.
2507
 * In particular, "n" needs to be finite.
2508
 * "build" is the build in which the unrolling will be performed.
2509
 * "expansion" is the expansion that needs to be applied to "domain"
2510
 * in the unrolling that will be performed.
2511
 *
2512
 * Inner dimensions have been eliminated from "domain" by the caller.
2513
 *
2514
 * We first construct a collection of lower bounds on the input set
2515
 * by computing its simple hull.  We then iterate through them,
2516
 * discarding those that we cannot use (either because they do not
2517
 * involve the dimension at "depth" or because they have no corresponding
2518
 * upper bound, meaning that "n" would be unbounded) and pick out the
2519
 * best from the remaining ones.
2520
 *
2521
 * If we cannot find a suitable lower bound, then we consider that
2522
 * to be an error.
2523
 */
2524
static __isl_give isl_aff *find_unroll_lower_bound(
2525
  __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2526
  int depth, __isl_keep isl_basic_map *expansion, int *n)
2527
2.31k
{
2528
2.31k
  struct isl_find_unroll_data data =
2529
2.31k
      { build, domain, depth, expansion, NULL, n, -1 };
2530
2.31k
  isl_basic_set *hull;
2531
2.31k
2532
2.31k
  hull = isl_set_simple_hull(isl_set_copy(domain));
2533
2.31k
2534
2.31k
  if (isl_basic_set_foreach_constraint(hull,
2535
2.31k
              &constraint_find_unroll, &data) < 0)
2536
0
    goto error;
2537
2.31k
2538
2.31k
  isl_basic_set_free(hull);
2539
2.31k
2540
2.31k
  if (!data.lower)
2541
2.31k
    
isl_die0
(isl_set_get_ctx(domain), isl_error_invalid,
2542
2.31k
      "cannot find lower bound for unrolling", return NULL);
2543
2.31k
2544
2.31k
  return data.lower;
2545
0
error:
2546
0
  isl_basic_set_free(hull);
2547
0
  return isl_aff_free(data.lower);
2548
2.31k
}
2549
2550
/* Call "fn" on each iteration of the current dimension of "domain".
2551
 * If "init" is not NULL, then it is called with the number of
2552
 * iterations before any call to "fn".
2553
 * Return -1 on failure.
2554
 *
2555
 * Since we are going to be iterating over the individual values,
2556
 * we first check if there are any strides on the current dimension.
2557
 * If there is, we rewrite the current dimension i as
2558
 *
2559
 *    i = stride i' + offset
2560
 *
2561
 * and then iterate over individual values of i' instead.
2562
 *
2563
 * We then look for a lower bound on i' and a size such that the domain
2564
 * is a subset of
2565
 *
2566
 *  { [j,i'] : l(j) <= i' < l(j) + n }
2567
 *
2568
 * and then take slices of the domain at values of i'
2569
 * between l(j) and l(j) + n - 1.
2570
 *
2571
 * We compute the unshifted simple hull of each slice to ensure that
2572
 * we have a single basic set per offset.  The slicing constraint
2573
 * may get simplified away before the unshifted simple hull is taken
2574
 * and may therefore in some rare cases disappear from the result.
2575
 * We therefore explicitly add the constraint back after computing
2576
 * the unshifted simple hull to ensure that the basic sets
2577
 * remain disjoint.  The constraints that are dropped by taking the hull
2578
 * will be taken into account at the next level, as in the case of the
2579
 * atomic option.
2580
 *
2581
 * Finally, we map i' back to i and call "fn".
2582
 */
2583
static int foreach_iteration(__isl_take isl_set *domain,
2584
  __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2585
  int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2586
2.31k
{
2587
2.31k
  int i, n;
2588
2.31k
  int empty;
2589
2.31k
  int depth;
2590
2.31k
  isl_multi_aff *expansion;
2591
2.31k
  isl_basic_map *bmap;
2592
2.31k
  isl_aff *lower = NULL;
2593
2.31k
  isl_ast_build *stride_build;
2594
2.31k
2595
2.31k
  depth = isl_ast_build_get_depth(build);
2596
2.31k
2597
2.31k
  domain = isl_ast_build_eliminate_inner(build, domain);
2598
2.31k
  domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2599
2.31k
  stride_build = isl_ast_build_copy(build);
2600
2.31k
  stride_build = isl_ast_build_detect_strides(stride_build,
2601
2.31k
              isl_set_copy(domain));
2602
2.31k
  expansion = isl_ast_build_get_stride_expansion(stride_build);
2603
2.31k
2604
2.31k
  domain = isl_set_preimage_multi_aff(domain,
2605
2.31k
              isl_multi_aff_copy(expansion));
2606
2.31k
  domain = isl_ast_build_eliminate_divs(stride_build, domain);
2607
2.31k
  isl_ast_build_free(stride_build);
2608
2.31k
2609
2.31k
  bmap = isl_basic_map_from_multi_aff(expansion);
2610
2.31k
2611
2.31k
  empty = isl_set_is_empty(domain);
2612
2.31k
  if (empty < 0) {
2613
0
    n = -1;
2614
2.31k
  } else if (empty) {
2615
0
    n = 0;
2616
2.31k
  } else {
2617
2.31k
    lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2618
2.31k
    if (!lower)
2619
0
      n = -1;
2620
2.31k
  }
2621
2.31k
  if (n >= 0 && init && 
init(n, user) < 02.31k
)
2622
0
    n = -1;
2623
6.72k
  for (i = 0; i < n; 
++i4.41k
) {
2624
4.41k
    isl_set *set;
2625
4.41k
    isl_basic_set *bset;
2626
4.41k
    isl_constraint *slice;
2627
4.41k
2628
4.41k
    slice = at_offset(depth, lower, i);
2629
4.41k
    set = isl_set_copy(domain);
2630
4.41k
    set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2631
4.41k
    bset = isl_set_unshifted_simple_hull(set);
2632
4.41k
    bset = isl_basic_set_add_constraint(bset, slice);
2633
4.41k
    bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2634
4.41k
2635
4.41k
    if (fn(bset, user) < 0)
2636
0
      break;
2637
4.41k
  }
2638
2.31k
2639
2.31k
  isl_aff_free(lower);
2640
2.31k
  isl_set_free(domain);
2641
2.31k
  isl_basic_map_free(bmap);
2642
2.31k
2643
2.31k
  return n < 0 || i < n ? 
-10
: 0;
2644
2.31k
}
2645
2646
/* Data structure for storing the results and the intermediate objects
2647
 * of compute_domains.
2648
 *
2649
 * "list" is the main result of the function and contains a list
2650
 * of disjoint basic sets for which code should be generated.
2651
 *
2652
 * "executed" and "build" are inputs to compute_domains.
2653
 * "schedule_domain" is the domain of "executed".
2654
 *
2655
 * "option" contains the domains at the current depth that should by
2656
 * atomic, separated or unrolled.  These domains are as specified by
2657
 * the user, except that inner dimensions have been eliminated and
2658
 * that they have been made pair-wise disjoint.
2659
 *
2660
 * "sep_class" contains the user-specified split into separation classes
2661
 * specialized to the current depth.
2662
 * "done" contains the union of the separation domains that have already
2663
 * been handled.
2664
 */
2665
struct isl_codegen_domains {
2666
  isl_basic_set_list *list;
2667
2668
  isl_union_map *executed;
2669
  isl_ast_build *build;
2670
  isl_set *schedule_domain;
2671
2672
  isl_set *option[4];
2673
2674
  isl_map *sep_class;
2675
  isl_set *done;
2676
};
2677
2678
/* Internal data structure for do_unroll.
2679
 *
2680
 * "domains" stores the results of compute_domains.
2681
 * "class_domain" is the original class domain passed to do_unroll.
2682
 * "unroll_domain" collects the unrolled iterations.
2683
 */
2684
struct isl_ast_unroll_data {
2685
  struct isl_codegen_domains *domains;
2686
  isl_set *class_domain;
2687
  isl_set *unroll_domain;
2688
};
2689
2690
/* Given an iteration of an unrolled domain represented by "bset",
2691
 * add it to data->domains->list.
2692
 * Since we may have dropped some constraints, we intersect with
2693
 * the class domain again to ensure that each element in the list
2694
 * is disjoint from the other class domains.
2695
 */
2696
static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2697
8
{
2698
8
  struct isl_ast_unroll_data *data = user;
2699
8
  isl_set *set;
2700
8
  isl_basic_set_list *list;
2701
8
2702
8
  set = isl_set_from_basic_set(bset);
2703
8
  data->unroll_domain = isl_set_union(data->unroll_domain,
2704
8
              isl_set_copy(set));
2705
8
  set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2706
8
  set = isl_set_make_disjoint(set);
2707
8
  list = isl_basic_set_list_from_set(set);
2708
8
  data->domains->list = isl_basic_set_list_concat(data->domains->list,
2709
8
              list);
2710
8
2711
8
  return 0;
2712
8
}
2713
2714
/* Extend domains->list with a list of basic sets, one for each value
2715
 * of the current dimension in "domain" and remove the corresponding
2716
 * sets from the class domain.  Return the updated class domain.
2717
 * The divs that involve the current dimension have not been projected out
2718
 * from this domain.
2719
 *
2720
 * We call foreach_iteration to iterate over the individual values and
2721
 * in do_unroll_iteration we collect the individual basic sets in
2722
 * domains->list and their union in data->unroll_domain, which is then
2723
 * used to update the class domain.
2724
 */
2725
static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2726
  __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2727
3
{
2728
3
  struct isl_ast_unroll_data data;
2729
3
2730
3
  if (!domain)
2731
0
    return isl_set_free(class_domain);
2732
3
  if (!class_domain)
2733
0
    return isl_set_free(domain);
2734
3
2735
3
  data.domains = domains;
2736
3
  data.class_domain = class_domain;
2737
3
  data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2738
3
2739
3
  if (foreach_iteration(domain, domains->build, NULL,
2740
3
        &do_unroll_iteration, &data) < 0)
2741
0
    data.unroll_domain = isl_set_free(data.unroll_domain);
2742
3
2743
3
  class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2744
3
2745
3
  return class_domain;
2746
3
}
2747
2748
/* Add domains to domains->list for each individual value of the current
2749
 * dimension, for that part of the schedule domain that lies in the
2750
 * intersection of the option domain and the class domain.
2751
 * Remove the corresponding sets from the class domain and
2752
 * return the updated class domain.
2753
 *
2754
 * We first break up the unroll option domain into individual pieces
2755
 * and then handle each of them separately.  The unroll option domain
2756
 * has been made disjoint in compute_domains_init_options,
2757
 *
2758
 * Note that we actively want to combine different pieces of the
2759
 * schedule domain that have the same value at the current dimension.
2760
 * We therefore need to break up the unroll option domain before
2761
 * intersecting with class and schedule domain, hoping that the
2762
 * unroll option domain specified by the user is relatively simple.
2763
 */
2764
static __isl_give isl_set *compute_unroll_domains(
2765
  struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2766
84
{
2767
84
  isl_set *unroll_domain;
2768
84
  isl_basic_set_list *unroll_list;
2769
84
  int i, n;
2770
84
  int empty;
2771
84
2772
84
  empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2773
84
  if (empty < 0)
2774
0
    return isl_set_free(class_domain);
2775
84
  if (empty)
2776
81
    return class_domain;
2777
3
2778
3
  unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2779
3
  unroll_list = isl_basic_set_list_from_set(unroll_domain);
2780
3
2781
3
  n = isl_basic_set_list_n_basic_set(unroll_list);
2782
6
  for (i = 0; i < n; 
++i3
) {
2783
3
    isl_basic_set *bset;
2784
3
2785
3
    bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2786
3
    unroll_domain = isl_set_from_basic_set(bset);
2787
3
    unroll_domain = isl_set_intersect(unroll_domain,
2788
3
                isl_set_copy(class_domain));
2789
3
    unroll_domain = isl_set_intersect(unroll_domain,
2790
3
          isl_set_copy(domains->schedule_domain));
2791
3
2792
3
    empty = isl_set_is_empty(unroll_domain);
2793
3
    if (empty >= 0 && empty) {
2794
0
      isl_set_free(unroll_domain);
2795
0
      continue;
2796
0
    }
2797
3
2798
3
    class_domain = do_unroll(domains, unroll_domain, class_domain);
2799
3
  }
2800
3
2801
3
  isl_basic_set_list_free(unroll_list);
2802
3
2803
3
  return class_domain;
2804
3
}
2805
2806
/* Try and construct a single basic set that includes the intersection of
2807
 * the schedule domain, the atomic option domain and the class domain.
2808
 * Add the resulting basic set(s) to domains->list and remove them
2809
 * from class_domain.  Return the updated class domain.
2810
 *
2811
 * We construct a single domain rather than trying to combine
2812
 * the schedule domains of individual domains because we are working
2813
 * within a single component so that non-overlapping schedule domains
2814
 * should already have been separated.
2815
 * We do however need to make sure that this single domains is a subset
2816
 * of the class domain so that it would not intersect with any other
2817
 * class domains.  This means that we may end up splitting up the atomic
2818
 * domain in case separation classes are being used.
2819
 *
2820
 * "domain" is the intersection of the schedule domain and the class domain,
2821
 * with inner dimensions projected out.
2822
 */
2823
static __isl_give isl_set *compute_atomic_domain(
2824
  struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2825
84
{
2826
84
  isl_basic_set *bset;
2827
84
  isl_basic_set_list *list;
2828
84
  isl_set *domain, *atomic_domain;
2829
84
  int empty;
2830
84
2831
84
  domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2832
84
  domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2833
84
  domain = isl_set_intersect(domain,
2834
84
        isl_set_copy(domains->schedule_domain));
2835
84
  empty = isl_set_is_empty(domain);
2836
84
  if (empty < 0)
2837
0
    class_domain = isl_set_free(class_domain);
2838
84
  if (empty) {
2839
83
    isl_set_free(domain);
2840
83
    return class_domain;
2841
83
  }
2842
1
2843
1
  domain = isl_ast_build_eliminate(domains->build, domain);
2844
1
  domain = isl_set_coalesce(domain);
2845
1
  bset = isl_set_unshifted_simple_hull(domain);
2846
1
  domain = isl_set_from_basic_set(bset);
2847
1
  atomic_domain = isl_set_copy(domain);
2848
1
  domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2849
1
  class_domain = isl_set_subtract(class_domain, atomic_domain);
2850
1
  domain = isl_set_make_disjoint(domain);
2851
1
  list = isl_basic_set_list_from_set(domain);
2852
1
  domains->list = isl_basic_set_list_concat(domains->list, list);
2853
1
2854
1
  return class_domain;
2855
1
}
2856
2857
/* Split up the schedule domain into uniform basic sets,
2858
 * in the sense that each element in a basic set is associated to
2859
 * elements of the same domains, and add the result to domains->list.
2860
 * Do this for that part of the schedule domain that lies in the
2861
 * intersection of "class_domain" and the separate option domain.
2862
 *
2863
 * "class_domain" may or may not include the constraints
2864
 * of the schedule domain, but this does not make a difference
2865
 * since we are going to intersect it with the domain of the inverse schedule.
2866
 * If it includes schedule domain constraints, then they may involve
2867
 * inner dimensions, but we will eliminate them in separation_domain.
2868
 */
2869
static int compute_separate_domain(struct isl_codegen_domains *domains,
2870
  __isl_keep isl_set *class_domain)
2871
84
{
2872
84
  isl_space *space;
2873
84
  isl_set *domain;
2874
84
  isl_union_map *executed;
2875
84
  isl_basic_set_list *list;
2876
84
  int empty;
2877
84
2878
84
  domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2879
84
  domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2880
84
  executed = isl_union_map_copy(domains->executed);
2881
84
  executed = isl_union_map_intersect_domain(executed,
2882
84
            isl_union_set_from_set(domain));
2883
84
  empty = isl_union_map_is_empty(executed);
2884
84
  if (empty < 0 || empty) {
2885
84
    isl_union_map_free(executed);
2886
84
    return empty < 0 ? 
-10
: 0;
2887
84
  }
2888
0
2889
0
  space = isl_set_get_space(class_domain);
2890
0
  domain = separate_schedule_domains(space, executed, domains->build);
2891
0
2892
0
  list = isl_basic_set_list_from_set(domain);
2893
0
  domains->list = isl_basic_set_list_concat(domains->list, list);
2894
0
2895
0
  return 0;
2896
0
}
2897
2898
/* Split up the domain at the current depth into disjoint
2899
 * basic sets for which code should be generated separately
2900
 * for the given separation class domain.
2901
 *
2902
 * If any separation classes have been defined, then "class_domain"
2903
 * is the domain of the current class and does not refer to inner dimensions.
2904
 * Otherwise, "class_domain" is the universe domain.
2905
 *
2906
 * We first make sure that the class domain is disjoint from
2907
 * previously considered class domains.
2908
 *
2909
 * The separate domains can be computed directly from the "class_domain".
2910
 *
2911
 * The unroll, atomic and remainder domains need the constraints
2912
 * from the schedule domain.
2913
 *
2914
 * For unrolling, the actual schedule domain is needed (with divs that
2915
 * may refer to the current dimension) so that stride detection can be
2916
 * performed.
2917
 *
2918
 * For atomic and remainder domains, inner dimensions and divs involving
2919
 * the current dimensions should be eliminated.
2920
 * In case we are working within a separation class, we need to intersect
2921
 * the result with the current "class_domain" to ensure that the domains
2922
 * are disjoint from those generated from other class domains.
2923
 *
2924
 * The domain that has been made atomic may be larger than specified
2925
 * by the user since it needs to be representable as a single basic set.
2926
 * This possibly larger domain is removed from class_domain by
2927
 * compute_atomic_domain.  It is computed first so that the extended domain
2928
 * would not overlap with any domains computed before.
2929
 * Similary, the unrolled domains may have some constraints removed and
2930
 * may therefore also be larger than specified by the user.
2931
 *
2932
 * If anything is left after handling separate, unroll and atomic,
2933
 * we split it up into basic sets and append the basic sets to domains->list.
2934
 */
2935
static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2936
  __isl_take isl_set *class_domain)
2937
84
{
2938
84
  isl_basic_set_list *list;
2939
84
  isl_set *domain;
2940
84
2941
84
  class_domain = isl_set_subtract(class_domain,
2942
84
          isl_set_copy(domains->done));
2943
84
  domains->done = isl_set_union(domains->done,
2944
84
          isl_set_copy(class_domain));
2945
84
2946
84
  class_domain = compute_atomic_domain(domains, class_domain);
2947
84
  class_domain = compute_unroll_domains(domains, class_domain);
2948
84
2949
84
  domain = isl_set_copy(class_domain);
2950
84
2951
84
  if (compute_separate_domain(domains, domain) < 0)
2952
0
    goto error;
2953
84
  domain = isl_set_subtract(domain,
2954
84
      isl_set_copy(domains->option[isl_ast_loop_separate]));
2955
84
2956
84
  domain = isl_set_intersect(domain,
2957
84
        isl_set_copy(domains->schedule_domain));
2958
84
2959
84
  domain = isl_ast_build_eliminate(domains->build, domain);
2960
84
  domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2961
84
2962
84
  domain = isl_set_coalesce(domain);
2963
84
  domain = isl_set_make_disjoint(domain);
2964
84
2965
84
  list = isl_basic_set_list_from_set(domain);
2966
84
  domains->list = isl_basic_set_list_concat(domains->list, list);
2967
84
2968
84
  isl_set_free(class_domain);
2969
84
2970
84
  return isl_stat_ok;
2971
0
error:
2972
0
  isl_set_free(domain);
2973
0
  isl_set_free(class_domain);
2974
0
  return isl_stat_error;
2975
84
}
2976
2977
/* Split up the domain at the current depth into disjoint
2978
 * basic sets for which code should be generated separately
2979
 * for the separation class identified by "pnt".
2980
 *
2981
 * We extract the corresponding class domain from domains->sep_class,
2982
 * eliminate inner dimensions and pass control to compute_partial_domains.
2983
 */
2984
static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
2985
0
{
2986
0
  struct isl_codegen_domains *domains = user;
2987
0
  isl_set *class_set;
2988
0
  isl_set *domain;
2989
0
  int disjoint;
2990
0
2991
0
  class_set = isl_set_from_point(pnt);
2992
0
  domain = isl_map_domain(isl_map_intersect_range(
2993
0
        isl_map_copy(domains->sep_class), class_set));
2994
0
  domain = isl_ast_build_compute_gist(domains->build, domain);
2995
0
  domain = isl_ast_build_eliminate(domains->build, domain);
2996
0
2997
0
  disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2998
0
  if (disjoint < 0)
2999
0
    return isl_stat_error;
3000
0
  if (disjoint) {
3001
0
    isl_set_free(domain);
3002
0
    return isl_stat_ok;
3003
0
  }
3004
0
3005
0
  return compute_partial_domains(domains, domain);
3006
0
}
3007
3008
/* Extract the domains at the current depth that should be atomic,
3009
 * separated or unrolled and store them in option.
3010
 *
3011
 * The domains specified by the user might overlap, so we make
3012
 * them disjoint by subtracting earlier domains from later domains.
3013
 */
3014
static void compute_domains_init_options(isl_set *option[4],
3015
  __isl_keep isl_ast_build *build)
3016
84
{
3017
84
  enum isl_ast_loop_type type, type2;
3018
84
  isl_set *unroll;
3019
84
3020
84
  for (type = isl_ast_loop_atomic;
3021
336
      type <= isl_ast_loop_separate; 
++type252
) {
3022
252
    option[type] = isl_ast_build_get_option_domain(build, type);
3023
504
    for (type2 = isl_ast_loop_atomic; type2 < type; 
++type2252
)
3024
252
      option[type] = isl_set_subtract(option[type],
3025
252
            isl_set_copy(option[type2]));
3026
252
  }
3027
84
3028
84
  unroll = option[isl_ast_loop_unroll];
3029
84
  unroll = isl_set_coalesce(unroll);
3030
84
  unroll = isl_set_make_disjoint(unroll);
3031
84
  option[isl_ast_loop_unroll] = unroll;
3032
84
}
3033
3034
/* Split up the domain at the current depth into disjoint
3035
 * basic sets for which code should be generated separately,
3036
 * based on the user-specified options.
3037
 * Return the list of disjoint basic sets.
3038
 *
3039
 * There are three kinds of domains that we need to keep track of.
3040
 * - the "schedule domain" is the domain of "executed"
3041
 * - the "class domain" is the domain corresponding to the currrent
3042
 *  separation class
3043
 * - the "option domain" is the domain corresponding to one of the options
3044
 *  atomic, unroll or separate
3045
 *
3046
 * We first consider the individial values of the separation classes
3047
 * and split up the domain for each of them separately.
3048
 * Finally, we consider the remainder.  If no separation classes were
3049
 * specified, then we call compute_partial_domains with the universe
3050
 * "class_domain".  Otherwise, we take the "schedule_domain" as "class_domain",
3051
 * with inner dimensions removed.  We do this because we want to
3052
 * avoid computing the complement of the class domains (i.e., the difference
3053
 * between the universe and domains->done).
3054
 */
3055
static __isl_give isl_basic_set_list *compute_domains(
3056
  __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3057
84
{
3058
84
  struct isl_codegen_domains domains;
3059
84
  isl_ctx *ctx;
3060
84
  isl_set *domain;
3061
84
  isl_union_set *schedule_domain;
3062
84
  isl_set *classes;
3063
84
  isl_space *space;
3064
84
  int n_param;
3065
84
  enum isl_ast_loop_type type;
3066
84
  int empty;
3067
84
3068
84
  if (!executed)
3069
0
    return NULL;
3070
84
3071
84
  ctx = isl_union_map_get_ctx(executed);
3072
84
  domains.list = isl_basic_set_list_alloc(ctx, 0);
3073
84
3074
84
  schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3075
84
  domain = isl_set_from_union_set(schedule_domain);
3076
84
3077
84
  compute_domains_init_options(domains.option, build);
3078
84
3079
84
  domains.sep_class = isl_ast_build_get_separation_class(build);
3080
84
  classes = isl_map_range(isl_map_copy(domains.sep_class));
3081
84
  n_param = isl_set_dim(classes, isl_dim_param);
3082
84
  classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3083
84
3084
84
  space = isl_set_get_space(domain);
3085
84
  domains.build = build;
3086
84
  domains.schedule_domain = isl_set_copy(domain);
3087
84
  domains.executed = executed;
3088
84
  domains.done = isl_set_empty(space);
3089
84
3090
84
  if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3091
0
    domains.list = isl_basic_set_list_free(domains.list);
3092
84
  isl_set_free(classes);
3093
84
3094
84
  empty = isl_set_is_empty(domains.done);
3095
84
  if (empty < 0) {
3096
0
    domains.list = isl_basic_set_list_free(domains.list);
3097
0
    domain = isl_set_free(domain);
3098
84
  } else if (empty) {
3099
84
    isl_set_free(domain);
3100
84
    domain = isl_set_universe(isl_set_get_space(domains.done));
3101
84
  } else {
3102
0
    domain = isl_ast_build_eliminate(build, domain);
3103
0
  }
3104
84
  if (compute_partial_domains(&domains, domain) < 0)
3105
0
    domains.list = isl_basic_set_list_free(domains.list);
3106
84
3107
84
  isl_set_free(domains.schedule_domain);
3108
84
  isl_set_free(domains.done);
3109
84
  isl_map_free(domains.sep_class);
3110
336
  for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; 
++type252
)
3111
252
    isl_set_free(domains.option[type]);
3112
84
3113
84
  return domains.list;
3114
84
}
3115
3116
/* Generate code for a single component, after shifting (if any)
3117
 * has been applied, in case the schedule was specified as a union map.
3118
 *
3119
 * We first split up the domain at the current depth into disjoint
3120
 * basic sets based on the user-specified options.
3121
 * Then we generated code for each of them and concatenate the results.
3122
 */
3123
static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3124
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3125
84
{
3126
84
  isl_basic_set_list *domain_list;
3127
84
  isl_ast_graft_list *list = NULL;
3128
84
3129
84
  domain_list = compute_domains(executed, build);
3130
84
  list = generate_parallel_domains(domain_list, executed, build);
3131
84
3132
84
  isl_basic_set_list_free(domain_list);
3133
84
  isl_union_map_free(executed);
3134
84
  isl_ast_build_free(build);
3135
84
3136
84
  return list;
3137
84
}
3138
3139
/* Generate code for a single component, after shifting (if any)
3140
 * has been applied, in case the schedule was specified as a schedule tree
3141
 * and the separate option was specified.
3142
 *
3143
 * We perform separation on the domain of "executed" and then generate
3144
 * an AST for each of the resulting disjoint basic sets.
3145
 */
3146
static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3147
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3148
24
{
3149
24
  isl_space *space;
3150
24
  isl_set *domain;
3151
24
  isl_basic_set_list *domain_list;
3152
24
  isl_ast_graft_list *list;
3153
24
3154
24
  space = isl_ast_build_get_space(build, 1);
3155
24
  domain = separate_schedule_domains(space,
3156
24
          isl_union_map_copy(executed), build);
3157
24
  domain_list = isl_basic_set_list_from_set(domain);
3158
24
3159
24
  list = generate_parallel_domains(domain_list, executed, build);
3160
24
3161
24
  isl_basic_set_list_free(domain_list);
3162
24
  isl_union_map_free(executed);
3163
24
  isl_ast_build_free(build);
3164
24
3165
24
  return list;
3166
24
}
3167
3168
/* Internal data structure for generate_shifted_component_tree_unroll.
3169
 *
3170
 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3171
 * "list" collects the constructs grafts.
3172
 */
3173
struct isl_ast_unroll_tree_data {
3174
  isl_union_map *executed;
3175
  isl_ast_build *build;
3176
  isl_ast_graft_list *list;
3177
};
3178
3179
/* Initialize data->list to a list of "n" elements.
3180
 */
3181
static int init_unroll_tree(int n, void *user)
3182
2.31k
{
3183
2.31k
  struct isl_ast_unroll_tree_data *data = user;
3184
2.31k
  isl_ctx *ctx;
3185
2.31k
3186
2.31k
  ctx = isl_ast_build_get_ctx(data->build);
3187
2.31k
  data->list = isl_ast_graft_list_alloc(ctx, n);
3188
2.31k
3189
2.31k
  return 0;
3190
2.31k
}
3191
3192
/* Given an iteration of an unrolled domain represented by "bset",
3193
 * generate the corresponding AST and add the result to data->list.
3194
 */
3195
static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3196
4.40k
{
3197
4.40k
  struct isl_ast_unroll_tree_data *data = user;
3198
4.40k
3199
4.40k
  data->list = add_node(data->list, isl_union_map_copy(data->executed),
3200
4.40k
        bset, isl_ast_build_copy(data->build));
3201
4.40k
3202
4.40k
  return 0;
3203
4.40k
}
3204
3205
/* Generate code for a single component, after shifting (if any)
3206
 * has been applied, in case the schedule was specified as a schedule tree
3207
 * and the unroll option was specified.
3208
 *
3209
 * We call foreach_iteration to iterate over the individual values and
3210
 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3211
 */
3212
static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3213
  __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3214
  __isl_take isl_ast_build *build)
3215
2.31k
{
3216
2.31k
  struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3217
2.31k
3218
2.31k
  if (foreach_iteration(domain, build, &init_unroll_tree,
3219
2.31k
        &do_unroll_tree_iteration, &data) < 0)
3220
0
    data.list = isl_ast_graft_list_free(data.list);
3221
2.31k
3222
2.31k
  isl_union_map_free(executed);
3223
2.31k
  isl_ast_build_free(build);
3224
2.31k
3225
2.31k
  return data.list;
3226
2.31k
}
3227
3228
/* Does "domain" involve a disjunction that is purely based on
3229
 * constraints involving only outer dimension?
3230
 *
3231
 * In particular, is there a disjunction such that the constraints
3232
 * involving the current and later dimensions are the same over
3233
 * all the disjuncts?
3234
 */
3235
static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3236
  __isl_keep isl_ast_build *build)
3237
801
{
3238
801
  isl_basic_set *hull;
3239
801
  isl_set *shared, *inner;
3240
801
  isl_bool equal;
3241
801
  int depth, dim;
3242
801
3243
801
  if (isl_set_n_basic_set(domain) <= 1)
3244
757
    return isl_bool_false;
3245
44
3246
44
  inner = isl_set_copy(domain);
3247
44
  depth = isl_ast_build_get_depth(build);
3248
44
  dim = isl_set_dim(inner, isl_dim_set);
3249
44
  inner = isl_set_drop_constraints_not_involving_dims(inner,
3250
44
              isl_dim_set, depth, dim - depth);
3251
44
  hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3252
44
  shared = isl_set_from_basic_set(hull);
3253
44
  equal = isl_set_plain_is_equal(inner, shared);
3254
44
  isl_set_free(inner);
3255
44
  isl_set_free(shared);
3256
44
3257
44
  return equal;
3258
44
}
3259
3260
/* Generate code for a single component, after shifting (if any)
3261
 * has been applied, in case the schedule was specified as a schedule tree.
3262
 * In particular, handle the base case where there is either no isolated
3263
 * set or we are within the isolated set (in which case "isolated" is set)
3264
 * or the iterations that precede or follow the isolated set.
3265
 *
3266
 * The schedule domain is broken up or combined into basic sets
3267
 * according to the AST generation option specified in the current
3268
 * schedule node, which may be either atomic, separate, unroll or
3269
 * unspecified.  If the option is unspecified, then we currently simply
3270
 * split the schedule domain into disjoint basic sets.
3271
 *
3272
 * In case the separate option is specified, the AST generation is
3273
 * handled by generate_shifted_component_tree_separate.
3274
 * In the other cases, we need the global schedule domain.
3275
 * In the unroll case, the AST generation is then handled by
3276
 * generate_shifted_component_tree_unroll which needs the actual
3277
 * schedule domain (with divs that may refer to the current dimension)
3278
 * so that stride detection can be performed.
3279
 * In the atomic or unspecified case, inner dimensions and divs involving
3280
 * the current dimensions should be eliminated.
3281
 * The result is then either combined into a single basic set or
3282
 * split up into disjoint basic sets.
3283
 * Finally an AST is generated for each basic set and the results are
3284
 * concatenated.
3285
 *
3286
 * If the schedule domain involves a disjunction that is purely based on
3287
 * constraints involving only outer dimension, then it is treated as
3288
 * if atomic was specified.  This ensures that only a single loop
3289
 * is generated instead of a sequence of identical loops with
3290
 * different guards.
3291
 */
3292
static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3293
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3294
  int isolated)
3295
3.13k
{
3296
3.13k
  isl_bool outer_disjunction;
3297
3.13k
  isl_union_set *schedule_domain;
3298
3.13k
  isl_set *domain;
3299
3.13k
  isl_basic_set_list *domain_list;
3300
3.13k
  isl_ast_graft_list *list;
3301
3.13k
  enum isl_ast_loop_type type;
3302
3.13k
3303
3.13k
  type = isl_ast_build_get_loop_type(build, isolated);
3304
3.13k
  if (type < 0)
3305
0
    goto error;
3306
3.13k
3307
3.13k
  if (type == isl_ast_loop_separate)
3308
24
    return generate_shifted_component_tree_separate(executed,
3309
24
                build);
3310
3.11k
3311
3.11k
  schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3312
3.11k
  domain = isl_set_from_union_set(schedule_domain);
3313
3.11k
3314
3.11k
  if (type == isl_ast_loop_unroll)
3315
2.31k
    return generate_shifted_component_tree_unroll(executed, domain,
3316
2.31k
                build);
3317
801
3318
801
  domain = isl_ast_build_eliminate(build, domain);
3319
801
  domain = isl_set_coalesce(domain);
3320
801
3321
801
  outer_disjunction = has_pure_outer_disjunction(domain, build);
3322
801
  if (outer_disjunction < 0)
3323
0
    domain = isl_set_free(domain);
3324
801
3325
801
  if (outer_disjunction || 
type == isl_ast_loop_atomic786
) {
3326
16
    isl_basic_set *hull;
3327
16
    hull = isl_set_unshifted_simple_hull(domain);
3328
16
    domain_list = isl_basic_set_list_from_basic_set(hull);
3329
785
  } else {
3330
785
    domain = isl_set_make_disjoint(domain);
3331
785
    domain_list = isl_basic_set_list_from_set(domain);
3332
785
  }
3333
801
3334
801
  list = generate_parallel_domains(domain_list, executed, build);
3335
801
3336
801
  isl_basic_set_list_free(domain_list);
3337
801
  isl_union_map_free(executed);
3338
801
  isl_ast_build_free(build);
3339
801
3340
801
  return list;
3341
0
error:
3342
0
  isl_union_map_free(executed);
3343
0
  isl_ast_build_free(build);
3344
0
  return NULL;
3345
801
}
3346
3347
/* Extract out the disjunction imposed by "domain" on the outer
3348
 * schedule dimensions.
3349
 *
3350
 * In particular, remove all inner dimensions from "domain" (including
3351
 * the current dimension) and then remove the constraints that are shared
3352
 * by all disjuncts in the result.
3353
 */
3354
static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3355
  __isl_keep isl_ast_build *build)
3356
796
{
3357
796
  isl_set *hull;
3358
796
  int depth, dim;
3359
796
3360
796
  domain = isl_ast_build_specialize(build, domain);
3361
796
  depth = isl_ast_build_get_depth(build);
3362
796
  dim = isl_set_dim(domain, isl_dim_set);
3363
796
  domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3364
796
  domain = isl_set_remove_unknown_divs(domain);
3365
796
  hull = isl_set_copy(domain);
3366
796
  hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3367
796
  domain = isl_set_gist(domain, hull);
3368
796
3369
796
  return domain;
3370
796
}
3371
3372
/* Add "guard" to the grafts in "list".
3373
 * "build" is the outer AST build, while "sub_build" includes "guard"
3374
 * in its generated domain.
3375
 *
3376
 * First combine the grafts into a single graft and then add the guard.
3377
 * If the list is empty, or if some error occurred, then simply return
3378
 * the list.
3379
 */
3380
static __isl_give isl_ast_graft_list *list_add_guard(
3381
  __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3382
  __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3383
796
{
3384
796
  isl_ast_graft *graft;
3385
796
3386
796
  list = isl_ast_graft_list_fuse(list, sub_build);
3387
796
3388
796
  if (isl_ast_graft_list_n_ast_graft(list) != 1)
3389
789
    return list;
3390
7
3391
7
  graft = isl_ast_graft_list_get_ast_graft(list, 0);
3392
7
  graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3393
7
  list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3394
7
3395
7
  return list;
3396
7
}
3397
3398
/* Generate code for a single component, after shifting (if any)
3399
 * has been applied, in case the schedule was specified as a schedule tree.
3400
 * In particular, do so for the specified subset of the schedule domain.
3401
 *
3402
 * If we are outside of the isolated part, then "domain" may include
3403
 * a disjunction.  Explicitly generate this disjunction at this point
3404
 * instead of relying on the disjunction getting hoisted back up
3405
 * to this level.
3406
 */
3407
static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3408
  __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3409
  __isl_keep isl_ast_build *build, int isolated)
3410
3.65k
{
3411
3.65k
  isl_union_set *uset;
3412
3.65k
  isl_ast_graft_list *list;
3413
3.65k
  isl_ast_build *sub_build;
3414
3.65k
  int empty;
3415
3.65k
3416
3.65k
  uset = isl_union_set_from_set(isl_set_copy(domain));
3417
3.65k
  executed = isl_union_map_copy(executed);
3418
3.65k
  executed = isl_union_map_intersect_domain(executed, uset);
3419
3.65k
  empty = isl_union_map_is_empty(executed);
3420
3.65k
  if (empty < 0)
3421
0
    goto error;
3422
3.65k
  if (empty) {
3423
1.94k
    isl_ctx *ctx;
3424
1.94k
    isl_union_map_free(executed);
3425
1.94k
    isl_set_free(domain);
3426
1.94k
    ctx = isl_ast_build_get_ctx(build);
3427
1.94k
    return isl_ast_graft_list_alloc(ctx, 0);
3428
1.94k
  }
3429
1.70k
3430
1.70k
  sub_build = isl_ast_build_copy(build);
3431
1.70k
  if (!isolated) {
3432
796
    domain = extract_disjunction(domain, build);
3433
796
    sub_build = isl_ast_build_restrict_generated(sub_build,
3434
796
              isl_set_copy(domain));
3435
796
  }
3436
1.70k
  list = generate_shifted_component_tree_base(executed,
3437
1.70k
        isl_ast_build_copy(sub_build), isolated);
3438
1.70k
  if (!isolated)
3439
796
    list = list_add_guard(list, domain, build, sub_build);
3440
1.70k
  isl_ast_build_free(sub_build);
3441
1.70k
  isl_set_free(domain);
3442
1.70k
  return list;
3443
0
error:
3444
0
  isl_union_map_free(executed);
3445
0
  isl_set_free(domain);
3446
0
  return NULL;
3447
1.70k
}
3448
3449
/* Generate code for a single component, after shifting (if any)
3450
 * has been applied, in case the schedule was specified as a schedule tree.
3451
 * In particular, do so for the specified sequence of subsets
3452
 * of the schedule domain, "before", "isolated", "after" and "other",
3453
 * where only the "isolated" part is considered to be isolated.
3454
 */
3455
static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3456
  __isl_take isl_union_map *executed, __isl_take isl_set *before,
3457
  __isl_take isl_set *isolated, __isl_take isl_set *after,
3458
  __isl_take isl_set *other, __isl_take isl_ast_build *build)
3459
913
{
3460
913
  isl_ast_graft_list *list, *res;
3461
913
3462
913
  res = generate_shifted_component_tree_part(executed, before, build, 0);
3463
913
  list = generate_shifted_component_tree_part(executed, isolated,
3464
913
                build, 1);
3465
913
  res = isl_ast_graft_list_concat(res, list);
3466
913
  list = generate_shifted_component_tree_part(executed, after, build, 0);
3467
913
  res = isl_ast_graft_list_concat(res, list);
3468
913
  list = generate_shifted_component_tree_part(executed, other, build, 0);
3469
913
  res = isl_ast_graft_list_concat(res, list);
3470
913
3471
913
  isl_union_map_free(executed);
3472
913
  isl_ast_build_free(build);
3473
913
3474
913
  return res;
3475
913
}
3476
3477
/* Does "set" intersect "first", but not "second"?
3478
 */
3479
static isl_bool only_intersects_first(__isl_keep isl_set *set,
3480
  __isl_keep isl_set *first, __isl_keep isl_set *second)
3481
913
{
3482
913
  isl_bool disjoint;
3483
913
3484
913
  disjoint = isl_set_is_disjoint(set, first);
3485
913
  if (disjoint < 0)
3486
0
    return isl_bool_error;
3487
913
  if (disjoint)
3488
906
    return isl_bool_false;
3489
7
3490
7
  return isl_set_is_disjoint(set, second);
3491
7
}
3492
3493
/* Generate code for a single component, after shifting (if any)
3494
 * has been applied, in case the schedule was specified as a schedule tree.
3495
 * In particular, do so in case of isolation where there is
3496
 * only an "isolated" part and an "after" part.
3497
 * "dead1" and "dead2" are freed by this function in order to simplify
3498
 * the caller.
3499
 *
3500
 * The "before" and "other" parts are set to empty sets.
3501
 */
3502
static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3503
  __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3504
  __isl_take isl_set *after, __isl_take isl_ast_build *build,
3505
  __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3506
7
{
3507
7
  isl_set *empty;
3508
7
3509
7
  empty = isl_set_empty(isl_set_get_space(after));
3510
7
  isl_set_free(dead1);
3511
7
  isl_set_free(dead2);
3512
7
  return generate_shifted_component_parts(executed, isl_set_copy(empty),
3513
7
            isolated, after, empty, build);
3514
7
}
3515
3516
/* Generate code for a single component, after shifting (if any)
3517
 * has been applied, in case the schedule was specified as a schedule tree.
3518
 *
3519
 * We first check if the user has specified an isolated schedule domain
3520
 * and that we are not already outside of this isolated schedule domain.
3521
 * If so, we break up the schedule domain into iterations that
3522
 * precede the isolated domain, the isolated domain itself,
3523
 * the iterations that follow the isolated domain and
3524
 * the remaining iterations (those that are incomparable
3525
 * to the isolated domain).
3526
 * We generate an AST for each piece and concatenate the results.
3527
 *
3528
 * If the isolated domain is not convex, then it is replaced
3529
 * by a convex superset to ensure that the sets of preceding and
3530
 * following iterations are properly defined and, in particular,
3531
 * that there are no intermediate iterations that do not belong
3532
 * to the isolated domain.
3533
 *
3534
 * In the special case where at least one element of the schedule
3535
 * domain that does not belong to the isolated domain needs
3536
 * to be scheduled after this isolated domain, but none of those
3537
 * elements need to be scheduled before, break up the schedule domain
3538
 * in only two parts, the isolated domain, and a part that will be
3539
 * scheduled after the isolated domain.
3540
 *
3541
 * If no isolated set has been specified, then we generate an
3542
 * AST for the entire inverse schedule.
3543
 */
3544
static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3545
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3546
2.33k
{
3547
2.33k
  int i, depth;
3548
2.33k
  int empty, has_isolate;
3549
2.33k
  isl_space *space;
3550
2.33k
  isl_union_set *schedule_domain;
3551
2.33k
  isl_set *domain;
3552
2.33k
  isl_basic_set *hull;
3553
2.33k
  isl_set *isolated, *before, *after, *test;
3554
2.33k
  isl_map *gt, *lt;
3555
2.33k
  isl_bool pure;
3556
2.33k
3557
2.33k
  build = isl_ast_build_extract_isolated(build);
3558
2.33k
  has_isolate = isl_ast_build_has_isolated(build);
3559
2.33k
  if (has_isolate < 0)
3560
0
    executed = isl_union_map_free(executed);
3561
2.33k
  else if (!has_isolate)
3562
922
    return generate_shifted_component_tree_base(executed, build, 0);
3563
1.41k
3564
1.41k
  schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3565
1.41k
  domain = isl_set_from_union_set(schedule_domain);
3566
1.41k
3567
1.41k
  isolated = isl_ast_build_get_isolated(build);
3568
1.41k
  isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3569
1.41k
  test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3570
1.41k
  empty = isl_set_is_empty(test);
3571
1.41k
  isl_set_free(test);
3572
1.41k
  if (empty < 0)
3573
0
    goto error;
3574
1.41k
  if (empty) {
3575
504
    isl_set_free(isolated);
3576
504
    isl_set_free(domain);
3577
504
    return generate_shifted_component_tree_base(executed, build, 0);
3578
504
  }
3579
913
  isolated = isl_ast_build_eliminate(build, isolated);
3580
913
  hull = isl_set_unshifted_simple_hull(isolated);
3581
913
  isolated = isl_set_from_basic_set(hull);
3582
913
3583
913
  depth = isl_ast_build_get_depth(build);
3584
913
  space = isl_space_map_from_set(isl_set_get_space(isolated));
3585
913
  gt = isl_map_universe(space);
3586
7.95k
  for (i = 0; i < depth; 
++i7.03k
)
3587
7.03k
    gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3588
913
  gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3589
913
  lt = isl_map_reverse(isl_map_copy(gt));
3590
913
  before = isl_set_apply(isl_set_copy(isolated), gt);
3591
913
  after = isl_set_apply(isl_set_copy(isolated), lt);
3592
913
3593
913
  domain = isl_set_subtract(domain, isl_set_copy(isolated));
3594
913
  pure = only_intersects_first(domain, after, before);
3595
913
  if (pure < 0)
3596
0
    executed = isl_union_map_free(executed);
3597
913
  else if (pure)
3598
7
    return generate_shifted_component_only_after(executed, isolated,
3599
7
            domain, build, before, after);
3600
906
  domain = isl_set_subtract(domain, isl_set_copy(before));
3601
906
  domain = isl_set_subtract(domain, isl_set_copy(after));
3602
906
  after = isl_set_subtract(after, isl_set_copy(isolated));
3603
906
  after = isl_set_subtract(after, isl_set_copy(before));
3604
906
  before = isl_set_subtract(before, isl_set_copy(isolated));
3605
906
3606
906
  return generate_shifted_component_parts(executed, before, isolated,
3607
906
            after, domain, build);
3608
0
error:
3609
0
  isl_set_free(domain);
3610
0
  isl_set_free(isolated);
3611
0
  isl_union_map_free(executed);
3612
0
  isl_ast_build_free(build);
3613
0
  return NULL;
3614
906
}
3615
3616
/* Generate code for a single component, after shifting (if any)
3617
 * has been applied.
3618
 *
3619
 * Call generate_shifted_component_tree or generate_shifted_component_flat
3620
 * depending on whether the schedule was specified as a schedule tree.
3621
 */
3622
static __isl_give isl_ast_graft_list *generate_shifted_component(
3623
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3624
2.42k
{
3625
2.42k
  if (isl_ast_build_has_schedule_node(build))
3626
2.33k
    return generate_shifted_component_tree(executed, build);
3627
84
  else
3628
84
    return generate_shifted_component_flat(executed, build);
3629
2.42k
}
3630
3631
struct isl_set_map_pair {
3632
  isl_set *set;
3633
  isl_map *map;
3634
};
3635
3636
/* Given an array "domain" of isl_set_map_pairs and an array "order"
3637
 * of indices into the "domain" array,
3638
 * return the union of the "map" fields of the elements
3639
 * indexed by the first "n" elements of "order".
3640
 */
3641
static __isl_give isl_union_map *construct_component_executed(
3642
  struct isl_set_map_pair *domain, int *order, int n)
3643
164
{
3644
164
  int i;
3645
164
  isl_map *map;
3646
164
  isl_union_map *executed;
3647
164
3648
164
  map = isl_map_copy(domain[order[0]].map);
3649
164
  executed = isl_union_map_from_map(map);
3650
311
  for (i = 1; i < n; 
++i147
) {
3651
147
    map = isl_map_copy(domain[order[i]].map);
3652
147
    executed = isl_union_map_add_map(executed, map);
3653
147
  }
3654
164
3655
164
  return executed;
3656
164
}
3657
3658
/* Generate code for a single component, after shifting (if any)
3659
 * has been applied.
3660
 *
3661
 * The component inverse schedule is specified as the "map" fields
3662
 * of the elements of "domain" indexed by the first "n" elements of "order".
3663
 */
3664
static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3665
  struct isl_set_map_pair *domain, int *order, int n,
3666
  __isl_take isl_ast_build *build)
3667
164
{
3668
164
  isl_union_map *executed;
3669
164
3670
164
  executed = construct_component_executed(domain, order, n);
3671
164
  return generate_shifted_component(executed, build);
3672
164
}
3673
3674
/* Does set dimension "pos" of "set" have an obviously fixed value?
3675
 */
3676
static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3677
406
{
3678
406
  int fixed;
3679
406
  isl_val *v;
3680
406
3681
406
  v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3682
406
  if (!v)
3683
0
    return -1;
3684
406
  fixed = !isl_val_is_nan(v);
3685
406
  isl_val_free(v);
3686
406
3687
406
  return fixed;
3688
406
}
3689
3690
/* Given an array "domain" of isl_set_map_pairs and an array "order"
3691
 * of indices into the "domain" array,
3692
 * do all (except for at most one) of the "set" field of the elements
3693
 * indexed by the first "n" elements of "order" have a fixed value
3694
 * at position "depth"?
3695
 */
3696
static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3697
  int *order, int n, int depth)
3698
85
{
3699
85
  int i;
3700
85
  int non_fixed = -1;
3701
85
3702
183
  for (i = 0; i < n; 
++i98
) {
3703
175
    int f;
3704
175
3705
175
    f = dim_is_fixed(domain[order[i]].set, depth);
3706
175
    if (f < 0)
3707
0
      return -1;
3708
175
    if (f)
3709
16
      continue;
3710
159
    if (non_fixed >= 0)
3711
77
      return 0;
3712
82
    non_fixed = i;
3713
82
  }
3714
85
3715
85
  
return 18
;
3716
85
}
3717
3718
/* Given an array "domain" of isl_set_map_pairs and an array "order"
3719
 * of indices into the "domain" array,
3720
 * eliminate the inner dimensions from the "set" field of the elements
3721
 * indexed by the first "n" elements of "order", provided the current
3722
 * dimension does not have a fixed value.
3723
 *
3724
 * Return the index of the first element in "order" with a corresponding
3725
 * "set" field that does not have an (obviously) fixed value.
3726
 */
3727
static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3728
  int *order, int n, int depth, __isl_keep isl_ast_build *build)
3729
77
{
3730
77
  int i;
3731
77
  int base = -1;
3732
77
3733
295
  for (i = n - 1; i >= 0; 
--i218
) {
3734
218
    int f;
3735
218
    f = dim_is_fixed(domain[order[i]].set, depth);
3736
218
    if (f < 0)
3737
0
      return -1;
3738
218
    if (f)
3739
1
      continue;
3740
217
    domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3741
217
              domain[order[i]].set);
3742
217
    base = i;
3743
217
  }
3744
77
3745
77
  return base;
3746
77
}
3747
3748
/* Given an array "domain" of isl_set_map_pairs and an array "order"
3749
 * of indices into the "domain" array,
3750
 * find the element of "domain" (amongst those indexed by the first "n"
3751
 * elements of "order") with the "set" field that has the smallest
3752
 * value for the current iterator.
3753
 *
3754
 * Note that the domain with the smallest value may depend on the parameters
3755
 * and/or outer loop dimension.  Since the result of this function is only
3756
 * used as heuristic, we only make a reasonable attempt at finding the best
3757
 * domain, one that should work in case a single domain provides the smallest
3758
 * value for the current dimension over all values of the parameters
3759
 * and outer dimensions.
3760
 *
3761
 * In particular, we compute the smallest value of the first domain
3762
 * and replace it by that of any later domain if that later domain
3763
 * has a smallest value that is smaller for at least some value
3764
 * of the parameters and outer dimensions.
3765
 */
3766
static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3767
  __isl_keep isl_ast_build *build)
3768
3
{
3769
3
  int i;
3770
3
  isl_map *min_first;
3771
3
  int first = 0;
3772
3
3773
3
  min_first = isl_ast_build_map_to_iterator(build,
3774
3
          isl_set_copy(domain[order[0]].set));
3775
3
  min_first = isl_map_lexmin(min_first);
3776
3
3777
9
  for (i = 1; i < n; 
++i6
) {
3778
6
    isl_map *min, *test;
3779
6
    int empty;
3780
6
3781
6
    min = isl_ast_build_map_to_iterator(build,
3782
6
          isl_set_copy(domain[order[i]].set));
3783
6
    min = isl_map_lexmin(min);
3784
6
    test = isl_map_copy(min);
3785
6
    test = isl_map_apply_domain(isl_map_copy(min_first), test);
3786
6
    test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3787
6
    empty = isl_map_is_empty(test);
3788
6
    isl_map_free(test);
3789
6
    if (empty >= 0 && !empty) {
3790
1
      isl_map_free(min_first);
3791
1
      first = i;
3792
1
      min_first = min;
3793
1
    } else
3794
5
      isl_map_free(min);
3795
6
3796
6
    if (empty < 0)
3797
0
      break;
3798
6
  }
3799
3
3800
3
  isl_map_free(min_first);
3801
3
3802
3
  return i < n ? 
-10
: first;
3803
3
}
3804
3805
/* Construct a shifted inverse schedule based on the original inverse schedule,
3806
 * the stride and the offset.
3807
 *
3808
 * The original inverse schedule is specified as the "map" fields
3809
 * of the elements of "domain" indexed by the first "n" elements of "order".
3810
 *
3811
 * "stride" and "offset" are such that the difference
3812
 * between the values of the current dimension of domain "i"
3813
 * and the values of the current dimension for some reference domain are
3814
 * equal to
3815
 *
3816
 *  stride * integer + offset[i]
3817
 *
3818
 * Moreover, 0 <= offset[i] < stride.
3819
 *
3820
 * For each domain, we create a map
3821
 *
3822
 *  { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3823
 *
3824
 * where j refers to the current dimension and the other dimensions are
3825
 * unchanged, and apply this map to the original schedule domain.
3826
 *
3827
 * For example, for the original schedule
3828
 *
3829
 *  { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3830
 *
3831
 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3832
 * we apply the mapping
3833
 *
3834
 *  { [j] -> [j, 0] }
3835
 *
3836
 * to the schedule of the "A" domain and the mapping
3837
 *
3838
 *  { [j - 1] -> [j, 1] }
3839
 *
3840
 * to the schedule of the "B" domain.
3841
 *
3842
 *
3843
 * Note that after the transformation, the differences between pairs
3844
 * of values of the current dimension over all domains are multiples
3845
 * of stride and that we have therefore exposed the stride.
3846
 *
3847
 *
3848
 * To see that the mapping preserves the lexicographic order,
3849
 * first note that each of the individual maps above preserves the order.
3850
 * If the value of the current iterator is j1 in one domain and j2 in another,
3851
 * then if j1 = j2, we know that the same map is applied to both domains
3852
 * and the order is preserved.
3853
 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3854
 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3855
 *
3856
 *  j1 - c1 < j2 - c2
3857
 *
3858
 * and the order is preserved.
3859
 * If c1 < c2, then we know
3860
 *
3861
 *  0 <= c2 - c1 < s
3862
 *
3863
 * We also have
3864
 *
3865
 *  j2 - j1 = n * s + r
3866
 *
3867
 * with n >= 0 and 0 <= r < s.
3868
 * In other words, r = c2 - c1.
3869
 * If n > 0, then
3870
 *
3871
 *  j1 - c1 < j2 - c2
3872
 *
3873
 * If n = 0, then
3874
 *
3875
 *  j1 - c1 = j2 - c2
3876
 *
3877
 * and so
3878
 *
3879
 *  (j1 - c1, c1) << (j2 - c2, c2)
3880
 *
3881
 * with "<<" the lexicographic order, proving that the order is preserved
3882
 * in all cases.
3883
 */
3884
static __isl_give isl_union_map *construct_shifted_executed(
3885
  struct isl_set_map_pair *domain, int *order, int n,
3886
  __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3887
  __isl_take isl_ast_build *build)
3888
3
{
3889
3
  int i;
3890
3
  isl_union_map *executed;
3891
3
  isl_space *space;
3892
3
  isl_map *map;
3893
3
  int depth;
3894
3
  isl_constraint *c;
3895
3
3896
3
  depth = isl_ast_build_get_depth(build);
3897
3
  space = isl_ast_build_get_space(build, 1);
3898
3
  executed = isl_union_map_empty(isl_space_copy(space));
3899
3
  space = isl_space_map_from_set(space);
3900
3
  map = isl_map_identity(isl_space_copy(space));
3901
3
  map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3902
3
  map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3903
3
  space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3904
3
3905
3
  c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3906
3
  c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3907
3
  c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3908
3
3909
12
  for (i = 0; i < n; 
++i9
) {
3910
9
    isl_map *map_i;
3911
9
    isl_val *v;
3912
9
3913
9
    v = isl_multi_val_get_val(offset, i);
3914
9
    if (!v)
3915
0
      break;
3916
9
    map_i = isl_map_copy(map);
3917
9
    map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3918
9
          isl_val_copy(v));
3919
9
    v = isl_val_neg(v);
3920
9
    c = isl_constraint_set_constant_val(c, v);
3921
9
    map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3922
9
3923
9
    map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3924
9
            map_i);
3925
9
    executed = isl_union_map_add_map(executed, map_i);
3926
9
  }
3927
3
3928
3
  isl_constraint_free(c);
3929
3
  isl_map_free(map);
3930
3
3931
3
  if (i < n)
3932
0
    executed = isl_union_map_free(executed);
3933
3
3934
3
  return executed;
3935
3
}
3936
3937
/* Generate code for a single component, after exposing the stride,
3938
 * given that the schedule domain is "shifted strided".
3939
 *
3940
 * The component inverse schedule is specified as the "map" fields
3941
 * of the elements of "domain" indexed by the first "n" elements of "order".
3942
 *
3943
 * The schedule domain being "shifted strided" means that the differences
3944
 * between the values of the current dimension of domain "i"
3945
 * and the values of the current dimension for some reference domain are
3946
 * equal to
3947
 *
3948
 *  stride * integer + offset[i]
3949
 *
3950
 * We first look for the domain with the "smallest" value for the current
3951
 * dimension and adjust the offsets such that the offset of the "smallest"
3952
 * domain is equal to zero.  The other offsets are reduced modulo stride.
3953
 *
3954
 * Based on this information, we construct a new inverse schedule in
3955
 * construct_shifted_executed that exposes the stride.
3956
 * Since this involves the introduction of a new schedule dimension,
3957
 * the build needs to be changed accordingly.
3958
 * After computing the AST, the newly introduced dimension needs
3959
 * to be removed again from the list of grafts.  We do this by plugging
3960
 * in a mapping that represents the new schedule domain in terms of the
3961
 * old schedule domain.
3962
 */
3963
static __isl_give isl_ast_graft_list *generate_shift_component(
3964
  struct isl_set_map_pair *domain, int *order, int n,
3965
  __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3966
  __isl_take isl_ast_build *build)
3967
3
{
3968
3
  isl_ast_graft_list *list;
3969
3
  int first;
3970
3
  int depth;
3971
3
  isl_val *val;
3972
3
  isl_multi_val *mv;
3973
3
  isl_space *space;
3974
3
  isl_multi_aff *ma, *zero;
3975
3
  isl_union_map *executed;
3976
3
3977
3
  depth = isl_ast_build_get_depth(build);
3978
3
3979
3
  first = first_offset(domain, order, n, build);
3980
3
  if (first < 0)
3981
0
    goto error;
3982
3
3983
3
  mv = isl_multi_val_copy(offset);
3984
3
  val = isl_multi_val_get_val(offset, first);
3985
3
  val = isl_val_neg(val);
3986
3
  mv = isl_multi_val_add_val(mv, val);
3987
3
  mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3988
3
3989
3
  executed = construct_shifted_executed(domain, order, n, stride, mv,
3990
3
            build);
3991
3
  space = isl_ast_build_get_space(build, 1);
3992
3
  space = isl_space_map_from_set(space);
3993
3
  ma = isl_multi_aff_identity(isl_space_copy(space));
3994
3
  space = isl_space_from_domain(isl_space_domain(space));
3995
3
  space = isl_space_add_dims(space, isl_dim_out, 1);
3996
3
  zero = isl_multi_aff_zero(space);
3997
3
  ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3998
3
  build = isl_ast_build_insert_dim(build, depth + 1);
3999
3
  list = generate_shifted_component(executed, build);
4000
3
4001
3
  list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4002
3
4003
3
  isl_multi_val_free(mv);
4004
3
4005
3
  return list;
4006
0
error:
4007
0
  isl_ast_build_free(build);
4008
0
  return NULL;
4009
3
}
4010
4011
/* Does any node in the schedule tree rooted at the current schedule node
4012
 * of "build" depend on outer schedule nodes?
4013
 */
4014
static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4015
76
{
4016
76
  isl_schedule_node *node;
4017
76
  int dependent = 0;
4018
76
4019
76
  node = isl_ast_build_get_schedule_node(build);
4020
76
  dependent = isl_schedule_node_is_subtree_anchored(node);
4021
76
  isl_schedule_node_free(node);
4022
76
4023
76
  return dependent;
4024
76
}
4025
4026
/* Generate code for a single component.
4027
 *
4028
 * The component inverse schedule is specified as the "map" fields
4029
 * of the elements of "domain" indexed by the first "n" elements of "order".
4030
 *
4031
 * This function may modify the "set" fields of "domain".
4032
 *
4033
 * Before proceeding with the actual code generation for the component,
4034
 * we first check if there are any "shifted" strides, meaning that
4035
 * the schedule domains of the individual domains are all strided,
4036
 * but that they have different offsets, resulting in the union
4037
 * of schedule domains not being strided anymore.
4038
 *
4039
 * The simplest example is the schedule
4040
 *
4041
 *  { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4042
 *
4043
 * Both schedule domains are strided, but their union is not.
4044
 * This function detects such cases and then rewrites the schedule to
4045
 *
4046
 *  { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4047
 *
4048
 * In the new schedule, the schedule domains have the same offset (modulo
4049
 * the stride), ensuring that the union of schedule domains is also strided.
4050
 *
4051
 *
4052
 * If there is only a single domain in the component, then there is
4053
 * nothing to do.   Similarly, if the current schedule dimension has
4054
 * a fixed value for almost all domains then there is nothing to be done.
4055
 * In particular, we need at least two domains where the current schedule
4056
 * dimension does not have a fixed value.
4057
 * Finally, in case of a schedule map input,
4058
 * if any of the options refer to the current schedule dimension,
4059
 * then we bail out as well.  It would be possible to reformulate the options
4060
 * in terms of the new schedule domain, but that would introduce constraints
4061
 * that separate the domains in the options and that is something we would
4062
 * like to avoid.
4063
 * In the case of a schedule tree input, we bail out if any of
4064
 * the descendants of the current schedule node refer to outer
4065
 * schedule nodes in any way.
4066
 *
4067
 *
4068
 * To see if there is any shifted stride, we look at the differences
4069
 * between the values of the current dimension in pairs of domains
4070
 * for equal values of outer dimensions.  These differences should be
4071
 * of the form
4072
 *
4073
 *  m x + r
4074
 *
4075
 * with "m" the stride and "r" a constant.  Note that we cannot perform
4076
 * this analysis on individual domains as the lower bound in each domain
4077
 * may depend on parameters or outer dimensions and so the current dimension
4078
 * itself may not have a fixed remainder on division by the stride.
4079
 *
4080
 * In particular, we compare the first domain that does not have an
4081
 * obviously fixed value for the current dimension to itself and all
4082
 * other domains and collect the offsets and the gcd of the strides.
4083
 * If the gcd becomes one, then we failed to find shifted strides.
4084
 * If the gcd is zero, then the differences were all fixed, meaning
4085
 * that some domains had non-obviously fixed values for the current dimension.
4086
 * If all the offsets are the same (for those domains that do not have
4087
 * an obviously fixed value for the current dimension), then we do not
4088
 * apply the transformation.
4089
 * If none of the domains were skipped, then there is nothing to do.
4090
 * If some of them were skipped, then if we apply separation, the schedule
4091
 * domain should get split in pieces with a (non-shifted) stride.
4092
 *
4093
 * Otherwise, we apply a shift to expose the stride in
4094
 * generate_shift_component.
4095
 */
4096
static __isl_give isl_ast_graft_list *generate_component(
4097
  struct isl_set_map_pair *domain, int *order, int n,
4098
  __isl_take isl_ast_build *build)
4099
167
{
4100
167
  int i, d;
4101
167
  int depth;
4102
167
  isl_ctx *ctx;
4103
167
  isl_map *map;
4104
167
  isl_set *deltas;
4105
167
  isl_val *gcd = NULL;
4106
167
  isl_multi_val *mv;
4107
167
  int fixed, skip;
4108
167
  int base;
4109
167
  isl_ast_graft_list *list;
4110
167
  int res = 0;
4111
167
4112
167
  depth = isl_ast_build_get_depth(build);
4113
167
4114
167
  skip = n == 1;
4115
167
  if (skip >= 0 && !skip)
4116
85
    skip = at_most_one_non_fixed(domain, order, n, depth);
4117
167
  if (skip >= 0 && !skip) {
4118
77
    if (isl_ast_build_has_schedule_node(build))
4119
76
      skip = has_anchored_subtree(build);
4120
1
    else
4121
1
      skip = isl_ast_build_options_involve_depth(build);
4122
77
  }
4123
167
  if (skip < 0)
4124
0
    goto error;
4125
167
  if (skip)
4126
90
    return generate_shifted_component_from_list(domain,
4127
90
                  order, n, build);
4128
77
4129
77
  base = eliminate_non_fixed(domain, order, n, depth, build);
4130
77
  if (base < 0)
4131
0
    goto error;
4132
77
4133
77
  ctx = isl_ast_build_get_ctx(build);
4134
77
4135
77
  mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4136
77
4137
77
  fixed = 1;
4138
90
  for (i = 0; i < n; 
++i13
) {
4139
87
    isl_val *r, *m;
4140
87
4141
87
    map = isl_map_from_domain_and_range(
4142
87
          isl_set_copy(domain[order[base]].set),
4143
87
          isl_set_copy(domain[order[i]].set));
4144
110
    for (d = 0; d < depth; 
++d23
)
4145
23
      map = isl_map_equate(map, isl_dim_in, d,
4146
23
                isl_dim_out, d);
4147
87
    deltas = isl_map_deltas(map);
4148
87
    res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4149
87
    isl_set_free(deltas);
4150
87
    if (res < 0)
4151
0
      break;
4152
87
4153
87
    if (i == 0)
4154
77
      gcd = m;
4155
10
    else
4156
10
      gcd = isl_val_gcd(gcd, m);
4157
87
    if (isl_val_is_one(gcd)) {
4158
74
      isl_val_free(r);
4159
74
      break;
4160
74
    }
4161
13
    mv = isl_multi_val_set_val(mv, i, r);
4162
13
4163
13
    res = dim_is_fixed(domain[order[i]].set, depth);
4164
13
    if (res < 0)
4165
0
      break;
4166
13
    if (res)
4167
0
      continue;
4168
13
4169
13
    if (fixed && 
i > base10
) {
4170
5
      isl_val *a, *b;
4171
5
      a = isl_multi_val_get_val(mv, i);
4172
5
      b = isl_multi_val_get_val(mv, base);
4173
5
      if (isl_val_ne(a, b))
4174
4
        fixed = 0;
4175
5
      isl_val_free(a);
4176
5
      isl_val_free(b);
4177
5
    }
4178
13
  }
4179
77
4180
77
  if (res < 0 || !gcd) {
4181
0
    isl_ast_build_free(build);
4182
0
    list = NULL;
4183
77
  } else if (i < n || 
fixed3
||
isl_val_is_zero(gcd)3
) {
4184
74
    list = generate_shifted_component_from_list(domain,
4185
74
                  order, n, build);
4186
74
  } else {
4187
3
    list = generate_shift_component(domain, order, n, gcd, mv,
4188
3
            build);
4189
3
  }
4190
77
4191
77
  isl_val_free(gcd);
4192
77
  isl_multi_val_free(mv);
4193
77
4194
77
  return list;
4195
0
error:
4196
0
  isl_ast_build_free(build);
4197
0
  return NULL;
4198
77
}
4199
4200
/* Store both "map" itself and its domain in the
4201
 * structure pointed to by *next and advance to the next array element.
4202
 */
4203
static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4204
320
{
4205
320
  struct isl_set_map_pair **next = user;
4206
320
4207
320
  (*next)->map = isl_map_copy(map);
4208
320
  (*next)->set = isl_map_domain(map);
4209
320
  (*next)++;
4210
320
4211
320
  return isl_stat_ok;
4212
320
}
4213
4214
static int after_in_tree(__isl_keep isl_union_map *umap,
4215
  __isl_keep isl_schedule_node *node);
4216
4217
/* Is any domain element of "umap" scheduled after any of
4218
 * the corresponding image elements by the tree rooted at
4219
 * the child of "node"?
4220
 */
4221
static int after_in_child(__isl_keep isl_union_map *umap,
4222
  __isl_keep isl_schedule_node *node)
4223
41
{
4224
41
  isl_schedule_node *child;
4225
41
  int after;
4226
41
4227
41
  child = isl_schedule_node_get_child(node, 0);
4228
41
  after = after_in_tree(umap, child);
4229
41
  isl_schedule_node_free(child);
4230
41
4231
41
  return after;
4232
41
}
4233
4234
/* Is any domain element of "umap" scheduled after any of
4235
 * the corresponding image elements by the tree rooted at
4236
 * the band node "node"?
4237
 *
4238
 * We first check if any domain element is scheduled after any
4239
 * of the corresponding image elements by the band node itself.
4240
 * If not, we restrict "map" to those pairs of element that
4241
 * are scheduled together by the band node and continue with
4242
 * the child of the band node.
4243
 * If there are no such pairs then the map passed to after_in_child
4244
 * will be empty causing it to return 0.
4245
 */
4246
static int after_in_band(__isl_keep isl_union_map *umap,
4247
  __isl_keep isl_schedule_node *node)
4248
0
{
4249
0
  isl_multi_union_pw_aff *mupa;
4250
0
  isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4251
0
  isl_union_set *domain, *range;
4252
0
  isl_space *space;
4253
0
  int empty;
4254
0
  int after;
4255
0
4256
0
  if (isl_schedule_node_band_n_member(node) == 0)
4257
0
    return after_in_child(umap, node);
4258
0
4259
0
  mupa = isl_schedule_node_band_get_partial_schedule(node);
4260
0
  space = isl_multi_union_pw_aff_get_space(mupa);
4261
0
  partial = isl_union_map_from_multi_union_pw_aff(mupa);
4262
0
  test = isl_union_map_copy(umap);
4263
0
  test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4264
0
  test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4265
0
  gt = isl_union_map_from_map(isl_map_lex_gt(space));
4266
0
  test = isl_union_map_intersect(test, gt);
4267
0
  empty = isl_union_map_is_empty(test);
4268
0
  isl_union_map_free(test);
4269
0
4270
0
  if (empty < 0 || !empty) {
4271
0
    isl_union_map_free(partial);
4272
0
    return empty < 0 ? -1 : 1;
4273
0
  }
4274
0
4275
0
  universe = isl_union_map_universe(isl_union_map_copy(umap));
4276
0
  domain = isl_union_map_domain(isl_union_map_copy(universe));
4277
0
  range = isl_union_map_range(universe);
4278
0
  umap1 = isl_union_map_copy(partial);
4279
0
  umap1 = isl_union_map_intersect_domain(umap1, domain);
4280
0
  umap2 = isl_union_map_intersect_domain(partial, range);
4281
0
  test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4282
0
  test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4283
0
  after = after_in_child(test, node);
4284
0
  isl_union_map_free(test);
4285
0
  return after;
4286
0
}
4287
4288
/* Is any domain element of "umap" scheduled after any of
4289
 * the corresponding image elements by the tree rooted at
4290
 * the context node "node"?
4291
 *
4292
 * The context constraints apply to the schedule domain,
4293
 * so we cannot apply them directly to "umap", which contains
4294
 * pairs of statement instances.  Instead, we add them
4295
 * to the range of the prefix schedule for both domain and
4296
 * range of "umap".
4297
 */
4298
static int after_in_context(__isl_keep isl_union_map *umap,
4299
  __isl_keep isl_schedule_node *node)
4300
0
{
4301
0
  isl_union_map *prefix, *universe, *umap1, *umap2;
4302
0
  isl_union_set *domain, *range;
4303
0
  isl_set *context;
4304
0
  int after;
4305
0
4306
0
  umap = isl_union_map_copy(umap);
4307
0
  context = isl_schedule_node_context_get_context(node);
4308
0
  prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4309
0
  universe = isl_union_map_universe(isl_union_map_copy(umap));
4310
0
  domain = isl_union_map_domain(isl_union_map_copy(universe));
4311
0
  range = isl_union_map_range(universe);
4312
0
  umap1 = isl_union_map_copy(prefix);
4313
0
  umap1 = isl_union_map_intersect_domain(umap1, domain);
4314
0
  umap2 = isl_union_map_intersect_domain(prefix, range);
4315
0
  umap1 = isl_union_map_intersect_range(umap1,
4316
0
              isl_union_set_from_set(context));
4317
0
  umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4318
0
  umap = isl_union_map_intersect(umap, umap1);
4319
0
4320
0
  after = after_in_child(umap, node);
4321
0
4322
0
  isl_union_map_free(umap);
4323
0
4324
0
  return after;
4325
0
}
4326
4327
/* Is any domain element of "umap" scheduled after any of
4328
 * the corresponding image elements by the tree rooted at
4329
 * the expansion node "node"?
4330
 *
4331
 * We apply the expansion to domain and range of "umap" and
4332
 * continue with its child.
4333
 */
4334
static int after_in_expansion(__isl_keep isl_union_map *umap,
4335
  __isl_keep isl_schedule_node *node)
4336
0
{
4337
0
  isl_union_map *expansion;
4338
0
  int after;
4339
0
4340
0
  expansion = isl_schedule_node_expansion_get_expansion(node);
4341
0
  umap = isl_union_map_copy(umap);
4342
0
  umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4343
0
  umap = isl_union_map_apply_range(umap, expansion);
4344
0
4345
0
  after = after_in_child(umap, node);
4346
0
4347
0
  isl_union_map_free(umap);
4348
0
4349
0
  return after;
4350
0
}
4351
4352
/* Is any domain element of "umap" scheduled after any of
4353
 * the corresponding image elements by the tree rooted at
4354
 * the extension node "node"?
4355
 *
4356
 * Since the extension node may add statement instances before or
4357
 * after the pairs of statement instances in "umap", we return 1
4358
 * to ensure that these pairs are not broken up.
4359
 */
4360
static int after_in_extension(__isl_keep isl_union_map *umap,
4361
  __isl_keep isl_schedule_node *node)
4362
0
{
4363
0
  return 1;
4364
0
}
4365
4366
/* Is any domain element of "umap" scheduled after any of
4367
 * the corresponding image elements by the tree rooted at
4368
 * the filter node "node"?
4369
 *
4370
 * We intersect domain and range of "umap" with the filter and
4371
 * continue with its child.
4372
 */
4373
static int after_in_filter(__isl_keep isl_union_map *umap,
4374
  __isl_keep isl_schedule_node *node)
4375
41
{
4376
41
  isl_union_set *filter;
4377
41
  int after;
4378
41
4379
41
  umap = isl_union_map_copy(umap);
4380
41
  filter = isl_schedule_node_filter_get_filter(node);
4381
41
  umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4382
41
  umap = isl_union_map_intersect_range(umap, filter);
4383
41
4384
41
  after = after_in_child(umap, node);
4385
41
4386
41
  isl_union_map_free(umap);
4387
41
4388
41
  return after;
4389
41
}
4390
4391
/* Is any domain element of "umap" scheduled after any of
4392
 * the corresponding image elements by the tree rooted at
4393
 * the set node "node"?
4394
 *
4395
 * This is only the case if this condition holds in any
4396
 * of the (filter) children of the set node.
4397
 * In particular, if the domain and the range of "umap"
4398
 * are contained in different children, then the condition
4399
 * does not hold.
4400
 */
4401
static int after_in_set(__isl_keep isl_union_map *umap,
4402
  __isl_keep isl_schedule_node *node)
4403
16
{
4404
16
  int i, n;
4405
16
4406
16
  n = isl_schedule_node_n_children(node);
4407
57
  for (i = 0; i < n; 
++i41
) {
4408
41
    isl_schedule_node *child;
4409
41
    int after;
4410
41
4411
41
    child = isl_schedule_node_get_child(node, i);
4412
41
    after = after_in_tree(umap, child);
4413
41
    isl_schedule_node_free(child);
4414
41
4415
41
    if (after < 0 || after)
4416
0
      return after;
4417
41
  }
4418
16
4419
16
  return 0;
4420
16
}
4421
4422
/* Return the filter of child "i" of "node".
4423
 */
4424
static __isl_give isl_union_set *child_filter(
4425
  __isl_keep isl_schedule_node *node, int i)
4426
36
{
4427
36
  isl_schedule_node *child;
4428
36
  isl_union_set *filter;
4429
36
4430
36
  child = isl_schedule_node_get_child(node, i);
4431
36
  filter = isl_schedule_node_filter_get_filter(child);
4432
36
  isl_schedule_node_free(child);
4433
36
4434
36
  return filter;
4435
36
}
4436
4437
/* Is any domain element of "umap" scheduled after any of
4438
 * the corresponding image elements by the tree rooted at
4439
 * the sequence node "node"?
4440
 *
4441
 * This happens in particular if any domain element is
4442
 * contained in a later child than one containing a range element or
4443
 * if the condition holds within a given child in the sequence.
4444
 * The later part of the condition is checked by after_in_set.
4445
 */
4446
static int after_in_sequence(__isl_keep isl_union_map *umap,
4447
  __isl_keep isl_schedule_node *node)
4448
19
{
4449
19
  int i, j, n;
4450
19
  isl_union_map *umap_i;
4451
19
  int empty, after = 0;
4452
19
4453
19
  n = isl_schedule_node_n_children(node);
4454
45
  for (i = 1; i < n; 
++i26
) {
4455
29
    isl_union_set *filter_i;
4456
29
4457
29
    umap_i = isl_union_map_copy(umap);
4458
29
    filter_i = child_filter(node, i);
4459
29
    umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4460
29
    empty = isl_union_map_is_empty(umap_i);
4461
29
    if (empty < 0)
4462
0
      goto error;
4463
29
    if (empty) {
4464
23
      isl_union_map_free(umap_i);
4465
23
      continue;
4466
23
    }
4467
6
4468
10
    
for (j = 0; 6
j < i;
++j4
) {
4469
7
      isl_union_set *filter_j;
4470
7
      isl_union_map *umap_ij;
4471
7
4472
7
      umap_ij = isl_union_map_copy(umap_i);
4473
7
      filter_j = child_filter(node, j);
4474
7
      umap_ij = isl_union_map_intersect_range(umap_ij,
4475
7
                filter_j);
4476
7
      empty = isl_union_map_is_empty(umap_ij);
4477
7
      isl_union_map_free(umap_ij);
4478
7
4479
7
      if (empty < 0)
4480
0
        goto error;
4481
7
      if (!empty)
4482
3
        after = 1;
4483
7
      if (after)
4484
3
        break;
4485
7
    }
4486
6
4487
6
    isl_union_map_free(umap_i);
4488
6
    if (after)
4489
3
      break;
4490
6
  }
4491
19
4492
19
  if (after < 0 || after)
4493
3
    return after;
4494
16
4495
16
  return after_in_set(umap, node);
4496
0
error:
4497
0
  isl_union_map_free(umap_i);
4498
0
  return -1;
4499
16
}
4500
4501
/* Is any domain element of "umap" scheduled after any of
4502
 * the corresponding image elements by the tree rooted at "node"?
4503
 *
4504
 * If "umap" is empty, then clearly there is no such element.
4505
 * Otherwise, consider the different types of nodes separately.
4506
 */
4507
static int after_in_tree(__isl_keep isl_union_map *umap,
4508
  __isl_keep isl_schedule_node *node)
4509
101
{
4510
101
  int empty;
4511
101
  enum isl_schedule_node_type type;
4512
101
4513
101
  empty = isl_union_map_is_empty(umap);
4514
101
  if (empty < 0)
4515
0
    return -1;
4516
101
  if (empty)
4517
41
    return 0;
4518
60
  if (!node)
4519
0
    return -1;
4520
60
4521
60
  type = isl_schedule_node_get_type(node);
4522
60
  switch (type) {
4523
60
  case isl_schedule_node_error:
4524
0
    return -1;
4525
60
  case isl_schedule_node_leaf:
4526
0
    return 0;
4527
60
  case isl_schedule_node_band:
4528
0
    return after_in_band(umap, node);
4529
60
  case isl_schedule_node_domain:
4530
0
    isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4531
0
      "unexpected internal domain node", return -1);
4532
0
  case isl_schedule_node_context:
4533
0
    return after_in_context(umap, node);
4534
0
  case isl_schedule_node_expansion:
4535
0
    return after_in_expansion(umap, node);
4536
0
  case isl_schedule_node_extension:
4537
0
    return after_in_extension(umap, node);
4538
41
  case isl_schedule_node_filter:
4539
41
    return after_in_filter(umap, node);
4540
0
  case isl_schedule_node_guard:
4541
0
  case isl_schedule_node_mark:
4542
0
    return after_in_child(umap, node);
4543
0
  case isl_schedule_node_set:
4544
0
    return after_in_set(umap, node);
4545
19
  case isl_schedule_node_sequence:
4546
19
    return after_in_sequence(umap, node);
4547
0
  }
4548
0
4549
0
  return 1;
4550
0
}
4551
4552
/* Is any domain element of "map1" scheduled after any domain
4553
 * element of "map2" by the subtree underneath the current band node,
4554
 * while at the same time being scheduled together by the current
4555
 * band node, i.e., by "map1" and "map2?
4556
 *
4557
 * If the child of the current band node is a leaf, then
4558
 * no element can be scheduled after any other element.
4559
 *
4560
 * Otherwise, we construct a relation between domain elements
4561
 * of "map1" and domain elements of "map2" that are scheduled
4562
 * together and then check if the subtree underneath the current
4563
 * band node determines their relative order.
4564
 */
4565
static int after_in_subtree(__isl_keep isl_ast_build *build,
4566
  __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4567
19
{
4568
19
  isl_schedule_node *node;
4569
19
  isl_map *map;
4570
19
  isl_union_map *umap;
4571
19
  int after;
4572
19
4573
19
  node = isl_ast_build_get_schedule_node(build);
4574
19
  if (!node)
4575
0
    return -1;
4576
19
  node = isl_schedule_node_child(node, 0);
4577
19
  if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4578
0
    isl_schedule_node_free(node);
4579
0
    return 0;
4580
0
  }
4581
19
  map = isl_map_copy(map2);
4582
19
  map = isl_map_apply_domain(map, isl_map_copy(map1));
4583
19
  umap = isl_union_map_from_map(map);
4584
19
  after = after_in_tree(umap, node);
4585
19
  isl_union_map_free(umap);
4586
19
  isl_schedule_node_free(node);
4587
19
  return after;
4588
19
}
4589
4590
/* Internal data for any_scheduled_after.
4591
 *
4592
 * "build" is the build in which the AST is constructed.
4593
 * "depth" is the number of loops that have already been generated
4594
 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4595
 * "domain" is an array of set-map pairs corresponding to the different
4596
 * iteration domains.  The set is the schedule domain, i.e., the domain
4597
 * of the inverse schedule, while the map is the inverse schedule itself.
4598
 */
4599
struct isl_any_scheduled_after_data {
4600
  isl_ast_build *build;
4601
  int depth;
4602
  int group_coscheduled;
4603
  struct isl_set_map_pair *domain;
4604
};
4605
4606
/* Is any element of domain "i" scheduled after any element of domain "j"
4607
 * (for a common iteration of the first data->depth loops)?
4608
 *
4609
 * data->domain[i].set contains the domain of the inverse schedule
4610
 * for domain "i", i.e., elements in the schedule domain.
4611
 *
4612
 * If we are inside a band of a schedule tree and there is a pair
4613
 * of elements in the two domains that is schedule together by
4614
 * the current band, then we check if any element of "i" may be schedule
4615
 * after element of "j" by the descendants of the band node.
4616
 *
4617
 * If data->group_coscheduled is set, then we also return 1 if there
4618
 * is any pair of elements in the two domains that are scheduled together.
4619
 */
4620
static isl_bool any_scheduled_after(int i, int j, void *user)
4621
406
{
4622
406
  struct isl_any_scheduled_after_data *data = user;
4623
406
  int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4624
406
  int pos;
4625
406
4626
445
  for (pos = data->depth; pos < dim; 
++pos39
) {
4627
426
    int follows;
4628
426
4629
426
    follows = isl_set_follows_at(data->domain[i].set,
4630
426
            data->domain[j].set, pos);
4631
426
4632
426
    if (follows < -1)
4633
0
      return isl_bool_error;
4634
426
    if (follows > 0)
4635
325
      return isl_bool_true;
4636
101
    if (follows < 0)
4637
62
      return isl_bool_false;
4638
101
  }
4639
406
4640
406
  
if (19
isl_ast_build_has_schedule_node(data->build)19
) {
4641
19
    int after;
4642
19
4643
19
    after = after_in_subtree(data->build, data->domain[i].map,
4644
19
              data->domain[j].map);
4645
19
    if (after < 0 || after)
4646
3
      return after;
4647
16
  }
4648
16
4649
16
  return data->group_coscheduled;
4650
16
}
4651
4652
/* Look for independent components at the current depth and generate code
4653
 * for each component separately.  The resulting lists of grafts are
4654
 * merged in an attempt to combine grafts with identical guards.
4655
 *
4656
 * Code for two domains can be generated separately if all the elements
4657
 * of one domain are scheduled before (or together with) all the elements
4658
 * of the other domain.  We therefore consider the graph with as nodes
4659
 * the domains and an edge between two nodes if any element of the first
4660
 * node is scheduled after any element of the second node.
4661
 * If the ast_build_group_coscheduled is set, then we also add an edge if
4662
 * there is any pair of elements in the two domains that are scheduled
4663
 * together.
4664
 * Code is then generated (by generate_component)
4665
 * for each of the strongly connected components in this graph
4666
 * in their topological order.
4667
 *
4668
 * Since the test is performed on the domain of the inverse schedules of
4669
 * the different domains, we precompute these domains and store
4670
 * them in data.domain.
4671
 */
4672
static __isl_give isl_ast_graft_list *generate_components(
4673
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4674
118
{
4675
118
  int i;
4676
118
  isl_ctx *ctx = isl_ast_build_get_ctx(build);
4677
118
  int n = isl_union_map_n_map(executed);
4678
118
  struct isl_any_scheduled_after_data data;
4679
118
  struct isl_set_map_pair *next;
4680
118
  struct isl_tarjan_graph *g = NULL;
4681
118
  isl_ast_graft_list *list = NULL;
4682
118
  int n_domain = 0;
4683
118
4684
118
  data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4685
118
  if (!data.domain)
4686
0
    goto error;
4687
118
  n_domain = n;
4688
118
4689
118
  next = data.domain;
4690
118
  if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4691
0
    goto error;
4692
118
4693
118
  if (!build)
4694
0
    goto error;
4695
118
  data.build = build;
4696
118
  data.depth = isl_ast_build_get_depth(build);
4697
118
  data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4698
118
  g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4699
118
  if (!g)
4700
0
    goto error;
4701
118
4702
118
  list = isl_ast_graft_list_alloc(ctx, 0);
4703
118
4704
118
  i = 0;
4705
285
  while (list && n) {
4706
167
    isl_ast_graft_list *list_c;
4707
167
    int first = i;
4708
167
4709
167
    if (g->order[i] == -1)
4710
167
      
isl_die0
(ctx, isl_error_internal, "cannot happen",
4711
167
        goto error);
4712
167
    ++i; --n;
4713
320
    while (g->order[i] != -1) {
4714
153
      ++i; --n;
4715
153
    }
4716
167
4717
167
    list_c = generate_component(data.domain,
4718
167
              g->order + first, i - first,
4719
167
              isl_ast_build_copy(build));
4720
167
    list = isl_ast_graft_list_merge(list, list_c, build);
4721
167
4722
167
    ++i;
4723
167
  }
4724
118
4725
118
  if (0)
4726
0
error:    list = isl_ast_graft_list_free(list);
4727
118
  isl_tarjan_graph_free(g);
4728
438
  for (i = 0; i < n_domain; 
++i320
) {
4729
320
    isl_map_free(data.domain[i].map);
4730
320
    isl_set_free(data.domain[i].set);
4731
320
  }
4732
118
  free(data.domain);
4733
118
  isl_union_map_free(executed);
4734
118
  isl_ast_build_free(build);
4735
118
4736
118
  return list;
4737
118
}
4738
4739
/* Generate code for the next level (and all inner levels).
4740
 *
4741
 * If "executed" is empty, i.e., no code needs to be generated,
4742
 * then we return an empty list.
4743
 *
4744
 * If we have already generated code for all loop levels, then we pass
4745
 * control to generate_inner_level.
4746
 *
4747
 * If "executed" lives in a single space, i.e., if code needs to be
4748
 * generated for a single domain, then there can only be a single
4749
 * component and we go directly to generate_shifted_component.
4750
 * Otherwise, we call generate_components to detect the components
4751
 * and to call generate_component on each of them separately.
4752
 */
4753
static __isl_give isl_ast_graft_list *generate_next_level(
4754
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4755
4.49k
{
4756
4.49k
  int depth;
4757
4.49k
4758
4.49k
  if (!build || !executed)
4759
0
    goto error;
4760
4.49k
4761
4.49k
  if (isl_union_map_is_empty(executed)) {
4762
0
    isl_ctx *ctx = isl_ast_build_get_ctx(build);
4763
0
    isl_union_map_free(executed);
4764
0
    isl_ast_build_free(build);
4765
0
    return isl_ast_graft_list_alloc(ctx, 0);
4766
0
  }
4767
4.49k
4768
4.49k
  depth = isl_ast_build_get_depth(build);
4769
4.49k
  if (depth >= isl_ast_build_dim(build, isl_dim_set))
4770
2.11k
    return generate_inner_level(executed, build);
4771
2.37k
4772
2.37k
  if (isl_union_map_n_map(executed) == 1)
4773
2.25k
    return generate_shifted_component(executed, build);
4774
118
4775
118
  return generate_components(executed, build);
4776
0
error:
4777
0
  isl_union_map_free(executed);
4778
0
  isl_ast_build_free(build);
4779
0
  return NULL;
4780
118
}
4781
4782
/* Internal data structure used by isl_ast_build_node_from_schedule_map.
4783
 * internal, executed and build are the inputs to generate_code.
4784
 * list collects the output.
4785
 */
4786
struct isl_generate_code_data {
4787
  int internal;
4788
  isl_union_map *executed;
4789
  isl_ast_build *build;
4790
4791
  isl_ast_graft_list *list;
4792
};
4793
4794
/* Given an inverse schedule in terms of the external build schedule, i.e.,
4795
 *
4796
 *  [E -> S] -> D
4797
 *
4798
 * with E the external build schedule and S the additional schedule "space",
4799
 * reformulate the inverse schedule in terms of the internal schedule domain,
4800
 * i.e., return
4801
 *
4802
 *  [I -> S] -> D
4803
 *
4804
 * We first obtain a mapping
4805
 *
4806
 *  I -> E
4807
 *
4808
 * take the inverse and the product with S -> S, resulting in
4809
 *
4810
 *  [I -> S] -> [E -> S]
4811
 *
4812
 * Applying the map to the input produces the desired result.
4813
 */
4814
static __isl_give isl_union_map *internal_executed(
4815
  __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4816
  __isl_keep isl_ast_build *build)
4817
3
{
4818
3
  isl_map *id, *proj;
4819
3
4820
3
  proj = isl_ast_build_get_schedule_map(build);
4821
3
  proj = isl_map_reverse(proj);
4822
3
  space = isl_space_map_from_set(isl_space_copy(space));
4823
3
  id = isl_map_identity(space);
4824
3
  proj = isl_map_product(proj, id);
4825
3
  executed = isl_union_map_apply_domain(executed,
4826
3
            isl_union_map_from_map(proj));
4827
3
  return executed;
4828
3
}
4829
4830
/* Generate an AST that visits the elements in the range of data->executed
4831
 * in the relative order specified by the corresponding domain element(s)
4832
 * for those domain elements that belong to "set".
4833
 * Add the result to data->list.
4834
 *
4835
 * The caller ensures that "set" is a universe domain.
4836
 * "space" is the space of the additional part of the schedule.
4837
 * It is equal to the space of "set" if build->domain is parametric.
4838
 * Otherwise, it is equal to the range of the wrapped space of "set".
4839
 *
4840
 * If the build space is not parametric and
4841
 * if isl_ast_build_node_from_schedule_map
4842
 * was called from an outside user (data->internal not set), then
4843
 * the (inverse) schedule refers to the external build domain and needs to
4844
 * be transformed to refer to the internal build domain.
4845
 *
4846
 * If the build space is parametric, then we add some of the parameter
4847
 * constraints to the executed relation.  Adding these constraints
4848
 * allows for an earlier detection of conflicts in some cases.
4849
 * However, we do not want to divide the executed relation into
4850
 * more disjuncts than necessary.  We therefore approximate
4851
 * the constraints on the parameters by a single disjunct set.
4852
 *
4853
 * The build is extended to include the additional part of the schedule.
4854
 * If the original build space was not parametric, then the options
4855
 * in data->build refer only to the additional part of the schedule
4856
 * and they need to be adjusted to refer to the complete AST build
4857
 * domain.
4858
 *
4859
 * After having adjusted inverse schedule and build, we start generating
4860
 * code with the outer loop of the current code generation
4861
 * in generate_next_level.
4862
 *
4863
 * If the original build space was not parametric, we undo the embedding
4864
 * on the resulting isl_ast_node_list so that it can be used within
4865
 * the outer AST build.
4866
 */
4867
static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4868
  __isl_take isl_set *set, __isl_take isl_space *space)
4869
31
{
4870
31
  isl_union_map *executed;
4871
31
  isl_ast_build *build;
4872
31
  isl_ast_graft_list *list;
4873
31
  int embed;
4874
31
4875
31
  executed = isl_union_map_copy(data->executed);
4876
31
  executed = isl_union_map_intersect_domain(executed,
4877
31
             isl_union_set_from_set(set));
4878
31
4879
31
  embed = !isl_set_is_params(data->build->domain);
4880
31
  if (embed && 
!data->internal25
)
4881
3
    executed = internal_executed(executed, space, data->build);
4882
31
  if (!embed) {
4883
6
    isl_set *domain;
4884
6
    domain = isl_ast_build_get_domain(data->build);
4885
6
    domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4886
6
    executed = isl_union_map_intersect_params(executed, domain);
4887
6
  }
4888
31
4889
31
  build = isl_ast_build_copy(data->build);
4890
31
  build = isl_ast_build_product(build, space);
4891
31
4892
31
  list = generate_next_level(executed, build);
4893
31
4894
31
  list = isl_ast_graft_list_unembed(list, embed);
4895
31
4896
31
  data->list = isl_ast_graft_list_concat(data->list, list);
4897
31
4898
31
  return isl_stat_ok;
4899
31
}
4900
4901
/* Generate an AST that visits the elements in the range of data->executed
4902
 * in the relative order specified by the corresponding domain element(s)
4903
 * for those domain elements that belong to "set".
4904
 * Add the result to data->list.
4905
 *
4906
 * The caller ensures that "set" is a universe domain.
4907
 *
4908
 * If the build space S is not parametric, then the space of "set"
4909
 * need to be a wrapped relation with S as domain.  That is, it needs
4910
 * to be of the form
4911
 *
4912
 *  [S -> T]
4913
 *
4914
 * Check this property and pass control to generate_code_in_space
4915
 * passing along T.
4916
 * If the build space is not parametric, then T is the space of "set".
4917
 */
4918
static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
4919
31
{
4920
31
  struct isl_generate_code_data *data = user;
4921
31
  isl_space *space, *build_space;
4922
31
  int is_domain;
4923
31
4924
31
  space = isl_set_get_space(set);
4925
31
4926
31
  if (isl_set_is_params(data->build->domain))
4927
6
    return generate_code_in_space(data, set, space);
4928
25
4929
25
  build_space = isl_ast_build_get_space(data->build, data->internal);
4930
25
  space = isl_space_unwrap(space);
4931
25
  is_domain = isl_space_is_domain(build_space, space);
4932
25
  isl_space_free(build_space);
4933
25
  space = isl_space_range(space);
4934
25
4935
25
  if (is_domain < 0)
4936
0
    goto error;
4937
25
  if (!is_domain)
4938
25
    
isl_die0
(isl_set_get_ctx(set), isl_error_invalid,
4939
25
      "invalid nested schedule space", goto error);
4940
25
4941
25
  return generate_code_in_space(data, set, space);
4942
0
error:
4943
0
  isl_set_free(set);
4944
0
  isl_space_free(space);
4945
0
  return isl_stat_error;
4946
25
}
4947
4948
/* Generate an AST that visits the elements in the range of "executed"
4949
 * in the relative order specified by the corresponding domain element(s).
4950
 *
4951
 * "build" is an isl_ast_build that has either been constructed by
4952
 * isl_ast_build_from_context or passed to a callback set by
4953
 * isl_ast_build_set_create_leaf.
4954
 * In the first case, the space of the isl_ast_build is typically
4955
 * a parametric space, although this is currently not enforced.
4956
 * In the second case, the space is never a parametric space.
4957
 * If the space S is not parametric, then the domain space(s) of "executed"
4958
 * need to be wrapped relations with S as domain.
4959
 *
4960
 * If the domain of "executed" consists of several spaces, then an AST
4961
 * is generated for each of them (in arbitrary order) and the results
4962
 * are concatenated.
4963
 *
4964
 * If "internal" is set, then the domain "S" above refers to the internal
4965
 * schedule domain representation.  Otherwise, it refers to the external
4966
 * representation, as returned by isl_ast_build_get_schedule_space.
4967
 *
4968
 * We essentially run over all the spaces in the domain of "executed"
4969
 * and call generate_code_set on each of them.
4970
 */
4971
static __isl_give isl_ast_graft_list *generate_code(
4972
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4973
  int internal)
4974
31
{
4975
31
  isl_ctx *ctx;
4976
31
  struct isl_generate_code_data data = { 0 };
4977
31
  isl_space *space;
4978
31
  isl_union_set *schedule_domain;
4979
31
  isl_union_map *universe;
4980
31
4981
31
  if (!build)
4982
0
    goto error;
4983
31
  space = isl_ast_build_get_space(build, 1);
4984
31
  space = isl_space_align_params(space,
4985
31
            isl_union_map_get_space(executed));
4986
31
  space = isl_space_align_params(space,
4987
31
            isl_union_map_get_space(build->options));
4988
31
  build = isl_ast_build_align_params(build, isl_space_copy(space));
4989
31
  executed = isl_union_map_align_params(executed, space);
4990
31
  if (!executed || !build)
4991
0
    goto error;
4992
31
4993
31
  ctx = isl_ast_build_get_ctx(build);
4994
31
4995
31
  data.internal = internal;
4996
31
  data.executed = executed;
4997
31
  data.build = build;
4998
31
  data.list = isl_ast_graft_list_alloc(ctx, 0);
4999
31
5000
31
  universe = isl_union_map_universe(isl_union_map_copy(executed));
5001
31
  schedule_domain = isl_union_map_domain(universe);
5002
31
  if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5003
31
          &data) < 0)
5004
0
    data.list = isl_ast_graft_list_free(data.list);
5005
31
5006
31
  isl_union_set_free(schedule_domain);
5007
31
  isl_union_map_free(executed);
5008
31
5009
31
  isl_ast_build_free(build);
5010
31
  return data.list;
5011
0
error:
5012
0
  isl_union_map_free(executed);
5013
0
  isl_ast_build_free(build);
5014
0
  return NULL;
5015
31
}
5016
5017
/* Generate an AST that visits the elements in the domain of "schedule"
5018
 * in the relative order specified by the corresponding image element(s).
5019
 *
5020
 * "build" is an isl_ast_build that has either been constructed by
5021
 * isl_ast_build_from_context or passed to a callback set by
5022
 * isl_ast_build_set_create_leaf.
5023
 * In the first case, the space of the isl_ast_build is typically
5024
 * a parametric space, although this is currently not enforced.
5025
 * In the second case, the space is never a parametric space.
5026
 * If the space S is not parametric, then the range space(s) of "schedule"
5027
 * need to be wrapped relations with S as domain.
5028
 *
5029
 * If the range of "schedule" consists of several spaces, then an AST
5030
 * is generated for each of them (in arbitrary order) and the results
5031
 * are concatenated.
5032
 *
5033
 * We first initialize the local copies of the relevant options.
5034
 * We do this here rather than when the isl_ast_build is created
5035
 * because the options may have changed between the construction
5036
 * of the isl_ast_build and the call to isl_generate_code.
5037
 *
5038
 * The main computation is performed on an inverse schedule (with
5039
 * the schedule domain in the domain and the elements to be executed
5040
 * in the range) called "executed".
5041
 */
5042
__isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5043
  __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5044
9
{
5045
9
  isl_ast_graft_list *list;
5046
9
  isl_ast_node *node;
5047
9
  isl_union_map *executed;
5048
9
5049
9
  build = isl_ast_build_copy(build);
5050
9
  build = isl_ast_build_set_single_valued(build, 0);
5051
9
  schedule = isl_union_map_coalesce(schedule);
5052
9
  schedule = isl_union_map_remove_redundancies(schedule);
5053
9
  executed = isl_union_map_reverse(schedule);
5054
9
  list = generate_code(executed, isl_ast_build_copy(build), 0);
5055
9
  node = isl_ast_node_from_graft_list(list, build);
5056
9
  isl_ast_build_free(build);
5057
9
5058
9
  return node;
5059
9
}
5060
5061
/* The old name for isl_ast_build_node_from_schedule_map.
5062
 * It is being kept for backward compatibility, but
5063
 * it will be removed in the future.
5064
 */
5065
__isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5066
  __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5067
0
{
5068
0
  return isl_ast_build_node_from_schedule_map(build, schedule);
5069
0
}
5070
5071
/* Generate an AST that visits the elements in the domain of "executed"
5072
 * in the relative order specified by the band node "node" and its descendants.
5073
 *
5074
 * The relation "executed" maps the outer generated loop iterators
5075
 * to the domain elements executed by those iterations.
5076
 *
5077
 * If the band is empty, we continue with its descendants.
5078
 * Otherwise, we extend the build and the inverse schedule with
5079
 * the additional space/partial schedule and continue generating
5080
 * an AST in generate_next_level.
5081
 * As soon as we have extended the inverse schedule with the additional
5082
 * partial schedule, we look for equalities that may exists between
5083
 * the old and the new part.
5084
 */
5085
static __isl_give isl_ast_graft_list *build_ast_from_band(
5086
  __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5087
  __isl_take isl_union_map *executed)
5088
595
{
5089
595
  isl_space *space;
5090
595
  isl_multi_union_pw_aff *extra;
5091
595
  isl_union_map *extra_umap;
5092
595
  isl_ast_graft_list *list;
5093
595
  unsigned n1, n2;
5094
595
5095
595
  if (!build || !node || !executed)
5096
0
    goto error;
5097
595
5098
595
  if (isl_schedule_node_band_n_member(node) == 0)
5099
0
    return build_ast_from_child(build, node, executed);
5100
595
5101
595
  extra = isl_schedule_node_band_get_partial_schedule(node);
5102
595
  extra = isl_multi_union_pw_aff_align_params(extra,
5103
595
        isl_ast_build_get_space(build, 1));
5104
595
  space = isl_multi_union_pw_aff_get_space(extra);
5105
595
5106
595
  extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5107
595
  extra_umap = isl_union_map_reverse(extra_umap);
5108
595
5109
595
  executed = isl_union_map_domain_product(executed, extra_umap);
5110
595
  executed = isl_union_map_detect_equalities(executed);
5111
595
5112
595
  n1 = isl_ast_build_dim(build, isl_dim_param);
5113
595
  build = isl_ast_build_product(build, space);
5114
595
  n2 = isl_ast_build_dim(build, isl_dim_param);
5115
595
  if (n2 > n1)
5116
595
    
isl_die0
(isl_ast_build_get_ctx(build), isl_error_invalid,
5117
595
      "band node is not allowed to introduce new parameters",
5118
595
      build = isl_ast_build_free(build));
5119
595
  build = isl_ast_build_set_schedule_node(build, node);
5120
595
5121
595
  list = generate_next_level(executed, build);
5122
595
5123
595
  list = isl_ast_graft_list_unembed(list, 1);
5124
595
5125
595
  return list;
5126
0
error:
5127
0
  isl_schedule_node_free(node);
5128
0
  isl_union_map_free(executed);
5129
0
  isl_ast_build_free(build);
5130
0
  return NULL;
5131
595
}
5132
5133
/* Hoist a list of grafts (in practice containing a single graft)
5134
 * from "sub_build" (which includes extra context information)
5135
 * to "build".
5136
 *
5137
 * In particular, project out all additional parameters introduced
5138
 * by the context node from the enforced constraints and the guard
5139
 * of the single graft.
5140
 */
5141
static __isl_give isl_ast_graft_list *hoist_out_of_context(
5142
  __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5143
  __isl_keep isl_ast_build *sub_build)
5144
0
{
5145
0
  isl_ast_graft *graft;
5146
0
  isl_basic_set *enforced;
5147
0
  isl_set *guard;
5148
0
  unsigned n_param, extra_param;
5149
0
5150
0
  if (!build || !sub_build)
5151
0
    return isl_ast_graft_list_free(list);
5152
0
5153
0
  n_param = isl_ast_build_dim(build, isl_dim_param);
5154
0
  extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5155
0
5156
0
  if (extra_param == n_param)
5157
0
    return list;
5158
0
5159
0
  extra_param -= n_param;
5160
0
  enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5161
0
  enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5162
0
              n_param, extra_param);
5163
0
  enforced = isl_basic_set_remove_unknown_divs(enforced);
5164
0
  guard = isl_ast_graft_list_extract_hoistable_guard(list, sub_build);
5165
0
  guard = isl_set_remove_divs_involving_dims(guard, isl_dim_param,
5166
0
              n_param, extra_param);
5167
0
  guard = isl_set_project_out(guard, isl_dim_param, n_param, extra_param);
5168
0
  guard = isl_set_compute_divs(guard);
5169
0
  graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
5170
0
              build, sub_build);
5171
0
  list = isl_ast_graft_list_from_ast_graft(graft);
5172
0
5173
0
  return list;
5174
0
}
5175
5176
/* Generate an AST that visits the elements in the domain of "executed"
5177
 * in the relative order specified by the context node "node"
5178
 * and its descendants.
5179
 *
5180
 * The relation "executed" maps the outer generated loop iterators
5181
 * to the domain elements executed by those iterations.
5182
 *
5183
 * The context node may introduce additional parameters as well as
5184
 * constraints on the outer schedule dimensions or original parameters.
5185
 *
5186
 * We add the extra parameters to a new build and the context
5187
 * constraints to both the build and (as a single disjunct)
5188
 * to the domain of "executed".  Since the context constraints
5189
 * are specified in terms of the input schedule, we first need
5190
 * to map them to the internal schedule domain.
5191
 *
5192
 * After constructing the AST from the descendants of "node",
5193
 * we combine the list of grafts into a single graft within
5194
 * the new build, in order to be able to exploit the additional
5195
 * context constraints during this combination.
5196
 *
5197
 * Additionally, if the current node is the outermost node in
5198
 * the schedule tree (apart from the root domain node), we generate
5199
 * all pending guards, again to be able to exploit the additional
5200
 * context constraints.  We currently do not do this for internal
5201
 * context nodes since we may still want to hoist conditions
5202
 * to outer AST nodes.
5203
 *
5204
 * If the context node introduced any new parameters, then they
5205
 * are removed from the set of enforced constraints and guard
5206
 * in hoist_out_of_context.
5207
 */
5208
static __isl_give isl_ast_graft_list *build_ast_from_context(
5209
  __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5210
  __isl_take isl_union_map *executed)
5211
0
{
5212
0
  isl_set *context;
5213
0
  isl_space *space;
5214
0
  isl_multi_aff *internal2input;
5215
0
  isl_ast_build *sub_build;
5216
0
  isl_ast_graft_list *list;
5217
0
  int n, depth;
5218
0
5219
0
  depth = isl_schedule_node_get_tree_depth(node);
5220
0
  space = isl_ast_build_get_space(build, 1);
5221
0
  context = isl_schedule_node_context_get_context(node);
5222
0
  context = isl_set_align_params(context, space);
5223
0
  sub_build = isl_ast_build_copy(build);
5224
0
  space = isl_set_get_space(context);
5225
0
  sub_build = isl_ast_build_align_params(sub_build, space);
5226
0
  internal2input = isl_ast_build_get_internal2input(sub_build);
5227
0
  context = isl_set_preimage_multi_aff(context, internal2input);
5228
0
  sub_build = isl_ast_build_restrict_generated(sub_build,
5229
0
          isl_set_copy(context));
5230
0
  context = isl_set_from_basic_set(isl_set_simple_hull(context));
5231
0
  executed = isl_union_map_intersect_domain(executed,
5232
0
          isl_union_set_from_set(context));
5233
0
5234
0
  list = build_ast_from_child(isl_ast_build_copy(sub_build),
5235
0
            node, executed);
5236
0
  n = isl_ast_graft_list_n_ast_graft(list);
5237
0
  if (n < 0)
5238
0
    list = isl_ast_graft_list_free(list);
5239
0
5240
0
  list = isl_ast_graft_list_fuse(list, sub_build);
5241
0
  if (depth == 1)
5242
0
    list = isl_ast_graft_list_insert_pending_guard_nodes(list,
5243
0
                sub_build);
5244
0
  if (n >= 1)
5245
0
    list = hoist_out_of_context(list, build, sub_build);
5246
0
5247
0
  isl_ast_build_free(build);
5248
0
  isl_ast_build_free(sub_build);
5249
0
5250
0
  return list;
5251
0
}
5252
5253
/* Generate an AST that visits the elements in the domain of "executed"
5254
 * in the relative order specified by the expansion node "node" and
5255
 * its descendants.
5256
 *
5257
 * The relation "executed" maps the outer generated loop iterators
5258
 * to the domain elements executed by those iterations.
5259
 *
5260
 * We expand the domain elements by the expansion and
5261
 * continue with the descendants of the node.
5262
 */
5263
static __isl_give isl_ast_graft_list *build_ast_from_expansion(
5264
  __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5265
  __isl_take isl_union_map *executed)
5266
0
{
5267
0
  isl_union_map *expansion;
5268
0
  unsigned n1, n2;
5269
0
5270
0
  expansion = isl_schedule_node_expansion_get_expansion(node);
5271
0
  expansion = isl_union_map_align_params(expansion,
5272
0
        isl_union_map_get_space(executed));
5273
0
5274
0
  n1 = isl_union_map_dim(executed, isl_dim_param);
5275
0
  executed = isl_union_map_apply_range(executed, expansion);
5276
0
  n2 = isl_union_map_dim(executed, isl_dim_param);
5277
0
  if (n2 > n1)
5278
0
    isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
5279
0
      "expansion node is not allowed to introduce "
5280
0
      "new parameters", goto error);
5281
0
5282
0
  return build_ast_from_child(build, node, executed);
5283
0
error:
5284
0
  isl_ast_build_free(build);
5285
0
  isl_schedule_node_free(node);
5286
0
  isl_union_map_free(executed);
5287
0
  return NULL;
5288
0
}
5289
5290
/* Generate an AST that visits the elements in the domain of "executed"
5291
 * in the relative order specified by the extension node "node" and
5292
 * its descendants.
5293
 *
5294
 * The relation "executed" maps the outer generated loop iterators
5295
 * to the domain elements executed by those iterations.
5296
 *
5297
 * Extend the inverse schedule with the extension applied to current
5298
 * set of generated constraints.  Since the extension if formulated
5299
 * in terms of the input schedule, it first needs to be transformed
5300
 * to refer to the internal schedule.
5301
 */
5302
static __isl_give isl_ast_graft_list *build_ast_from_extension(
5303
  __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5304
  __isl_take isl_union_map *executed)
5305
22
{
5306
22
  isl_union_set *schedule_domain;
5307
22
  isl_union_map *extension;
5308
22
  isl_set *set;
5309
22
5310
22
  set = isl_ast_build_get_generated(build);
5311
22
  set = isl_set_from_basic_set(isl_set_simple_hull(set));
5312
22
  schedule_domain = isl_union_set_from_set(set);
5313
22
5314
22
  extension = isl_schedule_node_extension_get_extension(node);
5315
22
5316
22
  extension = isl_union_map_preimage_domain_multi_aff(extension,
5317
22
      isl_multi_aff_copy(build->internal2input));
5318
22
  extension = isl_union_map_intersect_domain(extension, schedule_domain);
5319
22
  extension = isl_ast_build_substitute_values_union_map_domain(build,
5320
22
                    extension);
5321
22
  executed = isl_union_map_union(executed, extension);
5322
22
5323
22
  return build_ast_from_child(build, node, executed);
5324
22
}
5325
5326
/* Generate an AST that visits the elements in the domain of "executed"
5327
 * in the relative order specified by the filter node "node" and
5328
 * its descendants.
5329
 *
5330
 * The relation "executed" maps the outer generated loop iterators
5331
 * to the domain elements executed by those iterations.
5332
 *
5333
 * We simply intersect the iteration domain (i.e., the range of "executed")
5334
 * with the filter and continue with the descendants of the node,
5335
 * unless the resulting inverse schedule is empty, in which
5336
 * case we return an empty list.
5337