[Mlir-commits] [mlir] c12a0d2 - [mlir][ODS] Share optional operand and type parser logic (NFC) (#226066)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Sep 24 02:18:00 PDT 2026
Author: Mehdi Amini
Date: 2026-09-24T09:17:50Z
New Revision: c12a0d255928c9376a6e1a6df36d4338b6cfedf8
URL: https://github.com/llvm/llvm-project/commit/c12a0d255928c9376a6e1a6df36d4338b6cfedf8
DIFF: https://github.com/llvm/llvm-project/commit/c12a0d255928c9376a6e1a6df36d4338b6cfedf8.diff
LOG: [mlir][ODS] Share optional operand and type parser logic (NFC) (#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
Added:
Modified:
mlir/include/mlir/IR/OpImplementation.h
mlir/lib/IR/OperationSupport.cpp
mlir/test/mlir-tblgen/op-format.td
mlir/tools/mlir-tblgen/OpFormatGen.cpp
Removed:
################################################################################
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 83f9ae3be139f..5b8eeb4f789a8 100644
--- a/mlir/lib/IR/OperationSupport.cpp
+++ b/mlir/lib/IR/OperationSupport.cpp
@@ -15,6 +15,7 @@
#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>
@@ -28,6 +29,31 @@ void mlir::detail::appendAttributeProperty(
attrs.emplace_back(name, attr);
}
+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