@@ -1493,6 +1493,134 @@ std::optional<SmallVector<int64_t, 4>> ApplyScaleOp::getShapeForUnroll() {
1493
1493
return std::nullopt;
1494
1494
}
1495
1495
1496
+ // parse and print of IfOp refer to the implementation of SCF dialect.
1497
+ ParseResult IfOp::parse (OpAsmParser &parser, OperationState &result) {
1498
+ // Create the regions for 'then'.
1499
+ result.regions .reserve (2 );
1500
+ Region *thenRegion = result.addRegion ();
1501
+ Region *elseRegion = result.addRegion ();
1502
+
1503
+ auto &builder = parser.getBuilder ();
1504
+ OpAsmParser::UnresolvedOperand cond;
1505
+ // Create a i1 tensor type for the boolean condition.
1506
+ Type i1Type = RankedTensorType::get ({}, builder.getIntegerType (1 ));
1507
+ if (parser.parseOperand (cond) ||
1508
+ parser.resolveOperand (cond, i1Type, result.operands ))
1509
+ return failure ();
1510
+ // Parse optional results type list.
1511
+ if (parser.parseOptionalArrowTypeList (result.types ))
1512
+ return failure ();
1513
+ // Parse the 'then' region.
1514
+ if (parser.parseRegion (*thenRegion, /* arguments=*/ {}, /* argTypes=*/ {}))
1515
+ return failure ();
1516
+
1517
+ // If we find an 'else' keyword then parse the 'else' region.
1518
+ if (!parser.parseOptionalKeyword (" else" )) {
1519
+ if (parser.parseRegion (*elseRegion, /* arguments=*/ {}, /* argTypes=*/ {}))
1520
+ return failure ();
1521
+ }
1522
+
1523
+ // Parse the optional attribute list.
1524
+ if (parser.parseOptionalAttrDict (result.attributes ))
1525
+ return failure ();
1526
+ return success ();
1527
+ }
1528
+
1529
+ void IfOp::print (OpAsmPrinter &p) {
1530
+ bool printBlockTerminators = false ;
1531
+
1532
+ p << " " << getCond ();
1533
+ if (!getResults ().empty ()) {
1534
+ p << " -> (" << getResultTypes () << " )" ;
1535
+ // Print yield explicitly if the op defines values.
1536
+ printBlockTerminators = true ;
1537
+ }
1538
+ p << ' ' ;
1539
+ p.printRegion (getThenBranch (),
1540
+ /* printEntryBlockArgs=*/ false ,
1541
+ /* printBlockTerminators=*/ printBlockTerminators);
1542
+
1543
+ // Print the 'else' regions if it exists and has a block.
1544
+ auto &elseRegion = getElseBranch ();
1545
+ if (!elseRegion.empty ()) {
1546
+ p << " else " ;
1547
+ p.printRegion (elseRegion,
1548
+ /* printEntryBlockArgs=*/ false ,
1549
+ /* printBlockTerminators=*/ printBlockTerminators);
1550
+ }
1551
+
1552
+ p.printOptionalAttrDict ((*this )->getAttrs ());
1553
+ }
1554
+
1555
+ // parse and print of WhileOp refer to the implementation of SCF dialect.
1556
+ ParseResult WhileOp::parse (OpAsmParser &parser, OperationState &result) {
1557
+ SmallVector<OpAsmParser::Argument, 4 > regionArgs;
1558
+ SmallVector<OpAsmParser::UnresolvedOperand, 4 > operands;
1559
+ Region *cond = result.addRegion ();
1560
+ Region *body = result.addRegion ();
1561
+
1562
+ OptionalParseResult listResult =
1563
+ parser.parseOptionalAssignmentList (regionArgs, operands);
1564
+ if (listResult.has_value () && failed (listResult.value ()))
1565
+ return failure ();
1566
+
1567
+ FunctionType functionType;
1568
+ SMLoc typeLoc = parser.getCurrentLocation ();
1569
+ if (failed (parser.parseColonType (functionType)))
1570
+ return failure ();
1571
+
1572
+ result.addTypes (functionType.getResults ());
1573
+
1574
+ if (functionType.getNumInputs () != operands.size ()) {
1575
+ return parser.emitError (typeLoc)
1576
+ << " expected as many input types as operands "
1577
+ << " (expected " << operands.size () << " got "
1578
+ << functionType.getNumInputs () << " )" ;
1579
+ }
1580
+
1581
+ // Resolve input operands.
1582
+ if (failed (parser.resolveOperands (operands, functionType.getInputs (),
1583
+ parser.getCurrentLocation (),
1584
+ result.operands )))
1585
+ return failure ();
1586
+
1587
+ // Propagate the types into the region arguments.
1588
+ for (size_t i = 0 , e = regionArgs.size (); i != e; ++i)
1589
+ regionArgs[i].type = functionType.getInput (i);
1590
+
1591
+ return failure (parser.parseRegion (*cond, regionArgs) ||
1592
+ parser.parseKeyword (" do" ) || parser.parseRegion (*body) ||
1593
+ parser.parseOptionalAttrDictWithKeyword (result.attributes ));
1594
+ }
1595
+
1596
+ static void printInitializationList (OpAsmPrinter &parser,
1597
+ Block::BlockArgListType blocksArgs,
1598
+ ValueRange initializers,
1599
+ StringRef prefix = " " ) {
1600
+ assert (blocksArgs.size () == initializers.size () &&
1601
+ " expected same length of arguments and initializers" );
1602
+ if (initializers.empty ())
1603
+ return ;
1604
+
1605
+ parser << prefix << ' (' ;
1606
+ llvm::interleaveComma (
1607
+ llvm::zip (blocksArgs, initializers), parser,
1608
+ [&](auto it) { parser << std::get<0 >(it) << " = " << std::get<1 >(it); });
1609
+ parser << " )" ;
1610
+ }
1611
+
1612
+ void WhileOp::print (OpAsmPrinter &parser) {
1613
+ printInitializationList (parser, getCond ().front ().getArguments (), getInputs (),
1614
+ " " );
1615
+ parser << " : " ;
1616
+ parser.printFunctionalType (getInputs ().getTypes (), getResults ().getTypes ());
1617
+ parser << ' ' ;
1618
+ parser.printRegion (getCond (), /* printEntryBlockArgs=*/ false );
1619
+ parser << " do " ;
1620
+ parser.printRegion (getBody ());
1621
+ parser.printOptionalAttrDictWithKeyword ((*this )->getAttrs ());
1622
+ }
1623
+
1496
1624
// ===----------------------------------------------------------------------===//
1497
1625
// TOSA Attribute Definitions.
1498
1626
// ===----------------------------------------------------------------------===//
0 commit comments