Skip to content

Commit 99499c3

Browse files
committed
[OpAsmParser] Simplify logic for requiredOperandCount in parseOperandList.
I would ideally like to eliminate 'requiredOperandCount' as a bit of verification that should be in the client side, but it is much more widely used than I expected. Just tidy some pieces up around it given we can't drop it immediately. NFC. Differential Revision: https://reviews.llvm.org/D124629
1 parent 463790b commit 99499c3

File tree

5 files changed

+27
-56
lines changed

5 files changed

+27
-56
lines changed

mlir/include/mlir/IR/OpImplementation.h

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,29 +1122,30 @@ class OpAsmParser : public AsmParser {
11221122

11231123
/// Parse zero or more SSA comma-separated operand references with a specified
11241124
/// surrounding delimiter, and an optional required operand count.
1125-
virtual ParseResult parseOperandList(
1126-
SmallVectorImpl<UnresolvedOperand> &result, int requiredOperandCount = -1,
1127-
Delimiter delimiter = Delimiter::None, bool allowResultNumber = true) = 0;
1125+
virtual ParseResult
1126+
parseOperandList(SmallVectorImpl<UnresolvedOperand> &result,
1127+
Delimiter delimiter = Delimiter::None,
1128+
bool allowResultNumber = true,
1129+
int requiredOperandCount = -1) = 0;
11281130

1131+
/// Parse a specified number of comma separated operands.
11291132
ParseResult parseOperandList(SmallVectorImpl<UnresolvedOperand> &result,
1130-
Delimiter delimiter,
1131-
bool allowResultNumber = true) {
1132-
return parseOperandList(result, /*requiredOperandCount=*/-1, delimiter,
1133-
allowResultNumber);
1133+
int requiredOperandCount,
1134+
Delimiter delimiter = Delimiter::None) {
1135+
return parseOperandList(result, delimiter,
1136+
/*allowResultNumber=*/true, requiredOperandCount);
11341137
}
11351138

11361139
/// Parse zero or more trailing SSA comma-separated trailing operand
11371140
/// references with a specified surrounding delimiter, and an optional
1138-
/// required operand count. A leading comma is expected before the operands.
1139-
virtual ParseResult
1140-
parseTrailingOperandList(SmallVectorImpl<UnresolvedOperand> &result,
1141-
int requiredOperandCount = -1,
1142-
Delimiter delimiter = Delimiter::None) = 0;
1141+
/// required operand count. A leading comma is expected before the
1142+
/// operands.
11431143
ParseResult
11441144
parseTrailingOperandList(SmallVectorImpl<UnresolvedOperand> &result,
1145-
Delimiter delimiter) {
1146-
return parseTrailingOperandList(result, /*requiredOperandCount=*/-1,
1147-
delimiter);
1145+
Delimiter delimiter = Delimiter::None) {
1146+
if (failed(parseOptionalComma()))
1147+
return success(); // The comma is optional.
1148+
return parseOperandList(result, delimiter);
11481149
}
11491150

11501151
/// Resolve an operand to an SSA value, emitting an error on failure.
@@ -1297,14 +1298,6 @@ class OpAsmParser : public AsmParser {
12971298
parseOptionalAssignmentListWithTypes(SmallVectorImpl<UnresolvedOperand> &lhs,
12981299
SmallVectorImpl<UnresolvedOperand> &rhs,
12991300
SmallVectorImpl<Type> &types) = 0;
1300-
1301-
private:
1302-
/// Parse either an operand list or a region argument list depending on
1303-
/// whether isOperandList is true.
1304-
ParseResult
1305-
parseOperandOrRegionArgList(SmallVectorImpl<UnresolvedOperand> &result,
1306-
bool isOperandList, int requiredOperandCount,
1307-
Delimiter delimiter);
13081301
};
13091302

13101303
//===--------------------------------------------------------------------===//

mlir/lib/Dialect/Affine/IR/AffineOps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,9 +1075,9 @@ ParseResult AffineDmaStartOp::parse(OpAsmParser &parser,
10751075
return failure();
10761076

10771077
// Parse optional stride and elements per stride.
1078-
if (parser.parseTrailingOperandList(strideInfo)) {
1078+
if (parser.parseTrailingOperandList(strideInfo))
10791079
return failure();
1080-
}
1080+
10811081
if (!strideInfo.empty() && strideInfo.size() != 2) {
10821082
return parser.emitError(parser.getNameLoc(),
10831083
"expected two stride related operands");

mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,22 +530,18 @@ parseWsLoopControl(OpAsmParser &parser, Region &region,
530530

531531
size_t numIVs = ivs.size();
532532
Type loopVarType;
533-
if (parser.parseColonType(loopVarType))
534-
return failure();
535-
536-
// Parse loop bounds.
537-
if (parser.parseEqual() ||
533+
if (parser.parseColonType(loopVarType) ||
534+
// Parse loop bounds.
535+
parser.parseEqual() ||
538536
parser.parseOperandList(lowerBound, numIVs,
539-
OpAsmParser::Delimiter::Paren))
540-
return failure();
541-
if (parser.parseKeyword("to") ||
537+
OpAsmParser::Delimiter::Paren) ||
538+
parser.parseKeyword("to") ||
542539
parser.parseOperandList(upperBound, numIVs,
543540
OpAsmParser::Delimiter::Paren))
544541
return failure();
545542

546-
if (succeeded(parser.parseOptionalKeyword("inclusive"))) {
543+
if (succeeded(parser.parseOptionalKeyword("inclusive")))
547544
inclusive = UnitAttr::get(parser.getBuilder().getContext());
548-
}
549545

550546
// Parse step values.
551547
if (parser.parseKeyword("step") ||

mlir/lib/Dialect/SCF/SCF.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,8 +2006,7 @@ ParseResult ParallelOp::parse(OpAsmParser &parser, OperationState &result) {
20062006
// Parse init values.
20072007
SmallVector<OpAsmParser::UnresolvedOperand, 4> initVals;
20082008
if (succeeded(parser.parseOptionalKeyword("init"))) {
2009-
if (parser.parseOperandList(initVals, /*requiredOperandCount=*/-1,
2010-
OpAsmParser::Delimiter::Paren))
2009+
if (parser.parseOperandList(initVals, OpAsmParser::Delimiter::Paren))
20112010
return failure();
20122011
}
20132012

mlir/lib/Parser/Parser.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,9 +1297,9 @@ class CustomOpAsmParser : public AsmParserImpl<OpAsmParser> {
12971297
/// Parse zero or more SSA comma-separated operand references with a specified
12981298
/// surrounding delimiter, and an optional required operand count.
12991299
ParseResult parseOperandList(SmallVectorImpl<UnresolvedOperand> &result,
1300-
int requiredOperandCount = -1,
13011300
Delimiter delimiter = Delimiter::None,
1302-
bool allowResultNumber = true) override {
1301+
bool allowResultNumber = true,
1302+
int requiredOperandCount = -1) override {
13031303
auto startLoc = parser.getToken().getLoc();
13041304

13051305
// The no-delimiter case has some special handling for better diagnostics.
@@ -1339,23 +1339,6 @@ class CustomOpAsmParser : public AsmParserImpl<OpAsmParser> {
13391339
return success();
13401340
}
13411341

1342-
/// Parse zero or more trailing SSA comma-separated trailing operand
1343-
/// references with a specified surrounding delimiter, and an optional
1344-
/// required operand count. A leading comma is expected before the operands.
1345-
ParseResult
1346-
parseTrailingOperandList(SmallVectorImpl<UnresolvedOperand> &result,
1347-
int requiredOperandCount,
1348-
Delimiter delimiter) override {
1349-
if (parser.getToken().is(Token::comma)) {
1350-
parseComma();
1351-
return parseOperandList(result, requiredOperandCount, delimiter);
1352-
}
1353-
if (requiredOperandCount != -1)
1354-
return emitError(parser.getToken().getLoc(), "expected ")
1355-
<< requiredOperandCount << " operands";
1356-
return success();
1357-
}
1358-
13591342
/// Resolve an operand to an SSA value, emitting an error on failure.
13601343
ParseResult resolveOperand(const UnresolvedOperand &operand, Type type,
13611344
SmallVectorImpl<Value> &result) override {

0 commit comments

Comments
 (0)