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,14 @@ /// Parse a type list. ParseResult parseTypeList(SmallVectorImpl &result) { - do { + auto parse = [&]() { Type type; if (parseType(type)) return failure(); result.push_back(type); - } while (succeeded(parseOptionalComma())); - return success(); + return success(); + }; + return parseCommaSeparatedList(parse); } /// 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,7 +3482,10 @@ return failure(); numMapsPerGroup.push_back(1); } - } while (succeeded(parser.parseOptionalComma())); + return success(); + }; + if (failed(parser.parseCommaSeparatedList(parseOperands))) + return failure(); if (failed(parser.parseRParen())) return failure(); 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,11 +286,14 @@ return get(parser.getContext(), {}); SmallVector entries; - do { + auto parseEntries = [&]() { entries.emplace_back(); if (failed(parser.parseAttribute(entries.back()))) - return {}; - } while (succeeded(parser.parseOptionalComma())); + return failure(); + return success(); + }; + if (failed(parser.parseCommaSeparatedList(parseEntries))) + return {}; if (failed(parser.parseGreater())) return {}; 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 { + auto parseIndices = [&]() { int32_t constantIndex; OptionalParseResult parsedInteger = parser.parseOptionalInteger(constantIndex); @@ -548,13 +548,15 @@ 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(); + }; + parser.parseCommaSeparatedList(parseIndices); structIndices = parser.getBuilder().getI32TensorAttr(constantIndices); return success(); @@ -2868,20 +2870,23 @@ FastmathFlags flags = {}; if (failed(parser.parseOptionalGreater())) { - do { + auto parseFlags = [&]() { StringRef elemName; if (failed(parser.parseKeyword(&elemName))) - return {}; + return failure(); auto elem = symbolizeFastmathFlags(elemName); if (!elem) { parser.emitError(parser.getNameLoc(), "Unknown fastmath flag: ") << elemName; - return {}; + return failure(); } flags = flags | *elem; - } while (succeeded(parser.parseOptionalComma())); + return success(); + }; + if (failed(parser.parseCommaSeparatedList(parseFlags))) + return {}; if (failed(parser.parseGreater())) return {}; @@ -3031,23 +3036,23 @@ SmallVector> options; llvm::SmallDenseSet seenOptions; - do { + auto parseLoopOption = [&]() { StringRef optionName; if (parser.parseKeyword(&optionName)) - return {}; + return failure(); auto option = symbolizeLoopOptionCase(optionName); if (!option) { parser.emitError(parser.getNameLoc(), "unknown loop option: ") << optionName; - return {}; + return failure(); } if (!seenOptions.insert(*option).second) { parser.emitError(parser.getNameLoc(), "loop option present twice"); - return {}; + return failure(); } if (failed(parser.parseEqual())) - return {}; + return failure(); int64_t value; switch (*option) { @@ -3061,19 +3066,23 @@ else { parser.emitError(parser.getNameLoc(), "expected boolean value 'true' or 'false'"); - return {}; + 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 {}; + return failure(); } break; } options.push_back(std::make_pair(*option, value)); - } while (succeeded(parser.parseOptionalComma())); + return success(); + }; + if (failed(parser.parseCommaSeparatedList(parseLoopOption))) + return {}; + if (failed(parser.parseGreater())) return {}; 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,7 +66,7 @@ if (succeeded(parser.parseOptionalRParen())) return success(); - do { + auto parseOperands = [&]() { OpAsmParser::UnresolvedOperand arg; Type type; @@ -76,7 +76,10 @@ args.push_back(arg); argTypes.push_back(type); - } while (succeeded(parser.parseOptionalComma())); + return success(); + }; + if (failed(parser.parseCommaSeparatedList(parseOperands))) + return failure(); if (failed(parser.parseRParen())) return failure(); 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 @@ -142,7 +142,7 @@ SmallVectorImpl &vars, SmallVectorImpl &types, SmallVectorImpl &stepVars) { - do { + return parser.parseCommaSeparatedList([&]() -> ParseResult { 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 { + auto parseReductionList = [&]() { if (parser.parseAttribute(reductionVec.emplace_back()) || parser.parseArrow() || parser.parseOperand(operands.emplace_back()) || parser.parseColonType(types.emplace_back())) return failure(); - } while (succeeded(parser.parseOptionalComma())); + return success(); + }; + if (failed(parser.parseCommaSeparatedList(parseReductionList))) + 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,7 +148,7 @@ Builder &builder = p.getBuilder(); SmallVector attrNames; if (succeeded(p.parseOptionalLBrace())) { - do { + auto parseOperands = [&]() { StringAttr nameAttr; OpAsmParser::UnresolvedOperand operand; if (p.parseAttribute(nameAttr) || p.parseEqual() || @@ -156,7 +156,10 @@ return failure(); attrNames.push_back(nameAttr); attrOperands.push_back(operand); - } while (succeeded(p.parseOptionalComma())); + return success(); + }; + if (failed(p.parseCommaSeparatedList(parseOperands))) + return failure(); if (p.parseRBrace()) return failure(); } 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,7 +71,7 @@ Builder &builder = p.getBuilder(); SmallVector attrNames; if (succeeded(p.parseOptionalLBrace())) { - do { + auto parseOperands = [&]() { StringAttr nameAttr; OpAsmParser::UnresolvedOperand operand; if (p.parseAttribute(nameAttr) || p.parseEqual() || @@ -79,7 +79,11 @@ return failure(); attrNames.push_back(nameAttr); attrOperands.push_back(operand); - } while (succeeded(p.parseOptionalComma())); + return success(); + }; + if (failed(p.parseCommaSeparatedList(parseOperands))) + return failure(); + if (p.parseRBrace()) return failure(); } 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,8 +613,10 @@ static_cast(memberTypes.size() - 1), 0, memberDecoration.getValue(), 0); } - - } while (succeeded(parser.parseOptionalComma())); + return success(); + }; + if (failed(parser.parseCommaSeparatedList(parseDecorations))) + return failure(); return parser.parseRSquare(); } @@ -885,12 +887,15 @@ // 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 parseKeyword = [&]() { auto loc = parser.getCurrentLocation(); StringRef keyword; if (parser.parseKeyword(&keyword) || failed(processKeyword(loc, keyword))) return failure(); - } while (succeeded(parser.parseOptionalComma())); + return success(); + }; + if (failed(parser.parseCommaSeparatedList(parseKeyword))) + return failure(); if (parser.parseRSquare()) return failure(); 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,13 @@ return CustomDataLayoutSpec::get(parser.getContext(), {}); SmallVector entries; - do { + auto parseEntries = [&]() { entries.emplace_back(); ok = succeeded(parser.parseAttribute(entries.back())); assert(ok); - } while (succeeded(parser.parseOptionalComma())); + return success(); + }; + parser.parseCommaSeparatedList(parseEntries); ok = succeeded(parser.parseGreater()); assert(ok); return CustomDataLayoutSpec::get(parser.getContext(), entries);