blob: b67c12d98c29801fcae319d7b1241406407097b4 [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"
Chris Lattnerff0d5902018-07-05 09:12:11 -070025#include "llvm/Support/raw_ostream.h"
26using namespace mlir;
27
MLIR Team554a8ad2018-07-30 13:08:05 -070028static void printDimAndSymbolList(Operation::const_operand_iterator begin,
29 Operation::const_operand_iterator end,
30 unsigned numDims, OpAsmPrinter *p) {
31 *p << '(';
32 p->printOperands(begin, begin + numDims);
33 *p << ')';
34
35 if (begin + numDims != end) {
36 *p << '[';
37 p->printOperands(begin + numDims, end);
38 *p << ']';
39 }
40}
41
42// Parses dimension and symbol list, and sets 'numDims' to the number of
43// dimension operands parsed.
44// Returns 'false' on success and 'true' on error.
45static bool
46parseDimAndSymbolList(OpAsmParser *parser,
MLIR Team554a8ad2018-07-30 13:08:05 -070047 SmallVector<SSAValue *, 4> &operands, unsigned &numDims) {
Chris Lattnereed6c4d2018-08-07 09:12:35 -070048 SmallVector<OpAsmParser::OperandType, 8> opInfos;
Chris Lattner85cf26d2018-08-02 16:54:36 -070049 if (parser->parseOperandList(opInfos, -1, OpAsmParser::Delimiter::Paren))
MLIR Team554a8ad2018-07-30 13:08:05 -070050 return true;
51 // Store number of dimensions for validation by caller.
52 numDims = opInfos.size();
53
54 // Parse the optional symbol operands.
55 auto *affineIntTy = parser->getBuilder().getAffineIntType();
Chris Lattner85cf26d2018-08-02 16:54:36 -070056 if (parser->parseOperandList(opInfos, -1,
57 OpAsmParser::Delimiter::OptionalSquare) ||
MLIR Team554a8ad2018-07-30 13:08:05 -070058 parser->resolveOperands(opInfos, affineIntTy, operands))
59 return true;
60 return false;
61}
62
Chris Lattnereed6c4d2018-08-07 09:12:35 -070063bool AddFOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -070064 SmallVector<OpAsmParser::OperandType, 2> ops;
65 Type *type;
Chris Lattner85cf26d2018-08-02 16:54:36 -070066 if (parser->parseOperandList(ops, 2) ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -070067 parser->parseOptionalAttributeDict(result->attributes) ||
Chris Lattner85cf26d2018-08-02 16:54:36 -070068 parser->parseColonType(type) ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -070069 parser->resolveOperands(ops, type, result->operands))
70 return true;
Chris Lattner85ee1512018-07-25 11:15:20 -070071
Chris Lattnereed6c4d2018-08-07 09:12:35 -070072 // TODO(clattner): rework parseColonType to eliminate the need for this.
73 result->types.push_back(type);
74 return false;
Chris Lattner85ee1512018-07-25 11:15:20 -070075}
76
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -070077void AddFOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -070078 *p << "addf " << *getOperand(0) << ", " << *getOperand(1);
79 p->printOptionalAttrDict(getAttrs());
80 *p << " : " << *getType();
Chris Lattnerff0d5902018-07-05 09:12:11 -070081}
82
Chris Lattnereed6c4d2018-08-07 09:12:35 -070083// TODO: Have verify functions return std::string to enable more descriptive
84// error messages.
Chris Lattner21e67f62018-07-06 10:46:19 -070085// Return an error message on failure.
86const char *AddFOp::verify() const {
87 // TODO: Check that the types of the LHS and RHS match.
88 // TODO: This should be a refinement of TwoOperands.
89 // TODO: There should also be a OneResultWhoseTypeMatchesFirstOperand.
90 return nullptr;
91}
92
Chris Lattnereed6c4d2018-08-07 09:12:35 -070093bool AffineApplyOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner3164ae62018-07-28 09:36:25 -070094 auto &builder = parser->getBuilder();
95 auto *affineIntTy = builder.getAffineIntType();
96
97 AffineMapAttr *mapAttr;
MLIR Team554a8ad2018-07-30 13:08:05 -070098 unsigned numDims;
Chris Lattnereed6c4d2018-08-07 09:12:35 -070099 if (parser->parseAttribute(mapAttr, "map", result->attributes) ||
100 parseDimAndSymbolList(parser, result->operands, numDims) ||
101 parser->parseOptionalAttributeDict(result->attributes))
102 return true;
Chris Lattner3164ae62018-07-28 09:36:25 -0700103 auto *map = mapAttr->getValue();
MLIR Team554a8ad2018-07-30 13:08:05 -0700104
Chris Lattner3164ae62018-07-28 09:36:25 -0700105 if (map->getNumDims() != numDims ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700106 numDims + map->getNumSymbols() != result->operands.size()) {
107 return parser->emitError(parser->getNameLoc(),
108 "dimension or symbol index mismatch");
Chris Lattner3164ae62018-07-28 09:36:25 -0700109 }
110
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700111 result->types.append(map->getNumResults(), affineIntTy);
112 return false;
Chris Lattner3164ae62018-07-28 09:36:25 -0700113}
114
115void AffineApplyOp::print(OpAsmPrinter *p) const {
116 auto *map = getAffineMap();
117 *p << "affine_apply " << *map;
MLIR Team554a8ad2018-07-30 13:08:05 -0700118 printDimAndSymbolList(operand_begin(), operand_end(), map->getNumDims(), p);
Chris Lattner85cf26d2018-08-02 16:54:36 -0700119 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map");
Chris Lattner3164ae62018-07-28 09:36:25 -0700120}
121
122const char *AffineApplyOp::verify() const {
123 // Check that affine map attribute was specified.
124 auto *affineMapAttr = getAttrOfType<AffineMapAttr>("map");
125 if (!affineMapAttr)
126 return "requires an affine map.";
127
128 // Check input and output dimensions match.
129 auto *map = affineMapAttr->getValue();
130
131 // Verify that operand count matches affine map dimension and symbol count.
132 if (getNumOperands() != map->getNumDims() + map->getNumSymbols())
133 return "operand count and affine map dimension and symbol count must match";
134
135 // Verify that result count matches affine map result count.
136 if (getNumResults() != map->getNumResults())
137 return "result count and affine map result count must match";
138
139 return nullptr;
140}
141
MLIR Team554a8ad2018-07-30 13:08:05 -0700142void AllocOp::print(OpAsmPrinter *p) const {
143 MemRefType *type = cast<MemRefType>(getMemRef()->getType());
144 *p << "alloc";
145 // Print dynamic dimension operands.
146 printDimAndSymbolList(operand_begin(), operand_end(),
147 type->getNumDynamicDims(), p);
Chris Lattner85cf26d2018-08-02 16:54:36 -0700148 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map");
MLIR Team554a8ad2018-07-30 13:08:05 -0700149 *p << " : " << *type;
150}
151
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700152bool AllocOp::parse(OpAsmParser *parser, OperationState *result) {
MLIR Team554a8ad2018-07-30 13:08:05 -0700153 MemRefType *type;
MLIR Team554a8ad2018-07-30 13:08:05 -0700154
Chris Lattner7d3b77c2018-07-31 16:21:36 -0700155 // Parse the dimension operands and optional symbol operands, followed by a
156 // memref type.
MLIR Team554a8ad2018-07-30 13:08:05 -0700157 unsigned numDimOperands;
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700158 if (parseDimAndSymbolList(parser, result->operands, numDimOperands) ||
159 parser->parseOptionalAttributeDict(result->attributes) ||
160 parser->parseColonType(type))
161 return true;
MLIR Team554a8ad2018-07-30 13:08:05 -0700162
163 // Check numDynamicDims against number of question marks in memref type.
164 if (numDimOperands != type->getNumDynamicDims()) {
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700165 return parser->emitError(parser->getNameLoc(),
166 "dimension operand count does not equal memref "
167 "dynamic dimension count");
MLIR Team554a8ad2018-07-30 13:08:05 -0700168 }
169
170 // Check that the number of symbol operands matches the number of symbols in
171 // the first affinemap of the memref's affine map composition.
172 // Note that a memref must specify at least one affine map in the composition.
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700173 if (result->operands.size() - numDimOperands !=
MLIR Team554a8ad2018-07-30 13:08:05 -0700174 type->getAffineMaps()[0]->getNumSymbols()) {
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700175 return parser->emitError(
176 parser->getNameLoc(),
177 "affine map symbol operand count does not equal memref affine map "
178 "symbol count");
MLIR Team554a8ad2018-07-30 13:08:05 -0700179 }
180
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700181 result->types.push_back(type);
182 return false;
MLIR Team554a8ad2018-07-30 13:08:05 -0700183}
184
185const char *AllocOp::verify() const {
186 // TODO(andydavis): Verify alloc.
187 return nullptr;
188}
189
Chris Lattnerd4964212018-08-01 10:43:18 -0700190void ConstantOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -0700191 *p << "constant " << *getValue();
192 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"value");
193 *p << " : " << *getType();
Chris Lattnerd4964212018-08-01 10:43:18 -0700194}
195
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700196bool ConstantOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattnerd4964212018-08-01 10:43:18 -0700197 Attribute *valueAttr;
198 Type *type;
Chris Lattnerd4964212018-08-01 10:43:18 -0700199
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700200 if (parser->parseAttribute(valueAttr, "value", result->attributes) ||
201 parser->parseOptionalAttributeDict(result->attributes) ||
202 parser->parseColonType(type))
203 return true;
204
205 result->types.push_back(type);
206 return false;
Chris Lattnerd4964212018-08-01 10:43:18 -0700207}
208
Chris Lattner9361fb32018-07-24 08:34:58 -0700209/// The constant op requires an attribute, and furthermore requires that it
210/// matches the return type.
211const char *ConstantOp::verify() const {
212 auto *value = getValue();
213 if (!value)
214 return "requires a 'value' attribute";
215
216 auto *type = this->getType();
Chris Lattner1ec70572018-07-24 10:41:30 -0700217 if (isa<IntegerType>(type) || type->isAffineInt()) {
Chris Lattner9361fb32018-07-24 08:34:58 -0700218 if (!isa<IntegerAttr>(value))
219 return "requires 'value' to be an integer for an integer result type";
220 return nullptr;
221 }
222
223 if (isa<FunctionType>(type)) {
224 // TODO: Verify a function attr.
225 }
226
227 return "requires a result type that aligns with the 'value' attribute";
228}
229
Chris Lattner3da86ad2018-07-26 09:58:23 -0700230/// ConstantIntOp only matches values whose result type is an IntegerType or
231/// AffineInt.
Chris Lattner9361fb32018-07-24 08:34:58 -0700232bool ConstantIntOp::isClassFor(const Operation *op) {
233 return ConstantOp::isClassFor(op) &&
Chris Lattner3da86ad2018-07-26 09:58:23 -0700234 (isa<IntegerType>(op->getResult(0)->getType()) ||
235 op->getResult(0)->getType()->isAffineInt());
Chris Lattner9361fb32018-07-24 08:34:58 -0700236}
237
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -0700238void DimOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -0700239 *p << "dim " << *getOperand() << ", " << getIndex();
240 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"index");
241 *p << " : " << *getOperand()->getType();
Chris Lattnerff0d5902018-07-05 09:12:11 -0700242}
243
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700244bool DimOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -0700245 OpAsmParser::OperandType operandInfo;
246 IntegerAttr *indexAttr;
247 Type *type;
Chris Lattner85cf26d2018-08-02 16:54:36 -0700248
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700249 // TODO(clattner): remove resolveOperand or change it to push onto the
250 // operands list.
Chris Lattner85ee1512018-07-25 11:15:20 -0700251 if (parser->parseOperand(operandInfo) || parser->parseComma() ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700252 parser->parseAttribute(indexAttr, "index", result->attributes) ||
253 parser->parseOptionalAttributeDict(result->attributes) ||
Chris Lattner85cf26d2018-08-02 16:54:36 -0700254 parser->parseColonType(type) ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700255 parser->resolveOperands(operandInfo, type, result->operands))
256 return true;
Chris Lattner85ee1512018-07-25 11:15:20 -0700257
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700258 result->types.push_back(parser->getBuilder().getAffineIntType());
259 return false;
Chris Lattner85ee1512018-07-25 11:15:20 -0700260}
261
Chris Lattner21e67f62018-07-06 10:46:19 -0700262const char *DimOp::verify() const {
Chris Lattner21e67f62018-07-06 10:46:19 -0700263 // Check that we have an integer index operand.
264 auto indexAttr = getAttrOfType<IntegerAttr>("index");
265 if (!indexAttr)
Chris Lattner9361fb32018-07-24 08:34:58 -0700266 return "requires an integer attribute named 'index'";
267 uint64_t index = (uint64_t)indexAttr->getValue();
Chris Lattner21e67f62018-07-06 10:46:19 -0700268
Chris Lattner9361fb32018-07-24 08:34:58 -0700269 auto *type = getOperand()->getType();
270 if (auto *tensorType = dyn_cast<RankedTensorType>(type)) {
271 if (index >= tensorType->getRank())
272 return "index is out of range";
273 } else if (auto *memrefType = dyn_cast<MemRefType>(type)) {
274 if (index >= memrefType->getRank())
275 return "index is out of range";
276
277 } else if (isa<UnrankedTensorType>(type)) {
278 // ok, assumed to be in-range.
279 } else {
280 return "requires an operand with tensor or memref type";
281 }
Chris Lattner21e67f62018-07-06 10:46:19 -0700282
283 return nullptr;
284}
285
Chris Lattner85ee1512018-07-25 11:15:20 -0700286void LoadOp::print(OpAsmPrinter *p) const {
287 *p << "load " << *getMemRef() << '[';
288 p->printOperands(getIndices());
Chris Lattner85cf26d2018-08-02 16:54:36 -0700289 *p << ']';
290 p->printOptionalAttrDict(getAttrs());
291 *p << " : " << *getMemRef()->getType();
Chris Lattner85ee1512018-07-25 11:15:20 -0700292}
293
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700294bool LoadOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -0700295 OpAsmParser::OperandType memrefInfo;
296 SmallVector<OpAsmParser::OperandType, 4> indexInfo;
297 MemRefType *type;
Chris Lattner85ee1512018-07-25 11:15:20 -0700298
299 auto affineIntTy = parser->getBuilder().getAffineIntType();
300 if (parser->parseOperand(memrefInfo) ||
Chris Lattner85cf26d2018-08-02 16:54:36 -0700301 parser->parseOperandList(indexInfo, -1, OpAsmParser::Delimiter::Square) ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700302 parser->parseOptionalAttributeDict(result->attributes) ||
Chris Lattner85ee1512018-07-25 11:15:20 -0700303 parser->parseColonType(type) ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700304 // TODO: use a new resolveOperand()
305 parser->resolveOperands(memrefInfo, type, result->operands) ||
306 parser->resolveOperands(indexInfo, affineIntTy, result->operands))
307 return true;
Chris Lattner85ee1512018-07-25 11:15:20 -0700308
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700309 result->types.push_back(type->getElementType());
310 return false;
Chris Lattner85ee1512018-07-25 11:15:20 -0700311}
312
313const char *LoadOp::verify() const {
Chris Lattner3164ae62018-07-28 09:36:25 -0700314 if (getNumOperands() == 0)
315 return "expected a memref to load from";
Chris Lattner85ee1512018-07-25 11:15:20 -0700316
Chris Lattner3164ae62018-07-28 09:36:25 -0700317 auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType());
318 if (!memRefType)
319 return "first operand must be a memref";
MLIR Team3fa00ab2018-07-24 10:13:31 -0700320
Chris Lattner3164ae62018-07-28 09:36:25 -0700321 for (auto *idx : getIndices())
322 if (!idx->getType()->isAffineInt())
323 return "index to load must have 'affineint' type";
MLIR Team3fa00ab2018-07-24 10:13:31 -0700324
Chris Lattner3164ae62018-07-28 09:36:25 -0700325 // TODO: Verify we have the right number of indices.
MLIR Team39a3a602018-07-24 17:43:56 -0700326
Chris Lattner3164ae62018-07-28 09:36:25 -0700327 // TODO: in MLFunction verify that the indices are parameters, IV's, or the
328 // result of an affine_apply.
MLIR Team3fa00ab2018-07-24 10:13:31 -0700329 return nullptr;
330}
331
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700332void StoreOp::print(OpAsmPrinter *p) const {
333 *p << "store " << *getValueToStore();
334 *p << ", " << *getMemRef() << '[';
335 p->printOperands(getIndices());
Chris Lattner85cf26d2018-08-02 16:54:36 -0700336 *p << ']';
337 p->printOptionalAttrDict(getAttrs());
338 *p << " : " << *getMemRef()->getType();
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700339}
340
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700341bool StoreOp::parse(OpAsmParser *parser, OperationState *result) {
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700342 OpAsmParser::OperandType storeValueInfo;
343 OpAsmParser::OperandType memrefInfo;
344 SmallVector<OpAsmParser::OperandType, 4> indexInfo;
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700345 MemRefType *memrefType;
346
347 auto affineIntTy = parser->getBuilder().getAffineIntType();
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700348 return parser->parseOperand(storeValueInfo) || parser->parseComma() ||
349 parser->parseOperand(memrefInfo) ||
350 parser->parseOperandList(indexInfo, -1,
351 OpAsmParser::Delimiter::Square) ||
352 parser->parseOptionalAttributeDict(result->attributes) ||
353 parser->parseColonType(memrefType) ||
354 parser->resolveOperands(storeValueInfo, memrefType->getElementType(),
355 result->operands) ||
356 // TODO: use a new resolveOperand().
357 parser->resolveOperands(memrefInfo, memrefType, result->operands) ||
358 parser->resolveOperands(indexInfo, affineIntTy, result->operands);
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700359}
360
361const char *StoreOp::verify() const {
362 if (getNumOperands() < 2)
363 return "expected a value to store and a memref";
364
365 // Second operand is a memref type.
366 auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType());
367 if (!memRefType)
368 return "second operand must be a memref";
369
370 // First operand must have same type as memref element type.
371 if (getValueToStore()->getType() != memRefType->getElementType())
372 return "first operand must have same type memref element type ";
373
374 if (getNumOperands() != 2 + memRefType->getRank())
375 return "store index operand count not equal to memref rank";
376
377 for (auto *idx : getIndices())
378 if (!idx->getType()->isAffineInt())
379 return "index to load must have 'affineint' type";
380
381 // TODO: Verify we have the right number of indices.
382
383 // TODO: in MLFunction verify that the indices are parameters, IV's, or the
384 // result of an affine_apply.
385 return nullptr;
386}
387
Chris Lattnerff0d5902018-07-05 09:12:11 -0700388/// Install the standard operations in the specified operation set.
389void mlir::registerStandardOperations(OperationSet &opSet) {
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700390 opSet.addOperations<AddFOp, AffineApplyOp, AllocOp, ConstantOp, DimOp, LoadOp,
391 StoreOp>(
392 /*prefix=*/"");
Chris Lattnerff0d5902018-07-05 09:12:11 -0700393}