| 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::AllOf; |
| 32 |
using testing::ElementsAre; |
| 33 |
using testing::Eq; |
| 34 |
using testing::Field; |
| 35 |
using testing::Property; |
| 36 |
using testing::SizeIs; |
| 37 |
|
| 38 |
using llvm::unittest::TempDir; |
| 39 |
|
| 40 |
class X86SnippetFileTest : public X86TestBase { |
| 41 |
protected: |
| 42 |
Expected<std::vector<BenchmarkCode>> TestCommon(StringRef Contents) { |
| 43 |
TempDir TestDirectory("SnippetFileTestDir", /*Unique*/ true); |
| 44 |
SmallString<64> Filename(TestDirectory.path()); |
| 45 |
sys::path::append(Filename, "snippet.s"); |
| 46 |
errs() << Filename << "-------\n"; |
| 47 |
{ |
| 48 |
std::error_code EC; |
| 49 |
raw_fd_ostream FOS(Filename, EC); |
| 50 |
FOS << Contents; |
| 51 |
EXPECT_FALSE(EC); |
| 52 |
} |
| 53 |
return readSnippets(State, Filename); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
// FIXME: Refactor these to ../Common/Matchers.h |
| 58 |
static auto HasOpcode = [](unsigned Opcode) { |
| 59 |
return Property(&MCInst::getOpcode, Eq(Opcode)); |
| 60 |
}; |
| 61 |
|
| 62 |
MATCHER_P2(RegisterInitialValueIs, Reg, Val, "") { |
| 63 |
if (arg.Register == Reg && |
| 64 |
arg.Value.getLimitedValue() == static_cast<uint64_t>(Val)) |
| 65 |
return true; |
| 66 |
*result_listener << "expected: {" << Reg << ", " << Val << "} "; |
| 67 |
*result_listener << "actual: {" << arg.Register << ", " |
| 68 |
<< arg.Value.getLimitedValue() << "}"; |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
TEST_F(X86SnippetFileTest, Works) { |
| 73 |
auto Snippets = TestCommon(R"( |
| 74 |
# LLVM-EXEGESIS-DEFREG RAX 0f |
| 75 |
# LLVM-EXEGESIS-DEFREG SIL 0 |
| 76 |
# LLVM-EXEGESIS-LIVEIN RDI |
| 77 |
# LLVM-EXEGESIS-LIVEIN DL |
| 78 |
incq %rax |
| 79 |
)"); |
| 80 |
EXPECT_FALSE((bool)Snippets.takeError()); |
| 81 |
ASSERT_THAT(*Snippets, SizeIs(1)); |
| 82 |
const auto &Snippet = (*Snippets)[0]; |
| 83 |
ASSERT_THAT(Snippet.Key.Instructions, ElementsAre(HasOpcode(X86::INC64r))); |
| 84 |
ASSERT_THAT(Snippet.Key.RegisterInitialValues, |
| 85 |
ElementsAre(RegisterInitialValueIs(X86::RAX, 15), |
| 86 |
RegisterInitialValueIs(X86::SIL, 0))); |
| 87 |
ASSERT_THAT(Snippet.LiveIns, ElementsAre(X86::RDI, X86::DL)); |
| 88 |
} |
| 89 |
|
| 90 |
TEST_F(X86SnippetFileTest, BadDefregParam) { |
| 91 |
auto Error = TestCommon(R"( |
| 92 |
# LLVM-EXEGESIS-DEFREG DOESNOEXIST 0 |
| 93 |
incq %rax |
| 94 |
)") |
| 95 |
.takeError(); |
| 96 |
EXPECT_TRUE((bool)Error); |
| 97 |
consumeError(std::move(Error)); |
| 98 |
} |
| 99 |
|
| 100 |
TEST_F(X86SnippetFileTest, NoDefregValue) { |
| 101 |
auto Error = TestCommon(R"( |
| 102 |
# LLVM-EXEGESIS-DEFREG RAX |
| 103 |
incq %rax |
| 104 |
)") |
| 105 |
.takeError(); |
| 106 |
EXPECT_TRUE((bool)Error); |
| 107 |
consumeError(std::move(Error)); |
| 108 |
} |
| 109 |
|
| 110 |
TEST_F(X86SnippetFileTest, MissingParam) { |
| 111 |
auto Error = TestCommon(R"( |
| 112 |
# LLVM-EXEGESIS-LIVEIN |
| 113 |
incq %rax |
| 114 |
)") |
| 115 |
.takeError(); |
| 116 |
EXPECT_TRUE((bool)Error); |
| 117 |
consumeError(std::move(Error)); |
| 118 |
} |
| 119 |
|
| 120 |
TEST_F(X86SnippetFileTest, NoAsmStreamer) { |
| 121 |
auto Snippets = TestCommon(R"( |
| 122 |
.cv_fpo_proc foo 4 |
| 123 |
)"); |
| 124 |
EXPECT_FALSE((bool)Snippets.takeError()); |
| 125 |
} |
| 126 |
|
| 127 |
} // namespace |
| 128 |
} // namespace exegesis |
| 129 |
} // namespace llvm |