| 1 | //===- Syntax.cpp - Custom syntax for Linalg transform ops ----------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mlir/Dialect/Linalg/TransformOps/Syntax.h" |
| 10 | #include "mlir/IR/OpImplementation.h" |
| 11 | #include "llvm/Support/InterleavedRange.h" |
| 12 | |
| 13 | using namespace mlir; |
| 14 | |
| 15 | ParseResult mlir::parseSemiFunctionType(OpAsmParser &parser, Type &argumentType, |
| 16 | Type &resultType, bool resultOptional) { |
| 17 | argumentType = resultType = nullptr; |
| 18 | |
| 19 | bool hasLParen = resultOptional ? parser.parseOptionalLParen().succeeded() |
| 20 | : parser.parseLParen().succeeded(); |
| 21 | if (!resultOptional && !hasLParen) |
| 22 | return failure(); |
| 23 | if (parser.parseType(result&: argumentType).failed()) |
| 24 | return failure(); |
| 25 | if (!hasLParen) |
| 26 | return success(); |
| 27 | |
| 28 | return failure(IsFailure: parser.parseRParen().failed() || |
| 29 | parser.parseArrow().failed() || |
| 30 | parser.parseType(result&: resultType).failed()); |
| 31 | } |
| 32 | |
| 33 | ParseResult mlir::parseSemiFunctionType(OpAsmParser &parser, Type &argumentType, |
| 34 | SmallVectorImpl<Type> &resultTypes) { |
| 35 | argumentType = nullptr; |
| 36 | bool hasLParen = parser.parseOptionalLParen().succeeded(); |
| 37 | if (parser.parseType(result&: argumentType).failed()) |
| 38 | return failure(); |
| 39 | if (!hasLParen) |
| 40 | return success(); |
| 41 | |
| 42 | if (parser.parseRParen().failed() || parser.parseArrow().failed()) |
| 43 | return failure(); |
| 44 | |
| 45 | if (parser.parseOptionalLParen().failed()) { |
| 46 | Type type; |
| 47 | if (parser.parseType(result&: type).failed()) |
| 48 | return failure(); |
| 49 | resultTypes.push_back(Elt: type); |
| 50 | return success(); |
| 51 | } |
| 52 | if (parser.parseTypeList(result&: resultTypes).failed() || |
| 53 | parser.parseRParen().failed()) { |
| 54 | resultTypes.clear(); |
| 55 | return failure(); |
| 56 | } |
| 57 | return success(); |
| 58 | } |
| 59 | |
| 60 | void mlir::printSemiFunctionType(OpAsmPrinter &printer, Operation *op, |
| 61 | Type argumentType, TypeRange resultType) { |
| 62 | if (!resultType.empty()) |
| 63 | printer << "(" ; |
| 64 | printer << argumentType; |
| 65 | if (resultType.empty()) |
| 66 | return; |
| 67 | printer << ") -> " ; |
| 68 | |
| 69 | if (resultType.size() > 1) |
| 70 | printer << "(" ; |
| 71 | printer << llvm::interleaved(R: resultType); |
| 72 | if (resultType.size() > 1) |
| 73 | printer << ")" ; |
| 74 | } |
| 75 | |
| 76 | void mlir::printSemiFunctionType(OpAsmPrinter &printer, Operation *op, |
| 77 | Type argumentType, Type resultType, |
| 78 | bool resultOptional) { |
| 79 | assert(resultOptional || resultType != nullptr); |
| 80 | return printSemiFunctionType(printer, op, argumentType, |
| 81 | resultType: resultType ? TypeRange(resultType) |
| 82 | : TypeRange()); |
| 83 | } |
| 84 | |