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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jan 26 05:40:02 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-core

Author: Sergei Lebedev (superbobry)

<details>
<summary>Changes</summary>

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;

---
Full diff: https://github.com/llvm/llvm-project/pull/177959.diff


1 Files Affected:

- (modified) mlir/include/mlir/IR/Diagnostics.h (+11) 


``````````diff
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,

``````````

</details>


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


More information about the Mlir-commits mailing list