1 //===- StableFunctionMapRecordTest.cpp ------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/CGData/StableFunctionMapRecord.h" 10 #include "gmock/gmock.h" 11 #include "gtest/gtest.h" 12 13 using namespace llvm; 14 15 namespace { 16 17 TEST(StableFunctionMapRecordTest, Print) { 18 StableFunctionMapRecord MapRecord; 19 StableFunction Func1{1, "Func1", "Mod1", 2, {{{0, 1}, 3}}}; 20 MapRecord.FunctionMap->insert(Func1); 21 22 const char *ExpectedMapStr = R"(--- 23 - Hash: 1 24 FunctionName: Func1 25 ModuleName: Mod1 26 InstCount: 2 27 IndexOperandHashes: 28 - InstIndex: 0 29 OpndIndex: 1 30 OpndHash: 3 31 ... 32 )"; 33 std::string MapDump; 34 raw_string_ostream OS(MapDump); 35 MapRecord.print(OS); 36 EXPECT_EQ(ExpectedMapStr, MapDump); 37 } 38 39 TEST(StableFunctionMapRecordTest, Stable) { 40 StableFunction Func1{1, "Func2", "Mod1", 1, {}}; 41 StableFunction Func2{1, "Func3", "Mod1", 1, {}}; 42 StableFunction Func3{1, "Func1", "Mod2", 1, {}}; 43 StableFunction Func4{2, "Func4", "Mod3", 1, {}}; 44 45 StableFunctionMapRecord MapRecord1; 46 MapRecord1.FunctionMap->insert(Func1); 47 MapRecord1.FunctionMap->insert(Func2); 48 MapRecord1.FunctionMap->insert(Func3); 49 MapRecord1.FunctionMap->insert(Func4); 50 51 StableFunctionMapRecord MapRecord2; 52 MapRecord2.FunctionMap->insert(Func4); 53 MapRecord2.FunctionMap->insert(Func3); 54 MapRecord2.FunctionMap->insert(Func2); 55 MapRecord2.FunctionMap->insert(Func1); 56 57 // Output is sorted by hash (1 < 2), module name (Mod1 < Mod2), and function 58 // name (Func2 < Func3). 59 std::string MapDump1; 60 raw_string_ostream OS1(MapDump1); 61 MapRecord1.print(OS1); 62 std::string MapDump2; 63 raw_string_ostream OS2(MapDump2); 64 MapRecord2.print(OS2); 65 EXPECT_EQ(MapDump1, MapDump2); 66 } 67 68 TEST(StableFunctionMapRecordTest, Serialize) { 69 StableFunctionMapRecord MapRecord1; 70 StableFunction Func1{1, "Func1", "Mod1", 2, {{{0, 1}, 3}, {{1, 2}, 4}}}; 71 StableFunction Func2{2, "Func2", "Mod1", 3, {{{0, 1}, 2}}}; 72 StableFunction Func3{2, "Func3", "Mod1", 3, {{{0, 1}, 3}}}; 73 MapRecord1.FunctionMap->insert(Func1); 74 MapRecord1.FunctionMap->insert(Func2); 75 MapRecord1.FunctionMap->insert(Func3); 76 77 // Serialize and deserialize the map. 78 SmallVector<char> Out; 79 raw_svector_ostream OS(Out); 80 MapRecord1.serialize(OS); 81 82 StableFunctionMapRecord MapRecord2; 83 const uint8_t *Data = reinterpret_cast<const uint8_t *>(Out.data()); 84 MapRecord2.deserialize(Data); 85 86 // Two maps should be identical. 87 std::string MapDump1; 88 raw_string_ostream OS1(MapDump1); 89 MapRecord1.print(OS1); 90 std::string MapDump2; 91 raw_string_ostream OS2(MapDump2); 92 MapRecord2.print(OS2); 93 94 EXPECT_EQ(MapDump1, MapDump2); 95 } 96 97 TEST(StableFunctionMapRecordTest, SerializeYAML) { 98 StableFunctionMapRecord MapRecord1; 99 StableFunction Func1{1, "Func1", "Mod1", 2, {{{0, 1}, 3}, {{1, 2}, 4}}}; 100 StableFunction Func2{2, "Func2", "Mod1", 3, {{{0, 1}, 2}}}; 101 StableFunction Func3{2, "Func3", "Mod1", 3, {{{0, 1}, 3}}}; 102 MapRecord1.FunctionMap->insert(Func1); 103 MapRecord1.FunctionMap->insert(Func2); 104 MapRecord1.FunctionMap->insert(Func3); 105 106 // Serialize and deserialize the map in a YAML format. 107 std::string Out; 108 raw_string_ostream OS(Out); 109 yaml::Output YOS(OS); 110 MapRecord1.serializeYAML(YOS); 111 112 StableFunctionMapRecord MapRecord2; 113 yaml::Input YIS(StringRef(Out.data(), Out.size())); 114 MapRecord2.deserializeYAML(YIS); 115 116 // Two maps should be identical. 117 std::string MapDump1; 118 raw_string_ostream OS1(MapDump1); 119 MapRecord1.print(OS1); 120 std::string MapDump2; 121 raw_string_ostream OS2(MapDump2); 122 MapRecord2.print(OS2); 123 124 EXPECT_EQ(MapDump1, MapDump2); 125 } 126 127 } // end namespace 128