[Mlir-commits] [mlir] [mlir] Add emitRemark overload taking ArrayRef<Twine> (PR #217804)
lonely eagle
llvmlistbot at llvm.org
Wed Sep 2 06:46:26 PDT 2026
https://github.com/linuxlonelyeagle updated https://github.com/llvm/llvm-project/pull/217804
>From e5740ace91d704cd1a3ca6499dfcc91c3a3602ae Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Fri, 21 Aug 2026 02:27:45 +0000
Subject: [PATCH 1/6] make emitWarning/emitRemark use OpWithFlags.
---
mlir/lib/IR/Operation.cpp | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index d262e844603a1..87a2dd67a9019 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -276,7 +276,9 @@ InFlightDiagnostic Operation::emitError(const Twine &message) {
InFlightDiagnostic Operation::emitWarning(const Twine &message) {
InFlightDiagnostic diag = mlir::emitWarning(getLoc(), message);
if (getContext()->shouldPrintOpOnDiagnostic())
- diag.attachNote(getLoc()) << "see current operation: " << *this;
+ diag.attachNote(getLoc())
+ << "see current operation: "
+ << OpWithFlags(this, OpPrintingFlags().skipRegions());
return diag;
}
@@ -285,7 +287,9 @@ InFlightDiagnostic Operation::emitWarning(const Twine &message) {
InFlightDiagnostic Operation::emitRemark(const Twine &message) {
InFlightDiagnostic diag = mlir::emitRemark(getLoc(), message);
if (getContext()->shouldPrintOpOnDiagnostic())
- diag.attachNote(getLoc()) << "see current operation: " << *this;
+ diag.attachNote(getLoc())
+ << "see current operation: "
+ << OpWithFlags(this, OpPrintingFlags().skipRegions());
return diag;
}
>From 05f87df9de2b4b95e02e798716ae0b7c63dd4fd0 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Thu, 27 Aug 2026 16:40:58 +0000
Subject: [PATCH 2/6] add new emitRemark method use ArrayRef<Twine> as
parameter.
---
mlir/include/mlir/IR/OpDefinition.h | 4 ++++
mlir/include/mlir/IR/Operation.h | 4 ++++
mlir/lib/IR/Operation.cpp | 28 ++++++++++++++++++++++------
3 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index d92a93746bf54..b2a56058b1af7 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -150,6 +150,10 @@ class OpState {
/// handlers that may be listening.
InFlightDiagnostic emitRemark(const Twine &message = {});
+ // Emit a remark about this operation for each message, reporting up to
+ /// any diagnostic handlers that may be listening.
+ InFlightDiagnostic emitRemark(const ArrayRef<Twine> messages);
+
/// Walk the operation by calling the callback for each nested operation
/// (including this one), block or region, depending on the callback provided.
/// The order in which regions, blocks and operations the same nesting level
diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index 3ea9ad8ca0cfc..b580d35784f93 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -944,6 +944,10 @@ class alignas(8) Operation final
/// handlers that may be listening.
InFlightDiagnostic emitRemark(const Twine &message = {});
+ // Emit a remark about this operation for each message, reporting up to
+ /// any diagnostic handlers that may be listening.
+ InFlightDiagnostic emitRemark(const ArrayRef<Twine> messages);
+
/// Returns the properties storage size.
int getPropertiesStorageSize() const {
return ((int)propertiesStorageSize) * 8;
diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index 87a2dd67a9019..f7396805887f0 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -276,9 +276,7 @@ InFlightDiagnostic Operation::emitError(const Twine &message) {
InFlightDiagnostic Operation::emitWarning(const Twine &message) {
InFlightDiagnostic diag = mlir::emitWarning(getLoc(), message);
if (getContext()->shouldPrintOpOnDiagnostic())
- diag.attachNote(getLoc())
- << "see current operation: "
- << OpWithFlags(this, OpPrintingFlags().skipRegions());
+ diag.attachNote(getLoc()) << "see current operation: " << *this;
return diag;
}
@@ -287,12 +285,24 @@ InFlightDiagnostic Operation::emitWarning(const Twine &message) {
InFlightDiagnostic Operation::emitRemark(const Twine &message) {
InFlightDiagnostic diag = mlir::emitRemark(getLoc(), message);
if (getContext()->shouldPrintOpOnDiagnostic())
- diag.attachNote(getLoc())
- << "see current operation: "
- << OpWithFlags(this, OpPrintingFlags().skipRegions());
+ diag.attachNote(getLoc()) << "see current operation: " << *this;
return diag;
}
+/// Emit a remark about this operation for each message, reporting up to
+/// any diagnostic handlers that may be listening.
+InFlightDiagnostic Operation::emitRemark(const ArrayRef<Twine> messages) {
+ assert(!messages.empty() && "emitRemark messages is empty");
+ for (size_t i = 0, e = messages.size(); i < e; ++i) {
+ InFlightDiagnostic diag = mlir::emitRemark(getLoc(), messages[i]);
+ if (i == e - 1 && getContext()->shouldPrintOpOnDiagnostic()) {
+ diag.attachNote(getLoc()) << "see current operation: " << *this;
+ return diag;
+ }
+ }
+ return {};
+}
+
DictionaryAttr Operation::getAttrDictionary() {
if (getPropertiesStorageSize()) {
NamedAttrList attrsList = attrs;
@@ -859,6 +869,12 @@ InFlightDiagnostic OpState::emitRemark(const Twine &message) {
return getOperation()->emitRemark(message);
}
+/// Emit a remark about this operation for each message, reporting up to
+/// any diagnostic handlers that may be listening.
+InFlightDiagnostic OpState::emitRemark(const ArrayRef<Twine> messages) {
+ return getOperation()->emitRemark(messages);
+}
+
//===----------------------------------------------------------------------===//
// Op Trait implementations
//===----------------------------------------------------------------------===//
>From 60529ac05929a499dd68ad44c9cb2b26a7225e13 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Fri, 28 Aug 2026 02:48:33 +0000
Subject: [PATCH 3/6] make emitRemark return vector<InFlightDiagnostic>c>.
---
mlir/include/mlir/IR/OpDefinition.h | 2 +-
mlir/include/mlir/IR/Operation.h | 2 +-
mlir/lib/IR/Operation.cpp | 15 ++++++++-------
3 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index b2a56058b1af7..0e10be7eac0b5 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -152,7 +152,7 @@ class OpState {
// Emit a remark about this operation for each message, reporting up to
/// any diagnostic handlers that may be listening.
- InFlightDiagnostic emitRemark(const ArrayRef<Twine> messages);
+ std::vector<InFlightDiagnostic> emitRemark(const ArrayRef<Twine> messages);
/// Walk the operation by calling the callback for each nested operation
/// (including this one), block or region, depending on the callback provided.
diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index b580d35784f93..6c9d89d02587a 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -946,7 +946,7 @@ class alignas(8) Operation final
// Emit a remark about this operation for each message, reporting up to
/// any diagnostic handlers that may be listening.
- InFlightDiagnostic emitRemark(const ArrayRef<Twine> messages);
+ std::vector<InFlightDiagnostic> emitRemark(const ArrayRef<Twine> messages);
/// Returns the properties storage size.
int getPropertiesStorageSize() const {
diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index f7396805887f0..2c072c6b70456 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -291,16 +291,16 @@ InFlightDiagnostic Operation::emitRemark(const Twine &message) {
/// Emit a remark about this operation for each message, reporting up to
/// any diagnostic handlers that may be listening.
-InFlightDiagnostic Operation::emitRemark(const ArrayRef<Twine> messages) {
- assert(!messages.empty() && "emitRemark messages is empty");
+std::vector<InFlightDiagnostic>
+Operation::emitRemark(const ArrayRef<Twine> messages) {
+ std::vector<InFlightDiagnostic> diags;
for (size_t i = 0, e = messages.size(); i < e; ++i) {
InFlightDiagnostic diag = mlir::emitRemark(getLoc(), messages[i]);
- if (i == e - 1 && getContext()->shouldPrintOpOnDiagnostic()) {
+ if (i == e - 1 && getContext()->shouldPrintOpOnDiagnostic())
diag.attachNote(getLoc()) << "see current operation: " << *this;
- return diag;
- }
+ diags.push_back(std::move(diag));
}
- return {};
+ return diags;
}
DictionaryAttr Operation::getAttrDictionary() {
@@ -871,7 +871,8 @@ InFlightDiagnostic OpState::emitRemark(const Twine &message) {
/// Emit a remark about this operation for each message, reporting up to
/// any diagnostic handlers that may be listening.
-InFlightDiagnostic OpState::emitRemark(const ArrayRef<Twine> messages) {
+std::vector<InFlightDiagnostic>
+OpState::emitRemark(const ArrayRef<Twine> messages) {
return getOperation()->emitRemark(messages);
}
>From 58fd08b02847093f3a3a944e7b4e0a966bfcb394 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Sat, 29 Aug 2026 07:55:39 +0000
Subject: [PATCH 4/6] fix comment.
---
mlir/include/mlir/IR/OpDefinition.h | 4 ++--
mlir/include/mlir/IR/Operation.h | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index 0e10be7eac0b5..5711c627ed311 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -150,8 +150,8 @@ class OpState {
/// handlers that may be listening.
InFlightDiagnostic emitRemark(const Twine &message = {});
- // Emit a remark about this operation for each message, reporting up to
- /// any diagnostic handlers that may be listening.
+ /// Emit a remark about this operation for each message, reporting up to any
+ /// diagnostic handlers that may be listening.
std::vector<InFlightDiagnostic> emitRemark(const ArrayRef<Twine> messages);
/// Walk the operation by calling the callback for each nested operation
diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index 6c9d89d02587a..e9e4c602f9969 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -944,8 +944,8 @@ class alignas(8) Operation final
/// handlers that may be listening.
InFlightDiagnostic emitRemark(const Twine &message = {});
- // Emit a remark about this operation for each message, reporting up to
- /// any diagnostic handlers that may be listening.
+ /// Emit a remark about this operation for each message, reporting up to any
+ /// diagnostic handlers that may be listening.
std::vector<InFlightDiagnostic> emitRemark(const ArrayRef<Twine> messages);
/// Returns the properties storage size.
>From e84957ae1dcbc8e4ce8467ae1310d7faf5e4038c Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Tue, 1 Sep 2026 15:46:30 +0000
Subject: [PATCH 5/6] use SmallVector.
---
mlir/include/mlir/IR/OpDefinition.h | 2 +-
mlir/include/mlir/IR/Operation.h | 2 +-
mlir/lib/IR/Operation.cpp | 11 ++++++++---
3 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index 5711c627ed311..92eb51e7148b6 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -152,7 +152,7 @@ class OpState {
/// Emit a remark about this operation for each message, reporting up to any
/// diagnostic handlers that may be listening.
- std::vector<InFlightDiagnostic> emitRemark(const ArrayRef<Twine> messages);
+ SmallVector<InFlightDiagnostic> emitRemark(const ArrayRef<Twine> messages);
/// Walk the operation by calling the callback for each nested operation
/// (including this one), block or region, depending on the callback provided.
diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index e9e4c602f9969..16cb76382eefd 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -946,7 +946,7 @@ class alignas(8) Operation final
/// Emit a remark about this operation for each message, reporting up to any
/// diagnostic handlers that may be listening.
- std::vector<InFlightDiagnostic> emitRemark(const ArrayRef<Twine> messages);
+ SmallVector<InFlightDiagnostic> emitRemark(const ArrayRef<Twine> messages);
/// Returns the properties storage size.
int getPropertiesStorageSize() const {
diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index 2c072c6b70456..ba0ed618720e1 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -291,13 +291,18 @@ InFlightDiagnostic Operation::emitRemark(const Twine &message) {
/// Emit a remark about this operation for each message, reporting up to
/// any diagnostic handlers that may be listening.
-std::vector<InFlightDiagnostic>
+SmallVector<InFlightDiagnostic>
Operation::emitRemark(const ArrayRef<Twine> messages) {
- std::vector<InFlightDiagnostic> diags;
+ SmallVector<InFlightDiagnostic> diags;
+ diags.reserve(messages.size());
for (size_t i = 0, e = messages.size(); i < e; ++i) {
InFlightDiagnostic diag = mlir::emitRemark(getLoc(), messages[i]);
if (i == e - 1 && getContext()->shouldPrintOpOnDiagnostic())
diag.attachNote(getLoc()) << "see current operation: " << *this;
+
+ // Explicitly report to flush immediately in order, preventing reverse-order
+ // emission during vector destruction.
+ diag.report();
diags.push_back(std::move(diag));
}
return diags;
@@ -871,7 +876,7 @@ InFlightDiagnostic OpState::emitRemark(const Twine &message) {
/// Emit a remark about this operation for each message, reporting up to
/// any diagnostic handlers that may be listening.
-std::vector<InFlightDiagnostic>
+SmallVector<InFlightDiagnostic>
OpState::emitRemark(const ArrayRef<Twine> messages) {
return getOperation()->emitRemark(messages);
}
>From 3ceaf8f68961c9bd833a63f582c5f0c335146831 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 2 Sep 2026 13:45:33 +0000
Subject: [PATCH 6/6] add keepEngine arguemnt to InFlightDiagnostic's report
method and add clear method to Diagnostic class.
---
mlir/include/mlir/IR/Diagnostics.h | 9 ++++++--
mlir/include/mlir/IR/OpDefinition.h | 2 +-
mlir/include/mlir/IR/Operation.h | 5 ++---
mlir/lib/IR/Diagnostics.cpp | 22 ++++++++++++++++----
mlir/lib/IR/Operation.cpp | 32 +++++++++++++----------------
5 files changed, 42 insertions(+), 28 deletions(-)
diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h
index 3b8fb46b06a48..b7c129211bba8 100644
--- a/mlir/include/mlir/IR/Diagnostics.h
+++ b/mlir/include/mlir/IR/Diagnostics.h
@@ -288,6 +288,10 @@ class Diagnostic {
/// Returns the current list of diagnostic metadata.
SmallVectorImpl<DiagnosticArgument> &getMetadata() { return metadata; }
+ /// Clears all payload data (arguments, strings, notes, metadata) while
+ /// preserving location and severity.
+ void clear();
+
private:
Diagnostic(const Diagnostic &rhs) = delete;
Diagnostic &operator=(const Diagnostic &rhs) = delete;
@@ -371,8 +375,9 @@ class InFlightDiagnostic {
/// active.
Diagnostic *getUnderlyingDiagnostic() { return impl ? &*impl : nullptr; }
- /// Reports the diagnostic to the engine.
- void report();
+ /// Reports the diagnostic to the engine. If keepEngine is true, retains the
+ /// engine reference to allow further appending to this diagnostic.
+ void report(bool keepEngine = false);
/// Abandons this diagnostic so that it will no longer be reported.
void abandon();
diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index 92eb51e7148b6..68c8ffba7bff0 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -152,7 +152,7 @@ class OpState {
/// Emit a remark about this operation for each message, reporting up to any
/// diagnostic handlers that may be listening.
- SmallVector<InFlightDiagnostic> emitRemark(const ArrayRef<Twine> messages);
+ InFlightDiagnostic emitRemark(const ArrayRef<Twine> messages);
/// Walk the operation by calling the callback for each nested operation
/// (including this one), block or region, depending on the callback provided.
diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index 16cb76382eefd..f147dbeef5208 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -944,9 +944,8 @@ class alignas(8) Operation final
/// handlers that may be listening.
InFlightDiagnostic emitRemark(const Twine &message = {});
- /// Emit a remark about this operation for each message, reporting up to any
- /// diagnostic handlers that may be listening.
- SmallVector<InFlightDiagnostic> emitRemark(const ArrayRef<Twine> messages);
+ /// Emit remarks about this operation for each message.
+ InFlightDiagnostic emitRemark(const ArrayRef<Twine> messages);
/// Returns the properties storage size.
int getPropertiesStorageSize() const {
diff --git a/mlir/lib/IR/Diagnostics.cpp b/mlir/lib/IR/Diagnostics.cpp
index 5caf826c84bdd..ec100830d2b89 100644
--- a/mlir/lib/IR/Diagnostics.cpp
+++ b/mlir/lib/IR/Diagnostics.cpp
@@ -196,6 +196,15 @@ Diagnostic &Diagnostic::attachNote(std::optional<Location> noteLoc) {
/// Allow a diagnostic to be converted to 'failure'.
Diagnostic::operator LogicalResult() const { return failure(); }
+// Clears all payload data (arguments, strings, notes, metadata) while
+// preserving location and severity.
+void Diagnostic::clear() {
+ arguments.clear();
+ strings.clear();
+ notes.clear();
+ metadata.clear();
+}
+
//===----------------------------------------------------------------------===//
// InFlightDiagnostic
//===----------------------------------------------------------------------===//
@@ -206,15 +215,20 @@ InFlightDiagnostic::operator LogicalResult() const {
return failure(isActive());
}
-/// Reports the diagnostic to the engine.
-void InFlightDiagnostic::report() {
+/// Reports the diagnostic to the engine. If keepEngine is true, retains the
+/// engine reference to allow further appending to this diagnostic.
+void InFlightDiagnostic::report(bool keepEngine) {
// If this diagnostic is still inflight and it hasn't been abandoned, then
// report it.
if (isInFlight()) {
owner->emit(std::move(*impl));
- owner = nullptr;
+ if (!keepEngine)
+ owner = nullptr;
}
- impl.reset();
+ if (keepEngine)
+ impl->clear();
+ else
+ impl.reset();
}
/// Abandons this diagnostic.
diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index ba0ed618720e1..6c5c7f21ca896 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -10,6 +10,7 @@
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
+#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/IR/Matchers.h"
@@ -289,23 +290,19 @@ InFlightDiagnostic Operation::emitRemark(const Twine &message) {
return diag;
}
-/// Emit a remark about this operation for each message, reporting up to
-/// any diagnostic handlers that may be listening.
-SmallVector<InFlightDiagnostic>
-Operation::emitRemark(const ArrayRef<Twine> messages) {
- SmallVector<InFlightDiagnostic> diags;
- diags.reserve(messages.size());
- for (size_t i = 0, e = messages.size(); i < e; ++i) {
- InFlightDiagnostic diag = mlir::emitRemark(getLoc(), messages[i]);
- if (i == e - 1 && getContext()->shouldPrintOpOnDiagnostic())
- diag.attachNote(getLoc()) << "see current operation: " << *this;
-
- // Explicitly report to flush immediately in order, preventing reverse-order
- // emission during vector destruction.
- diag.report();
- diags.push_back(std::move(diag));
+/// Emit remarks about this operation for each message.
+InFlightDiagnostic Operation::emitRemark(const ArrayRef<Twine> messages) {
+ if (messages.empty())
+ return {};
+
+ InFlightDiagnostic diag = mlir::emitRemark(getLoc(), messages.front());
+ for (size_t i = 1, e = messages.size(); i < e; ++i) {
+ diag.report(true);
+ diag.append(messages[i]);
}
- return diags;
+ if (getContext()->shouldPrintOpOnDiagnostic())
+ diag.attachNote(getLoc()) << "see current operation: " << *this;
+ return diag;
}
DictionaryAttr Operation::getAttrDictionary() {
@@ -876,8 +873,7 @@ InFlightDiagnostic OpState::emitRemark(const Twine &message) {
/// Emit a remark about this operation for each message, reporting up to
/// any diagnostic handlers that may be listening.
-SmallVector<InFlightDiagnostic>
-OpState::emitRemark(const ArrayRef<Twine> messages) {
+InFlightDiagnostic OpState::emitRemark(const ArrayRef<Twine> messages) {
return getOperation()->emitRemark(messages);
}
More information about the Mlir-commits
mailing list