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 { |
| 42 | auto *stmt = cast<OperationStmt>(this); |
| 43 | (void)stmt; |
| 44 | // TODO: Add operands to OperationStmt. |
| 45 | return 0; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | SSAValue *Operation::getOperand(unsigned idx) { |
| 50 | if (auto *inst = dyn_cast<OperationInst>(this)) { |
| 51 | return inst->getOperand(idx); |
| 52 | } else { |
| 53 | auto *stmt = cast<OperationStmt>(this); |
| 54 | (void)stmt; |
| 55 | // TODO: Add operands to OperationStmt. |
| 56 | abort(); |
| 57 | } |
| 58 | } |
| 59 | |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame^] | 60 | void Operation::setOperand(unsigned idx, SSAValue *value) { |
| 61 | if (auto *inst = dyn_cast<OperationInst>(this)) { |
| 62 | inst->setOperand(idx, cast<CFGValue>(value)); |
| 63 | } else { |
| 64 | auto *stmt = cast<OperationStmt>(this); |
| 65 | (void)stmt; |
| 66 | // TODO: Add operands to OperationStmt. |
| 67 | abort(); |
| 68 | } |
| 69 | } |
| 70 | |
Chris Lattner | ac591f1 | 2018-07-22 21:02:26 -0700 | [diff] [blame] | 71 | /// Return the number of results this operation has. |
| 72 | unsigned Operation::getNumResults() const { |
| 73 | if (auto *inst = dyn_cast<OperationInst>(this)) { |
| 74 | return inst->getNumResults(); |
| 75 | } else { |
| 76 | auto *stmt = cast<OperationStmt>(this); |
| 77 | (void)stmt; |
| 78 | // TODO: Add results to OperationStmt. |
| 79 | return 0; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Return the indicated result. |
| 84 | SSAValue *Operation::getResult(unsigned idx) { |
| 85 | if (auto *inst = dyn_cast<OperationInst>(this)) { |
| 86 | return inst->getResult(idx); |
| 87 | } else { |
| 88 | auto *stmt = cast<OperationStmt>(this); |
| 89 | (void)stmt; |
| 90 | // TODO: Add operands to OperationStmt. |
| 91 | abort(); |
| 92 | } |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 95 | ArrayRef<NamedAttribute> Operation::getAttrs() const { |
| 96 | if (!attrs) |
| 97 | return {}; |
| 98 | return attrs->getElements(); |
| 99 | } |
| 100 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 101 | /// If an attribute exists with the specified name, change it to the new |
| 102 | /// value. Otherwise, add a new attribute with the specified name/value. |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 103 | void Operation::setAttr(Identifier name, Attribute *value, |
| 104 | MLIRContext *context) { |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 105 | assert(value && "attributes may never be null"); |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 106 | auto origAttrs = getAttrs(); |
| 107 | |
| 108 | SmallVector<NamedAttribute, 8> newAttrs(origAttrs.begin(), origAttrs.end()); |
| 109 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 110 | // If we already have this attribute, replace it. |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 111 | for (auto &elt : newAttrs) |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 112 | if (elt.first == name) { |
| 113 | elt.second = value; |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 114 | attrs = AttributeListStorage::get(newAttrs, context); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | // Otherwise, add it. |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 119 | newAttrs.push_back({name, value}); |
| 120 | attrs = AttributeListStorage::get(newAttrs, context); |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /// Remove the attribute with the specified name if it exists. The return |
| 124 | /// value indicates whether the attribute was present or not. |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 125 | auto Operation::removeAttr(Identifier name, MLIRContext *context) |
| 126 | -> RemoveResult { |
| 127 | auto origAttrs = getAttrs(); |
| 128 | for (unsigned i = 0, e = origAttrs.size(); i != e; ++i) { |
| 129 | if (origAttrs[i].first == name) { |
| 130 | SmallVector<NamedAttribute, 8> newAttrs; |
| 131 | newAttrs.reserve(origAttrs.size() - 1); |
| 132 | newAttrs.append(origAttrs.begin(), origAttrs.begin() + i); |
| 133 | newAttrs.append(origAttrs.begin() + i + 1, origAttrs.end()); |
| 134 | attrs = AttributeListStorage::get(newAttrs, context); |
Chris Lattner | 7121b80 | 2018-07-04 20:45:39 -0700 | [diff] [blame] | 135 | return RemoveResult::Removed; |
| 136 | } |
| 137 | } |
| 138 | return RemoveResult::NotFound; |
| 139 | } |