From bcdce5cd41207f6554e463119c0c4cb0da7eac0d Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Sat, 20 Apr 2019 18:30:17 +0000 Subject: [PATCH] [JITLink] Add check to JITLink unit test to bail out for unsupported targets. This should prevent spurious JITLink unit test failures for builds that do not support the target(s) required by the tests. llvm-svn: 358825 --- .../ExecutionEngine/JITLink/JITLinkTestCommon.cpp | 43 ++++++++++++++++------ .../ExecutionEngine/JITLink/JITLinkTestCommon.h | 21 ++++++++--- .../JITLink/JITLinkTest_MachO_x86_64_Tests.cpp | 7 +++- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/llvm/unittests/ExecutionEngine/JITLink/JITLinkTestCommon.cpp b/llvm/unittests/ExecutionEngine/JITLink/JITLinkTestCommon.cpp index 786b00e..b0c6d5b 100644 --- a/llvm/unittests/ExecutionEngine/JITLink/JITLinkTestCommon.cpp +++ b/llvm/unittests/ExecutionEngine/JITLink/JITLinkTestCommon.cpp @@ -15,14 +15,16 @@ using namespace llvm::jitlink; namespace llvm { -JITLinkTestCommon::TestResources::TestResources(StringRef AsmSrc, - StringRef TripleStr, bool PIC, - bool LargeCodeModel, - MCTargetOptions Options) - : ObjStream(ObjBuffer), Options(std::move(Options)) { - Triple TT(Triple::normalize(TripleStr)); - initializeTripleSpecifics(TT); - initializeTestSpecifics(AsmSrc, TT, PIC, LargeCodeModel); +Expected> +JITLinkTestCommon::TestResources::Create(StringRef AsmSrc, StringRef TripleStr, + bool PIC, bool LargeCodeModel, + MCTargetOptions Options) { + Error Err = Error::success(); + auto R = std::unique_ptr(new TestResources( + AsmSrc, TripleStr, PIC, LargeCodeModel, std::move(Options), Err)); + if (Err) + return std::move(Err); + return std::move(R); } MemoryBufferRef @@ -31,12 +33,27 @@ JITLinkTestCommon::TestResources::getTestObjectBufferRef() const { "Test object"); } -void JITLinkTestCommon::TestResources::initializeTripleSpecifics(Triple &TT) { - std::string Error; - TheTarget = TargetRegistry::lookupTarget("", TT, Error); +JITLinkTestCommon::TestResources::TestResources(StringRef AsmSrc, + StringRef TripleStr, bool PIC, + bool LargeCodeModel, + MCTargetOptions Options, + Error &Err) + : ObjStream(ObjBuffer), Options(std::move(Options)) { + ErrorAsOutParameter _(&Err); + Triple TT(Triple::normalize(TripleStr)); + if (auto Err2 = initializeTripleSpecifics(TT)) { + Err = std::move(Err2); + return; + } + initializeTestSpecifics(AsmSrc, TT, PIC, LargeCodeModel); +} + +Error JITLinkTestCommon::TestResources::initializeTripleSpecifics(Triple &TT) { + std::string ErrorMsg; + TheTarget = TargetRegistry::lookupTarget("", TT, ErrorMsg); if (!TheTarget) - report_fatal_error(Error); + return make_error(ErrorMsg, inconvertibleErrorCode()); MRI.reset(TheTarget->createMCRegInfo(TT.getTriple())); if (!MRI) @@ -59,6 +76,8 @@ void JITLinkTestCommon::TestResources::initializeTripleSpecifics(Triple &TT) { if (!Dis) report_fatal_error("Could not build MCDisassembler"); + + return Error::success(); } void JITLinkTestCommon::TestResources::initializeTestSpecifics( diff --git a/llvm/unittests/ExecutionEngine/JITLink/JITLinkTestCommon.h b/llvm/unittests/ExecutionEngine/JITLink/JITLinkTestCommon.h index c19232e..8e1273e 100644 --- a/llvm/unittests/ExecutionEngine/JITLink/JITLinkTestCommon.h +++ b/llvm/unittests/ExecutionEngine/JITLink/JITLinkTestCommon.h @@ -40,15 +40,19 @@ public: class TestResources { public: - TestResources(StringRef AsmSrc, StringRef TripleStr, bool PIC, - bool LargeCodeModel, MCTargetOptions Options); + static Expected> + Create(StringRef AsmSrc, StringRef TripleStr, bool PIC, bool LargeCodeModel, + MCTargetOptions Options); MemoryBufferRef getTestObjectBufferRef() const; const MCDisassembler &getDisassembler() const { return *Dis; } private: - void initializeTripleSpecifics(Triple &TT); + TestResources(StringRef AsmSrc, StringRef TripleStr, bool PIC, + bool LargeCodeModel, MCTargetOptions Options, Error &Err); + + Error initializeTripleSpecifics(Triple &TT); void initializeTestSpecifics(StringRef AsmSource, const Triple &TT, bool PIC, bool LargeCodeModel); @@ -123,11 +127,16 @@ public: JITLinkTestCommon(); - std::unique_ptr + /// Get TestResources for this target/test. + /// + /// If this method fails it is likely because the target is not supported in + /// this build. The test should bail out without failing (possibly logging a + /// diagnostic). + Expected> getTestResources(StringRef AsmSrc, StringRef Triple, bool PIC, bool LargeCodeModel, MCTargetOptions Options) const { - return llvm::make_unique(AsmSrc, Triple, PIC, LargeCodeModel, - std::move(Options)); + return TestResources::Create(AsmSrc, Triple, PIC, LargeCodeModel, + std::move(Options)); } template diff --git a/llvm/unittests/ExecutionEngine/JITLink/JITLinkTest_MachO_x86_64_Tests.cpp b/llvm/unittests/ExecutionEngine/JITLink/JITLinkTest_MachO_x86_64_Tests.cpp index f8728c7..36e62a4 100644 --- a/llvm/unittests/ExecutionEngine/JITLink/JITLinkTest_MachO_x86_64_Tests.cpp +++ b/llvm/unittests/ExecutionEngine/JITLink/JITLinkTest_MachO_x86_64_Tests.cpp @@ -33,9 +33,14 @@ public: BasicVerifyGraphFunction RunGraphTest) { auto TR = getTestResources(AsmSrc, Triple, PIC, LargeCodeModel, std::move(Options)); + if (!TR) { + dbgs() << "Skipping JITLInk unit test: " << toString(TR.takeError()) + << "\n"; + return; + } auto JTCtx = llvm::make_unique( - *TR, [&](AtomGraph &G) { RunGraphTest(G, TR->getDisassembler()); }); + **TR, [&](AtomGraph &G) { RunGraphTest(G, (*TR)->getDisassembler()); }); JTCtx->externals() = std::move(Externals); -- 2.7.4