1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "unit_test.h"
17 #include "optimizer/ir/graph_cloner.h"
18 #include "optimizer/optimizations/loop_unroll.h"
19 #include "optimizer/optimizations/loop_peeling.h"
20
21 namespace ark::compiler {
22 class GraphCreationTest : public CommonTest {};
23
24 // NOLINTBEGIN(readability-magic-numbers)
TEST_F(GraphCreationTest,CreateEmptyGraph)25 TEST_F(GraphCreationTest, CreateEmptyGraph)
26 {
27 Graph *graph = CreateEmptyGraph();
28 EXPECT_NE(graph, nullptr);
29 }
30
TEST_F(GraphCreationTest,CreateGraphStartEndBlocks)31 TEST_F(GraphCreationTest, CreateGraphStartEndBlocks)
32 {
33 Graph *graph = CreateGraphStartEndBlocks();
34 EXPECT_NE(graph, nullptr);
35 EXPECT_NE(graph->GetStartBlock(), nullptr);
36 EXPECT_NE(graph->GetEndBlock(), nullptr);
37 EXPECT_EQ(graph->GetAliveBlocksCount(), 2U);
38 }
39
TEST_F(GraphCreationTest,OsrModeGraph)40 TEST_F(GraphCreationTest, OsrModeGraph)
41 {
42 auto graph = CreateEmptyGraph();
43 GRAPH(graph)
44 {
45 PARAMETER(0U, 0U).u64();
46 PARAMETER(1U, 1U).u64();
47 PARAMETER(2U, 2U).u64();
48 BASIC_BLOCK(2U, 3U, 4U)
49 {
50 INST(3U, Opcode::Phi).u64().Inputs(1U, 5U);
51 INST(4U, Opcode::Phi).u64().Inputs(2U, 10U);
52 INST(5U, Opcode::Sub).u64().Inputs(3U, 2U);
53 INST(6U, Opcode::SafePoint).Inputs(0U, 3U, 4U).SrcVregs({0U, 1U, 2U});
54 INST(7U, Opcode::Compare).CC(CC_EQ).b().Inputs(5U, 0U);
55 INST(8U, Opcode::IfImm).SrcType(DataType::BOOL).CC(CC_NE).Imm(0U).Inputs(7U);
56 }
57 BASIC_BLOCK(3U, 2U)
58 {
59 INST(9U, Opcode::And).u64().Inputs(4U, 5U);
60 INST(10U, Opcode::Add).u64().Inputs(9U, 4U);
61 }
62 BASIC_BLOCK(4U, -1L)
63 {
64 INST(11U, Opcode::Return).u64().Inputs(4U);
65 }
66 }
67 BB(2U).SetOsrEntry(true);
68 auto cloneGraph = GraphCloner(graph, graph->GetAllocator(), graph->GetLocalAllocator()).CloneGraph();
69
70 graph->RunPass<LoopPeeling>();
71 graph->RunPass<LoopUnroll>(1000U, 4U);
72 EXPECT_TRUE(GraphComparator().Compare(graph, cloneGraph));
73 }
74 // NOLINTEND(readability-magic-numbers)
75
76 } // namespace ark::compiler
77