Skip to content

Commit f3a2990

Browse files
authored
[mlir] BytecodeWriter: invoke reserveExtraSpace (#126953)
Update `BytecodeWriter` to invoke `reserveExtraSpace` on the stream before writing to it. This will give clients implementing custom output streams the opportunity to allocate an appropriately sized buffer for the write.
1 parent ceb00c0 commit f3a2990

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

mlir/lib/Bytecode/Writer/BytecodeWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@ class RawEmitterOstream : public raw_ostream {
613613
} // namespace
614614

615615
void EncodingEmitter::writeTo(raw_ostream &os) const {
616+
// Reserve space in the ostream for the encoded contents.
617+
os.reserveExtraSpace(size());
618+
616619
for (auto &prevResult : prevResultList)
617620
os.write((const char *)prevResult.data(), prevResult.size());
618621
os.write((const char *)currentResult.data(), currentResult.size());

mlir/unittests/Bytecode/BytecodeTest.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/StringRef.h"
1818
#include "llvm/Support/Endian.h"
1919
#include "llvm/Support/MemoryBufferRef.h"
20+
#include "llvm/Support/raw_ostream.h"
2021
#include "gmock/gmock.h"
2122
#include "gtest/gtest.h"
2223

@@ -37,6 +38,29 @@ module @TestDialectResources attributes {
3738
#-}
3839
)";
3940

41+
struct MockOstream final : public raw_ostream {
42+
std::unique_ptr<std::byte[]> buffer;
43+
size_t size = 0;
44+
45+
MOCK_METHOD(void, reserveExtraSpace, (uint64_t extraSpace), (override));
46+
47+
MockOstream() : raw_ostream(true) {}
48+
uint64_t current_pos() const override { return pos; }
49+
50+
private:
51+
size_t pos = 0;
52+
53+
void write_impl(const char *ptr, size_t length) override {
54+
if (pos + length <= size) {
55+
memcpy((void *)(buffer.get() + pos), ptr, length);
56+
pos += length;
57+
} else {
58+
report_fatal_error(
59+
"Attempted to write past the end of the fixed size buffer.");
60+
}
61+
}
62+
};
63+
4064
TEST(Bytecode, MultiModuleWithResource) {
4165
MLIRContext context;
4266
Builder builder(&context);
@@ -45,12 +69,17 @@ TEST(Bytecode, MultiModuleWithResource) {
4569
parseSourceString<Operation *>(irWithResources, parseConfig);
4670
ASSERT_TRUE(module);
4771

48-
// Write the module to bytecode
49-
std::string buffer;
50-
llvm::raw_string_ostream ostream(buffer);
72+
// Write the module to bytecode.
73+
MockOstream ostream;
74+
EXPECT_CALL(ostream, reserveExtraSpace).WillOnce([&](uint64_t space) {
75+
ostream.buffer = std::make_unique<std::byte[]>(space);
76+
ostream.size = space;
77+
});
5178
ASSERT_TRUE(succeeded(writeBytecodeToFile(module.get(), ostream)));
5279

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

0 commit comments

Comments
 (0)