llvm.org GIT mirror llvm / 06d6207
Pass a reference to a module to the bitcode writer. This simplifies most callers as they are already using references or std::unique_ptr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325155 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 1 year, 6 months ago
21 changed file(s) with 53 addition(s) and 55 deletion(s). Raw diff Collapse all Expand all
8585 /// Can be used to produce the same module hash for a minimized bitcode
8686 /// used just for the thin link as in the regular full bitcode that will
8787 /// be used in the backend.
88 void writeModule(const Module *M, bool ShouldPreserveUseListOrder = false,
88 void writeModule(const Module &M, bool ShouldPreserveUseListOrder = false,
8989 const ModuleSummaryIndex *Index = nullptr,
9090 bool GenerateHash = false, ModuleHash *ModHash = nullptr);
9191
9696 ///
9797 /// ModHash is for use in ThinLTO incremental build, generated while the
9898 /// IR bitcode file writing.
99 void writeThinLinkBitcode(const Module *M, const ModuleSummaryIndex &Index,
99 void writeThinLinkBitcode(const Module &M, const ModuleSummaryIndex &Index,
100100 const ModuleHash &ModHash);
101101
102102 void writeIndex(
125125 /// Can be used to produce the same module hash for a minimized bitcode
126126 /// used just for the thin link as in the regular full bitcode that will
127127 /// be used in the backend.
128 void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
128 void WriteBitcodeToFile(const Module &M, raw_ostream &Out,
129129 bool ShouldPreserveUseListOrder = false,
130130 const ModuleSummaryIndex *Index = nullptr,
131131 bool GenerateHash = false,
138138 ///
139139 /// ModHash is for use in ThinLTO incremental build, generated while the IR
140140 /// bitcode file writing.
141 void WriteThinLinkBitcodeToFile(const Module *M, raw_ostream &Out,
141 void WriteThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
142142 const ModuleSummaryIndex &Index,
143143 const ModuleHash &ModHash);
144144
2424 if (EC)
2525 return -1;
2626
27 WriteBitcodeToFile(unwrap(M), OS);
27 WriteBitcodeToFile(*unwrap(M), OS);
2828 return 0;
2929 }
3030
3232 int Unbuffered) {
3333 raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
3434
35 WriteBitcodeToFile(unwrap(M), OS);
35 WriteBitcodeToFile(*unwrap(M), OS);
3636 return 0;
3737 }
3838
4444 std::string Data;
4545 raw_string_ostream OS(Data);
4646
47 WriteBitcodeToFile(unwrap(M), OS);
47 WriteBitcodeToFile(*unwrap(M), OS);
4848 return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());
4949 }
169169 public:
170170 /// Constructs a ModuleBitcodeWriterBase object for the given Module,
171171 /// writing to the provided \p Buffer.
172 ModuleBitcodeWriterBase(const Module *M, StringTableBuilder &StrtabBuilder,
172 ModuleBitcodeWriterBase(const Module &M, StringTableBuilder &StrtabBuilder,
173173 BitstreamWriter &Stream,
174174 bool ShouldPreserveUseListOrder,
175175 const ModuleSummaryIndex *Index)
176 : BitcodeWriterBase(Stream, StrtabBuilder), M(*M),
177 VE(*M, ShouldPreserveUseListOrder), Index(Index) {
176 : BitcodeWriterBase(Stream, StrtabBuilder), M(M),
177 VE(M, ShouldPreserveUseListOrder), Index(Index) {
178178 // Assign ValueIds to any callee values in the index that came from
179179 // indirect call profiles and were recorded as a GUID not a Value*
180180 // (which would have been assigned an ID by the ValueEnumerator).
253253 public:
254254 /// Constructs a ModuleBitcodeWriter object for the given Module,
255255 /// writing to the provided \p Buffer.
256 ModuleBitcodeWriter(const Module *M, SmallVectorImpl &Buffer,
256 ModuleBitcodeWriter(const Module &M, SmallVectorImpl &Buffer,
257257 StringTableBuilder &StrtabBuilder,
258258 BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
259259 const ModuleSummaryIndex *Index, bool GenerateHash,
40374037 WroteStrtab = true;
40384038 }
40394039
4040 void BitcodeWriter::writeModule(const Module *M,
4040 void BitcodeWriter::writeModule(const Module &M,
40414041 bool ShouldPreserveUseListOrder,
40424042 const ModuleSummaryIndex *Index,
40434043 bool GenerateHash, ModuleHash *ModHash) {
40474047 // Modules in case it needs to materialize metadata. But the bitcode writer
40484048 // requires that the module is materialized, so we can cast to non-const here,
40494049 // after checking that it is in fact materialized.
4050 assert(M->isMaterialized());
4051 Mods.push_back(const_cast(M));
4050 assert(M.isMaterialized());
4051 Mods.push_back(const_cast(&M));
40524052
40534053 ModuleBitcodeWriter ModuleWriter(M, Buffer, StrtabBuilder, *Stream,
40544054 ShouldPreserveUseListOrder, Index,
40644064 IndexWriter.write();
40654065 }
40664066
4067 /// WriteBitcodeToFile - Write the specified module to the specified output
4068 /// stream.
4069 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
4067 /// Write the specified module to the specified output stream.
4068 void llvm::WriteBitcodeToFile(const Module &M, raw_ostream &Out,
40704069 bool ShouldPreserveUseListOrder,
40714070 const ModuleSummaryIndex *Index,
40724071 bool GenerateHash, ModuleHash *ModHash) {
40754074
40764075 // If this is darwin or another generic macho target, reserve space for the
40774076 // header.
4078 Triple TT(M->getTargetTriple());
4077 Triple TT(M.getTargetTriple());
40794078 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
40804079 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
40814080
41324131 const ModuleHash *ModHash;
41334132
41344133 public:
4135 ThinLinkBitcodeWriter(const Module *M, StringTableBuilder &StrtabBuilder,
4134 ThinLinkBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder,
41364135 BitstreamWriter &Stream,
41374136 const ModuleSummaryIndex &Index,
41384137 const ModuleHash &ModHash)
42504249 Stream.ExitBlock();
42514250 }
42524251
4253 void BitcodeWriter::writeThinLinkBitcode(const Module *M,
4252 void BitcodeWriter::writeThinLinkBitcode(const Module &M,
42544253 const ModuleSummaryIndex &Index,
42554254 const ModuleHash &ModHash) {
42564255 assert(!WroteStrtab);
42594258 // Modules in case it needs to materialize metadata. But the bitcode writer
42604259 // requires that the module is materialized, so we can cast to non-const here,
42614260 // after checking that it is in fact materialized.
4262 assert(M->isMaterialized());
4263 Mods.push_back(const_cast(M));
4261 assert(M.isMaterialized());
4262 Mods.push_back(const_cast(&M));
42644263
42654264 ThinLinkBitcodeWriter ThinLinkWriter(M, StrtabBuilder, *Stream, Index,
42664265 ModHash);
42704269 // Write the specified thin link bitcode file to the given raw output stream,
42714270 // where it will be written in a new bitcode block. This is used when
42724271 // writing the per-module index file for ThinLTO.
4273 void llvm::WriteThinLinkBitcodeToFile(const Module *M, raw_ostream &Out,
4272 void llvm::WriteThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
42744273 const ModuleSummaryIndex &Index,
42754274 const ModuleHash &ModHash) {
42764275 SmallVector Buffer;
2222 const ModuleSummaryIndex *Index =
2323 EmitSummaryIndex ? &(AM.getResult(M))
2424 : nullptr;
25 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
25 WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
2626 return PreservedAnalyses::all();
2727 }
2828
5454 EmitSummaryIndex
5555 ? &(getAnalysis().getIndex())
5656 : nullptr;
57 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, Index,
57 WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index,
5858 EmitModuleHash);
5959 return false;
6060 }
4343
4444 if (OSs.size() == 1) {
4545 if (!BCOSs.empty())
46 WriteBitcodeToFile(M.get(), *BCOSs[0]);
46 WriteBitcodeToFile(*M, *BCOSs[0]);
4747 codegen(M.get(), *OSs[0], TMFactory, FileType);
4848 return M;
4949 }
6565 // FIXME: Provide a more direct way to do this in LLVM.
6666 SmallString<0> BC;
6767 raw_svector_ostream BCOS(BC);
68 WriteBitcodeToFile(MPart.get(), BCOS);
68 WriteBitcodeToFile(*MPart, BCOS);
6969
7070 if (!BCOSs.empty()) {
7171 BCOSs[ThreadCount]->write(BC.begin(), BC.size());
168168 std::string Buf;
169169 {
170170 raw_string_ostream OS(Buf);
171 WriteBitcodeToFile(&M, OS);
171 WriteBitcodeToFile(M, OS);
172172 }
173173 if (Buf.size() > MaxSize)
174174 return 0;
8181 // directly and exit.
8282 if (EC)
8383 reportOpenError(Path, EC.message());
84 WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false);
84 WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false);
8585 return true;
8686 };
8787 };
308308 // FIXME: Provide a more direct way to do this in LLVM.
309309 SmallString<0> BC;
310310 raw_svector_ostream BCOS(BC);
311 WriteBitcodeToFile(MPart.get(), BCOS);
311 WriteBitcodeToFile(*MPart, BCOS);
312312
313313 // Enqueue the task
314314 CodegenThreadPool.async(
219219 }
220220
221221 // write bitcode to it
222 WriteBitcodeToFile(MergedModule.get(), Out.os(), ShouldEmbedUselists);
222 WriteBitcodeToFile(*MergedModule, Out.os(), ShouldEmbedUselists);
223223 Out.os().close();
224224
225225 if (Out.os().has_error()) {
8181 if (EC)
8282 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
8383 " to save optimized bitcode\n");
84 WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
84 WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
8585 }
8686
8787 static const GlobalValueSummary *
475475 raw_svector_ostream OS(OutputBuffer);
476476 ProfileSummaryInfo PSI(TheModule);
477477 auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
478 WriteBitcodeToFile(&TheModule, OS, true, &Index);
478 WriteBitcodeToFile(TheModule, OS, true, &Index);
479479 }
480480 return make_unique(std::move(OutputBuffer));
481481 }
201201 if (ModuleId.empty()) {
202202 // We couldn't generate a module ID for this module, just write it out as a
203203 // regular LTO module.
204 WriteBitcodeToFile(&M, OS);
204 WriteBitcodeToFile(M, OS);
205205 if (ThinLinkOS)
206206 // We don't have a ThinLTO part, but still write the module to the
207207 // ThinLinkOS if requested so that the expected output file is produced.
208 WriteBitcodeToFile(&M, *ThinLinkOS);
208 WriteBitcodeToFile(M, *ThinLinkOS);
209209 return;
210210 }
211211
373373 // be used in the backends, and use that in the minimized bitcode
374374 // produced for the full link.
375375 ModuleHash ModHash = {{0}};
376 W.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index,
376 W.writeModule(M, /*ShouldPreserveUseListOrder=*/false, &Index,
377377 /*GenerateHash=*/true, &ModHash);
378 W.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false,
379 &MergedMIndex);
378 W.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false, &MergedMIndex);
380379 W.writeSymtab();
381380 W.writeStrtab();
382381 OS << Buffer;
388387 Buffer.clear();
389388 BitcodeWriter W2(Buffer);
390389 StripDebugInfo(M);
391 W2.writeThinLinkBitcode(&M, Index, ModHash);
392 W2.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false,
390 W2.writeThinLinkBitcode(M, Index, ModHash);
391 W2.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false,
393392 &MergedMIndex);
394393 W2.writeSymtab();
395394 W2.writeStrtab();
422421 // be used in the backends, and use that in the minimized bitcode
423422 // produced for the full link.
424423 ModuleHash ModHash = {{0}};
425 WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false, Index,
424 WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, Index,
426425 /*GenerateHash=*/true, &ModHash);
427426 // If a minimized bitcode module was requested for the thin link, only
428427 // the information that is needed by thin link will be written in the
429428 // given OS.
430429 if (ThinLinkOS && Index)
431 WriteThinLinkBitcodeToFile(&M, *ThinLinkOS, *Index, ModHash);
430 WriteThinLinkBitcodeToFile(M, *ThinLinkOS, *Index, ModHash);
432431 }
433432
434433 class WriteThinLTOBitcode : public ModulePass {
5151 /// file. If an error occurs, true is returned.
5252 ///
5353 static bool writeProgramToFileAux(ToolOutputFile &Out, const Module *M) {
54 WriteBitcodeToFile(M, Out.os(), PreserveBitcodeUseListOrder);
54 WriteBitcodeToFile(*M, Out.os(), PreserveBitcodeUseListOrder);
5555 Out.os().close();
5656 if (!Out.os().has_error()) {
5757 Out.keep();
6868
6969 bool BugDriver::writeProgramToFile(int FD, const Module *M) const {
7070 raw_fd_ostream OS(FD, /*shouldClose*/ false);
71 WriteBitcodeToFile(M, OS, PreserveBitcodeUseListOrder);
71 WriteBitcodeToFile(*M, OS, PreserveBitcodeUseListOrder);
7272 OS.flush();
7373 if (!OS.has_error())
7474 return false;
157157 DiscardTemp Discard{*Temp};
158158 raw_fd_ostream OS(Temp->FD, /*shouldClose*/ false);
159159
160 WriteBitcodeToFile(Program, OS, PreserveBitcodeUseListOrder);
160 WriteBitcodeToFile(*Program, OS, PreserveBitcodeUseListOrder);
161161 OS.flush();
162162 if (OS.has_error()) {
163163 errs() << "Error writing bitcode file: " << Temp->TmpName << "\n";
783783 raw_fd_ostream OS(output_name, EC, sys::fs::OpenFlags::F_None);
784784 if (EC)
785785 message(LDPL_FATAL, "Failed to write the output file.");
786 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ false);
786 WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ false);
787787 return false;
788788 };
789789 break;
8484 }
8585
8686 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
87 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder, nullptr,
87 WriteBitcodeToFile(*M, Out->os(), PreserveBitcodeUseListOrder, nullptr,
8888 EmitModuleHash);
8989
9090 // Declare success.
7272 Err.print(argv[0], errs());
7373 return 1;
7474 }
75 Writer.writeModule(M.get());
75 Writer.writeModule(*M);
7676 OwnedMods.push_back(std::move(M));
7777 }
7878 Writer.writeStrtab();
397397 if (OutputAssembly) {
398398 Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
399399 } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
400 WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder);
400 WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder);
401401
402402 // Declare success.
403403 Out.keep();
461461 raw_fd_ostream OS(Filename, EC, sys::fs::OpenFlags::F_None);
462462 error(EC, "error opening the file '" + Filename + "'");
463463 maybeVerifyModule(TheModule);
464 WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
464 WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
465465 }
466466
467467 class ThinLTOProcessing {
6969 }
7070
7171 std::unique_ptr M = ExitOnErr(Ms[ModuleIndex].parseModule(Context));
72 WriteBitcodeToFile(M.get(), Out->os());
72 WriteBitcodeToFile(*M, Out->os());
7373
7474 Out->keep();
7575 return 0;
7676 std::string Buf;
7777 {
7878 raw_string_ostream OS(Buf);
79 WriteBitcodeToFile(M.get(), OS);
79 WriteBitcodeToFile(*M, OS);
8080 }
8181 if (Buf.size() > MaxSize)
8282 return 0;
6262 }
6363
6464 verifyModule(*MPart);
65 WriteBitcodeToFile(MPart.get(), Out->os());
65 WriteBitcodeToFile(*MPart, Out->os());
6666
6767 // Declare success.
6868 Out->keep();
131131 return true;
132132 }
133133
134 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
134 WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
135135 return false;
136136 }
137137
4343 static void writeModuleToBuffer(std::unique_ptr Mod,
4444 SmallVectorImpl &Buffer) {
4545 raw_svector_ostream OS(Buffer);
46 WriteBitcodeToFile(Mod.get(), OS);
46 WriteBitcodeToFile(*Mod, OS);
4747 }
4848
4949 static std::unique_ptr getLazyModuleFromAssembly(LLVMContext &Context,