tejohnson created this revision. tejohnson added a reviewer: mehdi_amini. tejohnson added a subscriber: cfe-commits.
In order to simplify distributed build system integration, where actions may be scheduled before the Thin Link which determines the list of objects selected by the linker. The gold plugin currently will emit 0-sized index files for objects not selected by the link, to enable checking for expected output files by the build system. If the build system then schedules a backend action for these bitcode files, we want to be able to fall back to normal compilation instead of failing. Fallback is enabled under an option with this patch (I am investigating whether this can be addressed in our build system, but that is a longer term fix and so this enables a workaround in the meantime). https://reviews.llvm.org/D28362 Files: lib/CodeGen/BackendUtil.cpp test/CodeGen/thinlto_backend.ll 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 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<bool> 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<raw_pwrite_stream> 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 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<bool> 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<raw_pwrite_stream> 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);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits