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