llvm.org GIT mirror llvm / 9053d35
[ThinLTO] Make a copy of buffer identifier in ThinLTOCodeGenerator We can't assume that the `const char *` provided through libLTO has a lifetime that expands beyond the codegenerator itself. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@295018 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
2 changed file(s) with 35 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
2929 class StringRef;
3030 class LLVMContext;
3131 class TargetMachine;
32
33 /// Wrapper around MemoryBufferRef, owning the identifier
34 class ThinLTOBuffer {
35 std::string OwnedIdentifier;
36 StringRef Buffer;
37
38 public:
39 ThinLTOBuffer(StringRef Buffer, StringRef Identifier)
40 : OwnedIdentifier(Identifier), Buffer(Buffer) {}
41
42 MemoryBufferRef getMemBuffer() const {
43 return MemoryBufferRef(Buffer,
44 {OwnedIdentifier.c_str(), OwnedIdentifier.size()});
45 }
46 StringRef getBuffer() const { return Buffer; }
47 StringRef getBufferIdentifier() const { return OwnedIdentifier; }
48 };
3249
3350 /// Helper to gather options relevant to the target machine creation
3451 struct TargetMachineBuilder {
279296
280297 /// Vector holding the input buffers containing the bitcode modules to
281298 /// process.
282 std::vector<MemoryBufferRef> Modules;
299 std::vector<ThinLTOBuffer> Modules;
283300
284301 /// Set of symbols that need to be preserved outside of the set of bitcode
285302 /// files.
128128 }
129129
130130 static StringMap
131 generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
131 generateModuleMap(const std::vector<ThinLTOBuffer> &Modules) {
132132 StringMap ModuleMap;
133133 for (auto &ModuleBuffer : Modules) {
134134 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
135135 ModuleMap.end() &&
136136 "Expect unique Buffer Identifier");
137 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
137 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer.getMemBuffer();
138138 }
139139 return ModuleMap;
140140 }
500500 } // end anonymous namespace
501501
502502 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
503 MemoryBufferRef Buffer(Data, Identifier);
503 ThinLTOBuffer Buffer(Data, Identifier);
504504 if (Modules.empty()) {
505505 // First module added, so initialize the triple and some options
506506 LLVMContext Context;
507507 StringRef TripleStr;
508 ErrorOr TripleOrErr =
509 expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(Buffer));
508 ErrorOr TripleOrErr = expectedToErrorOrAndEmitErrors(
509 Context, getBitcodeTargetTriple(Buffer.getMemBuffer()));
510510 if (TripleOrErr)
511511 TripleStr = *TripleOrErr;
512512 Triple TheTriple(TripleStr);
516516 else {
517517 LLVMContext Context;
518518 StringRef TripleStr;
519 ErrorOr TripleOrErr =
520 expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(Buffer));
519 ErrorOr TripleOrErr = expectedToErrorOrAndEmitErrors(
520 Context, getBitcodeTargetTriple(Buffer.getMemBuffer()));
521521 if (TripleOrErr)
522522 TripleStr = *TripleOrErr;
523523 assert(TMBuilder.TheTriple.str() == TripleStr &&
566566 uint64_t NextModuleId = 0;
567567 for (auto &ModuleBuffer : Modules) {
568568 Expected> ObjOrErr =
569 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer);
569 object::ModuleSummaryIndexObjectFile::create(
570 ModuleBuffer.getMemBuffer());
570571 if (!ObjOrErr) {
571572 // FIXME diagnose
572573 logAllUnhandledErrors(
830831 Context.setDiscardValueNames(LTODiscardValueNames);
831832
832833 // Parse module now
833 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false,
834 /*IsImporting*/ false);
834 auto TheModule =
835 loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
836 /*IsImporting*/ false);
835837
836838 // CodeGen
837839 auto OutputBuffer = codegen(*TheModule);
921923 std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
922924 std::sort(ModulesOrdering.begin(), ModulesOrdering.end(),
923925 [&](int LeftIndex, int RightIndex) {
924 auto LSize = Modules[LeftIndex].getBufferSize();
925 auto RSize = Modules[RightIndex].getBufferSize();
926 auto LSize = Modules[LeftIndex].getBuffer().size();
927 auto RSize = Modules[RightIndex].getBuffer().size();
926928 return LSize > RSize;
927929 });
928930
975977 }
976978
977979 // Parse module now
978 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false,
979 /*IsImporting*/ false);
980 auto TheModule =
981 loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
982 /*IsImporting*/ false);
980983
981984 // Save temps: original file.
982985 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");