[Mlir-commits] [mlir] [mlir][ODS] Share optional operand and type parser logic (NFC) (PR #226066)
Mehdi Amini
llvmlistbot at llvm.org
Thu Sep 24 01:20:23 PDT 2026
https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/226066
Move repeated optional operand and type parsing into out-of-line helpers.
Generated parsers now reuse those helpers across operations.
This improves MLIR build time by avoid redundant compilation of these helpers.
Assisted-by: Codex
>From c54c429c080ed5bc93187920e005bc2311cf7ffe Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Wed, 23 Sep 2026 09:49:03 -0700
Subject: [PATCH] [mlir][ODS] Share optional operand and type parser logic
Move repeated optional operand and type parsing into out-of-line helpers.
Generated parsers now reuse those helpers across operations.
Assisted-by: Codex
---
mlir/include/mlir/IR/OpImplementation.h | 9 +++++++++
mlir/lib/IR/OperationSupport.cpp | 26 +++++++++++++++++++++++++
mlir/test/mlir-tblgen/op-format.td | 2 +-
mlir/tools/mlir-tblgen/OpFormatGen.cpp | 26 +++++--------------------
4 files changed, 41 insertions(+), 22 deletions(-)
diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h
index 63dde74c33d51..66fb5a31a07d6 100644
--- a/mlir/include/mlir/IR/OpImplementation.h
+++ b/mlir/include/mlir/IR/OpImplementation.h
@@ -1824,6 +1824,15 @@ class OpAsmParser : public AsmParser {
SmallVectorImpl<UnresolvedOperand> &rhs) = 0;
};
+namespace detail {
+/// Parse an optional operand or type into a generated parser's storage.
+ParseResult parseOptionalOperandInto(
+ OpAsmParser &parser,
+ SmallVectorImpl<OpAsmParser::UnresolvedOperand> &operands);
+ParseResult parseOptionalTypeInto(AsmParser &parser,
+ SmallVectorImpl<Type> &types);
+} // namespace detail
+
//===--------------------------------------------------------------------===//
// Custom printers and parsers.
//===--------------------------------------------------------------------===//
diff --git a/mlir/lib/IR/OperationSupport.cpp b/mlir/lib/IR/OperationSupport.cpp
index 4ecb73398aa77..eb6fb6dce59cc 100644
--- a/mlir/lib/IR/OperationSupport.cpp
+++ b/mlir/lib/IR/OperationSupport.cpp
@@ -15,12 +15,38 @@
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/OpDefinition.h"
+#include "mlir/IR/OpImplementation.h"
#include "llvm/Support/SHA1.h"
#include <numeric>
#include <optional>
using namespace mlir;
+ParseResult mlir::detail::parseOptionalOperandInto(
+ OpAsmParser &parser,
+ SmallVectorImpl<OpAsmParser::UnresolvedOperand> &operands) {
+ OpAsmParser::UnresolvedOperand operand;
+ OptionalParseResult result = parser.parseOptionalOperand(operand);
+ if (!result.has_value())
+ return success();
+ if (failed(*result))
+ return failure();
+ operands.push_back(operand);
+ return success();
+}
+
+ParseResult mlir::detail::parseOptionalTypeInto(AsmParser &parser,
+ SmallVectorImpl<Type> &types) {
+ Type type;
+ OptionalParseResult result = parser.parseOptionalType(type);
+ if (!result.has_value())
+ return success();
+ if (failed(*result))
+ return failure();
+ types.push_back(type);
+ return success();
+}
+
//===----------------------------------------------------------------------===//
// NamedAttrList
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/mlir-tblgen/op-format.td b/mlir/test/mlir-tblgen/op-format.td
index f7f0c1b212e04..54d6e3e6249b6 100644
--- a/mlir/test/mlir-tblgen/op-format.td
+++ b/mlir/test/mlir-tblgen/op-format.td
@@ -326,7 +326,7 @@ def LegacyUnquotedBitEnumOp : TestFormat_Op<"$attr attr-dict">,
// CHECK-LABEL: OptionalGroupA::parse
// CHECK: if (::mlir::succeeded(parser.parseOptionalQuestion())
// CHECK-NEXT: else
-// CHECK: parser.parseOptionalOperand
+// CHECK: ::mlir::detail::parseOptionalOperandInto(parser, aOperands)
// CHECK-LABEL: OptionalGroupA::print
// CHECK: if (!getA())
// CHECK-NEXT: odsPrinter << ' ' << "?";
diff --git a/mlir/tools/mlir-tblgen/OpFormatGen.cpp b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
index becdcc825d547..9bbb38328767a 100644
--- a/mlir/tools/mlir-tblgen/OpFormatGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
@@ -673,17 +673,9 @@ const char *const variadicOperandParserCode = R"(
return ::mlir::failure();
)";
const char *const optionalOperandParserCode = R"(
- {
- {0}OperandsLoc = parser.getCurrentLocation();
- ::mlir::OpAsmParser::UnresolvedOperand operand;
- ::mlir::OptionalParseResult parseResult =
- parser.parseOptionalOperand(operand);
- if (parseResult.has_value()) {
- if (failed(*parseResult))
- return ::mlir::failure();
- {0}Operands.push_back(operand);
- }
- }
+ {0}OperandsLoc = parser.getCurrentLocation();
+ if (::mlir::detail::parseOptionalOperandInto(parser, {0}Operands))
+ return ::mlir::failure();
)";
const char *const operandParserCode = R"(
{0}OperandsLoc = parser.getCurrentLocation();
@@ -727,16 +719,8 @@ const char *const variadicTypeParserCode = R"(
return ::mlir::failure();
)";
const char *const optionalTypeParserCode = R"(
- {
- ::mlir::Type optionalType;
- ::mlir::OptionalParseResult parseResult =
- parser.parseOptionalType(optionalType);
- if (parseResult.has_value()) {
- if (failed(*parseResult))
- return ::mlir::failure();
- {0}Types.push_back(optionalType);
- }
- }
+ if (::mlir::detail::parseOptionalTypeInto(parser, {0}Types))
+ return ::mlir::failure();
)";
const char *const typeParserCode = R"(
{
More information about the Mlir-commits
mailing list