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+
4064TEST (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