-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[RISCV][llvm-exegesis] Add unittests. NFC #121862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| add_llvm_exegesis_unittest_includes( | ||
| ${LLVM_MAIN_SRC_DIR}/lib/Target/RISCV | ||
| ${LLVM_BINARY_DIR}/lib/Target/RISCV | ||
| ${LLVM_MAIN_SRC_DIR}/tools/llvm-exegesis/lib | ||
| ) | ||
|
|
||
| add_llvm_exegesis_unittest_link_components( | ||
| MC | ||
| MCParser | ||
| Object | ||
| Support | ||
| Symbolize | ||
| RISCV | ||
| ) | ||
|
|
||
| add_llvm_exegesis_unittest_sources( | ||
| SnippetGeneratorTest.cpp | ||
| TargetTest.cpp | ||
| ) | ||
| add_llvm_exegesis_unittest_link_libraries( | ||
| LLVMExegesisRISCV) |
122 changes: 122 additions & 0 deletions
122
llvm/unittests/tools/llvm-exegesis/RISCV/SnippetGeneratorTest.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| //===-- SnippetGeneratorTest.cpp --------------------------------*- C++ -*-===// | ||
| // | ||
| // 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 "../Common/AssemblerUtils.h" | ||
| #include "LlvmState.h" | ||
| #include "MCInstrDescView.h" | ||
| #include "RISCVInstrInfo.h" | ||
| #include "ParallelSnippetGenerator.h" | ||
| #include "RegisterAliasing.h" | ||
| #include "SerialSnippetGenerator.h" | ||
| #include "TestBase.h" | ||
|
|
||
| namespace llvm { | ||
| namespace exegesis { | ||
| namespace { | ||
|
|
||
| using testing::AnyOf; | ||
| using testing::ElementsAre; | ||
| using testing::HasSubstr; | ||
| using testing::SizeIs; | ||
|
|
||
| MATCHER(IsInvalid, "") { return !arg.isValid(); } | ||
| MATCHER(IsReg, "") { return arg.isReg(); } | ||
|
|
||
| template <typename SnippetGeneratorT> | ||
| class RISCVSnippetGeneratorTest : public RISCVTestBase { | ||
| protected: | ||
| RISCVSnippetGeneratorTest() : Generator(State, SnippetGenerator::Options()) {} | ||
|
|
||
| std::vector<CodeTemplate> checkAndGetCodeTemplates(unsigned Opcode) { | ||
| randomGenerator().seed(0); // Initialize seed. | ||
| const Instruction &Instr = State.getIC().getInstr(Opcode); | ||
| auto CodeTemplateOrError = Generator.generateCodeTemplates( | ||
| &Instr, State.getRATC().emptyRegisters()); | ||
| EXPECT_FALSE(CodeTemplateOrError.takeError()); // Valid configuration. | ||
| return std::move(CodeTemplateOrError.get()); | ||
| } | ||
|
|
||
| SnippetGeneratorT Generator; | ||
| }; | ||
|
|
||
| using RISCVSerialSnippetGeneratorTest = RISCVSnippetGeneratorTest<SerialSnippetGenerator>; | ||
|
|
||
| using RISCVParallelSnippetGeneratorTest = | ||
| RISCVSnippetGeneratorTest<ParallelSnippetGenerator>; | ||
|
|
||
| TEST_F(RISCVSerialSnippetGeneratorTest, ImplicitSelfDependencyThroughExplicitRegs) { | ||
| // - ADD | ||
| // - Op0 Explicit Def RegClass(GPR) | ||
| // - Op1 Explicit Use RegClass(GPR) | ||
| // - Op2 Explicit Use RegClass(GPR) | ||
| // - Var0 [Op0] | ||
| // - Var1 [Op1] | ||
| // - Var2 [Op2] | ||
| // - hasAliasingRegisters | ||
| const unsigned Opcode = RISCV::ADD; | ||
| const auto CodeTemplates = checkAndGetCodeTemplates(Opcode); | ||
| ASSERT_THAT(CodeTemplates, SizeIs(1)); | ||
| const auto &CT = CodeTemplates[0]; | ||
| EXPECT_THAT(CT.Execution, ExecutionMode::SERIAL_VIA_EXPLICIT_REGS); | ||
| ASSERT_THAT(CT.Instructions, SizeIs(1)); | ||
| const InstructionTemplate &IT = CT.Instructions[0]; | ||
| EXPECT_THAT(IT.getOpcode(), Opcode); | ||
| ASSERT_THAT(IT.getVariableValues(), SizeIs(3)); | ||
| EXPECT_THAT(IT.getVariableValues(), | ||
| AnyOf(ElementsAre(IsReg(), IsInvalid(), IsReg()), | ||
| ElementsAre(IsReg(), IsReg(), IsInvalid()))) | ||
| << "Op0 is either set to Op1 or to Op2"; | ||
| } | ||
|
|
||
| TEST_F(RISCVSerialSnippetGeneratorTest, | ||
| ImplicitSelfDependencyThroughExplicitRegsForbidAll) { | ||
| // - XOR | ||
| // - Op0 Explicit Def RegClass(GPR) | ||
| // - Op1 Explicit Use RegClass(GPR) | ||
| // - Op2 Explicit Use RegClass(GPR) | ||
| // - Var0 [Op0] | ||
| // - Var1 [Op1] | ||
| // - Var2 [Op2] | ||
| // - hasAliasingRegisters | ||
| randomGenerator().seed(0); // Initialize seed. | ||
| const Instruction &Instr = State.getIC().getInstr(RISCV::XOR); | ||
| auto AllRegisters = State.getRATC().emptyRegisters(); | ||
| AllRegisters.flip(); | ||
| auto Error = | ||
| Generator.generateCodeTemplates(&Instr, AllRegisters).takeError(); | ||
| EXPECT_TRUE((bool)Error); | ||
| consumeError(std::move(Error)); | ||
| } | ||
|
|
||
| TEST_F(RISCVParallelSnippetGeneratorTest, MemoryUse) { | ||
| // LB reads from memory. | ||
| // - LB | ||
| // - Op0 Explicit Def RegClass(GPR) | ||
| // - Op1 Explicit Use Memory RegClass(GPR) | ||
| // - Op2 Explicit Use Memory | ||
| // - Var0 [Op0] | ||
| // - Var1 [Op1] | ||
| // - Var2 [Op2] | ||
| // - hasMemoryOperands | ||
| const unsigned Opcode = RISCV::LB; | ||
| const auto CodeTemplates = checkAndGetCodeTemplates(Opcode); | ||
| ASSERT_THAT(CodeTemplates, SizeIs(1)); | ||
| const auto &CT = CodeTemplates[0]; | ||
| EXPECT_THAT(CT.Info, HasSubstr("instruction has no tied variables")); | ||
| EXPECT_THAT(CT.Execution, ExecutionMode::UNKNOWN); | ||
| ASSERT_THAT(CT.Instructions, | ||
| SizeIs(ParallelSnippetGenerator::kMinNumDifferentAddresses)); | ||
| const InstructionTemplate &IT = CT.Instructions[0]; | ||
| EXPECT_THAT(IT.getOpcode(), Opcode); | ||
| ASSERT_THAT(IT.getVariableValues(), SizeIs(3)); | ||
| EXPECT_EQ(IT.getVariableValues()[1].getReg(), RISCV::X10); | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace exegesis | ||
| } // namespace llvm | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| //===-- TargetTest.cpp ---------------------------------------*- C++ -*-===// | ||
| // | ||
| // 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 "Target.h" | ||
|
|
||
| #include <cassert> | ||
| #include <memory> | ||
|
|
||
| #include "MCTargetDesc/RISCVMCTargetDesc.h" | ||
| #include "llvm/MC/TargetRegistry.h" | ||
| #include "llvm/Support/TargetSelect.h" | ||
| #include "gmock/gmock.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace llvm{ | ||
| namespace exegesis { | ||
|
|
||
| void InitializeRISCVExegesisTarget(); | ||
|
|
||
| namespace { | ||
|
|
||
| using testing::NotNull; | ||
| using testing::IsEmpty; | ||
| using testing::Not; | ||
|
|
||
| constexpr const char kTriple[] = "riscv64-unknown-linux"; | ||
|
|
||
| class RISCVTargetTest : public ::testing::Test { | ||
boomanaiden154 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| protected: | ||
| RISCVTargetTest() | ||
| : ExegesisTarget_(ExegesisTarget::lookup(Triple(kTriple))) { | ||
| EXPECT_THAT(ExegesisTarget_, NotNull()); | ||
| std::string error; | ||
| Target_ = TargetRegistry::lookupTarget(kTriple, error); | ||
| EXPECT_THAT(Target_, NotNull()); | ||
| } | ||
| static void SetUpTestCase() { | ||
| LLVMInitializeRISCVTargetInfo(); | ||
| LLVMInitializeRISCVTarget(); | ||
| LLVMInitializeRISCVTargetMC(); | ||
| InitializeRISCVExegesisTarget(); | ||
| } | ||
|
|
||
| const Target *Target_; | ||
| const ExegesisTarget *const ExegesisTarget_; | ||
| }; | ||
|
|
||
| TEST_F(RISCVTargetTest, SetRegToConstant) { | ||
| const std::unique_ptr<MCSubtargetInfo> STI( | ||
| Target_->createMCSubtargetInfo(kTriple, "generic", "")); | ||
| const auto Insts = ExegesisTarget_->setRegTo(*STI, RISCV::X10, APInt()); | ||
| EXPECT_THAT(Insts, Not(IsEmpty())); | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace exegesis | ||
| } // namespace llvm | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| //===-- TestBase.h ----------------------------------------------*- C++ -*-===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // Test fixture common to all RISC-V tests. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_UNITTESTS_TOOLS_LLVMEXEGESIS_RISCV_TESTBASE_H | ||
| #define LLVM_UNITTESTS_TOOLS_LLVMEXEGESIS_RISCV_TESTBASE_H | ||
|
|
||
| #include "LlvmState.h" | ||
| #include "llvm/MC/TargetRegistry.h" | ||
| #include "llvm/Support/TargetSelect.h" | ||
| #include "gmock/gmock.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace llvm { | ||
| namespace exegesis { | ||
|
|
||
| void InitializeRISCVExegesisTarget(); | ||
|
|
||
| class RISCVTestBase : public ::testing::Test { | ||
| protected: | ||
| RISCVTestBase() | ||
| : State(cantFail(LLVMState::Create("riscv64-unknown-linux", "generic-rv64"))) {} | ||
|
|
||
| static void SetUpTestCase() { | ||
| LLVMInitializeRISCVTargetInfo(); | ||
| LLVMInitializeRISCVTargetMC(); | ||
| LLVMInitializeRISCVTarget(); | ||
| InitializeRISCVExegesisTarget(); | ||
| } | ||
|
|
||
| const LLVMState State; | ||
| }; | ||
|
|
||
| } // namespace exegesis | ||
| } // namespace llvm | ||
|
|
||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.