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" |
| 21 | #include "mlir/IR/Statements.h" |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 22 | using namespace mlir; |
| 23 | |
Chris Lattner | 55315d5 | 2018-07-18 19:06:45 -0700 | [diff] [blame] | 24 | Operation::Operation(Identifier name, bool isInstruction, |
| 25 | ArrayRef<NamedAttribute> attrs, MLIRContext *context) |
| 26 | : nameAndIsInstruction(name, isInstruction) { |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 27 | this->attrs = AttributeListStorage::get(attrs, context); |
| 28 | |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 29 | #ifndef NDEBUG |
| 30 | for (auto elt : attrs) |
| 31 | assert(elt.second != nullptr && "Attributes cannot have null entries"); |
| 32 | #endif |
| 33 | } |
| 34 | |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 35 | Operation::~Operation() {} |
| 36 | |
| 37 | /// Return the number of operands this operation has. |
| 38 | unsigned Operation::getNumOperands() const { |
| 39 | if (auto *inst = dyn_cast<OperationInst>(this)) { |
| 40 | return inst->getNumOperands(); |
| 41 | } else { |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 42 | return cast<OperationStmt>(this)->getNumOperands(); |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | SSAValue *Operation::getOperand(unsigned idx) { |
| 47 | if (auto *inst = dyn_cast<OperationInst>(this)) { |
| 48 | return inst->getOperand(idx); |
| 49 | } else { |
| 50 | auto *stmt = cast<OperationStmt>(this); |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 51 | return stmt->getOperand(idx); |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 55 | void Operation::setOperand(unsigned idx, SSAValue *value) { |
| 56 | if (auto *inst = dyn_cast<OperationInst>(this)) { |
| 57 | inst->setOperand(idx, cast<CFGValue>(value)); |
| 58 | } else { |
| 59 | auto *stmt = cast<OperationStmt>(this); |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 60 | stmt->setOperand(idx, cast<MLValue>(value)); |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 64 | /// Return the number of results this operation has. |
| 65 | unsigned Operation::getNumResults() const { |
| 66 | if (auto *inst = dyn_cast<OperationInst>(this)) { |
| 67 | return inst->getNumResults(); |
| 68 | } else { |
| 69 | auto *stmt = cast<OperationStmt>(this); |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 70 | return stmt->getNumResults(); |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | /// Return the indicated result. |
| 75 | SSAValue *Operation::getResult(unsigned idx) { |
| 76 | if (auto *inst = dyn_cast<OperationInst>(this)) { |
| 77 | return inst->getResult(idx); |
| 78 | } else { |
| 79 | auto *stmt = cast<OperationStmt>(this); |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 80 | return stmt->getResult(idx); |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 81 | } |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 84 | ArrayRef<NamedAttribute> Operation::getAttrs() const { |
| 85 | if (!attrs) |
| 86 | return {}; |
| 87 | return attrs->getElements(); |
| 88 | } |
| 89 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 90 | /// If an attribute exists with the specified name, change it to the new |
| 91 | /// value. Otherwise, add a new attribute with the specified name/value. |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 92 | void Operation::setAttr(Identifier name, Attribute *value, |
| 93 | MLIRContext *context) { |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 94 | assert(value && "attributes may never be null"); |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 95 | auto origAttrs = getAttrs(); |
| 96 | |
| 97 | SmallVector<NamedAttribute, 8> newAttrs(origAttrs.begin(), origAttrs.end()); |
| 98 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 99 | // If we already have this attribute, replace it. |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 100 | for (auto &elt : newAttrs) |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 101 | if (elt.first == name) { |
| 102 | elt.second = value; |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 103 | attrs = AttributeListStorage::get(newAttrs, context); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
| 107 | // Otherwise, add it. |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 108 | newAttrs.push_back({name, value}); |
| 109 | attrs = AttributeListStorage::get(newAttrs, context); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /// Remove the attribute with the specified name if it exists. The return |
| 113 | /// value indicates whether the attribute was present or not. |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 114 | auto Operation::removeAttr(Identifier name, MLIRContext *context) |
| 115 | -> RemoveResult { |
| 116 | auto origAttrs = getAttrs(); |
| 117 | for (unsigned i = 0, e = origAttrs.size(); i != e; ++i) { |
| 118 | if (origAttrs[i].first == name) { |
| 119 | SmallVector<NamedAttribute, 8> newAttrs; |
| 120 | newAttrs.reserve(origAttrs.size() - 1); |
| 121 | newAttrs.append(origAttrs.begin(), origAttrs.begin() + i); |
| 122 | newAttrs.append(origAttrs.begin() + i + 1, origAttrs.end()); |
| 123 | attrs = AttributeListStorage::get(newAttrs, context); |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 124 | return RemoveResult::Removed; |
| 125 | } |
| 126 | } |
| 127 | return RemoveResult::NotFound; |
| 128 | } |