[llvm] 7053e86 - [llvm-exegesis][NFC] Use factory function for LlvmState.
Clement Courbet via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 12 05:20:17 PDT 2022
Author: Clement Courbet
Date: 2022-09-12T14:19:33+02:00
New Revision: 7053e863a159c35b57efab1aba844231077c526e
URL: https://github.com/llvm/llvm-project/commit/7053e863a159c35b57efab1aba844231077c526e
DIFF: https://github.com/llvm/llvm-project/commit/7053e863a159c35b57efab1aba844231077c526e.diff
LOG: [llvm-exegesis][NFC] Use factory function for LlvmState.
This allows failing more gracefully.
Added:
Modified:
llvm/tools/llvm-exegesis/lib/LlvmState.cpp
llvm/tools/llvm-exegesis/lib/LlvmState.h
llvm/tools/llvm-exegesis/llvm-exegesis.cpp
llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h
llvm/unittests/tools/llvm-exegesis/PowerPC/TestBase.h
llvm/unittests/tools/llvm-exegesis/X86/BenchmarkResultTest.cpp
llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp
llvm/unittests/tools/llvm-exegesis/X86/TestBase.h
Removed:
################################################################################
diff --git a/llvm/tools/llvm-exegesis/lib/LlvmState.cpp b/llvm/tools/llvm-exegesis/lib/LlvmState.cpp
index 26575ccb54959..7a4770d86c410 100644
--- a/llvm/tools/llvm-exegesis/lib/LlvmState.cpp
+++ b/llvm/tools/llvm-exegesis/lib/LlvmState.cpp
@@ -22,21 +22,41 @@
namespace llvm {
namespace exegesis {
-LLVMState::LLVMState(const std::string &Triple, const std::string &CpuName,
- const std::string &Features) {
+Expected<LLVMState> LLVMState::Create(std::string Triple, std::string CpuName,
+ const StringRef Features) {
+ if (Triple.empty())
+ Triple = sys::getProcessTriple();
+ if (CpuName.empty())
+ CpuName = sys::getHostCPUName().str();
std::string Error;
const Target *const TheTarget = TargetRegistry::lookupTarget(Triple, Error);
- assert(TheTarget && "unknown target for host");
+ if (!TheTarget) {
+ return llvm::make_error<llvm::StringError>(
+ "no LLVM target for triple " + Triple, llvm::inconvertibleErrorCode());
+ }
const TargetOptions Options;
- TheTargetMachine.reset(
+ std::unique_ptr<const TargetMachine> TM(
static_cast<LLVMTargetMachine *>(TheTarget->createTargetMachine(
Triple, CpuName, Features, Options, Reloc::Model::Static)));
- assert(TheTargetMachine && "unable to create target machine");
- TheExegesisTarget = ExegesisTarget::lookup(TheTargetMachine->getTargetTriple());
- if (!TheExegesisTarget) {
- errs() << "no exegesis target for " << Triple << ", using default\n";
- TheExegesisTarget = &ExegesisTarget::getDefault();
+ if (!TM) {
+ return llvm::make_error<llvm::StringError>(
+ "unable to create target machine", llvm::inconvertibleErrorCode());
+ }
+
+ const ExegesisTarget *ET =
+ Triple.empty() ? &ExegesisTarget::getDefault()
+ : ExegesisTarget::lookup(TM->getTargetTriple());
+ if (!ET) {
+ return llvm::make_error<llvm::StringError>(
+ "no Exegesis target for triple " + Triple,
+ llvm::inconvertibleErrorCode());
}
+ return LLVMState(std::move(TM), ET, CpuName);
+}
+
+LLVMState::LLVMState(std::unique_ptr<const TargetMachine> TM,
+ const ExegesisTarget *ET, const StringRef CpuName)
+ : TheExegesisTarget(ET), TheTargetMachine(std::move(TM)) {
PfmCounters = &TheExegesisTarget->getPfmCounters(CpuName);
BitVector ReservedRegs = getFunctionReservedRegs(getTargetMachine());
@@ -47,10 +67,6 @@ LLVMState::LLVMState(const std::string &Triple, const std::string &CpuName,
IC.reset(new InstructionsCache(getInstrInfo(), getRATC()));
}
-LLVMState::LLVMState(const std::string &CpuName)
- : LLVMState(sys::getProcessTriple(),
- CpuName.empty() ? sys::getHostCPUName().str() : CpuName, "") {}
-
std::unique_ptr<LLVMTargetMachine> LLVMState::createTargetMachine() const {
return std::unique_ptr<LLVMTargetMachine>(static_cast<LLVMTargetMachine *>(
TheTargetMachine->getTarget().createTargetMachine(
diff --git a/llvm/tools/llvm-exegesis/lib/LlvmState.h b/llvm/tools/llvm-exegesis/lib/LlvmState.h
index e660a9f56b4c1..55247fe30e7b8 100644
--- a/llvm/tools/llvm-exegesis/lib/LlvmState.h
+++ b/llvm/tools/llvm-exegesis/lib/LlvmState.h
@@ -35,12 +35,12 @@ struct PfmCountersInfo;
// measurements.
class LLVMState {
public:
- // Uses the host triple. If CpuName is empty, uses the host CPU.
- LLVMState(const std::string &CpuName);
-
- LLVMState(const std::string &Triple,
- const std::string &CpuName,
- const std::string &Features = ""); // For tests.
+ // Factory function.
+ // If `Triple` is empty, uses the host triple.
+ // If `CpuName` is empty, uses the host CPU.
+ // `Features` is intended for tests.
+ static Expected<LLVMState> Create(std::string Triple, std::string CpuName,
+ StringRef Features = "");
const TargetMachine &getTargetMachine() const { return *TheTargetMachine; }
std::unique_ptr<LLVMTargetMachine> createTargetMachine() const;
@@ -66,6 +66,9 @@ class LLVMState {
const PfmCountersInfo &getPfmCounters() const { return *PfmCounters; }
private:
+ LLVMState(std::unique_ptr<const TargetMachine> TM, const ExegesisTarget *ET,
+ StringRef CpuName);
+
const ExegesisTarget *TheExegesisTarget;
std::unique_ptr<const TargetMachine> TheTargetMachine;
std::unique_ptr<const RegisterAliasingTrackerCache> RATC;
diff --git a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
index dde1e5c9ada86..4a17280fc6d55 100644
--- a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
+++ b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
@@ -304,7 +304,7 @@ void benchmarkMain() {
InitializeAllAsmParsers();
InitializeNativeExegesisTarget();
- const LLVMState State(CpuName);
+ const LLVMState State = ExitOnErr(LLVMState::Create("", CpuName));
// Preliminary check to ensure features needed for requested
// benchmark mode are present on target CPU and/or OS.
@@ -414,9 +414,10 @@ static void analysisMain() {
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetDisassembler();
+ InitializeNativeExegesisTarget();
// Read benchmarks.
- const LLVMState State("");
+ const LLVMState State = ExitOnErr(LLVMState::Create("", ""));
const std::vector<InstructionBenchmark> Points = ExitOnFileError(
BenchmarkFile, InstructionBenchmark::readYamls(State, BenchmarkFile));
diff --git a/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h b/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h
index a68843e341c25..d92b8494b551a 100644
--- a/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h
+++ b/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h
@@ -24,7 +24,8 @@ void InitializeMipsExegesisTarget();
class MipsTestBase : public ::testing::Test {
protected:
- MipsTestBase() : State("mips-unknown-linux", "mips32") {}
+ MipsTestBase()
+ : State(cantFail(LLVMState::Create("mips-unknown-linux", "mips32"))) {}
static void SetUpTestCase() {
LLVMInitializeMipsTargetInfo();
diff --git a/llvm/unittests/tools/llvm-exegesis/PowerPC/TestBase.h b/llvm/unittests/tools/llvm-exegesis/PowerPC/TestBase.h
index e1efe2c467730..97b6078b5c345 100644
--- a/llvm/unittests/tools/llvm-exegesis/PowerPC/TestBase.h
+++ b/llvm/unittests/tools/llvm-exegesis/PowerPC/TestBase.h
@@ -24,7 +24,9 @@ void InitializePowerPCExegesisTarget();
class PPCTestBase : public ::testing::Test {
protected:
- PPCTestBase() : State("powerpc64le-unknown-linux", "ppc64le") {}
+ PPCTestBase()
+ : State(cantFail(
+ LLVMState::Create("powerpc64le-unknown-linux", "ppc64le"))) {}
static void SetUpTestCase() {
LLVMInitializePowerPCTargetInfo();
diff --git a/llvm/unittests/tools/llvm-exegesis/X86/BenchmarkResultTest.cpp b/llvm/unittests/tools/llvm-exegesis/X86/BenchmarkResultTest.cpp
index 5b47a6d967a07..f8e7d627e1d9a 100644
--- a/llvm/unittests/tools/llvm-exegesis/X86/BenchmarkResultTest.cpp
+++ b/llvm/unittests/tools/llvm-exegesis/X86/BenchmarkResultTest.cpp
@@ -28,6 +28,8 @@ using ::testing::Property;
namespace llvm {
namespace exegesis {
+void InitializeX86ExegesisTarget();
+
static std::string Dump(const MCInst &McInst) {
std::string Buffer;
raw_string_ostream OS(Buffer);
@@ -51,9 +53,11 @@ TEST(BenchmarkResultTest, WriteToAndReadFromDisk) {
LLVMInitializeX86TargetInfo();
LLVMInitializeX86Target();
LLVMInitializeX86TargetMC();
+ InitializeX86ExegesisTarget();
// Read benchmarks.
- const LLVMState State("x86_64-unknown-linux", "haswell");
+ const LLVMState State =
+ cantFail(LLVMState::Create("x86_64-unknown-linux", "haswell"));
ExitOnError ExitOnErr;
diff --git a/llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp b/llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp
index cd74688b00ea3..a76dddc69e60e 100644
--- a/llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp
+++ b/llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp
@@ -105,7 +105,8 @@ constexpr const char kTriple[] = "x86_64-unknown-linux";
class X86TargetTest : public ::testing::Test {
protected:
- X86TargetTest(const char *Features) : State(kTriple, "core2", Features) {}
+ X86TargetTest(const char *Features)
+ : State(cantFail(LLVMState::Create(kTriple, "core2", Features))) {}
static void SetUpTestCase() {
LLVMInitializeX86TargetInfo();
diff --git a/llvm/unittests/tools/llvm-exegesis/X86/TestBase.h b/llvm/unittests/tools/llvm-exegesis/X86/TestBase.h
index 287b54eb748e6..ea8063ee44d2c 100644
--- a/llvm/unittests/tools/llvm-exegesis/X86/TestBase.h
+++ b/llvm/unittests/tools/llvm-exegesis/X86/TestBase.h
@@ -24,7 +24,8 @@ void InitializeX86ExegesisTarget();
class X86TestBase : public ::testing::Test {
protected:
- X86TestBase() : State("x86_64-unknown-linux", "haswell") {}
+ X86TestBase()
+ : State(cantFail(LLVMState::Create("x86_64-unknown-linux", "haswell"))) {}
static void SetUpTestCase() {
LLVMInitializeX86TargetInfo();
More information about the llvm-commits
mailing list