Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 1 | //===- Operation.cpp - MLIR Operation Class -------------------------------===// |
| 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/Operation.h" |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 19 | #include "AttributeListStorage.h" |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 20 | #include "mlir/IR/CFGFunction.h" |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 21 | #include "mlir/IR/Instructions.h" |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 22 | #include "mlir/IR/MLFunction.h" |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 23 | #include "mlir/IR/MLIRContext.h" |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 24 | #include "mlir/IR/Statements.h" |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 25 | using namespace mlir; |
| 26 | |
Chris Lattner | 55315d5 | 2018-07-18 19:06:45 -0700 | [diff] [blame] | 27 | Operation::Operation(Identifier name, bool isInstruction, |
| 28 | ArrayRef<NamedAttribute> attrs, MLIRContext *context) |
| 29 | : nameAndIsInstruction(name, isInstruction) { |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 30 | this->attrs = AttributeListStorage::get(attrs, context); |
| 31 | |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 32 | #ifndef NDEBUG |
| 33 | for (auto elt : attrs) |
| 34 | assert(elt.second != nullptr && "Attributes cannot have null entries"); |
| 35 | #endif |
| 36 | } |
| 37 | |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 38 | Operation::~Operation() {} |
| 39 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 40 | /// Return the context this operation is associated with. |
| 41 | MLIRContext *Operation::getContext() const { |
| 42 | if (auto *inst = dyn_cast<OperationInst>(this)) |
| 43 | return inst->getContext(); |
| 44 | return cast<OperationStmt>(this)->getContext(); |
| 45 | } |
| 46 | |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 47 | /// Return the function this operation is defined in. |
| 48 | Function *Operation::getOperationFunction() { |
| 49 | if (auto *inst = dyn_cast<OperationInst>(this)) |
| 50 | return inst->getFunction(); |
| 51 | return cast<OperationStmt>(this)->findFunction(); |
| 52 | } |
| 53 | |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 54 | /// Return the number of operands this operation has. |
| 55 | unsigned Operation::getNumOperands() const { |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 56 | if (auto *inst = dyn_cast<OperationInst>(this)) |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 57 | return inst->getNumOperands(); |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 58 | |
| 59 | return cast<OperationStmt>(this)->getNumOperands(); |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | SSAValue *Operation::getOperand(unsigned idx) { |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 63 | if (auto *inst = dyn_cast<OperationInst>(this)) |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 64 | return inst->getOperand(idx); |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 65 | |
| 66 | return cast<OperationStmt>(this)->getOperand(idx); |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 69 | void Operation::setOperand(unsigned idx, SSAValue *value) { |
| 70 | if (auto *inst = dyn_cast<OperationInst>(this)) { |
| 71 | inst->setOperand(idx, cast<CFGValue>(value)); |
| 72 | } else { |
| 73 | auto *stmt = cast<OperationStmt>(this); |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 74 | stmt->setOperand(idx, cast<MLValue>(value)); |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 78 | /// Return the number of results this operation has. |
| 79 | unsigned Operation::getNumResults() const { |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 80 | if (auto *inst = dyn_cast<OperationInst>(this)) |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 81 | return inst->getNumResults(); |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 82 | |
| 83 | return cast<OperationStmt>(this)->getNumResults(); |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /// Return the indicated result. |
| 87 | SSAValue *Operation::getResult(unsigned idx) { |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 88 | if (auto *inst = dyn_cast<OperationInst>(this)) |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 89 | return inst->getResult(idx); |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 90 | |
| 91 | return cast<OperationStmt>(this)->getResult(idx); |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 94 | ArrayRef<NamedAttribute> Operation::getAttrs() const { |
| 95 | if (!attrs) |
| 96 | return {}; |
| 97 | return attrs->getElements(); |
| 98 | } |
| 99 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 100 | /// If an attribute exists with the specified name, change it to the new |
| 101 | /// value. Otherwise, add a new attribute with the specified name/value. |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 102 | void Operation::setAttr(Identifier name, Attribute *value) { |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 103 | assert(value && "attributes may never be null"); |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 104 | auto origAttrs = getAttrs(); |
| 105 | |
| 106 | SmallVector<NamedAttribute, 8> newAttrs(origAttrs.begin(), origAttrs.end()); |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 107 | auto *context = getContext(); |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 108 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 109 | // If we already have this attribute, replace it. |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 110 | for (auto &elt : newAttrs) |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 111 | if (elt.first == name) { |
| 112 | elt.second = value; |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 113 | attrs = AttributeListStorage::get(newAttrs, context); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
| 117 | // Otherwise, add it. |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 118 | newAttrs.push_back({name, value}); |
| 119 | attrs = AttributeListStorage::get(newAttrs, context); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /// Remove the attribute with the specified name if it exists. The return |
| 123 | /// value indicates whether the attribute was present or not. |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 124 | auto Operation::removeAttr(Identifier name) -> RemoveResult { |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 125 | auto origAttrs = getAttrs(); |
| 126 | for (unsigned i = 0, e = origAttrs.size(); i != e; ++i) { |
| 127 | if (origAttrs[i].first == name) { |
| 128 | SmallVector<NamedAttribute, 8> newAttrs; |
| 129 | newAttrs.reserve(origAttrs.size() - 1); |
| 130 | newAttrs.append(origAttrs.begin(), origAttrs.begin() + i); |
| 131 | newAttrs.append(origAttrs.begin() + i + 1, origAttrs.end()); |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 132 | attrs = AttributeListStorage::get(newAttrs, getContext()); |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 133 | return RemoveResult::Removed; |
| 134 | } |
| 135 | } |
| 136 | return RemoveResult::NotFound; |
| 137 | } |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 138 | |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 139 | /// Emit a note about this operation, reporting up to any diagnostic |
| 140 | /// handlers that may be listening. |
| 141 | void Operation::emitNote(const Twine &message) const { |
| 142 | getContext()->emitDiagnostic(getAttr(":location"), message, |
| 143 | MLIRContext::DiagnosticKind::Note); |
| 144 | } |
| 145 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 146 | /// Emit a warning about this operation, reporting up to any diagnostic |
| 147 | /// handlers that may be listening. |
| 148 | void Operation::emitWarning(const Twine &message) const { |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 149 | getContext()->emitDiagnostic(getAttr(":location"), message, |
| 150 | MLIRContext::DiagnosticKind::Warning); |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /// Emit an error about fatal conditions with this operation, reporting up to |
| 154 | /// any diagnostic handlers that may be listening. NOTE: This may terminate |
| 155 | /// the containing application, only use when the IR is in an inconsistent |
| 156 | /// state. |
| 157 | void Operation::emitError(const Twine &message) const { |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 158 | getContext()->emitDiagnostic(getAttr(":location"), message, |
| 159 | MLIRContext::DiagnosticKind::Error); |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 160 | } |