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