• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 #include <numeric>
18 #include <unordered_map>
19 
20 #include "compiler_options.h"
21 #include "graph_test.h"
22 #include "mem/arena_allocator.h"
23 #include "mem/pool_manager.h"
24 #include "optimizer/analysis/rpo.h"
25 #include "optimizer/analysis/loop_analyzer.h"
26 #include "optimizer/optimizations/cleanup.h"
27 #include "optimizer/optimizations/lowering.h"
28 #include "optimizer/optimizations/move_constants.h"
29 #include "optimizer/optimizations/vn.h"
30 #include "optimizer/ir/basicblock.h"
31 #include "optimizer/ir/graph.h"
32 #include "optimizer/ir/inst.h"
33 #include "utils/arena_containers.h"
34 
35 using namespace testing::ext;
36 
37 namespace panda::compiler {
38 class OptimizationsTest : public testing::Test {
39 public:
SetUpTestCase(void)40     static void SetUpTestCase(void) {}
TearDownTestCase(void)41     static void TearDownTestCase(void) {}
SetUp()42     void SetUp() {}
TearDown()43     void TearDown() {}
44 
45     GraphTest graph_test_;
46 };
47 
48 /**
49  * @tc.name: optimizations_test_001
50  * @tc.desc: Verify the Add function.
51  * @tc.type: FUNC
52  * @tc.require: issueNumber
53  */
54 HWTEST_F(OptimizationsTest, optimizations_test_001, TestSize.Level1)
55 {
56     panda::compiler::VnObject vn_obj;
57     uint32_t obj1 = 1U;
58     uint64_t obj2 = 1U;
59     vn_obj.Add(obj1);
60     vn_obj.Add(obj2);
61     auto size = vn_obj.GetSize();
62     EXPECT_NE(vn_obj.GetArray(), nullptr);
63     EXPECT_EQ(vn_obj.GetElement(0), 1);
64     EXPECT_EQ(size, 3);
65 }
66 
67 /**
68  * @tc.name: optimizations_test_002
69  * @tc.desc: Verify the VnObject function.
70  * @tc.type: FUNC
71  * @tc.require: issueNumber
72  */
73 HWTEST_F(OptimizationsTest, optimizations_test_002, TestSize.Level1)
74 {
75     std::string pfile = GRAPH_TEST_ABC_DIR "regallocTest.abc";
76     const char *test_method_name = "func_main_0";
77     bool status = false;
78     bool status1 = false;
79     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status, &status1](Graph* graph,
__anon9a96323c0102(Graph* graph, std::string &method_name) 80         std::string &method_name) {
81         if (test_method_name != method_name) {
82             return;
83         }
84         EXPECT_NE(graph, nullptr);
85         Inst *inst1 = nullptr;
86         Inst *inst2 = nullptr;
87         for (const auto &block : graph->GetVectorBlocks()) {
88             if (block == nullptr) {
89                 continue;
90             }
91             for (auto inst : block->AllInsts()) {
92                 if (inst->GetOpcode() == Opcode::SaveState) {
93                     inst->SetType(DataType::UINT64);
94                     EXPECT_EQ(inst->GetType(), DataType::UINT64);
95                     inst1 = inst;
96                     status = true;
97                 }
98 
99                 if (inst->GetOpcode() == Opcode::Intrinsic) {
100                     inst->SetType(DataType::UINT64);
101                     EXPECT_EQ(inst->GetType(), DataType::UINT64);
102                     inst->SetFlag(inst_flags::Flags::NONE);
103                     EXPECT_FALSE(inst->GetFlag(inst_flags::Flags::NONE));
104                     inst2 = inst;
105                     status1 = true;
106                 }
107             }
108         }
109         panda::compiler::VnObject vn_obj;
110         inst1->SetVN(0);
111         EXPECT_EQ(inst1->GetVN(), 0);
112         vn_obj.Add(inst1);
113         inst2->SetVN(1);
114         EXPECT_EQ(inst2->GetVN(), 1);
115         vn_obj.Add(inst2);
116         auto size = vn_obj.GetSize();
117         EXPECT_EQ(size, 5);
118     });
119     EXPECT_TRUE(status);
120     EXPECT_TRUE(status1);
121 }
122 
123 /**
124  * @tc.name: optimizations_test_003
125  * @tc.desc: Verify the Compare function.
126  * @tc.type: FUNC
127  * @tc.require: issueNumber
128  */
129 HWTEST_F(OptimizationsTest, optimizations_test_003, TestSize.Level1)
130 {
131     panda::compiler::VnObject vn_obj;
132     uint32_t obj1 = 1U;
133     uint64_t obj2 = 1U;
134     vn_obj.Add(obj1);
135     vn_obj.Add(obj2);
136     auto size = vn_obj.GetSize();
137     EXPECT_EQ(size, 3);
138 
139     EXPECT_TRUE(vn_obj.Compare(&vn_obj));
140 
141     panda::compiler::VnObject vn_obj1;
142     EXPECT_FALSE(vn_obj.Compare(&vn_obj1));
143     EXPECT_TRUE(vn_obj1.Compare(&vn_obj1));
144 
145     obj1 = 2U;
146     obj2 = 3U;
147     vn_obj1.Add(obj1);
148     vn_obj1.Add(obj2);
149     VnObjEqual vnEqual;
150     vnEqual(&vn_obj, &vn_obj1);
151     EXPECT_FALSE(vn_obj.Compare(&vn_obj1));
152 }
153 
154 /**
155  * @tc.name: optimizations_test_004
156  * @tc.desc: Verify the FindEqualVnOrCreateNew function.
157  * @tc.type: FUNC
158  * @tc.require: issueNumber
159  */
160 HWTEST_F(OptimizationsTest, optimizations_test_004, TestSize.Level1)
161 {
162     std::string pfile = GRAPH_TEST_ABC_DIR "regallocTryTest.abc";
163     const char *test_method_name = "func2";
164     bool status = false;
__anon9a96323c0202(Graph* graph, std::string &method_name) 165     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
166         if (test_method_name != method_name) {
167             return;
168         }
169 
170         EXPECT_NE(graph, nullptr);
171         EXPECT_FALSE(graph->RunPass<ValNum>());
172         ValNum vnum(graph);
173         for (const auto &block : graph->GetVectorBlocks()) {
174             if (block == nullptr) {
175                 continue;
176             }
177             for (auto inst : block->AllInsts()) {
178                 if (inst->GetOpcode() == Opcode::Compare) {
179                     vnum.FindEqualVnOrCreateNew(inst);
180                     status = true;
181                 }
182             }
183         }
184         EXPECT_EQ(vnum.GetPassName(), "GVN");
185         EXPECT_TRUE(vnum.IsEnable());
186     });
187     EXPECT_TRUE(status);
188 }
189 
190 /**
191  * @tc.name: optimizations_test_005
192  * @tc.desc: Verify the MoveConstants function.
193  * @tc.type: FUNC
194  * @tc.require: issueNumber
195  */
196 HWTEST_F(OptimizationsTest, optimizations_test_005, TestSize.Level1)
197 {
198     std::string pfile = GRAPH_TEST_ABC_DIR "regallocTest.abc";
199     const char *test_method_name = "func1";
200     bool status = false;
__anon9a96323c0302(Graph* graph, std::string &method_name) 201     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
202         if (test_method_name != method_name) {
203             return;
204         }
205         status = true;
206         EXPECT_NE(graph, nullptr);
207         EXPECT_TRUE(graph->RunPass<MoveConstants>());
208     });
209     EXPECT_TRUE(status);
210 }
211 
212 /**
213  * @tc.name: optimizations_test_006
214  * @tc.desc: Verify the Lowering function.
215  * @tc.type: FUNC
216  * @tc.require: issueNumber
217  */
218 HWTEST_F(OptimizationsTest, optimizations_test_006, TestSize.Level1)
219 {
220     std::string pfile = GRAPH_TEST_ABC_DIR "regallocTryTest.abc";
221     const char *test_method_name = "func_main_0";
222     bool status = false;
__anon9a96323c0402(Graph* graph, std::string &method_name) 223     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
224         if (test_method_name != method_name) {
225             return;
226         }
227         status = true;
228         EXPECT_NE(graph, nullptr);
229         EXPECT_TRUE(graph->RunPass<Lowering>());
230         EXPECT_TRUE(graph->RunPass<Cleanup>());
231     });
232     EXPECT_TRUE(status);
233 }
234 
235 /**
236  * @tc.name: optimizations_test_007
237  * @tc.desc: Verify the Cleanup function.
238  * @tc.type: FUNC
239  * @tc.require: issueNumber
240  */
241 HWTEST_F(OptimizationsTest, optimizations_test_007, TestSize.Level1)
242 {
243     std::string pfile = GRAPH_TEST_ABC_DIR "regallocTryTest.abc";
244     const char *test_method_name = "func1";
245     bool status = false;
__anon9a96323c0502(Graph* graph, std::string &method_name) 246     graph_test_.TestBuildGraphFromFile(pfile, [&test_method_name, &status](Graph* graph, std::string &method_name) {
247         if (test_method_name != method_name) {
248             return;
249         }
250         status = true;
251         EXPECT_NE(graph, nullptr);
252         EXPECT_TRUE(graph->RunPass<Lowering>());
253         EXPECT_TRUE(graph->RunPass<Cleanup>());
254     });
255     EXPECT_TRUE(status);
256 }
257 }
258