1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
| /*
* Copyright 2012-2013 Ecole Normale Superieure
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege,
* Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
*/
#include <isl/val.h>
#include <isl_map_private.h>
#include <isl_aff_private.h>
#include <isl/constraint.h>
#include <isl/set.h>
/* Stride information about a specific set dimension.
* The values of the set dimension are equal to
* "offset" plus a multiple of "stride".
*/
struct isl_stride_info {
isl_val *stride;
isl_aff *offset;
};
/* Return the ctx to which "si" belongs.
*/
isl_ctx *isl_stride_info_get_ctx(__isl_keep isl_stride_info *si)
{
if (!si)
return NULL;
return isl_val_get_ctx(si->stride);
}
/* Free "si" and return NULL.
*/
__isl_null isl_stride_info *isl_stride_info_free(
__isl_take isl_stride_info *si)
{
if (!si)
return NULL;
isl_val_free(si->stride);
isl_aff_free(si->offset);
free(si);
return NULL;
}
/* Construct an isl_stride_info object with given offset and stride.
*/
__isl_give isl_stride_info *isl_stride_info_alloc(
__isl_take isl_val *stride, __isl_take isl_aff *offset)
{
struct isl_stride_info *si;
if (!stride || !offset)
goto error;
si = isl_alloc_type(isl_val_get_ctx(stride), struct isl_stride_info);
if (!si)
goto error;
si->stride = stride;
si->offset = offset;
return si;
error:
isl_val_free(stride);
isl_aff_free(offset);
return NULL;
}
/* Make a copy of "si" and return it.
*/
__isl_give isl_stride_info *isl_stride_info_copy(
__isl_keep isl_stride_info *si)
{
if (!si)
return NULL;
return isl_stride_info_alloc(isl_val_copy(si->stride),
isl_aff_copy(si->offset));
}
/* Return the stride of "si".
*/
__isl_give isl_val *isl_stride_info_get_stride(__isl_keep isl_stride_info *si)
{
if (!si)
return NULL;
return isl_val_copy(si->stride);
}
/* Return the offset of "si".
*/
__isl_give isl_aff *isl_stride_info_get_offset(__isl_keep isl_stride_info *si)
{
if (!si)
return NULL;
return isl_aff_copy(si->offset);
}
/* Information used inside detect_stride.
*
* "pos" is the set dimension at which the stride is being determined.
* "want_offset" is set if the offset should be computed.
* "found" is set if some stride was found already.
* "stride" and "offset" contain the (combined) stride and offset
* found so far and are NULL when "found" is not set.
* If "want_offset" is not set, then "offset" remains NULL.
*/
struct isl_detect_stride_data {
int pos;
int want_offset;
int found;
isl_val *stride;
isl_aff *offset;
};
/* Set the stride and offset of data->pos to the given
* value and expression.
*
* If we had already found a stride before, then the two strides
* are combined into a single stride.
*
* In particular, if the new stride information is of the form
*
* i = f + s (...)
*
* and the old stride information is of the form
*
* i = f2 + s2 (...)
*
* then we compute the extended gcd of s and s2
*
* a s + b s2 = g,
*
* with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
* and the second with t2 = a s1/g.
* This results in
*
* i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
*
* so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
* is the combined stride.
*/
static isl_stat set_stride(struct isl_detect_stride_data *data,
__isl_take isl_val *stride, __isl_take isl_aff *offset)
{
int pos;
if (!stride || !offset)
goto error;
pos = data->pos;
if (data->found) {
isl_val *stride2, *a, *b, *g;
isl_aff *offset2;
stride2 = data->stride;
g = isl_val_gcdext(isl_val_copy(stride), isl_val_copy(stride2),
&a, &b);
a = isl_val_mul(a, isl_val_copy(stride));
a = isl_val_div(a, isl_val_copy(g));
stride2 = isl_val_div(stride2, g);
b = isl_val_mul(b, isl_val_copy(stride2));
stride = isl_val_mul(stride, stride2);
if (!data->want_offset) {
isl_val_free(a);
isl_val_free(b);
} else {
offset2 = data->offset;
offset2 = isl_aff_scale_val(offset2, a);
offset = isl_aff_scale_val(offset, b);
offset = isl_aff_add(offset, offset2);
}
}
data->found = 1;
data->stride = stride;
if (data->want_offset)
data->offset = offset;
else
isl_aff_free(offset);
if (!data->stride || (data->want_offset && !data->offset))
return isl_stat_error;
return isl_stat_ok;
error:
isl_val_free(stride);
isl_aff_free(offset);
return isl_stat_error;
}
/* Check if constraint "c" imposes any stride on dimension data->pos
* and, if so, update the stride information in "data".
*
* In order to impose a stride on the dimension, "c" needs to be an equality
* and it needs to involve the dimension. Note that "c" may also be
* a div constraint and thus an inequality that we cannot use.
*
* Let c be of the form
*
* h(p) + g * v * i + g * stride * f(alpha) = 0
*
* with h(p) an expression in terms of the parameters and other dimensions
* and f(alpha) an expression in terms of the existentially quantified
* variables.
*
* If "stride" is not zero and not one, then it represents a non-trivial stride
* on "i". We compute a and b such that
*
* a v + b stride = 1
*
* We have
*
* g v i = -h(p) + g stride f(alpha)
*
* a g v i = -a h(p) + g stride f(alpha)
*
* a g v i + b g stride i = -a h(p) + g stride * (...)
*
* g i = -a h(p) + g stride * (...)
*
* i = -a h(p)/g + stride * (...)
*
* The expression "-a h(p)/g" can therefore be used as offset.
*/
static isl_stat detect_stride(__isl_take isl_constraint *c, void *user)
{
struct isl_detect_stride_data *data = user;
int i, n_div;
isl_ctx *ctx;
isl_stat r = isl_stat_ok;
isl_val *v, *stride, *m;
isl_bool is_eq, relevant, has_stride;
is_eq = isl_constraint_is_equality(c);
relevant = isl_constraint_involves_dims(c, isl_dim_set, data->pos, 1);
if (is_eq < 0 || relevant < 0)
goto error;
if (!is_eq || !relevant) {
isl_constraint_free(c);
return isl_stat_ok;
}
ctx = isl_constraint_get_ctx(c);
stride = isl_val_zero(ctx);
n_div = isl_constraint_dim(c, isl_dim_div);
for (i = 0; i < n_div; ++i) {
v = isl_constraint_get_coefficient_val(c, isl_dim_div, i);
stride = isl_val_gcd(stride, v);
}
v = isl_constraint_get_coefficient_val(c, isl_dim_set, data->pos);
m = isl_val_gcd(isl_val_copy(stride), isl_val_copy(v));
stride = isl_val_div(stride, isl_val_copy(m));
v = isl_val_div(v, isl_val_copy(m));
has_stride = isl_val_gt_si(stride, 1);
if (has_stride >= 0 && has_stride) {
isl_aff *aff;
isl_val *gcd, *a, *b;
gcd = isl_val_gcdext(v, isl_val_copy(stride), &a, &b);
isl_val_free(gcd);
isl_val_free(b);
aff = isl_constraint_get_aff(c);
for (i = 0; i < n_div; ++i)
aff = isl_aff_set_coefficient_si(aff,
isl_dim_div, i, 0);
aff = isl_aff_set_coefficient_si(aff, isl_dim_in, data->pos, 0);
aff = isl_aff_remove_unused_divs(aff);
a = isl_val_neg(a);
aff = isl_aff_scale_val(aff, a);
aff = isl_aff_scale_down_val(aff, m);
r = set_stride(data, stride, aff);
} else {
isl_val_free(stride);
isl_val_free(m);
isl_val_free(v);
}
isl_constraint_free(c);
if (has_stride < 0)
return isl_stat_error;
return r;
error:
isl_constraint_free(c);
return isl_stat_error;
}
/* Check if the constraints in "set" imply any stride on set dimension "pos" and
* store the results in data->stride and data->offset.
*
* In particular, compute the affine hull and then check if
* any of the constraints in the hull impose any stride on the dimension.
* If no such constraint can be found, then the offset is taken
* to be the zero expression and the stride is taken to be one.
*/
static void set_detect_stride(__isl_keep isl_set *set, int pos,
struct isl_detect_stride_data *data)
{
isl_basic_set *hull;
hull = isl_set_affine_hull(isl_set_copy(set));
data->pos = pos;
data->found = 0;
data->stride = NULL;
data->offset = NULL;
if (isl_basic_set_foreach_constraint(hull, &detect_stride, data) < 0)
goto error;
if (!data->found) {
data->stride = isl_val_one(isl_set_get_ctx(set));
if (data->want_offset) {
isl_space *space;
isl_local_space *ls;
space = isl_set_get_space(set);
ls = isl_local_space_from_space(space);
data->offset = isl_aff_zero_on_domain(ls);
}
}
isl_basic_set_free(hull);
return;
error:
isl_basic_set_free(hull);
data->stride = isl_val_free(data->stride);
data->offset = isl_aff_free(data->offset);
}
/* Check if the constraints in "set" imply any stride on set dimension "pos" and
* return the results in the form of an offset and a stride.
*/
__isl_give isl_stride_info *isl_set_get_stride_info(__isl_keep isl_set *set,
int pos)
{
struct isl_detect_stride_data data;
data.want_offset = 1;
set_detect_stride(set, pos, &data);
return isl_stride_info_alloc(data.stride, data.offset);
}
/* Check if the constraints in "set" imply any stride on set dimension "pos" and
* return this stride.
*/
__isl_give isl_val *isl_set_get_stride(__isl_keep isl_set *set, int pos)
{
struct isl_detect_stride_data data;
data.want_offset = 0;
set_detect_stride(set, pos, &data);
return data.stride;
}
/* Check if the constraints in "map" imply any stride on output dimension "pos",
* independently of any other output dimensions, and
* return the results in the form of an offset and a stride.
*
* Convert the input to a set with only the input dimensions and
* the single output dimension such that it be passed to
* isl_set_get_stride_info and convert the result back to
* an expression defined over the domain of "map".
*/
__isl_give isl_stride_info *isl_map_get_range_stride_info(
__isl_keep isl_map *map, int pos)
{
isl_stride_info *si;
isl_set *set;
map = isl_map_copy(map);
map = isl_map_project_onto(map, isl_dim_out, pos, 1);
pos = isl_map_dim(map, isl_dim_in);
set = isl_map_wrap(map);
si = isl_set_get_stride_info(set, pos);
isl_set_free(set);
if (!si)
return NULL;
si->offset = isl_aff_domain_factor_domain(si->offset);
if (!si->offset)
return isl_stride_info_free(si);
return si;
}
|