| 1 | //===- SPIRVOpDefinition.cpp - MLIR SPIR-V Op Definition Implementation ---===// |
| 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 | // Defines helpers referenced by the TableGen-generated SPIR-V operation |
| 10 | // implementations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" |
| 15 | |
| 16 | #include "SPIRVOpDefinition.h" |
| 17 | #include "SPIRVParsingUtils.h" |
| 18 | |
| 19 | #include "mlir/IR/TypeUtilities.h" |
| 20 | |
| 21 | namespace mlir::spirv { |
| 22 | /// Returns true if the given op is a function-like op or nested in a |
| 23 | /// function-like op without a module-like op in the middle. |
| 24 | bool isNestedInFunctionOpInterface(Operation *op) { |
| 25 | if (!op) |
| 26 | return false; |
| 27 | if (op->hasTrait<OpTrait::SymbolTable>()) |
| 28 | return false; |
| 29 | if (isa<FunctionOpInterface>(Val: op)) |
| 30 | return true; |
| 31 | return isNestedInFunctionOpInterface(op: op->getParentOp()); |
| 32 | } |
| 33 | |
| 34 | /// Returns true if the given op is a GraphARM op or nested in a |
| 35 | /// GraphARM op without a module-like op in the middle. |
| 36 | bool isNestedInGraphARMOpInterface(Operation *op) { |
| 37 | if (!op) |
| 38 | return false; |
| 39 | if (op->hasTrait<OpTrait::SymbolTable>()) |
| 40 | return false; |
| 41 | if (isa<spirv::GraphARMOp>(Val: op)) |
| 42 | return true; |
| 43 | return isNestedInGraphARMOpInterface(op: op->getParentOp()); |
| 44 | } |
| 45 | |
| 46 | /// Returns true if the given op is an module-like op that maintains a symbol |
| 47 | /// table. |
| 48 | bool isDirectInModuleLikeOp(Operation *op) { |
| 49 | return op && op->hasTrait<OpTrait::SymbolTable>(); |
| 50 | } |
| 51 | |
| 52 | /// Returns a boolean scalar or vector type matching the shape of the given |
| 53 | /// type. Scalar inputs yield i1, vector inputs yield vector<Nxi1>. |
| 54 | Type getMatchingBoolType(Type operandType) { |
| 55 | Builder builder(operandType.getContext()); |
| 56 | Type resultType = builder.getIntegerType(width: 1); |
| 57 | if (auto vecType = dyn_cast<VectorType>(Val&: operandType)) |
| 58 | return VectorType::get(shape: vecType.getNumElements(), elementType: resultType); |
| 59 | return resultType; |
| 60 | } |
| 61 | |
| 62 | ParseResult parseImageOperands(OpAsmParser &parser, |
| 63 | spirv::ImageOperandsAttr &attr) { |
| 64 | // Expect image operands |
| 65 | if (parser.parseOptionalLSquare()) |
| 66 | return success(); |
| 67 | |
| 68 | spirv::ImageOperands imageOperands; |
| 69 | if (parseEnumStrAttr(value&: imageOperands, parser)) |
| 70 | return failure(); |
| 71 | |
| 72 | attr = spirv::ImageOperandsAttr::get(context: parser.getContext(), value: imageOperands); |
| 73 | |
| 74 | return parser.parseRSquare(); |
| 75 | } |
| 76 | |
| 77 | void printImageOperands(OpAsmPrinter &printer, Operation *imageOp, |
| 78 | spirv::ImageOperandsAttr attr) { |
| 79 | if (attr) { |
| 80 | auto strImageOperands = stringifyImageOperands(attr.getValue()); |
| 81 | printer << "[\"" << strImageOperands << "\"]" ; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// Adapted from the cf.switch implementation. |
| 86 | /// <cases> ::= `default` `:` bb-id (`(` ssa-use-and-type-list `)`)? |
| 87 | /// ( `,` integer `:` bb-id (`(` ssa-use-and-type-list `)`)? )* |
| 88 | ParseResult parseSwitchOpCases( |
| 89 | OpAsmParser &parser, Type &selectorType, Block *&defaultTarget, |
| 90 | SmallVectorImpl<OpAsmParser::UnresolvedOperand> &defaultOperands, |
| 91 | SmallVectorImpl<Type> &defaultOperandTypes, DenseIntElementsAttr &literals, |
| 92 | SmallVectorImpl<Block *> &targets, |
| 93 | SmallVectorImpl<SmallVector<OpAsmParser::UnresolvedOperand>> |
| 94 | &targetOperands, |
| 95 | SmallVectorImpl<SmallVector<Type>> &targetOperandTypes) { |
| 96 | if (parser.parseKeyword(keyword: "default" ) || parser.parseColon() || |
| 97 | parser.parseSuccessor(dest&: defaultTarget)) |
| 98 | return failure(); |
| 99 | if (succeeded(Result: parser.parseOptionalLParen())) { |
| 100 | if (parser.parseOperandList(result&: defaultOperands, delimiter: OpAsmParser::Delimiter::None, |
| 101 | /*allowResultNumber=*/false) || |
| 102 | parser.parseColonTypeList(result&: defaultOperandTypes) || parser.parseRParen()) |
| 103 | return failure(); |
| 104 | } |
| 105 | |
| 106 | SmallVector<APInt> values; |
| 107 | unsigned bitWidth = selectorType.getIntOrFloatBitWidth(); |
| 108 | while (succeeded(Result: parser.parseOptionalComma())) { |
| 109 | SMLoc valueLoc = parser.getCurrentLocation(); |
| 110 | APInt value; |
| 111 | if (failed(Result: parser.parseInteger(result&: value))) |
| 112 | return failure(); |
| 113 | if (value.isNegative() ? !value.isSignedIntN(N: bitWidth) |
| 114 | : !value.isIntN(N: bitWidth)) |
| 115 | return parser.emitError(loc: valueLoc) |
| 116 | << "literal is out of range for " << selectorType; |
| 117 | values.push_back(Elt: value.sextOrTrunc(width: bitWidth)); |
| 118 | |
| 119 | Block *target; |
| 120 | SmallVector<OpAsmParser::UnresolvedOperand> operands; |
| 121 | SmallVector<Type> operandTypes; |
| 122 | if (failed(Result: parser.parseColon()) || failed(Result: parser.parseSuccessor(dest&: target))) |
| 123 | return failure(); |
| 124 | if (succeeded(Result: parser.parseOptionalLParen())) { |
| 125 | if (failed(Result: parser.parseOperandList(result&: operands, |
| 126 | delimiter: OpAsmParser::Delimiter::None)) || |
| 127 | failed(Result: parser.parseColonTypeList(result&: operandTypes)) || |
| 128 | failed(Result: parser.parseRParen())) |
| 129 | return failure(); |
| 130 | } |
| 131 | targets.push_back(Elt: target); |
| 132 | targetOperands.emplace_back(Args&: operands); |
| 133 | targetOperandTypes.emplace_back(Args&: operandTypes); |
| 134 | } |
| 135 | |
| 136 | if (!values.empty()) { |
| 137 | ShapedType literalType = |
| 138 | VectorType::get(shape: static_cast<int64_t>(values.size()), elementType: selectorType); |
| 139 | literals = DenseIntElementsAttr::get(type: literalType, arg&: values); |
| 140 | } |
| 141 | return success(); |
| 142 | } |
| 143 | |
| 144 | void printSwitchOpCases(OpAsmPrinter &p, SwitchOp op, Type selectorType, |
| 145 | Block *defaultTarget, OperandRange defaultOperands, |
| 146 | TypeRange defaultOperandTypes, |
| 147 | DenseIntElementsAttr literals, SuccessorRange targets, |
| 148 | OperandRangeRange targetOperands, |
| 149 | const TypeRangeRange &targetOperandTypes) { |
| 150 | p << " default: " ; |
| 151 | p.printSuccessorAndUseList(successor: defaultTarget, succOperands: defaultOperands); |
| 152 | |
| 153 | if (!literals) |
| 154 | return; |
| 155 | |
| 156 | for (auto [index, literal] : llvm::enumerate(First: literals.getValues<APInt>())) { |
| 157 | p << ','; |
| 158 | p.printNewline(); |
| 159 | p << " " ; |
| 160 | p << literal; |
| 161 | p << ": " ; |
| 162 | p.printSuccessorAndUseList(successor: targets[index], succOperands: targetOperands[index]); |
| 163 | } |
| 164 | p.printNewline(); |
| 165 | } |
| 166 | |
| 167 | } // namespace mlir::spirv |
| 168 | |