[llvm] [llvm-exegesis] Use TestBase for TargetTest (PR #121895)
Aiden Grossman via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 6 23:34:51 PST 2025
https://github.com/boomanaiden154 created https://github.com/llvm/llvm-project/pull/121895
This patch makes the PPC and X86 Exegesis TargetTests use TestBase to provide initial setup rather than doing it themselves. This promotes code reuse a little bit and makes the tests a bit more consistent (with MIPS and with the initial RISC-V tests landing soon).
>From e0cb049ed4c0c33754b79156c1e0a33d6d62a0de Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Tue, 7 Jan 2025 07:32:31 +0000
Subject: [PATCH] [llvm-exegesis] Use TestBase for TargetTest
This patch makes the PPC and X86 Exegesis TargetTests use TestBase to
provide initial setup rather than doing it themselves. This promotes
code reuse a little bit and makes the tests a bit more consistent (with
MIPS and with the initial RISC-V tests landing soon).
---
.../tools/llvm-exegesis/PowerPC/TargetTest.cpp | 9 ++-------
.../tools/llvm-exegesis/X86/TargetTest.cpp | 17 +++--------------
.../tools/llvm-exegesis/X86/TestBase.h | 6 ++++--
3 files changed, 9 insertions(+), 23 deletions(-)
diff --git a/llvm/unittests/tools/llvm-exegesis/PowerPC/TargetTest.cpp b/llvm/unittests/tools/llvm-exegesis/PowerPC/TargetTest.cpp
index c3107ee4ec0eb2..da24ae8cae2a4c 100644
--- a/llvm/unittests/tools/llvm-exegesis/PowerPC/TargetTest.cpp
+++ b/llvm/unittests/tools/llvm-exegesis/PowerPC/TargetTest.cpp
@@ -12,6 +12,7 @@
#include <memory>
#include "MCTargetDesc/PPCMCTargetDesc.h"
+#include "TestBase.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "gmock/gmock.h"
@@ -30,7 +31,7 @@ using testing::Not;
constexpr const char kTriple[] = "powerpc64le-unknown-linux";
-class PowerPCTargetTest : public ::testing::Test {
+class PowerPCTargetTest : public PPCTestBase {
protected:
PowerPCTargetTest()
: ExegesisTarget_(ExegesisTarget::lookup(Triple(kTriple))) {
@@ -39,12 +40,6 @@ class PowerPCTargetTest : public ::testing::Test {
Target_ = TargetRegistry::lookupTarget(kTriple, error);
EXPECT_THAT(Target_, NotNull());
}
- static void SetUpTestCase() {
- LLVMInitializePowerPCTargetInfo();
- LLVMInitializePowerPCTarget();
- LLVMInitializePowerPCTargetMC();
- InitializePowerPCExegesisTarget();
- }
const Target *Target_;
const ExegesisTarget *const ExegesisTarget_;
diff --git a/llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp b/llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp
index 3dff50c44798d7..846729c6f85ee4 100644
--- a/llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp
+++ b/llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp
@@ -14,6 +14,7 @@
#include "MCTargetDesc/X86MCTargetDesc.h"
#include "MmapUtils.h"
#include "SubprocessMemory.h"
+#include "TestBase.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "gmock/gmock.h"
@@ -112,19 +113,9 @@ Matcher<MCInst> IsStackDeallocate(unsigned Size) {
ElementsAre(IsReg(X86::RSP), IsReg(X86::RSP), IsImm(Size)));
}
-constexpr const char kTriple[] = "x86_64-unknown-linux";
-
-class X86TargetTest : public ::testing::Test {
+class X86TargetTest : public X86TestBase {
protected:
- X86TargetTest(const char *Features)
- : State(cantFail(LLVMState::Create(kTriple, "core2", Features))) {}
-
- static void SetUpTestCase() {
- LLVMInitializeX86TargetInfo();
- LLVMInitializeX86Target();
- LLVMInitializeX86TargetMC();
- InitializeX86ExegesisTarget();
- }
+ X86TargetTest(const char *Features) : X86TestBase("core2", Features) {}
std::vector<MCInst> setRegTo(unsigned Reg, const APInt &Value) {
return State.getExegesisTarget().setRegTo(State.getSubtargetInfo(), Reg,
@@ -134,8 +125,6 @@ class X86TargetTest : public ::testing::Test {
const Instruction &getInstr(unsigned OpCode) {
return State.getIC().getInstr(OpCode);
}
-
- LLVMState State;
};
class X86Core2TargetTest : public X86TargetTest {
diff --git a/llvm/unittests/tools/llvm-exegesis/X86/TestBase.h b/llvm/unittests/tools/llvm-exegesis/X86/TestBase.h
index ea8063ee44d2c7..4122726aef94a6 100644
--- a/llvm/unittests/tools/llvm-exegesis/X86/TestBase.h
+++ b/llvm/unittests/tools/llvm-exegesis/X86/TestBase.h
@@ -22,10 +22,12 @@ namespace exegesis {
void InitializeX86ExegesisTarget();
+constexpr const char kTriple[] = "x86_64-unknown-linux";
+
class X86TestBase : public ::testing::Test {
protected:
- X86TestBase()
- : State(cantFail(LLVMState::Create("x86_64-unknown-linux", "haswell"))) {}
+ X86TestBase(std::string CPUName = "haswell", const char *Features = "")
+ : State(cantFail(LLVMState::Create(kTriple, CPUName, Features))) {}
static void SetUpTestCase() {
LLVMInitializeX86TargetInfo();
More information about the llvm-commits
mailing list