2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "luci/Pass/FoldCastPass.h"
18 #include "PassTestGraphs.h"
20 #include <luci/IR/CircleNodes.h>
22 #include <gtest/gtest.h>
27 template <loco::DataType FromT, loco::DataType ToT>
28 class FoldCastTest : public luci::ConstantFoldingAddTestGraph
31 FoldCastTest(std::initializer_list<uint32_t> shape)
32 : luci::ConstantFoldingAddTestGraph(shape, ToT)
34 _cast = _g.nodes()->create<luci::CircleCast>();
35 _x = _g.nodes()->create<luci::CircleConst>();
43 uint32_t num_elems = 1;
44 for (auto dim = shape.begin(); dim != shape.end(); dim++)
47 _x->size<FromT>(num_elems);
48 for (uint32_t i = 0; i < num_elems; i++)
49 _x->at<FromT>(i) = i + 1;
57 loco::Node *createFoldedPattern() override { return _cast; }
60 luci::CircleCast *_cast = nullptr;
61 luci::CircleConst *_x = nullptr;
65 * Graph that has a Cast Op with constant input
78 class FoldS64ToS32CastTest : public FoldCastTest<loco::DataType::S64, loco::DataType::S32>,
79 public ::testing::Test
82 FoldS64ToS32CastTest() : FoldCastTest<loco::DataType::S64, loco::DataType::S32>({3}) {}
84 virtual void SetUp() { init(); }
89 TEST(FoldCastPassTest, name)
91 luci::FoldCastPass pass;
92 auto const name = pass.name();
93 ASSERT_NE(nullptr, name);
96 TEST_F(FoldS64ToS32CastTest, fold_cast_s64_to_s32)
98 luci::FoldCastPass pass;
99 while (pass.run(graph()))
102 auto folded_const = getFoldedPattern();
103 EXPECT_NE(nullptr, folded_const);
105 // Check type, shape, values of folded const
106 EXPECT_EQ(loco::DataType::S32, folded_const->dtype());
107 EXPECT_EQ(1, folded_const->rank());
108 EXPECT_EQ(3, folded_const->dim(0).value());
109 EXPECT_EQ(1, folded_const->at<loco::DataType::S32>(0));
110 EXPECT_EQ(2, folded_const->at<loco::DataType::S32>(1));
111 EXPECT_EQ(3, folded_const->at<loco::DataType::S32>(2));