blob: cec23e069391fcd82c69f9d3847b573b0ebf2eb9 [file] [log] [blame]
Chris Lattnerff0d5902018-07-05 09:12:11 -07001//===- 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 Team3fa00ab2018-07-24 10:13:31 -070019#include "mlir/IR/AffineMap.h"
Chris Lattner85ee1512018-07-25 11:15:20 -070020#include "mlir/IR/Builders.h"
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -070021#include "mlir/IR/OpImplementation.h"
Chris Lattnerff0d5902018-07-05 09:12:11 -070022#include "mlir/IR/OperationSet.h"
Chris Lattner9361fb32018-07-24 08:34:58 -070023#include "mlir/IR/SSAValue.h"
24#include "mlir/IR/Types.h"
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -070025#include "mlir/Support/STLExtras.h"
Chris Lattnerff0d5902018-07-05 09:12:11 -070026#include "llvm/Support/raw_ostream.h"
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -070027
Chris Lattnerff0d5902018-07-05 09:12:11 -070028using namespace mlir;
29
MLIR Team554a8ad2018-07-30 13:08:05 -070030static 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.
47static bool
48parseDimAndSymbolList(OpAsmParser *parser,
MLIR Team554a8ad2018-07-30 13:08:05 -070049 SmallVector<SSAValue *, 4> &operands, unsigned &numDims) {
Chris Lattnereed6c4d2018-08-07 09:12:35 -070050 SmallVector<OpAsmParser::OperandType, 8> opInfos;
Chris Lattner85cf26d2018-08-02 16:54:36 -070051 if (parser->parseOperandList(opInfos, -1, OpAsmParser::Delimiter::Paren))
MLIR Team554a8ad2018-07-30 13:08:05 -070052 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 Lattner85cf26d2018-08-02 16:54:36 -070058 if (parser->parseOperandList(opInfos, -1,
59 OpAsmParser::Delimiter::OptionalSquare) ||
MLIR Team554a8ad2018-07-30 13:08:05 -070060 parser->resolveOperands(opInfos, affineIntTy, operands))
61 return true;
62 return false;
63}
64
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -070065//===----------------------------------------------------------------------===//
66// AddFOp
67//===----------------------------------------------------------------------===//
68
Chris Lattnereed6c4d2018-08-07 09:12:35 -070069bool AddFOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -070070 SmallVector<OpAsmParser::OperandType, 2> ops;
71 Type *type;
Chris Lattner8bdbebf2018-08-08 11:02:58 -070072 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 Lattner85ee1512018-07-25 11:15:20 -070077}
78
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -070079void AddFOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -070080 *p << "addf " << *getOperand(0) << ", " << *getOperand(1);
81 p->printOptionalAttrDict(getAttrs());
82 *p << " : " << *getType();
Chris Lattnerff0d5902018-07-05 09:12:11 -070083}
84
Chris Lattnereed6c4d2018-08-07 09:12:35 -070085// TODO: Have verify functions return std::string to enable more descriptive
86// error messages.
Chris Lattner21e67f62018-07-06 10:46:19 -070087// Return an error message on failure.
88const 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 Shpeismand9b1d862018-08-09 12:28:58 -070095//===----------------------------------------------------------------------===//
96// AffineApplyOp
97//===----------------------------------------------------------------------===//
98
Chris Lattnereed6c4d2018-08-07 09:12:35 -070099bool AffineApplyOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner3164ae62018-07-28 09:36:25 -0700100 auto &builder = parser->getBuilder();
101 auto *affineIntTy = builder.getAffineIntType();
102
103 AffineMapAttr *mapAttr;
MLIR Team554a8ad2018-07-30 13:08:05 -0700104 unsigned numDims;
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700105 if (parser->parseAttribute(mapAttr, "map", result->attributes) ||
106 parseDimAndSymbolList(parser, result->operands, numDims) ||
107 parser->parseOptionalAttributeDict(result->attributes))
108 return true;
Chris Lattner3164ae62018-07-28 09:36:25 -0700109 auto *map = mapAttr->getValue();
MLIR Team554a8ad2018-07-30 13:08:05 -0700110
Chris Lattner3164ae62018-07-28 09:36:25 -0700111 if (map->getNumDims() != numDims ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700112 numDims + map->getNumSymbols() != result->operands.size()) {
113 return parser->emitError(parser->getNameLoc(),
114 "dimension or symbol index mismatch");
Chris Lattner3164ae62018-07-28 09:36:25 -0700115 }
116
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700117 result->types.append(map->getNumResults(), affineIntTy);
118 return false;
Chris Lattner3164ae62018-07-28 09:36:25 -0700119}
120
121void AffineApplyOp::print(OpAsmPrinter *p) const {
122 auto *map = getAffineMap();
123 *p << "affine_apply " << *map;
MLIR Team554a8ad2018-07-30 13:08:05 -0700124 printDimAndSymbolList(operand_begin(), operand_end(), map->getNumDims(), p);
Chris Lattner85cf26d2018-08-02 16:54:36 -0700125 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map");
Chris Lattner3164ae62018-07-28 09:36:25 -0700126}
127
128const 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 Shpeismand9b1d862018-08-09 12:28:58 -0700148//===----------------------------------------------------------------------===//
149// AllocOp
150//===----------------------------------------------------------------------===//
151
MLIR Team554a8ad2018-07-30 13:08:05 -0700152void 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 Lattner85cf26d2018-08-02 16:54:36 -0700158 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map");
MLIR Team554a8ad2018-07-30 13:08:05 -0700159 *p << " : " << *type;
160}
161
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700162bool AllocOp::parse(OpAsmParser *parser, OperationState *result) {
MLIR Team554a8ad2018-07-30 13:08:05 -0700163 MemRefType *type;
MLIR Team554a8ad2018-07-30 13:08:05 -0700164
Chris Lattner7d3b77c2018-07-31 16:21:36 -0700165 // Parse the dimension operands and optional symbol operands, followed by a
166 // memref type.
MLIR Team554a8ad2018-07-30 13:08:05 -0700167 unsigned numDimOperands;
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700168 if (parseDimAndSymbolList(parser, result->operands, numDimOperands) ||
169 parser->parseOptionalAttributeDict(result->attributes) ||
170 parser->parseColonType(type))
171 return true;
MLIR Team554a8ad2018-07-30 13:08:05 -0700172
173 // Check numDynamicDims against number of question marks in memref type.
174 if (numDimOperands != type->getNumDynamicDims()) {
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700175 return parser->emitError(parser->getNameLoc(),
176 "dimension operand count does not equal memref "
177 "dynamic dimension count");
MLIR Team554a8ad2018-07-30 13:08:05 -0700178 }
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 Lattnereed6c4d2018-08-07 09:12:35 -0700183 if (result->operands.size() - numDimOperands !=
MLIR Team554a8ad2018-07-30 13:08:05 -0700184 type->getAffineMaps()[0]->getNumSymbols()) {
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700185 return parser->emitError(
186 parser->getNameLoc(),
187 "affine map symbol operand count does not equal memref affine map "
188 "symbol count");
MLIR Team554a8ad2018-07-30 13:08:05 -0700189 }
190
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700191 result->types.push_back(type);
192 return false;
MLIR Team554a8ad2018-07-30 13:08:05 -0700193}
194
195const char *AllocOp::verify() const {
196 // TODO(andydavis): Verify alloc.
197 return nullptr;
198}
199
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700200//===----------------------------------------------------------------------===//
201// ConstantOp
202//===----------------------------------------------------------------------===//
203
Chris Lattnerd4964212018-08-01 10:43:18 -0700204void ConstantOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -0700205 *p << "constant " << *getValue();
206 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"value");
Chris Lattner4613d9e2018-08-19 21:17:22 -0700207
208 if (!isa<FunctionAttr>(getValue()))
209 *p << " : " << *getType();
Chris Lattnerd4964212018-08-01 10:43:18 -0700210}
211
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700212bool ConstantOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattnerd4964212018-08-01 10:43:18 -0700213 Attribute *valueAttr;
214 Type *type;
Chris Lattnerd4964212018-08-01 10:43:18 -0700215
Chris Lattner4613d9e2018-08-19 21:17:22 -0700216 if (parser->parseAttribute(valueAttr, "value", result->attributes) ||
217 parser->parseOptionalAttributeDict(result->attributes))
218 return true;
219
220 // 'constant' taking a function reference doesn't get a redundant type
221 // specifier. The attribute itself carries it.
222 if (auto *fnAttr = dyn_cast<FunctionAttr>(valueAttr))
223 return parser->addTypeToList(fnAttr->getValue()->getType(), result->types);
224
225 return parser->parseColonType(type) ||
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700226 parser->addTypeToList(type, result->types);
Chris Lattnerd4964212018-08-01 10:43:18 -0700227}
228
Chris Lattner9361fb32018-07-24 08:34:58 -0700229/// The constant op requires an attribute, and furthermore requires that it
230/// matches the return type.
231const char *ConstantOp::verify() const {
232 auto *value = getValue();
233 if (!value)
234 return "requires a 'value' attribute";
235
236 auto *type = this->getType();
Chris Lattner1ec70572018-07-24 10:41:30 -0700237 if (isa<IntegerType>(type) || type->isAffineInt()) {
Chris Lattner9361fb32018-07-24 08:34:58 -0700238 if (!isa<IntegerAttr>(value))
239 return "requires 'value' to be an integer for an integer result type";
240 return nullptr;
241 }
242
Chris Lattner7ba98c62018-08-16 16:56:40 -0700243 if (isa<FloatType>(type)) {
244 if (!isa<FloatAttr>(value))
245 return "requires 'value' to be a floating point constant";
246 return nullptr;
247 }
248
249 if (type->isTFString()) {
250 if (!isa<StringAttr>(value))
251 return "requires 'value' to be a string constant";
252 return nullptr;
253 }
254
Chris Lattner9361fb32018-07-24 08:34:58 -0700255 if (isa<FunctionType>(type)) {
Chris Lattner4613d9e2018-08-19 21:17:22 -0700256 if (!isa<FunctionAttr>(value))
257 return "requires 'value' to be a function reference";
258 return nullptr;
Chris Lattner9361fb32018-07-24 08:34:58 -0700259 }
260
261 return "requires a result type that aligns with the 'value' attribute";
262}
263
Chris Lattner7ba98c62018-08-16 16:56:40 -0700264OperationState ConstantFloatOp::build(Builder *builder, double value,
265 FloatType *type) {
266 OperationState result(builder->getIdentifier("constant"));
267 result.attributes.push_back(
268 {builder->getIdentifier("value"), builder->getFloatAttr(value)});
269 result.types.push_back(type);
270 return result;
271}
272
273bool ConstantFloatOp::isClassFor(const Operation *op) {
274 return ConstantOp::isClassFor(op) &&
275 isa<FloatType>(op->getResult(0)->getType());
276}
277
Chris Lattner992a1272018-08-07 12:02:37 -0700278/// ConstantIntOp only matches values whose result type is an IntegerType.
Chris Lattner9361fb32018-07-24 08:34:58 -0700279bool ConstantIntOp::isClassFor(const Operation *op) {
280 return ConstantOp::isClassFor(op) &&
Chris Lattner992a1272018-08-07 12:02:37 -0700281 isa<IntegerType>(op->getResult(0)->getType());
282}
283
284OperationState ConstantIntOp::build(Builder *builder, int64_t value,
285 unsigned width) {
286 OperationState result(builder->getIdentifier("constant"));
287 result.attributes.push_back(
288 {builder->getIdentifier("value"), builder->getIntegerAttr(value)});
289 result.types.push_back(builder->getIntegerType(width));
290 return result;
291}
292
293/// ConstantAffineIntOp only matches values whose result type is AffineInt.
294bool ConstantAffineIntOp::isClassFor(const Operation *op) {
295 return ConstantOp::isClassFor(op) &&
296 op->getResult(0)->getType()->isAffineInt();
297}
298
299OperationState ConstantAffineIntOp::build(Builder *builder, int64_t value) {
300 OperationState result(builder->getIdentifier("constant"));
301 result.attributes.push_back(
302 {builder->getIdentifier("value"), builder->getIntegerAttr(value)});
303 result.types.push_back(builder->getAffineIntType());
304 return result;
Chris Lattner9361fb32018-07-24 08:34:58 -0700305}
306
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700307//===----------------------------------------------------------------------===//
Uday Bondhugula67701712018-08-21 16:01:23 -0700308// AffineApplyOp
309//===----------------------------------------------------------------------===//
310
311OperationState AffineApplyOp::build(Builder *builder, AffineMap *map,
312 ArrayRef<SSAValue *> operands) {
313 SmallVector<Type *, 4> resultTypes(map->getNumResults(),
314 builder->getAffineIntType());
315
316 OperationState result(
317 builder->getIdentifier("affine_apply"), operands, resultTypes,
318 {{builder->getIdentifier("map"), builder->getAffineMapAttr(map)}});
319
320 return result;
321}
322
323//===----------------------------------------------------------------------===//
MLIR Team1989cc12018-08-15 15:39:26 -0700324// DeallocOp
325//===----------------------------------------------------------------------===//
326
327void DeallocOp::print(OpAsmPrinter *p) const {
328 *p << "dealloc " << *getMemRef() << " : " << *getMemRef()->getType();
329}
330
331bool DeallocOp::parse(OpAsmParser *parser, OperationState *result) {
332 OpAsmParser::OperandType memrefInfo;
333 MemRefType *type;
334
335 return parser->parseOperand(memrefInfo) || parser->parseColonType(type) ||
336 parser->resolveOperand(memrefInfo, type, result->operands);
337}
338
339const char *DeallocOp::verify() const {
340 if (!isa<MemRefType>(getMemRef()->getType()))
341 return "operand must be a memref";
342 return nullptr;
343}
344
345//===----------------------------------------------------------------------===//
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700346// DimOp
347//===----------------------------------------------------------------------===//
348
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -0700349void DimOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -0700350 *p << "dim " << *getOperand() << ", " << getIndex();
351 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"index");
352 *p << " : " << *getOperand()->getType();
Chris Lattnerff0d5902018-07-05 09:12:11 -0700353}
354
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700355bool DimOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -0700356 OpAsmParser::OperandType operandInfo;
357 IntegerAttr *indexAttr;
358 Type *type;
Chris Lattner85cf26d2018-08-02 16:54:36 -0700359
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700360 return parser->parseOperand(operandInfo) || parser->parseComma() ||
361 parser->parseAttribute(indexAttr, "index", result->attributes) ||
362 parser->parseOptionalAttributeDict(result->attributes) ||
363 parser->parseColonType(type) ||
364 parser->resolveOperand(operandInfo, type, result->operands) ||
365 parser->addTypeToList(parser->getBuilder().getAffineIntType(),
366 result->types);
Chris Lattner85ee1512018-07-25 11:15:20 -0700367}
368
Chris Lattner21e67f62018-07-06 10:46:19 -0700369const char *DimOp::verify() const {
Chris Lattner21e67f62018-07-06 10:46:19 -0700370 // Check that we have an integer index operand.
371 auto indexAttr = getAttrOfType<IntegerAttr>("index");
372 if (!indexAttr)
Chris Lattner9361fb32018-07-24 08:34:58 -0700373 return "requires an integer attribute named 'index'";
374 uint64_t index = (uint64_t)indexAttr->getValue();
Chris Lattner21e67f62018-07-06 10:46:19 -0700375
Chris Lattner9361fb32018-07-24 08:34:58 -0700376 auto *type = getOperand()->getType();
377 if (auto *tensorType = dyn_cast<RankedTensorType>(type)) {
378 if (index >= tensorType->getRank())
379 return "index is out of range";
380 } else if (auto *memrefType = dyn_cast<MemRefType>(type)) {
381 if (index >= memrefType->getRank())
382 return "index is out of range";
383
384 } else if (isa<UnrankedTensorType>(type)) {
385 // ok, assumed to be in-range.
386 } else {
387 return "requires an operand with tensor or memref type";
388 }
Chris Lattner21e67f62018-07-06 10:46:19 -0700389
390 return nullptr;
391}
392
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700393//===----------------------------------------------------------------------===//
394// LoadOp
395//===----------------------------------------------------------------------===//
396
Chris Lattner85ee1512018-07-25 11:15:20 -0700397void LoadOp::print(OpAsmPrinter *p) const {
398 *p << "load " << *getMemRef() << '[';
399 p->printOperands(getIndices());
Chris Lattner85cf26d2018-08-02 16:54:36 -0700400 *p << ']';
401 p->printOptionalAttrDict(getAttrs());
402 *p << " : " << *getMemRef()->getType();
Chris Lattner85ee1512018-07-25 11:15:20 -0700403}
404
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700405bool LoadOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -0700406 OpAsmParser::OperandType memrefInfo;
407 SmallVector<OpAsmParser::OperandType, 4> indexInfo;
408 MemRefType *type;
Chris Lattner85ee1512018-07-25 11:15:20 -0700409
410 auto affineIntTy = parser->getBuilder().getAffineIntType();
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700411 return parser->parseOperand(memrefInfo) ||
412 parser->parseOperandList(indexInfo, -1,
413 OpAsmParser::Delimiter::Square) ||
414 parser->parseOptionalAttributeDict(result->attributes) ||
415 parser->parseColonType(type) ||
416 parser->resolveOperand(memrefInfo, type, result->operands) ||
417 parser->resolveOperands(indexInfo, affineIntTy, result->operands) ||
418 parser->addTypeToList(type->getElementType(), result->types);
Chris Lattner85ee1512018-07-25 11:15:20 -0700419}
420
421const char *LoadOp::verify() const {
Chris Lattner3164ae62018-07-28 09:36:25 -0700422 if (getNumOperands() == 0)
423 return "expected a memref to load from";
Chris Lattner85ee1512018-07-25 11:15:20 -0700424
Chris Lattner3164ae62018-07-28 09:36:25 -0700425 auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType());
426 if (!memRefType)
427 return "first operand must be a memref";
MLIR Team3fa00ab2018-07-24 10:13:31 -0700428
Chris Lattner3164ae62018-07-28 09:36:25 -0700429 for (auto *idx : getIndices())
430 if (!idx->getType()->isAffineInt())
431 return "index to load must have 'affineint' type";
MLIR Team3fa00ab2018-07-24 10:13:31 -0700432
Chris Lattner3164ae62018-07-28 09:36:25 -0700433 // TODO: Verify we have the right number of indices.
MLIR Team39a3a602018-07-24 17:43:56 -0700434
Chris Lattner3164ae62018-07-28 09:36:25 -0700435 // TODO: in MLFunction verify that the indices are parameters, IV's, or the
436 // result of an affine_apply.
MLIR Team3fa00ab2018-07-24 10:13:31 -0700437 return nullptr;
438}
439
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700440//===----------------------------------------------------------------------===//
441// ReturnOp
442//===----------------------------------------------------------------------===//
443
444bool ReturnOp::parse(OpAsmParser *parser, OperationState *result) {
445 SmallVector<OpAsmParser::OperandType, 2> opInfo;
446 SmallVector<Type *, 2> types;
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700447
448 return parser->parseOperandList(opInfo, -1, OpAsmParser::Delimiter::None) ||
449 (!opInfo.empty() && parser->parseColonTypeList(types)) ||
450 parser->resolveOperands(opInfo, types, result->operands);
451}
452
453void ReturnOp::print(OpAsmPrinter *p) const {
454 *p << "return";
455 if (getNumOperands() > 0) {
456 *p << " ";
457 p->printOperands(operand_begin(), operand_end());
458 *p << " : ";
459 interleave(operand_begin(), operand_end(),
460 [&](auto *e) { p->printType(e->getType()); },
461 [&]() { *p << ", "; });
462 }
463}
464
465const char *ReturnOp::verify() const {
466 // ReturnOp must be part of an ML function.
467 if (auto *stmt = dyn_cast<OperationStmt>(getOperation())) {
Tatiana Shpeisman3abd6bd2018-08-16 20:19:44 -0700468 StmtBlock *block = stmt->getBlock();
469 if (!block || !isa<MLFunction>(block) || &block->back() != stmt)
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700470 return "must be the last statement in the ML function";
471
472 // Return success. Checking that operand types match those in the function
473 // signature is performed in the ML function verifier.
474 return nullptr;
475 }
Tatiana Shpeismanb697ac12018-08-09 23:21:19 -0700476 return "cannot occur in a CFG function";
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700477}
478
479//===----------------------------------------------------------------------===//
480// StoreOp
481//===----------------------------------------------------------------------===//
482
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700483void StoreOp::print(OpAsmPrinter *p) const {
484 *p << "store " << *getValueToStore();
485 *p << ", " << *getMemRef() << '[';
486 p->printOperands(getIndices());
Chris Lattner85cf26d2018-08-02 16:54:36 -0700487 *p << ']';
488 p->printOptionalAttrDict(getAttrs());
489 *p << " : " << *getMemRef()->getType();
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700490}
491
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700492bool StoreOp::parse(OpAsmParser *parser, OperationState *result) {
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700493 OpAsmParser::OperandType storeValueInfo;
494 OpAsmParser::OperandType memrefInfo;
495 SmallVector<OpAsmParser::OperandType, 4> indexInfo;
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700496 MemRefType *memrefType;
497
498 auto affineIntTy = parser->getBuilder().getAffineIntType();
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700499 return parser->parseOperand(storeValueInfo) || parser->parseComma() ||
500 parser->parseOperand(memrefInfo) ||
501 parser->parseOperandList(indexInfo, -1,
502 OpAsmParser::Delimiter::Square) ||
503 parser->parseOptionalAttributeDict(result->attributes) ||
504 parser->parseColonType(memrefType) ||
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700505 parser->resolveOperand(storeValueInfo, memrefType->getElementType(),
506 result->operands) ||
507 parser->resolveOperand(memrefInfo, memrefType, result->operands) ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700508 parser->resolveOperands(indexInfo, affineIntTy, result->operands);
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700509}
510
511const char *StoreOp::verify() const {
512 if (getNumOperands() < 2)
513 return "expected a value to store and a memref";
514
515 // Second operand is a memref type.
516 auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType());
517 if (!memRefType)
518 return "second operand must be a memref";
519
520 // First operand must have same type as memref element type.
521 if (getValueToStore()->getType() != memRefType->getElementType())
522 return "first operand must have same type memref element type ";
523
524 if (getNumOperands() != 2 + memRefType->getRank())
525 return "store index operand count not equal to memref rank";
526
527 for (auto *idx : getIndices())
528 if (!idx->getType()->isAffineInt())
529 return "index to load must have 'affineint' type";
530
531 // TODO: Verify we have the right number of indices.
532
533 // TODO: in MLFunction verify that the indices are parameters, IV's, or the
534 // result of an affine_apply.
535 return nullptr;
536}
537
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700538//===----------------------------------------------------------------------===//
539// Register operations.
540//===----------------------------------------------------------------------===//
541
Chris Lattnerff0d5902018-07-05 09:12:11 -0700542/// Install the standard operations in the specified operation set.
543void mlir::registerStandardOperations(OperationSet &opSet) {
MLIR Team1989cc12018-08-15 15:39:26 -0700544 opSet.addOperations<AddFOp, AffineApplyOp, AllocOp, ConstantOp, DeallocOp,
545 DimOp, LoadOp, ReturnOp, StoreOp>(
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700546 /*prefix=*/"");
Chris Lattnerff0d5902018-07-05 09:12:11 -0700547}