Index: lib/CodeGen/BackendUtil.cpp =================================================================== --- lib/CodeGen/BackendUtil.cpp +++ lib/CodeGen/BackendUtil.cpp @@ -1153,6 +1153,7 @@ const llvm::DataLayout &TDesc, Module *M, BackendAction Action, std::unique_ptr OS) { + std::unique_ptr EmptyModule; if (!CGOpts.ThinLTOIndexFile.empty()) { // If we are performing a ThinLTO importing compile, load the function index // into memory and pass it into runThinLTOBackend, which will run the @@ -1170,11 +1171,22 @@ // A null CombinedIndex means we should skip ThinLTO compilation // (LLVM will optionally ignore empty index files, returning null instead // of an error). - bool DoThinLTOBackend = CombinedIndex != nullptr; - if (DoThinLTOBackend) { - runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts, - LOpts, std::move(OS), CGOpts.SampleProfileFile, Action); - return; + if (CombinedIndex) { + if (!CombinedIndex->skipModuleByDistributedBackend()) { + runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts, + LOpts, std::move(OS), CGOpts.SampleProfileFile, + Action); + return; + } + // Distributed indexing detected that nothing from the module is needed + // for the final linking. So we can skip the compilation. We sill need to + // output an empty object file to make sure that a linker does not fail + // trying to read it. Also for some features, like CFI, we must skip + // the compilation as CombinedIndex does not contain all required + // information. + EmptyModule = llvm::make_unique("empty", M->getContext()); + EmptyModule->setTargetTriple(M->getTargetTriple()); + M = EmptyModule.get(); } } Index: test/CodeGen/thinlto-distributed-backend-skip.ll =================================================================== --- test/CodeGen/thinlto-distributed-backend-skip.ll +++ test/CodeGen/thinlto-distributed-backend-skip.ll @@ -0,0 +1,21 @@ +; REQUIRES: x86-registered-target + +; Check that ThinLTO backend respects "SkipModuleByDistributedBackend" +; flag which can be set by indexing. + +; RUN: opt -thinlto-bc -o %t.o %s + +; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \ +; RUN: -fthinlto-index=%S/Inputs/thinlto-distributed-backend-skip.bc \ +; RUN: -emit-llvm -o - -x ir %t.o | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-grtev4-linux-gnu" + +; CHECK: "empty" +; CHECK: target triple = +; CHECK-NOT: @main +define i32 @main() { +entry: + ret i32 0 +}