1 //===- FlattenTest.cpp ----------------------------------------------------===//
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 #include "polly/FlattenAlgo.h"
10 #include "polly/Support/GICHelper.h"
11 #include "gtest/gtest.h"
12 #include "isl/union_map.h"
13
14 using namespace llvm;
15 using namespace polly;
16
17 namespace {
18
19 /// Flatten a schedule and compare to the expected result.
20 ///
21 /// @param ScheduleStr The schedule to flatten as string.
22 /// @param ExpectedStr The expected result as string.
23 ///
24 /// @result Whether the flattened schedule is the same as the expected schedule.
checkFlatten(const char * ScheduleStr,const char * ExpectedStr)25 bool checkFlatten(const char *ScheduleStr, const char *ExpectedStr) {
26 auto *Ctx = isl_ctx_alloc();
27 bool Success;
28
29 {
30 auto Schedule = isl::union_map(Ctx, ScheduleStr);
31 auto Expected = isl::union_map(Ctx, ExpectedStr);
32
33 auto Result = flattenSchedule(std::move(Schedule));
34 Success = Result.is_equal(Expected);
35 }
36
37 isl_ctx_free(Ctx);
38 return Success;
39 }
40
TEST(Flatten,FlattenTrivial)41 TEST(Flatten, FlattenTrivial) {
42 EXPECT_TRUE(checkFlatten("{ A[] -> [0] }", "{ A[] -> [0] }"));
43 EXPECT_TRUE(checkFlatten("{ A[i] -> [i, 0] : 0 <= i < 10 }",
44 "{ A[i] -> [i] : 0 <= i < 10 }"));
45 EXPECT_TRUE(checkFlatten("{ A[i] -> [0, i] : 0 <= i < 10 }",
46 "{ A[i] -> [i] : 0 <= i < 10 }"));
47 }
48
TEST(Flatten,FlattenSequence)49 TEST(Flatten, FlattenSequence) {
50 EXPECT_TRUE(checkFlatten(
51 "[n] -> { A[i] -> [0, i] : 0 <= i < n; B[i] -> [1, i] : 0 <= i < n }",
52 "[n] -> { A[i] -> [i] : 0 <= i < n; B[i] -> [n + i] : 0 <= i < n }"));
53
54 EXPECT_TRUE(checkFlatten(
55 "{ A[i] -> [0, i] : 0 <= i < 10; B[i] -> [1, i] : 0 <= i < 10 }",
56 "{ A[i] -> [i] : 0 <= i < 10; B[i] -> [10 + i] : 0 <= i < 10 }"));
57 }
58
TEST(Flatten,FlattenLoop)59 TEST(Flatten, FlattenLoop) {
60 EXPECT_TRUE(checkFlatten(
61 "[n] -> { A[i] -> [i, 0] : 0 <= i < n; B[i] -> [i, 1] : 0 <= i < n }",
62 "[n] -> { A[i] -> [2i] : 0 <= i < n; B[i] -> [2i + 1] : 0 <= i < n }"));
63
64 EXPECT_TRUE(checkFlatten(
65 "{ A[i] -> [i, 0] : 0 <= i < 10; B[i] -> [i, 1] : 0 <= i < 10 }",
66 "{ A[i] -> [2i] : 0 <= i < 10; B[i] -> [2i + 1] : 0 <= i < 10 }"));
67 }
68 } // anonymous namespace
69