[FlattenAlgo] Translate to C++ bindings

Translate the full algorithm to use the new isl C++ bindings

This is a large piece of code that has been written with the Polly IslPtr<>
memory management tool, which only performed memory management, but did not
provide a method interface. As such the code was littered with calls to
give(), copy(), keep(), and take(). The diff of this change should give a
good example how the new method interface simplifies the code by removing the
need for switching between managed types and C functions all the time
and consequently also the need to use the long C function names.

These are a couple of examples comparing the old IslPtr memory management
interface with the complete method interface.

Check properties
----------------

Before:

  if (isl_aff_is_zero(Aff.get()) ||  isl_aff_is_one(Aff.get()))
    return true;

After:

  if (Aff.is_zero() || Aff.is_one())
    return true;

Type conversion
---------------

Before:

  isl_union_pw_multi_aff *UPMA =
      give(isl_union_pw_multi_aff_from_union_map(UMap.copy());

After:

  isl::union_pw_multi_aff UPMA = UMap;

Type construction
-----------------

Before:

  auto Empty = give(isl_union_map_empty(Space.copy());

After:

  auto Empty = isl::union_map::empty(Space);

Operations
----------

Before:

  Set = give(isl_union_set_intersect(Set.copy(), Set2.copy());

After:

  Set = Set.intersect(Set2);

Tags: #polly

Reviewed By: Meinersbur

Differential Revision: https://reviews.llvm.org/D30617

llvm-svn: 297463
This commit is contained in:
Tobias Grosser 2017-03-10 14:55:58 +00:00
parent 4c24e57965
commit 51ebda8c9d
2 changed files with 89 additions and 127 deletions

View File

@ -25,12 +25,12 @@ namespace {
/// i.e. there are two constants Min and Max, such that every value x of the
/// chosen dimensions is Min <= x <= Max.
bool isDimBoundedByConstant(isl::set Set, unsigned dim) {
auto ParamDims = isl_set_dim(Set.keep(), isl_dim_param);
Set = give(isl_set_project_out(Set.take(), isl_dim_param, 0, ParamDims));
Set = give(isl_set_project_out(Set.take(), isl_dim_set, 0, dim));
auto SetDims = isl_set_dim(Set.keep(), isl_dim_set);
Set = give(isl_set_project_out(Set.take(), isl_dim_set, 1, SetDims - 1));
return isl_set_is_bounded(Set.keep());
auto ParamDims = Set.dim(isl::dim::param);
Set = Set.project_out(isl::dim::param, 0, ParamDims);
Set = Set.project_out(isl::dim::set, 0, dim);
auto SetDims = Set.dim(isl::dim::set);
Set = Set.project_out(isl::dim::set, 1, SetDims - 1);
return bool(Set.is_bounded());
}
/// Whether a dimension of a set is (lower and upper) bounded by a constant or
@ -38,17 +38,16 @@ bool isDimBoundedByConstant(isl::set Set, unsigned dim) {
/// p, such that every value x of the chosen dimensions is
/// Min_p <= x <= Max_p.
bool isDimBoundedByParameter(isl::set Set, unsigned dim) {
Set = give(isl_set_project_out(Set.take(), isl_dim_set, 0, dim));
auto SetDims = isl_set_dim(Set.keep(), isl_dim_set);
Set = give(isl_set_project_out(Set.take(), isl_dim_set, 1, SetDims - 1));
return isl_set_is_bounded(Set.keep());
Set = Set.project_out(isl::dim::set, 0, dim);
auto SetDims = Set.dim(isl::dim::set);
Set = Set.project_out(isl::dim::set, 1, SetDims - 1);
return bool(Set.is_bounded());
}
/// Whether BMap's first out-dimension is not a constant.
bool isVariableDim(const isl::basic_map &BMap) {
auto FixedVal =
give(isl_basic_map_plain_get_val_if_fixed(BMap.keep(), isl_dim_out, 0));
return !FixedVal || isl_val_is_nan(FixedVal.keep());
auto FixedVal = BMap.plain_get_val_if_fixed(isl::dim::out, 0);
return !FixedVal || FixedVal.is_nan();
}
/// Whether Map's first out dimension is no constant nor piecewise constant.
@ -76,37 +75,37 @@ isl::val getConstant(isl::pw_aff PwAff, bool Max, bool Min) {
assert(!Max || !Min);
isl::val Result;
foreachPieceWithBreak(PwAff, [=, &Result](isl::set Set, isl::aff Aff) {
if (Result && isl_val_is_nan(Result.keep()))
if (Result && Result.is_nan())
return isl_stat_ok;
// TODO: If Min/Max, we can also determine a minimum/maximum value if
// Set is constant-bounded.
if (!isl_aff_is_cst(Aff.keep())) {
Result = give(isl_val_nan(Aff.get_ctx().get()));
if (!Aff.is_cst()) {
Result = isl::val::nan(Aff.get_ctx());
return isl_stat_error;
}
auto ThisVal = give(isl_aff_get_constant_val(Aff.keep()));
auto ThisVal = Aff.get_constant();
if (!Result) {
Result = ThisVal;
return isl_stat_ok;
}
if (isl_val_eq(Result.keep(), ThisVal.keep()))
if (Result.eq(ThisVal))
return isl_stat_ok;
if (Max && isl_val_gt(ThisVal.keep(), Result.keep())) {
if (Max && ThisVal.gt(Result)) {
Result = ThisVal;
return isl_stat_ok;
}
if (Min && isl_val_lt(ThisVal.keep(), Result.keep())) {
if (Min && ThisVal.lt(Result)) {
Result = ThisVal;
return isl_stat_ok;
}
// Not compatible
Result = give(isl_val_nan(Aff.get_ctx().get()));
Result = isl::val::nan(Aff.get_ctx());
return isl_stat_error;
});
return Result;
@ -114,36 +113,30 @@ isl::val getConstant(isl::pw_aff PwAff, bool Max, bool Min) {
/// Compute @p UPwAff - @p Val.
isl::union_pw_aff subtract(isl::union_pw_aff UPwAff, isl::val Val) {
if (isl_val_is_zero(Val.keep()))
if (Val.is_zero())
return UPwAff;
auto Result =
give(isl_union_pw_aff_empty(isl_union_pw_aff_get_space(UPwAff.keep())));
auto Result = isl::union_pw_aff::empty(UPwAff.get_space());
foreachElt(UPwAff, [=, &Result](isl::pw_aff PwAff) {
auto ValAff = give(isl_pw_aff_val_on_domain(
isl_set_universe(isl_space_domain(isl_pw_aff_get_space(PwAff.keep()))),
Val.copy()));
auto Subtracted = give(isl_pw_aff_sub(PwAff.copy(), ValAff.take()));
Result = give(isl_union_pw_aff_union_add(
Result.take(), isl_union_pw_aff_from_pw_aff(Subtracted.take())));
auto ValAff =
isl::pw_aff(isl::set::universe(PwAff.get_space().domain()), Val);
auto Subtracted = PwAff.sub(ValAff);
Result = Result.union_add(isl::union_pw_aff(Subtracted));
});
return Result;
}
/// Compute @UPwAff * @p Val.
isl::union_pw_aff multiply(isl::union_pw_aff UPwAff, isl::val Val) {
if (isl_val_is_one(Val.keep()))
if (Val.is_one())
return UPwAff;
auto Result =
give(isl_union_pw_aff_empty(isl_union_pw_aff_get_space(UPwAff.keep())));
auto Result = isl::union_pw_aff::empty(UPwAff.get_space());
foreachElt(UPwAff, [=, &Result](isl::pw_aff PwAff) {
auto ValAff = give(isl_pw_aff_val_on_domain(
isl_set_universe(isl_space_domain(isl_pw_aff_get_space(PwAff.keep()))),
Val.copy()));
auto Multiplied = give(isl_pw_aff_mul(PwAff.copy(), ValAff.take()));
Result = give(isl_union_pw_aff_union_add(
Result.take(), isl_union_pw_aff_from_pw_aff(Multiplied.take())));
auto ValAff =
isl::pw_aff(isl::set::universe(PwAff.get_space().domain()), Val);
auto Multiplied = PwAff.mul(ValAff);
Result = Result.union_add(Multiplied);
});
return Result;
}
@ -158,11 +151,10 @@ isl::union_map scheduleProjectOut(const isl::union_map &UMap, unsigned first,
return UMap; /* isl_map_project_out would also reset the tuple, which should
have no effect on schedule ranges */
auto Result = give(isl_union_map_empty(isl_union_map_get_space(UMap.keep())));
auto Result = isl::union_map::empty(UMap.get_space());
foreachElt(UMap, [=, &Result](isl::map Map) {
auto Outprojected =
give(isl_map_project_out(Map.take(), isl_dim_out, first, n));
Result = give(isl_union_map_add_map(Result.take(), Outprojected.take()));
auto Outprojected = Map.project_out(isl::dim::out, first, n);
Result = Result.add_map(Outprojected);
});
return Result;
}
@ -175,28 +167,24 @@ isl::union_map scheduleProjectOut(const isl::union_map &UMap, unsigned first,
size_t scheduleScatterDims(const isl::union_map &Schedule) {
unsigned Dims = 0;
foreachElt(Schedule, [&Dims](isl::map Map) {
Dims = std::max(Dims, isl_map_dim(Map.keep(), isl_dim_out));
Dims = std::max(Dims, Map.dim(isl::dim::out));
});
return Dims;
}
/// Return the @p pos' range dimension, converted to an isl_union_pw_aff.
isl::union_pw_aff scheduleExtractDimAff(isl::union_map UMap, unsigned pos) {
auto SingleUMap =
give(isl_union_map_empty(isl_union_map_get_space(UMap.keep())));
auto SingleUMap = isl::union_map::empty(UMap.get_space());
foreachElt(UMap, [=, &SingleUMap](isl::map Map) {
auto MapDims = isl_map_dim(Map.keep(), isl_dim_out);
auto SingleMap = give(isl_map_project_out(Map.take(), isl_dim_out, 0, pos));
SingleMap = give(isl_map_project_out(SingleMap.take(), isl_dim_out, 1,
MapDims - pos - 1));
SingleUMap =
give(isl_union_map_add_map(SingleUMap.take(), SingleMap.take()));
auto MapDims = Map.dim(isl::dim::out);
auto SingleMap = Map.project_out(isl::dim::out, 0, pos);
SingleMap = SingleMap.project_out(isl::dim::out, 1, MapDims - pos - 1);
SingleUMap = SingleUMap.add_map(SingleMap);
});
auto UAff = give(isl_union_pw_multi_aff_from_union_map(SingleUMap.take()));
auto FirstMAff =
give(isl_multi_union_pw_aff_from_union_pw_multi_aff(UAff.take()));
return give(isl_multi_union_pw_aff_get_union_pw_aff(FirstMAff.keep(), 0));
auto UAff = isl::union_pw_multi_aff(SingleUMap);
auto FirstMAff = isl::multi_union_pw_aff(UAff);
return FirstMAff.get_union_pw_aff(0);
}
/// Flatten a sequence-like first dimension.
@ -220,12 +208,10 @@ isl::union_pw_aff scheduleExtractDimAff(isl::union_map UMap, unsigned pos) {
/// { Stmt_X[] -> [X - l_X, ...]; Stmt_B -> [l_X - u_X + 1 + Y - l_Y, ...] }
isl::union_map tryFlattenSequence(isl::union_map Schedule) {
auto IslCtx = Schedule.get_ctx();
auto ScatterSet =
give(isl_set_from_union_set(isl_union_map_range(Schedule.copy())));
auto ScatterSet = isl::set(Schedule.range());
auto ParamSpace =
give(isl_space_params(isl_union_map_get_space(Schedule.keep())));
auto Dims = isl_set_dim(ScatterSet.keep(), isl_dim_set);
auto ParamSpace = Schedule.get_space().params();
auto Dims = ScatterSet.dim(isl::dim::set);
assert(Dims >= 2);
// Would cause an infinite loop.
@ -234,36 +220,29 @@ isl::union_map tryFlattenSequence(isl::union_map Schedule) {
return nullptr;
}
auto AllDomains = give(isl_union_map_domain(Schedule.copy()));
auto AllDomainsToNull =
give(isl_union_pw_multi_aff_from_domain(AllDomains.take()));
auto AllDomains = Schedule.domain();
auto AllDomainsToNull = isl::union_pw_multi_aff(AllDomains);
auto NewSchedule = give(isl_union_map_empty(ParamSpace.copy()));
auto Counter = give(isl_pw_aff_zero_on_domain(isl_local_space_from_space(
isl_space_set_from_params(ParamSpace.copy()))));
auto NewSchedule = isl::union_map::empty(ParamSpace);
auto Counter = isl::pw_aff(isl::local_space(ParamSpace.set_from_params()));
while (!isl_set_is_empty(ScatterSet.keep())) {
while (!ScatterSet.is_empty()) {
DEBUG(dbgs() << "Next counter:\n " << Counter << "\n");
DEBUG(dbgs() << "Remaining scatter set:\n " << ScatterSet << "\n");
auto ThisSet =
give(isl_set_project_out(ScatterSet.copy(), isl_dim_set, 1, Dims - 1));
auto ThisFirst = give(isl_set_lexmin(ThisSet.take()));
auto ScatterFirst =
give(isl_set_add_dims(ThisFirst.take(), isl_dim_set, Dims - 1));
auto ThisSet = ScatterSet.project_out(isl::dim::set, 1, Dims - 1);
auto ThisFirst = ThisSet.lexmin();
auto ScatterFirst = ThisFirst.add_dims(isl::dim::set, Dims - 1);
auto SubSchedule = give(isl_union_map_intersect_range(
Schedule.copy(), isl_union_set_from_set(ScatterFirst.copy())));
SubSchedule = scheduleProjectOut(std::move(SubSchedule), 0, 1);
SubSchedule = flattenSchedule(std::move(SubSchedule));
auto SubSchedule = Schedule.intersect_range(ScatterFirst);
SubSchedule = scheduleProjectOut(SubSchedule, 0, 1);
SubSchedule = flattenSchedule(SubSchedule);
auto SubDims = scheduleScatterDims(SubSchedule);
auto FirstSubSchedule = scheduleProjectOut(SubSchedule, 1, SubDims - 1);
auto FirstScheduleAff = scheduleExtractDimAff(FirstSubSchedule, 0);
auto RemainingSubSchedule =
scheduleProjectOut(std::move(SubSchedule), 0, 1);
auto RemainingSubSchedule = scheduleProjectOut(SubSchedule, 0, 1);
auto FirstSubScatter = give(
isl_set_from_union_set(isl_union_map_range(FirstSubSchedule.take())));
auto FirstSubScatter = isl::set(FirstSubSchedule.range());
DEBUG(dbgs() << "Next step in sequence is:\n " << FirstSubScatter << "\n");
if (!isDimBoundedByParameter(FirstSubScatter, 0)) {
@ -271,39 +250,31 @@ isl::union_map tryFlattenSequence(isl::union_map Schedule) {
return nullptr;
}
auto FirstSubScatterMap = give(isl_map_from_range(FirstSubScatter.take()));
auto FirstSubScatterMap = isl::map::from_range(FirstSubScatter);
// isl_set_dim_max returns a strange isl_pw_aff with domain tuple_id of
// 'none'. It doesn't match with any space including a 0-dimensional
// anonymous tuple.
// Interesting, one can create such a set using
// isl_set_universe(ParamSpace). Bug?
auto PartMin = give(isl_map_dim_min(FirstSubScatterMap.copy(), 0));
auto PartMax = give(isl_map_dim_max(FirstSubScatterMap.take(), 0));
auto One = give(isl_pw_aff_val_on_domain(
isl_set_universe(isl_space_set_from_params(ParamSpace.copy())),
isl_val_one(IslCtx.get())));
auto PartLen = give(isl_pw_aff_add(
isl_pw_aff_add(PartMax.take(), isl_pw_aff_neg(PartMin.copy())),
One.take()));
auto PartMin = FirstSubScatterMap.dim_min(0);
auto PartMax = FirstSubScatterMap.dim_max(0);
auto One = isl::pw_aff(isl::set::universe(ParamSpace.set_from_params()),
isl::val::one(IslCtx));
auto PartLen = PartMax.add(PartMin.neg()).add(One);
auto AllPartMin = give(isl_union_pw_aff_pullback_union_pw_multi_aff(
isl_union_pw_aff_from_pw_aff(PartMin.take()), AllDomainsToNull.copy()));
auto FirstScheduleAffNormalized =
give(isl_union_pw_aff_sub(FirstScheduleAff.take(), AllPartMin.take()));
auto AllCounter = give(isl_union_pw_aff_pullback_union_pw_multi_aff(
isl_union_pw_aff_from_pw_aff(Counter.copy()), AllDomainsToNull.copy()));
auto FirstScheduleAffWithOffset = give(isl_union_pw_aff_add(
FirstScheduleAffNormalized.take(), AllCounter.take()));
auto AllPartMin = isl::union_pw_aff(PartMin).pullback(AllDomainsToNull);
auto FirstScheduleAffNormalized = FirstScheduleAff.sub(AllPartMin);
auto AllCounter = isl::union_pw_aff(Counter).pullback(AllDomainsToNull);
auto FirstScheduleAffWithOffset =
FirstScheduleAffNormalized.add(AllCounter);
auto ScheduleWithOffset = give(isl_union_map_flat_range_product(
isl_union_map_from_union_pw_aff(FirstScheduleAffWithOffset.take()),
RemainingSubSchedule.take()));
NewSchedule = give(
isl_union_map_union(NewSchedule.take(), ScheduleWithOffset.take()));
auto ScheduleWithOffset = isl::union_map(FirstScheduleAffWithOffset)
.flat_range_product(RemainingSubSchedule);
NewSchedule = NewSchedule.unite(ScheduleWithOffset);
ScatterSet = give(isl_set_subtract(ScatterSet.take(), ScatterFirst.take()));
Counter = give(isl_pw_aff_add(Counter.take(), PartLen.take()));
ScatterSet = ScatterSet.subtract(ScatterFirst);
Counter = Counter.add(PartLen);
}
DEBUG(dbgs() << "Sequence-flatten result is:\n " << NewSchedule << "\n");
@ -327,28 +298,24 @@ isl::union_map tryFlattenLoop(isl::union_map Schedule) {
auto SubSchedule = flattenSchedule(Remaining);
auto SubDims = scheduleScatterDims(SubSchedule);
auto SubExtent =
give(isl_set_from_union_set(isl_union_map_range(SubSchedule.copy())));
auto SubExtentDims = isl_set_dim(SubExtent.keep(), isl_dim_param);
SubExtent = give(
isl_set_project_out(SubExtent.take(), isl_dim_param, 0, SubExtentDims));
SubExtent =
give(isl_set_project_out(SubExtent.take(), isl_dim_set, 1, SubDims - 1));
auto SubExtent = isl::set(SubSchedule.range());
auto SubExtentDims = SubExtent.dim(isl::dim::param);
SubExtent = SubExtent.project_out(isl::dim::param, 0, SubExtentDims);
SubExtent = SubExtent.project_out(isl::dim::set, 1, SubDims - 1);
if (!isDimBoundedByConstant(SubExtent, 0)) {
DEBUG(dbgs() << "Abort; dimension not bounded by constant\n");
return nullptr;
}
auto Min = give(isl_set_dim_min(SubExtent.copy(), 0));
auto Min = SubExtent.dim_min(0);
DEBUG(dbgs() << "Min bound:\n " << Min << "\n");
auto MinVal = getConstant(Min, false, true);
auto Max = give(isl_set_dim_max(SubExtent.take(), 0));
auto Max = SubExtent.dim_max(0);
DEBUG(dbgs() << "Max bound:\n " << Max << "\n");
auto MaxVal = getConstant(Max, true, false);
if (!MinVal || !MaxVal || isl_val_is_nan(MinVal.keep()) ||
isl_val_is_nan(MaxVal.keep())) {
if (!MinVal || !MaxVal || MinVal.is_nan() || MaxVal.is_nan()) {
DEBUG(dbgs() << "Abort; dimension bounds could not be determined\n");
return nullptr;
}
@ -356,20 +323,17 @@ isl::union_map tryFlattenLoop(isl::union_map Schedule) {
auto FirstSubScheduleAff = scheduleExtractDimAff(SubSchedule, 0);
auto RemainingSubSchedule = scheduleProjectOut(std::move(SubSchedule), 0, 1);
auto LenVal =
give(isl_val_add_ui(isl_val_sub(MaxVal.take(), MinVal.copy()), 1));
auto LenVal = MaxVal.sub(MinVal).add_ui(1);
auto FirstSubScheduleNormalized = subtract(FirstSubScheduleAff, MinVal);
// TODO: Normalize FirstAff to zero (convert to isl_map, determine minimum,
// subtract it)
auto FirstAff = scheduleExtractDimAff(Schedule, 0);
auto Offset = multiply(FirstAff, LenVal);
auto Index = give(
isl_union_pw_aff_add(FirstSubScheduleNormalized.take(), Offset.take()));
auto IndexMap = give(isl_union_map_from_union_pw_aff(Index.take()));
auto Index = FirstSubScheduleNormalized.add(Offset);
auto IndexMap = isl::union_map(Index);
auto Result = give(isl_union_map_flat_range_product(
IndexMap.take(), RemainingSubSchedule.take()));
auto Result = IndexMap.flat_range_product(RemainingSubSchedule);
DEBUG(dbgs() << "Loop-flatten result is:\n " << Result << "\n");
return Result;
}

View File

@ -61,8 +61,7 @@ public:
DEBUG(printSchedule(dbgs(), OldSchedule, 2));
auto Domains = give(S.getDomains());
auto RestrictedOldSchedule = give(
isl_union_map_intersect_domain(OldSchedule.copy(), Domains.copy()));
auto RestrictedOldSchedule = OldSchedule.intersect_domain(Domains);
DEBUG(dbgs() << "Old schedule with domains:\n");
DEBUG(printSchedule(dbgs(), RestrictedOldSchedule, 2));
@ -71,8 +70,7 @@ public:
DEBUG(dbgs() << "Flattened new schedule:\n");
DEBUG(printSchedule(dbgs(), NewSchedule, 2));
NewSchedule =
give(isl_union_map_gist_domain(NewSchedule.take(), Domains.take()));
NewSchedule = NewSchedule.gist_domain(Domains);
DEBUG(dbgs() << "Gisted, flattened new schedule:\n");
DEBUG(printSchedule(dbgs(), NewSchedule, 2));