blob: 9644a0086c241a859470c760ffd3ec6a95fe6d58 [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 Lattner8bdbebf2018-08-08 11:02:58 -070066 return parser->parseOperandList(ops, 2) ||
67 parser->parseOptionalAttributeDict(result->attributes) ||
68 parser->parseColonType(type) ||
69 parser->resolveOperands(ops, type, result->operands) ||
70 parser->addTypeToList(type, result->types);
Chris Lattner85ee1512018-07-25 11:15:20 -070071}
72
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -070073void AddFOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -070074 *p << "addf " << *getOperand(0) << ", " << *getOperand(1);
75 p->printOptionalAttrDict(getAttrs());
76 *p << " : " << *getType();
Chris Lattnerff0d5902018-07-05 09:12:11 -070077}
78
Chris Lattnereed6c4d2018-08-07 09:12:35 -070079// TODO: Have verify functions return std::string to enable more descriptive
80// error messages.
Chris Lattner21e67f62018-07-06 10:46:19 -070081// Return an error message on failure.
82const char *AddFOp::verify() const {
83 // TODO: Check that the types of the LHS and RHS match.
84 // TODO: This should be a refinement of TwoOperands.
85 // TODO: There should also be a OneResultWhoseTypeMatchesFirstOperand.
86 return nullptr;
87}
88
Chris Lattnereed6c4d2018-08-07 09:12:35 -070089bool AffineApplyOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner3164ae62018-07-28 09:36:25 -070090 auto &builder = parser->getBuilder();
91 auto *affineIntTy = builder.getAffineIntType();
92
93 AffineMapAttr *mapAttr;
MLIR Team554a8ad2018-07-30 13:08:05 -070094 unsigned numDims;
Chris Lattnereed6c4d2018-08-07 09:12:35 -070095 if (parser->parseAttribute(mapAttr, "map", result->attributes) ||
96 parseDimAndSymbolList(parser, result->operands, numDims) ||
97 parser->parseOptionalAttributeDict(result->attributes))
98 return true;
Chris Lattner3164ae62018-07-28 09:36:25 -070099 auto *map = mapAttr->getValue();
MLIR Team554a8ad2018-07-30 13:08:05 -0700100
Chris Lattner3164ae62018-07-28 09:36:25 -0700101 if (map->getNumDims() != numDims ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700102 numDims + map->getNumSymbols() != result->operands.size()) {
103 return parser->emitError(parser->getNameLoc(),
104 "dimension or symbol index mismatch");
Chris Lattner3164ae62018-07-28 09:36:25 -0700105 }
106
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700107 result->types.append(map->getNumResults(), affineIntTy);
108 return false;
Chris Lattner3164ae62018-07-28 09:36:25 -0700109}
110
111void AffineApplyOp::print(OpAsmPrinter *p) const {
112 auto *map = getAffineMap();
113 *p << "affine_apply " << *map;
MLIR Team554a8ad2018-07-30 13:08:05 -0700114 printDimAndSymbolList(operand_begin(), operand_end(), map->getNumDims(), p);
Chris Lattner85cf26d2018-08-02 16:54:36 -0700115 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map");
Chris Lattner3164ae62018-07-28 09:36:25 -0700116}
117
118const char *AffineApplyOp::verify() const {
119 // Check that affine map attribute was specified.
120 auto *affineMapAttr = getAttrOfType<AffineMapAttr>("map");
121 if (!affineMapAttr)
122 return "requires an affine map.";
123
124 // Check input and output dimensions match.
125 auto *map = affineMapAttr->getValue();
126
127 // Verify that operand count matches affine map dimension and symbol count.
128 if (getNumOperands() != map->getNumDims() + map->getNumSymbols())
129 return "operand count and affine map dimension and symbol count must match";
130
131 // Verify that result count matches affine map result count.
132 if (getNumResults() != map->getNumResults())
133 return "result count and affine map result count must match";
134
135 return nullptr;
136}
137
MLIR Team554a8ad2018-07-30 13:08:05 -0700138void AllocOp::print(OpAsmPrinter *p) const {
139 MemRefType *type = cast<MemRefType>(getMemRef()->getType());
140 *p << "alloc";
141 // Print dynamic dimension operands.
142 printDimAndSymbolList(operand_begin(), operand_end(),
143 type->getNumDynamicDims(), p);
Chris Lattner85cf26d2018-08-02 16:54:36 -0700144 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"map");
MLIR Team554a8ad2018-07-30 13:08:05 -0700145 *p << " : " << *type;
146}
147
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700148bool AllocOp::parse(OpAsmParser *parser, OperationState *result) {
MLIR Team554a8ad2018-07-30 13:08:05 -0700149 MemRefType *type;
MLIR Team554a8ad2018-07-30 13:08:05 -0700150
Chris Lattner7d3b77c2018-07-31 16:21:36 -0700151 // Parse the dimension operands and optional symbol operands, followed by a
152 // memref type.
MLIR Team554a8ad2018-07-30 13:08:05 -0700153 unsigned numDimOperands;
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700154 if (parseDimAndSymbolList(parser, result->operands, numDimOperands) ||
155 parser->parseOptionalAttributeDict(result->attributes) ||
156 parser->parseColonType(type))
157 return true;
MLIR Team554a8ad2018-07-30 13:08:05 -0700158
159 // Check numDynamicDims against number of question marks in memref type.
160 if (numDimOperands != type->getNumDynamicDims()) {
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700161 return parser->emitError(parser->getNameLoc(),
162 "dimension operand count does not equal memref "
163 "dynamic dimension count");
MLIR Team554a8ad2018-07-30 13:08:05 -0700164 }
165
166 // Check that the number of symbol operands matches the number of symbols in
167 // the first affinemap of the memref's affine map composition.
168 // Note that a memref must specify at least one affine map in the composition.
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700169 if (result->operands.size() - numDimOperands !=
MLIR Team554a8ad2018-07-30 13:08:05 -0700170 type->getAffineMaps()[0]->getNumSymbols()) {
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700171 return parser->emitError(
172 parser->getNameLoc(),
173 "affine map symbol operand count does not equal memref affine map "
174 "symbol count");
MLIR Team554a8ad2018-07-30 13:08:05 -0700175 }
176
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700177 result->types.push_back(type);
178 return false;
MLIR Team554a8ad2018-07-30 13:08:05 -0700179}
180
181const char *AllocOp::verify() const {
182 // TODO(andydavis): Verify alloc.
183 return nullptr;
184}
185
Chris Lattnerd4964212018-08-01 10:43:18 -0700186void ConstantOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -0700187 *p << "constant " << *getValue();
188 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"value");
189 *p << " : " << *getType();
Chris Lattnerd4964212018-08-01 10:43:18 -0700190}
191
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700192bool ConstantOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattnerd4964212018-08-01 10:43:18 -0700193 Attribute *valueAttr;
194 Type *type;
Chris Lattnerd4964212018-08-01 10:43:18 -0700195
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700196 return parser->parseAttribute(valueAttr, "value", result->attributes) ||
197 parser->parseOptionalAttributeDict(result->attributes) ||
198 parser->parseColonType(type) ||
199 parser->addTypeToList(type, result->types);
Chris Lattnerd4964212018-08-01 10:43:18 -0700200}
201
Chris Lattner9361fb32018-07-24 08:34:58 -0700202/// The constant op requires an attribute, and furthermore requires that it
203/// matches the return type.
204const char *ConstantOp::verify() const {
205 auto *value = getValue();
206 if (!value)
207 return "requires a 'value' attribute";
208
209 auto *type = this->getType();
Chris Lattner1ec70572018-07-24 10:41:30 -0700210 if (isa<IntegerType>(type) || type->isAffineInt()) {
Chris Lattner9361fb32018-07-24 08:34:58 -0700211 if (!isa<IntegerAttr>(value))
212 return "requires 'value' to be an integer for an integer result type";
213 return nullptr;
214 }
215
216 if (isa<FunctionType>(type)) {
217 // TODO: Verify a function attr.
218 }
219
220 return "requires a result type that aligns with the 'value' attribute";
221}
222
Chris Lattner992a1272018-08-07 12:02:37 -0700223/// ConstantIntOp only matches values whose result type is an IntegerType.
Chris Lattner9361fb32018-07-24 08:34:58 -0700224bool ConstantIntOp::isClassFor(const Operation *op) {
225 return ConstantOp::isClassFor(op) &&
Chris Lattner992a1272018-08-07 12:02:37 -0700226 isa<IntegerType>(op->getResult(0)->getType());
227}
228
229OperationState ConstantIntOp::build(Builder *builder, int64_t value,
230 unsigned width) {
231 OperationState result(builder->getIdentifier("constant"));
232 result.attributes.push_back(
233 {builder->getIdentifier("value"), builder->getIntegerAttr(value)});
234 result.types.push_back(builder->getIntegerType(width));
235 return result;
236}
237
238/// ConstantAffineIntOp only matches values whose result type is AffineInt.
239bool ConstantAffineIntOp::isClassFor(const Operation *op) {
240 return ConstantOp::isClassFor(op) &&
241 op->getResult(0)->getType()->isAffineInt();
242}
243
244OperationState ConstantAffineIntOp::build(Builder *builder, int64_t value) {
245 OperationState result(builder->getIdentifier("constant"));
246 result.attributes.push_back(
247 {builder->getIdentifier("value"), builder->getIntegerAttr(value)});
248 result.types.push_back(builder->getAffineIntType());
249 return result;
Chris Lattner9361fb32018-07-24 08:34:58 -0700250}
251
Chris Lattnerdd0c2ca2018-07-24 16:07:22 -0700252void DimOp::print(OpAsmPrinter *p) const {
Chris Lattner85cf26d2018-08-02 16:54:36 -0700253 *p << "dim " << *getOperand() << ", " << getIndex();
254 p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/"index");
255 *p << " : " << *getOperand()->getType();
Chris Lattnerff0d5902018-07-05 09:12:11 -0700256}
257
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700258bool DimOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -0700259 OpAsmParser::OperandType operandInfo;
260 IntegerAttr *indexAttr;
261 Type *type;
Chris Lattner85cf26d2018-08-02 16:54:36 -0700262
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700263 return parser->parseOperand(operandInfo) || parser->parseComma() ||
264 parser->parseAttribute(indexAttr, "index", result->attributes) ||
265 parser->parseOptionalAttributeDict(result->attributes) ||
266 parser->parseColonType(type) ||
267 parser->resolveOperand(operandInfo, type, result->operands) ||
268 parser->addTypeToList(parser->getBuilder().getAffineIntType(),
269 result->types);
Chris Lattner85ee1512018-07-25 11:15:20 -0700270}
271
Chris Lattner21e67f62018-07-06 10:46:19 -0700272const char *DimOp::verify() const {
Chris Lattner21e67f62018-07-06 10:46:19 -0700273 // Check that we have an integer index operand.
274 auto indexAttr = getAttrOfType<IntegerAttr>("index");
275 if (!indexAttr)
Chris Lattner9361fb32018-07-24 08:34:58 -0700276 return "requires an integer attribute named 'index'";
277 uint64_t index = (uint64_t)indexAttr->getValue();
Chris Lattner21e67f62018-07-06 10:46:19 -0700278
Chris Lattner9361fb32018-07-24 08:34:58 -0700279 auto *type = getOperand()->getType();
280 if (auto *tensorType = dyn_cast<RankedTensorType>(type)) {
281 if (index >= tensorType->getRank())
282 return "index is out of range";
283 } else if (auto *memrefType = dyn_cast<MemRefType>(type)) {
284 if (index >= memrefType->getRank())
285 return "index is out of range";
286
287 } else if (isa<UnrankedTensorType>(type)) {
288 // ok, assumed to be in-range.
289 } else {
290 return "requires an operand with tensor or memref type";
291 }
Chris Lattner21e67f62018-07-06 10:46:19 -0700292
293 return nullptr;
294}
295
Chris Lattner85ee1512018-07-25 11:15:20 -0700296void LoadOp::print(OpAsmPrinter *p) const {
297 *p << "load " << *getMemRef() << '[';
298 p->printOperands(getIndices());
Chris Lattner85cf26d2018-08-02 16:54:36 -0700299 *p << ']';
300 p->printOptionalAttrDict(getAttrs());
301 *p << " : " << *getMemRef()->getType();
Chris Lattner85ee1512018-07-25 11:15:20 -0700302}
303
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700304bool LoadOp::parse(OpAsmParser *parser, OperationState *result) {
Chris Lattner85ee1512018-07-25 11:15:20 -0700305 OpAsmParser::OperandType memrefInfo;
306 SmallVector<OpAsmParser::OperandType, 4> indexInfo;
307 MemRefType *type;
Chris Lattner85ee1512018-07-25 11:15:20 -0700308
309 auto affineIntTy = parser->getBuilder().getAffineIntType();
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700310 return parser->parseOperand(memrefInfo) ||
311 parser->parseOperandList(indexInfo, -1,
312 OpAsmParser::Delimiter::Square) ||
313 parser->parseOptionalAttributeDict(result->attributes) ||
314 parser->parseColonType(type) ||
315 parser->resolveOperand(memrefInfo, type, result->operands) ||
316 parser->resolveOperands(indexInfo, affineIntTy, result->operands) ||
317 parser->addTypeToList(type->getElementType(), result->types);
Chris Lattner85ee1512018-07-25 11:15:20 -0700318}
319
320const char *LoadOp::verify() const {
Chris Lattner3164ae62018-07-28 09:36:25 -0700321 if (getNumOperands() == 0)
322 return "expected a memref to load from";
Chris Lattner85ee1512018-07-25 11:15:20 -0700323
Chris Lattner3164ae62018-07-28 09:36:25 -0700324 auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType());
325 if (!memRefType)
326 return "first operand must be a memref";
MLIR Team3fa00ab2018-07-24 10:13:31 -0700327
Chris Lattner3164ae62018-07-28 09:36:25 -0700328 for (auto *idx : getIndices())
329 if (!idx->getType()->isAffineInt())
330 return "index to load must have 'affineint' type";
MLIR Team3fa00ab2018-07-24 10:13:31 -0700331
Chris Lattner3164ae62018-07-28 09:36:25 -0700332 // TODO: Verify we have the right number of indices.
MLIR Team39a3a602018-07-24 17:43:56 -0700333
Chris Lattner3164ae62018-07-28 09:36:25 -0700334 // TODO: in MLFunction verify that the indices are parameters, IV's, or the
335 // result of an affine_apply.
MLIR Team3fa00ab2018-07-24 10:13:31 -0700336 return nullptr;
337}
338
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700339void StoreOp::print(OpAsmPrinter *p) const {
340 *p << "store " << *getValueToStore();
341 *p << ", " << *getMemRef() << '[';
342 p->printOperands(getIndices());
Chris Lattner85cf26d2018-08-02 16:54:36 -0700343 *p << ']';
344 p->printOptionalAttrDict(getAttrs());
345 *p << " : " << *getMemRef()->getType();
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700346}
347
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700348bool StoreOp::parse(OpAsmParser *parser, OperationState *result) {
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700349 OpAsmParser::OperandType storeValueInfo;
350 OpAsmParser::OperandType memrefInfo;
351 SmallVector<OpAsmParser::OperandType, 4> indexInfo;
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700352 MemRefType *memrefType;
353
354 auto affineIntTy = parser->getBuilder().getAffineIntType();
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700355 return parser->parseOperand(storeValueInfo) || parser->parseComma() ||
356 parser->parseOperand(memrefInfo) ||
357 parser->parseOperandList(indexInfo, -1,
358 OpAsmParser::Delimiter::Square) ||
359 parser->parseOptionalAttributeDict(result->attributes) ||
360 parser->parseColonType(memrefType) ||
Chris Lattner8bdbebf2018-08-08 11:02:58 -0700361 parser->resolveOperand(storeValueInfo, memrefType->getElementType(),
362 result->operands) ||
363 parser->resolveOperand(memrefInfo, memrefType, result->operands) ||
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700364 parser->resolveOperands(indexInfo, affineIntTy, result->operands);
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700365}
366
367const char *StoreOp::verify() const {
368 if (getNumOperands() < 2)
369 return "expected a value to store and a memref";
370
371 // Second operand is a memref type.
372 auto *memRefType = dyn_cast<MemRefType>(getMemRef()->getType());
373 if (!memRefType)
374 return "second operand must be a memref";
375
376 // First operand must have same type as memref element type.
377 if (getValueToStore()->getType() != memRefType->getElementType())
378 return "first operand must have same type memref element type ";
379
380 if (getNumOperands() != 2 + memRefType->getRank())
381 return "store index operand count not equal to memref rank";
382
383 for (auto *idx : getIndices())
384 if (!idx->getType()->isAffineInt())
385 return "index to load must have 'affineint' type";
386
387 // TODO: Verify we have the right number of indices.
388
389 // TODO: in MLFunction verify that the indices are parameters, IV's, or the
390 // result of an affine_apply.
391 return nullptr;
392}
393
Chris Lattnerff0d5902018-07-05 09:12:11 -0700394/// Install the standard operations in the specified operation set.
395void mlir::registerStandardOperations(OperationSet &opSet) {
MLIR Teamc5efe7e2018-07-31 14:11:38 -0700396 opSet.addOperations<AddFOp, AffineApplyOp, AllocOp, ConstantOp, DimOp, LoadOp,
397 StoreOp>(
398 /*prefix=*/"");
Chris Lattnerff0d5902018-07-05 09:12:11 -0700399}