| 1 |
//===-- SnippetFileTest.cpp -------------------------------------*- C++ -*-===// |
| 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 "SnippetFile.h" |
| 10 |
|
| 11 |
#include "LlvmState.h" |
| 12 |
#include "TestBase.h" |
| 13 |
#include "X86InstrInfo.h" |
| 14 |
#include "llvm/MC/TargetRegistry.h" |
| 15 |
#include "llvm/Support/Error.h" |
| 16 |
#include "llvm/Support/FileSystem.h" |
| 17 |
#include "llvm/Support/Path.h" |
| 18 |
#include "llvm/Support/TargetSelect.h" |
| 19 |
#include "llvm/Support/raw_ostream.h" |
| 20 |
#include "llvm/Testing/Support/SupportHelpers.h" |
| 21 |
#include "gmock/gmock.h" |
| 22 |
#include "gtest/gtest.h" |
| 23 |
|
| 24 |
namespace llvm { |
| 25 |
namespace exegesis { |
| 26 |
|
| 27 |
void InitializeX86ExegesisTarget(); |
| 28 |
|
| 29 |
namespace { |
| 30 |
|
| 31 |
using testing::ElementsAre; |
| 32 |
using testing::Eq; |
| 33 |
using testing::Property; |
| 34 |
using testing::SizeIs; |
| 35 |
using testing::UnorderedElementsAre; |
| 36 |
|
| 37 |
using llvm::unittest::TempDir; |
| 38 |
|
| 39 |
class X86SnippetFileTest : public X86TestBase { |
| 40 |
protected: |
| 41 |
Expected<std::vector<BenchmarkCode>> TestCommon(StringRef Contents) { |
| 42 |
TempDir TestDirectory("SnippetFileTestDir", /*Unique*/ true); |
| 43 |
SmallString<64> Filename(TestDirectory.path()); |
| 44 |
sys::path::append(Filename, "snippet.s"); |
| 45 |
errs() << Filename << "-------\n"; |
| 46 |
{ |
| 47 |
std::error_code EC; |
| 48 |
raw_fd_ostream FOS(Filename, EC); |
| 49 |
FOS << Contents; |
| 50 |
EXPECT_FALSE(EC); |
| 51 |
} |
| 52 |
return readSnippets(State, Filename); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
// FIXME: Refactor these to ../Common/Matchers.h |
| 57 |
static auto HasOpcode = [](unsigned Opcode) { |
| 58 |
return Property(&MCInst::getOpcode, Eq(Opcode)); |
| 59 |
}; |
| 60 |
|
| 61 |
MATCHER_P2(RegisterInitialValueIs, Reg, Val, "") { |
| 62 |
if (arg.Register == Reg && |
| 63 |
arg.Value.getLimitedValue() == static_cast<uint64_t>(Val)) |
| 64 |
return true; |
| 65 |
*result_listener << "expected: {" << Reg << ", " << Val << "} "; |
| 66 |
*result_listener << "actual: {" << arg.Register << ", " |
| 67 |
<< arg.Value.getLimitedValue() << "}"; |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
MATCHER_P3(MemoryDefinitionIs, Name, Value, Size, "") { |
| 72 |
if (arg.second.Value.getLimitedValue() == static_cast<uint64_t>(Value) && |
| 73 |
arg.second.SizeBytes == static_cast<size_t>(Size) && arg.first == Name) |
| 74 |
return true; |
| 75 |
*result_listener << "expected: {" << Name << ", " << Value << ", " << Size |
| 76 |
<< "} "; |
| 77 |
*result_listener << "actual: {" << arg.first << ", " |
| 78 |
<< arg.second.Value.getLimitedValue() << ", " |
| 79 |
<< arg.second.SizeBytes << "}"; |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
MATCHER_P2(MemoryMappingIs, Address, Name, "") { |
| 84 |
if (arg.Address == Address && arg.MemoryValueName == Name) |
| 85 |
return true; |
| 86 |
*result_listener << "expected: {" << Address << ", " << Name << "} "; |
| 87 |
*result_listener << "actual: {" << arg.Address << ", " << arg.MemoryValueName |
| 88 |
<< "}"; |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
TEST_F(X86SnippetFileTest, Works) { |
| 93 |
auto Snippets = TestCommon(R"( |
| 94 |
# LLVM-EXEGESIS-DEFREG RAX 0f |
| 95 |
# LLVM-EXEGESIS-DEFREG SIL 0 |
| 96 |
# LLVM-EXEGESIS-LIVEIN RDI |
| 97 |
# LLVM-EXEGESIS-LIVEIN DL |
| 98 |
incq %rax |
| 99 |
)"); |
| 100 |
EXPECT_FALSE((bool)Snippets.takeError()); |
| 101 |
ASSERT_THAT(*Snippets, SizeIs(1)); |
| 102 |
const auto &Snippet = (*Snippets)[0]; |
| 103 |
ASSERT_THAT(Snippet.Key.Instructions, ElementsAre(HasOpcode(X86::INC64r))); |
| 104 |
ASSERT_THAT(Snippet.Key.RegisterInitialValues, |
| 105 |
ElementsAre(RegisterInitialValueIs(X86::RAX, 15), |
| 106 |
RegisterInitialValueIs(X86::SIL, 0))); |
| 107 |
ASSERT_THAT(Snippet.LiveIns, ElementsAre(X86::RDI, X86::DL)); |
| 108 |
} |
| 109 |
|
| 110 |
TEST_F(X86SnippetFileTest, BadDefregParam) { |
| 111 |
auto Error = TestCommon(R"( |
| 112 |
# LLVM-EXEGESIS-DEFREG DOESNOEXIST 0 |
| 113 |
incq %rax |
| 114 |
)") |
| 115 |
.takeError(); |
| 116 |
EXPECT_TRUE((bool)Error); |
| 117 |
consumeError(std::move(Error)); |
| 118 |
} |
| 119 |
|
| 120 |
TEST_F(X86SnippetFileTest, NoDefregValue) { |
| 121 |
auto Error = TestCommon(R"( |
| 122 |
# LLVM-EXEGESIS-DEFREG RAX |
| 123 |
incq %rax |
| 124 |
)") |
| 125 |
.takeError(); |
| 126 |
EXPECT_TRUE((bool)Error); |
| 127 |
consumeError(std::move(Error)); |
| 128 |
} |
| 129 |
|
| 130 |
TEST_F(X86SnippetFileTest, MissingParam) { |
| 131 |
auto Error = TestCommon(R"( |
| 132 |
# LLVM-EXEGESIS-LIVEIN |
| 133 |
incq %rax |
| 134 |
)") |
| 135 |
.takeError(); |
| 136 |
EXPECT_TRUE((bool)Error); |
| 137 |
consumeError(std::move(Error)); |
| 138 |
} |
| 139 |
|
| 140 |
TEST_F(X86SnippetFileTest, NoAsmStreamer) { |
| 141 |
auto Snippets = TestCommon(R"( |
| 142 |
.cv_fpo_proc foo 4 |
| 143 |
)"); |
| 144 |
EXPECT_FALSE((bool)Snippets.takeError()); |
| 145 |
} |
| 146 |
|
| 147 |
TEST_F(X86SnippetFileTest, MemoryDefinitionTestSingleDef) { |
| 148 |
auto Snippets = TestCommon(R"( |
| 149 |
# LLVM-EXEGESIS-MEM-DEF test1 4096 ff |
| 150 |
# LLVM-EXEGESIS-MEM-MAP test1 65536 |
| 151 |
# LLVM-EXEGESIS-MEM-MAP test1 131072 |
| 152 |
movq $8192, %r10 |
| 153 |
movq (%r10), %r11 |
| 154 |
)"); |
| 155 |
EXPECT_FALSE((bool)Snippets.takeError()); |
| 156 |
ASSERT_THAT(*Snippets, SizeIs(1)); |
| 157 |
const auto &Snippet = (*Snippets)[0]; |
| 158 |
ASSERT_THAT(Snippet.Key.MemoryValues, |
| 159 |
UnorderedElementsAre(MemoryDefinitionIs("test1", 255, 4096))); |
| 160 |
ASSERT_THAT(Snippet.Key.MemoryMappings, |
| 161 |
ElementsAre(MemoryMappingIs(65536, "test1"), |
| 162 |
MemoryMappingIs(131072, "test1"))); |
| 163 |
} |
| 164 |
|
| 165 |
TEST_F(X86SnippetFileTest, MemoryDefinitionsTestTwoDef) { |
| 166 |
auto Snippets = TestCommon(R"( |
| 167 |
# LLVM-EXEGESIS-MEM-DEF test1 4096 ff |
| 168 |
# LLVM-EXEGESIS-MEM-DEF test2 4096 100 |
| 169 |
# LLVM-EXEGESIS-MEM-MAP test1 65536 |
| 170 |
# LLVM-EXEGESIS-MEM-MAP test2 131072 |
| 171 |
movq $8192, %r10 |
| 172 |
movq (%r10), %r11 |
| 173 |
)"); |
| 174 |
EXPECT_FALSE((bool)Snippets.takeError()); |
| 175 |
ASSERT_THAT(*Snippets, SizeIs(1)); |
| 176 |
const auto &Snippet = (*Snippets)[0]; |
| 177 |
ASSERT_THAT(Snippet.Key.MemoryValues, |
| 178 |
UnorderedElementsAre(MemoryDefinitionIs("test1", 255, 4096), |
| 179 |
MemoryDefinitionIs("test2", 256, 4096))); |
| 180 |
ASSERT_THAT(Snippet.Key.MemoryMappings, |
| 181 |
ElementsAre(MemoryMappingIs(65536, "test1"), |
| 182 |
MemoryMappingIs(131072, "test2"))); |
| 183 |
} |
| 184 |
|
| 185 |
TEST_F(X86SnippetFileTest, MemoryDefinitionMissingParameter) { |
| 186 |
auto Error = TestCommon(R"( |
| 187 |
# LLVM-EXEGESIS-MEM-DEF test1 4096 |
| 188 |
)") |
| 189 |
.takeError(); |
| 190 |
EXPECT_TRUE((bool)Error); |
| 191 |
consumeError(std::move(Error)); |
| 192 |
} |
| 193 |
|
| 194 |
TEST_F(X86SnippetFileTest, MemoryMappingMissingParameters) { |
| 195 |
auto Error = TestCommon(R"( |
| 196 |
# LLVM-EXEGESIS-MEM-MAP test1 |
| 197 |
)") |
| 198 |
.takeError(); |
| 199 |
EXPECT_TRUE((bool)Error); |
| 200 |
consumeError(std::move(Error)); |
| 201 |
} |
| 202 |
|
| 203 |
TEST_F(X86SnippetFileTest, MemoryMappingNoDefinition) { |
| 204 |
auto Error = TestCommon(R"( |
| 205 |
# LLVM-EXEGESIS-MEM-MAP test1 65536 |
| 206 |
)") |
| 207 |
.takeError(); |
| 208 |
EXPECT_TRUE((bool)Error); |
| 209 |
consumeError(std::move(Error)); |
| 210 |
} |
| 211 |
|
| 212 |
TEST_F(X86SnippetFileTest, SnippetAddress) { |
| 213 |
auto Snippets = TestCommon(R"( |
| 214 |
# LLVM-EXEGESIS-SNIPPET-ADDRESS 10000 |
| 215 |
)"); |
| 216 |
ASSERT_TRUE(static_cast<bool>(Snippets)); |
| 217 |
EXPECT_THAT(*Snippets, SizeIs(1)); |
| 218 |
const auto &Snippet = (*Snippets)[0]; |
| 219 |
EXPECT_EQ(Snippet.Key.SnippetAddress, 0x10000); |
| 220 |
} |
| 221 |
|
| 222 |
TEST_F(X86SnippetFileTest, LoopRegister) { |
| 223 |
auto Snippets = TestCommon(R"( |
| 224 |
# LLVM-EXEGESIS-LOOP-REGISTER R11 |
| 225 |
)"); |
| 226 |
ASSERT_TRUE(static_cast<bool>(Snippets)); |
| 227 |
EXPECT_THAT(*Snippets, SizeIs(1)); |
| 228 |
const auto &Snippet = (*Snippets)[0]; |
| 229 |
EXPECT_EQ(Snippet.Key.LoopRegister, X86::R11); |
| 230 |
} |
| 231 |
|
| 232 |
TEST_F(X86SnippetFileTest, LoopRegisterInvalidRegister) { |
| 233 |
auto Error = TestCommon(R"( |
| 234 |
# LLVM-EXEGESIS-LOOP-REGISTER INVALID |
| 235 |
)") |
| 236 |
.takeError(); |
| 237 |
EXPECT_TRUE(static_cast<bool>(Error)); |
| 238 |
consumeError(std::move(Error)); |
| 239 |
} |
| 240 |
|
| 241 |
} // namespace |
| 242 |
} // namespace exegesis |
| 243 |
} // namespace llvm |