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" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | using namespace mlir; |
| 27 | |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 28 | static void printDimAndSymbolList(Operation::const_operand_iterator begin, |
| 29 | Operation::const_operand_iterator end, |
| 30 | unsigned numDims, OpAsmPrinter *p) { |
| 31 | *p << '('; |
| 32 | p->printOperands(begin, begin + numDims); |
| 33 | *p << ')'; |
| 34 | |
| 35 | if (begin + numDims != end) { |
| 36 | *p << '['; |
| 37 | p->printOperands(begin + numDims, end); |
| 38 | *p << ']'; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Parses dimension and symbol list, and sets 'numDims' to the number of |
| 43 | // dimension operands parsed. |
| 44 | // Returns 'false' on success and 'true' on error. |
| 45 | static bool |
| 46 | parseDimAndSymbolList(OpAsmParser *parser, |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 47 | SmallVector<SSAValue *, 4> &operands, unsigned &numDims) { |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 48 | SmallVector<OpAsmParser::OperandType, 8> opInfos; |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 49 | if (parser->parseOperandList(opInfos, -1, OpAsmParser::Delimiter::Paren)) |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 50 | return true; |
| 51 | // Store number of dimensions for validation by caller. |
| 52 | numDims = opInfos.size(); |
| 53 | |
| 54 | // Parse the optional symbol operands. |
| 55 | auto *affineIntTy = parser->getBuilder().getAffineIntType(); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 56 | if (parser->parseOperandList(opInfos, -1, |
| 57 | OpAsmParser::Delimiter::OptionalSquare) || |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 58 | parser->resolveOperands(opInfos, affineIntTy, operands)) |
| 59 | return true; |
| 60 | return false; |
| 61 | } |
| 62 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 63 | bool AddFOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 64 | SmallVector<OpAsmParser::OperandType, 2> ops; |
| 65 | Type *type; |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame^] | 66 | return parser->parseOperandList(ops, 2) || |
| 67 | parser->parseOptionalAttributeDict(result->attributes) || |
| 68 | parser->parseColonType(type) || |
| 69 | parser->resolveOperands(ops, type, result->operands) || |
| 70 | parser->addTypeToList(type, result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | dd0c2ca | 2018-07-24 16:07:22 -0700 | [diff] [blame] | 73 | void AddFOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 74 | *p << "addf " << *getOperand(0) << ", " << *getOperand(1); |
| 75 | p->printOptionalAttrDict(getAttrs()); |
| 76 | *p << " : " << *getType(); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 79 | // TODO: Have verify functions return std::string to enable more descriptive |
| 80 | // error messages. |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 81 | // Return an error message on failure. |
| 82 | const char *AddFOp::verify() const { |
| 83 | // TODO: Check that the types of the LHS and RHS match. |
| 84 | // TODO: This should be a refinement of TwoOperands. |
| 85 | // TODO: There should also be a OneResultWhoseTypeMatchesFirstOperand. |
| 86 | return nullptr; |
| 87 | } |
| 88 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 89 | bool AffineApplyOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 90 | auto &builder = parser->getBuilder(); |
| 91 | auto *affineIntTy = builder.getAffineIntType(); |
| 92 | |
| 93 | AffineMapAttr *mapAttr; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 94 | unsigned numDims; |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 95 | if (parser->parseAttribute(mapAttr, "map", result->attributes) || |
| 96 | parseDimAndSymbolList(parser, result->operands, numDims) || |
| 97 | parser->parseOptionalAttributeDict(result->attributes)) |
| 98 | return true; |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 99 | auto *map = mapAttr->getValue(); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 100 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 101 | if (map->getNumDims() != numDims || |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 102 | numDims + map->getNumSymbols() != result->operands.size()) { |
| 103 | return parser->emitError(parser->getNameLoc(), |
| 104 | "dimension or symbol index mismatch"); |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 107 | result->types.append(map->getNumResults(), affineIntTy); |
| 108 | return false; |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void AffineApplyOp::print(OpAsmPrinter *p) const { |
| 112 | auto *map = getAffineMap(); |
| 113 | *p << "affine_apply " << *map; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 114 | printDimAndSymbolList(operand_begin(), operand_end(), map->getNumDims(), p); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 115 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map"); |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | const char *AffineApplyOp::verify() const { |
| 119 | // Check that affine map attribute was specified. |
| 120 | auto *affineMapAttr = getAttrOfType<AffineMapAttr>("map"); |
| 121 | if (!affineMapAttr) |
| 122 | return "requires an affine map."; |
| 123 | |
| 124 | // Check input and output dimensions match. |
| 125 | auto *map = affineMapAttr->getValue(); |
| 126 | |
| 127 | // Verify that operand count matches affine map dimension and symbol count. |
| 128 | if (getNumOperands() != map->getNumDims() + map->getNumSymbols()) |
| 129 | return "operand count and affine map dimension and symbol count must match"; |
| 130 | |
| 131 | // Verify that result count matches affine map result count. |
| 132 | if (getNumResults() != map->getNumResults()) |
| 133 | return "result count and affine map result count must match"; |
| 134 | |
| 135 | return nullptr; |
| 136 | } |
| 137 | |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 138 | void AllocOp::print(OpAsmPrinter *p) const { |
| 139 | MemRefType *type = cast<MemRefType>(getMemRef()->getType()); |
| 140 | *p << "alloc"; |
| 141 | // Print dynamic dimension operands. |
| 142 | printDimAndSymbolList(operand_begin(), operand_end(), |
| 143 | type->getNumDynamicDims(), p); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 144 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 145 | *p << " : " << *type; |
| 146 | } |
| 147 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 148 | bool AllocOp::parse(OpAsmParser *parser, OperationState *result) { |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 149 | MemRefType *type; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 150 | |
Chris Lattner | 7d3b77c | 2018-07-31 16:21:36 -0700 | [diff] [blame] | 151 | // Parse the dimension operands and optional symbol operands, followed by a |
| 152 | // memref type. |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 153 | unsigned numDimOperands; |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 154 | if (parseDimAndSymbolList(parser, result->operands, numDimOperands) || |
| 155 | parser->parseOptionalAttributeDict(result->attributes) || |
| 156 | parser->parseColonType(type)) |
| 157 | return true; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 158 | |
| 159 | // Check numDynamicDims against number of question marks in memref type. |
| 160 | if (numDimOperands != type->getNumDynamicDims()) { |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 161 | return parser->emitError(parser->getNameLoc(), |
| 162 | "dimension operand count does not equal memref " |
| 163 | "dynamic dimension count"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Check that the number of symbol operands matches the number of symbols in |
| 167 | // the first affinemap of the memref's affine map composition. |
| 168 | // 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] | 169 | if (result->operands.size() - numDimOperands != |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 170 | type->getAffineMaps()[0]->getNumSymbols()) { |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 171 | return parser->emitError( |
| 172 | parser->getNameLoc(), |
| 173 | "affine map symbol operand count does not equal memref affine map " |
| 174 | "symbol count"); |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 177 | result->types.push_back(type); |
| 178 | return false; |
MLIR Team | 554a8ad | 2018-07-30 13:08:05 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | const char *AllocOp::verify() const { |
| 182 | // TODO(andydavis): Verify alloc. |
| 183 | return nullptr; |
| 184 | } |
| 185 | |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 186 | void ConstantOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 187 | *p << "constant " << *getValue(); |
| 188 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"value"); |
| 189 | *p << " : " << *getType(); |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 192 | bool ConstantOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 193 | Attribute *valueAttr; |
| 194 | Type *type; |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 195 | |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame^] | 196 | return parser->parseAttribute(valueAttr, "value", result->attributes) || |
| 197 | parser->parseOptionalAttributeDict(result->attributes) || |
| 198 | parser->parseColonType(type) || |
| 199 | parser->addTypeToList(type, result->types); |
Chris Lattner | d496421 | 2018-08-01 10:43:18 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 202 | /// The constant op requires an attribute, and furthermore requires that it |
| 203 | /// matches the return type. |
| 204 | const char *ConstantOp::verify() const { |
| 205 | auto *value = getValue(); |
| 206 | if (!value) |
| 207 | return "requires a 'value' attribute"; |
| 208 | |
| 209 | auto *type = this->getType(); |
Chris Lattner | 1ec7057 | 2018-07-24 10:41:30 -0700 | [diff] [blame] | 210 | if (isa<IntegerType>(type) || type->isAffineInt()) { |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 211 | if (!isa<IntegerAttr>(value)) |
| 212 | return "requires 'value' to be an integer for an integer result type"; |
| 213 | return nullptr; |
| 214 | } |
| 215 | |
| 216 | if (isa<FunctionType>(type)) { |
| 217 | // TODO: Verify a function attr. |
| 218 | } |
| 219 | |
| 220 | return "requires a result type that aligns with the 'value' attribute"; |
| 221 | } |
| 222 | |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 223 | /// ConstantIntOp only matches values whose result type is an IntegerType. |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 224 | bool ConstantIntOp::isClassFor(const Operation *op) { |
| 225 | return ConstantOp::isClassFor(op) && |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 226 | isa<IntegerType>(op->getResult(0)->getType()); |
| 227 | } |
| 228 | |
| 229 | OperationState ConstantIntOp::build(Builder *builder, int64_t value, |
| 230 | unsigned width) { |
| 231 | OperationState result(builder->getIdentifier("constant")); |
| 232 | result.attributes.push_back( |
| 233 | {builder->getIdentifier("value"), builder->getIntegerAttr(value)}); |
| 234 | result.types.push_back(builder->getIntegerType(width)); |
| 235 | return result; |
| 236 | } |
| 237 | |
| 238 | /// ConstantAffineIntOp only matches values whose result type is AffineInt. |
| 239 | bool ConstantAffineIntOp::isClassFor(const Operation *op) { |
| 240 | return ConstantOp::isClassFor(op) && |
| 241 | op->getResult(0)->getType()->isAffineInt(); |
| 242 | } |
| 243 | |
| 244 | OperationState ConstantAffineIntOp::build(Builder *builder, int64_t value) { |
| 245 | OperationState result(builder->getIdentifier("constant")); |
| 246 | result.attributes.push_back( |
| 247 | {builder->getIdentifier("value"), builder->getIntegerAttr(value)}); |
| 248 | result.types.push_back(builder->getAffineIntType()); |
| 249 | return result; |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Chris Lattner | dd0c2ca | 2018-07-24 16:07:22 -0700 | [diff] [blame] | 252 | void DimOp::print(OpAsmPrinter *p) const { |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 253 | *p << "dim " << *getOperand() << ", " << getIndex(); |
| 254 | p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"index"); |
| 255 | *p << " : " << *getOperand()->getType(); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 258 | bool DimOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 259 | OpAsmParser::OperandType operandInfo; |
| 260 | IntegerAttr *indexAttr; |
| 261 | Type *type; |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 262 | |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame^] | 263 | return parser->parseOperand(operandInfo) || parser->parseComma() || |
| 264 | parser->parseAttribute(indexAttr, "index", result->attributes) || |
| 265 | parser->parseOptionalAttributeDict(result->attributes) || |
| 266 | parser->parseColonType(type) || |
| 267 | parser->resolveOperand(operandInfo, type, result->operands) || |
| 268 | parser->addTypeToList(parser->getBuilder().getAffineIntType(), |
| 269 | result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 272 | const char *DimOp::verify() const { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 273 | // Check that we have an integer index operand. |
| 274 | auto indexAttr = getAttrOfType<IntegerAttr>("index"); |
| 275 | if (!indexAttr) |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 276 | return "requires an integer attribute named 'index'"; |
| 277 | uint64_t index = (uint64_t)indexAttr->getValue(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 278 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame] | 279 | auto *type = getOperand()->getType(); |
| 280 | if (auto *tensorType = dyn_cast<RankedTensorType>(type)) { |
| 281 | if (index >= tensorType->getRank()) |
| 282 | return "index is out of range"; |
| 283 | } else if (auto *memrefType = dyn_cast<MemRefType>(type)) { |
| 284 | if (index >= memrefType->getRank()) |
| 285 | return "index is out of range"; |
| 286 | |
| 287 | } else if (isa<UnrankedTensorType>(type)) { |
| 288 | // ok, assumed to be in-range. |
| 289 | } else { |
| 290 | return "requires an operand with tensor or memref type"; |
| 291 | } |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 292 | |
| 293 | return nullptr; |
| 294 | } |
| 295 | |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 296 | void LoadOp::print(OpAsmPrinter *p) const { |
| 297 | *p << "load " << *getMemRef() << '['; |
| 298 | p->printOperands(getIndices()); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 299 | *p << ']'; |
| 300 | p->printOptionalAttrDict(getAttrs()); |
| 301 | *p << " : " << *getMemRef()->getType(); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 304 | bool LoadOp::parse(OpAsmParser *parser, OperationState *result) { |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 305 | OpAsmParser::OperandType memrefInfo; |
| 306 | SmallVector<OpAsmParser::OperandType, 4> indexInfo; |
| 307 | MemRefType *type; |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 308 | |
| 309 | auto affineIntTy = parser->getBuilder().getAffineIntType(); |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame^] | 310 | return parser->parseOperand(memrefInfo) || |
| 311 | parser->parseOperandList(indexInfo, -1, |
| 312 | OpAsmParser::Delimiter::Square) || |
| 313 | parser->parseOptionalAttributeDict(result->attributes) || |
| 314 | parser->parseColonType(type) || |
| 315 | parser->resolveOperand(memrefInfo, type, result->operands) || |
| 316 | parser->resolveOperands(indexInfo, affineIntTy, result->operands) || |
| 317 | parser->addTypeToList(type->getElementType(), result->types); |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | const char *LoadOp::verify() const { |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 321 | if (getNumOperands() == 0) |
| 322 | return "expected a memref to load from"; |
Chris Lattner | 85ee151 | 2018-07-25 11:15:20 -0700 | [diff] [blame] | 323 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 324 | auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType()); |
| 325 | if (!memRefType) |
| 326 | return "first operand must be a memref"; |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 327 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 328 | for (auto *idx : getIndices()) |
| 329 | if (!idx->getType()->isAffineInt()) |
| 330 | return "index to load must have 'affineint' type"; |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 331 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 332 | // TODO: Verify we have the right number of indices. |
MLIR Team | 39a3a60 | 2018-07-24 17:43:56 -0700 | [diff] [blame] | 333 | |
Chris Lattner | 3164ae6 | 2018-07-28 09:36:25 -0700 | [diff] [blame] | 334 | // TODO: in MLFunction verify that the indices are parameters, IV's, or the |
| 335 | // result of an affine_apply. |
MLIR Team | 3fa00ab | 2018-07-24 10:13:31 -0700 | [diff] [blame] | 336 | return nullptr; |
| 337 | } |
| 338 | |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 339 | void StoreOp::print(OpAsmPrinter *p) const { |
| 340 | *p << "store " << *getValueToStore(); |
| 341 | *p << ", " << *getMemRef() << '['; |
| 342 | p->printOperands(getIndices()); |
Chris Lattner | 85cf26d | 2018-08-02 16:54:36 -0700 | [diff] [blame] | 343 | *p << ']'; |
| 344 | p->printOptionalAttrDict(getAttrs()); |
| 345 | *p << " : " << *getMemRef()->getType(); |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 348 | bool StoreOp::parse(OpAsmParser *parser, OperationState *result) { |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 349 | OpAsmParser::OperandType storeValueInfo; |
| 350 | OpAsmParser::OperandType memrefInfo; |
| 351 | SmallVector<OpAsmParser::OperandType, 4> indexInfo; |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 352 | MemRefType *memrefType; |
| 353 | |
| 354 | auto affineIntTy = parser->getBuilder().getAffineIntType(); |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 355 | return parser->parseOperand(storeValueInfo) || parser->parseComma() || |
| 356 | parser->parseOperand(memrefInfo) || |
| 357 | parser->parseOperandList(indexInfo, -1, |
| 358 | OpAsmParser::Delimiter::Square) || |
| 359 | parser->parseOptionalAttributeDict(result->attributes) || |
| 360 | parser->parseColonType(memrefType) || |
Chris Lattner | 8bdbebf | 2018-08-08 11:02:58 -0700 | [diff] [blame^] | 361 | parser->resolveOperand(storeValueInfo, memrefType->getElementType(), |
| 362 | result->operands) || |
| 363 | parser->resolveOperand(memrefInfo, memrefType, result->operands) || |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 364 | parser->resolveOperands(indexInfo, affineIntTy, result->operands); |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | const char *StoreOp::verify() const { |
| 368 | if (getNumOperands() < 2) |
| 369 | return "expected a value to store and a memref"; |
| 370 | |
| 371 | // Second operand is a memref type. |
| 372 | auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType()); |
| 373 | if (!memRefType) |
| 374 | return "second operand must be a memref"; |
| 375 | |
| 376 | // First operand must have same type as memref element type. |
| 377 | if (getValueToStore()->getType() != memRefType->getElementType()) |
| 378 | return "first operand must have same type memref element type "; |
| 379 | |
| 380 | if (getNumOperands() != 2 + memRefType->getRank()) |
| 381 | return "store index operand count not equal to memref rank"; |
| 382 | |
| 383 | for (auto *idx : getIndices()) |
| 384 | if (!idx->getType()->isAffineInt()) |
| 385 | return "index to load must have 'affineint' type"; |
| 386 | |
| 387 | // TODO: Verify we have the right number of indices. |
| 388 | |
| 389 | // TODO: in MLFunction verify that the indices are parameters, IV's, or the |
| 390 | // result of an affine_apply. |
| 391 | return nullptr; |
| 392 | } |
| 393 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 394 | /// Install the standard operations in the specified operation set. |
| 395 | void mlir::registerStandardOperations(OperationSet &opSet) { |
MLIR Team | c5efe7e | 2018-07-31 14:11:38 -0700 | [diff] [blame] | 396 | opSet.addOperations<AddFOp, AffineApplyOp, AllocOp, ConstantOp, DimOp, LoadOp, |
| 397 | StoreOp>( |
| 398 | /*prefix=*/""); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 399 | } |