llvm.org GIT mirror llvm / aeb2eff
Bitcode: Change getModuleSummaryIndex() to return an llvm::Expected. Differential Revision: https://reviews.llvm.org/D26539 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286624 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 years ago
9 changed file(s) with 60 addition(s) and 153 deletion(s). Raw diff Collapse all Expand all
3131 // Remove these functions once no longer needed by the C and libLTO APIs.
3232
3333 std::error_code errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, Error Err);
34 std::error_code
35 errorToErrorCodeAndEmitErrors(const DiagnosticHandlerFunction &DiagHandler,
36 Error Err);
3734
3835 template
3936 ErrorOr expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected Val) {
7976 Expected hasGlobalValueSummary(MemoryBufferRef Buffer);
8077
8178 /// Parse the specified bitcode buffer, returning the module summary index.
82 ErrorOr>
83 getModuleSummaryIndex(MemoryBufferRef Buffer,
84 const DiagnosticHandlerFunction &DiagnosticHandler);
79 Expected>
80 getModuleSummaryIndex(MemoryBufferRef Buffer);
8581
8682 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
8783 /// for an LLVM IR bitcode wrapper.
8181 /// \brief Parse module summary index in the given memory buffer.
8282 /// Return new ModuleSummaryIndexObjectFile instance containing parsed module
8383 /// summary/index.
84 static ErrorOr>
85 create(MemoryBufferRef Object,
86 const DiagnosticHandlerFunction &DiagnosticHandler);
84 static Expected>
85 create(MemoryBufferRef Object);
8786 };
8887 }
8988
9089 /// Parse the module summary index out of an IR file and return the module
9190 /// summary index object if found, or nullptr if not.
92 ErrorOr> getModuleSummaryIndexForFile(
93 StringRef Path, const DiagnosticHandlerFunction &DiagnosticHandler);
91 Expected>
92 getModuleSummaryIndexForFile(StringRef Path);
9493 }
9594
9695 #endif
834834 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
835835 EC = EIB.convertToErrorCode();
836836 Ctx.emitError(EIB.message());
837 });
838 return EC;
839 }
840 return std::error_code();
841 }
842
843 std::error_code llvm::errorToErrorCodeAndEmitErrors(
844 const DiagnosticHandlerFunction &DiagHandler, Error Err) {
845 if (Err) {
846 std::error_code EC;
847 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
848 EC = EIB.convertToErrorCode();
849 DiagHandler(DiagnosticInfoInlineAsm(EIB.message()));
850837 });
851838 return EC;
852839 }
66626649 }
66636650
66646651 // Parse the specified bitcode buffer, returning the function info index.
6665 ErrorOr> llvm::getModuleSummaryIndex(
6666 MemoryBufferRef Buffer,
6667 const DiagnosticHandlerFunction &DiagnosticHandler) {
6652 Expected>
6653 llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) {
66686654 Expected StreamOrErr = initStream(Buffer);
66696655 if (!StreamOrErr)
6670 return errorToErrorCodeAndEmitErrors(DiagnosticHandler,
6671 StreamOrErr.takeError());
6656 return StreamOrErr.takeError();
66726657
66736658 ModuleSummaryIndexBitcodeReader R(std::move(*StreamOrErr));
66746659
66756660 auto Index = llvm::make_unique();
66766661
6677 if (std::error_code EC = errorToErrorCodeAndEmitErrors(
6678 DiagnosticHandler,
6679 R.parseSummaryIndexInto(Index.get(), Buffer.getBufferIdentifier())))
6680 return EC;
6662 if (Error Err =
6663 R.parseSummaryIndexInto(Index.get(), Buffer.getBufferIdentifier()))
6664 return std::move(Err);
66816665
66826666 return std::move(Index);
66836667 }
416416 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
417417
418418 MemoryBufferRef MBRef = Input->Obj->getMemoryBufferRef();
419 ErrorOr>
420 SummaryObjOrErr =
421 object::ModuleSummaryIndexObjectFile::create(MBRef, Conf.DiagHandler);
419 Expected>
420 SummaryObjOrErr = object::ModuleSummaryIndexObjectFile::create(MBRef);
422421 if (!SummaryObjOrErr)
423 return errorCodeToError(SummaryObjOrErr.getError());
422 return SummaryObjOrErr.takeError();
424423 ThinLTO.CombinedIndex.mergeFrom((*SummaryObjOrErr)->takeIndex(),
425424 ThinLTO.ModuleMap.size());
426425
6868 static cl::opt
6969 ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
7070
71 static void diagnosticHandler(const DiagnosticInfo &DI) {
72 DiagnosticPrinterRawOStream DP(errs());
73 DI.print(DP);
74 errs() << '\n';
75 }
76
7771 // Simple helper to save temporary files for debug.
7872 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
7973 unsigned count, StringRef Suffix) {
512506 std::unique_ptr CombinedIndex;
513507 uint64_t NextModuleId = 0;
514508 for (auto &ModuleBuffer : Modules) {
515 ErrorOr> ObjOrErr =
516 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
517 diagnosticHandler);
518 if (std::error_code EC = ObjOrErr.getError()) {
509 Expected> ObjOrErr =
510 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer);
511 if (!ObjOrErr) {
519512 // FIXME diagnose
520 errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
521 << EC.message() << "\n";
513 logAllUnhandledErrors(
514 ObjOrErr.takeError(), errs(),
515 "error: can't create ModuleSummaryIndexObjectFile for buffer: ");
522516 return nullptr;
523517 }
524518 auto Index = (*ObjOrErr)->takeIndex();
6969 // Parse module summary index in the given memory buffer.
7070 // Return new ModuleSummaryIndexObjectFile instance containing parsed
7171 // module summary/index.
72 ErrorOr>
73 ModuleSummaryIndexObjectFile::create(
74 MemoryBufferRef Object,
75 const DiagnosticHandlerFunction &DiagnosticHandler) {
76 std::unique_ptr Index;
77
72 Expected>
73 ModuleSummaryIndexObjectFile::create(MemoryBufferRef Object) {
7874 ErrorOr BCOrErr = findBitcodeInMemBuffer(Object);
7975 if (!BCOrErr)
80 return BCOrErr.getError();
76 return errorCodeToError(BCOrErr.getError());
8177
82 ErrorOr> IOrErr =
83 getModuleSummaryIndex(BCOrErr.get(), DiagnosticHandler);
78 Expected> IOrErr =
79 getModuleSummaryIndex(BCOrErr.get());
8480
85 if (std::error_code EC = IOrErr.getError())
86 return EC;
81 if (!IOrErr)
82 return std::move(IOrErr.takeError());
8783
88 Index = std::move(IOrErr.get());
89
84 std::unique_ptr Index = std::move(IOrErr.get());
9085 return llvm::make_unique(Object,
9186 std::move(Index));
9287 }
9388
9489 // Parse the module summary index out of an IR file and return the summary
9590 // index object if found, or nullptr if not.
96 ErrorOr> llvm::getModuleSummaryIndexForFile(
97 StringRef Path, const DiagnosticHandlerFunction &DiagnosticHandler) {
91 Expected>
92 llvm::getModuleSummaryIndexForFile(StringRef Path) {
9893 ErrorOr> FileOrErr =
9994 MemoryBuffer::getFileOrSTDIN(Path);
10095 std::error_code EC = FileOrErr.getError();
10196 if (EC)
102 return EC;
97 return errorCodeToError(EC);
10398 MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef();
104 ErrorOr> ObjOrErr =
105 object::ModuleSummaryIndexObjectFile::create(BufferRef,
106 DiagnosticHandler);
107 EC = ObjOrErr.getError();
108 if (EC)
109 return EC;
99 Expected> ObjOrErr =
100 object::ModuleSummaryIndexObjectFile::create(BufferRef);
101 if (!ObjOrErr)
102 return ObjOrErr.takeError();
110103
111104 object::ModuleSummaryIndexObjectFile &Obj = **ObjOrErr;
112105 return Obj.takeIndex();
738738 SummaryFile("summary-file",
739739 cl::desc("The summary file to use for function importing."));
740740
741 static void diagnosticHandler(const DiagnosticInfo &DI) {
742 raw_ostream &OS = errs();
743 DiagnosticPrinterRawOStream DP(OS);
744 DI.print(DP);
745 OS << '\n';
746 }
747
748 /// Parse the summary index out of an IR file and return the summary
749 /// index object if found, or nullptr if not.
750 static std::unique_ptr getModuleSummaryIndexForFile(
751 StringRef Path, std::string &Error,
752 const DiagnosticHandlerFunction &DiagnosticHandler) {
753 std::unique_ptr Buffer;
754 ErrorOr> BufferOrErr =
755 MemoryBuffer::getFile(Path);
756 if (std::error_code EC = BufferOrErr.getError()) {
757 Error = EC.message();
758 return nullptr;
759 }
760 Buffer = std::move(BufferOrErr.get());
761 ErrorOr> ObjOrErr =
762 object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(),
763 DiagnosticHandler);
764 if (std::error_code EC = ObjOrErr.getError()) {
765 Error = EC.message();
766 return nullptr;
767 }
768 return (*ObjOrErr)->takeIndex();
769 }
770
771741 static bool doImportingForModule(Module &M, const ModuleSummaryIndex *Index) {
772742 if (SummaryFile.empty() && !Index)
773743 report_fatal_error("error: -function-import requires -summary-file or "
776746 if (!SummaryFile.empty()) {
777747 if (Index)
778748 report_fatal_error("error: -summary-file and index from frontend\n");
779 std::string Error;
780 IndexPtr =
781 getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler);
782 if (!IndexPtr) {
783 errs() << "Error loading file '" << SummaryFile << "': " << Error << "\n";
749 Expected> IndexPtrOrErr =
750 getModuleSummaryIndexForFile(SummaryFile);
751 if (!IndexPtrOrErr) {
752 logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
753 "Error loading file '" + SummaryFile + "': ");
784754 return false;
785755 }
756 IndexPtr = std::move(*IndexPtrOrErr);
786757 Index = IndexPtr.get();
787758 }
788759
179179 }
180180 } // anonymous namespace
181181
182 static void diagnosticHandler(const DiagnosticInfo &DI) {
182 static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
183183 unsigned Severity = DI.getSeverity();
184184 switch (Severity) {
185185 case DS_Error:
200200 errs() << '\n';
201201 }
202202
203 static void diagnosticHandlerWithContext(const DiagnosticInfo &DI, void *C) {
204 diagnosticHandler(DI);
205 }
206
207203 /// Import any functions requested via the -import option.
208204 static bool importFunctions(const char *argv0, LLVMContext &Context,
209205 Linker &L) {
210206 if (SummaryIndex.empty())
211207 return true;
212 ErrorOr> IndexOrErr =
213 llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler);
214 std::error_code EC = IndexOrErr.getError();
215 if (EC) {
216 errs() << EC.message() << '\n';
217 return false;
218 }
219 auto Index = std::move(IndexOrErr.get());
208 std::unique_ptr Index =
209 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
220210
221211 // Map of Module -> List of globals to import from the Module
222212 std::map> ModuleToGlobalsToImportMap;
318308 // If a module summary index is supplied, load it so linkInModule can treat
319309 // local functions/variables as exported and promote if necessary.
320310 if (!SummaryIndex.empty()) {
321 ErrorOr> IndexOrErr =
322 llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler);
323 std::error_code EC = IndexOrErr.getError();
324 if (EC) {
325 errs() << EC.message() << '\n';
326 return false;
327 }
328 auto Index = std::move(IndexOrErr.get());
311 std::unique_ptr Index =
312 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
329313
330314 // Promotion
331315 if (renameModuleForThinLTO(*M, *Index))
352336 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
353337
354338 LLVMContext Context;
355 Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true);
339 Context.setDiagnosticHandler(diagnosticHandler, nullptr, true);
356340
357341 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
358342 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
196196 }
197197
198198 static std::string CurrentActivity;
199 static void diagnosticHandler(const DiagnosticInfo &DI) {
199 static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
200200 raw_ostream &OS = errs();
201201 OS << "llvm-lto: ";
202202 switch (DI.getSeverity()) {
225225 exit(1);
226226 }
227227
228 static void diagnosticHandlerWithContext(const DiagnosticInfo &DI,
229 void *Context) {
230 diagnosticHandler(DI);
231 }
232
233228 static void error(const Twine &Msg) {
234229 errs() << "llvm-lto: " << Msg << '\n';
235230 exit(1);
259254 Buffer = std::move(BufferOrErr.get());
260255 CurrentActivity = ("loading file '" + Path + "'").str();
261256 std::unique_ptr Context = llvm::make_unique();
262 Context->setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true);
257 Context->setDiagnosticHandler(diagnosticHandler, nullptr, true);
263258 ErrorOr> Ret = LTOModule::createInLocalContext(
264259 std::move(Context), Buffer->getBufferStart(), Buffer->getBufferSize(),
265260 Options, Path);
271266 /// Print some statistics on the index for each input files.
272267 void printIndexStats() {
273268 for (auto &Filename : InputFilenames) {
274 CurrentActivity = "loading file '" + Filename + "'";
275 ErrorOr> IndexOrErr =
276 llvm::getModuleSummaryIndexForFile(Filename, diagnosticHandler);
277 error(IndexOrErr, "error " + CurrentActivity);
278 std::unique_ptr Index = std::move(IndexOrErr.get());
279 CurrentActivity = "";
269 ExitOnError ExitOnErr("llvm-lto: error loading file '" + Filename + "': ");
270 std::unique_ptr Index =
271 ExitOnErr(llvm::getModuleSummaryIndexForFile(Filename));
280272 // Skip files without a module summary.
281273 if (!Index)
282274 report_fatal_error(Filename + " does not contain an index");
329321 ModuleSummaryIndex CombinedIndex;
330322 uint64_t NextModuleId = 0;
331323 for (auto &Filename : InputFilenames) {
332 CurrentActivity = "loading file '" + Filename + "'";
333 ErrorOr> IndexOrErr =
334 llvm::getModuleSummaryIndexForFile(Filename, diagnosticHandler);
335 error(IndexOrErr, "error " + CurrentActivity);
336 std::unique_ptr Index = std::move(IndexOrErr.get());
337 CurrentActivity = "";
324 ExitOnError ExitOnErr("llvm-lto: error loading file '" + Filename + "': ");
325 std::unique_ptr Index =
326 ExitOnErr(llvm::getModuleSummaryIndexForFile(Filename));
338327 // Skip files without a module summary.
339328 if (!Index)
340329 continue;
399388 std::unique_ptr loadCombinedIndex() {
400389 if (ThinLTOIndex.empty())
401390 report_fatal_error("Missing -thinlto-index for ThinLTO promotion stage");
402 auto CurrentActivity = "loading file '" + ThinLTOIndex + "'";
403 ErrorOr> IndexOrErr =
404 llvm::getModuleSummaryIndexForFile(ThinLTOIndex, diagnosticHandler);
405 error(IndexOrErr, "error " + CurrentActivity);
406 return std::move(IndexOrErr.get());
391 ExitOnError ExitOnErr("llvm-lto: error loading file '" + ThinLTOIndex +
392 "': ");
393 return ExitOnErr(llvm::getModuleSummaryIndexForFile(ThinLTOIndex));
407394 }
408395
409396 static std::unique_ptr loadModule(StringRef Filename,
801788 unsigned BaseArg = 0;
802789
803790 LLVMContext Context;
804 Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true);
791 Context.setDiagnosticHandler(diagnosticHandler, nullptr, true);
805792
806793 LTOCodeGenerator CodeGen(Context);
807794