[unittest] Translate isl tests to C++ bindings

For this translation we introduce two functions, valFromAPInt and APIntFromVal,
to convert between isl::val and APInt. For now these are just proxies, but in
the future they will replace the current isl_val* based conversion functions.

The isl unit test cases benefit most from the new isl::boolean (from Michael
Kruse), which can be explicitly casted to bool and which -- as part of this cast
-- emits a check that no error condition has been triggered so far. This allows
us to simplify

  EXPECT_EQ(isl_bool_true, isl_val_is_zero(IslZero));

to

  EXPECT_TRUE(IslZero.is_zero());

This simplification also becomes very clear in operator==, which changes from

  auto IsEqual = isl_set_is_equal(LHS.keep(), RHS.keep());
  EXPECT_NE(isl_bool_error, IsEqual);
  return IsEqual;

to just

  return bool(LHS.is_equal(RHS));

Some background for non-isl users. The isl C interface has an isl_bool type,
which can be either true, false, or error. Hence, whenever a function returns
a value of type isl_bool, an explicit error check should be considered. By
using isl::boolean, we can just cast the isl::boolean to 'bool' or simply use
the isl::boolean in a context where it will automatically be casted to bool
(e.g., in an if-condition). When doing so, the C++ bindings automatically add
code that verifies that the return value is not an error code. If it is, the
program will warn about this and abort. For cases where errors are expected,
isl::boolean provides checks such as boolean::is_true_or_error() or
boolean::is_true_no_error() to explicitly control program behavior in case of
error conditions.

Thanks to the new automatic memory management, we also can avoid many calls to
isl_*_free. For code that had previously been managed by IslPtr<>, many calls
to give/take/copy are eliminated.

Tags: #polly

Reviewed By: Meinersbur

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

llvm-svn: 297464
This commit is contained in:
Tobias Grosser 2017-03-10 14:58:50 +00:00
parent 51ebda8c9d
commit 3cc57fa1e7
2 changed files with 101 additions and 114 deletions

View File

@ -70,6 +70,10 @@ namespace polly {
/// @return The isl_val corresponding to @p Int.
__isl_give isl_val *isl_valFromAPInt(isl_ctx *Ctx, const llvm::APInt Int,
bool IsSigned);
inline isl::val valFromAPInt(isl_ctx *Ctx, const llvm::APInt Int,
bool IsSigned) {
return isl::manage(isl_valFromAPInt(Ctx, Int, IsSigned));
}
/// Translate isl_val to llvm::APInt.
///
@ -99,6 +103,9 @@ __isl_give isl_val *isl_valFromAPInt(isl_ctx *Ctx, const llvm::APInt Int,
///
/// @return The APInt value corresponding to @p Val.
llvm::APInt APIntFromVal(__isl_take isl_val *Val);
inline llvm::APInt APIntFromVal(isl::val V) {
return APIntFromVal(V.release());
}
/// Get c++ string from Isl objects.
//@{

View File

@ -34,43 +34,45 @@ static isl::space parseSpace(isl_ctx *Ctx, const char *Str) {
#define SPACE(Str) parseSpace(Ctx.get(), Str)
#define SET(Str) give(isl_set_read_from_str(Ctx.get(), Str))
#define MAP(Str) give(isl_map_read_from_str(Ctx.get(), Str))
#define SET(Str) isl::set(Ctx.get(), Str)
#define MAP(Str) isl::map(Ctx.get(), Str)
#define USET(Str) give(isl_union_set_read_from_str(Ctx.get(), Str))
#define UMAP(Str) give(isl_union_map_read_from_str(Ctx.get(), Str))
#define USET(Str) isl::union_set(Ctx.get(), Str)
#define UMAP(Str) isl::union_map(Ctx.get(), Str)
namespace isl {
inline namespace noexceptions {
static bool operator==(const isl::space &LHS, const isl::space &RHS) {
auto IsEqual = isl_space_is_equal(LHS.keep(), RHS.keep());
EXPECT_NE(isl_bool_error, IsEqual);
return IsEqual;
return bool(LHS.is_equal(RHS));
}
static bool operator==(const isl::basic_set &LHS, const isl::basic_set &RHS) {
return bool(LHS.is_equal(RHS));
}
static bool operator==(const isl::set &LHS, const isl::set &RHS) {
auto IsEqual = isl_set_is_equal(LHS.keep(), RHS.keep());
EXPECT_NE(isl_bool_error, IsEqual);
return IsEqual;
return bool(LHS.is_equal(RHS));
}
static bool operator==(const isl::basic_map &LHS, const isl::basic_map &RHS) {
return bool(LHS.is_equal(RHS));
}
static bool operator==(const isl::map &LHS, const isl::map &RHS) {
auto IsEqual = isl_map_is_equal(LHS.keep(), RHS.keep());
EXPECT_NE(isl_bool_error, IsEqual);
return IsEqual;
return bool(LHS.is_equal(RHS));
}
static bool operator==(const isl::union_set &LHS, const isl::union_set &RHS) {
auto IsEqual = isl_union_set_is_equal(LHS.keep(), RHS.keep());
EXPECT_NE(isl_bool_error, IsEqual);
return IsEqual;
return bool(LHS.is_equal(RHS));
}
static bool operator==(const isl::union_map &LHS, const isl::union_map &RHS) {
auto IsEqual = isl_union_map_is_equal(LHS.keep(), RHS.keep());
EXPECT_NE(isl_bool_error, IsEqual);
return IsEqual;
return bool(LHS.is_equal(RHS));
}
static bool operator==(const isl::val &LHS, const isl::val &RHS) {
return bool(LHS.eq(RHS));
}
} // namespace noexceptions
} // namespace isl
@ -82,87 +84,73 @@ TEST(Isl, APIntToIslVal) {
{
APInt APZero(1, 0, true);
auto *IslZero = isl_valFromAPInt(IslCtx, APZero, true);
EXPECT_EQ(isl_bool_true, isl_val_is_zero(IslZero));
isl_val_free(IslZero);
auto IslZero = valFromAPInt(IslCtx, APZero, true);
EXPECT_TRUE(IslZero.is_zero());
}
{
APInt APNOne(1, -1, true);
auto *IslNOne = isl_valFromAPInt(IslCtx, APNOne, true);
EXPECT_EQ(isl_bool_true, isl_val_is_negone(IslNOne));
isl_val_free(IslNOne);
auto IslNOne = valFromAPInt(IslCtx, APNOne, true);
EXPECT_TRUE(IslNOne.is_negone());
}
{
APInt APZero(1, 0, false);
auto *IslZero = isl_valFromAPInt(IslCtx, APZero, false);
EXPECT_EQ(isl_bool_true, isl_val_is_zero(IslZero));
isl_val_free(IslZero);
auto IslZero = valFromAPInt(IslCtx, APZero, false);
EXPECT_TRUE(IslZero.is_zero());
}
{
APInt APOne(1, 1, false);
auto *IslOne = isl_valFromAPInt(IslCtx, APOne, false);
EXPECT_EQ(isl_bool_true, isl_val_is_one(IslOne));
isl_val_free(IslOne);
auto IslOne = valFromAPInt(IslCtx, APOne, false);
EXPECT_TRUE(IslOne.is_one());
}
{
APInt APNTwo(2, -2, true);
auto *IslNTwo = isl_valFromAPInt(IslCtx, APNTwo, true);
auto *IslNTwoCmp = isl_val_int_from_si(IslCtx, -2);
EXPECT_EQ(isl_bool_true, isl_val_eq(IslNTwo, IslNTwoCmp));
isl_val_free(IslNTwo);
isl_val_free(IslNTwoCmp);
auto IslNTwo = valFromAPInt(IslCtx, APNTwo, true);
auto IslNTwoCmp = isl::val(IslCtx, -2);
EXPECT_EQ(IslNTwo, IslNTwoCmp);
}
{
APInt APNOne(32, -1, true);
auto *IslNOne = isl_valFromAPInt(IslCtx, APNOne, true);
EXPECT_EQ(isl_bool_true, isl_val_is_negone(IslNOne));
isl_val_free(IslNOne);
auto IslNOne = valFromAPInt(IslCtx, APNOne, true);
EXPECT_TRUE(IslNOne.is_negone());
}
{
APInt APZero(32, 0, false);
auto *IslZero = isl_valFromAPInt(IslCtx, APZero, false);
EXPECT_EQ(isl_bool_true, isl_val_is_zero(IslZero));
isl_val_free(IslZero);
auto IslZero = valFromAPInt(IslCtx, APZero, false);
EXPECT_TRUE(IslZero.is_zero());
}
{
APInt APOne(32, 1, false);
auto *IslOne = isl_valFromAPInt(IslCtx, APOne, false);
EXPECT_EQ(isl_bool_true, isl_val_is_one(IslOne));
isl_val_free(IslOne);
auto IslOne = valFromAPInt(IslCtx, APOne, false);
EXPECT_TRUE(IslOne.is_one());
}
{
APInt APTwo(32, 2, false);
auto *IslTwo = isl_valFromAPInt(IslCtx, APTwo, false);
EXPECT_EQ(0, isl_val_cmp_si(IslTwo, 2));
isl_val_free(IslTwo);
auto IslTwo = valFromAPInt(IslCtx, APTwo, false);
EXPECT_EQ(0, IslTwo.cmp_si(2));
}
{
APInt APNOne(32, (1ull << 32) - 1, false);
auto *IslNOne = isl_valFromAPInt(IslCtx, APNOne, false);
auto *IslRef = isl_val_int_from_ui(IslCtx, (1ull << 32) - 1);
EXPECT_EQ(isl_bool_true, isl_val_eq(IslNOne, IslRef));
isl_val_free(IslNOne);
isl_val_free(IslRef);
auto IslNOne = valFromAPInt(IslCtx, APNOne, false);
auto IslRef = isl::val(IslCtx, (1ull << 32) - 1);
EXPECT_EQ(IslNOne, IslRef);
}
{
APInt APLarge(130, 2, false);
APLarge = APLarge.shl(70);
auto *IslLarge = isl_valFromAPInt(IslCtx, APLarge, false);
auto *IslRef = isl_val_int_from_ui(IslCtx, 71);
IslRef = isl_val_2exp(IslRef);
EXPECT_EQ(isl_bool_true, isl_val_eq(IslLarge, IslRef));
isl_val_free(IslLarge);
isl_val_free(IslRef);
auto IslLarge = valFromAPInt(IslCtx, APLarge, false);
auto IslRef = isl::val(IslCtx, 71);
IslRef = IslRef.two_exp();
EXPECT_EQ(IslLarge, IslRef);
}
isl_ctx_free(IslCtx);
@ -172,7 +160,7 @@ TEST(Isl, IslValToAPInt) {
isl_ctx *IslCtx = isl_ctx_alloc();
{
auto *IslNOne = isl_val_int_from_si(IslCtx, -1);
auto IslNOne = isl::val(IslCtx, -1);
auto APNOne = APIntFromVal(IslNOne);
// Compare with the two's complement of -1 in a 1-bit integer.
EXPECT_EQ(1, APNOne);
@ -180,7 +168,7 @@ TEST(Isl, IslValToAPInt) {
}
{
auto *IslNTwo = isl_val_int_from_si(IslCtx, -2);
auto IslNTwo = isl::val(IslCtx, -2);
auto APNTwo = APIntFromVal(IslNTwo);
// Compare with the two's complement of -2 in a 2-bit integer.
EXPECT_EQ(2, APNTwo);
@ -188,7 +176,7 @@ TEST(Isl, IslValToAPInt) {
}
{
auto *IslNThree = isl_val_int_from_si(IslCtx, -3);
auto IslNThree = isl::val(IslCtx, -3);
auto APNThree = APIntFromVal(IslNThree);
// Compare with the two's complement of -3 in a 3-bit integer.
EXPECT_EQ(5, APNThree);
@ -196,7 +184,7 @@ TEST(Isl, IslValToAPInt) {
}
{
auto *IslNFour = isl_val_int_from_si(IslCtx, -4);
auto IslNFour = isl::val(IslCtx, -4);
auto APNFour = APIntFromVal(IslNFour);
// Compare with the two's complement of -4 in a 3-bit integer.
EXPECT_EQ(4, APNFour);
@ -204,59 +192,59 @@ TEST(Isl, IslValToAPInt) {
}
{
auto *IslZero = isl_val_int_from_ui(IslCtx, 0);
auto IslZero = isl::val(IslCtx, 0);
auto APZero = APIntFromVal(IslZero);
EXPECT_EQ(0, APZero);
EXPECT_EQ(1u, APZero.getBitWidth());
}
{
auto *IslOne = isl_val_int_from_ui(IslCtx, 1);
auto IslOne = isl::val(IslCtx, 1);
auto APOne = APIntFromVal(IslOne);
EXPECT_EQ(1, APOne);
EXPECT_EQ(2u, APOne.getBitWidth());
}
{
auto *IslTwo = isl_val_int_from_ui(IslCtx, 2);
auto IslTwo = isl::val(IslCtx, 2);
auto APTwo = APIntFromVal(IslTwo);
EXPECT_EQ(2, APTwo);
EXPECT_EQ(3u, APTwo.getBitWidth());
}
{
auto *IslThree = isl_val_int_from_ui(IslCtx, 3);
auto IslThree = isl::val(IslCtx, 3);
auto APThree = APIntFromVal(IslThree);
EXPECT_EQ(3, APThree);
EXPECT_EQ(3u, APThree.getBitWidth());
}
{
auto *IslFour = isl_val_int_from_ui(IslCtx, 4);
auto IslFour = isl::val(IslCtx, 4);
auto APFour = APIntFromVal(IslFour);
EXPECT_EQ(4, APFour);
EXPECT_EQ(4u, APFour.getBitWidth());
}
{
auto *IslNOne = isl_val_int_from_ui(IslCtx, (1ull << 32) - 1);
auto IslNOne = isl::val(IslCtx, (1ull << 32) - 1);
auto APNOne = APIntFromVal(IslNOne);
EXPECT_EQ((1ull << 32) - 1, APNOne);
EXPECT_EQ(33u, APNOne.getBitWidth());
}
{
auto *IslLargeNum = isl_val_int_from_ui(IslCtx, 60);
IslLargeNum = isl_val_2exp(IslLargeNum);
IslLargeNum = isl_val_sub_ui(IslLargeNum, 1);
auto IslLargeNum = isl::val(IslCtx, 60);
IslLargeNum = IslLargeNum.two_exp();
IslLargeNum = IslLargeNum.sub_ui(1);
auto APLargeNum = APIntFromVal(IslLargeNum);
EXPECT_EQ((1ull << 60) - 1, APLargeNum);
EXPECT_EQ(61u, APLargeNum.getBitWidth());
}
{
auto *IslExp = isl_val_int_from_ui(IslCtx, 500);
auto *IslLargePow2 = isl_val_2exp(IslExp);
auto IslExp = isl::val(IslCtx, 500);
auto IslLargePow2 = IslExp.two_exp();
auto APLargePow2 = APIntFromVal(IslLargePow2);
EXPECT_TRUE(APLargePow2.isPowerOf2());
EXPECT_EQ(502u, APLargePow2.getBitWidth());
@ -264,8 +252,8 @@ TEST(Isl, IslValToAPInt) {
}
{
auto *IslExp = isl_val_int_from_ui(IslCtx, 500);
auto *IslLargeNPow2 = isl_val_neg(isl_val_2exp(IslExp));
auto IslExp = isl::val(IslCtx, 500);
auto IslLargeNPow2 = IslExp.two_exp().neg();
auto APLargeNPow2 = APIntFromVal(IslLargeNPow2);
EXPECT_EQ(501u, APLargeNPow2.getBitWidth());
EXPECT_EQ(501u, APLargeNPow2.getMinSignedBits());
@ -273,8 +261,8 @@ TEST(Isl, IslValToAPInt) {
}
{
auto *IslExp = isl_val_int_from_ui(IslCtx, 512);
auto *IslLargePow2 = isl_val_2exp(IslExp);
auto IslExp = isl::val(IslCtx, 512);
auto IslLargePow2 = IslExp.two_exp();
auto APLargePow2 = APIntFromVal(IslLargePow2);
EXPECT_TRUE(APLargePow2.isPowerOf2());
EXPECT_EQ(514u, APLargePow2.getBitWidth());
@ -282,8 +270,8 @@ TEST(Isl, IslValToAPInt) {
}
{
auto *IslExp = isl_val_int_from_ui(IslCtx, 512);
auto *IslLargeNPow2 = isl_val_neg(isl_val_2exp(IslExp));
auto IslExp = isl::val(IslCtx, 512);
auto IslLargeNPow2 = IslExp.two_exp().neg();
auto APLargeNPow2 = APIntFromVal(IslLargeNPow2);
EXPECT_EQ(513u, APLargeNPow2.getBitWidth());
EXPECT_EQ(513u, APLargeNPow2.getMinSignedBits());
@ -297,24 +285,22 @@ TEST(Isl, Foreach) {
std::unique_ptr<isl_ctx, decltype(&isl_ctx_free)> Ctx(isl_ctx_alloc(),
&isl_ctx_free);
auto MapSpace = give(isl_space_alloc(Ctx.get(), 0, 1, 1));
auto TestBMap = give(isl_basic_map_universe(MapSpace.copy()));
TestBMap = give(isl_basic_map_fix_si(TestBMap.take(), isl_dim_in, 0, 0));
TestBMap = give(isl_basic_map_fix_si(TestBMap.take(), isl_dim_out, 0, 0));
auto TestMap = give(isl_map_from_basic_map(TestBMap.copy()));
auto TestUMap = give(isl_union_map_from_map(TestMap.copy()));
auto MapSpace = isl::space(Ctx.get(), 0, 1, 1);
auto TestBMap = isl::basic_map::universe(MapSpace);
TestBMap = TestBMap.fix_si(isl::dim::out, 0, 0);
TestBMap = TestBMap.fix_si(isl::dim::out, 0, 0);
isl::map TestMap = TestBMap;
isl::union_map TestUMap = TestMap;
auto SetSpace = give(isl_space_set_alloc(Ctx.get(), 0, 1));
auto TestBSet =
give(isl_basic_set_from_point(isl_point_zero(SetSpace.copy())));
auto TestSet = give(isl_set_from_basic_set(TestBSet.copy()));
auto TestUSet = give(isl_union_set_from_set(TestSet.copy()));
auto SetSpace = isl::space(Ctx.get(), 0, 1);
isl::basic_set TestBSet = isl::point(SetSpace);
isl::set TestSet = TestBSet;
isl::union_set TestUSet = TestSet;
{
auto NumBMaps = 0;
foreachElt(TestMap, [&](isl::basic_map BMap) {
EXPECT_EQ(isl_bool_true,
isl_basic_map_is_equal(BMap.keep(), TestBMap.keep()));
EXPECT_EQ(BMap, TestBMap);
NumBMaps++;
});
EXPECT_EQ(1, NumBMaps);
@ -323,8 +309,7 @@ TEST(Isl, Foreach) {
{
auto NumBSets = 0;
foreachElt(TestSet, [&](isl::basic_set BSet) {
EXPECT_EQ(isl_bool_true,
isl_basic_set_is_equal(BSet.keep(), TestBSet.keep()));
EXPECT_EQ(BSet, TestBSet);
NumBSets++;
});
EXPECT_EQ(1, NumBSets);
@ -333,7 +318,7 @@ TEST(Isl, Foreach) {
{
auto NumMaps = 0;
foreachElt(TestUMap, [&](isl::map Map) {
EXPECT_EQ(isl_bool_true, isl_map_is_equal(Map.keep(), TestMap.keep()));
EXPECT_EQ(Map, TestMap);
NumMaps++;
});
EXPECT_EQ(1, NumMaps);
@ -342,18 +327,17 @@ TEST(Isl, Foreach) {
{
auto NumSets = 0;
foreachElt(TestUSet, [&](isl::set Set) {
EXPECT_EQ(isl_bool_true, isl_set_is_equal(Set.keep(), TestSet.keep()));
EXPECT_EQ(Set, TestSet);
NumSets++;
});
EXPECT_EQ(1, NumSets);
}
{
auto UPwAff = give(isl_union_pw_aff_val_on_domain(TestUSet.copy(),
isl_val_zero(Ctx.get())));
auto UPwAff = isl::union_pw_aff(TestUSet, isl::val::zero(Ctx.get()));
auto NumPwAffs = 0;
foreachElt(UPwAff, [&](isl::pw_aff PwAff) {
EXPECT_EQ(isl_bool_true, isl_pw_aff_is_cst(PwAff.keep()));
EXPECT_TRUE(PwAff.is_cst());
NumPwAffs++;
});
EXPECT_EQ(1, NumPwAffs);
@ -364,8 +348,7 @@ TEST(Isl, Foreach) {
EXPECT_EQ(
isl_stat_error,
foreachEltWithBreak(TestMap, [&](isl::basic_map BMap) -> isl_stat {
EXPECT_EQ(isl_bool_true,
isl_basic_map_is_equal(BMap.keep(), TestBMap.keep()));
EXPECT_EQ(BMap, TestBMap);
NumBMaps++;
return isl_stat_error;
}));
@ -376,8 +359,7 @@ TEST(Isl, Foreach) {
auto NumMaps = 0;
EXPECT_EQ(isl_stat_error,
foreachEltWithBreak(TestUMap, [&](isl::map Map) -> isl_stat {
EXPECT_EQ(isl_bool_true,
isl_map_is_equal(Map.keep(), TestMap.keep()));
EXPECT_EQ(Map, TestMap);
NumMaps++;
return isl_stat_error;
}));
@ -385,17 +367,15 @@ TEST(Isl, Foreach) {
}
{
auto TestPwAff =
give(isl_pw_aff_val_on_domain(TestSet.copy(), isl_val_zero(Ctx.get())));
auto TestPwAff = isl::pw_aff(TestSet, isl::val::zero(Ctx.get()));
auto NumPieces = 0;
foreachPieceWithBreak(
TestPwAff, [&](isl::set Domain, isl::aff Aff) -> isl_stat {
EXPECT_EQ(isl_bool_true,
isl_set_is_equal(Domain.keep(), TestSet.keep()));
EXPECT_EQ(isl_bool_true, isl_aff_is_cst(Aff.keep()));
NumPieces++;
return isl_stat_error;
});
foreachPieceWithBreak(TestPwAff,
[&](isl::set Domain, isl::aff Aff) -> isl_stat {
EXPECT_EQ(Domain, TestSet);
EXPECT_TRUE(Aff.is_cst());
NumPieces++;
return isl_stat_error;
});
EXPECT_EQ(1, NumPieces);
}
}