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 | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 69 | bool AddFOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 70 | SmallVector<OpAsmParser::OperandType, 2> ops; |
| 71 | Type *type; |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 72 | return parser->parseOperandList(ops, 2) || |
| 73 | parser->parseOptionalAttributeDict(result->attributes) || |
| 74 | parser->parseColonType(type) || |
| 75 | parser->resolveOperands(ops, type, result->operands) || |
| 76 | parser->addTypeToList(type, result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | dd0c2ca | 2018-07-24 16:07:22 -0700 | [diff] [blame] | 79 | void AddFOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 80 | *p << "addf " << *getOperand(0) << ", " << *getOperand(1); |
| 81 | p->printOptionalAttrDict(getAttrs()); |
| 82 | *p << " : " << *getType(); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 85 | // TODO: Have verify functions return std::string to enable more descriptive |
| 86 | // error messages. |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 87 | // Return an error message on failure. |
| 88 | const char *AddFOp::verify() const { |
| 89 | // TODO: Check that the types of the LHS and RHS match. |
| 90 | // TODO: This should be a refinement of TwoOperands. |
| 91 | // TODO: There should also be a OneResultWhoseTypeMatchesFirstOperand. |
| 92 | return nullptr; |
| 93 | } |
| 94 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 95 | //===----------------------------------------------------------------------===// |
| 96 | // AffineApplyOp |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 99 | bool AffineApplyOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 100 | auto &builder = parser->getBuilder(); |
| 101 | auto *affineIntTy = builder.getAffineIntType(); |
| 102 | |
| 103 | AffineMapAttr *mapAttr; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 104 | unsigned numDims; |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 105 | if (parser->parseAttribute(mapAttr, "map", result->attributes) || |
| 106 | parseDimAndSymbolList(parser, result->operands, numDims) || |
| 107 | parser->parseOptionalAttributeDict(result->attributes)) |
| 108 | return true; |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 109 | auto *map = mapAttr->getValue(); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 110 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 111 | if (map->getNumDims() != numDims || |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 112 | numDims + map->getNumSymbols() != result->operands.size()) { |
| 113 | return parser->emitError(parser->getNameLoc(), |
| 114 | "dimension or symbol index mismatch"); |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 117 | result->types.append(map->getNumResults(), affineIntTy); |
| 118 | return false; |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void AffineApplyOp::print(OpAsmPrinter *p) const { |
| 122 | auto *map = getAffineMap(); |
| 123 | *p << "affine_apply " << *map; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 124 | printDimAndSymbolList(operand_begin(), operand_end(), map->getNumDims(), p); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 125 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map"); |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | const char *AffineApplyOp::verify() const { |
| 129 | // Check that affine map attribute was specified. |
| 130 | auto *affineMapAttr = getAttrOfType<AffineMapAttr>("map"); |
| 131 | if (!affineMapAttr) |
| 132 | return "requires an affine map."; |
| 133 | |
| 134 | // Check input and output dimensions match. |
| 135 | auto *map = affineMapAttr->getValue(); |
| 136 | |
| 137 | // Verify that operand count matches affine map dimension and symbol count. |
| 138 | if (getNumOperands() != map->getNumDims() + map->getNumSymbols()) |
| 139 | return "operand count and affine map dimension and symbol count must match"; |
| 140 | |
| 141 | // Verify that result count matches affine map result count. |
| 142 | if (getNumResults() != map->getNumResults()) |
| 143 | return "result count and affine map result count must match"; |
| 144 | |
| 145 | return nullptr; |
| 146 | } |
| 147 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 148 | //===----------------------------------------------------------------------===// |
| 149 | // AllocOp |
| 150 | //===----------------------------------------------------------------------===// |
| 151 | |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 152 | void AllocOp::print(OpAsmPrinter *p) const { |
| 153 | MemRefType *type = cast<MemRefType>(getMemRef()->getType()); |
| 154 | *p << "alloc"; |
| 155 | // Print dynamic dimension operands. |
| 156 | printDimAndSymbolList(operand_begin(), operand_end(), |
| 157 | type->getNumDynamicDims(), p); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 158 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 159 | *p << " : " << *type; |
| 160 | } |
| 161 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 162 | bool AllocOp::parse(OpAsmParser *parser, OperationState *result) { |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 163 | MemRefType *type; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 164 | |
Chris Lattner | 7d3b77c | 2018-07-31 16:21:36 -0700 | [diff] [blame] | 165 | // Parse the dimension operands and optional symbol operands, followed by a |
| 166 | // memref type. |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 167 | unsigned numDimOperands; |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 168 | if (parseDimAndSymbolList(parser, result->operands, numDimOperands) || |
| 169 | parser->parseOptionalAttributeDict(result->attributes) || |
| 170 | parser->parseColonType(type)) |
| 171 | return true; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 172 | |
| 173 | // Check numDynamicDims against number of question marks in memref type. |
| 174 | if (numDimOperands != type->getNumDynamicDims()) { |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 175 | return parser->emitError(parser->getNameLoc(), |
| 176 | "dimension operand count does not equal memref " |
| 177 | "dynamic dimension count"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Check that the number of symbol operands matches the number of symbols in |
| 181 | // the first affinemap of the memref's affine map composition. |
| 182 | // 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] | 183 | if (result->operands.size() - numDimOperands != |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 184 | type->getAffineMaps()[0]->getNumSymbols()) { |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 185 | return parser->emitError( |
| 186 | parser->getNameLoc(), |
| 187 | "affine map symbol operand count does not equal memref affine map " |
| 188 | "symbol count"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 191 | result->types.push_back(type); |
| 192 | return false; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | const char *AllocOp::verify() const { |
| 196 | // TODO(andydavis): Verify alloc. |
| 197 | return nullptr; |
| 198 | } |
| 199 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 200 | //===----------------------------------------------------------------------===// |
| 201 | // ConstantOp |
| 202 | //===----------------------------------------------------------------------===// |
| 203 | |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 204 | void ConstantOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 205 | *p << "constant " << *getValue(); |
| 206 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"value"); |
| 207 | *p << " : " << *getType(); |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 210 | bool ConstantOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 211 | Attribute *valueAttr; |
| 212 | Type *type; |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 213 | |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 214 | return parser->parseAttribute(valueAttr, "value", result->attributes) || |
| 215 | parser->parseOptionalAttributeDict(result->attributes) || |
| 216 | parser->parseColonType(type) || |
| 217 | parser->addTypeToList(type, result->types); |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 220 | /// The constant op requires an attribute, and furthermore requires that it |
| 221 | /// matches the return type. |
| 222 | const char *ConstantOp::verify() const { |
| 223 | auto *value = getValue(); |
| 224 | if (!value) |
| 225 | return "requires a 'value' attribute"; |
| 226 | |
| 227 | auto *type = this->getType(); |
Chris Lattner | 1ec7057 | 2018-07-24 10:41:30 -0700 | [diff] [blame] | 228 | if (isa<IntegerType>(type) || type->isAffineInt()) { |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 229 | if (!isa<IntegerAttr>(value)) |
| 230 | return "requires 'value' to be an integer for an integer result type"; |
| 231 | return nullptr; |
| 232 | } |
| 233 | |
Chris Lattner | 7ba98c6 | 2018-08-16 16:56:40 -0700 | [diff] [blame^] | 234 | if (isa<FloatType>(type)) { |
| 235 | if (!isa<FloatAttr>(value)) |
| 236 | return "requires 'value' to be a floating point constant"; |
| 237 | return nullptr; |
| 238 | } |
| 239 | |
| 240 | if (type->isTFString()) { |
| 241 | if (!isa<StringAttr>(value)) |
| 242 | return "requires 'value' to be a string constant"; |
| 243 | return nullptr; |
| 244 | } |
| 245 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 246 | if (isa<FunctionType>(type)) { |
| 247 | // TODO: Verify a function attr. |
| 248 | } |
| 249 | |
| 250 | return "requires a result type that aligns with the 'value' attribute"; |
| 251 | } |
| 252 | |
Chris Lattner | 7ba98c6 | 2018-08-16 16:56:40 -0700 | [diff] [blame^] | 253 | OperationState ConstantFloatOp::build(Builder *builder, double value, |
| 254 | FloatType *type) { |
| 255 | OperationState result(builder->getIdentifier("constant")); |
| 256 | result.attributes.push_back( |
| 257 | {builder->getIdentifier("value"), builder->getFloatAttr(value)}); |
| 258 | result.types.push_back(type); |
| 259 | return result; |
| 260 | } |
| 261 | |
| 262 | bool ConstantFloatOp::isClassFor(const Operation *op) { |
| 263 | return ConstantOp::isClassFor(op) && |
| 264 | isa<FloatType>(op->getResult(0)->getType()); |
| 265 | } |
| 266 | |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 267 | /// ConstantIntOp only matches values whose result type is an IntegerType. |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 268 | bool ConstantIntOp::isClassFor(const Operation *op) { |
| 269 | return ConstantOp::isClassFor(op) && |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 270 | isa<IntegerType>(op->getResult(0)->getType()); |
| 271 | } |
| 272 | |
| 273 | OperationState ConstantIntOp::build(Builder *builder, int64_t value, |
| 274 | unsigned width) { |
| 275 | OperationState result(builder->getIdentifier("constant")); |
| 276 | result.attributes.push_back( |
| 277 | {builder->getIdentifier("value"), builder->getIntegerAttr(value)}); |
| 278 | result.types.push_back(builder->getIntegerType(width)); |
| 279 | return result; |
| 280 | } |
| 281 | |
| 282 | /// ConstantAffineIntOp only matches values whose result type is AffineInt. |
| 283 | bool ConstantAffineIntOp::isClassFor(const Operation *op) { |
| 284 | return ConstantOp::isClassFor(op) && |
| 285 | op->getResult(0)->getType()->isAffineInt(); |
| 286 | } |
| 287 | |
| 288 | OperationState ConstantAffineIntOp::build(Builder *builder, int64_t value) { |
| 289 | OperationState result(builder->getIdentifier("constant")); |
| 290 | result.attributes.push_back( |
| 291 | {builder->getIdentifier("value"), builder->getIntegerAttr(value)}); |
| 292 | result.types.push_back(builder->getAffineIntType()); |
| 293 | return result; |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 296 | //===----------------------------------------------------------------------===// |
MLIR Team | 1989cc1 | 2018-08-15 15:39:26 -0700 | [diff] [blame] | 297 | // DeallocOp |
| 298 | //===----------------------------------------------------------------------===// |
| 299 | |
| 300 | void DeallocOp::print(OpAsmPrinter *p) const { |
| 301 | *p << "dealloc " << *getMemRef() << " : " << *getMemRef()->getType(); |
| 302 | } |
| 303 | |
| 304 | bool DeallocOp::parse(OpAsmParser *parser, OperationState *result) { |
| 305 | OpAsmParser::OperandType memrefInfo; |
| 306 | MemRefType *type; |
| 307 | |
| 308 | return parser->parseOperand(memrefInfo) || parser->parseColonType(type) || |
| 309 | parser->resolveOperand(memrefInfo, type, result->operands); |
| 310 | } |
| 311 | |
| 312 | const char *DeallocOp::verify() const { |
| 313 | if (!isa<MemRefType>(getMemRef()->getType())) |
| 314 | return "operand must be a memref"; |
| 315 | return nullptr; |
| 316 | } |
| 317 | |
| 318 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 319 | // DimOp |
| 320 | //===----------------------------------------------------------------------===// |
| 321 | |
Chris Lattner | dd0c2ca | 2018-07-24 16:07:22 -0700 | [diff] [blame] | 322 | void DimOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 323 | *p << "dim " << *getOperand() << ", " << getIndex(); |
| 324 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"index"); |
| 325 | *p << " : " << *getOperand()->getType(); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 328 | bool DimOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 329 | OpAsmParser::OperandType operandInfo; |
| 330 | IntegerAttr *indexAttr; |
| 331 | Type *type; |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 332 | |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 333 | return parser->parseOperand(operandInfo) || parser->parseComma() || |
| 334 | parser->parseAttribute(indexAttr, "index", result->attributes) || |
| 335 | parser->parseOptionalAttributeDict(result->attributes) || |
| 336 | parser->parseColonType(type) || |
| 337 | parser->resolveOperand(operandInfo, type, result->operands) || |
| 338 | parser->addTypeToList(parser->getBuilder().getAffineIntType(), |
| 339 | result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 342 | const char *DimOp::verify() const { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 343 | // Check that we have an integer index operand. |
| 344 | auto indexAttr = getAttrOfType<IntegerAttr>("index"); |
| 345 | if (!indexAttr) |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 346 | return "requires an integer attribute named 'index'"; |
| 347 | uint64_t index = (uint64_t)indexAttr->getValue(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 348 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 349 | auto *type = getOperand()->getType(); |
| 350 | if (auto *tensorType = dyn_cast<RankedTensorType>(type)) { |
| 351 | if (index >= tensorType->getRank()) |
| 352 | return "index is out of range"; |
| 353 | } else if (auto *memrefType = dyn_cast<MemRefType>(type)) { |
| 354 | if (index >= memrefType->getRank()) |
| 355 | return "index is out of range"; |
| 356 | |
| 357 | } else if (isa<UnrankedTensorType>(type)) { |
| 358 | // ok, assumed to be in-range. |
| 359 | } else { |
| 360 | return "requires an operand with tensor or memref type"; |
| 361 | } |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 362 | |
| 363 | return nullptr; |
| 364 | } |
| 365 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
| 367 | // LoadOp |
| 368 | //===----------------------------------------------------------------------===// |
| 369 | |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 370 | void LoadOp::print(OpAsmPrinter *p) const { |
| 371 | *p << "load " << *getMemRef() << '['; |
| 372 | p->printOperands(getIndices()); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 373 | *p << ']'; |
| 374 | p->printOptionalAttrDict(getAttrs()); |
| 375 | *p << " : " << *getMemRef()->getType(); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 378 | bool LoadOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 379 | OpAsmParser::OperandType memrefInfo; |
| 380 | SmallVector<OpAsmParser::OperandType, 4> indexInfo; |
| 381 | MemRefType *type; |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 382 | |
| 383 | auto affineIntTy = parser->getBuilder().getAffineIntType(); |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 384 | return parser->parseOperand(memrefInfo) || |
| 385 | parser->parseOperandList(indexInfo, -1, |
| 386 | OpAsmParser::Delimiter::Square) || |
| 387 | parser->parseOptionalAttributeDict(result->attributes) || |
| 388 | parser->parseColonType(type) || |
| 389 | parser->resolveOperand(memrefInfo, type, result->operands) || |
| 390 | parser->resolveOperands(indexInfo, affineIntTy, result->operands) || |
| 391 | parser->addTypeToList(type->getElementType(), result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | const char *LoadOp::verify() const { |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 395 | if (getNumOperands() == 0) |
| 396 | return "expected a memref to load from"; |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 397 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 398 | auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType()); |
| 399 | if (!memRefType) |
| 400 | return "first operand must be a memref"; |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 401 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 402 | for (auto *idx : getIndices()) |
| 403 | if (!idx->getType()->isAffineInt()) |
| 404 | return "index to load must have 'affineint' type"; |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 405 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 406 | // TODO: Verify we have the right number of indices. |
MLIR Team | 39a3a60 | 2018-07-24 17:43:56 -0700 | [diff] [blame] | 407 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 408 | // TODO: in MLFunction verify that the indices are parameters, IV's, or the |
| 409 | // result of an affine_apply. |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 410 | return nullptr; |
| 411 | } |
| 412 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 413 | //===----------------------------------------------------------------------===// |
| 414 | // ReturnOp |
| 415 | //===----------------------------------------------------------------------===// |
| 416 | |
| 417 | bool ReturnOp::parse(OpAsmParser *parser, OperationState *result) { |
| 418 | SmallVector<OpAsmParser::OperandType, 2> opInfo; |
| 419 | SmallVector<Type *, 2> types; |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 420 | |
| 421 | return parser->parseOperandList(opInfo, -1, OpAsmParser::Delimiter::None) || |
| 422 | (!opInfo.empty() && parser->parseColonTypeList(types)) || |
| 423 | parser->resolveOperands(opInfo, types, result->operands); |
| 424 | } |
| 425 | |
| 426 | void ReturnOp::print(OpAsmPrinter *p) const { |
| 427 | *p << "return"; |
| 428 | if (getNumOperands() > 0) { |
| 429 | *p << " "; |
| 430 | p->printOperands(operand_begin(), operand_end()); |
| 431 | *p << " : "; |
| 432 | interleave(operand_begin(), operand_end(), |
| 433 | [&](auto *e) { p->printType(e->getType()); }, |
| 434 | [&]() { *p << ", "; }); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | const char *ReturnOp::verify() const { |
| 439 | // ReturnOp must be part of an ML function. |
| 440 | if (auto *stmt = dyn_cast<OperationStmt>(getOperation())) { |
Tatiana Shpeisman | b697ac1 | 2018-08-09 23:21:19 -0700 | [diff] [blame] | 441 | MLFunction *func = dyn_cast_or_null<MLFunction>(stmt->getBlock()); |
| 442 | if (!func || &func->back() != stmt) |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 443 | return "must be the last statement in the ML function"; |
| 444 | |
| 445 | // Return success. Checking that operand types match those in the function |
| 446 | // signature is performed in the ML function verifier. |
| 447 | return nullptr; |
| 448 | } |
Tatiana Shpeisman | b697ac1 | 2018-08-09 23:21:19 -0700 | [diff] [blame] | 449 | return "cannot occur in a CFG function"; |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | //===----------------------------------------------------------------------===// |
| 453 | // StoreOp |
| 454 | //===----------------------------------------------------------------------===// |
| 455 | |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 456 | void StoreOp::print(OpAsmPrinter *p) const { |
| 457 | *p << "store " << *getValueToStore(); |
| 458 | *p << ", " << *getMemRef() << '['; |
| 459 | p->printOperands(getIndices()); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 460 | *p << ']'; |
| 461 | p->printOptionalAttrDict(getAttrs()); |
| 462 | *p << " : " << *getMemRef()->getType(); |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 465 | bool StoreOp::parse(OpAsmParser *parser, OperationState *result) { |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 466 | OpAsmParser::OperandType storeValueInfo; |
| 467 | OpAsmParser::OperandType memrefInfo; |
| 468 | SmallVector<OpAsmParser::OperandType, 4> indexInfo; |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 469 | MemRefType *memrefType; |
| 470 | |
| 471 | auto affineIntTy = parser->getBuilder().getAffineIntType(); |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 472 | return parser->parseOperand(storeValueInfo) || parser->parseComma() || |
| 473 | parser->parseOperand(memrefInfo) || |
| 474 | parser->parseOperandList(indexInfo, -1, |
| 475 | OpAsmParser::Delimiter::Square) || |
| 476 | parser->parseOptionalAttributeDict(result->attributes) || |
| 477 | parser->parseColonType(memrefType) || |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame] | 478 | parser->resolveOperand(storeValueInfo, memrefType->getElementType(), |
| 479 | result->operands) || |
| 480 | parser->resolveOperand(memrefInfo, memrefType, result->operands) || |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 481 | parser->resolveOperands(indexInfo, affineIntTy, result->operands); |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | const char *StoreOp::verify() const { |
| 485 | if (getNumOperands() < 2) |
| 486 | return "expected a value to store and a memref"; |
| 487 | |
| 488 | // Second operand is a memref type. |
| 489 | auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType()); |
| 490 | if (!memRefType) |
| 491 | return "second operand must be a memref"; |
| 492 | |
| 493 | // First operand must have same type as memref element type. |
| 494 | if (getValueToStore()->getType() != memRefType->getElementType()) |
| 495 | return "first operand must have same type memref element type "; |
| 496 | |
| 497 | if (getNumOperands() != 2 + memRefType->getRank()) |
| 498 | return "store index operand count not equal to memref rank"; |
| 499 | |
| 500 | for (auto *idx : getIndices()) |
| 501 | if (!idx->getType()->isAffineInt()) |
| 502 | return "index to load must have 'affineint' type"; |
| 503 | |
| 504 | // TODO: Verify we have the right number of indices. |
| 505 | |
| 506 | // TODO: in MLFunction verify that the indices are parameters, IV's, or the |
| 507 | // result of an affine_apply. |
| 508 | return nullptr; |
| 509 | } |
| 510 | |
Tatiana Shpeisman | d9b1d86 | 2018-08-09 12:28:58 -0700 | [diff] [blame] | 511 | //===----------------------------------------------------------------------===// |
| 512 | // Register operations. |
| 513 | //===----------------------------------------------------------------------===// |
| 514 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 515 | /// Install the standard operations in the specified operation set. |
| 516 | void mlir::registerStandardOperations(OperationSet &opSet) { |
MLIR Team | 1989cc1 | 2018-08-15 15:39:26 -0700 | [diff] [blame] | 517 | opSet.addOperations<AddFOp, AffineApplyOp, AllocOp, ConstantOp, DeallocOp, |
| 518 | DimOp, LoadOp, ReturnOp, StoreOp>( |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 519 | /*prefix=*/""); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 520 | } |