1 //===------ ISLTools.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Tools, utilities, helpers and extensions useful in conjunction with the
10 // Integer Set Library (isl).
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "polly/Support/ISLTools.h"
15 #include "polly/Support/GICHelper.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <cassert>
18 #include <vector>
19
20 using namespace polly;
21
22 namespace {
23 /// Create a map that shifts one dimension by an offset.
24 ///
25 /// Example:
26 /// makeShiftDimAff({ [i0, i1] -> [o0, o1] }, 1, -2)
27 /// = { [i0, i1] -> [i0, i1 - 1] }
28 ///
29 /// @param Space The map space of the result. Must have equal number of in- and
30 /// out-dimensions.
31 /// @param Pos Position to shift.
32 /// @param Amount Value added to the shifted dimension.
33 ///
34 /// @return An isl_multi_aff for the map with this shifted dimension.
makeShiftDimAff(isl::space Space,int Pos,int Amount)35 isl::multi_aff makeShiftDimAff(isl::space Space, int Pos, int Amount) {
36 auto Identity = isl::multi_aff::identity(Space);
37 if (Amount == 0)
38 return Identity;
39 auto ShiftAff = Identity.at(Pos);
40 ShiftAff = ShiftAff.set_constant_si(Amount);
41 return Identity.set_aff(Pos, ShiftAff);
42 }
43
44 /// Construct a map that swaps two nested tuples.
45 ///
46 /// @param FromSpace1 { Space1[] }
47 /// @param FromSpace2 { Space2[] }
48 ///
49 /// @return { [Space1[] -> Space2[]] -> [Space2[] -> Space1[]] }
makeTupleSwapBasicMap(isl::space FromSpace1,isl::space FromSpace2)50 isl::basic_map makeTupleSwapBasicMap(isl::space FromSpace1,
51 isl::space FromSpace2) {
52 // Fast-path on out-of-quota.
53 if (FromSpace1.is_null() || FromSpace2.is_null())
54 return {};
55
56 assert(FromSpace1.is_set());
57 assert(FromSpace2.is_set());
58
59 unsigned Dims1 = unsignedFromIslSize(FromSpace1.dim(isl::dim::set));
60 unsigned Dims2 = unsignedFromIslSize(FromSpace2.dim(isl::dim::set));
61
62 isl::space FromSpace =
63 FromSpace1.map_from_domain_and_range(FromSpace2).wrap();
64 isl::space ToSpace = FromSpace2.map_from_domain_and_range(FromSpace1).wrap();
65 isl::space MapSpace = FromSpace.map_from_domain_and_range(ToSpace);
66
67 isl::basic_map Result = isl::basic_map::universe(MapSpace);
68 for (unsigned i = 0u; i < Dims1; i += 1)
69 Result = Result.equate(isl::dim::in, i, isl::dim::out, Dims2 + i);
70 for (unsigned i = 0u; i < Dims2; i += 1) {
71 Result = Result.equate(isl::dim::in, Dims1 + i, isl::dim::out, i);
72 }
73
74 return Result;
75 }
76
77 /// Like makeTupleSwapBasicMap(isl::space,isl::space), but returns
78 /// an isl_map.
makeTupleSwapMap(isl::space FromSpace1,isl::space FromSpace2)79 isl::map makeTupleSwapMap(isl::space FromSpace1, isl::space FromSpace2) {
80 isl::basic_map BMapResult = makeTupleSwapBasicMap(FromSpace1, FromSpace2);
81 return isl::map(BMapResult);
82 }
83 } // anonymous namespace
84
beforeScatter(isl::map Map,bool Strict)85 isl::map polly::beforeScatter(isl::map Map, bool Strict) {
86 isl::space RangeSpace = Map.get_space().range();
87 isl::map ScatterRel =
88 Strict ? isl::map::lex_gt(RangeSpace) : isl::map::lex_ge(RangeSpace);
89 return Map.apply_range(ScatterRel);
90 }
91
beforeScatter(isl::union_map UMap,bool Strict)92 isl::union_map polly::beforeScatter(isl::union_map UMap, bool Strict) {
93 isl::union_map Result = isl::union_map::empty(UMap.ctx());
94
95 for (isl::map Map : UMap.get_map_list()) {
96 isl::map After = beforeScatter(Map, Strict);
97 Result = Result.unite(After);
98 }
99
100 return Result;
101 }
102
afterScatter(isl::map Map,bool Strict)103 isl::map polly::afterScatter(isl::map Map, bool Strict) {
104 isl::space RangeSpace = Map.get_space().range();
105 isl::map ScatterRel =
106 Strict ? isl::map::lex_lt(RangeSpace) : isl::map::lex_le(RangeSpace);
107 return Map.apply_range(ScatterRel);
108 }
109
afterScatter(const isl::union_map & UMap,bool Strict)110 isl::union_map polly::afterScatter(const isl::union_map &UMap, bool Strict) {
111 isl::union_map Result = isl::union_map::empty(UMap.ctx());
112 for (isl::map Map : UMap.get_map_list()) {
113 isl::map After = afterScatter(Map, Strict);
114 Result = Result.unite(After);
115 }
116 return Result;
117 }
118
betweenScatter(isl::map From,isl::map To,bool InclFrom,bool InclTo)119 isl::map polly::betweenScatter(isl::map From, isl::map To, bool InclFrom,
120 bool InclTo) {
121 isl::map AfterFrom = afterScatter(From, !InclFrom);
122 isl::map BeforeTo = beforeScatter(To, !InclTo);
123
124 return AfterFrom.intersect(BeforeTo);
125 }
126
betweenScatter(isl::union_map From,isl::union_map To,bool InclFrom,bool InclTo)127 isl::union_map polly::betweenScatter(isl::union_map From, isl::union_map To,
128 bool InclFrom, bool InclTo) {
129 isl::union_map AfterFrom = afterScatter(From, !InclFrom);
130 isl::union_map BeforeTo = beforeScatter(To, !InclTo);
131
132 return AfterFrom.intersect(BeforeTo);
133 }
134
singleton(isl::union_map UMap,isl::space ExpectedSpace)135 isl::map polly::singleton(isl::union_map UMap, isl::space ExpectedSpace) {
136 if (UMap.is_null())
137 return {};
138
139 if (isl_union_map_n_map(UMap.get()) == 0)
140 return isl::map::empty(ExpectedSpace);
141
142 isl::map Result = isl::map::from_union_map(UMap);
143 assert(Result.is_null() ||
144 Result.get_space().has_equal_tuples(ExpectedSpace));
145
146 return Result;
147 }
148
singleton(isl::union_set USet,isl::space ExpectedSpace)149 isl::set polly::singleton(isl::union_set USet, isl::space ExpectedSpace) {
150 if (USet.is_null())
151 return {};
152
153 if (isl_union_set_n_set(USet.get()) == 0)
154 return isl::set::empty(ExpectedSpace);
155
156 isl::set Result(USet);
157 assert(Result.is_null() ||
158 Result.get_space().has_equal_tuples(ExpectedSpace));
159
160 return Result;
161 }
162
getNumScatterDims(const isl::union_map & Schedule)163 unsigned polly::getNumScatterDims(const isl::union_map &Schedule) {
164 unsigned Dims = 0;
165 for (isl::map Map : Schedule.get_map_list()) {
166 if (Map.is_null())
167 continue;
168
169 Dims = std::max(Dims, unsignedFromIslSize(Map.range_tuple_dim()));
170 }
171 return Dims;
172 }
173
getScatterSpace(const isl::union_map & Schedule)174 isl::space polly::getScatterSpace(const isl::union_map &Schedule) {
175 if (Schedule.is_null())
176 return {};
177 unsigned Dims = getNumScatterDims(Schedule);
178 isl::space ScatterSpace = Schedule.get_space().set_from_params();
179 return ScatterSpace.add_dims(isl::dim::set, Dims);
180 }
181
makeIdentityMap(const isl::set & Set,bool RestrictDomain)182 isl::map polly::makeIdentityMap(const isl::set &Set, bool RestrictDomain) {
183 isl::map Result = isl::map::identity(Set.get_space().map_from_set());
184 if (RestrictDomain)
185 Result = Result.intersect_domain(Set);
186 return Result;
187 }
188
makeIdentityMap(const isl::union_set & USet,bool RestrictDomain)189 isl::union_map polly::makeIdentityMap(const isl::union_set &USet,
190 bool RestrictDomain) {
191 isl::union_map Result = isl::union_map::empty(USet.ctx());
192 for (isl::set Set : USet.get_set_list()) {
193 isl::map IdentityMap = makeIdentityMap(Set, RestrictDomain);
194 Result = Result.unite(IdentityMap);
195 }
196 return Result;
197 }
198
reverseDomain(isl::map Map)199 isl::map polly::reverseDomain(isl::map Map) {
200 isl::space DomSpace = Map.get_space().domain().unwrap();
201 isl::space Space1 = DomSpace.domain();
202 isl::space Space2 = DomSpace.range();
203 isl::map Swap = makeTupleSwapMap(Space1, Space2);
204 return Map.apply_domain(Swap);
205 }
206
reverseDomain(const isl::union_map & UMap)207 isl::union_map polly::reverseDomain(const isl::union_map &UMap) {
208 isl::union_map Result = isl::union_map::empty(UMap.ctx());
209 for (isl::map Map : UMap.get_map_list()) {
210 auto Reversed = reverseDomain(std::move(Map));
211 Result = Result.unite(Reversed);
212 }
213 return Result;
214 }
215
shiftDim(isl::set Set,int Pos,int Amount)216 isl::set polly::shiftDim(isl::set Set, int Pos, int Amount) {
217 unsigned NumDims = unsignedFromIslSize(Set.tuple_dim());
218 if (Pos < 0)
219 Pos = NumDims + Pos;
220 assert(unsigned(Pos) < NumDims && "Dimension index must be in range");
221 isl::space Space = Set.get_space();
222 Space = Space.map_from_domain_and_range(Space);
223 isl::multi_aff Translator = makeShiftDimAff(Space, Pos, Amount);
224 isl::map TranslatorMap = isl::map::from_multi_aff(Translator);
225 return Set.apply(TranslatorMap);
226 }
227
shiftDim(isl::union_set USet,int Pos,int Amount)228 isl::union_set polly::shiftDim(isl::union_set USet, int Pos, int Amount) {
229 isl::union_set Result = isl::union_set::empty(USet.ctx());
230 for (isl::set Set : USet.get_set_list()) {
231 isl::set Shifted = shiftDim(Set, Pos, Amount);
232 Result = Result.unite(Shifted);
233 }
234 return Result;
235 }
236
shiftDim(isl::map Map,isl::dim Dim,int Pos,int Amount)237 isl::map polly::shiftDim(isl::map Map, isl::dim Dim, int Pos, int Amount) {
238 unsigned NumDims = unsignedFromIslSize(Map.dim(Dim));
239 if (Pos < 0)
240 Pos = NumDims + Pos;
241 assert(unsigned(Pos) < NumDims && "Dimension index must be in range");
242 isl::space Space = Map.get_space();
243 switch (Dim) {
244 case isl::dim::in:
245 Space = Space.domain();
246 break;
247 case isl::dim::out:
248 Space = Space.range();
249 break;
250 default:
251 llvm_unreachable("Unsupported value for 'dim'");
252 }
253 Space = Space.map_from_domain_and_range(Space);
254 isl::multi_aff Translator = makeShiftDimAff(Space, Pos, Amount);
255 isl::map TranslatorMap = isl::map::from_multi_aff(Translator);
256 switch (Dim) {
257 case isl::dim::in:
258 return Map.apply_domain(TranslatorMap);
259 case isl::dim::out:
260 return Map.apply_range(TranslatorMap);
261 default:
262 llvm_unreachable("Unsupported value for 'dim'");
263 }
264 }
265
shiftDim(isl::union_map UMap,isl::dim Dim,int Pos,int Amount)266 isl::union_map polly::shiftDim(isl::union_map UMap, isl::dim Dim, int Pos,
267 int Amount) {
268 isl::union_map Result = isl::union_map::empty(UMap.ctx());
269
270 for (isl::map Map : UMap.get_map_list()) {
271 isl::map Shifted = shiftDim(Map, Dim, Pos, Amount);
272 Result = Result.unite(Shifted);
273 }
274 return Result;
275 }
276
simplify(isl::set & Set)277 void polly::simplify(isl::set &Set) {
278 Set = isl::manage(isl_set_compute_divs(Set.copy()));
279 Set = Set.detect_equalities();
280 Set = Set.coalesce();
281 }
282
simplify(isl::union_set & USet)283 void polly::simplify(isl::union_set &USet) {
284 USet = isl::manage(isl_union_set_compute_divs(USet.copy()));
285 USet = USet.detect_equalities();
286 USet = USet.coalesce();
287 }
288
simplify(isl::map & Map)289 void polly::simplify(isl::map &Map) {
290 Map = isl::manage(isl_map_compute_divs(Map.copy()));
291 Map = Map.detect_equalities();
292 Map = Map.coalesce();
293 }
294
simplify(isl::union_map & UMap)295 void polly::simplify(isl::union_map &UMap) {
296 UMap = isl::manage(isl_union_map_compute_divs(UMap.copy()));
297 UMap = UMap.detect_equalities();
298 UMap = UMap.coalesce();
299 }
300
computeReachingWrite(isl::union_map Schedule,isl::union_map Writes,bool Reverse,bool InclPrevDef,bool InclNextDef)301 isl::union_map polly::computeReachingWrite(isl::union_map Schedule,
302 isl::union_map Writes, bool Reverse,
303 bool InclPrevDef, bool InclNextDef) {
304
305 // { Scatter[] }
306 isl::space ScatterSpace = getScatterSpace(Schedule);
307
308 // { ScatterRead[] -> ScatterWrite[] }
309 isl::map Relation;
310 if (Reverse)
311 Relation = InclPrevDef ? isl::map::lex_lt(ScatterSpace)
312 : isl::map::lex_le(ScatterSpace);
313 else
314 Relation = InclNextDef ? isl::map::lex_gt(ScatterSpace)
315 : isl::map::lex_ge(ScatterSpace);
316
317 // { ScatterWrite[] -> [ScatterRead[] -> ScatterWrite[]] }
318 isl::map RelationMap = Relation.range_map().reverse();
319
320 // { Element[] -> ScatterWrite[] }
321 isl::union_map WriteAction = Schedule.apply_domain(Writes);
322
323 // { ScatterWrite[] -> Element[] }
324 isl::union_map WriteActionRev = WriteAction.reverse();
325
326 // { Element[] -> [ScatterUse[] -> ScatterWrite[]] }
327 isl::union_map DefSchedRelation =
328 isl::union_map(RelationMap).apply_domain(WriteActionRev);
329
330 // For each element, at every point in time, map to the times of previous
331 // definitions. { [Element[] -> ScatterRead[]] -> ScatterWrite[] }
332 isl::union_map ReachableWrites = DefSchedRelation.uncurry();
333 if (Reverse)
334 ReachableWrites = ReachableWrites.lexmin();
335 else
336 ReachableWrites = ReachableWrites.lexmax();
337
338 // { [Element[] -> ScatterWrite[]] -> ScatterWrite[] }
339 isl::union_map SelfUse = WriteAction.range_map();
340
341 if (InclPrevDef && InclNextDef) {
342 // Add the Def itself to the solution.
343 ReachableWrites = ReachableWrites.unite(SelfUse).coalesce();
344 } else if (!InclPrevDef && !InclNextDef) {
345 // Remove Def itself from the solution.
346 ReachableWrites = ReachableWrites.subtract(SelfUse);
347 }
348
349 // { [Element[] -> ScatterRead[]] -> Domain[] }
350 return ReachableWrites.apply_range(Schedule.reverse());
351 }
352
353 isl::union_map
computeArrayUnused(isl::union_map Schedule,isl::union_map Writes,isl::union_map Reads,bool ReadEltInSameInst,bool IncludeLastRead,bool IncludeWrite)354 polly::computeArrayUnused(isl::union_map Schedule, isl::union_map Writes,
355 isl::union_map Reads, bool ReadEltInSameInst,
356 bool IncludeLastRead, bool IncludeWrite) {
357 // { Element[] -> Scatter[] }
358 isl::union_map ReadActions = Schedule.apply_domain(Reads);
359 isl::union_map WriteActions = Schedule.apply_domain(Writes);
360
361 // { [Element[] -> DomainWrite[]] -> Scatter[] }
362 isl::union_map EltDomWrites =
363 Writes.reverse().range_map().apply_range(Schedule);
364
365 // { [Element[] -> Scatter[]] -> DomainWrite[] }
366 isl::union_map ReachingOverwrite = computeReachingWrite(
367 Schedule, Writes, true, ReadEltInSameInst, !ReadEltInSameInst);
368
369 // { [Element[] -> Scatter[]] -> DomainWrite[] }
370 isl::union_map ReadsOverwritten =
371 ReachingOverwrite.intersect_domain(ReadActions.wrap());
372
373 // { [Element[] -> DomainWrite[]] -> Scatter[] }
374 isl::union_map ReadsOverwrittenRotated =
375 reverseDomain(ReadsOverwritten).curry().reverse();
376 isl::union_map LastOverwrittenRead = ReadsOverwrittenRotated.lexmax();
377
378 // { [Element[] -> DomainWrite[]] -> Scatter[] }
379 isl::union_map BetweenLastReadOverwrite = betweenScatter(
380 LastOverwrittenRead, EltDomWrites, IncludeLastRead, IncludeWrite);
381
382 // { [Element[] -> Scatter[]] -> DomainWrite[] }
383 isl::union_map ReachingOverwriteZone = computeReachingWrite(
384 Schedule, Writes, true, IncludeLastRead, IncludeWrite);
385
386 // { [Element[] -> DomainWrite[]] -> Scatter[] }
387 isl::union_map ReachingOverwriteRotated =
388 reverseDomain(ReachingOverwriteZone).curry().reverse();
389
390 // { [Element[] -> DomainWrite[]] -> Scatter[] }
391 isl::union_map WritesWithoutReads = ReachingOverwriteRotated.subtract_domain(
392 ReadsOverwrittenRotated.domain());
393
394 return BetweenLastReadOverwrite.unite(WritesWithoutReads)
395 .domain_factor_domain();
396 }
397
convertZoneToTimepoints(isl::union_set Zone,bool InclStart,bool InclEnd)398 isl::union_set polly::convertZoneToTimepoints(isl::union_set Zone,
399 bool InclStart, bool InclEnd) {
400 if (!InclStart && InclEnd)
401 return Zone;
402
403 auto ShiftedZone = shiftDim(Zone, -1, -1);
404 if (InclStart && !InclEnd)
405 return ShiftedZone;
406 else if (!InclStart && !InclEnd)
407 return Zone.intersect(ShiftedZone);
408
409 assert(InclStart && InclEnd);
410 return Zone.unite(ShiftedZone);
411 }
412
convertZoneToTimepoints(isl::union_map Zone,isl::dim Dim,bool InclStart,bool InclEnd)413 isl::union_map polly::convertZoneToTimepoints(isl::union_map Zone, isl::dim Dim,
414 bool InclStart, bool InclEnd) {
415 if (!InclStart && InclEnd)
416 return Zone;
417
418 auto ShiftedZone = shiftDim(Zone, Dim, -1, -1);
419 if (InclStart && !InclEnd)
420 return ShiftedZone;
421 else if (!InclStart && !InclEnd)
422 return Zone.intersect(ShiftedZone);
423
424 assert(InclStart && InclEnd);
425 return Zone.unite(ShiftedZone);
426 }
427
convertZoneToTimepoints(isl::map Zone,isl::dim Dim,bool InclStart,bool InclEnd)428 isl::map polly::convertZoneToTimepoints(isl::map Zone, isl::dim Dim,
429 bool InclStart, bool InclEnd) {
430 if (!InclStart && InclEnd)
431 return Zone;
432
433 auto ShiftedZone = shiftDim(Zone, Dim, -1, -1);
434 if (InclStart && !InclEnd)
435 return ShiftedZone;
436 else if (!InclStart && !InclEnd)
437 return Zone.intersect(ShiftedZone);
438
439 assert(InclStart && InclEnd);
440 return Zone.unite(ShiftedZone);
441 }
442
distributeDomain(isl::map Map)443 isl::map polly::distributeDomain(isl::map Map) {
444 // Note that we cannot take Map apart into { Domain[] -> Range1[] } and {
445 // Domain[] -> Range2[] } and combine again. We would loose any relation
446 // between Range1[] and Range2[] that is not also a constraint to Domain[].
447
448 isl::space Space = Map.get_space();
449 isl::space DomainSpace = Space.domain();
450 if (DomainSpace.is_null())
451 return {};
452 unsigned DomainDims = unsignedFromIslSize(DomainSpace.dim(isl::dim::set));
453 isl::space RangeSpace = Space.range().unwrap();
454 isl::space Range1Space = RangeSpace.domain();
455 if (Range1Space.is_null())
456 return {};
457 unsigned Range1Dims = unsignedFromIslSize(Range1Space.dim(isl::dim::set));
458 isl::space Range2Space = RangeSpace.range();
459 if (Range2Space.is_null())
460 return {};
461 unsigned Range2Dims = unsignedFromIslSize(Range2Space.dim(isl::dim::set));
462
463 isl::space OutputSpace =
464 DomainSpace.map_from_domain_and_range(Range1Space)
465 .wrap()
466 .map_from_domain_and_range(
467 DomainSpace.map_from_domain_and_range(Range2Space).wrap());
468
469 isl::basic_map Translator = isl::basic_map::universe(
470 Space.wrap().map_from_domain_and_range(OutputSpace.wrap()));
471
472 for (unsigned i = 0; i < DomainDims; i += 1) {
473 Translator = Translator.equate(isl::dim::in, i, isl::dim::out, i);
474 Translator = Translator.equate(isl::dim::in, i, isl::dim::out,
475 DomainDims + Range1Dims + i);
476 }
477 for (unsigned i = 0; i < Range1Dims; i += 1)
478 Translator = Translator.equate(isl::dim::in, DomainDims + i, isl::dim::out,
479 DomainDims + i);
480 for (unsigned i = 0; i < Range2Dims; i += 1)
481 Translator = Translator.equate(isl::dim::in, DomainDims + Range1Dims + i,
482 isl::dim::out,
483 DomainDims + Range1Dims + DomainDims + i);
484
485 return Map.wrap().apply(Translator).unwrap();
486 }
487
distributeDomain(isl::union_map UMap)488 isl::union_map polly::distributeDomain(isl::union_map UMap) {
489 isl::union_map Result = isl::union_map::empty(UMap.ctx());
490 for (isl::map Map : UMap.get_map_list()) {
491 auto Distributed = distributeDomain(Map);
492 Result = Result.unite(Distributed);
493 }
494 return Result;
495 }
496
liftDomains(isl::union_map UMap,isl::union_set Factor)497 isl::union_map polly::liftDomains(isl::union_map UMap, isl::union_set Factor) {
498
499 // { Factor[] -> Factor[] }
500 isl::union_map Factors = makeIdentityMap(Factor, true);
501
502 return Factors.product(UMap);
503 }
504
applyDomainRange(isl::union_map UMap,isl::union_map Func)505 isl::union_map polly::applyDomainRange(isl::union_map UMap,
506 isl::union_map Func) {
507 // This implementation creates unnecessary cross products of the
508 // DomainDomain[] and Func. An alternative implementation could reverse
509 // domain+uncurry,apply Func to what now is the domain, then undo the
510 // preparing transformation. Another alternative implementation could create a
511 // translator map for each piece.
512
513 // { DomainDomain[] }
514 isl::union_set DomainDomain = UMap.domain().unwrap().domain();
515
516 // { [DomainDomain[] -> DomainRange[]] -> [DomainDomain[] -> NewDomainRange[]]
517 // }
518 isl::union_map LifetedFunc = liftDomains(std::move(Func), DomainDomain);
519
520 return UMap.apply_domain(LifetedFunc);
521 }
522
intersectRange(isl::map Map,isl::union_set Range)523 isl::map polly::intersectRange(isl::map Map, isl::union_set Range) {
524 isl::set RangeSet = Range.extract_set(Map.get_space().range());
525 return Map.intersect_range(RangeSet);
526 }
527
subtractParams(isl::map Map,isl::set Params)528 isl::map polly::subtractParams(isl::map Map, isl::set Params) {
529 auto MapSpace = Map.get_space();
530 auto ParamsMap = isl::map::universe(MapSpace).intersect_params(Params);
531 return Map.subtract(ParamsMap);
532 }
533
subtractParams(isl::set Set,isl::set Params)534 isl::set polly::subtractParams(isl::set Set, isl::set Params) {
535 isl::space SetSpace = Set.get_space();
536 isl::set ParamsSet = isl::set::universe(SetSpace).intersect_params(Params);
537 return Set.subtract(ParamsSet);
538 }
539
getConstant(isl::pw_aff PwAff,bool Max,bool Min)540 isl::val polly::getConstant(isl::pw_aff PwAff, bool Max, bool Min) {
541 assert(!Max || !Min); // Cannot return min and max at the same time.
542 isl::val Result;
543 isl::stat Stat = PwAff.foreach_piece(
544 [=, &Result](isl::set Set, isl::aff Aff) -> isl::stat {
545 if (!Result.is_null() && Result.is_nan())
546 return isl::stat::ok();
547
548 // TODO: If Min/Max, we can also determine a minimum/maximum value if
549 // Set is constant-bounded.
550 if (!Aff.is_cst()) {
551 Result = isl::val::nan(Aff.ctx());
552 return isl::stat::error();
553 }
554
555 isl::val ThisVal = Aff.get_constant_val();
556 if (Result.is_null()) {
557 Result = ThisVal;
558 return isl::stat::ok();
559 }
560
561 if (Result.eq(ThisVal))
562 return isl::stat::ok();
563
564 if (Max && ThisVal.gt(Result)) {
565 Result = ThisVal;
566 return isl::stat::ok();
567 }
568
569 if (Min && ThisVal.lt(Result)) {
570 Result = ThisVal;
571 return isl::stat::ok();
572 }
573
574 // Not compatible
575 Result = isl::val::nan(Aff.ctx());
576 return isl::stat::error();
577 });
578
579 if (Stat.is_error())
580 return {};
581
582 return Result;
583 }
584
rangeIslSize(unsigned Begin,isl::size End)585 llvm::iota_range<unsigned> polly::rangeIslSize(unsigned Begin, isl::size End) {
586 unsigned UEnd = unsignedFromIslSize(End);
587 return llvm::seq<unsigned>(std::min(Begin, UEnd), UEnd);
588 }
589
590 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
foreachPoint(const isl::set & Set,const std::function<void (isl::point P)> & F)591 static void foreachPoint(const isl::set &Set,
592 const std::function<void(isl::point P)> &F) {
593 Set.foreach_point([&](isl::point P) -> isl::stat {
594 F(P);
595 return isl::stat::ok();
596 });
597 }
598
foreachPoint(isl::basic_set BSet,const std::function<void (isl::point P)> & F)599 static void foreachPoint(isl::basic_set BSet,
600 const std::function<void(isl::point P)> &F) {
601 foreachPoint(isl::set(BSet), F);
602 }
603
604 /// Determine the sorting order of the sets @p A and @p B without considering
605 /// the space structure.
606 ///
607 /// Ordering is based on the lower bounds of the set's dimensions. First
608 /// dimensions are considered first.
flatCompare(const isl::basic_set & A,const isl::basic_set & B)609 static int flatCompare(const isl::basic_set &A, const isl::basic_set &B) {
610 // Quick bail-out on out-of-quota.
611 if (A.is_null() || B.is_null())
612 return 0;
613
614 unsigned ALen = unsignedFromIslSize(A.dim(isl::dim::set));
615 unsigned BLen = unsignedFromIslSize(B.dim(isl::dim::set));
616 unsigned Len = std::min(ALen, BLen);
617
618 for (unsigned i = 0; i < Len; i += 1) {
619 isl::basic_set ADim =
620 A.project_out(isl::dim::param, 0,
621 unsignedFromIslSize(A.dim(isl::dim::param)))
622 .project_out(isl::dim::set, i + 1, ALen - i - 1)
623 .project_out(isl::dim::set, 0, i);
624 isl::basic_set BDim =
625 B.project_out(isl::dim::param, 0,
626 unsignedFromIslSize(B.dim(isl::dim::param)))
627 .project_out(isl::dim::set, i + 1, BLen - i - 1)
628 .project_out(isl::dim::set, 0, i);
629
630 isl::basic_set AHull = isl::set(ADim).convex_hull();
631 isl::basic_set BHull = isl::set(BDim).convex_hull();
632
633 bool ALowerBounded =
634 bool(isl::set(AHull).dim_has_any_lower_bound(isl::dim::set, 0));
635 bool BLowerBounded =
636 bool(isl::set(BHull).dim_has_any_lower_bound(isl::dim::set, 0));
637
638 int BoundedCompare = BLowerBounded - ALowerBounded;
639 if (BoundedCompare != 0)
640 return BoundedCompare;
641
642 if (!ALowerBounded || !BLowerBounded)
643 continue;
644
645 isl::pw_aff AMin = isl::set(ADim).dim_min(0);
646 isl::pw_aff BMin = isl::set(BDim).dim_min(0);
647
648 isl::val AMinVal = polly::getConstant(AMin, false, true);
649 isl::val BMinVal = polly::getConstant(BMin, false, true);
650
651 int MinCompare = AMinVal.sub(BMinVal).sgn();
652 if (MinCompare != 0)
653 return MinCompare;
654 }
655
656 // If all the dimensions' lower bounds are equal or incomparable, sort based
657 // on the number of dimensions.
658 return ALen - BLen;
659 }
660
661 /// Compare the sets @p A and @p B according to their nested space structure.
662 /// Returns 0 if the structure is considered equal.
663 /// If @p ConsiderTupleLen is false, the number of dimensions in a tuple are
664 /// ignored, i.e. a tuple with the same name but different number of dimensions
665 /// are considered equal.
structureCompare(const isl::space & ASpace,const isl::space & BSpace,bool ConsiderTupleLen)666 static int structureCompare(const isl::space &ASpace, const isl::space &BSpace,
667 bool ConsiderTupleLen) {
668 int WrappingCompare = bool(ASpace.is_wrapping()) - bool(BSpace.is_wrapping());
669 if (WrappingCompare != 0)
670 return WrappingCompare;
671
672 if (ASpace.is_wrapping() && BSpace.is_wrapping()) {
673 isl::space AMap = ASpace.unwrap();
674 isl::space BMap = BSpace.unwrap();
675
676 int FirstResult =
677 structureCompare(AMap.domain(), BMap.domain(), ConsiderTupleLen);
678 if (FirstResult != 0)
679 return FirstResult;
680
681 return structureCompare(AMap.range(), BMap.range(), ConsiderTupleLen);
682 }
683
684 std::string AName;
685 if (!ASpace.is_params() && ASpace.has_tuple_name(isl::dim::set))
686 AName = ASpace.get_tuple_name(isl::dim::set);
687
688 std::string BName;
689 if (!BSpace.is_params() && BSpace.has_tuple_name(isl::dim::set))
690 BName = BSpace.get_tuple_name(isl::dim::set);
691
692 int NameCompare = AName.compare(BName);
693 if (NameCompare != 0)
694 return NameCompare;
695
696 if (ConsiderTupleLen) {
697 int LenCompare = (int)unsignedFromIslSize(BSpace.dim(isl::dim::set)) -
698 (int)unsignedFromIslSize(ASpace.dim(isl::dim::set));
699 if (LenCompare != 0)
700 return LenCompare;
701 }
702
703 return 0;
704 }
705
706 /// Compare the sets @p A and @p B according to their nested space structure. If
707 /// the structure is the same, sort using the dimension lower bounds.
708 /// Returns an std::sort compatible bool.
orderComparer(const isl::basic_set & A,const isl::basic_set & B)709 static bool orderComparer(const isl::basic_set &A, const isl::basic_set &B) {
710 isl::space ASpace = A.get_space();
711 isl::space BSpace = B.get_space();
712
713 // Ignoring number of dimensions first ensures that structures with same tuple
714 // names, but different number of dimensions are still sorted close together.
715 int TupleNestingCompare = structureCompare(ASpace, BSpace, false);
716 if (TupleNestingCompare != 0)
717 return TupleNestingCompare < 0;
718
719 int TupleCompare = structureCompare(ASpace, BSpace, true);
720 if (TupleCompare != 0)
721 return TupleCompare < 0;
722
723 return flatCompare(A, B) < 0;
724 }
725
726 /// Print a string representation of @p USet to @p OS.
727 ///
728 /// The pieces of @p USet are printed in a sorted order. Spaces with equal or
729 /// similar nesting structure are printed together. Compared to isl's own
730 /// printing function the uses the structure itself as base of the sorting, not
731 /// a hash of it. It ensures that e.g. maps spaces with same domain structure
732 /// are printed together. Set pieces with same structure are printed in order of
733 /// their lower bounds.
734 ///
735 /// @param USet Polyhedra to print.
736 /// @param OS Target stream.
737 /// @param Simplify Whether to simplify the polyhedron before printing.
738 /// @param IsMap Whether @p USet is a wrapped map. If true, sets are
739 /// unwrapped before printing to again appear as a map.
printSortedPolyhedra(isl::union_set USet,llvm::raw_ostream & OS,bool Simplify,bool IsMap)740 static void printSortedPolyhedra(isl::union_set USet, llvm::raw_ostream &OS,
741 bool Simplify, bool IsMap) {
742 if (USet.is_null()) {
743 OS << "<null>\n";
744 return;
745 }
746
747 if (Simplify)
748 simplify(USet);
749
750 // Get all the polyhedra.
751 std::vector<isl::basic_set> BSets;
752
753 for (isl::set Set : USet.get_set_list()) {
754 for (isl::basic_set BSet : Set.get_basic_set_list()) {
755 BSets.push_back(BSet);
756 }
757 }
758
759 if (BSets.empty()) {
760 OS << "{\n}\n";
761 return;
762 }
763
764 // Sort the polyhedra.
765 llvm::sort(BSets, orderComparer);
766
767 // Print the polyhedra.
768 bool First = true;
769 for (const isl::basic_set &BSet : BSets) {
770 std::string Str;
771 if (IsMap)
772 Str = stringFromIslObj(isl::map(BSet.unwrap()));
773 else
774 Str = stringFromIslObj(isl::set(BSet));
775 size_t OpenPos = Str.find_first_of('{');
776 assert(OpenPos != std::string::npos);
777 size_t ClosePos = Str.find_last_of('}');
778 assert(ClosePos != std::string::npos);
779
780 if (First)
781 OS << llvm::StringRef(Str).substr(0, OpenPos + 1) << "\n ";
782 else
783 OS << ";\n ";
784
785 OS << llvm::StringRef(Str).substr(OpenPos + 1, ClosePos - OpenPos - 2);
786 First = false;
787 }
788 assert(!First);
789 OS << "\n}\n";
790 }
791
recursiveExpand(isl::basic_set BSet,unsigned Dim,isl::set & Expanded)792 static void recursiveExpand(isl::basic_set BSet, unsigned Dim,
793 isl::set &Expanded) {
794 unsigned Dims = unsignedFromIslSize(BSet.dim(isl::dim::set));
795 if (Dim >= Dims) {
796 Expanded = Expanded.unite(BSet);
797 return;
798 }
799
800 isl::basic_set DimOnly =
801 BSet.project_out(isl::dim::param, 0,
802 unsignedFromIslSize(BSet.dim(isl::dim::param)))
803 .project_out(isl::dim::set, Dim + 1, Dims - Dim - 1)
804 .project_out(isl::dim::set, 0, Dim);
805 if (!DimOnly.is_bounded()) {
806 recursiveExpand(BSet, Dim + 1, Expanded);
807 return;
808 }
809
810 foreachPoint(DimOnly, [&, Dim](isl::point P) {
811 isl::val Val = P.get_coordinate_val(isl::dim::set, 0);
812 isl::basic_set FixBSet = BSet.fix_val(isl::dim::set, Dim, Val);
813 recursiveExpand(FixBSet, Dim + 1, Expanded);
814 });
815 }
816
817 /// Make each point of a set explicit.
818 ///
819 /// "Expanding" makes each point a set contains explicit. That is, the result is
820 /// a set of singleton polyhedra. Unbounded dimensions are not expanded.
821 ///
822 /// Example:
823 /// { [i] : 0 <= i < 2 }
824 /// is expanded to:
825 /// { [0]; [1] }
expand(const isl::set & Set)826 static isl::set expand(const isl::set &Set) {
827 isl::set Expanded = isl::set::empty(Set.get_space());
828 for (isl::basic_set BSet : Set.get_basic_set_list())
829 recursiveExpand(BSet, 0, Expanded);
830 return Expanded;
831 }
832
833 /// Expand all points of a union set explicit.
834 ///
835 /// @see expand(const isl::set)
expand(const isl::union_set & USet)836 static isl::union_set expand(const isl::union_set &USet) {
837 isl::union_set Expanded = isl::union_set::empty(USet.ctx());
838 for (isl::set Set : USet.get_set_list()) {
839 isl::set SetExpanded = expand(Set);
840 Expanded = Expanded.unite(SetExpanded);
841 }
842 return Expanded;
843 }
844
dumpPw(const isl::set & Set)845 LLVM_DUMP_METHOD void polly::dumpPw(const isl::set &Set) {
846 printSortedPolyhedra(Set, llvm::errs(), true, false);
847 }
848
dumpPw(const isl::map & Map)849 LLVM_DUMP_METHOD void polly::dumpPw(const isl::map &Map) {
850 printSortedPolyhedra(Map.wrap(), llvm::errs(), true, true);
851 }
852
dumpPw(const isl::union_set & USet)853 LLVM_DUMP_METHOD void polly::dumpPw(const isl::union_set &USet) {
854 printSortedPolyhedra(USet, llvm::errs(), true, false);
855 }
856
dumpPw(const isl::union_map & UMap)857 LLVM_DUMP_METHOD void polly::dumpPw(const isl::union_map &UMap) {
858 printSortedPolyhedra(UMap.wrap(), llvm::errs(), true, true);
859 }
860
dumpPw(__isl_keep isl_set * Set)861 LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_set *Set) {
862 dumpPw(isl::manage_copy(Set));
863 }
864
dumpPw(__isl_keep isl_map * Map)865 LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_map *Map) {
866 dumpPw(isl::manage_copy(Map));
867 }
868
dumpPw(__isl_keep isl_union_set * USet)869 LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_union_set *USet) {
870 dumpPw(isl::manage_copy(USet));
871 }
872
dumpPw(__isl_keep isl_union_map * UMap)873 LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_union_map *UMap) {
874 dumpPw(isl::manage_copy(UMap));
875 }
876
dumpExpanded(const isl::set & Set)877 LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::set &Set) {
878 printSortedPolyhedra(expand(Set), llvm::errs(), false, false);
879 }
880
dumpExpanded(const isl::map & Map)881 LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::map &Map) {
882 printSortedPolyhedra(expand(Map.wrap()), llvm::errs(), false, true);
883 }
884
dumpExpanded(const isl::union_set & USet)885 LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::union_set &USet) {
886 printSortedPolyhedra(expand(USet), llvm::errs(), false, false);
887 }
888
dumpExpanded(const isl::union_map & UMap)889 LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::union_map &UMap) {
890 printSortedPolyhedra(expand(UMap.wrap()), llvm::errs(), false, true);
891 }
892
dumpExpanded(__isl_keep isl_set * Set)893 LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_set *Set) {
894 dumpExpanded(isl::manage_copy(Set));
895 }
896
dumpExpanded(__isl_keep isl_map * Map)897 LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_map *Map) {
898 dumpExpanded(isl::manage_copy(Map));
899 }
900
dumpExpanded(__isl_keep isl_union_set * USet)901 LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_union_set *USet) {
902 dumpExpanded(isl::manage_copy(USet));
903 }
904
dumpExpanded(__isl_keep isl_union_map * UMap)905 LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_union_map *UMap) {
906 dumpExpanded(isl::manage_copy(UMap));
907 }
908 #endif
909