blob: ce9857adf6d3e3c93806d1aa9efb848ed3da101f [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"
19#include "mlir/IR/OperationSet.h"
Chris Lattner9361fb32018-07-24 08:34:58 -070020#include "mlir/IR/SSAValue.h"
21#include "mlir/IR/Types.h"
Chris Lattnerff0d5902018-07-05 09:12:11 -070022#include "llvm/Support/raw_ostream.h"
23using namespace mlir;
24
25void AddFOp::print(raw_ostream &os) const {
Jacques Pienaarb020c542018-07-15 00:06:54 -070026 os << "addf xx, yy : sometype";
Chris Lattnerff0d5902018-07-05 09:12:11 -070027}
28
Chris Lattner21e67f62018-07-06 10:46:19 -070029// Return an error message on failure.
30const char *AddFOp::verify() const {
31 // TODO: Check that the types of the LHS and RHS match.
32 // TODO: This should be a refinement of TwoOperands.
33 // TODO: There should also be a OneResultWhoseTypeMatchesFirstOperand.
34 return nullptr;
35}
36
Chris Lattner9361fb32018-07-24 08:34:58 -070037/// The constant op requires an attribute, and furthermore requires that it
38/// matches the return type.
39const char *ConstantOp::verify() const {
40 auto *value = getValue();
41 if (!value)
42 return "requires a 'value' attribute";
43
44 auto *type = this->getType();
45 if (isa<IntegerType>(type)) {
46 if (!isa<IntegerAttr>(value))
47 return "requires 'value' to be an integer for an integer result type";
48 return nullptr;
49 }
50
51 if (isa<FunctionType>(type)) {
52 // TODO: Verify a function attr.
53 }
54
55 return "requires a result type that aligns with the 'value' attribute";
56}
57
58/// ConstantIntOp only matches values whose result type is an IntegerType.
59bool ConstantIntOp::isClassFor(const Operation *op) {
60 return ConstantOp::isClassFor(op) &&
61 isa<IntegerType>(op->getResult(0)->getType());
62}
63
Chris Lattnerff0d5902018-07-05 09:12:11 -070064void DimOp::print(raw_ostream &os) const {
Jacques Pienaarb020c542018-07-15 00:06:54 -070065 os << "dim xxx, " << getIndex() << " : sometype";
Chris Lattnerff0d5902018-07-05 09:12:11 -070066}
67
Chris Lattner21e67f62018-07-06 10:46:19 -070068const char *DimOp::verify() const {
Chris Lattner21e67f62018-07-06 10:46:19 -070069 // Check that we have an integer index operand.
70 auto indexAttr = getAttrOfType<IntegerAttr>("index");
71 if (!indexAttr)
Chris Lattner9361fb32018-07-24 08:34:58 -070072 return "requires an integer attribute named 'index'";
73 uint64_t index = (uint64_t)indexAttr->getValue();
Chris Lattner21e67f62018-07-06 10:46:19 -070074
Chris Lattner9361fb32018-07-24 08:34:58 -070075 auto *type = getOperand()->getType();
76 if (auto *tensorType = dyn_cast<RankedTensorType>(type)) {
77 if (index >= tensorType->getRank())
78 return "index is out of range";
79 } else if (auto *memrefType = dyn_cast<MemRefType>(type)) {
80 if (index >= memrefType->getRank())
81 return "index is out of range";
82
83 } else if (isa<UnrankedTensorType>(type)) {
84 // ok, assumed to be in-range.
85 } else {
86 return "requires an operand with tensor or memref type";
87 }
Chris Lattner21e67f62018-07-06 10:46:19 -070088
89 return nullptr;
90}
91
Chris Lattnerff0d5902018-07-05 09:12:11 -070092/// Install the standard operations in the specified operation set.
93void mlir::registerStandardOperations(OperationSet &opSet) {
Chris Lattner9361fb32018-07-24 08:34:58 -070094 opSet.addOperations<AddFOp, ConstantOp, DimOp>(/*prefix=*/"");
Chris Lattnerff0d5902018-07-05 09:12:11 -070095}