Skip to content

Commit c96caf4

Browse files
committed
[RISCV][llvm-exegesis] Add unittests. NFC
This is largely a copy paste from Mips or PowerPC.
1 parent 9709795 commit c96caf4

File tree

5 files changed

+251
-0
lines changed

5 files changed

+251
-0
lines changed

llvm/unittests/tools/llvm-exegesis/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ endif()
5353
if(LLVM_TARGETS_TO_BUILD MATCHES "Mips")
5454
include(Mips/CMakeLists.txt)
5555
endif()
56+
if(LLVM_TARGETS_TO_BUILD MATCHES "RISCV")
57+
include(RISCV/CMakeLists.txt)
58+
endif()
5659

5760
include_directories(${exegesis_includes})
5861

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
add_llvm_exegesis_unittest_includes(
2+
${LLVM_MAIN_SRC_DIR}/lib/Target/RISCV
3+
${LLVM_BINARY_DIR}/lib/Target/RISCV
4+
${LLVM_MAIN_SRC_DIR}/tools/llvm-exegesis/lib
5+
)
6+
7+
add_llvm_exegesis_unittest_link_components(
8+
MC
9+
MCParser
10+
Object
11+
Support
12+
Symbolize
13+
RISCV
14+
)
15+
16+
add_llvm_exegesis_unittest_sources(
17+
SnippetGeneratorTest.cpp
18+
TargetTest.cpp
19+
)
20+
add_llvm_exegesis_unittest_link_libraries(
21+
LLVMExegesisRISCV)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//===-- SnippetGeneratorTest.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 "../Common/AssemblerUtils.h"
10+
#include "LlvmState.h"
11+
#include "MCInstrDescView.h"
12+
#include "RISCVInstrInfo.h"
13+
#include "ParallelSnippetGenerator.h"
14+
#include "RegisterAliasing.h"
15+
#include "SerialSnippetGenerator.h"
16+
#include "TestBase.h"
17+
18+
namespace llvm {
19+
namespace exegesis {
20+
namespace {
21+
22+
using testing::AnyOf;
23+
using testing::ElementsAre;
24+
using testing::HasSubstr;
25+
using testing::SizeIs;
26+
27+
MATCHER(IsInvalid, "") { return !arg.isValid(); }
28+
MATCHER(IsReg, "") { return arg.isReg(); }
29+
30+
template <typename SnippetGeneratorT>
31+
class RISCVSnippetGeneratorTest : public RISCVTestBase {
32+
protected:
33+
RISCVSnippetGeneratorTest() : Generator(State, SnippetGenerator::Options()) {}
34+
35+
std::vector<CodeTemplate> checkAndGetCodeTemplates(unsigned Opcode) {
36+
randomGenerator().seed(0); // Initialize seed.
37+
const Instruction &Instr = State.getIC().getInstr(Opcode);
38+
auto CodeTemplateOrError = Generator.generateCodeTemplates(
39+
&Instr, State.getRATC().emptyRegisters());
40+
EXPECT_FALSE(CodeTemplateOrError.takeError()); // Valid configuration.
41+
return std::move(CodeTemplateOrError.get());
42+
}
43+
44+
SnippetGeneratorT Generator;
45+
};
46+
47+
using RISCVSerialSnippetGeneratorTest = RISCVSnippetGeneratorTest<SerialSnippetGenerator>;
48+
49+
using RISCVParallelSnippetGeneratorTest =
50+
RISCVSnippetGeneratorTest<ParallelSnippetGenerator>;
51+
52+
TEST_F(RISCVSerialSnippetGeneratorTest, ImplicitSelfDependencyThroughExplicitRegs) {
53+
// - ADD
54+
// - Op0 Explicit Def RegClass(GPR)
55+
// - Op1 Explicit Use RegClass(GPR)
56+
// - Op2 Explicit Use RegClass(GPR)
57+
// - Var0 [Op0]
58+
// - Var1 [Op1]
59+
// - Var2 [Op2]
60+
// - hasAliasingRegisters
61+
const unsigned Opcode = RISCV::ADD;
62+
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
63+
ASSERT_THAT(CodeTemplates, SizeIs(1));
64+
const auto &CT = CodeTemplates[0];
65+
EXPECT_THAT(CT.Execution, ExecutionMode::SERIAL_VIA_EXPLICIT_REGS);
66+
ASSERT_THAT(CT.Instructions, SizeIs(1));
67+
const InstructionTemplate &IT = CT.Instructions[0];
68+
EXPECT_THAT(IT.getOpcode(), Opcode);
69+
ASSERT_THAT(IT.getVariableValues(), SizeIs(3));
70+
EXPECT_THAT(IT.getVariableValues(),
71+
AnyOf(ElementsAre(IsReg(), IsInvalid(), IsReg()),
72+
ElementsAre(IsReg(), IsReg(), IsInvalid())))
73+
<< "Op0 is either set to Op1 or to Op2";
74+
}
75+
76+
TEST_F(RISCVSerialSnippetGeneratorTest,
77+
ImplicitSelfDependencyThroughExplicitRegsForbidAll) {
78+
// - XOR
79+
// - Op0 Explicit Def RegClass(GPR)
80+
// - Op1 Explicit Use RegClass(GPR)
81+
// - Op2 Explicit Use RegClass(GPR)
82+
// - Var0 [Op0]
83+
// - Var1 [Op1]
84+
// - Var2 [Op2]
85+
// - hasAliasingRegisters
86+
randomGenerator().seed(0); // Initialize seed.
87+
const Instruction &Instr = State.getIC().getInstr(RISCV::XOR);
88+
auto AllRegisters = State.getRATC().emptyRegisters();
89+
AllRegisters.flip();
90+
auto Error =
91+
Generator.generateCodeTemplates(&Instr, AllRegisters).takeError();
92+
EXPECT_TRUE((bool)Error);
93+
consumeError(std::move(Error));
94+
}
95+
96+
TEST_F(RISCVParallelSnippetGeneratorTest, MemoryUse) {
97+
// LB reads from memory.
98+
// - LB
99+
// - Op0 Explicit Def RegClass(GPR)
100+
// - Op1 Explicit Use Memory RegClass(GPR)
101+
// - Op2 Explicit Use Memory
102+
// - Var0 [Op0]
103+
// - Var1 [Op1]
104+
// - Var2 [Op2]
105+
// - hasMemoryOperands
106+
const unsigned Opcode = RISCV::LB;
107+
const auto CodeTemplates = checkAndGetCodeTemplates(Opcode);
108+
ASSERT_THAT(CodeTemplates, SizeIs(1));
109+
const auto &CT = CodeTemplates[0];
110+
EXPECT_THAT(CT.Info, HasSubstr("instruction has no tied variables"));
111+
EXPECT_THAT(CT.Execution, ExecutionMode::UNKNOWN);
112+
ASSERT_THAT(CT.Instructions,
113+
SizeIs(ParallelSnippetGenerator::kMinNumDifferentAddresses));
114+
const InstructionTemplate &IT = CT.Instructions[0];
115+
EXPECT_THAT(IT.getOpcode(), Opcode);
116+
ASSERT_THAT(IT.getVariableValues(), SizeIs(3));
117+
EXPECT_EQ(IT.getVariableValues()[1].getReg(), RISCV::X10);
118+
}
119+
120+
} // namespace
121+
} // namespace exegesis
122+
} // namespace llvm
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===-- TargetTest.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 "Target.h"
10+
11+
#include <cassert>
12+
#include <memory>
13+
14+
#include "MCTargetDesc/RISCVMCTargetDesc.h"
15+
#include "llvm/MC/TargetRegistry.h"
16+
#include "llvm/Support/TargetSelect.h"
17+
#include "gmock/gmock.h"
18+
#include "gtest/gtest.h"
19+
20+
namespace llvm{
21+
namespace exegesis {
22+
23+
void InitializeRISCVExegesisTarget();
24+
25+
namespace {
26+
27+
using testing::NotNull;
28+
using testing::IsEmpty;
29+
using testing::Not;
30+
31+
constexpr const char kTriple[] = "riscv64-unknown-linux";
32+
33+
class RISCVTargetTest : public ::testing::Test {
34+
protected:
35+
RISCVTargetTest()
36+
: ExegesisTarget_(ExegesisTarget::lookup(Triple(kTriple))) {
37+
EXPECT_THAT(ExegesisTarget_, NotNull());
38+
std::string error;
39+
Target_ = TargetRegistry::lookupTarget(kTriple, error);
40+
EXPECT_THAT(Target_, NotNull());
41+
}
42+
static void SetUpTestCase() {
43+
LLVMInitializeRISCVTargetInfo();
44+
LLVMInitializeRISCVTarget();
45+
LLVMInitializeRISCVTargetMC();
46+
InitializeRISCVExegesisTarget();
47+
}
48+
49+
const Target *Target_;
50+
const ExegesisTarget *const ExegesisTarget_;
51+
};
52+
53+
TEST_F(RISCVTargetTest, SetRegToConstant) {
54+
const std::unique_ptr<MCSubtargetInfo> STI(
55+
Target_->createMCSubtargetInfo(kTriple, "generic", ""));
56+
const auto Insts = ExegesisTarget_->setRegTo(*STI, RISCV::X10, APInt());
57+
EXPECT_THAT(Insts, Not(IsEmpty()));
58+
}
59+
60+
} // namespace
61+
} // namespace exegesis
62+
} // namespace llvm
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===-- TestBase.h ----------------------------------------------*- 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+
// Test fixture common to all RISC-V tests.
9+
//===----------------------------------------------------------------------===//
10+
11+
#ifndef LLVM_UNITTESTS_TOOLS_LLVMEXEGESIS_RISCV_TESTBASE_H
12+
#define LLVM_UNITTESTS_TOOLS_LLVMEXEGESIS_RISCV_TESTBASE_H
13+
14+
#include "LlvmState.h"
15+
#include "llvm/MC/TargetRegistry.h"
16+
#include "llvm/Support/TargetSelect.h"
17+
#include "gmock/gmock.h"
18+
#include "gtest/gtest.h"
19+
20+
namespace llvm {
21+
namespace exegesis {
22+
23+
void InitializeRISCVExegesisTarget();
24+
25+
class RISCVTestBase : public ::testing::Test {
26+
protected:
27+
RISCVTestBase()
28+
: State(cantFail(LLVMState::Create("riscv64-unknown-linux", "generic-rv64"))) {}
29+
30+
static void SetUpTestCase() {
31+
LLVMInitializeRISCVTargetInfo();
32+
LLVMInitializeRISCVTargetMC();
33+
LLVMInitializeRISCVTarget();
34+
InitializeRISCVExegesisTarget();
35+
}
36+
37+
const LLVMState State;
38+
};
39+
40+
} // namespace exegesis
41+
} // namespace llvm
42+
43+
#endif

0 commit comments

Comments
 (0)