blob: 18507b0634255fef4b07a641b075c242563cb735 [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 Lattner1eb77482018-08-22 19:25:49 -070069void AddFOp::build(Builder *builder, OperationState *result, SSAValue *lhs,
70 SSAValue *rhs) {
71 assert(lhs->getType() == rhs->getType());
72 result->addOperands({lhs, rhs});
73 result->types.push_back(lhs->getType());
74}
75
Chris Lattnereed6c4d2018-08-07 09:12:35 -070076bool AddFOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -070077 SmallVector<OpAsmParser::OperandType, 2> ops;
78 Type *type;
Chris Lattner8bdbebf2018-08-08 11:02:58 -070079 return parser->parseOperandList(ops, 2) ||
80 parser->parseOptionalAttributeDict(result->attributes) ||
81 parser->parseColonType(type) ||
82 parser->resolveOperands(ops, type, result->operands) ||
83 parser->addTypeToList(type, result->types);
Chris Lattner85ee1512018-07-25 11:15:20 -070084}
85
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -070086void AddFOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -070087 *p << "addf " << *getOperand(0) << ", " << *getOperand(1);
88 p->printOptionalAttrDict(getAttrs());
89 *p << " : " << *getType();
Chris Lattnerff0d5902018-07-05 09:12:11 -070090}
91
Chris Lattnereed6c4d2018-08-07 09:12:35 -070092// TODO: Have verify functions return std::string to enable more descriptive
93// error messages.
Chris Lattner21e67f62018-07-06 10:46:19 -070094// Return an error message on failure.
95const char *AddFOp::verify() const {
96 // TODO: Check that the types of the LHS and RHS match.
97 // TODO: This should be a refinement of TwoOperands.
98 // TODO: There should also be a OneResultWhoseTypeMatchesFirstOperand.
99 return nullptr;
100}
101
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700102//===----------------------------------------------------------------------===//
103// AffineApplyOp
104//===----------------------------------------------------------------------===//
105
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700106bool AffineApplyOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner3164ae62018-07-28 09:36:25 -0700107 auto &builder = parser->getBuilder();
108 auto *affineIntTy = builder.getAffineIntType();
109
110 AffineMapAttr *mapAttr;
MLIR Team554a8ad2018-07-30 13:08:05 -0700111 unsigned numDims;
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700112 if (parser->parseAttribute(mapAttr, "map", result->attributes) ||
113 parseDimAndSymbolList(parser, result->operands, numDims) ||
114 parser->parseOptionalAttributeDict(result->attributes))
115 return true;
Chris Lattner3164ae62018-07-28 09:36:25 -0700116 auto *map = mapAttr->getValue();
MLIR Team554a8ad2018-07-30 13:08:05 -0700117
Chris Lattner3164ae62018-07-28 09:36:25 -0700118 if (map->getNumDims() != numDims ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700119 numDims + map->getNumSymbols() != result->operands.size()) {
120 return parser->emitError(parser->getNameLoc(),
121 "dimension or symbol index mismatch");
Chris Lattner3164ae62018-07-28 09:36:25 -0700122 }
123
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700124 result->types.append(map->getNumResults(), affineIntTy);
125 return false;
Chris Lattner3164ae62018-07-28 09:36:25 -0700126}
127
128void AffineApplyOp::print(OpAsmPrinter *p) const {
129 auto *map = getAffineMap();
130 *p << "affine_apply " << *map;
MLIR Team554a8ad2018-07-30 13:08:05 -0700131 printDimAndSymbolList(operand_begin(), operand_end(), map->getNumDims(), p);
Chris Lattner85cf26d2018-08-02 16:54:36 -0700132 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map");
Chris Lattner3164ae62018-07-28 09:36:25 -0700133}
134
135const char *AffineApplyOp::verify() const {
136 // Check that affine map attribute was specified.
137 auto *affineMapAttr = getAttrOfType<AffineMapAttr>("map");
138 if (!affineMapAttr)
Chris Lattner1aa46322018-08-21 17:55:22 -0700139 return "requires an affine map";
Chris Lattner3164ae62018-07-28 09:36:25 -0700140
141 // Check input and output dimensions match.
142 auto *map = affineMapAttr->getValue();
143
144 // Verify that operand count matches affine map dimension and symbol count.
145 if (getNumOperands() != map->getNumDims() + map->getNumSymbols())
146 return "operand count and affine map dimension and symbol count must match";
147
148 // Verify that result count matches affine map result count.
149 if (getNumResults() != map->getNumResults())
150 return "result count and affine map result count must match";
151
152 return nullptr;
153}
154
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700155//===----------------------------------------------------------------------===//
156// AllocOp
157//===----------------------------------------------------------------------===//
158
MLIR Team554a8ad2018-07-30 13:08:05 -0700159void AllocOp::print(OpAsmPrinter *p) const {
160 MemRefType *type = cast<MemRefType>(getMemRef()->getType());
161 *p << "alloc";
162 // Print dynamic dimension operands.
163 printDimAndSymbolList(operand_begin(), operand_end(),
164 type->getNumDynamicDims(), p);
Chris Lattner85cf26d2018-08-02 16:54:36 -0700165 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map");
MLIR Team554a8ad2018-07-30 13:08:05 -0700166 *p << " : " << *type;
167}
168
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700169bool AllocOp::parse(OpAsmParser *parser, OperationState *result) {
MLIR Team554a8ad2018-07-30 13:08:05 -0700170 MemRefType *type;
MLIR Team554a8ad2018-07-30 13:08:05 -0700171
Chris Lattner7d3b77c2018-07-31 16:21:36 -0700172 // Parse the dimension operands and optional symbol operands, followed by a
173 // memref type.
MLIR Team554a8ad2018-07-30 13:08:05 -0700174 unsigned numDimOperands;
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700175 if (parseDimAndSymbolList(parser, result->operands, numDimOperands) ||
176 parser->parseOptionalAttributeDict(result->attributes) ||
177 parser->parseColonType(type))
178 return true;
MLIR Team554a8ad2018-07-30 13:08:05 -0700179
180 // Check numDynamicDims against number of question marks in memref type.
181 if (numDimOperands != type->getNumDynamicDims()) {
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700182 return parser->emitError(parser->getNameLoc(),
183 "dimension operand count does not equal memref "
184 "dynamic dimension count");
MLIR Team554a8ad2018-07-30 13:08:05 -0700185 }
186
187 // Check that the number of symbol operands matches the number of symbols in
188 // the first affinemap of the memref's affine map composition.
189 // Note that a memref must specify at least one affine map in the composition.
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700190 if (result->operands.size() - numDimOperands !=
MLIR Team554a8ad2018-07-30 13:08:05 -0700191 type->getAffineMaps()[0]->getNumSymbols()) {
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700192 return parser->emitError(
193 parser->getNameLoc(),
194 "affine map symbol operand count does not equal memref affine map "
195 "symbol count");
MLIR Team554a8ad2018-07-30 13:08:05 -0700196 }
197
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700198 result->types.push_back(type);
199 return false;
MLIR Team554a8ad2018-07-30 13:08:05 -0700200}
201
202const char *AllocOp::verify() const {
203 // TODO(andydavis): Verify alloc.
204 return nullptr;
205}
206
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700207//===----------------------------------------------------------------------===//
Chris Lattner1aa46322018-08-21 17:55:22 -0700208// CallOp
209//===----------------------------------------------------------------------===//
210
Chris Lattner1eb77482018-08-22 19:25:49 -0700211void CallOp::build(Builder *builder, OperationState *result, Function *callee,
212 ArrayRef<SSAValue *> operands) {
213 result->addOperands(operands);
214 result->addAttribute("callee", builder->getFunctionAttr(callee));
215 result->addTypes(callee->getType()->getResults());
Chris Lattner1aa46322018-08-21 17:55:22 -0700216}
217
218bool CallOp::parse(OpAsmParser *parser, OperationState *result) {
219 StringRef calleeName;
220 llvm::SMLoc calleeLoc;
221 FunctionType *calleeType = nullptr;
222 SmallVector<OpAsmParser::OperandType, 4> operands;
223 Function *callee = nullptr;
224 if (parser->parseFunctionName(calleeName, calleeLoc) ||
225 parser->parseOperandList(operands, /*requiredOperandCount=*/-1,
226 OpAsmParser::Delimiter::Paren) ||
227 parser->parseOptionalAttributeDict(result->attributes) ||
228 parser->parseColonType(calleeType) ||
229 parser->resolveFunctionName(calleeName, calleeType, calleeLoc, callee) ||
230 parser->addTypesToList(calleeType->getResults(), result->types) ||
231 parser->resolveOperands(operands, calleeType->getInputs(), calleeLoc,
232 result->operands))
233 return true;
234
Chris Lattner1eb77482018-08-22 19:25:49 -0700235 result->addAttribute("callee", parser->getBuilder().getFunctionAttr(callee));
Chris Lattner1aa46322018-08-21 17:55:22 -0700236 return false;
237}
238
239void 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
249const char *CallOp::verify() const {
250 // Check that the callee attribute was specified.
251 auto *fnAttr = getAttrOfType<FunctionAttr>("callee");
252 if (!fnAttr)
253 return "requires a 'callee' function attribute";
254
255 // Verify that the operand and result types match the callee.
256 auto *fnType = fnAttr->getValue()->getType();
257 if (fnType->getNumInputs() != getNumOperands())
258 return "incorrect number of operands for callee";
259
260 for (unsigned i = 0, e = fnType->getNumInputs(); i != e; ++i) {
261 if (getOperand(i)->getType() != fnType->getInput(i))
262 return "operand type mismatch";
263 }
264
265 if (fnType->getNumResults() != getNumResults())
266 return "incorrect number of results for callee";
267
268 for (unsigned i = 0, e = fnType->getNumResults(); i != e; ++i) {
269 if (getResult(i)->getType() != fnType->getResult(i))
270 return "result type mismatch";
271 }
272
273 return nullptr;
274}
275
276//===----------------------------------------------------------------------===//
277// CallIndirectOp
278//===----------------------------------------------------------------------===//
279
Chris Lattner1eb77482018-08-22 19:25:49 -0700280void CallIndirectOp::build(Builder *builder, OperationState *result,
281 SSAValue *callee, ArrayRef<SSAValue *> operands) {
Chris Lattner1aa46322018-08-21 17:55:22 -0700282 auto *fnType = cast<FunctionType>(callee->getType());
Chris Lattner1eb77482018-08-22 19:25:49 -0700283 result->operands.push_back(callee);
284 result->addOperands(operands);
285 result->addTypes(fnType->getResults());
Chris Lattner1aa46322018-08-21 17:55:22 -0700286}
287
288bool CallIndirectOp::parse(OpAsmParser *parser, OperationState *result) {
289 FunctionType *calleeType = nullptr;
290 OpAsmParser::OperandType callee;
291 llvm::SMLoc operandsLoc;
292 SmallVector<OpAsmParser::OperandType, 4> operands;
293 return parser->parseOperand(callee) ||
294 parser->getCurrentLocation(&operandsLoc) ||
295 parser->parseOperandList(operands, /*requiredOperandCount=*/-1,
296 OpAsmParser::Delimiter::Paren) ||
297 parser->parseOptionalAttributeDict(result->attributes) ||
298 parser->parseColonType(calleeType) ||
299 parser->resolveOperand(callee, calleeType, result->operands) ||
300 parser->resolveOperands(operands, calleeType->getInputs(), operandsLoc,
301 result->operands) ||
302 parser->addTypesToList(calleeType->getResults(), result->types);
303}
304
305void CallIndirectOp::print(OpAsmPrinter *p) const {
306 *p << "call_indirect ";
307 p->printOperand(getCallee());
308 *p << '(';
309 auto operandRange = getOperands();
310 p->printOperands(++operandRange.begin(), operandRange.end());
311 *p << ')';
312 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"callee");
313 *p << " : " << *getCallee()->getType();
314}
315
316const char *CallIndirectOp::verify() const {
317 // The callee must be a function.
318 auto *fnType = dyn_cast<FunctionType>(getCallee()->getType());
319 if (!fnType)
320 return "callee must have function type";
321
322 // Verify that the operand and result types match the callee.
323 if (fnType->getNumInputs() != getNumOperands() - 1)
324 return "incorrect number of operands for callee";
325
326 for (unsigned i = 0, e = fnType->getNumInputs(); i != e; ++i) {
327 if (getOperand(i + 1)->getType() != fnType->getInput(i))
328 return "operand type mismatch";
329 }
330
331 if (fnType->getNumResults() != getNumResults())
332 return "incorrect number of results for callee";
333
334 for (unsigned i = 0, e = fnType->getNumResults(); i != e; ++i) {
335 if (getResult(i)->getType() != fnType->getResult(i))
336 return "result type mismatch";
337 }
338
339 return nullptr;
340}
341
342//===----------------------------------------------------------------------===//
343// Constant*Op
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700344//===----------------------------------------------------------------------===//
345
Chris Lattnerd4964212018-08-01 10:43:18 -0700346void ConstantOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -0700347 *p << "constant " << *getValue();
348 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"value");
Chris Lattner4613d9e2018-08-19 21:17:22 -0700349
350 if (!isa<FunctionAttr>(getValue()))
351 *p << " : " << *getType();
Chris Lattnerd4964212018-08-01 10:43:18 -0700352}
353
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700354bool ConstantOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattnerd4964212018-08-01 10:43:18 -0700355 Attribute *valueAttr;
356 Type *type;
Chris Lattnerd4964212018-08-01 10:43:18 -0700357
Chris Lattner4613d9e2018-08-19 21:17:22 -0700358 if (parser->parseAttribute(valueAttr, "value", result->attributes) ||
359 parser->parseOptionalAttributeDict(result->attributes))
360 return true;
361
362 // 'constant' taking a function reference doesn't get a redundant type
363 // specifier. The attribute itself carries it.
364 if (auto *fnAttr = dyn_cast<FunctionAttr>(valueAttr))
365 return parser->addTypeToList(fnAttr->getValue()->getType(), result->types);
366
367 return parser->parseColonType(type) ||
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700368 parser->addTypeToList(type, result->types);
Chris Lattnerd4964212018-08-01 10:43:18 -0700369}
370
Chris Lattner9361fb32018-07-24 08:34:58 -0700371/// The constant op requires an attribute, and furthermore requires that it
372/// matches the return type.
373const char *ConstantOp::verify() const {
374 auto *value = getValue();
375 if (!value)
376 return "requires a 'value' attribute";
377
378 auto *type = this->getType();
Chris Lattner1ec70572018-07-24 10:41:30 -0700379 if (isa<IntegerType>(type) || type->isAffineInt()) {
Chris Lattner9361fb32018-07-24 08:34:58 -0700380 if (!isa<IntegerAttr>(value))
381 return "requires 'value' to be an integer for an integer result type";
382 return nullptr;
383 }
384
Chris Lattner7ba98c62018-08-16 16:56:40 -0700385 if (isa<FloatType>(type)) {
386 if (!isa<FloatAttr>(value))
387 return "requires 'value' to be a floating point constant";
388 return nullptr;
389 }
390
391 if (type->isTFString()) {
392 if (!isa<StringAttr>(value))
393 return "requires 'value' to be a string constant";
394 return nullptr;
395 }
396
Chris Lattner9361fb32018-07-24 08:34:58 -0700397 if (isa<FunctionType>(type)) {
Chris Lattner4613d9e2018-08-19 21:17:22 -0700398 if (!isa<FunctionAttr>(value))
399 return "requires 'value' to be a function reference";
400 return nullptr;
Chris Lattner9361fb32018-07-24 08:34:58 -0700401 }
402
403 return "requires a result type that aligns with the 'value' attribute";
404}
405
Chris Lattner1eb77482018-08-22 19:25:49 -0700406void ConstantFloatOp::build(Builder *builder, OperationState *result,
407 double value, FloatType *type) {
408 result->addAttribute("value", builder->getFloatAttr(value));
409 result->types.push_back(type);
Chris Lattner7ba98c62018-08-16 16:56:40 -0700410}
411
412bool ConstantFloatOp::isClassFor(const Operation *op) {
413 return ConstantOp::isClassFor(op) &&
414 isa<FloatType>(op->getResult(0)->getType());
415}
416
Chris Lattner992a1272018-08-07 12:02:37 -0700417/// ConstantIntOp only matches values whose result type is an IntegerType.
Chris Lattner9361fb32018-07-24 08:34:58 -0700418bool ConstantIntOp::isClassFor(const Operation *op) {
419 return ConstantOp::isClassFor(op) &&
Chris Lattner992a1272018-08-07 12:02:37 -0700420 isa<IntegerType>(op->getResult(0)->getType());
421}
422
Chris Lattner1eb77482018-08-22 19:25:49 -0700423void ConstantIntOp::build(Builder *builder, OperationState *result,
424 int64_t value, unsigned width) {
425 result->addAttribute("value", builder->getIntegerAttr(value));
426 result->types.push_back(builder->getIntegerType(width));
Chris Lattner992a1272018-08-07 12:02:37 -0700427}
428
429/// ConstantAffineIntOp only matches values whose result type is AffineInt.
430bool ConstantAffineIntOp::isClassFor(const Operation *op) {
431 return ConstantOp::isClassFor(op) &&
432 op->getResult(0)->getType()->isAffineInt();
433}
434
Chris Lattner1eb77482018-08-22 19:25:49 -0700435void ConstantAffineIntOp::build(Builder *builder, OperationState *result,
436 int64_t value) {
437 result->addAttribute("value", builder->getIntegerAttr(value));
438 result->types.push_back(builder->getAffineIntType());
Chris Lattner9361fb32018-07-24 08:34:58 -0700439}
440
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700441//===----------------------------------------------------------------------===//
Uday Bondhugula67701712018-08-21 16:01:23 -0700442// AffineApplyOp
443//===----------------------------------------------------------------------===//
444
Chris Lattner1eb77482018-08-22 19:25:49 -0700445void AffineApplyOp::build(Builder *builder, OperationState *result,
446 AffineMap *map, ArrayRef<SSAValue *> operands) {
447 result->addOperands(operands);
448 result->types.append(map->getNumResults(), builder->getAffineIntType());
449 result->addAttribute("map", builder->getAffineMapAttr(map));
Uday Bondhugula67701712018-08-21 16:01:23 -0700450}
451
452//===----------------------------------------------------------------------===//
MLIR Team1989cc12018-08-15 15:39:26 -0700453// DeallocOp
454//===----------------------------------------------------------------------===//
455
456void DeallocOp::print(OpAsmPrinter *p) const {
457 *p << "dealloc " << *getMemRef() << " : " << *getMemRef()->getType();
458}
459
460bool DeallocOp::parse(OpAsmParser *parser, OperationState *result) {
461 OpAsmParser::OperandType memrefInfo;
462 MemRefType *type;
463
464 return parser->parseOperand(memrefInfo) || parser->parseColonType(type) ||
465 parser->resolveOperand(memrefInfo, type, result->operands);
466}
467
468const char *DeallocOp::verify() const {
469 if (!isa<MemRefType>(getMemRef()->getType()))
470 return "operand must be a memref";
471 return nullptr;
472}
473
474//===----------------------------------------------------------------------===//
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700475// DimOp
476//===----------------------------------------------------------------------===//
477
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -0700478void DimOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -0700479 *p << "dim " << *getOperand() << ", " << getIndex();
480 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"index");
481 *p << " : " << *getOperand()->getType();
Chris Lattnerff0d5902018-07-05 09:12:11 -0700482}
483
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700484bool DimOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -0700485 OpAsmParser::OperandType operandInfo;
486 IntegerAttr *indexAttr;
487 Type *type;
Chris Lattner85cf26d2018-08-02 16:54:36 -0700488
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700489 return parser->parseOperand(operandInfo) || parser->parseComma() ||
490 parser->parseAttribute(indexAttr, "index", result->attributes) ||
491 parser->parseOptionalAttributeDict(result->attributes) ||
492 parser->parseColonType(type) ||
493 parser->resolveOperand(operandInfo, type, result->operands) ||
494 parser->addTypeToList(parser->getBuilder().getAffineIntType(),
495 result->types);
Chris Lattner85ee1512018-07-25 11:15:20 -0700496}
497
Chris Lattner21e67f62018-07-06 10:46:19 -0700498const char *DimOp::verify() const {
Chris Lattner21e67f62018-07-06 10:46:19 -0700499 // Check that we have an integer index operand.
500 auto indexAttr = getAttrOfType<IntegerAttr>("index");
501 if (!indexAttr)
Chris Lattner9361fb32018-07-24 08:34:58 -0700502 return "requires an integer attribute named 'index'";
503 uint64_t index = (uint64_t)indexAttr->getValue();
Chris Lattner21e67f62018-07-06 10:46:19 -0700504
Chris Lattner9361fb32018-07-24 08:34:58 -0700505 auto *type = getOperand()->getType();
506 if (auto *tensorType = dyn_cast<RankedTensorType>(type)) {
507 if (index >= tensorType->getRank())
508 return "index is out of range";
509 } else if (auto *memrefType = dyn_cast<MemRefType>(type)) {
510 if (index >= memrefType->getRank())
511 return "index is out of range";
512
513 } else if (isa<UnrankedTensorType>(type)) {
514 // ok, assumed to be in-range.
515 } else {
516 return "requires an operand with tensor or memref type";
517 }
Chris Lattner21e67f62018-07-06 10:46:19 -0700518
519 return nullptr;
520}
521
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700522//===----------------------------------------------------------------------===//
Chris Lattner8c7feba2018-08-23 09:58:23 -0700523// ExtractElementOp
524//===----------------------------------------------------------------------===//
525
526void ExtractElementOp::build(Builder *builder, OperationState *result,
527 SSAValue *aggregate,
528 ArrayRef<SSAValue *> indices) {
529 auto *aggregateType = cast<VectorOrTensorType>(aggregate->getType());
530 result->addOperands(aggregate);
531 result->addOperands(indices);
532 result->types.push_back(aggregateType->getElementType());
533}
534
535void ExtractElementOp::print(OpAsmPrinter *p) const {
536 *p << "extract_element " << *getAggregate() << '[';
537 p->printOperands(getIndices());
538 *p << ']';
539 p->printOptionalAttrDict(getAttrs());
540 *p << " : " << *getAggregate()->getType();
541}
542
543bool ExtractElementOp::parse(OpAsmParser *parser, OperationState *result) {
544 OpAsmParser::OperandType aggregateInfo;
545 SmallVector<OpAsmParser::OperandType, 4> indexInfo;
546 VectorOrTensorType *type;
547
548 auto affineIntTy = parser->getBuilder().getAffineIntType();
549 return parser->parseOperand(aggregateInfo) ||
550 parser->parseOperandList(indexInfo, -1,
551 OpAsmParser::Delimiter::Square) ||
552 parser->parseOptionalAttributeDict(result->attributes) ||
553 parser->parseColonType(type) ||
554 parser->resolveOperand(aggregateInfo, type, result->operands) ||
555 parser->resolveOperands(indexInfo, affineIntTy, result->operands) ||
556 parser->addTypeToList(type->getElementType(), result->types);
557}
558
559const char *ExtractElementOp::verify() const {
560 if (getNumOperands() == 0)
561 return "expected an aggregate to index into";
562
563 auto *aggregateType = dyn_cast<VectorOrTensorType>(getAggregate()->getType());
564 if (!aggregateType)
565 return "first operand must be a vector or tensor";
566
567 if (getResult()->getType() != aggregateType->getElementType())
568 return "result type must match element type of aggregate";
569
570 for (auto *idx : getIndices())
571 if (!idx->getType()->isAffineInt())
572 return "index to extract_element must have 'affineint' type";
573
574 // Verify the # indices match if we have a ranked type.
575 auto aggregateRank = aggregateType->getRankIfPresent();
576 if (aggregateRank != -1 && aggregateRank != getNumOperands() - 1)
577 return "incorrect number of indices for extract_element";
578
579 return nullptr;
580}
581
582//===----------------------------------------------------------------------===//
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700583// LoadOp
584//===----------------------------------------------------------------------===//
585
Chris Lattner8c7feba2018-08-23 09:58:23 -0700586void LoadOp::build(Builder *builder, OperationState *result, SSAValue *memref,
587 ArrayRef<SSAValue *> indices) {
588 auto *memrefType = cast<MemRefType>(memref->getType());
589 result->addOperands(memref);
590 result->addOperands(indices);
591 result->types.push_back(memrefType->getElementType());
592}
593
Chris Lattner85ee1512018-07-25 11:15:20 -0700594void LoadOp::print(OpAsmPrinter *p) const {
595 *p << "load " << *getMemRef() << '[';
596 p->printOperands(getIndices());
Chris Lattner85cf26d2018-08-02 16:54:36 -0700597 *p << ']';
598 p->printOptionalAttrDict(getAttrs());
599 *p << " : " << *getMemRef()->getType();
Chris Lattner85ee1512018-07-25 11:15:20 -0700600}
601
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700602bool LoadOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -0700603 OpAsmParser::OperandType memrefInfo;
604 SmallVector<OpAsmParser::OperandType, 4> indexInfo;
605 MemRefType *type;
Chris Lattner85ee1512018-07-25 11:15:20 -0700606
607 auto affineIntTy = parser->getBuilder().getAffineIntType();
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700608 return parser->parseOperand(memrefInfo) ||
609 parser->parseOperandList(indexInfo, -1,
610 OpAsmParser::Delimiter::Square) ||
611 parser->parseOptionalAttributeDict(result->attributes) ||
612 parser->parseColonType(type) ||
613 parser->resolveOperand(memrefInfo, type, result->operands) ||
614 parser->resolveOperands(indexInfo, affineIntTy, result->operands) ||
615 parser->addTypeToList(type->getElementType(), result->types);
Chris Lattner85ee1512018-07-25 11:15:20 -0700616}
617
618const char *LoadOp::verify() const {
Chris Lattner3164ae62018-07-28 09:36:25 -0700619 if (getNumOperands() == 0)
620 return "expected a memref to load from";
Chris Lattner85ee1512018-07-25 11:15:20 -0700621
Chris Lattner3164ae62018-07-28 09:36:25 -0700622 auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType());
623 if (!memRefType)
624 return "first operand must be a memref";
MLIR Team3fa00ab2018-07-24 10:13:31 -0700625
Chris Lattner8c7feba2018-08-23 09:58:23 -0700626 if (getResult()->getType() != memRefType->getElementType())
627 return "result type must match element type of memref";
628
629 if (memRefType->getRank() != getNumOperands() - 1)
630 return "incorrect number of indices for load";
631
Chris Lattner3164ae62018-07-28 09:36:25 -0700632 for (auto *idx : getIndices())
633 if (!idx->getType()->isAffineInt())
634 return "index to load must have 'affineint' type";
MLIR Team3fa00ab2018-07-24 10:13:31 -0700635
Chris Lattner3164ae62018-07-28 09:36:25 -0700636 // TODO: Verify we have the right number of indices.
MLIR Team39a3a602018-07-24 17:43:56 -0700637
Chris Lattner3164ae62018-07-28 09:36:25 -0700638 // TODO: in MLFunction verify that the indices are parameters, IV's, or the
639 // result of an affine_apply.
MLIR Team3fa00ab2018-07-24 10:13:31 -0700640 return nullptr;
641}
642
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700643//===----------------------------------------------------------------------===//
644// ReturnOp
645//===----------------------------------------------------------------------===//
646
647bool ReturnOp::parse(OpAsmParser *parser, OperationState *result) {
648 SmallVector<OpAsmParser::OperandType, 2> opInfo;
649 SmallVector<Type *, 2> types;
Chris Lattner1aa46322018-08-21 17:55:22 -0700650 llvm::SMLoc loc;
651 return parser->getCurrentLocation(&loc) || parser->parseOperandList(opInfo) ||
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700652 (!opInfo.empty() && parser->parseColonTypeList(types)) ||
Chris Lattner1aa46322018-08-21 17:55:22 -0700653 parser->resolveOperands(opInfo, types, loc, result->operands);
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700654}
655
656void ReturnOp::print(OpAsmPrinter *p) const {
657 *p << "return";
658 if (getNumOperands() > 0) {
659 *p << " ";
660 p->printOperands(operand_begin(), operand_end());
661 *p << " : ";
662 interleave(operand_begin(), operand_end(),
663 [&](auto *e) { p->printType(e->getType()); },
664 [&]() { *p << ", "; });
665 }
666}
667
668const char *ReturnOp::verify() const {
669 // ReturnOp must be part of an ML function.
670 if (auto *stmt = dyn_cast<OperationStmt>(getOperation())) {
Tatiana Shpeisman3abd6bd2018-08-16 20:19:44 -0700671 StmtBlock *block = stmt->getBlock();
672 if (!block || !isa<MLFunction>(block) || &block->back() != stmt)
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700673 return "must be the last statement in the ML function";
674
675 // Return success. Checking that operand types match those in the function
676 // signature is performed in the ML function verifier.
677 return nullptr;
678 }
Tatiana Shpeismanb697ac12018-08-09 23:21:19 -0700679 return "cannot occur in a CFG function";
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700680}
681
682//===----------------------------------------------------------------------===//
683// StoreOp
684//===----------------------------------------------------------------------===//
685
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700686void StoreOp::print(OpAsmPrinter *p) const {
687 *p << "store " << *getValueToStore();
688 *p << ", " << *getMemRef() << '[';
689 p->printOperands(getIndices());
Chris Lattner85cf26d2018-08-02 16:54:36 -0700690 *p << ']';
691 p->printOptionalAttrDict(getAttrs());
692 *p << " : " << *getMemRef()->getType();
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700693}
694
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700695bool StoreOp::parse(OpAsmParser *parser, OperationState *result) {
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700696 OpAsmParser::OperandType storeValueInfo;
697 OpAsmParser::OperandType memrefInfo;
698 SmallVector<OpAsmParser::OperandType, 4> indexInfo;
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700699 MemRefType *memrefType;
700
701 auto affineIntTy = parser->getBuilder().getAffineIntType();
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700702 return parser->parseOperand(storeValueInfo) || parser->parseComma() ||
703 parser->parseOperand(memrefInfo) ||
704 parser->parseOperandList(indexInfo, -1,
705 OpAsmParser::Delimiter::Square) ||
706 parser->parseOptionalAttributeDict(result->attributes) ||
707 parser->parseColonType(memrefType) ||
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700708 parser->resolveOperand(storeValueInfo, memrefType->getElementType(),
709 result->operands) ||
710 parser->resolveOperand(memrefInfo, memrefType, result->operands) ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700711 parser->resolveOperands(indexInfo, affineIntTy, result->operands);
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700712}
713
714const char *StoreOp::verify() const {
715 if (getNumOperands() < 2)
716 return "expected a value to store and a memref";
717
718 // Second operand is a memref type.
719 auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType());
720 if (!memRefType)
721 return "second operand must be a memref";
722
723 // First operand must have same type as memref element type.
724 if (getValueToStore()->getType() != memRefType->getElementType())
725 return "first operand must have same type memref element type ";
726
727 if (getNumOperands() != 2 + memRefType->getRank())
728 return "store index operand count not equal to memref rank";
729
730 for (auto *idx : getIndices())
731 if (!idx->getType()->isAffineInt())
732 return "index to load must have 'affineint' type";
733
734 // TODO: Verify we have the right number of indices.
735
736 // TODO: in MLFunction verify that the indices are parameters, IV's, or the
737 // result of an affine_apply.
738 return nullptr;
739}
740
Tatiana Shpeismand9b1d862018-08-09 12:28:58 -0700741//===----------------------------------------------------------------------===//
742// Register operations.
743//===----------------------------------------------------------------------===//
744
Chris Lattnerff0d5902018-07-05 09:12:11 -0700745/// Install the standard operations in the specified operation set.
746void mlir::registerStandardOperations(OperationSet &opSet) {
Chris Lattner1aa46322018-08-21 17:55:22 -0700747 opSet.addOperations<AddFOp, AffineApplyOp, AllocOp, CallOp, CallIndirectOp,
Chris Lattner8c7feba2018-08-23 09:58:23 -0700748 ConstantOp, DeallocOp, DimOp, ExtractElementOp, LoadOp,
749 ReturnOp, StoreOp>(
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700750 /*prefix=*/"");
Chris Lattnerff0d5902018-07-05 09:12:11 -0700751}