Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 1 | //===- 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 Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame^] | 20 | #include "mlir/IR/SSAValue.h" |
| 21 | #include "mlir/IR/Types.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | using namespace mlir; |
| 24 | |
| 25 | void AddFOp::print(raw_ostream &os) const { |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 26 | os << "addf xx, yy : sometype"; |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 29 | // Return an error message on failure. |
| 30 | const 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 Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame^] | 37 | /// The constant op requires an attribute, and furthermore requires that it |
| 38 | /// matches the return type. |
| 39 | const 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. |
| 59 | bool ConstantIntOp::isClassFor(const Operation *op) { |
| 60 | return ConstantOp::isClassFor(op) && |
| 61 | isa<IntegerType>(op->getResult(0)->getType()); |
| 62 | } |
| 63 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 64 | void DimOp::print(raw_ostream &os) const { |
Jacques Pienaar | b020c54 | 2018-07-15 00:06:54 -0700 | [diff] [blame] | 65 | os << "dim xxx, " << getIndex() << " : sometype"; |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 68 | const char *DimOp::verify() const { |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 69 | // Check that we have an integer index operand. |
| 70 | auto indexAttr = getAttrOfType<IntegerAttr>("index"); |
| 71 | if (!indexAttr) |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame^] | 72 | return "requires an integer attribute named 'index'"; |
| 73 | uint64_t index = (uint64_t)indexAttr->getValue(); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 74 | |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame^] | 75 | 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 Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 88 | |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 92 | /// Install the standard operations in the specified operation set. |
| 93 | void mlir::registerStandardOperations(OperationSet &opSet) { |
Chris Lattner | 9361fb3 | 2018-07-24 08:34:58 -0700 | [diff] [blame^] | 94 | opSet.addOperations<AddFOp, ConstantOp, DimOp>(/*prefix=*/""); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 95 | } |