Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / unittests / Support / Host.cpp
blobb809317147587ce3bdbf1a8c5cdc079257a6863e
1 //========- unittests/Support/Host.cpp - Host.cpp tests --------------========//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Host.h"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/ADT/Triple.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/Path.h"
14 #include "llvm/Support/Program.h"
16 #include "gtest/gtest.h"
18 #define ASSERT_NO_ERROR(x) \
19 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
20 SmallString<128> MessageStorage; \
21 raw_svector_ostream Message(MessageStorage); \
22 Message << #x ": did not return errc::success.\n" \
23 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
24 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
25 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
26 } else { \
29 using namespace llvm;
31 class HostTest : public testing::Test {
32 Triple Host;
34 protected:
35 bool isSupportedArchAndOS() {
36 // Initially this is only testing detection of the number of
37 // physical cores, which is currently only supported/tested for
38 // x86_64 Linux and Darwin.
39 return (Host.getArch() == Triple::x86_64 &&
40 (Host.isOSDarwin() || Host.getOS() == Triple::Linux));
43 HostTest() : Host(Triple::normalize(sys::getProcessTriple())) {}
46 TEST_F(HostTest, NumPhysicalCores) {
47 int Num = sys::getHostNumPhysicalCores();
49 if (isSupportedArchAndOS())
50 ASSERT_GT(Num, 0);
51 else
52 ASSERT_EQ(Num, -1);
55 TEST(getLinuxHostCPUName, ARM) {
56 StringRef CortexA9ProcCpuinfo = R"(
57 processor : 0
58 model name : ARMv7 Processor rev 10 (v7l)
59 BogoMIPS : 1393.66
60 Features : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
61 CPU implementer : 0x41
62 CPU architecture: 7
63 CPU variant : 0x2
64 CPU part : 0xc09
65 CPU revision : 10
67 processor : 1
68 model name : ARMv7 Processor rev 10 (v7l)
69 BogoMIPS : 1393.66
70 Features : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
71 CPU implementer : 0x41
72 CPU architecture: 7
73 CPU variant : 0x2
74 CPU part : 0xc09
75 CPU revision : 10
77 Hardware : Generic OMAP4 (Flattened Device Tree)
78 Revision : 0000
79 Serial : 0000000000000000
80 )";
82 EXPECT_EQ(sys::detail::getHostCPUNameForARM(CortexA9ProcCpuinfo),
83 "cortex-a9");
84 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
85 "CPU part : 0xc0f"),
86 "cortex-a15");
87 // Verify that both CPU implementer and CPU part are checked:
88 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
89 "CPU part : 0xc0f"),
90 "generic");
91 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
92 "CPU part : 0x06f"),
93 "krait");
96 TEST(getLinuxHostCPUName, AArch64) {
97 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
98 "CPU part : 0xd03"),
99 "cortex-a53");
100 // Verify that both CPU implementer and CPU part are checked:
101 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
102 "CPU part : 0xd03"),
103 "generic");
104 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
105 "CPU part : 0x201"),
106 "kryo");
107 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
108 "CPU part : 0x800"),
109 "cortex-a73");
110 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
111 "CPU part : 0x801"),
112 "cortex-a73");
113 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
114 "CPU part : 0xc00"),
115 "falkor");
116 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
117 "CPU part : 0xc01"),
118 "saphira");
120 // MSM8992/4 weirdness
121 StringRef MSM8992ProcCpuInfo = R"(
122 Processor : AArch64 Processor rev 3 (aarch64)
123 processor : 0
124 processor : 1
125 processor : 2
126 processor : 3
127 processor : 4
128 processor : 5
129 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
130 CPU implementer : 0x41
131 CPU architecture: 8
132 CPU variant : 0x0
133 CPU part : 0xd03
134 CPU revision : 3
136 Hardware : Qualcomm Technologies, Inc MSM8992
139 EXPECT_EQ(sys::detail::getHostCPUNameForARM(MSM8992ProcCpuInfo),
140 "cortex-a53");
142 // Exynos big.LITTLE weirdness
143 const std::string ExynosProcCpuInfo = R"(
144 processor : 0
145 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
146 CPU implementer : 0x41
147 CPU architecture: 8
148 CPU variant : 0x0
149 CPU part : 0xd03
151 processor : 1
152 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
153 CPU implementer : 0x53
154 CPU architecture: 8
157 // Verify default for Exynos.
158 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
159 "CPU variant : 0xc\n"
160 "CPU part : 0xafe"),
161 "exynos-m1");
162 // Verify Exynos M1.
163 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
164 "CPU variant : 0x1\n"
165 "CPU part : 0x001"),
166 "exynos-m1");
167 // Verify Exynos M2.
168 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
169 "CPU variant : 0x4\n"
170 "CPU part : 0x001"),
171 "exynos-m2");
173 const std::string ThunderX2T99ProcCpuInfo = R"(
174 processor : 0
175 BogoMIPS : 400.00
176 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics
177 CPU implementer : 0x43
178 CPU architecture: 8
179 CPU variant : 0x1
180 CPU part : 0x0af
183 // Verify different versions of ThunderX2T99.
184 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
185 "CPU implementer : 0x42\n"
186 "CPU part : 0x516"),
187 "thunderx2t99");
189 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
190 "CPU implementer : 0x42\n"
191 "CPU part : 0x0516"),
192 "thunderx2t99");
194 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
195 "CPU implementer : 0x43\n"
196 "CPU part : 0x516"),
197 "thunderx2t99");
199 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
200 "CPU implementer : 0x43\n"
201 "CPU part : 0x0516"),
202 "thunderx2t99");
204 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
205 "CPU implementer : 0x42\n"
206 "CPU part : 0xaf"),
207 "thunderx2t99");
209 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
210 "CPU implementer : 0x42\n"
211 "CPU part : 0x0af"),
212 "thunderx2t99");
214 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
215 "CPU implementer : 0x43\n"
216 "CPU part : 0xaf"),
217 "thunderx2t99");
219 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
220 "CPU implementer : 0x43\n"
221 "CPU part : 0x0af"),
222 "thunderx2t99");
224 // Verify ThunderXT88.
225 const std::string ThunderXT88ProcCpuInfo = R"(
226 processor : 0
227 BogoMIPS : 200.00
228 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
229 CPU implementer : 0x43
230 CPU architecture: 8
231 CPU variant : 0x1
232 CPU part : 0x0a1
235 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo +
236 "CPU implementer : 0x43\n"
237 "CPU part : 0x0a1"),
238 "thunderxt88");
240 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo +
241 "CPU implementer : 0x43\n"
242 "CPU part : 0xa1"),
243 "thunderxt88");
245 // Verify HiSilicon processors.
246 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x48\n"
247 "CPU part : 0xd01"),
248 "tsv110");
251 #if defined(__APPLE__)
252 TEST_F(HostTest, getMacOSHostVersion) {
253 using namespace llvm::sys;
254 llvm::Triple HostTriple(getProcessTriple());
255 if (!HostTriple.isMacOSX())
256 return;
258 SmallString<128> TestDirectory;
259 ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory));
260 SmallString<128> OutputFile(TestDirectory);
261 path::append(OutputFile, "out");
263 const char *SwVersPath = "/usr/bin/sw_vers";
264 StringRef argv[] = {SwVersPath, "-productVersion"};
265 StringRef OutputPath = OutputFile.str();
266 const Optional<StringRef> Redirects[] = {/*STDIN=*/None,
267 /*STDOUT=*/OutputPath,
268 /*STDERR=*/None};
269 int RetCode = ExecuteAndWait(SwVersPath, argv, /*env=*/llvm::None, Redirects);
270 ASSERT_EQ(0, RetCode);
272 int FD = 0;
273 ASSERT_NO_ERROR(fs::openFileForRead(OutputPath, FD));
274 off_t Size = ::lseek(FD, 0, SEEK_END);
275 ASSERT_NE(-1, Size);
276 ::lseek(FD, 0, SEEK_SET);
277 std::unique_ptr<char[]> Buffer = llvm::make_unique<char[]>(Size);
278 ASSERT_EQ(::read(FD, Buffer.get(), Size), Size);
279 ::close(FD);
281 // Ensure that the two versions match.
282 StringRef SystemVersion(Buffer.get(), Size);
283 unsigned SystemMajor, SystemMinor, SystemMicro;
284 ASSERT_EQ(llvm::Triple((Twine("x86_64-apple-macos") + SystemVersion))
285 .getMacOSXVersion(SystemMajor, SystemMinor, SystemMicro),
286 true);
287 unsigned HostMajor, HostMinor, HostMicro;
288 ASSERT_EQ(HostTriple.getMacOSXVersion(HostMajor, HostMinor, HostMicro), true);
290 // Don't compare the 'Micro' version, as it's always '0' for the 'Darwin'
291 // triples.
292 ASSERT_EQ(std::tie(SystemMajor, SystemMinor), std::tie(HostMajor, HostMinor));
294 ASSERT_NO_ERROR(fs::remove(OutputPath));
295 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
297 #endif