[Mlir-commits] [llvm] [mlir] [MLIR][Remark] Report why the LLVM remark streamer could not be created (PR #230130)

Henrich Lauko llvmlistbot at llvm.org
Fri Oct 9 05:17:29 PDT 2026


https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/230130

>From 7f2daab55525bebd1835fe3f44269a99f961d06b Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Thu, 8 Oct 2026 13:43:13 +0000
Subject: [PATCH] [MLIR][Remark] Report why the LLVM remark streamer could not
 be created

LLVMRemarkStreamer::createToFile dropped the reason it failed: a file that
could not be opened and a serializer that could not be created both turned
into a bare failure, and enableOptimizationRemarksWithLLVMStreamer failed
without a diagnostic.

- createToFile returns llvm::Expected. It opens the file with
  mlir::openOutputFile and returns its message; a serializer error is
  returned as a FileError naming the file.
- enableOptimizationRemarksWithLLVMStreamer emits the error on the context.

Assisted-by: Claude Code (Claude Opus 5.5)
---
 mlir/include/mlir/Remark/RemarkStreamer.h     |  7 +++--
 mlir/lib/Remark/CMakeLists.txt                |  1 +
 mlir/lib/Remark/RemarkStreamer.cpp            | 27 ++++++++---------
 mlir/test/Pass/remark-output-error.mlir       |  5 ++++
 mlir/unittests/IR/RemarkTest.cpp              | 29 +++++++++++++++++++
 .../llvm-project-overlay/mlir/BUILD.bazel     |  1 +
 6 files changed, 53 insertions(+), 17 deletions(-)
 create mode 100644 mlir/test/Pass/remark-output-error.mlir

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 fba3d96b4389f0..324ffc94d8a7f1 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"
@@ -793,4 +795,31 @@ TEST(Remark, TestRemarkLinking) {
   EXPECT_THAT(errOut, HasSubstr("vectorized loop"));
 }
 
+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 007c2e7fedf508..9e27ee7a3385ad 100644
--- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
@@ -590,6 +590,7 @@ cc_library(
     includes = ["include"],
     deps = [
         ":IR",
+        ":Support",
         "//llvm:BitstreamReader",
         "//llvm:Core",
         "//llvm:Remarks",



More information about the Mlir-commits mailing list