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