Index: lib/CodeGen/BackendUtil.cpp =================================================================== --- lib/CodeGen/BackendUtil.cpp +++ lib/CodeGen/BackendUtil.cpp @@ -36,6 +36,7 @@ #include "llvm/Object/ModuleSummaryIndexObjectFile.h" #include "llvm/Passes/PassBuilder.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/TargetRegistry.h" @@ -57,6 +58,12 @@ using namespace clang; using namespace llvm; +static llvm::cl::opt IgnoreEmptyThinLTOIndexFile( + "ignore-empty-index-file", llvm::cl::ZeroOrMore, + llvm::cl::desc( + "Ignore an empty index file and perform non-ThinLTO compilation"), + llvm::cl::init(false)); + namespace { class EmitAssemblyHelper { @@ -935,8 +942,16 @@ Module *M, BackendAction Action, std::unique_ptr OS) { if (!CGOpts.ThinLTOIndexFile.empty()) { - runThinLTOBackend(CGOpts, M, std::move(OS)); - return; + bool DoThinLTOBackend = true; + if (IgnoreEmptyThinLTOIndexFile) { + uint64_t IndexFileSize; + llvm::sys::fs::file_size(CGOpts.ThinLTOIndexFile, IndexFileSize); + DoThinLTOBackend = IndexFileSize > 0; + } + if (DoThinLTOBackend) { + runThinLTOBackend(CGOpts, M, std::move(OS)); + return; + } } EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M); Index: test/CodeGen/thinlto_backend.ll =================================================================== --- test/CodeGen/thinlto_backend.ll +++ test/CodeGen/thinlto_backend.ll @@ -12,6 +12,11 @@ ; RUN: %clang -O2 -o %t4.o -x ir %t1.o -c -fthinlto-index=bad.thinlto.bc 2>&1 | FileCheck %s -check-prefix=CHECK-ERROR1 ; CHECK-ERROR1: Error loading index file 'bad.thinlto.bc' +; Ensure we ignore empty index file under -ignore-empty-index-file +; RUN: touch %t4.thinlto.bc +; RUN: %clang -O2 -o %t4.o -x ir %t1.o -c -fthinlto-index=%t4.thinlto.bc -ignore-empty-index-file 2>&1 | FileCheck %s -check-prefix=CHECK-NOERROR +; CHECK-NOERROR-NOT: Error loading index file 'bad.thinlto.bc' + ; 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