Skip to content

Conversation

@rsuderman
Copy link
Contributor

Add memstream.h with MemStream and FprintAdapter classes that provide a FILE* interface backed by memory. On Linux, uses open_memstream. On Windows, uses a temporary file approach for compatibility.

Update logging.h to use the new cross-platform FprintAdapter, making FprintToString an alias for backward compatibility.

Add memstream.h with MemStream and FprintAdapter classes that provide
a FILE* interface backed by memory. On Linux, uses open_memstream.
On Windows, uses a temporary file approach for compatibility.

Update logging.h to use the new cross-platform FprintAdapter,
making FprintToString an alias for backward compatibility.

Signed-off-by: Rob Suderman <[email protected]>
Copy link
Member

@sjain-stanford sjain-stanford left a comment

Choose a reason for hiding this comment

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

LGTM modulo comments. FprintToString was initially added by @AaronStGeorge so would be great if Aaron can chime in (post-merge is fine too).

@@ -0,0 +1,219 @@
// Copyright 2025 Advanced Micro Devices, Inc.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Copyright 2025 Advanced Micro Devices, Inc.
// Copyright 2026 Advanced Micro Devices, Inc.

// On Windows, it uses a pure C++ implementation with temporary files.
//
//===----------------------------------------------------------------------===//

Copy link
Member

Choose a reason for hiding this comment

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

Please include memstream.h, target_platform.h and python_utils.h in the main fusilli.h.

@@ -0,0 +1,175 @@
// Copyright 2025 Advanced Micro Devices, Inc.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Copyright 2025 Advanced Micro Devices, Inc.
// Copyright 2026 Advanced Micro Devices, Inc.

Comment on lines +18 to +23
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>

#include "fusilli/support/target_platform.h"
Copy link
Member

Choose a reason for hiding this comment

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

nit: Move fusilli include up before the system includes

https://llvm.org/docs/CodingStandards.html#include-style

Comment on lines +59 to +64
if (GetTempPathA(MAX_PATH, tempPath) == 0) {
return;
}
if (GetTempFileNameA(tempPath, "mem", 0, tempFileName) == 0) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

bool isValid() const { return stream_ != nullptr; }

// Retrieve the contents as a string.
std::string str() {
Copy link
Member

Choose a reason for hiding this comment

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

Several file operations here could fail - fflush, fseek. Consider checking for success or returning empty on failure.

}

// Get current size of the stream.
size_t size() {
Copy link
Member

Choose a reason for hiding this comment

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

same (consider error checking / returning zero size on failure)

bool isValid() const { return stream_ != nullptr; }

// Retrieve the contents as a string.
std::string str() {
Copy link
Member

Choose a reason for hiding this comment

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

Both str and size have side effects (fflush modifies internal state) despite it not being apparent from the name. Consider documenting this in the docstring.

Comment on lines +182 to +188
// Example usage:
// std::string output;
// {
// FprintAdapter adapter(output);
// fprintf(adapter, "Value: %d", 42);
// } // adapter destructor copies content to output
// // output now contains "Value: 42"
Copy link
Member

Choose a reason for hiding this comment

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

Since this is a potential footgun, worth documenting that if nothing is written (no fprintf), it clears any output previously stored

std::string output = "initial";
{
  FprintAdapter adapter(output);
}
// output is now empty ""

fwrite(data, 1, sizeof(data) - 1, ms);
std::string result = ms.str();
REQUIRE(result.size() == sizeof(data) - 1);
REQUIRE(memcmp(result.data(), data, sizeof(data) - 1) == 0);
Copy link
Member

Choose a reason for hiding this comment

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

minor nit: could be simplified as

REQUIRE(result == std::string(data, sizeof(data) - 1));

Comment on lines +26 to +27
#include <fcntl.h>
#include <vector>
Copy link
Member

Choose a reason for hiding this comment

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

Are these both unused?

#include <fcntl.h>
#include <vector>
#elif FUSILLI_PLATFORM_WINDOWS
#include <stdio.h>
Copy link
Member

Choose a reason for hiding this comment

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

nit: maybe redundant (since you have up top)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants