From: Vitaly Buka Date: Fri, 16 Feb 2018 23:38:22 +0000 (+0000) Subject: [ThinLTO] Allow indexing to request backend to ignore the module X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4735edbbaf28ced185e76223b39ab4f0141186e3;p=clang [ThinLTO] Allow indexing to request backend to ignore the module Summary: Gold plugin does not add pass to ThinLTO modules without useful symbols. In this case ThinLTO can't create corresponding index file and some features, like CFI, cannot be processes by backed correctly without index. Given that we don't need the backed output we can request it to avoid processing the module. This is implemented by this patch using new "SkipModuleByDistributedBackend" flag. Reviewers: pcc, tejohnson Subscribers: mehdi_amini, inglorion, eraman, cfe-commits Differential Revision: https://reviews.llvm.org/D42995 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@325411 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/BackendUtil.cpp b/lib/CodeGen/BackendUtil.cpp index 384800e951..dde657787a 100644 --- a/lib/CodeGen/BackendUtil.cpp +++ b/lib/CodeGen/BackendUtil.cpp @@ -1153,6 +1153,7 @@ void clang::EmitBackendOutput(DiagnosticsEngine &Diags, 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 @@ void clang::EmitBackendOutput(DiagnosticsEngine &Diags, // 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(); } } diff --git a/test/CodeGen/Inputs/thinlto-distributed-backend-skip.bc b/test/CodeGen/Inputs/thinlto-distributed-backend-skip.bc new file mode 100644 index 0000000000..0243eb6960 Binary files /dev/null and b/test/CodeGen/Inputs/thinlto-distributed-backend-skip.bc differ diff --git a/test/CodeGen/thinlto-distributed-backend-skip.ll b/test/CodeGen/thinlto-distributed-backend-skip.ll new file mode 100644 index 0000000000..d9fa47d23d --- /dev/null +++ b/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 +}