From ec74867c5e6a8ea38ea848f47ee2025ebb727397 Mon Sep 17 00:00:00 2001 From: Alex Zinenko Date: Wed, 25 Mar 2020 16:25:52 +0100 Subject: [PATCH] [mlir] Provide CustomOpAsmParser::parseOptionalOperand Summary: Some operations have custom syntax where an operand is always followed by a specific token of streams if the operand is present. Parsing such operations requires the ability to optionally parse an operand. Provide a relevant function in the custom Op parser. Differential Revision: https://reviews.llvm.org/D76779 --- mlir/include/mlir/IR/OpImplementation.h | 3 +++ mlir/lib/Parser/Parser.cpp | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h index f471e6ca0cc5..0a2602544112 100644 --- a/mlir/include/mlir/IR/OpImplementation.h +++ b/mlir/include/mlir/IR/OpImplementation.h @@ -452,6 +452,9 @@ public: /// Parse a single operand. virtual ParseResult parseOperand(OperandType &result) = 0; + /// Parse a single operand if present. + virtual OptionalParseResult parseOptionalOperand(OperandType &result) = 0; + /// These are the supported delimiters around operand lists and region /// argument lists, used by parseOperandList and parseRegionArgumentList. enum class Delimiter { diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp index 437803388e99..06eb2cfc8aa9 100644 --- a/mlir/lib/Parser/Parser.cpp +++ b/mlir/lib/Parser/Parser.cpp @@ -4253,6 +4253,13 @@ public: return success(); } + /// Parse a single operand if present. + OptionalParseResult parseOptionalOperand(OperandType &result) override { + if (parser.getToken().is(Token::percent_identifier)) + return parseOperand(result); + return llvm::None; + } + /// Parse zero or more SSA comma-separated operand references with a specified /// surrounding delimiter, and an optional required operand count. ParseResult parseOperandList(SmallVectorImpl &result,