[Mlir-commits] [mlir] [MLIR] Allowed streaming enums into an mlir::Diagnostic (PR #177959)

Sergei Lebedev llvmlistbot at llvm.org
Mon Jan 26 05:39:27 PST 2026


https://github.com/superbobry created https://github.com/llvm/llvm-project/pull/177959

Prior to this change users had to manually call `stringifyEnum` or `mlir::debugString` to bea ble to stream an enum value into a diagnostic, e.g.

    op.emiError("Something went wrong: ")
        << mlir::some_dialect::stringifyEnum(some_enum);

The added overload allows streaming the value directly

    op.emitError("Something went wrong: ") << some_enum;

>From bf55d0a9ad3b335323ae3f7ffd567e74e59ed995 Mon Sep 17 00:00:00 2001
From: Sergei Lebedev <slebedev at google.com>
Date: Mon, 26 Jan 2026 13:38:25 +0000
Subject: [PATCH] [MLIR] Allowed streaming enums into an mlir::Diagnostic

Prior to this change users had to manually call `stringifyEnum` or
`mlir::debugString` to bea ble to stream an enum value into a diagnostic,
e.g.

    op.emiError("Something went wrong: ")
        << mlir::some_dialect::stringifyEnum(some_enum);

The added overload allows streaming the value directly

    op.emitError("Something went wrong: ") << some_enum;
---
 mlir/include/mlir/IR/Diagnostics.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h
index a0a99f4953822..3b8fb46b06a48 100644
--- a/mlir/include/mlir/IR/Diagnostics.h
+++ b/mlir/include/mlir/IR/Diagnostics.h
@@ -208,6 +208,17 @@ class Diagnostic {
   /// Stream in a Value.
   Diagnostic &operator<<(Value val);
 
+  /// Stream in an enum that has a `stringifyEnum` function.
+  template <typename EnumT>
+  std::enable_if_t<
+      std::is_enum_v<EnumT> &&
+          std::is_convertible_v<decltype(stringifyEnum(std::declval<EnumT>())),
+                                StringRef>,
+      Diagnostic &>
+  operator<<(EnumT val) {
+    return *this << stringifyEnum(val);
+  }
+
   /// Stream in a range.
   template <typename T, typename ValueT = llvm::detail::ValueOfRange<T>>
   std::enable_if_t<!std::is_constructible<DiagnosticArgument, T>::value,



More information about the Mlir-commits mailing list