Skip to content

Unittests and usability for BitstreamWriter incremental flushing #92983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 0 additions & 53 deletions llvm/include/llvm/Bitcode/BitcodeWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,59 +68,6 @@ class BitcodeWriter {
/// should be called after copying the module itself into the bitcode file.
void copyStrtab(StringRef Strtab);

/// Write the specified module to the buffer specified at construction time.
///
/// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
/// Value in \c M. These will be reconstructed exactly when \a M is
/// deserialized.
///
/// If \c Index is supplied, the bitcode will contain the summary index
/// (currently for use in ThinLTO optimization).
///
/// \p GenerateHash enables hashing the Module and including the hash in the
/// bitcode (currently for use in ThinLTO incremental build).
///
/// If \p ModHash is non-null, when GenerateHash is true, the resulting
/// hash is written into ModHash. When GenerateHash is false, that value
/// is used as the hash instead of computing from the generated bitcode.
/// Can be used to produce the same module hash for a minimized bitcode
/// used just for the thin link as in the regular full bitcode that will
/// be used in the backend.
void writeModule(const Module &M, bool ShouldPreserveUseListOrder = false,
const ModuleSummaryIndex *Index = nullptr,
bool GenerateHash = false, ModuleHash *ModHash = nullptr);

/// Write the specified thin link bitcode file (i.e., the minimized bitcode
/// file) to the buffer specified at construction time. The thin link
/// bitcode file is used for thin link, and it only contains the necessary
/// information for thin link.
///
/// ModHash is for use in ThinLTO incremental build, generated while the
/// IR bitcode file writing.
void writeThinLinkBitcode(const Module &M, const ModuleSummaryIndex &Index,
const ModuleHash &ModHash);

void writeIndex(
const ModuleSummaryIndex *Index,
const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex);
};

/// Write the specified module to the specified raw output stream.
///
/// A reader does not require a symbol table to interpret a bitcode file;
/// the symbol table is needed only to improve link-time performance. So
/// this function may decide not to write a symbol table. It may so decide
/// if, for example, the target is unregistered or the IR is malformed.
void writeSymtab();

/// Write the bitcode file's string table. This must be called exactly once
/// after all modules and the optional symbol table have been written.
void writeStrtab();

/// Copy the string table for another module into this bitcode file. This
/// should be called after copying the module itself into the bitcode file.
void copyStrtab(StringRef Strtab);

/// Write the specified module to the buffer specified at construction time.
///
/// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
Expand Down
7 changes: 6 additions & 1 deletion llvm/include/llvm/Bitstream/BitstreamWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class BitstreamWriter {
/// Owned buffer, used to init Buffer if the provided stream doesn't happen to
/// be a buffer itself.
SmallVector<char, 0> OwnBuffer;
/// Internal buffer. The writer backpatches, so it is efficient to buffer.
/// Internal buffer for unflushed bytes (unless there is no stream to flush
/// to, case in which these are "the bytes"). The writer backpatches, so it is
/// efficient to buffer.
SmallVectorImpl<char> &Buffer;

/// FS - The file stream that Buffer flushes to. If FS is a raw_fd_stream, the
Expand Down Expand Up @@ -216,6 +218,9 @@ class BitstreamWriter {
return;
}

// If we don't have a raw_fd_stream, GetNumOfFlushedBytes() should have
// returned 0, and we shouldn't be here.
assert(fdStream() != nullptr);
// If the byte offset to backpatch is flushed, use seek to backfill data.
// First, save the file position to restore later.
uint64_t CurPos = fdStream()->tell();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this change and at least one below, do we need to invoke fdStream() or can it simply continue to use FD which should have been initialized in the constructor?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could use FD, but we should only be here if fdStream() != nullptr. The reason that happens is not very apparent, though: GetNumOfFlushedBytes returns 0 in that case, which makes the if-check above always succeed. Added an assert and a comment.

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5093,7 +5093,7 @@ void llvm::WriteBitcodeToFile(const Module &M, raw_ostream &Out,
if (TT.isOSDarwin() || TT.isOSBinFormatMachO()) {
// If this is darwin or another generic macho target, reserve space for the
// header. Note that the header is computed *after* the output is known, so
// we currently expliclty use a buffer, write to it, and then subsequently
// we currently explicilty use a buffer, write to it, and then subsequently
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still a typo in that word =)

// flush to Out.
SmallVector<char, 256 * 1024> Buffer;
Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
Expand Down
Loading