From: Vitaly Buka Date: Fri, 16 Feb 2018 23:34:16 +0000 (+0000) Subject: [ThinLTO] Ignore object files with no ThinLTO modules if -fthinlto-index= is set X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4a11be988c50be905a2ce2c9171e9acffd2b46ad;p=clang [ThinLTO] Ignore object files with no ThinLTO modules if -fthinlto-index= is set Summary: ThinLTO compilation may decide not to split module and keep at as regular LTO. In this can this module already processed during indexing and already a part of merged object file. So here we can just skip it. Reviewers: pcc, tejohnson Reviewed By: tejohnson Subscribers: mehdi_amini, inglorion, eraman, cfe-commits Differential Revision: https://reviews.llvm.org/D42680 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@325410 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/CodeGen/BackendUtil.h b/include/clang/CodeGen/BackendUtil.h index bb105cb533..3d1221a43b 100644 --- a/include/clang/CodeGen/BackendUtil.h +++ b/include/clang/CodeGen/BackendUtil.h @@ -49,6 +49,8 @@ namespace clang { llvm::Expected FindThinLTOModule(llvm::MemoryBufferRef MBRef); + llvm::BitcodeModule * + FindThinLTOModule(llvm::MutableArrayRef BMs); } #endif diff --git a/lib/CodeGen/BackendUtil.cpp b/lib/CodeGen/BackendUtil.cpp index fc8b552352..384800e951 100644 --- a/lib/CodeGen/BackendUtil.cpp +++ b/lib/CodeGen/BackendUtil.cpp @@ -1025,16 +1025,22 @@ Expected clang::FindThinLTOModule(MemoryBufferRef MBRef) { // The bitcode file may contain multiple modules, we want the one that is // marked as being the ThinLTO module. - for (BitcodeModule &BM : *BMsOrErr) { - Expected LTOInfo = BM.getLTOInfo(); - if (LTOInfo && LTOInfo->IsThinLTO) - return BM; - } + if (const BitcodeModule *Bm = FindThinLTOModule(*BMsOrErr)) + return *Bm; return make_error("Could not find module summary", inconvertibleErrorCode()); } +BitcodeModule *clang::FindThinLTOModule(MutableArrayRef BMs) { + for (BitcodeModule &BM : BMs) { + Expected LTOInfo = BM.getLTOInfo(); + if (LTOInfo && LTOInfo->IsThinLTO) + return &BM; + } + return nullptr; +} + static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M, const HeaderSearchOptions &HeaderOpts, const CodeGenOptions &CGOpts, diff --git a/lib/CodeGen/CodeGenAction.cpp b/lib/CodeGen/CodeGenAction.cpp index 6ca69d63cd..13fa4a8330 100644 --- a/lib/CodeGen/CodeGenAction.cpp +++ b/lib/CodeGen/CodeGenAction.cpp @@ -947,12 +947,21 @@ std::unique_ptr CodeGenAction::loadModule(MemoryBufferRef MBRef) { return {}; }; - Expected BMOrErr = FindThinLTOModule(MBRef); - if (!BMOrErr) - return DiagErrors(BMOrErr.takeError()); - + Expected> BMsOrErr = getBitcodeModuleList(MBRef); + if (!BMsOrErr) + return DiagErrors(BMsOrErr.takeError()); + BitcodeModule *Bm = FindThinLTOModule(*BMsOrErr); + // We have nothing to do if the file contains no ThinLTO module. This is + // possible if ThinLTO compilation was not able to split module. Content of + // the file was already processed by indexing and will be passed to the + // linker using merged object file. + if (!Bm) { + auto M = llvm::make_unique("empty", *VMContext); + M->setTargetTriple(CI.getTargetOpts().Triple); + return M; + } Expected> MOrErr = - BMOrErr->parseModule(*VMContext); + Bm->parseModule(*VMContext); if (!MOrErr) return DiagErrors(MOrErr.takeError()); return std::move(*MOrErr); diff --git a/test/CodeGen/thinlto_backend.ll b/test/CodeGen/thinlto_backend.ll index 86f30c0374..40530ec536 100644 --- a/test/CodeGen/thinlto_backend.ll +++ b/test/CodeGen/thinlto_backend.ll @@ -20,6 +20,12 @@ ; CHECK-OBJ-IGNORE-EMPTY: T f1 ; CHECK-OBJ-IGNORE-EMPTY: U f2 +; Ensure we don't fail with index and non-ThinLTO object file, and output must +; be empty file. +; RUN: opt -o %t5.o %s +; RUN: %clang -target x86_64-unknown-linux-gnu -O2 -o %t4.o -x ir %t5.o -c -fthinlto-index=%t.thinlto.bc +; RUN: llvm-nm %t4.o | count 0 + ; Ensure f2 was imported ; RUN: %clang -target x86_64-unknown-linux-gnu -O2 -o %t3.o -x ir %t1.o -c -fthinlto-index=%t.thinlto.bc ; RUN: llvm-nm %t3.o | FileCheck --check-prefix=CHECK-OBJ %s