};
/// Abstract class to manage the bitcode writing, subclassed for each bitcode
-/// file type. Owns the BitstreamWriter, and includes the main entry point for
-/// writing.
-class BitcodeWriter {
+/// file type.
+class BitcodeWriterBase {
protected:
- /// Pointer to the buffer allocated by caller for bitcode writing.
- const SmallVectorImpl<char> &Buffer;
-
- /// The stream created and owned by the BitodeWriter.
- BitstreamWriter Stream;
+ /// The stream created and owned by the client.
+ BitstreamWriter &Stream;
/// Saves the offset of the VSTOffset record that must eventually be
/// backpatched with the offset of the actual VST.
uint64_t VSTOffsetPlaceholder = 0;
public:
- /// Constructs a BitcodeWriter object, and initializes a BitstreamRecord,
- /// writing to the provided \p Buffer.
- BitcodeWriter(SmallVectorImpl<char> &Buffer)
- : Buffer(Buffer), Stream(Buffer) {}
-
- virtual ~BitcodeWriter() = default;
-
- /// Main entry point to write the bitcode file, which writes the bitcode
- /// header and will then invoke the virtual writeBlocks() method.
- void write();
-
-private:
- /// Derived classes must implement this to write the corresponding blocks for
- /// that bitcode file type.
- virtual void writeBlocks() = 0;
+ /// Constructs a BitcodeWriterBase object that writes to the provided
+ /// \p Stream.
+ BitcodeWriterBase(BitstreamWriter &Stream) : Stream(Stream) {}
protected:
bool hasVSTOffsetPlaceholder() { return VSTOffsetPlaceholder != 0; }
};
/// Class to manage the bitcode writing for a module.
-class ModuleBitcodeWriter : public BitcodeWriter {
+class ModuleBitcodeWriter : public BitcodeWriterBase {
+ /// Pointer to the buffer allocated by caller for bitcode writing.
+ const SmallVectorImpl<char> &Buffer;
+
/// The Module to write to bitcode.
const Module &M;
/// True if a module hash record should be written.
bool GenerateHash;
- /// The start bit of the module block, for use in generating a module hash
- uint64_t BitcodeStartBit = 0;
+ /// The start bit of the identification block.
+ uint64_t BitcodeStartBit;
/// Map that holds the correspondence between GUIDs in the summary index,
/// that came from indirect call profiles, and a value id generated by this
/// Constructs a ModuleBitcodeWriter object for the given Module,
/// writing to the provided \p Buffer.
ModuleBitcodeWriter(const Module *M, SmallVectorImpl<char> &Buffer,
- bool ShouldPreserveUseListOrder,
+ BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
const ModuleSummaryIndex *Index, bool GenerateHash)
- : BitcodeWriter(Buffer), M(*M), VE(*M, ShouldPreserveUseListOrder),
- Index(Index), GenerateHash(GenerateHash) {
- // Save the start bit of the actual bitcode, in case there is space
- // saved at the start for the darwin header above. The reader stream
- // will start at the bitcode, and we need the offset of the VST
- // to line up.
- BitcodeStartBit = Stream.GetCurrentBitNo();
-
+ : BitcodeWriterBase(Stream), Buffer(Buffer), M(*M),
+ VE(*M, ShouldPreserveUseListOrder), Index(Index),
+ GenerateHash(GenerateHash), BitcodeStartBit(Stream.GetCurrentBitNo()) {
// Assign ValueIds to any callee values in the index that came from
// indirect call profiles and were recorded as a GUID not a Value*
// (which would have been assigned an ID by the ValueEnumerator).
assignValueId(CallEdge.first.getGUID());
}
-private:
- /// Main entry point for writing a module to bitcode, invoked by
- /// BitcodeWriter::write() after it writes the header.
- void writeBlocks() override;
-
- /// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
- /// current llvm version, and a record for the epoch number.
- void writeIdentificationBlock();
-
/// Emit the current module to the bitstream.
- void writeModule();
+ void write();
+private:
uint64_t bitcodeStartBit() { return BitcodeStartBit; }
- void writeStringRecord(unsigned Code, StringRef Str, unsigned AbbrevToUse);
void writeAttributeGroupTable();
void writeAttributeTable();
void writeTypeTable();
};
/// Class to manage the bitcode writing for a combined index.
-class IndexBitcodeWriter : public BitcodeWriter {
+class IndexBitcodeWriter : public BitcodeWriterBase {
/// The combined index to write to bitcode.
const ModuleSummaryIndex &Index;
/// Constructs a IndexBitcodeWriter object for the given combined index,
/// writing to the provided \p Buffer. When writing a subset of the index
/// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
- IndexBitcodeWriter(SmallVectorImpl<char> &Buffer,
- const ModuleSummaryIndex &Index,
+ IndexBitcodeWriter(BitstreamWriter &Stream, const ModuleSummaryIndex &Index,
const std::map<std::string, GVSummaryMapTy>
*ModuleToSummariesForIndex = nullptr)
- : BitcodeWriter(Buffer), Index(Index),
+ : BitcodeWriterBase(Stream), Index(Index),
ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
// Assign unique value ids to all summaries to be written, for use
// in writing out the call graph edges. Save the mapping from GUID
/// Obtain the end iterator over the summaries to be written.
iterator end() { return iterator(*this, /*IsAtEnd=*/true); }
-private:
- /// Main entry point for writing a combined index to bitcode, invoked by
- /// BitcodeWriter::write() after it writes the header.
- void writeBlocks() override;
+ /// Main entry point for writing a combined index to bitcode.
+ void write();
+private:
void writeIndex();
void writeModStrings();
void writeCombinedValueSymbolTable();
llvm_unreachable("Invalid synch scope");
}
-void ModuleBitcodeWriter::writeStringRecord(unsigned Code, StringRef Str,
- unsigned AbbrevToUse) {
+static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
+ StringRef Str, unsigned AbbrevToUse) {
SmallVector<unsigned, 64> Vals;
// Code: [strchar x N]
// Emit the name if it is present.
if (!ST->getName().empty())
- writeStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
+ writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
StructNameAbbrev);
}
break;
/// Write a record that will eventually hold the word offset of the
/// module-level VST. For now the offset is 0, which will be backpatched
/// after the real VST is written. Saves the bit offset to backpatch.
-void BitcodeWriter::writeValueSymbolTableForwardDecl() {
+void BitcodeWriterBase::writeValueSymbolTableForwardDecl() {
// Write a placeholder value in for the offset of the real VST,
// which is written after the function blocks so that it can include
// the offset of each function. The placeholder offset will be
void ModuleBitcodeWriter::writeModuleInfo() {
// Emit various pieces of data attached to a module.
if (!M.getTargetTriple().empty())
- writeStringRecord(bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
+ writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
0 /*TODO*/);
const std::string &DL = M.getDataLayoutStr();
if (!DL.empty())
- writeStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
+ writeStringRecord(Stream, bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
if (!M.getModuleInlineAsm().empty())
- writeStringRecord(bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
+ writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
0 /*TODO*/);
// Emit information about sections and GC, computing how many there are. Also
// Give section names unique ID's.
unsigned &Entry = SectionMap[GV.getSection()];
if (!Entry) {
- writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
+ writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
0 /*TODO*/);
Entry = SectionMap.size();
}
// Give section names unique ID's.
unsigned &Entry = SectionMap[F.getSection()];
if (!Entry) {
- writeStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
+ writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
0 /*TODO*/);
Entry = SectionMap.size();
}
// Same for GC names.
unsigned &Entry = GCMap[F.getGC()];
if (!Entry) {
- writeStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(), 0 /*TODO*/);
+ writeStringRecord(Stream, bitc::MODULE_CODE_GCNAME, F.getGC(),
+ 0 /*TODO*/);
Entry = GCMap.size();
}
}
// Get the offset of the VST we are writing, and backpatch it into
// the VST forward declaration record.
uint64_t VSTOffset = Stream.GetCurrentBitNo();
- // The BitcodeStartBit was the stream offset of the actual bitcode
- // (e.g. excluding any initial darwin header).
+ // The BitcodeStartBit was the stream offset of the identification block.
VSTOffset -= bitcodeStartBit();
assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
- Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
+ // Note that we add 1 here because the offset is relative to one word
+ // before the start of the identification block, which was historically
+ // always the start of the regular bitcode header.
+ Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
}
Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
// actual bitcode written to the stream).
uint64_t BitcodeIndex = (*FunctionToBitcodeIndex)[F] - bitcodeStartBit();
assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
- NameVals.push_back(BitcodeIndex / 32);
+ // Note that we add 1 here because the offset is relative to one word
+ // before the start of the identification block, which was historically
+ // always the start of the regular bitcode header.
+ NameVals.push_back(BitcodeIndex / 32 + 1);
Code = bitc::VST_CODE_FNENTRY;
AbbrevToUse = FnEntry8BitAbbrev;
Stream.ExitBlock();
}
-void ModuleBitcodeWriter::writeIdentificationBlock() {
+/// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
+/// current llvm version, and a record for the epoch number.
+void writeIdentificationBlock(BitstreamWriter &Stream) {
Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
// Write the "user readable" string identifying the bitcode producer
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
auto StringAbbrev = Stream.EmitAbbrev(Abbv);
- writeStringRecord(bitc::IDENTIFICATION_CODE_STRING,
+ writeStringRecord(Stream, bitc::IDENTIFICATION_CODE_STRING,
"LLVM" LLVM_VERSION_STRING, StringAbbrev);
// Write the epoch version
Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
}
-void BitcodeWriter::write() {
- // Emit the file header first.
- writeBitcodeHeader();
-
- writeBlocks();
-}
-
-void ModuleBitcodeWriter::writeBlocks() {
- writeIdentificationBlock();
- writeModule();
-}
-
-void IndexBitcodeWriter::writeBlocks() {
- // Index contains only a single outer (module) block.
- writeIndex();
-}
+void ModuleBitcodeWriter::write() {
+ writeIdentificationBlock(Stream);
-void ModuleBitcodeWriter::writeModule() {
Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
size_t BlockStartPos = Buffer.size();
}
/// Helper to write the header common to all bitcode files.
-void BitcodeWriter::writeBitcodeHeader() {
+static void writeBitcodeHeader(BitstreamWriter &Stream) {
// Emit the file header.
Stream.Emit((unsigned)'B', 8);
Stream.Emit((unsigned)'C', 8);
Stream.Emit(0xD, 4);
}
+BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer)
+ : Buffer(Buffer), Stream(new BitstreamWriter(Buffer)) {
+ writeBitcodeHeader(*Stream);
+}
+
+BitcodeWriter::~BitcodeWriter() = default;
+
+void BitcodeWriter::writeModule(const Module *M,
+ bool ShouldPreserveUseListOrder,
+ const ModuleSummaryIndex *Index,
+ bool GenerateHash) {
+ ModuleBitcodeWriter ModuleWriter(
+ M, Buffer, *Stream, ShouldPreserveUseListOrder, Index, GenerateHash);
+ ModuleWriter.write();
+}
+
/// WriteBitcodeToFile - Write the specified module to the specified output
/// stream.
void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
- // Emit the module into the buffer.
- ModuleBitcodeWriter ModuleWriter(M, Buffer, ShouldPreserveUseListOrder, Index,
- GenerateHash);
- ModuleWriter.write();
+ BitcodeWriter Writer(Buffer);
+ Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash);
if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
emitDarwinBCHeaderAndTrailer(Buffer, TT);
Out.write((char*)&Buffer.front(), Buffer.size());
}
-void IndexBitcodeWriter::writeIndex() {
+void IndexBitcodeWriter::write() {
Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
SmallVector<unsigned, 1> Vals;
SmallVector<char, 0> Buffer;
Buffer.reserve(256 * 1024);
- IndexBitcodeWriter IndexWriter(Buffer, Index, ModuleToSummariesForIndex);
+ BitstreamWriter Stream(Buffer);
+ writeBitcodeHeader(Stream);
+
+ IndexBitcodeWriter IndexWriter(Stream, Index, ModuleToSummariesForIndex);
IndexWriter.write();
Out.write((char *)&Buffer.front(), Buffer.size());