Coverage Report

Created: 2017-06-28 17:40

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/Transform/FlattenAlgo.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------ FlattenAlgo.cpp ------------------------------------*- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// Main algorithm of the FlattenSchedulePass. This is a separate file to avoid
11
// the unittest for this requiring linking against LLVM.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "polly/FlattenAlgo.h"
16
#include "polly/Support/ISLOStream.h"
17
#include "llvm/Support/Debug.h"
18
#define DEBUG_TYPE "polly-flatten-algo"
19
20
using namespace polly;
21
using namespace llvm;
22
23
namespace {
24
25
/// Whether a dimension of a set is bounded (lower and upper) by a constant,
26
/// i.e. there are two constants Min and Max, such that every value x of the
27
/// chosen dimensions is Min <= x <= Max.
28
85
bool isDimBoundedByConstant(isl::set Set, unsigned dim) {
29
85
  auto ParamDims = Set.dim(isl::dim::param);
30
85
  Set = Set.project_out(isl::dim::param, 0, ParamDims);
31
85
  Set = Set.project_out(isl::dim::set, 0, dim);
32
85
  auto SetDims = Set.dim(isl::dim::set);
33
85
  Set = Set.project_out(isl::dim::set, 1, SetDims - 1);
34
85
  return bool(Set.is_bounded());
35
85
}
36
37
/// Whether a dimension of a set is (lower and upper) bounded by a constant or
38
/// parameters, i.e. there are two expressions Min_p and Max_p of the parameters
39
/// p, such that every value x of the chosen dimensions is
40
/// Min_p <= x <= Max_p.
41
86
bool isDimBoundedByParameter(isl::set Set, unsigned dim) {
42
86
  Set = Set.project_out(isl::dim::set, 0, dim);
43
86
  auto SetDims = Set.dim(isl::dim::set);
44
86
  Set = Set.project_out(isl::dim::set, 1, SetDims - 1);
45
86
  return bool(Set.is_bounded());
46
86
}
47
48
/// Whether BMap's first out-dimension is not a constant.
49
171
bool isVariableDim(const isl::basic_map &BMap) {
50
171
  auto FixedVal = BMap.plain_get_val_if_fixed(isl::dim::out, 0);
51
171
  return !FixedVal || FixedVal.is_nan();
52
171
}
53
54
/// Whether Map's first out dimension is no constant nor piecewise constant.
55
171
bool isVariableDim(const isl::map &Map) {
56
171
  return Map.foreach_basic_map([](isl::basic_map BMap) -> isl::stat {
57
171
    if (isVariableDim(BMap))
58
121
      return isl::stat::error;
59
50
    return isl::stat::ok;
60
171
  }) == isl::stat::ok;
61
171
}
62
63
/// Whether UMap's first out dimension is no (piecewise) constant.
64
84
bool isVariableDim(const isl::union_map &UMap) {
65
171
  return UMap.foreach_map([](isl::map Map) -> isl::stat {
66
171
    if (isVariableDim(Map))
67
50
      return isl::stat::error;
68
121
    return isl::stat::ok;
69
171
  }) == isl::stat::ok;
70
84
}
71
72
/// If @p PwAff maps to a constant, return said constant. If @p Max/@p Min, it
73
/// can also be a piecewise constant and it would return the minimum/maximum
74
/// value. Otherwise, return NaN.
75
66
isl::val getConstant(isl::pw_aff PwAff, bool Max, bool Min) {
76
66
  assert(!Max || !Min);
77
66
  isl::val Result;
78
66
  PwAff.foreach_piece([=, &Result](isl::set Set, isl::aff Aff) -> isl::stat {
79
66
    if (
Result && 66
Result.is_nan()0
)
80
0
      return isl::stat::ok;
81
66
82
66
    // TODO: If Min/Max, we can also determine a minimum/maximum value if
83
66
    // Set is constant-bounded.
84
66
    
if (66
!Aff.is_cst()66
)
{0
85
0
      Result = isl::val::nan(Aff.get_ctx());
86
0
      return isl::stat::error;
87
0
    }
88
66
89
66
    auto ThisVal = Aff.get_constant_val();
90
66
    if (
!Result66
)
{66
91
66
      Result = ThisVal;
92
66
      return isl::stat::ok;
93
66
    }
94
66
95
0
    
if (0
Result.eq(ThisVal)0
)
96
0
      return isl::stat::ok;
97
0
98
0
    
if (0
Max && 0
ThisVal.gt(Result)0
)
{0
99
0
      Result = ThisVal;
100
0
      return isl::stat::ok;
101
0
    }
102
0
103
0
    
if (0
Min && 0
ThisVal.lt(Result)0
)
{0
104
0
      Result = ThisVal;
105
0
      return isl::stat::ok;
106
0
    }
107
0
108
0
    // Not compatible
109
0
    Result = isl::val::nan(Aff.get_ctx());
110
0
    return isl::stat::error;
111
0
  });
112
66
  return Result;
113
66
}
114
115
/// Compute @p UPwAff - @p Val.
116
33
isl::union_pw_aff subtract(isl::union_pw_aff UPwAff, isl::val Val) {
117
33
  if (Val.is_zero())
118
33
    return UPwAff;
119
33
120
0
  auto Result = isl::union_pw_aff::empty(UPwAff.get_space());
121
0
  UPwAff.foreach_pw_aff([=, &Result](isl::pw_aff PwAff) -> isl::stat {
122
0
    auto ValAff =
123
0
        isl::pw_aff(isl::set::universe(PwAff.get_space().domain()), Val);
124
0
    auto Subtracted = PwAff.sub(ValAff);
125
0
    Result = Result.union_add(isl::union_pw_aff(Subtracted));
126
0
    return isl::stat::ok;
127
0
  });
128
0
  return Result;
129
33
}
130
131
/// Compute @UPwAff * @p Val.
132
33
isl::union_pw_aff multiply(isl::union_pw_aff UPwAff, isl::val Val) {
133
33
  if (Val.is_one())
134
1
    return UPwAff;
135
33
136
32
  auto Result = isl::union_pw_aff::empty(UPwAff.get_space());
137
115
  UPwAff.foreach_pw_aff([=, &Result](isl::pw_aff PwAff) -> isl::stat {
138
115
    auto ValAff =
139
115
        isl::pw_aff(isl::set::universe(PwAff.get_space().domain()), Val);
140
115
    auto Multiplied = PwAff.mul(ValAff);
141
115
    Result = Result.union_add(Multiplied);
142
115
    return isl::stat::ok;
143
115
  });
144
32
  return Result;
145
33
}
146
147
/// Remove @p n dimensions from @p UMap's range, starting at @p first.
148
///
149
/// It is assumed that all maps in the maps have at least the necessary number
150
/// of out dimensions.
151
isl::union_map scheduleProjectOut(const isl::union_map &UMap, unsigned first,
152
325
                                  unsigned n) {
153
325
  if (n == 0)
154
86
    return UMap; /* isl_map_project_out would also reset the tuple, which should
155
325
                    have no effect on schedule ranges */
156
325
157
239
  auto Result = isl::union_map::empty(UMap.get_space());
158
485
  UMap.foreach_map([=, &Result](isl::map Map) -> isl::stat {
159
485
    auto Outprojected = Map.project_out(isl::dim::out, first, n);
160
485
    Result = Result.add_map(Outprojected);
161
485
    return isl::stat::ok;
162
485
  });
163
239
  return Result;
164
325
}
165
166
/// Return the number of dimensions in the input map's range.
167
///
168
/// Because this function takes an isl_union_map, the out dimensions could be
169
/// different. We return the maximum number in this case. However, a different
170
/// number of dimensions is not supported by the other code in this file.
171
262
size_t scheduleScatterDims(const isl::union_map &Schedule) {
172
262
  unsigned Dims = 0;
173
570
  Schedule.foreach_map([&Dims](isl::map Map) -> isl::stat {
174
570
    Dims = std::max(Dims, Map.dim(isl::dim::out));
175
570
    return isl::stat::ok;
176
570
  });
177
262
  return Dims;
178
262
}
179
180
/// Return the @p pos' range dimension, converted to an isl_union_pw_aff.
181
152
isl::union_pw_aff scheduleExtractDimAff(isl::union_map UMap, unsigned pos) {
182
152
  auto SingleUMap = isl::union_map::empty(UMap.get_space());
183
356
  UMap.foreach_map([=, &SingleUMap](isl::map Map) -> isl::stat {
184
356
    auto MapDims = Map.dim(isl::dim::out);
185
356
    auto SingleMap = Map.project_out(isl::dim::out, 0, pos);
186
356
    SingleMap = SingleMap.project_out(isl::dim::out, 1, MapDims - pos - 1);
187
356
    SingleUMap = SingleUMap.add_map(SingleMap);
188
356
    return isl::stat::ok;
189
356
  });
190
152
191
152
  auto UAff = isl::union_pw_multi_aff(SingleUMap);
192
152
  auto FirstMAff = isl::multi_union_pw_aff(UAff);
193
152
  return FirstMAff.get_union_pw_aff(0);
194
152
}
195
196
/// Flatten a sequence-like first dimension.
197
///
198
/// A sequence-like scatter dimension is constant, or at least only small
199
/// variation, typically the result of ordering a sequence of different
200
/// statements. An example would be:
201
///   { Stmt_A[] -> [0, X, ...]; Stmt_B[] -> [1, Y, ...] }
202
/// to schedule all instances of Stmt_A before any instance of Stmt_B.
203
///
204
/// To flatten, first begin with an offset of zero. Then determine the lowest
205
/// possible value of the dimension, call it "i" [In the example we start at 0].
206
/// Considering only schedules with that value, consider only instances with
207
/// that value and determine the extent of the next dimension. Let l_X(i) and
208
/// u_X(i) its minimum (lower bound) and maximum (upper bound) value. Add them
209
/// as "Offset + X - l_X(i)" to the new schedule, then add "u_X(i) - l_X(i) + 1"
210
/// to Offset and remove all i-instances from the old schedule. Repeat with the
211
/// remaining lowest value i' until there are no instances in the old schedule
212
/// left.
213
/// The example schedule would be transformed to:
214
///   { Stmt_X[] -> [X - l_X, ...]; Stmt_B -> [l_X - u_X + 1 + Y - l_Y, ...] }
215
51
isl::union_map tryFlattenSequence(isl::union_map Schedule) {
216
51
  auto IslCtx = Schedule.get_ctx();
217
51
  auto ScatterSet = isl::set(Schedule.range());
218
51
219
51
  auto ParamSpace = Schedule.get_space().params();
220
51
  auto Dims = ScatterSet.dim(isl::dim::set);
221
51
  assert(Dims >= 2);
222
51
223
51
  // Would cause an infinite loop.
224
51
  if (
!isDimBoundedByConstant(ScatterSet, 0)51
)
{0
225
0
    DEBUG(dbgs() << "Abort; dimension is not of fixed size\n");
226
0
    return nullptr;
227
0
  }
228
51
229
51
  auto AllDomains = Schedule.domain();
230
51
  auto AllDomainsToNull = isl::union_pw_multi_aff(AllDomains);
231
51
232
51
  auto NewSchedule = isl::union_map::empty(ParamSpace);
233
51
  auto Counter = isl::pw_aff(isl::local_space(ParamSpace.set_from_params()));
234
51
235
137
  while (
!ScatterSet.is_empty()137
)
{86
236
86
    DEBUG(dbgs() << "Next counter:\n  " << Counter << "\n");
237
86
    DEBUG(dbgs() << "Remaining scatter set:\n  " << ScatterSet << "\n");
238
86
    auto ThisSet = ScatterSet.project_out(isl::dim::set, 1, Dims - 1);
239
86
    auto ThisFirst = ThisSet.lexmin();
240
86
    auto ScatterFirst = ThisFirst.add_dims(isl::dim::set, Dims - 1);
241
86
242
86
    auto SubSchedule = Schedule.intersect_range(ScatterFirst);
243
86
    SubSchedule = scheduleProjectOut(SubSchedule, 0, 1);
244
86
    SubSchedule = flattenSchedule(SubSchedule);
245
86
246
86
    auto SubDims = scheduleScatterDims(SubSchedule);
247
86
    auto FirstSubSchedule = scheduleProjectOut(SubSchedule, 1, SubDims - 1);
248
86
    auto FirstScheduleAff = scheduleExtractDimAff(FirstSubSchedule, 0);
249
86
    auto RemainingSubSchedule = scheduleProjectOut(SubSchedule, 0, 1);
250
86
251
86
    auto FirstSubScatter = isl::set(FirstSubSchedule.range());
252
86
    DEBUG(dbgs() << "Next step in sequence is:\n  " << FirstSubScatter << "\n");
253
86
254
86
    if (
!isDimBoundedByParameter(FirstSubScatter, 0)86
)
{0
255
0
      DEBUG(dbgs() << "Abort; sequence step is not bounded\n");
256
0
      return nullptr;
257
0
    }
258
86
259
86
    auto FirstSubScatterMap = isl::map::from_range(FirstSubScatter);
260
86
261
86
    // isl_set_dim_max returns a strange isl_pw_aff with domain tuple_id of
262
86
    // 'none'. It doesn't match with any space including a 0-dimensional
263
86
    // anonymous tuple.
264
86
    // Interesting, one can create such a set using
265
86
    // isl_set_universe(ParamSpace). Bug?
266
86
    auto PartMin = FirstSubScatterMap.dim_min(0);
267
86
    auto PartMax = FirstSubScatterMap.dim_max(0);
268
86
    auto One = isl::pw_aff(isl::set::universe(ParamSpace.set_from_params()),
269
86
                           isl::val::one(IslCtx));
270
86
    auto PartLen = PartMax.add(PartMin.neg()).add(One);
271
86
272
86
    auto AllPartMin = isl::union_pw_aff(PartMin).pullback(AllDomainsToNull);
273
86
    auto FirstScheduleAffNormalized = FirstScheduleAff.sub(AllPartMin);
274
86
    auto AllCounter = isl::union_pw_aff(Counter).pullback(AllDomainsToNull);
275
86
    auto FirstScheduleAffWithOffset =
276
86
        FirstScheduleAffNormalized.add(AllCounter);
277
86
278
86
    auto ScheduleWithOffset = isl::union_map(FirstScheduleAffWithOffset)
279
86
                                  .flat_range_product(RemainingSubSchedule);
280
86
    NewSchedule = NewSchedule.unite(ScheduleWithOffset);
281
86
282
86
    ScatterSet = ScatterSet.subtract(ScatterFirst);
283
86
    Counter = Counter.add(PartLen);
284
86
  }
285
51
286
51
  
DEBUG51
(dbgs() << "Sequence-flatten result is:\n " << NewSchedule << "\n");51
287
51
  return NewSchedule;
288
51
}
289
290
/// Flatten a loop-like first dimension.
291
///
292
/// A loop-like dimension is one that depends on a variable (usually a loop's
293
/// induction variable). Let the input schedule look like this:
294
///   { Stmt[i] -> [i, X, ...] }
295
///
296
/// To flatten, we determine the largest extent of X which may not depend on the
297
/// actual value of i. Let l_X() the smallest possible value of X and u_X() its
298
/// largest value. Then, construct a new schedule
299
///   { Stmt[i] -> [i * (u_X() - l_X() + 1), ...] }
300
34
isl::union_map tryFlattenLoop(isl::union_map Schedule) {
301
34
  assert(scheduleScatterDims(Schedule) >= 2);
302
34
303
34
  auto Remaining = scheduleProjectOut(Schedule, 0, 1);
304
34
  auto SubSchedule = flattenSchedule(Remaining);
305
34
  auto SubDims = scheduleScatterDims(SubSchedule);
306
34
307
34
  auto SubExtent = isl::set(SubSchedule.range());
308
34
  auto SubExtentDims = SubExtent.dim(isl::dim::param);
309
34
  SubExtent = SubExtent.project_out(isl::dim::param, 0, SubExtentDims);
310
34
  SubExtent = SubExtent.project_out(isl::dim::set, 1, SubDims - 1);
311
34
312
34
  if (
!isDimBoundedByConstant(SubExtent, 0)34
)
{1
313
1
    DEBUG(dbgs() << "Abort; dimension not bounded by constant\n");
314
1
    return nullptr;
315
1
  }
316
34
317
33
  auto Min = SubExtent.dim_min(0);
318
33
  DEBUG(dbgs() << "Min bound:\n  " << Min << "\n");
319
33
  auto MinVal = getConstant(Min, false, true);
320
33
  auto Max = SubExtent.dim_max(0);
321
33
  DEBUG(dbgs() << "Max bound:\n  " << Max << "\n");
322
33
  auto MaxVal = getConstant(Max, true, false);
323
33
324
33
  if (
!MinVal || 33
!MaxVal33
||
MinVal.is_nan()33
||
MaxVal.is_nan()33
)
{0
325
0
    DEBUG(dbgs() << "Abort; dimension bounds could not be determined\n");
326
0
    return nullptr;
327
0
  }
328
33
329
33
  auto FirstSubScheduleAff = scheduleExtractDimAff(SubSchedule, 0);
330
33
  auto RemainingSubSchedule = scheduleProjectOut(std::move(SubSchedule), 0, 1);
331
33
332
33
  auto LenVal = MaxVal.sub(MinVal).add_ui(1);
333
33
  auto FirstSubScheduleNormalized = subtract(FirstSubScheduleAff, MinVal);
334
33
335
33
  // TODO: Normalize FirstAff to zero (convert to isl_map, determine minimum,
336
33
  // subtract it)
337
33
  auto FirstAff = scheduleExtractDimAff(Schedule, 0);
338
33
  auto Offset = multiply(FirstAff, LenVal);
339
33
  auto Index = FirstSubScheduleNormalized.add(Offset);
340
33
  auto IndexMap = isl::union_map(Index);
341
33
342
33
  auto Result = IndexMap.flat_range_product(RemainingSubSchedule);
343
33
  DEBUG(dbgs() << "Loop-flatten result is:\n  " << Result << "\n");
344
33
  return Result;
345
33
}
346
} // anonymous namespace
347
348
142
isl::union_map polly::flattenSchedule(isl::union_map Schedule) {
349
142
  auto Dims = scheduleScatterDims(Schedule);
350
142
  DEBUG(dbgs() << "Recursive schedule to process:\n  " << Schedule << "\n");
351
142
352
142
  // Base case; no dimensions left
353
142
  if (
Dims == 0142
)
{0
354
0
    // TODO: Add one dimension?
355
0
    return Schedule;
356
0
  }
357
142
358
142
  // Base case; already one-dimensional
359
142
  
if (142
Dims == 1142
)
360
58
    return Schedule;
361
142
362
142
  // Fixed dimension; no need to preserve variabledness.
363
84
  
if (84
!isVariableDim(Schedule)84
)
{50
364
50
    DEBUG(dbgs() << "Fixed dimension; try sequence flattening\n");
365
50
    auto NewScheduleSequence = tryFlattenSequence(Schedule);
366
50
    if (NewScheduleSequence)
367
50
      return NewScheduleSequence;
368
50
  }
369
84
370
84
  // Constant stride
371
34
  
DEBUG34
(dbgs() << "Try loop flattening\n");34
372
34
  auto NewScheduleLoop = tryFlattenLoop(Schedule);
373
34
  if (NewScheduleLoop)
374
33
    return NewScheduleLoop;
375
34
376
34
  // Try again without loop condition (may blow up the number of pieces!!)
377
1
  
DEBUG1
(dbgs() << "Try sequence flattening again\n");1
378
1
  auto NewScheduleSequence = tryFlattenSequence(Schedule);
379
1
  if (NewScheduleSequence)
380
1
    return NewScheduleSequence;
381
1
382
1
  // Cannot flatten
383
0
  return Schedule;
384
1
}