Skip to content

[mlir] BytecodeWriter: invoke reserveExtraSpace #126953

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 4 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ class RawEmitterOstream : public raw_ostream {
} // namespace

void EncodingEmitter::writeTo(raw_ostream &os) const {
// Reserve space in the ostream for the encoded contents.
os.reserveExtraSpace(size());

for (auto &prevResult : prevResultList)
os.write((const char *)prevResult.data(), prevResult.size());
os.write((const char *)currentResult.data(), currentResult.size());
Expand Down
35 changes: 32 additions & 3 deletions mlir/unittests/Bytecode/BytecodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/raw_ostream.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand All @@ -37,6 +38,29 @@ module @TestDialectResources attributes {
#-}
)";

struct MockOstream final : public raw_ostream {
std::unique_ptr<std::byte[]> buffer;
size_t size = 0;

MOCK_METHOD(void, reserveExtraSpace, (uint64_t extraSpace), (override));

MockOstream() : raw_ostream(true) {}
uint64_t current_pos() const override { return pos; }

private:
size_t pos = 0;

void write_impl(const char *ptr, size_t length) override {
if (pos + length <= size) {
memcpy((void *)(buffer.get() + pos), ptr, length);
pos += length;
} else {
report_fatal_error(
"Attempted to write past the end of the fixed size buffer.");
}
}
};

TEST(Bytecode, MultiModuleWithResource) {
MLIRContext context;
Builder builder(&context);
Expand All @@ -45,12 +69,17 @@ TEST(Bytecode, MultiModuleWithResource) {
parseSourceString<Operation *>(irWithResources, parseConfig);
ASSERT_TRUE(module);

// Write the module to bytecode
std::string buffer;
llvm::raw_string_ostream ostream(buffer);
// Write the module to bytecode.
MockOstream ostream;
EXPECT_CALL(ostream, reserveExtraSpace).WillOnce([&](uint64_t space) {
ostream.buffer = std::make_unique<std::byte[]>(space);
ostream.size = space;
});
ASSERT_TRUE(succeeded(writeBytecodeToFile(module.get(), ostream)));

// Create copy of buffer which is aligned to requested resource alignment.
std::string buffer((char *)ostream.buffer.get(),
(char *)ostream.buffer.get() + ostream.size);
constexpr size_t kAlignment = 0x20;
size_t bufferSize = buffer.size();
buffer.reserve(bufferSize + kAlignment - 1);
Expand Down