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 | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 182 | void AllocOp::print(OpAsmPrinter *p) const { |
| 183 | MemRefType *type = cast<MemRefType>(getMemRef()->getType()); |
| 184 | *p << "alloc"; |
| 185 | // Print dynamic dimension operands. |
| 186 | printDimAndSymbolList(operand_begin(), operand_end(), |
| 187 | type->getNumDynamicDims(), p); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 188 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 189 | *p << " : " << *type; |
| 190 | } |
| 191 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 192 | bool AllocOp::parse(OpAsmParser *parser, OperationState *result) { |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 193 | MemRefType *type; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 194 | |
Chris Lattner | 7d3b77c | 2018-07-31 16:21:36 -0700 | [diff] [blame] | 195 | // Parse the dimension operands and optional symbol operands, followed by a |
| 196 | // memref type. |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 197 | unsigned numDimOperands; |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 198 | if (parseDimAndSymbolList(parser, result->operands, numDimOperands) || |
| 199 | parser->parseOptionalAttributeDict(result->attributes) || |
| 200 | parser->parseColonType(type)) |
| 201 | return true; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 202 | |
| 203 | // Check numDynamicDims against number of question marks in memref type. |
| 204 | if (numDimOperands != type->getNumDynamicDims()) { |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 205 | return parser->emitError(parser->getNameLoc(), |
| 206 | "dimension operand count does not equal memref " |
| 207 | "dynamic dimension count"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Check that the number of symbol operands matches the number of symbols in |
| 211 | // the first affinemap of the memref's affine map composition. |
| 212 | // 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] | 213 | if (result->operands.size() - numDimOperands != |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 214 | type->getAffineMaps()[0]->getNumSymbols()) { |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 215 | return parser->emitError( |
| 216 | parser->getNameLoc(), |
| 217 | "affine map symbol operand count does not equal memref affine map " |
| 218 | "symbol count"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 221 | result->types.push_back(type); |
| 222 | return false; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | const char *AllocOp::verify() const { |
| 226 | // TODO(andydavis): Verify alloc. |
| 227 | return nullptr; |
| 228 | } |
| 229 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 230 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 231 | // CallOp |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 234 | void CallOp::build(Builder *builder, OperationState *result, Function *callee, |
| 235 | ArrayRef<SSAValue *> operands) { |
| 236 | result->addOperands(operands); |
| 237 | result->addAttribute("callee", builder->getFunctionAttr(callee)); |
| 238 | result->addTypes(callee->getType()->getResults()); |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | bool CallOp::parse(OpAsmParser *parser, OperationState *result) { |
| 242 | StringRef calleeName; |
| 243 | llvm::SMLoc calleeLoc; |
| 244 | FunctionType *calleeType = nullptr; |
| 245 | SmallVector<OpAsmParser::OperandType, 4> operands; |
| 246 | Function *callee = nullptr; |
| 247 | if (parser->parseFunctionName(calleeName, calleeLoc) || |
| 248 | parser->parseOperandList(operands, /*requiredOperandCount=*/-1, |
| 249 | OpAsmParser::Delimiter::Paren) || |
| 250 | parser->parseOptionalAttributeDict(result->attributes) || |
| 251 | parser->parseColonType(calleeType) || |
| 252 | parser->resolveFunctionName(calleeName, calleeType, calleeLoc, callee) || |
| 253 | parser->addTypesToList(calleeType->getResults(), result->types) || |
| 254 | parser->resolveOperands(operands, calleeType->getInputs(), calleeLoc, |
| 255 | result->operands)) |
| 256 | return true; |
| 257 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 258 | result->addAttribute("callee", parser->getBuilder().getFunctionAttr(callee)); |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | |
| 262 | void CallOp::print(OpAsmPrinter *p) const { |
| 263 | *p << "call "; |
| 264 | p->printFunctionReference(getCallee()); |
| 265 | *p << '('; |
| 266 | p->printOperands(getOperands()); |
| 267 | *p << ')'; |
| 268 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"callee"); |
| 269 | *p << " : " << *getCallee()->getType(); |
| 270 | } |
| 271 | |
| 272 | const char *CallOp::verify() const { |
| 273 | // Check that the callee attribute was specified. |
| 274 | auto *fnAttr = getAttrOfType<FunctionAttr>("callee"); |
| 275 | if (!fnAttr) |
| 276 | return "requires a 'callee' function attribute"; |
| 277 | |
| 278 | // Verify that the operand and result types match the callee. |
| 279 | auto *fnType = fnAttr->getValue()->getType(); |
| 280 | if (fnType->getNumInputs() != getNumOperands()) |
| 281 | return "incorrect number of operands for callee"; |
| 282 | |
| 283 | for (unsigned i = 0, e = fnType->getNumInputs(); i != e; ++i) { |
| 284 | if (getOperand(i)->getType() != fnType->getInput(i)) |
| 285 | return "operand type mismatch"; |
| 286 | } |
| 287 | |
| 288 | if (fnType->getNumResults() != getNumResults()) |
| 289 | return "incorrect number of results for callee"; |
| 290 | |
| 291 | for (unsigned i = 0, e = fnType->getNumResults(); i != e; ++i) { |
| 292 | if (getResult(i)->getType() != fnType->getResult(i)) |
| 293 | return "result type mismatch"; |
| 294 | } |
| 295 | |
| 296 | return nullptr; |
| 297 | } |
| 298 | |
| 299 | //===----------------------------------------------------------------------===// |
| 300 | // CallIndirectOp |
| 301 | //===----------------------------------------------------------------------===// |
| 302 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 303 | void CallIndirectOp::build(Builder *builder, OperationState *result, |
| 304 | SSAValue *callee, ArrayRef<SSAValue *> operands) { |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 305 | auto *fnType = cast<FunctionType>(callee->getType()); |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 306 | result->operands.push_back(callee); |
| 307 | result->addOperands(operands); |
| 308 | result->addTypes(fnType->getResults()); |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | bool CallIndirectOp::parse(OpAsmParser *parser, OperationState *result) { |
| 312 | FunctionType *calleeType = nullptr; |
| 313 | OpAsmParser::OperandType callee; |
| 314 | llvm::SMLoc operandsLoc; |
| 315 | SmallVector<OpAsmParser::OperandType, 4> operands; |
| 316 | return parser->parseOperand(callee) || |
| 317 | parser->getCurrentLocation(&operandsLoc) || |
| 318 | parser->parseOperandList(operands, /*requiredOperandCount=*/-1, |
| 319 | OpAsmParser::Delimiter::Paren) || |
| 320 | parser->parseOptionalAttributeDict(result->attributes) || |
| 321 | parser->parseColonType(calleeType) || |
| 322 | parser->resolveOperand(callee, calleeType, result->operands) || |
| 323 | parser->resolveOperands(operands, calleeType->getInputs(), operandsLoc, |
| 324 | result->operands) || |
| 325 | parser->addTypesToList(calleeType->getResults(), result->types); |
| 326 | } |
| 327 | |
| 328 | void CallIndirectOp::print(OpAsmPrinter *p) const { |
| 329 | *p << "call_indirect "; |
| 330 | p->printOperand(getCallee()); |
| 331 | *p << '('; |
| 332 | auto operandRange = getOperands(); |
| 333 | p->printOperands(++operandRange.begin(), operandRange.end()); |
| 334 | *p << ')'; |
| 335 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"callee"); |
| 336 | *p << " : " << *getCallee()->getType(); |
| 337 | } |
| 338 | |
| 339 | const char *CallIndirectOp::verify() const { |
| 340 | // The callee must be a function. |
| 341 | auto *fnType = dyn_cast<FunctionType>(getCallee()->getType()); |
| 342 | if (!fnType) |
| 343 | return "callee must have function type"; |
| 344 | |
| 345 | // Verify that the operand and result types match the callee. |
| 346 | if (fnType->getNumInputs() != getNumOperands() - 1) |
| 347 | return "incorrect number of operands for callee"; |
| 348 | |
| 349 | for (unsigned i = 0, e = fnType->getNumInputs(); i != e; ++i) { |
| 350 | if (getOperand(i + 1)->getType() != fnType->getInput(i)) |
| 351 | return "operand type mismatch"; |
| 352 | } |
| 353 | |
| 354 | if (fnType->getNumResults() != getNumResults()) |
| 355 | return "incorrect number of results for callee"; |
| 356 | |
| 357 | for (unsigned i = 0, e = fnType->getNumResults(); i != e; ++i) { |
| 358 | if (getResult(i)->getType() != fnType->getResult(i)) |
| 359 | return "result type mismatch"; |
| 360 | } |
| 361 | |
| 362 | return nullptr; |
| 363 | } |
| 364 | |
| 365 | //===----------------------------------------------------------------------===// |
| 366 | // Constant*Op |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 367 | //===----------------------------------------------------------------------===// |
| 368 | |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 369 | void ConstantOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 370 | *p << "constant " << *getValue(); |
| 371 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"value"); |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 372 | |
| 373 | if (!isa<FunctionAttr>(getValue())) |
| 374 | *p << " : " << *getType(); |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 377 | bool ConstantOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 378 | Attribute *valueAttr; |
| 379 | Type *type; |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 380 | |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 381 | if (parser->parseAttribute(valueAttr, "value", result->attributes) || |
| 382 | parser->parseOptionalAttributeDict(result->attributes)) |
| 383 | return true; |
| 384 | |
| 385 | // 'constant' taking a function reference doesn't get a redundant type |
| 386 | // specifier. The attribute itself carries it. |
| 387 | if (auto *fnAttr = dyn_cast<FunctionAttr>(valueAttr)) |
| 388 | return parser->addTypeToList(fnAttr->getValue()->getType(), result->types); |
| 389 | |
| 390 | return parser->parseColonType(type) || |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 391 | parser->addTypeToList(type, result->types); |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 394 | /// The constant op requires an attribute, and furthermore requires that it |
| 395 | /// matches the return type. |
| 396 | const char *ConstantOp::verify() const { |
| 397 | auto *value = getValue(); |
| 398 | if (!value) |
| 399 | return "requires a 'value' attribute"; |
| 400 | |
| 401 | auto *type = this->getType(); |
Chris Lattner | 1ec7057 | 2018-07-24 10:41:30 -0700 | [diff] [blame] | 402 | if (isa<IntegerType>(type) || type->isAffineInt()) { |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 403 | if (!isa<IntegerAttr>(value)) |
| 404 | return "requires 'value' to be an integer for an integer result type"; |
| 405 | return nullptr; |
| 406 | } |
| 407 | |
Chris Lattner | 7ba98c6 | 2018-08-16 16:56:40 -0700 | [diff] [blame] | 408 | if (isa<FloatType>(type)) { |
| 409 | if (!isa<FloatAttr>(value)) |
| 410 | return "requires 'value' to be a floating point constant"; |
| 411 | return nullptr; |
| 412 | } |
| 413 | |
| 414 | if (type->isTFString()) { |
| 415 | if (!isa<StringAttr>(value)) |
| 416 | return "requires 'value' to be a string constant"; |
| 417 | return nullptr; |
| 418 | } |
| 419 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 420 | if (isa<FunctionType>(type)) { |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 421 | if (!isa<FunctionAttr>(value)) |
| 422 | return "requires 'value' to be a function reference"; |
| 423 | return nullptr; |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | return "requires a result type that aligns with the 'value' attribute"; |
| 427 | } |
| 428 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 429 | void ConstantFloatOp::build(Builder *builder, OperationState *result, |
| 430 | double value, FloatType *type) { |
| 431 | result->addAttribute("value", builder->getFloatAttr(value)); |
| 432 | result->types.push_back(type); |
Chris Lattner | 7ba98c6 | 2018-08-16 16:56:40 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | bool ConstantFloatOp::isClassFor(const Operation *op) { |
| 436 | return ConstantOp::isClassFor(op) && |
| 437 | isa<FloatType>(op->getResult(0)->getType()); |
| 438 | } |
| 439 | |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 440 | /// ConstantIntOp only matches values whose result type is an IntegerType. |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 441 | bool ConstantIntOp::isClassFor(const Operation *op) { |
| 442 | return ConstantOp::isClassFor(op) && |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 443 | isa<IntegerType>(op->getResult(0)->getType()); |
| 444 | } |
| 445 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 446 | void ConstantIntOp::build(Builder *builder, OperationState *result, |
| 447 | int64_t value, unsigned width) { |
| 448 | result->addAttribute("value", builder->getIntegerAttr(value)); |
| 449 | result->types.push_back(builder->getIntegerType(width)); |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /// ConstantAffineIntOp only matches values whose result type is AffineInt. |
| 453 | bool ConstantAffineIntOp::isClassFor(const Operation *op) { |
| 454 | return ConstantOp::isClassFor(op) && |
| 455 | op->getResult(0)->getType()->isAffineInt(); |
| 456 | } |
| 457 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 458 | void ConstantAffineIntOp::build(Builder *builder, OperationState *result, |
| 459 | int64_t value) { |
| 460 | result->addAttribute("value", builder->getIntegerAttr(value)); |
| 461 | result->types.push_back(builder->getAffineIntType()); |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 464 | //===----------------------------------------------------------------------===// |
Uday Bondhugula | 6770171 | 2018-08-21 16:01:23 -0700 | [diff] [blame] | 465 | // AffineApplyOp |
| 466 | //===----------------------------------------------------------------------===// |
| 467 | |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 468 | void AffineApplyOp::build(Builder *builder, OperationState *result, |
| 469 | AffineMap *map, ArrayRef<SSAValue *> operands) { |
| 470 | result->addOperands(operands); |
| 471 | result->types.append(map->getNumResults(), builder->getAffineIntType()); |
| 472 | result->addAttribute("map", builder->getAffineMapAttr(map)); |
Uday Bondhugula | 6770171 | 2018-08-21 16:01:23 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | //===----------------------------------------------------------------------===// |
MLIR Team | 1989cc1 | 2018-08-15 15:39:26 -0700 | [diff] [blame] | 476 | // DeallocOp |
| 477 | //===----------------------------------------------------------------------===// |
| 478 | |
| 479 | void DeallocOp::print(OpAsmPrinter *p) const { |
| 480 | *p << "dealloc " << *getMemRef() << " : " << *getMemRef()->getType(); |
| 481 | } |
| 482 | |
| 483 | bool DeallocOp::parse(OpAsmParser *parser, OperationState *result) { |
| 484 | OpAsmParser::OperandType memrefInfo; |
| 485 | MemRefType *type; |
| 486 | |
| 487 | return parser->parseOperand(memrefInfo) || parser->parseColonType(type) || |
| 488 | parser->resolveOperand(memrefInfo, type, result->operands); |
| 489 | } |
| 490 | |
| 491 | const char *DeallocOp::verify() const { |
| 492 | if (!isa<MemRefType>(getMemRef()->getType())) |
| 493 | return "operand must be a memref"; |
| 494 | return nullptr; |
| 495 | } |
| 496 | |
| 497 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 498 | // DimOp |
| 499 | //===----------------------------------------------------------------------===// |
| 500 | |
Chris Lattner | dd0c2ca | 2018-07-24 16:07:22 -0700 | [diff] [blame] | 501 | void DimOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 502 | *p << "dim " << *getOperand() << ", " << getIndex(); |
| 503 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"index"); |
| 504 | *p << " : " << *getOperand()->getType(); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 507 | bool DimOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 508 | OpAsmParser::OperandType operandInfo; |
| 509 | IntegerAttr *indexAttr; |
| 510 | Type *type; |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 511 | |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 512 | return parser->parseOperand(operandInfo) || parser->parseComma() || |
| 513 | parser->parseAttribute(indexAttr, "index", result->attributes) || |
| 514 | parser->parseOptionalAttributeDict(result->attributes) || |
| 515 | parser->parseColonType(type) || |
| 516 | parser->resolveOperand(operandInfo, type, result->operands) || |
| 517 | parser->addTypeToList(parser->getBuilder().getAffineIntType(), |
| 518 | result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 521 | const char *DimOp::verify() const { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 522 | // Check that we have an integer index operand. |
| 523 | auto indexAttr = getAttrOfType<IntegerAttr>("index"); |
| 524 | if (!indexAttr) |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 525 | return "requires an integer attribute named 'index'"; |
| 526 | uint64_t index = (uint64_t)indexAttr->getValue(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 527 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 528 | auto *type = getOperand()->getType(); |
| 529 | if (auto *tensorType = dyn_cast<RankedTensorType>(type)) { |
| 530 | if (index >= tensorType->getRank()) |
| 531 | return "index is out of range"; |
| 532 | } else if (auto *memrefType = dyn_cast<MemRefType>(type)) { |
| 533 | if (index >= memrefType->getRank()) |
| 534 | return "index is out of range"; |
| 535 | |
| 536 | } else if (isa<UnrankedTensorType>(type)) { |
| 537 | // ok, assumed to be in-range. |
| 538 | } else { |
| 539 | return "requires an operand with tensor or memref type"; |
| 540 | } |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 541 | |
| 542 | return nullptr; |
| 543 | } |
| 544 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 545 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8c7feba | 2018-08-23 09:58:23 -0700 | [diff] [blame] | 546 | // ExtractElementOp |
| 547 | //===----------------------------------------------------------------------===// |
| 548 | |
| 549 | void ExtractElementOp::build(Builder *builder, OperationState *result, |
| 550 | SSAValue *aggregate, |
| 551 | ArrayRef<SSAValue *> indices) { |
| 552 | auto *aggregateType = cast<VectorOrTensorType>(aggregate->getType()); |
| 553 | result->addOperands(aggregate); |
| 554 | result->addOperands(indices); |
| 555 | result->types.push_back(aggregateType->getElementType()); |
| 556 | } |
| 557 | |
| 558 | void ExtractElementOp::print(OpAsmPrinter *p) const { |
| 559 | *p << "extract_element " << *getAggregate() << '['; |
| 560 | p->printOperands(getIndices()); |
| 561 | *p << ']'; |
| 562 | p->printOptionalAttrDict(getAttrs()); |
| 563 | *p << " : " << *getAggregate()->getType(); |
| 564 | } |
| 565 | |
| 566 | bool ExtractElementOp::parse(OpAsmParser *parser, OperationState *result) { |
| 567 | OpAsmParser::OperandType aggregateInfo; |
| 568 | SmallVector<OpAsmParser::OperandType, 4> indexInfo; |
| 569 | VectorOrTensorType *type; |
| 570 | |
| 571 | auto affineIntTy = parser->getBuilder().getAffineIntType(); |
| 572 | return parser->parseOperand(aggregateInfo) || |
| 573 | parser->parseOperandList(indexInfo, -1, |
| 574 | OpAsmParser::Delimiter::Square) || |
| 575 | parser->parseOptionalAttributeDict(result->attributes) || |
| 576 | parser->parseColonType(type) || |
| 577 | parser->resolveOperand(aggregateInfo, type, result->operands) || |
| 578 | parser->resolveOperands(indexInfo, affineIntTy, result->operands) || |
| 579 | parser->addTypeToList(type->getElementType(), result->types); |
| 580 | } |
| 581 | |
| 582 | const char *ExtractElementOp::verify() const { |
| 583 | if (getNumOperands() == 0) |
| 584 | return "expected an aggregate to index into"; |
| 585 | |
| 586 | auto *aggregateType = dyn_cast<VectorOrTensorType>(getAggregate()->getType()); |
| 587 | if (!aggregateType) |
| 588 | return "first operand must be a vector or tensor"; |
| 589 | |
| 590 | if (getResult()->getType() != aggregateType->getElementType()) |
| 591 | return "result type must match element type of aggregate"; |
| 592 | |
| 593 | for (auto *idx : getIndices()) |
| 594 | if (!idx->getType()->isAffineInt()) |
| 595 | return "index to extract_element must have 'affineint' type"; |
| 596 | |
| 597 | // Verify the # indices match if we have a ranked type. |
| 598 | auto aggregateRank = aggregateType->getRankIfPresent(); |
| 599 | if (aggregateRank != -1 && aggregateRank != getNumOperands() - 1) |
| 600 | return "incorrect number of indices for extract_element"; |
| 601 | |
| 602 | return nullptr; |
| 603 | } |
| 604 | |
| 605 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 606 | // LoadOp |
| 607 | //===----------------------------------------------------------------------===// |
| 608 | |
Chris Lattner | 8c7feba | 2018-08-23 09:58:23 -0700 | [diff] [blame] | 609 | void LoadOp::build(Builder *builder, OperationState *result, SSAValue *memref, |
| 610 | ArrayRef<SSAValue *> indices) { |
| 611 | auto *memrefType = cast<MemRefType>(memref->getType()); |
| 612 | result->addOperands(memref); |
| 613 | result->addOperands(indices); |
| 614 | result->types.push_back(memrefType->getElementType()); |
| 615 | } |
| 616 | |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 617 | void LoadOp::print(OpAsmPrinter *p) const { |
| 618 | *p << "load " << *getMemRef() << '['; |
| 619 | p->printOperands(getIndices()); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 620 | *p << ']'; |
| 621 | p->printOptionalAttrDict(getAttrs()); |
| 622 | *p << " : " << *getMemRef()->getType(); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 625 | bool LoadOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 626 | OpAsmParser::OperandType memrefInfo; |
| 627 | SmallVector<OpAsmParser::OperandType, 4> indexInfo; |
| 628 | MemRefType *type; |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 629 | |
| 630 | auto affineIntTy = parser->getBuilder().getAffineIntType(); |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 631 | return parser->parseOperand(memrefInfo) || |
| 632 | parser->parseOperandList(indexInfo, -1, |
| 633 | OpAsmParser::Delimiter::Square) || |
| 634 | parser->parseOptionalAttributeDict(result->attributes) || |
| 635 | parser->parseColonType(type) || |
| 636 | parser->resolveOperand(memrefInfo, type, result->operands) || |
| 637 | parser->resolveOperands(indexInfo, affineIntTy, result->operands) || |
| 638 | parser->addTypeToList(type->getElementType(), result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | const char *LoadOp::verify() const { |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 642 | if (getNumOperands() == 0) |
| 643 | return "expected a memref to load from"; |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 644 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 645 | auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType()); |
| 646 | if (!memRefType) |
| 647 | return "first operand must be a memref"; |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 648 | |
Chris Lattner | 8c7feba | 2018-08-23 09:58:23 -0700 | [diff] [blame] | 649 | if (getResult()->getType() != memRefType->getElementType()) |
| 650 | return "result type must match element type of memref"; |
| 651 | |
| 652 | if (memRefType->getRank() != getNumOperands() - 1) |
| 653 | return "incorrect number of indices for load"; |
| 654 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 655 | for (auto *idx : getIndices()) |
| 656 | if (!idx->getType()->isAffineInt()) |
| 657 | return "index to load must have 'affineint' type"; |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 658 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 659 | // TODO: Verify we have the right number of indices. |
MLIR Team | 39a3a60 | 2018-07-24 17:43:56 -0700 | [diff] [blame] | 660 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 661 | // TODO: in MLFunction verify that the indices are parameters, IV's, or the |
| 662 | // result of an affine_apply. |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 663 | return nullptr; |
| 664 | } |
| 665 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 666 | //===----------------------------------------------------------------------===// |
| 667 | // ReturnOp |
| 668 | //===----------------------------------------------------------------------===// |
| 669 | |
| 670 | bool ReturnOp::parse(OpAsmParser *parser, OperationState *result) { |
| 671 | SmallVector<OpAsmParser::OperandType, 2> opInfo; |
| 672 | SmallVector<Type *, 2> types; |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 673 | llvm::SMLoc loc; |
| 674 | return parser->getCurrentLocation(&loc) || parser->parseOperandList(opInfo) || |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 675 | (!opInfo.empty() && parser->parseColonTypeList(types)) || |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 676 | parser->resolveOperands(opInfo, types, loc, result->operands); |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void ReturnOp::print(OpAsmPrinter *p) const { |
| 680 | *p << "return"; |
| 681 | if (getNumOperands() > 0) { |
| 682 | *p << " "; |
| 683 | p->printOperands(operand_begin(), operand_end()); |
| 684 | *p << " : "; |
| 685 | interleave(operand_begin(), operand_end(), |
| 686 | [&](auto *e) { p->printType(e->getType()); }, |
| 687 | [&]() { *p << ", "; }); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | const char *ReturnOp::verify() const { |
| 692 | // ReturnOp must be part of an ML function. |
| 693 | if (auto *stmt = dyn_cast<OperationStmt>(getOperation())) { |
Tatiana Shpeisman | 3abd6bd | 2018-08-16 20:19:44 -0700 | [diff] [blame] | 694 | StmtBlock *block = stmt->getBlock(); |
| 695 | if (!block || !isa<MLFunction>(block) || &block->back() != stmt) |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 696 | return "must be the last statement in the ML function"; |
| 697 | |
| 698 | // Return success. Checking that operand types match those in the function |
| 699 | // signature is performed in the ML function verifier. |
| 700 | return nullptr; |
| 701 | } |
Tatiana Shpeisman | b697ac1 | 2018-08-09 23:21:19 -0700 | [diff] [blame] | 702 | return "cannot occur in a CFG function"; |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | //===----------------------------------------------------------------------===// |
| 706 | // StoreOp |
| 707 | //===----------------------------------------------------------------------===// |
| 708 | |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 709 | void StoreOp::print(OpAsmPrinter *p) const { |
| 710 | *p << "store " << *getValueToStore(); |
| 711 | *p << ", " << *getMemRef() << '['; |
| 712 | p->printOperands(getIndices()); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 713 | *p << ']'; |
| 714 | p->printOptionalAttrDict(getAttrs()); |
| 715 | *p << " : " << *getMemRef()->getType(); |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 718 | bool StoreOp::parse(OpAsmParser *parser, OperationState *result) { |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 719 | OpAsmParser::OperandType storeValueInfo; |
| 720 | OpAsmParser::OperandType memrefInfo; |
| 721 | SmallVector<OpAsmParser::OperandType, 4> indexInfo; |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 722 | MemRefType *memrefType; |
| 723 | |
| 724 | auto affineIntTy = parser->getBuilder().getAffineIntType(); |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 725 | return parser->parseOperand(storeValueInfo) || parser->parseComma() || |
| 726 | parser->parseOperand(memrefInfo) || |
| 727 | parser->parseOperandList(indexInfo, -1, |
| 728 | OpAsmParser::Delimiter::Square) || |
| 729 | parser->parseOptionalAttributeDict(result->attributes) || |
| 730 | parser->parseColonType(memrefType) || |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 731 | parser->resolveOperand(storeValueInfo, memrefType->getElementType(), |
| 732 | result->operands) || |
| 733 | parser->resolveOperand(memrefInfo, memrefType, result->operands) || |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 734 | parser->resolveOperands(indexInfo, affineIntTy, result->operands); |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | const char *StoreOp::verify() const { |
| 738 | if (getNumOperands() < 2) |
| 739 | return "expected a value to store and a memref"; |
| 740 | |
| 741 | // Second operand is a memref type. |
| 742 | auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType()); |
| 743 | if (!memRefType) |
| 744 | return "second operand must be a memref"; |
| 745 | |
| 746 | // First operand must have same type as memref element type. |
| 747 | if (getValueToStore()->getType() != memRefType->getElementType()) |
| 748 | return "first operand must have same type memref element type "; |
| 749 | |
| 750 | if (getNumOperands() != 2 + memRefType->getRank()) |
| 751 | return "store index operand count not equal to memref rank"; |
| 752 | |
| 753 | for (auto *idx : getIndices()) |
| 754 | if (!idx->getType()->isAffineInt()) |
| 755 | return "index to load must have 'affineint' type"; |
| 756 | |
| 757 | // TODO: Verify we have the right number of indices. |
| 758 | |
| 759 | // TODO: in MLFunction verify that the indices are parameters, IV's, or the |
| 760 | // result of an affine_apply. |
| 761 | return nullptr; |
| 762 | } |
| 763 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 764 | //===----------------------------------------------------------------------===// |
| 765 | // Register operations. |
| 766 | //===----------------------------------------------------------------------===// |
| 767 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 768 | /// Install the standard operations in the specified operation set. |
| 769 | void mlir::registerStandardOperations(OperationSet &opSet) { |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 770 | opSet.addOperations<AddFOp, AffineApplyOp, AllocOp, CallOp, CallIndirectOp, |
Chris Lattner | 8c7feba | 2018-08-23 09:58:23 -0700 | [diff] [blame] | 771 | ConstantOp, DeallocOp, DimOp, ExtractElementOp, LoadOp, |
| 772 | ReturnOp, StoreOp>( |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 773 | /*prefix=*/""); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 774 | } |