@@ -3647,23 +3647,87 @@ std::optional<SmallVector<int64_t, 4>> ApplyScaleOp::getShapeForUnroll() {
36473647 return std::nullopt ;
36483648}
36493649
3650+ static void printInitializationList (OpAsmPrinter &parser,
3651+ Block::BlockArgListType blocksArgs,
3652+ ValueRange initializers,
3653+ StringRef prefix = " " ) {
3654+ assert (blocksArgs.size () == initializers.size () &&
3655+ " expected same length of arguments and initializers" );
3656+ if (initializers.empty ())
3657+ return ;
3658+
3659+ parser << prefix << ' (' ;
3660+ llvm::interleaveComma (
3661+ llvm::zip (blocksArgs, initializers), parser,
3662+ [&](auto it) { parser << std::get<0 >(it) << " = " << std::get<1 >(it); });
3663+ parser << " )" ;
3664+ }
3665+
36503666// parse and print of IfOp refer to the implementation of SCF dialect.
36513667ParseResult IfOp::parse (OpAsmParser &parser, OperationState &result) {
36523668 // Create the regions for 'then'.
36533669 result.regions .reserve (2 );
36543670 Region *thenRegion = result.addRegion ();
36553671 Region *elseRegion = result.addRegion ();
36563672
3657- auto &builder = parser.getBuilder ();
36583673 OpAsmParser::UnresolvedOperand cond;
3659- // Create a i1 tensor type for the boolean condition.
3660- Type i1Type = RankedTensorType::get ({}, builder.getIntegerType (1 ));
3661- if (parser.parseOperand (cond) ||
3662- parser.resolveOperand (cond, i1Type, result.operands ))
3674+
3675+ if (parser.parseOperand (cond))
36633676 return failure ();
3664- // Parse optional results type list.
3665- if (parser.parseOptionalArrowTypeList (result.types ))
3677+
3678+ SmallVector<OpAsmParser::Argument, 4 > regionArgs;
3679+ SmallVector<OpAsmParser::UnresolvedOperand, 4 > operands;
3680+
3681+ // Parse the optional block arguments
3682+ OptionalParseResult listResult =
3683+ parser.parseOptionalAssignmentList (regionArgs, operands);
3684+ if (listResult.has_value () && failed (listResult.value ()))
36663685 return failure ();
3686+
3687+ // Parse a colon.
3688+ if (failed (parser.parseColon ()))
3689+ return parser.emitError (parser.getCurrentLocation (),
3690+ " expected type for condition operand" );
3691+
3692+ // Parse the type of the condition operand
3693+ Type condType;
3694+ if (failed (parser.parseType (condType)))
3695+ return parser.emitError (parser.getCurrentLocation (),
3696+ " expected type for condition operand" );
3697+
3698+ // Resolve operand with provided type
3699+ if (failed (parser.resolveOperand (cond, condType, result.operands )))
3700+ return failure ();
3701+
3702+ // Parse optional block arg types
3703+ if (listResult.has_value ()) {
3704+ FunctionType functionType;
3705+
3706+ if (failed (parser.parseType (functionType)))
3707+ return parser.emitError (parser.getCurrentLocation ())
3708+ << " expected list of types for block arguments "
3709+ << " followed by arrow type and list of return types" ;
3710+
3711+ result.addTypes (functionType.getResults ());
3712+
3713+ if (functionType.getNumInputs () != operands.size ()) {
3714+ return parser.emitError (parser.getCurrentLocation ())
3715+ << " expected as many input types as operands "
3716+ << " (expected " << operands.size () << " got "
3717+ << functionType.getNumInputs () << " )" ;
3718+ }
3719+
3720+ // Resolve input operands.
3721+ if (failed (parser.resolveOperands (operands, functionType.getInputs (),
3722+ parser.getCurrentLocation (),
3723+ result.operands )))
3724+ return failure ();
3725+ } else {
3726+ // Parse optional results type list.
3727+ if (parser.parseOptionalArrowTypeList (result.types ))
3728+ return failure ();
3729+ }
3730+
36673731 // Parse the 'then' region.
36683732 if (parser.parseRegion (*thenRegion, /* arguments=*/ {}, /* argTypes=*/ {}))
36693733 return failure ();
@@ -3681,26 +3745,28 @@ ParseResult IfOp::parse(OpAsmParser &parser, OperationState &result) {
36813745}
36823746
36833747void IfOp::print (OpAsmPrinter &p) {
3684- bool printBlockTerminators = false ;
3685-
36863748 p << " " << getCondition ();
3687- if (!getResults ().empty ()) {
3688- p << " -> (" << getResultTypes () << " )" ;
3689- // Print yield explicitly if the op defines values.
3690- printBlockTerminators = true ;
3749+
3750+ printInitializationList (p, getThenGraph ().front ().getArguments (),
3751+ getInputList (), " " );
3752+ p << " : " ;
3753+ p << getCondition ().getType ();
3754+
3755+ if (!getInputList ().empty ()) {
3756+ p << " (" ;
3757+ llvm::interleaveComma (getInputList ().getTypes (), p);
3758+ p << " )" ;
36913759 }
3692- p << ' ' ;
3693- p. printRegion ( getThenGraph (),
3694- /* printEntryBlockArgs= */ false ,
3695- /* printBlockTerminators= */ printBlockTerminators );
3760+ p. printArrowTypeList ( getResultTypes ()) ;
3761+ p << " " ;
3762+
3763+ p. printRegion ( getThenGraph () );
36963764
36973765 // Print the 'else' regions if it exists and has a block.
36983766 auto &elseRegion = getElseGraph ();
36993767 if (!elseRegion.empty ()) {
37003768 p << " else " ;
3701- p.printRegion (elseRegion,
3702- /* printEntryBlockArgs=*/ false ,
3703- /* printBlockTerminators=*/ printBlockTerminators);
3769+ p.printRegion (elseRegion);
37043770 }
37053771
37063772 p.printOptionalAttrDict ((*this )->getAttrs ());
@@ -3909,22 +3975,6 @@ ParseResult WhileOp::parse(OpAsmParser &parser, OperationState &result) {
39093975 parser.parseOptionalAttrDictWithKeyword (result.attributes ));
39103976}
39113977
3912- static void printInitializationList (OpAsmPrinter &parser,
3913- Block::BlockArgListType blocksArgs,
3914- ValueRange initializers,
3915- StringRef prefix = " " ) {
3916- assert (blocksArgs.size () == initializers.size () &&
3917- " expected same length of arguments and initializers" );
3918- if (initializers.empty ())
3919- return ;
3920-
3921- parser << prefix << ' (' ;
3922- llvm::interleaveComma (
3923- llvm::zip (blocksArgs, initializers), parser,
3924- [&](auto it) { parser << std::get<0 >(it) << " = " << std::get<1 >(it); });
3925- parser << " )" ;
3926- }
3927-
39283978void WhileOp::print (OpAsmPrinter &parser) {
39293979 printInitializationList (parser, getCondGraph ().front ().getArguments (),
39303980 getInputList (), " " );
0 commit comments