xref: /llvm-project/polly/unittests/Flatten/FlattenTest.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
17886bd7cSMichael Kruse //===- FlattenTest.cpp ----------------------------------------------------===//
27886bd7cSMichael Kruse //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67886bd7cSMichael Kruse //
77886bd7cSMichael Kruse //===----------------------------------------------------------------------===//
87886bd7cSMichael Kruse 
97886bd7cSMichael Kruse #include "polly/FlattenAlgo.h"
107886bd7cSMichael Kruse #include "polly/Support/GICHelper.h"
117886bd7cSMichael Kruse #include "gtest/gtest.h"
127886bd7cSMichael Kruse #include "isl/union_map.h"
137886bd7cSMichael Kruse 
147886bd7cSMichael Kruse using namespace llvm;
157886bd7cSMichael Kruse using namespace polly;
167886bd7cSMichael Kruse 
177886bd7cSMichael Kruse namespace {
187886bd7cSMichael Kruse 
197886bd7cSMichael Kruse /// Flatten a schedule and compare to the expected result.
207886bd7cSMichael Kruse ///
217886bd7cSMichael Kruse /// @param ScheduleStr The schedule to flatten as string.
227886bd7cSMichael Kruse /// @param ExpectedStr The expected result as string.
237886bd7cSMichael Kruse ///
247886bd7cSMichael Kruse /// @result Whether the flattened schedule is the same as the expected schedule.
checkFlatten(const char * ScheduleStr,const char * ExpectedStr)257886bd7cSMichael Kruse bool checkFlatten(const char *ScheduleStr, const char *ExpectedStr) {
267886bd7cSMichael Kruse   auto *Ctx = isl_ctx_alloc();
27d3d3d6b7STobias Grosser   bool Success;
287886bd7cSMichael Kruse 
297886bd7cSMichael Kruse   {
300ba8c4a8STobias Grosser     auto Schedule = isl::union_map(Ctx, ScheduleStr);
310ba8c4a8STobias Grosser     auto Expected = isl::union_map(Ctx, ExpectedStr);
327886bd7cSMichael Kruse 
337886bd7cSMichael Kruse     auto Result = flattenSchedule(std::move(Schedule));
34d3d3d6b7STobias Grosser     Success = Result.is_equal(Expected);
357886bd7cSMichael Kruse   }
367886bd7cSMichael Kruse 
377886bd7cSMichael Kruse   isl_ctx_free(Ctx);
38d3d3d6b7STobias Grosser   return Success;
397886bd7cSMichael Kruse }
407886bd7cSMichael Kruse 
TEST(Flatten,FlattenTrivial)417886bd7cSMichael Kruse TEST(Flatten, FlattenTrivial) {
427886bd7cSMichael Kruse   EXPECT_TRUE(checkFlatten("{ A[] -> [0] }", "{ A[] -> [0] }"));
437886bd7cSMichael Kruse   EXPECT_TRUE(checkFlatten("{ A[i] -> [i, 0] : 0 <= i < 10 }",
447886bd7cSMichael Kruse                            "{ A[i] -> [i] : 0 <= i < 10 }"));
457886bd7cSMichael Kruse   EXPECT_TRUE(checkFlatten("{ A[i] -> [0, i] : 0 <= i < 10 }",
467886bd7cSMichael Kruse                            "{ A[i] -> [i] : 0 <= i < 10 }"));
477886bd7cSMichael Kruse }
487886bd7cSMichael Kruse 
TEST(Flatten,FlattenSequence)497886bd7cSMichael Kruse TEST(Flatten, FlattenSequence) {
507886bd7cSMichael Kruse   EXPECT_TRUE(checkFlatten(
517886bd7cSMichael Kruse       "[n] -> { A[i] -> [0, i] : 0 <= i < n; B[i] -> [1, i] : 0 <= i < n }",
527886bd7cSMichael Kruse       "[n] -> { A[i] -> [i] : 0 <= i < n; B[i] -> [n + i] : 0 <= i < n }"));
537886bd7cSMichael Kruse 
547886bd7cSMichael Kruse   EXPECT_TRUE(checkFlatten(
557886bd7cSMichael Kruse       "{ A[i] -> [0, i] : 0 <= i < 10; B[i] -> [1, i] : 0 <= i < 10 }",
567886bd7cSMichael Kruse       "{ A[i] -> [i] : 0 <= i < 10; B[i] -> [10 + i] : 0 <= i < 10 }"));
577886bd7cSMichael Kruse }
587886bd7cSMichael Kruse 
TEST(Flatten,FlattenLoop)597886bd7cSMichael Kruse TEST(Flatten, FlattenLoop) {
607886bd7cSMichael Kruse   EXPECT_TRUE(checkFlatten(
617886bd7cSMichael Kruse       "[n] -> { A[i] -> [i, 0] : 0 <= i < n; B[i] -> [i, 1] : 0 <= i < n }",
627886bd7cSMichael Kruse       "[n] -> { A[i] -> [2i] : 0 <= i < n; B[i] -> [2i + 1] : 0 <= i < n }"));
637886bd7cSMichael Kruse 
647886bd7cSMichael Kruse   EXPECT_TRUE(checkFlatten(
657886bd7cSMichael Kruse       "{ A[i] -> [i, 0] : 0 <= i < 10; B[i] -> [i, 1] : 0 <= i < 10 }",
667886bd7cSMichael Kruse       "{ A[i] -> [2i] : 0 <= i < 10; B[i] -> [2i + 1] : 0 <= i < 10 }"));
677886bd7cSMichael Kruse }
687886bd7cSMichael Kruse } // anonymous namespace
69