Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 1 | //===- StandardOps.cpp - Standard MLIR Operations -------------------------===// |
| 2 | // |
| 3 | // Copyright 2019 The MLIR Authors. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // ============================================================================= |
| 17 | |
| 18 | #include "mlir/IR/StandardOps.h" |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 19 | #include "mlir/IR/AffineMap.h" |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 20 | #include "mlir/IR/Builders.h" |
Chris Lattner | dd0c2ca | 2018-07-24 16:07:22 -0700 | [diff] [blame] | 21 | #include "mlir/IR/OpImplementation.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 22 | #include "mlir/IR/OperationSet.h" |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 23 | #include "mlir/IR/SSAValue.h" |
| 24 | #include "mlir/IR/Types.h" |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 25 | #include "mlir/Support/STLExtras.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 27 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 28 | using namespace mlir; |
| 29 | |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 30 | static void printDimAndSymbolList(Operation::const_operand_iterator begin, |
| 31 | Operation::const_operand_iterator end, |
| 32 | unsigned numDims, OpAsmPrinter *p) { |
| 33 | *p << '('; |
| 34 | p->printOperands(begin, begin + numDims); |
| 35 | *p << ')'; |
| 36 | |
| 37 | if (begin + numDims != end) { |
| 38 | *p << '['; |
| 39 | p->printOperands(begin + numDims, end); |
| 40 | *p << ']'; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Parses dimension and symbol list, and sets 'numDims' to the number of |
| 45 | // dimension operands parsed. |
| 46 | // Returns 'false' on success and 'true' on error. |
| 47 | static bool |
| 48 | parseDimAndSymbolList(OpAsmParser *parser, |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 49 | SmallVector<SSAValue *, 4> &operands, unsigned &numDims) { |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 50 | SmallVector<OpAsmParser::OperandType, 8> opInfos; |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 51 | if (parser->parseOperandList(opInfos, -1, OpAsmParser::Delimiter::Paren)) |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 52 | return true; |
| 53 | // Store number of dimensions for validation by caller. |
| 54 | numDims = opInfos.size(); |
| 55 | |
| 56 | // Parse the optional symbol operands. |
| 57 | auto *affineIntTy = parser->getBuilder().getAffineIntType(); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 58 | if (parser->parseOperandList(opInfos, -1, |
| 59 | OpAsmParser::Delimiter::OptionalSquare) || |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 60 | parser->resolveOperands(opInfos, affineIntTy, operands)) |
| 61 | return true; |
| 62 | return false; |
| 63 | } |
| 64 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 65 | //===----------------------------------------------------------------------===// |
| 66 | // AddFOp |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 69 | void AddFOp::build(Builder *builder, OperationState *result, SSAValue *lhs, |
| 70 | SSAValue *rhs) { |
| 71 | assert(lhs->getType() == rhs->getType()); |
| 72 | result->addOperands({lhs, rhs}); |
| 73 | result->types.push_back(lhs->getType()); |
| 74 | } |
| 75 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 76 | bool AddFOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 77 | SmallVector<OpAsmParser::OperandType, 2> ops; |
| 78 | Type *type; |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 79 | return parser->parseOperandList(ops, 2) || |
| 80 | parser->parseOptionalAttributeDict(result->attributes) || |
| 81 | parser->parseColonType(type) || |
| 82 | parser->resolveOperands(ops, type, result->operands) || |
| 83 | parser->addTypeToList(type, result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | dd0c2ca | 2018-07-24 16:07:22 -0700 | [diff] [blame] | 86 | void AddFOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 87 | *p << "addf " << *getOperand(0) << ", " << *getOperand(1); |
| 88 | p->printOptionalAttrDict(getAttrs()); |
| 89 | *p << " : " << *getType(); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 92 | // TODO: Have verify functions return std::string to enable more descriptive |
| 93 | // error messages. |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 94 | // Return an error message on failure. |
| 95 | const char *AddFOp::verify() const { |
| 96 | // TODO: Check that the types of the LHS and RHS match. |
| 97 | // TODO: This should be a refinement of TwoOperands. |
| 98 | // TODO: There should also be a OneResultWhoseTypeMatchesFirstOperand. |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 102 | //===----------------------------------------------------------------------===// |
| 103 | // AffineApplyOp |
| 104 | //===----------------------------------------------------------------------===// |
| 105 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 106 | bool AffineApplyOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 107 | auto &builder = parser->getBuilder(); |
| 108 | auto *affineIntTy = builder.getAffineIntType(); |
| 109 | |
| 110 | AffineMapAttr *mapAttr; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 111 | unsigned numDims; |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 112 | if (parser->parseAttribute(mapAttr, "map", result->attributes) || |
| 113 | parseDimAndSymbolList(parser, result->operands, numDims) || |
| 114 | parser->parseOptionalAttributeDict(result->attributes)) |
| 115 | return true; |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 116 | auto *map = mapAttr->getValue(); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 117 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 118 | if (map->getNumDims() != numDims || |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 119 | numDims + map->getNumSymbols() != result->operands.size()) { |
| 120 | return parser->emitError(parser->getNameLoc(), |
| 121 | "dimension or symbol index mismatch"); |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 124 | result->types.append(map->getNumResults(), affineIntTy); |
| 125 | return false; |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void AffineApplyOp::print(OpAsmPrinter *p) const { |
| 129 | auto *map = getAffineMap(); |
| 130 | *p << "affine_apply " << *map; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 131 | printDimAndSymbolList(operand_begin(), operand_end(), map->getNumDims(), p); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 132 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map"); |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | const char *AffineApplyOp::verify() const { |
| 136 | // Check that affine map attribute was specified. |
| 137 | auto *affineMapAttr = getAttrOfType<AffineMapAttr>("map"); |
| 138 | if (!affineMapAttr) |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 139 | return "requires an affine map"; |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 140 | |
| 141 | // Check input and output dimensions match. |
| 142 | auto *map = affineMapAttr->getValue(); |
| 143 | |
| 144 | // Verify that operand count matches affine map dimension and symbol count. |
| 145 | if (getNumOperands() != map->getNumDims() + map->getNumSymbols()) |
| 146 | return "operand count and affine map dimension and symbol count must match"; |
| 147 | |
| 148 | // Verify that result count matches affine map result count. |
| 149 | if (getNumResults() != map->getNumResults()) |
| 150 | return "result count and affine map result count must match"; |
| 151 | |
| 152 | return nullptr; |
| 153 | } |
| 154 | |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 155 | // The result of the affine apply operation can be used as a dimension id if it |
| 156 | // is a CFG value or if it is an MLValue, and all the operands are valid |
| 157 | // dimension ids. |
| 158 | bool AffineApplyOp::isValidDim() const { |
| 159 | for (auto *op : getOperands()) { |
| 160 | if (auto *v = dyn_cast<MLValue>(op)) |
| 161 | if (!v->isValidDim()) |
| 162 | return false; |
| 163 | } |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | // The result of the affine apply operation can be used as a symbol if it is |
| 168 | // a CFG value or if it is an MLValue, and all the operands are symbols. |
| 169 | bool AffineApplyOp::isValidSymbol() const { |
| 170 | for (auto *op : getOperands()) { |
| 171 | if (auto *v = dyn_cast<MLValue>(op)) |
| 172 | if (!v->isValidSymbol()) |
| 173 | return false; |
| 174 | } |
| 175 | return true; |
| 176 | } |
| 177 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 178 | //===----------------------------------------------------------------------===// |
| 179 | // AllocOp |
| 180 | //===----------------------------------------------------------------------===// |
| 181 | |
MLIR Team | 3802ebd | 2018-08-31 14:49:38 -0700 | [diff] [blame] | 182 | void AllocOp::build(Builder *builder, OperationState *result, |
| 183 | MemRefType *memrefType, ArrayRef<SSAValue *> operands) { |
| 184 | result->addOperands(operands); |
| 185 | result->types.push_back(memrefType); |
| 186 | } |
| 187 | |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 188 | void AllocOp::print(OpAsmPrinter *p) const { |
| 189 | MemRefType *type = cast<MemRefType>(getMemRef()->getType()); |
| 190 | *p << "alloc"; |
| 191 | // Print dynamic dimension operands. |
| 192 | printDimAndSymbolList(operand_begin(), operand_end(), |
| 193 | type->getNumDynamicDims(), p); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 194 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 195 | *p << " : " << *type; |
| 196 | } |
| 197 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 198 | bool AllocOp::parse(OpAsmParser *parser, OperationState *result) { |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 199 | MemRefType *type; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 200 | |
Chris Lattner | 7d3b77c | 2018-07-31 16:21:36 -0700 | [diff] [blame] | 201 | // Parse the dimension operands and optional symbol operands, followed by a |
| 202 | // memref type. |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 203 | unsigned numDimOperands; |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 204 | if (parseDimAndSymbolList(parser, result->operands, numDimOperands) || |
| 205 | parser->parseOptionalAttributeDict(result->attributes) || |
| 206 | parser->parseColonType(type)) |
| 207 | return true; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 208 | |
| 209 | // Check numDynamicDims against number of question marks in memref type. |
| 210 | if (numDimOperands != type->getNumDynamicDims()) { |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 211 | return parser->emitError(parser->getNameLoc(), |
| 212 | "dimension operand count does not equal memref " |
| 213 | "dynamic dimension count"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // Check that the number of symbol operands matches the number of symbols in |
| 217 | // the first affinemap of the memref's affine map composition. |
| 218 | // Note that a memref must specify at least one affine map in the composition. |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 219 | if (result->operands.size() - numDimOperands != |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 220 | type->getAffineMaps()[0]->getNumSymbols()) { |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 221 | return parser->emitError( |
| 222 | parser->getNameLoc(), |
| 223 | "affine map symbol operand count does not equal memref affine map " |
| 224 | "symbol count"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 227 | result->types.push_back(type); |
| 228 | return false; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | const char *AllocOp::verify() const { |
| 232 | // TODO(andydavis): Verify alloc. |
| 233 | return nullptr; |
| 234 | } |
| 235 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 236 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 237 | // CallOp |
| 238 | //===----------------------------------------------------------------------===// |
| 239 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 240 | void CallOp::build(Builder *builder, OperationState *result, Function *callee, |
| 241 | ArrayRef<SSAValue *> operands) { |
| 242 | result->addOperands(operands); |
| 243 | result->addAttribute("callee", builder->getFunctionAttr(callee)); |
| 244 | result->addTypes(callee->getType()->getResults()); |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | bool CallOp::parse(OpAsmParser *parser, OperationState *result) { |
| 248 | StringRef calleeName; |
| 249 | llvm::SMLoc calleeLoc; |
| 250 | FunctionType *calleeType = nullptr; |
| 251 | SmallVector<OpAsmParser::OperandType, 4> operands; |
| 252 | Function *callee = nullptr; |
| 253 | if (parser->parseFunctionName(calleeName, calleeLoc) || |
| 254 | parser->parseOperandList(operands, /*requiredOperandCount=*/-1, |
| 255 | OpAsmParser::Delimiter::Paren) || |
| 256 | parser->parseOptionalAttributeDict(result->attributes) || |
| 257 | parser->parseColonType(calleeType) || |
| 258 | parser->resolveFunctionName(calleeName, calleeType, calleeLoc, callee) || |
| 259 | parser->addTypesToList(calleeType->getResults(), result->types) || |
| 260 | parser->resolveOperands(operands, calleeType->getInputs(), calleeLoc, |
| 261 | result->operands)) |
| 262 | return true; |
| 263 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 264 | result->addAttribute("callee", parser->getBuilder().getFunctionAttr(callee)); |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | |
| 268 | void CallOp::print(OpAsmPrinter *p) const { |
| 269 | *p << "call "; |
| 270 | p->printFunctionReference(getCallee()); |
| 271 | *p << '('; |
| 272 | p->printOperands(getOperands()); |
| 273 | *p << ')'; |
| 274 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"callee"); |
| 275 | *p << " : " << *getCallee()->getType(); |
| 276 | } |
| 277 | |
| 278 | const char *CallOp::verify() const { |
| 279 | // Check that the callee attribute was specified. |
| 280 | auto *fnAttr = getAttrOfType<FunctionAttr>("callee"); |
| 281 | if (!fnAttr) |
| 282 | return "requires a 'callee' function attribute"; |
| 283 | |
| 284 | // Verify that the operand and result types match the callee. |
| 285 | auto *fnType = fnAttr->getValue()->getType(); |
| 286 | if (fnType->getNumInputs() != getNumOperands()) |
| 287 | return "incorrect number of operands for callee"; |
| 288 | |
| 289 | for (unsigned i = 0, e = fnType->getNumInputs(); i != e; ++i) { |
| 290 | if (getOperand(i)->getType() != fnType->getInput(i)) |
| 291 | return "operand type mismatch"; |
| 292 | } |
| 293 | |
| 294 | if (fnType->getNumResults() != getNumResults()) |
| 295 | return "incorrect number of results for callee"; |
| 296 | |
| 297 | for (unsigned i = 0, e = fnType->getNumResults(); i != e; ++i) { |
| 298 | if (getResult(i)->getType() != fnType->getResult(i)) |
| 299 | return "result type mismatch"; |
| 300 | } |
| 301 | |
| 302 | return nullptr; |
| 303 | } |
| 304 | |
| 305 | //===----------------------------------------------------------------------===// |
| 306 | // CallIndirectOp |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 309 | void CallIndirectOp::build(Builder *builder, OperationState *result, |
| 310 | SSAValue *callee, ArrayRef<SSAValue *> operands) { |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 311 | auto *fnType = cast<FunctionType>(callee->getType()); |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 312 | result->operands.push_back(callee); |
| 313 | result->addOperands(operands); |
| 314 | result->addTypes(fnType->getResults()); |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | bool CallIndirectOp::parse(OpAsmParser *parser, OperationState *result) { |
| 318 | FunctionType *calleeType = nullptr; |
| 319 | OpAsmParser::OperandType callee; |
| 320 | llvm::SMLoc operandsLoc; |
| 321 | SmallVector<OpAsmParser::OperandType, 4> operands; |
| 322 | return parser->parseOperand(callee) || |
| 323 | parser->getCurrentLocation(&operandsLoc) || |
| 324 | parser->parseOperandList(operands, /*requiredOperandCount=*/-1, |
| 325 | OpAsmParser::Delimiter::Paren) || |
| 326 | parser->parseOptionalAttributeDict(result->attributes) || |
| 327 | parser->parseColonType(calleeType) || |
| 328 | parser->resolveOperand(callee, calleeType, result->operands) || |
| 329 | parser->resolveOperands(operands, calleeType->getInputs(), operandsLoc, |
| 330 | result->operands) || |
| 331 | parser->addTypesToList(calleeType->getResults(), result->types); |
| 332 | } |
| 333 | |
| 334 | void CallIndirectOp::print(OpAsmPrinter *p) const { |
| 335 | *p << "call_indirect "; |
| 336 | p->printOperand(getCallee()); |
| 337 | *p << '('; |
| 338 | auto operandRange = getOperands(); |
| 339 | p->printOperands(++operandRange.begin(), operandRange.end()); |
| 340 | *p << ')'; |
| 341 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"callee"); |
| 342 | *p << " : " << *getCallee()->getType(); |
| 343 | } |
| 344 | |
| 345 | const char *CallIndirectOp::verify() const { |
| 346 | // The callee must be a function. |
| 347 | auto *fnType = dyn_cast<FunctionType>(getCallee()->getType()); |
| 348 | if (!fnType) |
| 349 | return "callee must have function type"; |
| 350 | |
| 351 | // Verify that the operand and result types match the callee. |
| 352 | if (fnType->getNumInputs() != getNumOperands() - 1) |
| 353 | return "incorrect number of operands for callee"; |
| 354 | |
| 355 | for (unsigned i = 0, e = fnType->getNumInputs(); i != e; ++i) { |
| 356 | if (getOperand(i + 1)->getType() != fnType->getInput(i)) |
| 357 | return "operand type mismatch"; |
| 358 | } |
| 359 | |
| 360 | if (fnType->getNumResults() != getNumResults()) |
| 361 | return "incorrect number of results for callee"; |
| 362 | |
| 363 | for (unsigned i = 0, e = fnType->getNumResults(); i != e; ++i) { |
| 364 | if (getResult(i)->getType() != fnType->getResult(i)) |
| 365 | return "result type mismatch"; |
| 366 | } |
| 367 | |
| 368 | return nullptr; |
| 369 | } |
| 370 | |
| 371 | //===----------------------------------------------------------------------===// |
| 372 | // Constant*Op |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 373 | //===----------------------------------------------------------------------===// |
| 374 | |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 375 | void ConstantOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 376 | *p << "constant " << *getValue(); |
| 377 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"value"); |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 378 | |
| 379 | if (!isa<FunctionAttr>(getValue())) |
| 380 | *p << " : " << *getType(); |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 383 | bool ConstantOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 384 | Attribute *valueAttr; |
| 385 | Type *type; |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 386 | |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 387 | if (parser->parseAttribute(valueAttr, "value", result->attributes) || |
| 388 | parser->parseOptionalAttributeDict(result->attributes)) |
| 389 | return true; |
| 390 | |
| 391 | // 'constant' taking a function reference doesn't get a redundant type |
| 392 | // specifier. The attribute itself carries it. |
| 393 | if (auto *fnAttr = dyn_cast<FunctionAttr>(valueAttr)) |
| 394 | return parser->addTypeToList(fnAttr->getValue()->getType(), result->types); |
| 395 | |
| 396 | return parser->parseColonType(type) || |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 397 | parser->addTypeToList(type, result->types); |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 400 | /// The constant op requires an attribute, and furthermore requires that it |
| 401 | /// matches the return type. |
| 402 | const char *ConstantOp::verify() const { |
| 403 | auto *value = getValue(); |
| 404 | if (!value) |
| 405 | return "requires a 'value' attribute"; |
| 406 | |
| 407 | auto *type = this->getType(); |
Chris Lattner | 1ec7057 | 2018-07-24 10:41:30 -0700 | [diff] [blame] | 408 | if (isa<IntegerType>(type) || type->isAffineInt()) { |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 409 | if (!isa<IntegerAttr>(value)) |
| 410 | return "requires 'value' to be an integer for an integer result type"; |
| 411 | return nullptr; |
| 412 | } |
| 413 | |
Chris Lattner | 7ba98c6 | 2018-08-16 16:56:40 -0700 | [diff] [blame] | 414 | if (isa<FloatType>(type)) { |
| 415 | if (!isa<FloatAttr>(value)) |
| 416 | return "requires 'value' to be a floating point constant"; |
| 417 | return nullptr; |
| 418 | } |
| 419 | |
| 420 | if (type->isTFString()) { |
| 421 | if (!isa<StringAttr>(value)) |
| 422 | return "requires 'value' to be a string constant"; |
| 423 | return nullptr; |
| 424 | } |
| 425 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 426 | if (isa<FunctionType>(type)) { |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 427 | if (!isa<FunctionAttr>(value)) |
| 428 | return "requires 'value' to be a function reference"; |
| 429 | return nullptr; |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | return "requires a result type that aligns with the 'value' attribute"; |
| 433 | } |
| 434 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 435 | void ConstantFloatOp::build(Builder *builder, OperationState *result, |
| 436 | double value, FloatType *type) { |
| 437 | result->addAttribute("value", builder->getFloatAttr(value)); |
| 438 | result->types.push_back(type); |
Chris Lattner | 7ba98c6 | 2018-08-16 16:56:40 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | bool ConstantFloatOp::isClassFor(const Operation *op) { |
| 442 | return ConstantOp::isClassFor(op) && |
| 443 | isa<FloatType>(op->getResult(0)->getType()); |
| 444 | } |
| 445 | |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 446 | /// ConstantIntOp only matches values whose result type is an IntegerType. |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 447 | bool ConstantIntOp::isClassFor(const Operation *op) { |
| 448 | return ConstantOp::isClassFor(op) && |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 449 | isa<IntegerType>(op->getResult(0)->getType()); |
| 450 | } |
| 451 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 452 | void ConstantIntOp::build(Builder *builder, OperationState *result, |
| 453 | int64_t value, unsigned width) { |
| 454 | result->addAttribute("value", builder->getIntegerAttr(value)); |
| 455 | result->types.push_back(builder->getIntegerType(width)); |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /// ConstantAffineIntOp only matches values whose result type is AffineInt. |
| 459 | bool ConstantAffineIntOp::isClassFor(const Operation *op) { |
| 460 | return ConstantOp::isClassFor(op) && |
| 461 | op->getResult(0)->getType()->isAffineInt(); |
| 462 | } |
| 463 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 464 | void ConstantAffineIntOp::build(Builder *builder, OperationState *result, |
| 465 | int64_t value) { |
| 466 | result->addAttribute("value", builder->getIntegerAttr(value)); |
| 467 | result->types.push_back(builder->getAffineIntType()); |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 470 | //===----------------------------------------------------------------------===// |
Uday Bondhugula | 6770171 | 2018-08-21 16:01:23 -0700 | [diff] [blame] | 471 | // AffineApplyOp |
| 472 | //===----------------------------------------------------------------------===// |
| 473 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 474 | void AffineApplyOp::build(Builder *builder, OperationState *result, |
| 475 | AffineMap *map, ArrayRef<SSAValue *> operands) { |
| 476 | result->addOperands(operands); |
| 477 | result->types.append(map->getNumResults(), builder->getAffineIntType()); |
| 478 | result->addAttribute("map", builder->getAffineMapAttr(map)); |
Uday Bondhugula | 6770171 | 2018-08-21 16:01:23 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | //===----------------------------------------------------------------------===// |
MLIR Team | 1989cc1 | 2018-08-15 15:39:26 -0700 | [diff] [blame] | 482 | // DeallocOp |
| 483 | //===----------------------------------------------------------------------===// |
| 484 | |
MLIR Team | 3802ebd | 2018-08-31 14:49:38 -0700 | [diff] [blame] | 485 | void DeallocOp::build(Builder *builder, OperationState *result, |
| 486 | SSAValue *memref) { |
| 487 | result->addOperands(memref); |
| 488 | } |
| 489 | |
MLIR Team | 1989cc1 | 2018-08-15 15:39:26 -0700 | [diff] [blame] | 490 | void DeallocOp::print(OpAsmPrinter *p) const { |
| 491 | *p << "dealloc " << *getMemRef() << " : " << *getMemRef()->getType(); |
| 492 | } |
| 493 | |
| 494 | bool DeallocOp::parse(OpAsmParser *parser, OperationState *result) { |
| 495 | OpAsmParser::OperandType memrefInfo; |
| 496 | MemRefType *type; |
| 497 | |
| 498 | return parser->parseOperand(memrefInfo) || parser->parseColonType(type) || |
| 499 | parser->resolveOperand(memrefInfo, type, result->operands); |
| 500 | } |
| 501 | |
| 502 | const char *DeallocOp::verify() const { |
| 503 | if (!isa<MemRefType>(getMemRef()->getType())) |
| 504 | return "operand must be a memref"; |
| 505 | return nullptr; |
| 506 | } |
| 507 | |
| 508 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 509 | // DimOp |
| 510 | //===----------------------------------------------------------------------===// |
| 511 | |
Chris Lattner | dd0c2ca | 2018-07-24 16:07:22 -0700 | [diff] [blame] | 512 | void DimOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 513 | *p << "dim " << *getOperand() << ", " << getIndex(); |
| 514 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"index"); |
| 515 | *p << " : " << *getOperand()->getType(); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 518 | bool DimOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 519 | OpAsmParser::OperandType operandInfo; |
| 520 | IntegerAttr *indexAttr; |
| 521 | Type *type; |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 522 | |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 523 | return parser->parseOperand(operandInfo) || parser->parseComma() || |
| 524 | parser->parseAttribute(indexAttr, "index", result->attributes) || |
| 525 | parser->parseOptionalAttributeDict(result->attributes) || |
| 526 | parser->parseColonType(type) || |
| 527 | parser->resolveOperand(operandInfo, type, result->operands) || |
| 528 | parser->addTypeToList(parser->getBuilder().getAffineIntType(), |
| 529 | result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 532 | const char *DimOp::verify() const { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 533 | // Check that we have an integer index operand. |
| 534 | auto indexAttr = getAttrOfType<IntegerAttr>("index"); |
| 535 | if (!indexAttr) |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 536 | return "requires an integer attribute named 'index'"; |
| 537 | uint64_t index = (uint64_t)indexAttr->getValue(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 538 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 539 | auto *type = getOperand()->getType(); |
| 540 | if (auto *tensorType = dyn_cast<RankedTensorType>(type)) { |
| 541 | if (index >= tensorType->getRank()) |
| 542 | return "index is out of range"; |
| 543 | } else if (auto *memrefType = dyn_cast<MemRefType>(type)) { |
| 544 | if (index >= memrefType->getRank()) |
| 545 | return "index is out of range"; |
| 546 | |
| 547 | } else if (isa<UnrankedTensorType>(type)) { |
| 548 | // ok, assumed to be in-range. |
| 549 | } else { |
| 550 | return "requires an operand with tensor or memref type"; |
| 551 | } |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 552 | |
| 553 | return nullptr; |
| 554 | } |
| 555 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 556 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8c7feba | 2018-08-23 09:58:23 -0700 | [diff] [blame] | 557 | // ExtractElementOp |
| 558 | //===----------------------------------------------------------------------===// |
| 559 | |
| 560 | void ExtractElementOp::build(Builder *builder, OperationState *result, |
| 561 | SSAValue *aggregate, |
| 562 | ArrayRef<SSAValue *> indices) { |
| 563 | auto *aggregateType = cast<VectorOrTensorType>(aggregate->getType()); |
| 564 | result->addOperands(aggregate); |
| 565 | result->addOperands(indices); |
| 566 | result->types.push_back(aggregateType->getElementType()); |
| 567 | } |
| 568 | |
| 569 | void ExtractElementOp::print(OpAsmPrinter *p) const { |
| 570 | *p << "extract_element " << *getAggregate() << '['; |
| 571 | p->printOperands(getIndices()); |
| 572 | *p << ']'; |
| 573 | p->printOptionalAttrDict(getAttrs()); |
| 574 | *p << " : " << *getAggregate()->getType(); |
| 575 | } |
| 576 | |
| 577 | bool ExtractElementOp::parse(OpAsmParser *parser, OperationState *result) { |
| 578 | OpAsmParser::OperandType aggregateInfo; |
| 579 | SmallVector<OpAsmParser::OperandType, 4> indexInfo; |
| 580 | VectorOrTensorType *type; |
| 581 | |
| 582 | auto affineIntTy = parser->getBuilder().getAffineIntType(); |
| 583 | return parser->parseOperand(aggregateInfo) || |
| 584 | parser->parseOperandList(indexInfo, -1, |
| 585 | OpAsmParser::Delimiter::Square) || |
| 586 | parser->parseOptionalAttributeDict(result->attributes) || |
| 587 | parser->parseColonType(type) || |
| 588 | parser->resolveOperand(aggregateInfo, type, result->operands) || |
| 589 | parser->resolveOperands(indexInfo, affineIntTy, result->operands) || |
| 590 | parser->addTypeToList(type->getElementType(), result->types); |
| 591 | } |
| 592 | |
| 593 | const char *ExtractElementOp::verify() const { |
| 594 | if (getNumOperands() == 0) |
| 595 | return "expected an aggregate to index into"; |
| 596 | |
| 597 | auto *aggregateType = dyn_cast<VectorOrTensorType>(getAggregate()->getType()); |
| 598 | if (!aggregateType) |
| 599 | return "first operand must be a vector or tensor"; |
| 600 | |
| 601 | if (getResult()->getType() != aggregateType->getElementType()) |
| 602 | return "result type must match element type of aggregate"; |
| 603 | |
| 604 | for (auto *idx : getIndices()) |
| 605 | if (!idx->getType()->isAffineInt()) |
| 606 | return "index to extract_element must have 'affineint' type"; |
| 607 | |
| 608 | // Verify the # indices match if we have a ranked type. |
| 609 | auto aggregateRank = aggregateType->getRankIfPresent(); |
| 610 | if (aggregateRank != -1 && aggregateRank != getNumOperands() - 1) |
| 611 | return "incorrect number of indices for extract_element"; |
| 612 | |
| 613 | return nullptr; |
| 614 | } |
| 615 | |
| 616 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 617 | // LoadOp |
| 618 | //===----------------------------------------------------------------------===// |
| 619 | |
Chris Lattner | 8c7feba | 2018-08-23 09:58:23 -0700 | [diff] [blame] | 620 | void LoadOp::build(Builder *builder, OperationState *result, SSAValue *memref, |
| 621 | ArrayRef<SSAValue *> indices) { |
| 622 | auto *memrefType = cast<MemRefType>(memref->getType()); |
| 623 | result->addOperands(memref); |
| 624 | result->addOperands(indices); |
| 625 | result->types.push_back(memrefType->getElementType()); |
| 626 | } |
| 627 | |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 628 | void LoadOp::print(OpAsmPrinter *p) const { |
| 629 | *p << "load " << *getMemRef() << '['; |
| 630 | p->printOperands(getIndices()); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 631 | *p << ']'; |
| 632 | p->printOptionalAttrDict(getAttrs()); |
| 633 | *p << " : " << *getMemRef()->getType(); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 636 | bool LoadOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 637 | OpAsmParser::OperandType memrefInfo; |
| 638 | SmallVector<OpAsmParser::OperandType, 4> indexInfo; |
| 639 | MemRefType *type; |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 640 | |
| 641 | auto affineIntTy = parser->getBuilder().getAffineIntType(); |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 642 | return parser->parseOperand(memrefInfo) || |
| 643 | parser->parseOperandList(indexInfo, -1, |
| 644 | OpAsmParser::Delimiter::Square) || |
| 645 | parser->parseOptionalAttributeDict(result->attributes) || |
| 646 | parser->parseColonType(type) || |
| 647 | parser->resolveOperand(memrefInfo, type, result->operands) || |
| 648 | parser->resolveOperands(indexInfo, affineIntTy, result->operands) || |
| 649 | parser->addTypeToList(type->getElementType(), result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | const char *LoadOp::verify() const { |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 653 | if (getNumOperands() == 0) |
| 654 | return "expected a memref to load from"; |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 655 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 656 | auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType()); |
| 657 | if (!memRefType) |
| 658 | return "first operand must be a memref"; |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 659 | |
Chris Lattner | 8c7feba | 2018-08-23 09:58:23 -0700 | [diff] [blame] | 660 | if (getResult()->getType() != memRefType->getElementType()) |
| 661 | return "result type must match element type of memref"; |
| 662 | |
| 663 | if (memRefType->getRank() != getNumOperands() - 1) |
| 664 | return "incorrect number of indices for load"; |
| 665 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 666 | for (auto *idx : getIndices()) |
| 667 | if (!idx->getType()->isAffineInt()) |
| 668 | return "index to load must have 'affineint' type"; |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 669 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 670 | // TODO: Verify we have the right number of indices. |
MLIR Team | 39a3a60 | 2018-07-24 17:43:56 -0700 | [diff] [blame] | 671 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 672 | // TODO: in MLFunction verify that the indices are parameters, IV's, or the |
| 673 | // result of an affine_apply. |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 674 | return nullptr; |
| 675 | } |
| 676 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 677 | //===----------------------------------------------------------------------===// |
| 678 | // ReturnOp |
| 679 | //===----------------------------------------------------------------------===// |
| 680 | |
| 681 | bool ReturnOp::parse(OpAsmParser *parser, OperationState *result) { |
| 682 | SmallVector<OpAsmParser::OperandType, 2> opInfo; |
| 683 | SmallVector<Type *, 2> types; |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 684 | llvm::SMLoc loc; |
| 685 | return parser->getCurrentLocation(&loc) || parser->parseOperandList(opInfo) || |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 686 | (!opInfo.empty() && parser->parseColonTypeList(types)) || |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 687 | parser->resolveOperands(opInfo, types, loc, result->operands); |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | void ReturnOp::print(OpAsmPrinter *p) const { |
| 691 | *p << "return"; |
| 692 | if (getNumOperands() > 0) { |
| 693 | *p << " "; |
| 694 | p->printOperands(operand_begin(), operand_end()); |
| 695 | *p << " : "; |
| 696 | interleave(operand_begin(), operand_end(), |
MLIR Team | 6a220a6 | 2018-09-07 12:34:19 -0700 | [diff] [blame^] | 697 | [&](const SSAValue *e) { p->printType(e->getType()); }, |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 698 | [&]() { *p << ", "; }); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | const char *ReturnOp::verify() const { |
| 703 | // ReturnOp must be part of an ML function. |
| 704 | if (auto *stmt = dyn_cast<OperationStmt>(getOperation())) { |
Tatiana Shpeisman | 3abd6bd | 2018-08-16 20:19:44 -0700 | [diff] [blame] | 705 | StmtBlock *block = stmt->getBlock(); |
| 706 | if (!block || !isa<MLFunction>(block) || &block->back() != stmt) |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 707 | return "must be the last statement in the ML function"; |
| 708 | |
| 709 | // Return success. Checking that operand types match those in the function |
| 710 | // signature is performed in the ML function verifier. |
| 711 | return nullptr; |
| 712 | } |
Tatiana Shpeisman | b697ac1 | 2018-08-09 23:21:19 -0700 | [diff] [blame] | 713 | return "cannot occur in a CFG function"; |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0473e4b | 2018-09-06 17:31:21 -0700 | [diff] [blame] | 717 | // ShapeCastOp |
| 718 | //===----------------------------------------------------------------------===// |
| 719 | |
| 720 | void ShapeCastOp::build(Builder *builder, OperationState *result, |
| 721 | SSAValue *input, Type *resultType) { |
| 722 | result->addOperands(input); |
| 723 | result->addTypes(resultType); |
| 724 | } |
| 725 | |
| 726 | const char *ShapeCastOp::verify() const { |
| 727 | auto *opType = dyn_cast<TensorType>(getOperand()->getType()); |
| 728 | auto *resType = dyn_cast<TensorType>(getResult()->getType()); |
| 729 | if (!opType || !resType) |
| 730 | return "requires input and result types to be tensors"; |
| 731 | |
| 732 | if (opType == resType) |
| 733 | return "requires the input and result type to be different"; |
| 734 | |
| 735 | if (opType->getElementType() != resType->getElementType()) |
| 736 | return "requires input and result element types to be the same"; |
| 737 | |
| 738 | // If the source or destination are unranked, then the cast is valid. |
| 739 | auto *opRType = dyn_cast<RankedTensorType>(opType); |
| 740 | auto *resRType = dyn_cast<RankedTensorType>(resType); |
| 741 | if (!opRType || !resRType) |
| 742 | return nullptr; |
| 743 | |
| 744 | // If they are both ranked, they have to have the same rank, and any specified |
| 745 | // dimensions must match. |
| 746 | if (opRType->getRank() != resRType->getRank()) |
| 747 | return "requires input and result ranks to match"; |
| 748 | |
| 749 | for (unsigned i = 0, e = opRType->getRank(); i != e; ++i) { |
| 750 | int opDim = opRType->getDimSize(i), resultDim = resRType->getDimSize(i); |
| 751 | if (opDim != -1 && resultDim != -1 && opDim != resultDim) |
| 752 | return "requires static dimensions to match"; |
| 753 | } |
| 754 | |
| 755 | return nullptr; |
| 756 | } |
| 757 | |
| 758 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 759 | // StoreOp |
| 760 | //===----------------------------------------------------------------------===// |
| 761 | |
MLIR Team | 3802ebd | 2018-08-31 14:49:38 -0700 | [diff] [blame] | 762 | void StoreOp::build(Builder *builder, OperationState *result, |
| 763 | SSAValue *valueToStore, SSAValue *memref, |
| 764 | ArrayRef<SSAValue *> indices) { |
| 765 | result->addOperands(valueToStore); |
| 766 | result->addOperands(memref); |
| 767 | result->addOperands(indices); |
| 768 | } |
| 769 | |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 770 | void StoreOp::print(OpAsmPrinter *p) const { |
| 771 | *p << "store " << *getValueToStore(); |
| 772 | *p << ", " << *getMemRef() << '['; |
| 773 | p->printOperands(getIndices()); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 774 | *p << ']'; |
| 775 | p->printOptionalAttrDict(getAttrs()); |
| 776 | *p << " : " << *getMemRef()->getType(); |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 779 | bool StoreOp::parse(OpAsmParser *parser, OperationState *result) { |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 780 | OpAsmParser::OperandType storeValueInfo; |
| 781 | OpAsmParser::OperandType memrefInfo; |
| 782 | SmallVector<OpAsmParser::OperandType, 4> indexInfo; |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 783 | MemRefType *memrefType; |
| 784 | |
| 785 | auto affineIntTy = parser->getBuilder().getAffineIntType(); |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 786 | return parser->parseOperand(storeValueInfo) || parser->parseComma() || |
| 787 | parser->parseOperand(memrefInfo) || |
| 788 | parser->parseOperandList(indexInfo, -1, |
| 789 | OpAsmParser::Delimiter::Square) || |
| 790 | parser->parseOptionalAttributeDict(result->attributes) || |
| 791 | parser->parseColonType(memrefType) || |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 792 | parser->resolveOperand(storeValueInfo, memrefType->getElementType(), |
| 793 | result->operands) || |
| 794 | parser->resolveOperand(memrefInfo, memrefType, result->operands) || |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 795 | parser->resolveOperands(indexInfo, affineIntTy, result->operands); |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | const char *StoreOp::verify() const { |
| 799 | if (getNumOperands() < 2) |
| 800 | return "expected a value to store and a memref"; |
| 801 | |
| 802 | // Second operand is a memref type. |
| 803 | auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType()); |
| 804 | if (!memRefType) |
| 805 | return "second operand must be a memref"; |
| 806 | |
| 807 | // First operand must have same type as memref element type. |
| 808 | if (getValueToStore()->getType() != memRefType->getElementType()) |
| 809 | return "first operand must have same type memref element type "; |
| 810 | |
| 811 | if (getNumOperands() != 2 + memRefType->getRank()) |
| 812 | return "store index operand count not equal to memref rank"; |
| 813 | |
| 814 | for (auto *idx : getIndices()) |
| 815 | if (!idx->getType()->isAffineInt()) |
| 816 | return "index to load must have 'affineint' type"; |
| 817 | |
| 818 | // TODO: Verify we have the right number of indices. |
| 819 | |
| 820 | // TODO: in MLFunction verify that the indices are parameters, IV's, or the |
| 821 | // result of an affine_apply. |
| 822 | return nullptr; |
| 823 | } |
| 824 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 825 | //===----------------------------------------------------------------------===// |
| 826 | // Register operations. |
| 827 | //===----------------------------------------------------------------------===// |
| 828 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 829 | /// Install the standard operations in the specified operation set. |
| 830 | void mlir::registerStandardOperations(OperationSet &opSet) { |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 831 | opSet.addOperations<AddFOp, AffineApplyOp, AllocOp, CallOp, CallIndirectOp, |
Chris Lattner | 8c7feba | 2018-08-23 09:58:23 -0700 | [diff] [blame] | 832 | ConstantOp, DeallocOp, DimOp, ExtractElementOp, LoadOp, |
Chris Lattner | 0473e4b | 2018-09-06 17:31:21 -0700 | [diff] [blame] | 833 | ReturnOp, ShapeCastOp, StoreOp>( |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 834 | /*prefix=*/""); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 835 | } |