File: StableFunctionMapRecordTest.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (127 lines) | stat: -rw-r--r-- 3,893 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//===- StableFunctionMapRecordTest.cpp ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/CGData/StableFunctionMapRecord.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

TEST(StableFunctionMapRecordTest, Print) {
  StableFunctionMapRecord MapRecord;
  StableFunction Func1{1, "Func1", "Mod1", 2, {{{0, 1}, 3}}};
  MapRecord.FunctionMap->insert(Func1);

  const char *ExpectedMapStr = R"(---
- Hash:            1
  FunctionName:    Func1
  ModuleName:      Mod1
  InstCount:       2
  IndexOperandHashes:
    - InstIndex:       0
      OpndIndex:       1
      OpndHash:        3
...
)";
  std::string MapDump;
  raw_string_ostream OS(MapDump);
  MapRecord.print(OS);
  EXPECT_EQ(ExpectedMapStr, MapDump);
}

TEST(StableFunctionMapRecordTest, Stable) {
  StableFunction Func1{1, "Func2", "Mod1", 1, {}};
  StableFunction Func2{1, "Func3", "Mod1", 1, {}};
  StableFunction Func3{1, "Func1", "Mod2", 1, {}};
  StableFunction Func4{2, "Func4", "Mod3", 1, {}};

  StableFunctionMapRecord MapRecord1;
  MapRecord1.FunctionMap->insert(Func1);
  MapRecord1.FunctionMap->insert(Func2);
  MapRecord1.FunctionMap->insert(Func3);
  MapRecord1.FunctionMap->insert(Func4);

  StableFunctionMapRecord MapRecord2;
  MapRecord2.FunctionMap->insert(Func4);
  MapRecord2.FunctionMap->insert(Func3);
  MapRecord2.FunctionMap->insert(Func2);
  MapRecord2.FunctionMap->insert(Func1);

  // Output is sorted by hash (1 < 2), module name (Mod1 < Mod2), and function
  // name (Func2 < Func3).
  std::string MapDump1;
  raw_string_ostream OS1(MapDump1);
  MapRecord1.print(OS1);
  std::string MapDump2;
  raw_string_ostream OS2(MapDump2);
  MapRecord2.print(OS2);
  EXPECT_EQ(MapDump1, MapDump2);
}

TEST(StableFunctionMapRecordTest, Serialize) {
  StableFunctionMapRecord MapRecord1;
  StableFunction Func1{1, "Func1", "Mod1", 2, {{{0, 1}, 3}, {{1, 2}, 4}}};
  StableFunction Func2{2, "Func2", "Mod1", 3, {{{0, 1}, 2}}};
  StableFunction Func3{2, "Func3", "Mod1", 3, {{{0, 1}, 3}}};
  MapRecord1.FunctionMap->insert(Func1);
  MapRecord1.FunctionMap->insert(Func2);
  MapRecord1.FunctionMap->insert(Func3);

  // Serialize and deserialize the map.
  SmallVector<char> Out;
  raw_svector_ostream OS(Out);
  MapRecord1.serialize(OS);

  StableFunctionMapRecord MapRecord2;
  const uint8_t *Data = reinterpret_cast<const uint8_t *>(Out.data());
  MapRecord2.deserialize(Data);

  // Two maps should be identical.
  std::string MapDump1;
  raw_string_ostream OS1(MapDump1);
  MapRecord1.print(OS1);
  std::string MapDump2;
  raw_string_ostream OS2(MapDump2);
  MapRecord2.print(OS2);

  EXPECT_EQ(MapDump1, MapDump2);
}

TEST(StableFunctionMapRecordTest, SerializeYAML) {
  StableFunctionMapRecord MapRecord1;
  StableFunction Func1{1, "Func1", "Mod1", 2, {{{0, 1}, 3}, {{1, 2}, 4}}};
  StableFunction Func2{2, "Func2", "Mod1", 3, {{{0, 1}, 2}}};
  StableFunction Func3{2, "Func3", "Mod1", 3, {{{0, 1}, 3}}};
  MapRecord1.FunctionMap->insert(Func1);
  MapRecord1.FunctionMap->insert(Func2);
  MapRecord1.FunctionMap->insert(Func3);

  // Serialize and deserialize the map in a YAML format.
  std::string Out;
  raw_string_ostream OS(Out);
  yaml::Output YOS(OS);
  MapRecord1.serializeYAML(YOS);

  StableFunctionMapRecord MapRecord2;
  yaml::Input YIS(StringRef(Out.data(), Out.size()));
  MapRecord2.deserializeYAML(YIS);

  // Two maps should be identical.
  std::string MapDump1;
  raw_string_ostream OS1(MapDump1);
  MapRecord1.print(OS1);
  std::string MapDump2;
  raw_string_ostream OS2(MapDump2);
  MapRecord2.print(OS2);

  EXPECT_EQ(MapDump1, MapDump2);
}

} // end namespace