Debian Codegraph @

llvm/unittests/CGData/StableFunctionMapRecordTest.cpp

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