From: Peter Collingbourne Date: Wed, 14 Dec 2016 01:17:59 +0000 (+0000) Subject: LTO: Add support for multi-module bitcode files. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cfc17356185dc7e0d1c992bb530f2eb74954b6aa;p=clang LTO: Add support for multi-module bitcode files. Differential Revision: https://reviews.llvm.org/D27313 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@289621 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/BackendUtil.cpp b/lib/CodeGen/BackendUtil.cpp index 87937c057b..cef7485446 100644 --- a/lib/CodeGen/BackendUtil.cpp +++ b/lib/CodeGen/BackendUtil.cpp @@ -753,7 +753,7 @@ static void runThinLTOBackend(const CodeGenOptions &CGOpts, Module *M, ImportList); std::vector> OwnedImports; - MapVector ModuleMap; + MapVector ModuleMap; for (auto &I : ImportList) { ErrorOr> MBOrErr = @@ -763,7 +763,34 @@ static void runThinLTOBackend(const CodeGenOptions &CGOpts, Module *M, << "': " << MBOrErr.getError().message() << "\n"; return; } - ModuleMap[I.first()] = (*MBOrErr)->getMemBufferRef(); + + Expected> BMsOrErr = + getBitcodeModuleList(**MBOrErr); + if (!BMsOrErr) { + handleAllErrors(BMsOrErr.takeError(), [&](ErrorInfoBase &EIB) { + errs() << "Error loading imported file '" << I.first() + << "': " << EIB.message() << '\n'; + }); + return; + } + + // The bitcode file may contain multiple modules, we want the one with a + // summary. + bool FoundModule = false; + for (BitcodeModule &BM : *BMsOrErr) { + Expected HasSummary = BM.hasSummary(); + if (HasSummary && *HasSummary) { + ModuleMap.insert({I.first(), BM}); + FoundModule = true; + break; + } + } + if (!FoundModule) { + errs() << "Error loading imported file '" << I.first() + << "': Could not find module summary\n"; + return; + } + OwnedImports.push_back(std::move(*MBOrErr)); } auto AddStream = [&](size_t Task) { diff --git a/test/CodeGen/thinlto_backend.ll b/test/CodeGen/thinlto_backend.ll index 0fb2643e03..4b01b1ad15 100644 --- a/test/CodeGen/thinlto_backend.ll +++ b/test/CodeGen/thinlto_backend.ll @@ -9,8 +9,8 @@ ; CHECK-WARNING: error: invalid argument '-fthinlto-index={{.*}}' only allowed with '-x ir' ; Ensure we get expected error for missing index file -; RUN: %clang -O2 -o %t3.o -x ir %t1.o -c -fthinlto-index=bad.thinlto.bc 2>&1 | FileCheck %s -check-prefix=CHECK-ERROR -; CHECK-ERROR: Error loading index file 'bad.thinlto.bc' +; 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 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 @@ -18,6 +18,11 @@ ; CHECK-OBJ: T f1 ; CHECK-OBJ-NOT: U f2 +; Ensure we get expected error for input files without summaries +; RUN: opt -o %t2.o %s +; RUN: %clang -target x86_64-unknown-linux-gnu -O2 -o %t3.o -x ir %t1.o -c -fthinlto-index=%t.thinlto.bc +; CHECK-ERROR2: Error loading imported file '{{.*}}': Could not find module summary + target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"