llvm.org GIT mirror llvm / 76c218e
Bitcode: Change the materializer interface to return llvm::Error. Differential Revision: https://reviews.llvm.org/D26439 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286382 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 2 years ago
19 changed file(s) with 113 addition(s) and 104 deletion(s). Raw diff Collapse all Expand all
1717 #ifndef LLVM_IR_GVMATERIALIZER_H
1818 #define LLVM_IR_GVMATERIALIZER_H
1919
20 #include
2120 #include
2221
2322 namespace llvm {
23 class Error;
2424 class Function;
2525 class GlobalValue;
2626 class Module;
3535
3636 /// Make sure the given GlobalValue is fully read.
3737 ///
38 virtual std::error_code materialize(GlobalValue *GV) = 0;
38 virtual Error materialize(GlobalValue *GV) = 0;
3939
4040 /// Make sure the entire Module has been completely read.
4141 ///
42 virtual std::error_code materializeModule() = 0;
42 virtual Error materializeModule() = 0;
4343
44 virtual std::error_code materializeMetadata() = 0;
44 virtual Error materializeMetadata() = 0;
4545 virtual void setStripDebugInfo() = 0;
4646
4747 virtual std::vector getIdentifiedStructTypes() const = 0;
2020 #include "llvm/IR/Constant.h"
2121 #include "llvm/IR/DerivedTypes.h"
2222 #include "llvm/Support/MD5.h"
23 #include
2423
2524 namespace llvm {
2625
2726 class Comdat;
27 class Error;
2828 class GlobalObject;
2929 class PointerType;
3030 class Module;
466466 /// function has been read in yet or not.
467467 bool isMaterializable() const;
468468
469 /// Make sure this GlobalValue is fully read. If the module is corrupt, this
470 /// returns true and fills in the optional string with information about the
471 /// problem. If successful, this returns false.
472 std::error_code materialize();
469 /// Make sure this GlobalValue is fully read.
470 Error materialize();
473471
474472 /// @}
475473
2525 #include "llvm/Support/CBindingWrapping.h"
2626 #include "llvm/Support/CodeGen.h"
2727 #include "llvm/Support/DataTypes.h"
28 #include
2928
3029 namespace llvm {
3130 template class Optional;
31 class Error;
3232 class FunctionType;
3333 class GVMaterializer;
3434 class LLVMContext;
453453 GVMaterializer *getMaterializer() const { return Materializer.get(); }
454454 bool isMaterialized() const { return !getMaterializer(); }
455455
456 /// Make sure the GlobalValue is fully read. If the module is corrupt, this
457 /// returns true and fills in the optional string with information about the
458 /// problem. If successful, this returns false.
459 std::error_code materialize(GlobalValue *GV);
456 /// Make sure the GlobalValue is fully read.
457 llvm::Error materialize(GlobalValue *GV);
460458
461459 /// Make sure all GlobalValues in this Module are fully read and clear the
462460 /// Materializer.
463 std::error_code materializeAll();
464
465 std::error_code materializeMetadata();
461 llvm::Error materializeAll();
462
463 llvm::Error materializeMetadata();
466464
467465 /// @}
468466 /// @name Direct access to the globals list, functions list, and symbol table
1313 #include "llvm/IR/GlobalValue.h"
1414 #include "llvm/IR/ModuleSummaryIndex.h"
1515 #include "llvm/IR/PassManager.h"
16 #include "llvm/Support/Error.h"
1617
1718 #include
1819 #include
5253 /// \p ForceImportReferencedDiscardableSymbols will set the ModuleLinker in
5354 /// a mode where referenced discarable symbols in the source modules will be
5455 /// imported as well even if they are not present in the ImportList.
55 bool importFunctions(Module &M, const ImportMapTy &ImportList,
56 bool ForceImportReferencedDiscardableSymbols = false);
56 Expected
57 importFunctions(Module &M, const ImportMapTy &ImportList,
58 bool ForceImportReferencedDiscardableSymbols = false);
5759
5860 private:
5961 /// The summaries index used to trigger importing.
369369
370370 Error materializeForwardReferencedFunctions();
371371
372 std::error_code materialize(GlobalValue *GV) override;
373 Error materializeImpl(GlobalValue *GV);
374 std::error_code materializeModule() override;
375 Error materializeModuleImpl();
372 Error materialize(GlobalValue *GV) override;
373 Error materializeModule() override;
376374 std::vector getIdentifiedStructTypes() const override;
377375
378376 /// \brief Main interface to parsing a bitcode buffer.
393391 static uint64_t decodeSignRotatedValue(uint64_t V);
394392
395393 /// Materialize any deferred Metadata block.
396 std::error_code materializeMetadata() override;
397 Error materializeMetadataImpl();
394 Error materializeMetadata() override;
398395
399396 void setStripDebugInfo() override;
400397
676673 return error("Never resolved function from blockaddress");
677674
678675 // Try to materialize F.
679 if (Error Err = materializeImpl(F))
676 if (Error Err = materialize(F))
680677 return Err;
681678 }
682679 assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
35793576 return Error::success();
35803577 }
35813578
3582 std::error_code BitcodeReader::materializeMetadata() {
3583 return errorToErrorCodeAndEmitErrors(Context, materializeMetadataImpl());
3584 }
3585
3586 Error BitcodeReader::materializeMetadataImpl() {
3579 Error BitcodeReader::materializeMetadata() {
35873580 for (uint64_t BitPos : DeferredMetadataInfo) {
35883581 // Move the bit stream to the saved position.
35893582 Stream.JumpToBit(BitPos);
58555848 // GVMaterializer implementation
58565849 //===----------------------------------------------------------------------===//
58575850
5858 std::error_code BitcodeReader::materialize(GlobalValue *GV) {
5859 return errorToErrorCodeAndEmitErrors(Context, materializeImpl(GV));
5860 }
5861
5862 Error BitcodeReader::materializeImpl(GlobalValue *GV) {
5851 Error BitcodeReader::materialize(GlobalValue *GV) {
58635852 Function *F = dyn_cast(GV);
58645853 // If it's not a function or is already material, ignore the request.
58655854 if (!F || !F->isMaterializable())
58745863 return Err;
58755864
58765865 // Materialize metadata before parsing any function bodies.
5877 if (Error Err = materializeMetadataImpl())
5866 if (Error Err = materializeMetadata())
58785867 return Err;
58795868
58805869 // Move the bit stream to the saved position of the deferred function body.
59145903 return materializeForwardReferencedFunctions();
59155904 }
59165905
5917 std::error_code BitcodeReader::materializeModule() {
5918 return errorToErrorCodeAndEmitErrors(Context, materializeModuleImpl());
5919 }
5920
5921 Error BitcodeReader::materializeModuleImpl() {
5922 if (Error Err = materializeMetadataImpl())
5906 Error BitcodeReader::materializeModule() {
5907 if (Error Err = materializeMetadata())
59235908 return Err;
59245909
59255910 // Promise to materialize all forward references.
59285913 // Iterate over the module, deserializing any functions that are still on
59295914 // disk.
59305915 for (Function &F : *TheModule) {
5931 if (Error Err = materializeImpl(&F))
5916 if (Error Err = materialize(&F))
59325917 return Err;
59335918 }
59345919 // At this point, if there are any function bodies, parse the rest of
66636648
66646649 if (MaterializeAll) {
66656650 // Read in the entire module, and destroy the BitcodeReader.
6666 if (std::error_code EC = M->materializeAll())
6667 return EC;
6651 if (Error Err = M->materializeAll())
6652 return errorToErrorCodeAndEmitErrors(Context, std::move(Err));
66686653 } else {
66696654 // Resolve forward references from blockaddresses.
66706655 if (Error Err = R->materializeForwardReferencedFunctions())
3434 ExecutionEngine *Interpreter::create(std::unique_ptr M,
3535 std::string *ErrStr) {
3636 // Tell this Module to materialize everything and release the GVMaterializer.
37 if (std::error_code EC = M->materializeAll()) {
37 if (Error Err = M->materializeAll()) {
38 std::string Msg;
39 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
40 Msg = EIB.message();
41 });
3842 if (ErrStr)
39 *ErrStr = EC.message();
43 *ErrStr = Msg;
4044 // We got an error, just return 0
4145 return nullptr;
4246 }
2020 #include "llvm/IR/GlobalVariable.h"
2121 #include "llvm/IR/Module.h"
2222 #include "llvm/IR/Operator.h"
23 #include "llvm/Support/Error.h"
2324 #include "llvm/Support/ErrorHandling.h"
2425 using namespace llvm;
2526
3233 return F->isMaterializable();
3334 return false;
3435 }
35 std::error_code GlobalValue::materialize() {
36 Error GlobalValue::materialize() {
3637 return getParent()->materialize(this);
3738 }
3839
1919 #include "llvm/Support/Chrono.h"
2020 #include "llvm/Support/CommandLine.h"
2121 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/Error.h"
2223 #include "llvm/Support/ErrorHandling.h"
2324 #include "llvm/Support/ManagedStatic.h"
2425 #include "llvm/Support/Mutex.h"
13761377 /// so, return true.
13771378 ///
13781379 bool FunctionPassManager::run(Function &F) {
1379 if (std::error_code EC = F.materialize())
1380 report_fatal_error("Error reading bitcode file: " + EC.message());
1380 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1381 report_fatal_error("Error reading bitcode file: " + EIB.message());
1382 });
13811383 return FPM->run(F);
13821384 }
13831385
2424 #include "llvm/IR/LLVMContext.h"
2525 #include "llvm/IR/TypeFinder.h"
2626 #include "llvm/Support/Dwarf.h"
27 #include "llvm/Support/Error.h"
2728 #include "llvm/Support/MemoryBuffer.h"
2829 #include "llvm/Support/Path.h"
2930 #include "llvm/Support/RandomNumberGenerator.h"
404405 Materializer.reset(GVM);
405406 }
406407
407 std::error_code Module::materialize(GlobalValue *GV) {
408 Error Module::materialize(GlobalValue *GV) {
408409 if (!Materializer)
409 return std::error_code();
410 return Error::success();
410411
411412 return Materializer->materialize(GV);
412413 }
413414
414 std::error_code Module::materializeAll() {
415 Error Module::materializeAll() {
415416 if (!Materializer)
416 return std::error_code();
417 return Error::success();
417418 std::unique_ptr M = std::move(Materializer);
418419 return M->materializeModule();
419420 }
420421
421 std::error_code Module::materializeMetadata() {
422 Error Module::materializeMetadata() {
422423 if (!Materializer)
423 return std::error_code();
424 return Error::success();
424425 return Materializer->materializeMetadata();
425426 }
426427
349349 std::unique_ptr Obj = std::move(*ObjOrErr);
350350
351351 Module &M = Obj->getModule();
352 M.materializeMetadata();
352 if (Error Err = M.materializeMetadata())
353 return Err;
353354 UpgradeDebugInfo(M);
354355
355356 SmallPtrSet Used;
358358 };
359359
360360 FunctionImporter Importer(CombinedIndex, ModuleLoader);
361 Importer.importFunctions(Mod, ImportList);
361 if (Error Err = Importer.importFunctions(Mod, ImportList).takeError())
362 return Err;
362363
363364 if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
364365 return Error();
151151 const FunctionImporter::ImportMapTy &ImportList) {
152152 ModuleLoader Loader(TheModule.getContext(), ModuleMap);
153153 FunctionImporter Importer(Index, Loader);
154 Importer.importFunctions(TheModule, ImportList);
154 if (!Importer.importFunctions(TheModule, ImportList))
155 report_fatal_error("importFunctions failed");
155156 }
156157
157158 static void optimizeModule(Module &TheModule, TargetMachine &TM) {
962962 assert(Dst.isDeclaration() && !Src.isDeclaration());
963963
964964 // Materialize if needed.
965 if (std::error_code EC = Src.materialize())
966 return errorCodeToError(EC);
965 if (Error Err = Src.materialize())
966 return Err;
967967
968968 // Link in the operands without remapping.
969969 if (Src.hasPrefixData())
11901190 Error IRLinker::run() {
11911191 // Ensure metadata materialized before value mapping.
11921192 if (SrcM->getMaterializer())
1193 if (std::error_code EC = SrcM->getMaterializer()->materializeMetadata())
1194 return errorCodeToError(EC);
1193 if (Error Err = SrcM->getMaterializer()->materializeMetadata())
1194 return Err;
11951195
11961196 // Inherit the target data from the source module if the destination module
11971197 // doesn't have one already.
605605 // Automatically import functions in Module \p DestModule based on the summaries
606606 // index.
607607 //
608 bool FunctionImporter::importFunctions(
608 Expected FunctionImporter::importFunctions(
609609 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList,
610610 bool ForceImportReferencedDiscardableSymbols) {
611611 DEBUG(dbgs() << "Starting import for Module "
629629
630630 // If modules were created with lazy metadata loading, materialize it
631631 // now, before linking it (otherwise this will be a noop).
632 SrcModule->materializeMetadata();
632 if (Error Err = SrcModule->materializeMetadata())
633 return std::move(Err);
633634 UpgradeDebugInfo(*SrcModule);
634635
635636 auto &ImportGUIDs = FunctionsToImportPerModule->second;
644645 << " " << F.getName() << " from "
645646 << SrcModule->getSourceFileName() << "\n");
646647 if (Import) {
647 F.materialize();
648 if (Error Err = F.materialize())
649 return std::move(Err);
648650 if (EnableImportMetadata) {
649651 // Add 'thinlto_src_module' metadata for statistics and debugging.
650652 F.setMetadata(
666668 << " " << GV.getName() << " from "
667669 << SrcModule->getSourceFileName() << "\n");
668670 if (Import) {
669 GV.materialize();
671 if (Error Err = GV.materialize())
672 return std::move(Err);
670673 GlobalsToImport.insert(&GV);
671674 }
672675 }
692695 << " " << GO->getName() << " from "
693696 << SrcModule->getSourceFileName() << "\n");
694697 #endif
695 GO->materialize();
698 if (Error Err = GO->materialize())
699 return std::move(Err);
696700 GlobalsToImport.insert(GO);
697 GA.materialize();
701 if (Error Err = GA.materialize())
702 return std::move(Err);
698703 GlobalsToImport.insert(&GA);
699704 }
700705 }
798803 return loadFile(Identifier, M.getContext());
799804 };
800805 FunctionImporter Importer(*Index, ModuleLoader);
801 return Importer.importFunctions(M, ImportList,
802 !DontForceImportReferencedDiscardableSymbols);
806 Expected Result = Importer.importFunctions(
807 M, ImportList, !DontForceImportReferencedDiscardableSymbols);
808
809 // FIXME: Probably need to propagate Errors through the pass manager.
810 if (!Result) {
811 logAllUnhandledErrors(Result.takeError(), errs(),
812 "Error importing module: ");
813 return false;
814 }
815
816 return *Result;
803817 }
804818
805819 namespace {
417417
418418 // If not jitting lazily, load the whole bitcode file eagerly too.
419419 if (NoLazyCompilation) {
420 if (std::error_code EC = Mod->materializeAll()) {
421 errs() << argv[0] << ": bitcode didn't read correctly.\n";
422 errs() << "Reason: " << EC.message() << "\n";
423 exit(1);
424 }
420 ExitOnError ExitOnErr(std::string(argv[0]) +
421 ": bitcode didn't read correctly: ");
422 ExitOnErr(Mod->materializeAll());
425423 }
426424
427425 std::string ErrorMsg;
136136 exit(1);
137137 }
138138
139 static Expected> openInputFile(LLVMContext &Context) {
140 ErrorOr> MBOrErr =
141 MemoryBuffer::getFileOrSTDIN(InputFilename);
142 if (!MBOrErr)
143 return errorCodeToError(MBOrErr.getError());
144 ErrorOr> MOrErr =
145 getOwningLazyBitcodeModule(std::move(*MBOrErr), Context,
146 /*ShouldLazyLoadMetadata=*/true);
147 if (!MOrErr)
148 return errorCodeToError(MOrErr.getError());
139 static ExitOnError ExitOnErr;
140
141 static std::unique_ptr openInputFile(LLVMContext &Context) {
142 std::unique_ptr MB =
143 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
144 std::unique_ptr M = ExitOnErr(errorOrToExpected(
145 getOwningLazyBitcodeModule(std::move(MB), Context,
146 /*ShouldLazyLoadMetadata=*/true)));
149147 if (MaterializeMetadata)
150 (*MOrErr)->materializeMetadata();
148 ExitOnErr(M->materializeMetadata());
151149 else
152 (*MOrErr)->materializeAll();
153 return std::move(*MOrErr);
150 ExitOnErr(M->materializeAll());
151 return M;
154152 }
155153
156154 int main(int argc, char **argv) {
157155 // Print a stack trace if we signal out.
158156 sys::PrintStackTraceOnErrorSignal(argv[0]);
159157 PrettyStackTraceProgram X(argc, argv);
158
159 ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
160160
161161 LLVMContext Context;
162162 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
2121 #include "llvm/IRReader/IRReader.h"
2222 #include "llvm/IR/LegacyPassManager.h"
2323 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Error.h"
2425 #include "llvm/Support/FileSystem.h"
2526 #include "llvm/Support/ManagedStatic.h"
2627 #include "llvm/Support/PrettyStackTrace.h"
221222 }
222223 }
223224
224 auto Materialize = [&](GlobalValue &GV) {
225 if (std::error_code EC = GV.materialize()) {
226 errs() << argv[0] << ": error reading input: " << EC.message() << "\n";
227 exit(1);
228 }
229 };
225 ExitOnError ExitOnErr(std::string(argv[0]) + ": error reading input: ");
226
227 auto Materialize = [&](GlobalValue &GV) { ExitOnErr(GV.materialize()); };
230228
231229 // Materialize requisite global values.
232230 if (!DeleteFn) {
250248 // Now that we have all the GVs we want, mark the module as fully
251249 // materialized.
252250 // FIXME: should the GVExtractionPass handle this?
253 M->materializeAll();
251 ExitOnErr(M->materializeAll());
254252 }
255253
256254 // In addition to deleting all other functions, we also want to spiff it
107107 cl::desc("Preserve use-list order when writing LLVM assembly."),
108108 cl::init(false), cl::Hidden);
109109
110 static ExitOnError ExitOnErr;
111
110112 // Read the specified bitcode file in and return it. This routine searches the
111113 // link path for the specified file to try to find it...
112114 //
128130 }
129131
130132 if (MaterializeMetadata) {
131 Result->materializeMetadata();
133 ExitOnErr(Result->materializeMetadata());
132134 UpgradeDebugInfo(*Result);
133135 }
134136
263265 auto &Entry = ModuleToGlobalsToImportMap[SrcModule.getModuleIdentifier()];
264266 Entry.insert(F);
265267
266 F->materialize();
268 ExitOnErr(F->materialize());
267269 }
268270
269271 // Do the actual import of globals now, one Module at a time
276278
277279 // If modules were created with lazy metadata loading, materialize it
278280 // now, before linking it (otherwise this will be a noop).
279 SrcModule->materializeMetadata();
281 ExitOnErr(SrcModule->materializeMetadata());
280282 UpgradeDebugInfo(*SrcModule);
281283
282284 // Linkage Promotion and renaming
347349 sys::PrintStackTraceOnErrorSignal(argv[0]);
348350 PrettyStackTraceProgram X(argc, argv);
349351
352 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
353
350354 LLVMContext Context;
351355 Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true);
352356
1818 #include "llvm/IR/Module.h"
1919 #include "llvm/IR/Verifier.h"
2020 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Error.h"
2122 #include "llvm/Support/MemoryBuffer.h"
2223 #include "llvm/Support/SourceMgr.h"
2324 #include "gtest/gtest.h"
8990 EXPECT_FALSE(verifyModule(*M, &dbgs()));
9091
9192 // Materialize h.
92 H->materialize();
93 ASSERT_FALSE(H->materialize());
9394 EXPECT_TRUE(F->empty());
9495 EXPECT_TRUE(G->empty());
9596 EXPECT_FALSE(H->empty());
9798 EXPECT_FALSE(verifyModule(*M, &dbgs()));
9899
99100 // Materialize g.
100 G->materialize();
101 ASSERT_FALSE(G->materialize());
101102 EXPECT_TRUE(F->empty());
102103 EXPECT_FALSE(G->empty());
103104 EXPECT_FALSE(H->empty());
105106 EXPECT_FALSE(verifyModule(*M, &dbgs()));
106107
107108 // Materialize j.
108 J->materialize();
109 ASSERT_FALSE(J->materialize());
109110 EXPECT_TRUE(F->empty());
110111 EXPECT_FALSE(G->empty());
111112 EXPECT_FALSE(H->empty());
113114 EXPECT_FALSE(verifyModule(*M, &dbgs()));
114115
115116 // Materialize f.
116 F->materialize();
117 ASSERT_FALSE(F->materialize());
117118 EXPECT_FALSE(F->empty());
118119 EXPECT_FALSE(G->empty());
119120 EXPECT_FALSE(H->empty());