1 /* 2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 * Copyright (c) 2024 Huawei Device Co., Ltd. 16 * Licensed under the Apache License, Version 2.0 (the "License"); 17 * you may not use this file except in compliance with the License. 18 * You may obtain a copy of the License at 19 20 * http://www.apache.org/licenses/LICENSE-2.0 21 * 22 * Unless required by applicable law or agreed to in writing, software 23 * distributed under the License is distributed on an "AS IS" BASIS, 24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 25 * See the License for the specific language governing permissions and 26 * limitations under the License. 27 */ 28 29 #include <filesystem> 30 #include <iostream> 31 #include <set> 32 #include <string> 33 #include <vector> 34 #include <array> 35 #include <cstdlib> 36 #include <bits/types/FILE.h> 37 #include <gtest/gtest.h> 38 #include "graph_test.h" 39 40 using namespace testing::ext; 41 42 namespace panda::compiler { 43 class DrawCfgTest : public testing::Test { 44 public: SetUpTestCase(void)45 static void SetUpTestCase(void) {} TearDownTestCase(void)46 static void TearDownTestCase(void) {} SetUp()47 void SetUp() {} TearDown()48 void TearDown() {} 49 GenGraphDump(const std::string & abc_file,const std::string & dump_file,const std::set<std::string> & test_method_names)50 void GenGraphDump(const std::string &abc_file, const std::string &dump_file, 51 const std::set<std::string> &test_method_names) 52 { 53 std::ofstream dump_out(dump_file); 54 options.SetCompilerUseSafepoint(false); 55 graph_test_.TestBuildGraphFromFile( 56 abc_file, 57 [&test_method_names, &dump_out](Graph *graph, std::string &method_name) { 58 if (test_method_names.count(method_name) == 0) { 59 return; 60 } 61 62 graph->Dump(&dump_out); 63 }, 64 true); 65 } 66 DrawCfg(const std::string & dump_file,const std::vector<std::string> & args)67 static bool DrawCfg(const std::string &dump_file, const std::vector<std::string> &args) 68 { 69 const char *script = DRAW_CFG_TEST_TOOLS_DIR "/draw_cfg.py"; 70 std::string command(script); 71 for (const auto &arg : args) { 72 command.push_back(' '); 73 command.append(arg); 74 } 75 command.append(" --out " DRAW_CFG_TEST_OUT_DIR); 76 command.append(" < " + dump_file); 77 78 int ret = system(command.c_str()); 79 return ret == 0; 80 } 81 CleanOutputs()82 static void CleanOutputs() 83 { 84 std::filesystem::remove_all(DRAW_CFG_TEST_OUT_DIR); 85 } 86 87 GraphTest graph_test_; 88 }; 89 90 /** 91 * @tc.name: draw_cfg_test_001 92 * @tc.desc: Verify drawing multiple functions. 93 * @tc.type: FUNC 94 * @tc.require: issueNumber 95 */ 96 HWTEST_F(DrawCfgTest, draw_cfg_test_001, TestSize.Level1) 97 { 98 const char *abc_file = DRAW_CFG_TEST_ABC_DIR "regallocTest.abc"; 99 const char *dump_file = DRAW_CFG_TEST_OUT_DIR "regallocTest.dump"; 100 std::set<std::string> test_methods { 101 "func", 102 "func2", 103 "func3", 104 "func4", 105 "func_main_0", 106 }; 107 108 std::filesystem::create_directory(DRAW_CFG_TEST_OUT_DIR); 109 GenGraphDump(abc_file, dump_file, test_methods); 110 EXPECT_TRUE(std::filesystem::exists(dump_file)); 111 112 EXPECT_TRUE(DrawCfg(dump_file, {})); 113 114 for (auto test_method : test_methods) { 115 std::string dot_file = DRAW_CFG_TEST_OUT_DIR "cfg_L_GLOBAL;::" + test_method + ".dot"; 116 std::string png_file = DRAW_CFG_TEST_OUT_DIR "cfg_L_GLOBAL;::" + test_method + ".png"; 117 EXPECT_TRUE(std::filesystem::exists(dot_file)); 118 EXPECT_TRUE(std::filesystem::exists(png_file)); 119 } 120 121 CleanOutputs(); 122 } 123 124 /** 125 * @tc.name: draw_cfg_test_002 126 * @tc.desc: Verify drawing multiple functions without instructions. 127 * @tc.type: FUNC 128 * @tc.require: issueNumber 129 */ 130 HWTEST_F(DrawCfgTest, draw_cfg_test_002, TestSize.Level1) 131 { 132 const char *abc_file = DRAW_CFG_TEST_ABC_DIR "regallocTest.abc"; 133 const char *dump_file = DRAW_CFG_TEST_OUT_DIR "regallocTest.dump"; 134 std::set<std::string> test_methods { 135 "func", 136 "func2", 137 "func3", 138 "func4", 139 "func_main_0", 140 }; 141 142 std::filesystem::create_directory(DRAW_CFG_TEST_OUT_DIR); 143 GenGraphDump(abc_file, dump_file, test_methods); 144 EXPECT_TRUE(std::filesystem::exists(dump_file)); 145 146 EXPECT_TRUE(DrawCfg(dump_file, {"--no-insts"})); 147 148 for (auto test_method : test_methods) { 149 std::string dot_file = DRAW_CFG_TEST_OUT_DIR "cfg_L_GLOBAL;::" + test_method + ".dot"; 150 std::string png_file = DRAW_CFG_TEST_OUT_DIR "cfg_L_GLOBAL;::" + test_method + ".png"; 151 EXPECT_TRUE(std::filesystem::exists(dot_file)); 152 EXPECT_TRUE(std::filesystem::exists(png_file)); 153 } 154 155 CleanOutputs(); 156 } 157 } // namespace panda::compiler 158