blob: 71167bde38de5bb33196f81082788914bb5fa304 [file] [log] [blame]
Chris Lattner7121b802018-07-04 20:45:39 -07001//===- 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 Lattnerdf1a2fc2018-07-05 21:20:59 -070019#include "AttributeListStorage.h"
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070020#include "mlir/IR/CFGFunction.h"
Chris Lattnerac591f12018-07-22 21:02:26 -070021#include "mlir/IR/Instructions.h"
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070022#include "mlir/IR/MLFunction.h"
Chris Lattner95865062018-08-01 10:18:59 -070023#include "mlir/IR/MLIRContext.h"
Chris Lattnerac591f12018-07-22 21:02:26 -070024#include "mlir/IR/Statements.h"
Chris Lattner7121b802018-07-04 20:45:39 -070025using namespace mlir;
26
Chris Lattner1628fa02018-08-23 14:32:25 -070027Operation::Operation(bool isInstruction, Identifier name,
Chris Lattner55315d52018-07-18 19:06:45 -070028 ArrayRef<NamedAttribute> attrs, MLIRContext *context)
29 : nameAndIsInstruction(name, isInstruction) {
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -070030 this->attrs = AttributeListStorage::get(attrs, context);
31
Chris Lattner7121b802018-07-04 20:45:39 -070032#ifndef NDEBUG
33 for (auto elt : attrs)
34 assert(elt.second != nullptr && "Attributes cannot have null entries");
35#endif
36}
37
Chris Lattnerac591f12018-07-22 21:02:26 -070038Operation::~Operation() {}
39
Chris Lattner95865062018-08-01 10:18:59 -070040/// Return the context this operation is associated with.
41MLIRContext *Operation::getContext() const {
42 if (auto *inst = dyn_cast<OperationInst>(this))
43 return inst->getContext();
44 return cast<OperationStmt>(this)->getContext();
45}
46
Chris Lattner1628fa02018-08-23 14:32:25 -070047/// The source location the operation was defined or derived from. Note that
48/// it is possible for this pointer to be null.
49Attribute *Operation::getLoc() const {
50 if (auto *inst = dyn_cast<OperationInst>(this))
51 return inst->getLoc();
52 return cast<OperationStmt>(this)->getLoc();
53}
54
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070055/// Return the function this operation is defined in.
56Function *Operation::getOperationFunction() {
57 if (auto *inst = dyn_cast<OperationInst>(this))
58 return inst->getFunction();
59 return cast<OperationStmt>(this)->findFunction();
60}
61
Chris Lattnerac591f12018-07-22 21:02:26 -070062/// Return the number of operands this operation has.
63unsigned Operation::getNumOperands() const {
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070064 if (auto *inst = dyn_cast<OperationInst>(this))
Chris Lattnerac591f12018-07-22 21:02:26 -070065 return inst->getNumOperands();
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070066
67 return cast<OperationStmt>(this)->getNumOperands();
Chris Lattnerac591f12018-07-22 21:02:26 -070068}
69
70SSAValue *Operation::getOperand(unsigned idx) {
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070071 if (auto *inst = dyn_cast<OperationInst>(this))
Chris Lattnerac591f12018-07-22 21:02:26 -070072 return inst->getOperand(idx);
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070073
74 return cast<OperationStmt>(this)->getOperand(idx);
Chris Lattnerac591f12018-07-22 21:02:26 -070075}
76
Chris Lattner68a3fd02018-07-23 10:08:00 -070077void Operation::setOperand(unsigned idx, SSAValue *value) {
78 if (auto *inst = dyn_cast<OperationInst>(this)) {
79 inst->setOperand(idx, cast<CFGValue>(value));
80 } else {
81 auto *stmt = cast<OperationStmt>(this);
Tatiana Shpeisman60bf7be2018-07-26 18:09:20 -070082 stmt->setOperand(idx, cast<MLValue>(value));
Chris Lattner68a3fd02018-07-23 10:08:00 -070083 }
84}
85
Chris Lattnerac591f12018-07-22 21:02:26 -070086/// Return the number of results this operation has.
87unsigned Operation::getNumResults() const {
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070088 if (auto *inst = dyn_cast<OperationInst>(this))
Chris Lattnerac591f12018-07-22 21:02:26 -070089 return inst->getNumResults();
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070090
91 return cast<OperationStmt>(this)->getNumResults();
Chris Lattnerac591f12018-07-22 21:02:26 -070092}
93
94/// Return the indicated result.
95SSAValue *Operation::getResult(unsigned idx) {
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070096 if (auto *inst = dyn_cast<OperationInst>(this))
Chris Lattnerac591f12018-07-22 21:02:26 -070097 return inst->getResult(idx);
Chris Lattnerea5c3dc2018-08-21 08:42:19 -070098
99 return cast<OperationStmt>(this)->getResult(idx);
Chris Lattner7121b802018-07-04 20:45:39 -0700100}
101
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -0700102ArrayRef<NamedAttribute> Operation::getAttrs() const {
103 if (!attrs)
104 return {};
105 return attrs->getElements();
106}
107
Chris Lattnerff0d5902018-07-05 09:12:11 -0700108/// If an attribute exists with the specified name, change it to the new
109/// value. Otherwise, add a new attribute with the specified name/value.
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700110void Operation::setAttr(Identifier name, Attribute *value) {
Chris Lattnerff0d5902018-07-05 09:12:11 -0700111 assert(value && "attributes may never be null");
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -0700112 auto origAttrs = getAttrs();
113
114 SmallVector<NamedAttribute, 8> newAttrs(origAttrs.begin(), origAttrs.end());
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700115 auto *context = getContext();
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -0700116
Chris Lattnerff0d5902018-07-05 09:12:11 -0700117 // If we already have this attribute, replace it.
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -0700118 for (auto &elt : newAttrs)
Chris Lattnerff0d5902018-07-05 09:12:11 -0700119 if (elt.first == name) {
120 elt.second = value;
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -0700121 attrs = AttributeListStorage::get(newAttrs, context);
Chris Lattnerff0d5902018-07-05 09:12:11 -0700122 return;
123 }
124
125 // Otherwise, add it.
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -0700126 newAttrs.push_back({name, value});
127 attrs = AttributeListStorage::get(newAttrs, context);
Chris Lattnerff0d5902018-07-05 09:12:11 -0700128}
129
130/// Remove the attribute with the specified name if it exists. The return
131/// value indicates whether the attribute was present or not.
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700132auto Operation::removeAttr(Identifier name) -> RemoveResult {
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -0700133 auto origAttrs = getAttrs();
134 for (unsigned i = 0, e = origAttrs.size(); i != e; ++i) {
135 if (origAttrs[i].first == name) {
136 SmallVector<NamedAttribute, 8> newAttrs;
137 newAttrs.reserve(origAttrs.size() - 1);
138 newAttrs.append(origAttrs.begin(), origAttrs.begin() + i);
139 newAttrs.append(origAttrs.begin() + i + 1, origAttrs.end());
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700140 attrs = AttributeListStorage::get(newAttrs, getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700141 return RemoveResult::Removed;
142 }
143 }
144 return RemoveResult::NotFound;
145}
Chris Lattner95865062018-08-01 10:18:59 -0700146
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700147/// Emit a note about this operation, reporting up to any diagnostic
148/// handlers that may be listening.
149void Operation::emitNote(const Twine &message) const {
Chris Lattner1628fa02018-08-23 14:32:25 -0700150 getContext()->emitDiagnostic(getLoc(), message,
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700151 MLIRContext::DiagnosticKind::Note);
152}
153
Chris Lattner95865062018-08-01 10:18:59 -0700154/// Emit a warning about this operation, reporting up to any diagnostic
155/// handlers that may be listening.
156void Operation::emitWarning(const Twine &message) const {
Chris Lattner1628fa02018-08-23 14:32:25 -0700157 getContext()->emitDiagnostic(getLoc(), message,
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700158 MLIRContext::DiagnosticKind::Warning);
Chris Lattner95865062018-08-01 10:18:59 -0700159}
160
161/// Emit an error about fatal conditions with this operation, reporting up to
162/// any diagnostic handlers that may be listening. NOTE: This may terminate
163/// the containing application, only use when the IR is in an inconsistent
164/// state.
165void Operation::emitError(const Twine &message) const {
Chris Lattner1628fa02018-08-23 14:32:25 -0700166 getContext()->emitDiagnostic(getLoc(), message,
Chris Lattnerf7bdf952018-08-05 21:12:29 -0700167 MLIRContext::DiagnosticKind::Error);
Chris Lattner95865062018-08-01 10:18:59 -0700168}