From 31458a9feecb636e64a360d87081f836f2920ccb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Milo=C5=A1=20Stojanovi=C4=87?= Date: Mon, 30 Dec 2019 15:50:51 +0100 Subject: [PATCH] [llvm-exegesis][NFC] Refactor Mips tests fixtures into a base class. Differential Revision: https://reviews.llvm.org/D72003 --- .../llvm-exegesis/Mips/BenchmarkResultTest.cpp | 15 +++----- .../llvm-exegesis/Mips/SnippetGeneratorTest.cpp | 22 ++---------- .../tools/llvm-exegesis/Mips/TargetTest.cpp | 19 ++-------- llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h | 42 ++++++++++++++++++++++ 4 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h diff --git a/llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp b/llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp index 650401b..fcbb770 100644 --- a/llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp +++ b/llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp @@ -8,6 +8,7 @@ #include "BenchmarkResult.h" #include "MipsInstrInfo.h" +#include "TestBase.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/Error.h" #include "llvm/Support/Path.h" @@ -27,8 +28,6 @@ using ::testing::Property; namespace llvm { namespace exegesis { -void InitializeMipsExegesisTarget(); - bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B) { return std::tie(A.Key, A.PerInstructionValue, A.PerSnippetValue) == std::tie(B.Key, B.PerInstructionValue, B.PerSnippetValue); @@ -53,15 +52,9 @@ MATCHER(EqMCInst, "") { namespace { -TEST(BenchmarkResultTest, WriteToAndReadFromDisk) { - LLVMInitializeMipsTargetInfo(); - LLVMInitializeMipsTarget(); - LLVMInitializeMipsTargetMC(); - InitializeMipsExegesisTarget(); - - // Read benchmarks. - const LLVMState State("mips-unknown-linux", "mips32"); +class BenchmarkResultTest : public MipsTestBase {}; +TEST_F(BenchmarkResultTest, WriteToAndReadFromDisk) { ExitOnError ExitOnErr; InstructionBenchmark ToDisk; @@ -126,7 +119,7 @@ TEST(BenchmarkResultTest, WriteToAndReadFromDisk) { } } -TEST(BenchmarkResultTest, PerInstructionStats) { +TEST_F(BenchmarkResultTest, PerInstructionStats) { PerInstructionStats Stats; Stats.push(BenchmarkMeasure{"a", 0.5, 0.0}); Stats.push(BenchmarkMeasure{"a", 1.5, 0.0}); diff --git a/llvm/unittests/tools/llvm-exegesis/Mips/SnippetGeneratorTest.cpp b/llvm/unittests/tools/llvm-exegesis/Mips/SnippetGeneratorTest.cpp index b9280ab..90f4546 100644 --- a/llvm/unittests/tools/llvm-exegesis/Mips/SnippetGeneratorTest.cpp +++ b/llvm/unittests/tools/llvm-exegesis/Mips/SnippetGeneratorTest.cpp @@ -10,16 +10,14 @@ #include "Latency.h" #include "LlvmState.h" #include "MCInstrDescView.h" -#include "RegisterAliasing.h" #include "MipsInstrInfo.h" +#include "RegisterAliasing.h" +#include "TestBase.h" #include namespace llvm { namespace exegesis { - -void InitializeMipsExegesisTarget(); - namespace { using testing::AnyOf; @@ -29,21 +27,7 @@ using testing::SizeIs; MATCHER(IsInvalid, "") { return !arg.isValid(); } MATCHER(IsReg, "") { return arg.isReg(); } -class MipsSnippetGeneratorTest : public ::testing::Test { -protected: - MipsSnippetGeneratorTest() : State("mips-unknown-linux", "mips32"), - InstrInfo(State.getInstrInfo()) {} - - static void SetUpTestCase() { - LLVMInitializeMipsTargetInfo(); - LLVMInitializeMipsTarget(); - LLVMInitializeMipsTargetMC(); - InitializeMipsExegesisTarget(); - } - - LLVMState State; - const MCInstrInfo &InstrInfo; -}; +class MipsSnippetGeneratorTest : public MipsTestBase {}; template class SnippetGeneratorTest : public MipsSnippetGeneratorTest { diff --git a/llvm/unittests/tools/llvm-exegesis/Mips/TargetTest.cpp b/llvm/unittests/tools/llvm-exegesis/Mips/TargetTest.cpp index 2509beb..9ee0f1e 100644 --- a/llvm/unittests/tools/llvm-exegesis/Mips/TargetTest.cpp +++ b/llvm/unittests/tools/llvm-exegesis/Mips/TargetTest.cpp @@ -12,6 +12,7 @@ #include #include "MCTargetDesc/MipsMCTargetDesc.h" +#include "TestBase.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" #include "gmock/gmock.h" @@ -19,9 +20,6 @@ namespace llvm { namespace exegesis { - -void InitializeMipsExegesisTarget(); - namespace { using testing::AllOf; @@ -62,25 +60,12 @@ Matcher IsShift(unsigned Reg, uint16_t Amount, bool IsGPR32) { ElementsAre(IsReg(Reg), IsReg(Reg), IsImm(Amount))); } -constexpr const char kTriple[] = "mips-unknown-linux"; - -class MipsTargetTest : public ::testing::Test { +class MipsTargetTest : public MipsTestBase { protected: - MipsTargetTest() : State(kTriple, "mips32", "") {} - - static void SetUpTestCase() { - LLVMInitializeMipsTargetInfo(); - LLVMInitializeMipsTarget(); - LLVMInitializeMipsTargetMC(); - InitializeMipsExegesisTarget(); - } - std::vector setRegTo(unsigned Reg, const APInt &Value) { return State.getExegesisTarget().setRegTo(State.getSubtargetInfo(), Reg, Value); } - - LLVMState State; }; TEST_F(MipsTargetTest, SetGPR32RegTo16BitValue) { diff --git a/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h b/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h new file mode 100644 index 0000000..b9c1efa --- /dev/null +++ b/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h @@ -0,0 +1,42 @@ +//===-- 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 Mips tests. +//===----------------------------------------------------------------------===// + +#ifndef LLVM_UNITTESTS_TOOLS_LLVMEXEGESIS_MIPS_TESTBASE_H +#define LLVM_UNITTESTS_TOOLS_LLVMEXEGESIS_MIPS_TESTBASE_H + +#include "LlvmState.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace llvm { +namespace exegesis { + +void InitializeMipsExegesisTarget(); + +class MipsTestBase : public ::testing::Test { +protected: + MipsTestBase() : State("mips-unknown-linux", "mips32") {} + + static void SetUpTestCase() { + LLVMInitializeMipsTargetInfo(); + LLVMInitializeMipsTargetMC(); + LLVMInitializeMipsTarget(); + InitializeMipsExegesisTarget(); + } + + const LLVMState State; +}; + +} // namespace exegesis +} // namespace llvm + +#endif -- 2.7.4