diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h --- a/mlir/include/mlir/IR/OpImplementation.h +++ b/mlir/include/mlir/IR/OpImplementation.h @@ -929,13 +929,8 @@ /// Parse a type list. ParseResult parseTypeList(SmallVectorImpl &result) { - do { - Type type; - if (parseType(type)) - return failure(); - result.push_back(type); - } while (succeeded(parseOptionalComma())); - return success(); + return parseCommaSeparatedList( + [&]() { return parseType(result.emplace_back()); }); } /// Parse an arrow followed by a type list. diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp --- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp +++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp @@ -3454,7 +3454,7 @@ SmallVector> flatSymOperands; SmallVector numMapsPerGroup; SmallVector mapOperands; - do { + auto parseOperands = [&]() { if (succeeded(parser.parseOptionalKeyword( kind == MinMaxKind::Min ? "min" : "max"))) { mapOperands.clear(); @@ -3482,9 +3482,9 @@ return failure(); numMapsPerGroup.push_back(1); } - } while (succeeded(parser.parseOptionalComma())); - - if (failed(parser.parseRParen())) + return success(); + }; + if (parser.parseCommaSeparatedList(parseOperands) || parser.parseRParen()) return failure(); unsigned totalNumDims = 0; diff --git a/mlir/lib/Dialect/DLTI/DLTI.cpp b/mlir/lib/Dialect/DLTI/DLTI.cpp --- a/mlir/lib/Dialect/DLTI/DLTI.cpp +++ b/mlir/lib/Dialect/DLTI/DLTI.cpp @@ -286,14 +286,11 @@ return get(parser.getContext(), {}); SmallVector entries; - do { - entries.emplace_back(); - if (failed(parser.parseAttribute(entries.back()))) - return {}; - } while (succeeded(parser.parseOptionalComma())); - - if (failed(parser.parseGreater())) + if (parser.parseCommaSeparatedList( + [&]() { return parser.parseAttribute(entries.emplace_back()); }) || + parser.parseGreater()) return {}; + return getChecked([&] { return parser.emitError(parser.getNameLoc()); }, parser.getContext(), entries); } diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp --- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp +++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp @@ -540,7 +540,7 @@ SmallVectorImpl &indices, DenseIntElementsAttr &structIndices) { SmallVector constantIndices; - do { + parser.parseCommaSeparatedList([&]() { int32_t constantIndex; OptionalParseResult parsedInteger = parser.parseOptionalInteger(constantIndex); @@ -548,13 +548,14 @@ if (failed(parsedInteger.getValue())) return failure(); constantIndices.push_back(constantIndex); - continue; + return success(); } constantIndices.push_back(LLVM::GEPOp::kDynamicIndex); if (failed(parser.parseOperand(indices.emplace_back()))) return failure(); - } while (succeeded(parser.parseOptionalComma())); + return success(); + }); structIndices = parser.getBuilder().getI32TensorAttr(constantIndices); return success(); @@ -2868,22 +2869,22 @@ FastmathFlags flags = {}; if (failed(parser.parseOptionalGreater())) { - do { - StringRef elemName; - if (failed(parser.parseKeyword(&elemName))) - return {}; - - auto elem = symbolizeFastmathFlags(elemName); - if (!elem) { - parser.emitError(parser.getNameLoc(), "Unknown fastmath flag: ") - << elemName; - return {}; - } - - flags = flags | *elem; - } while (succeeded(parser.parseOptionalComma())); - - if (failed(parser.parseGreater())) + if (failed(parser.parseCommaSeparatedList([&]() { + StringRef elemName; + if (failed(parser.parseKeyword(&elemName))) + return failure(); + + auto elem = symbolizeFastmathFlags(elemName); + if (!elem) { + parser.emitError(parser.getNameLoc(), "Unknown fastmath flag: ") + << elemName; + return failure(); + } + + flags = flags | *elem; + return success(); + })) || + parser.parseGreater()) return {}; } @@ -3031,50 +3032,51 @@ SmallVector> options; llvm::SmallDenseSet seenOptions; - do { - StringRef optionName; - if (parser.parseKeyword(&optionName)) - return {}; - - auto option = symbolizeLoopOptionCase(optionName); - if (!option) { - parser.emitError(parser.getNameLoc(), "unknown loop option: ") - << optionName; - return {}; - } - if (!seenOptions.insert(*option).second) { - parser.emitError(parser.getNameLoc(), "loop option present twice"); - return {}; - } - if (failed(parser.parseEqual())) - return {}; - - int64_t value; - switch (*option) { - case LoopOptionCase::disable_licm: - case LoopOptionCase::disable_unroll: - case LoopOptionCase::disable_pipeline: - if (succeeded(parser.parseOptionalKeyword("true"))) - value = 1; - else if (succeeded(parser.parseOptionalKeyword("false"))) - value = 0; - else { - parser.emitError(parser.getNameLoc(), - "expected boolean value 'true' or 'false'"); - return {}; - } - break; - case LoopOptionCase::interleave_count: - case LoopOptionCase::pipeline_initiation_interval: - if (failed(parser.parseInteger(value))) { - parser.emitError(parser.getNameLoc(), "expected integer value"); - return {}; - } - break; - } - options.push_back(std::make_pair(*option, value)); - } while (succeeded(parser.parseOptionalComma())); - if (failed(parser.parseGreater())) + if (parser.parseCommaSeparatedList([&]() { + StringRef optionName; + if (parser.parseKeyword(&optionName)) + return failure(); + + auto option = symbolizeLoopOptionCase(optionName); + if (!option) { + parser.emitError(parser.getNameLoc(), "unknown loop option: ") + << optionName; + return failure(); + } + if (!seenOptions.insert(*option).second) { + parser.emitError(parser.getNameLoc(), "loop option present twice"); + return failure(); + } + if (failed(parser.parseEqual())) + return failure(); + + int64_t value; + switch (*option) { + case LoopOptionCase::disable_licm: + case LoopOptionCase::disable_unroll: + case LoopOptionCase::disable_pipeline: + if (succeeded(parser.parseOptionalKeyword("true"))) + value = 1; + else if (succeeded(parser.parseOptionalKeyword("false"))) + value = 0; + else { + parser.emitError(parser.getNameLoc(), + "expected boolean value 'true' or 'false'"); + return failure(); + } + break; + case LoopOptionCase::interleave_count: + case LoopOptionCase::pipeline_initiation_interval: + if (failed(parser.parseInteger(value))) { + parser.emitError(parser.getNameLoc(), "expected integer value"); + return failure(); + } + break; + } + options.push_back(std::make_pair(*option, value)); + return success(); + }) || + parser.parseGreater()) return {}; llvm::sort(options, llvm::less_first()); diff --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp --- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp +++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp @@ -66,19 +66,19 @@ if (succeeded(parser.parseOptionalRParen())) return success(); - do { - OpAsmParser::UnresolvedOperand arg; - Type type; - - if (parser.parseOperand(arg, /*allowResultNumber=*/false) || - parser.parseColonType(type)) - return failure(); - - args.push_back(arg); - argTypes.push_back(type); - } while (succeeded(parser.parseOptionalComma())); - - if (failed(parser.parseRParen())) + if (failed(parser.parseCommaSeparatedList([&]() { + OpAsmParser::UnresolvedOperand arg; + Type type; + + if (parser.parseOperand(arg, /*allowResultNumber=*/false) || + parser.parseColonType(type)) + return failure(); + + args.push_back(arg); + argTypes.push_back(type); + return success(); + })) || + failed(parser.parseRParen())) return failure(); return parser.resolveOperands(args, argTypes, parser.getCurrentLocation(), diff --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp --- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp +++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp @@ -76,7 +76,7 @@ SmallVectorImpl &operandsAllocator, SmallVectorImpl &typesAllocator) { - return parser.parseCommaSeparatedList([&]() -> ParseResult { + return parser.parseCommaSeparatedList([&]() { OpAsmParser::UnresolvedOperand operand; Type type; if (parser.parseOperand(operand) || parser.parseColonType(type)) @@ -142,7 +142,7 @@ SmallVectorImpl &vars, SmallVectorImpl &types, SmallVectorImpl &stepVars) { - do { + return parser.parseCommaSeparatedList([&]() { OpAsmParser::UnresolvedOperand var; Type type; OpAsmParser::UnresolvedOperand stepVar; @@ -153,8 +153,8 @@ vars.push_back(var); types.push_back(type); stepVars.push_back(stepVar); - } while (succeeded(parser.parseOptionalComma())); - return success(); + return success(); + }); } /// Print Linear Clause @@ -304,12 +304,15 @@ SmallVectorImpl &types, ArrayAttr &redcuctionSymbols) { SmallVector reductionVec; - do { - if (parser.parseAttribute(reductionVec.emplace_back()) || - parser.parseArrow() || parser.parseOperand(operands.emplace_back()) || - parser.parseColonType(types.emplace_back())) - return failure(); - } while (succeeded(parser.parseOptionalComma())); + if (failed(parser.parseCommaSeparatedList([&]() { + if (parser.parseAttribute(reductionVec.emplace_back()) || + parser.parseArrow() || + parser.parseOperand(operands.emplace_back()) || + parser.parseColonType(types.emplace_back())) + return failure(); + return success(); + }))) + return failure(); SmallVector reductions(reductionVec.begin(), reductionVec.end()); redcuctionSymbols = ArrayAttr::get(parser.getContext(), reductions); return success(); diff --git a/mlir/lib/Dialect/PDL/IR/PDL.cpp b/mlir/lib/Dialect/PDL/IR/PDL.cpp --- a/mlir/lib/Dialect/PDL/IR/PDL.cpp +++ b/mlir/lib/Dialect/PDL/IR/PDL.cpp @@ -148,16 +148,17 @@ Builder &builder = p.getBuilder(); SmallVector attrNames; if (succeeded(p.parseOptionalLBrace())) { - do { - StringAttr nameAttr; - OpAsmParser::UnresolvedOperand operand; - if (p.parseAttribute(nameAttr) || p.parseEqual() || - p.parseOperand(operand)) - return failure(); - attrNames.push_back(nameAttr); - attrOperands.push_back(operand); - } while (succeeded(p.parseOptionalComma())); - if (p.parseRBrace()) + if (p.parseCommaSeparatedList([&]() { + StringAttr nameAttr; + OpAsmParser::UnresolvedOperand operand; + if (p.parseAttribute(nameAttr) || p.parseEqual() || + p.parseOperand(operand)) + return failure(); + attrNames.push_back(nameAttr); + attrOperands.push_back(operand); + return success(); + }) || + p.parseRBrace()) return failure(); } attrNamesAttr = builder.getArrayAttr(attrNames); diff --git a/mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp b/mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp --- a/mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp +++ b/mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp @@ -71,16 +71,17 @@ Builder &builder = p.getBuilder(); SmallVector attrNames; if (succeeded(p.parseOptionalLBrace())) { - do { - StringAttr nameAttr; - OpAsmParser::UnresolvedOperand operand; - if (p.parseAttribute(nameAttr) || p.parseEqual() || - p.parseOperand(operand)) - return failure(); - attrNames.push_back(nameAttr); - attrOperands.push_back(operand); - } while (succeeded(p.parseOptionalComma())); - if (p.parseRBrace()) + if (p.parseCommaSeparatedList([&]() { + StringAttr nameAttr; + OpAsmParser::UnresolvedOperand operand; + if (p.parseAttribute(nameAttr) || p.parseEqual() || + p.parseOperand(operand)) + return failure(); + attrNames.push_back(nameAttr); + attrOperands.push_back(operand); + return success(); + }) || + p.parseRBrace()) return failure(); } attrNamesAttr = builder.getArrayAttr(attrNames); diff --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp b/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp --- a/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp +++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp @@ -592,7 +592,7 @@ return failure(); // Check for spirv::Decorations. - do { + auto parseDecorations = [&]() { auto memberDecoration = parseAndVerify(dialect, parser); if (!memberDecoration) return failure(); @@ -613,10 +613,13 @@ static_cast(memberTypes.size() - 1), 0, memberDecoration.getValue(), 0); } + return success(); + }; + if (failed(parser.parseCommaSeparatedList(parseDecorations)) || + failed(parser.parseRSquare())) + return failure(); - } while (succeeded(parser.parseOptionalComma())); - - return parser.parseRSquare(); + return success(); } // struct-member-decoration ::= integer-literal? spirv-decoration* @@ -885,17 +888,16 @@ // Keep parsing the keyword and an optional comma following it. If the comma // is successfully parsed, then we have more keywords to parse. - do { - auto loc = parser.getCurrentLocation(); - StringRef keyword; - if (parser.parseKeyword(&keyword) || failed(processKeyword(loc, keyword))) - return failure(); - } while (succeeded(parser.parseOptionalComma())); - - if (parser.parseRSquare()) + if (failed(parser.parseCommaSeparatedList([&]() { + auto loc = parser.getCurrentLocation(); + StringRef keyword; + if (parser.parseKeyword(&keyword) || + failed(processKeyword(loc, keyword))) + return failure(); + return success(); + }))) return failure(); - - return success(); + return parser.parseRSquare(); } /// Parses a spirv::InterfaceVarABIAttr. diff --git a/mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp b/mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp --- a/mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp +++ b/mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp @@ -212,11 +212,12 @@ return CustomDataLayoutSpec::get(parser.getContext(), {}); SmallVector entries; - do { + parser.parseCommaSeparatedList([&]() { entries.emplace_back(); ok = succeeded(parser.parseAttribute(entries.back())); assert(ok); - } while (succeeded(parser.parseOptionalComma())); + return success(); + }); ok = succeeded(parser.parseGreater()); assert(ok); return CustomDataLayoutSpec::get(parser.getContext(), entries);