[Mlir-commits] [mlir] 5164574 - [MLIR][Remark] Report why the LLVM remark streamer could not be created (#230130)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Oct 15 04:25:08 PDT 2026
Author: Henrich Lauko
Date: 2026-10-15T13:25:02+02:00
New Revision: 516457489fee0763fc5e2f9baa1f55098547d2d4
URL: https://github.com/llvm/llvm-project/commit/516457489fee0763fc5e2f9baa1f55098547d2d4
DIFF: https://github.com/llvm/llvm-project/commit/516457489fee0763fc5e2f9baa1f55098547d2d4.diff
LOG: [MLIR][Remark] Report why the LLVM remark streamer could not be created (#230130)
`LLVMRemarkStreamer::createToFile` dropped the reason it failed. A file
that could not be opened and a serializer that could not be created both
became a bare failure, so `enableOptimizationRemarksWithLLVMStreamer`
failed without a diagnostic.
- `createToFile` now also returns a reason of a failure as unexpected
result.
- `enableOptimizationRemarksWithLLVMStreamer` now emits the error to the
context.
Assisted-by: Claude Code (Claude Opus 5.5)
Added:
mlir/test/Pass/remark-output-error.mlir
Modified:
mlir/include/mlir/Remark/RemarkStreamer.h
mlir/lib/Remark/CMakeLists.txt
mlir/lib/Remark/RemarkStreamer.cpp
mlir/unittests/IR/RemarkTest.cpp
utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
Removed:
################################################################################
diff --git a/mlir/include/mlir/Remark/RemarkStreamer.h b/mlir/include/mlir/Remark/RemarkStreamer.h
index 19a70fa4c4daad..f5b1086152c20d 100644
--- a/mlir/include/mlir/Remark/RemarkStreamer.h
+++ b/mlir/include/mlir/Remark/RemarkStreamer.h
@@ -14,6 +14,7 @@
#include "mlir/IR/Remarks.h"
#include "llvm/Remarks/RemarkStreamer.h"
+#include "llvm/Support/Error.h"
#include "llvm/Support/ToolOutputFile.h"
namespace mlir::remark::detail {
@@ -22,7 +23,8 @@ namespace mlir::remark::detail {
/// (YAML or Bitstream). Lives outside core.
class LLVMRemarkStreamer final : public MLIRRemarkStreamerBase {
public:
- static FailureOr<std::unique_ptr<MLIRRemarkStreamerBase>>
+ /// Open \p path and create the serializer for \p fmt.
+ static llvm::Expected<std::unique_ptr<MLIRRemarkStreamerBase>>
createToFile(llvm::StringRef path, llvm::remarks::Format fmt);
void streamOptimizationRemark(const Remark &remark) override;
@@ -42,7 +44,8 @@ namespace mlir::remark {
/// Enable optimization remarks to a file with the given path and format.
/// The remark categories are used to filter the remarks that are emitted.
/// If the printAsEmitRemarks flag is set, remarks will also be printed using
-/// mlir::emitRemarks.
+/// mlir::emitRemarks. If the file cannot be opened or the serializer cannot be
+/// created, an error is emitted on the context.
LogicalResult enableOptimizationRemarksWithLLVMStreamer(
MLIRContext &ctx, StringRef filePath, llvm::remarks::Format fmt,
std::unique_ptr<detail::RemarkEmittingPolicyBase> remarkEmittingPolicy,
diff --git a/mlir/lib/Remark/CMakeLists.txt b/mlir/lib/Remark/CMakeLists.txt
index a75c17288d5b6c..a747f79ef384f3 100644
--- a/mlir/lib/Remark/CMakeLists.txt
+++ b/mlir/lib/Remark/CMakeLists.txt
@@ -7,6 +7,7 @@ add_mlir_library(MLIRRemarkStreamer
LINK_LIBS PUBLIC
MLIRIR
+ MLIRSupport
LINK_COMPONENTS
Remarks
diff --git a/mlir/lib/Remark/RemarkStreamer.cpp b/mlir/lib/Remark/RemarkStreamer.cpp
index bf362862d24f6b..afece69c5b3876 100644
--- a/mlir/lib/Remark/RemarkStreamer.cpp
+++ b/mlir/lib/Remark/RemarkStreamer.cpp
@@ -1,6 +1,8 @@
#include "mlir/Remark/RemarkStreamer.h"
+#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Remarks.h"
+#include "mlir/Support/FileUtilities.h"
#include "llvm/Remarks/RemarkSerializer.h"
#include "llvm/Remarks/RemarkStreamer.h"
@@ -9,22 +11,17 @@
#include "llvm/Support/ToolOutputFile.h"
namespace mlir::remark::detail {
-
-FailureOr<std::unique_ptr<MLIRRemarkStreamerBase>>
+llvm::Expected<std::unique_ptr<MLIRRemarkStreamerBase>>
LLVMRemarkStreamer::createToFile(llvm::StringRef path,
llvm::remarks::Format fmt) {
- std::error_code ec;
- // Use error_code ctor; YAML is text. (Bitstream also works fine here.)
- auto f =
- std::make_unique<llvm::ToolOutputFile>(path, ec, llvm::sys::fs::OF_Text);
- if (ec)
- return failure();
+ std::string errorMessage;
+ std::unique_ptr<llvm::ToolOutputFile> f = openOutputFile(path, &errorMessage);
+ if (!f)
+ return llvm::createStringError(errorMessage);
auto serOr = llvm::remarks::createRemarkSerializer(fmt, f->os());
- if (!serOr) {
- llvm::consumeError(serOr.takeError());
- return failure();
- }
+ if (!serOr)
+ return llvm::createFileError(path, serOr.takeError());
auto rs =
std::make_unique<llvm::remarks::RemarkStreamer>(std::move(*serOr), path);
@@ -63,10 +60,10 @@ LogicalResult enableOptimizationRemarksWithLLVMStreamer(
std::unique_ptr<detail::RemarkEmittingPolicyBase> remarkEmittingPolicy,
const RemarkCategories &cat, bool printAsEmitRemarks) {
- FailureOr<std::unique_ptr<detail::MLIRRemarkStreamerBase>> sOr =
+ llvm::Expected<std::unique_ptr<detail::MLIRRemarkStreamerBase>> sOr =
detail::LLVMRemarkStreamer::createToFile(path, fmt);
- if (failed(sOr))
- return failure();
+ if (!sOr)
+ return emitError(UnknownLoc::get(&ctx)) << llvm::toString(sOr.takeError());
return remark::enableOptimizationRemarks(ctx, std::move(*sOr),
std::move(remarkEmittingPolicy), cat,
diff --git a/mlir/test/Pass/remark-output-error.mlir b/mlir/test/Pass/remark-output-error.mlir
new file mode 100644
index 00000000000000..88ce6ba610ae02
--- /dev/null
+++ b/mlir/test/Pass/remark-output-error.mlir
@@ -0,0 +1,5 @@
+// RUN: rm -rf %t
+// RUN: not mlir-opt %s --remarks-filter="category.*" --remark-format=yaml --remarks-output-file=%t/missing/out.yaml 2>&1 | FileCheck %s
+
+// CHECK: error: cannot open output file '{{.*}}out.yaml': {{.+}}
+module {}
diff --git a/mlir/unittests/IR/RemarkTest.cpp b/mlir/unittests/IR/RemarkTest.cpp
index 6a41631671773a..ece76e628739fa 100644
--- a/mlir/unittests/IR/RemarkTest.cpp
+++ b/mlir/unittests/IR/RemarkTest.cpp
@@ -13,11 +13,13 @@
#include "mlir/IR/Remarks.h"
#include "mlir/Remark/RemarkStreamer.h"
#include "mlir/Support/TypeID.h"
+#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/LLVMRemarkStreamer.h"
#include "llvm/Remarks/RemarkFormat.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/LogicalResult.h"
+#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLParser.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
@@ -830,4 +832,32 @@ TEST(Remark, TestNestedLocation) {
remark.print(os, /*printLocation=*/true);
EXPECT_THAT(printed, Not(HasSubstr(" @")));
}
+
+TEST(Remark, TestLLVMStreamerErrors) {
+ llvm::SmallString<64> dir;
+ ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory("remarks", dir));
+ llvm::scope_exit removeDir([&] { llvm::sys::fs::remove_directories(dir); });
+ llvm::SmallString<64> missingPath(dir);
+ llvm::sys::path::append(missingPath, "missing", "out.yaml");
+ llvm::SmallString<64> filePath(dir);
+ llvm::sys::path::append(filePath, "out.yaml");
+ std::string noSuchFile =
+ std::make_error_code(std::errc::no_such_file_or_directory).message();
+
+ // A file that cannot be opened.
+ auto missing = remark::detail::LLVMRemarkStreamer::createToFile(
+ missingPath, llvm::remarks::Format::YAML);
+ ASSERT_FALSE(static_cast<bool>(missing));
+ EXPECT_EQ(llvm::toString(missing.takeError()), "cannot open output file '" +
+ missingPath.str().str() +
+ "': " + noSuchFile);
+
+ // A serializer that cannot be created: the file is not left behind.
+ auto badFormat = remark::detail::LLVMRemarkStreamer::createToFile(
+ filePath, llvm::remarks::Format::Unknown);
+ ASSERT_FALSE(static_cast<bool>(badFormat));
+ EXPECT_THAT(llvm::toString(badFormat.takeError()),
+ StartsWith("'" + filePath.str().str() + "': "));
+ EXPECT_FALSE(llvm::sys::fs::exists(filePath));
+}
} // namespace
diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
index 197fe063f4a713..6a77deb1bb041f 100644
--- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
@@ -595,6 +595,7 @@ cc_library(
includes = ["include"],
deps = [
":IR",
+ ":Support",
"//llvm:BitstreamReader",
"//llvm:Core",
"//llvm:Remarks",
More information about the Mlir-commits
mailing list