Add parsing for attributes and attibutes on operations. Add IR representation
for attributes on operations. Split Operation out from OperationInst so it
can be shared with OperationStmt one day.
PiperOrigin-RevId: 203325366
diff --git a/lib/IR/AsmPrinter.cpp b/lib/IR/AsmPrinter.cpp
index b24eb2f..1cefc0a 100644
--- a/lib/IR/AsmPrinter.cpp
+++ b/lib/IR/AsmPrinter.cpp
@@ -22,6 +22,7 @@
#include "mlir/IR/AffineExpr.h"
#include "mlir/IR/AffineMap.h"
+#include "mlir/IR/Attributes.h"
#include "mlir/IR/CFGFunction.h"
#include "mlir/IR/MLFunction.h"
#include "mlir/IR/Module.h"
@@ -32,6 +33,14 @@
using namespace mlir;
+void Identifier::print(raw_ostream &os) const {
+ os << str();
+}
+
+void Identifier::dump() const {
+ print(llvm::errs());
+}
+
//===----------------------------------------------------------------------===//
// Function printing
//===----------------------------------------------------------------------===//
@@ -140,7 +149,18 @@
void CFGFunctionState::print(const OperationInst *inst) {
// TODO: escape name if necessary.
- os << " \"" << inst->getName().str() << "\"()\n";
+ os << " \"" << inst->getName().str() << "\"()";
+
+ auto attrs = inst->getAttrs();
+ if (!attrs.empty()) {
+ os << '{';
+ interleave(attrs, [&](NamedAttribute attr) {
+ os << attr.first << ": " << *attr.second;
+ }, [&]() { os << ", "; });
+ os << '}';
+ }
+
+ os << '\n';
}
void CFGFunctionState::print(const BranchInst *inst) {
diff --git a/lib/IR/Attributes.cpp b/lib/IR/Attributes.cpp
index 0a773f1..df23424 100644
--- a/lib/IR/Attributes.cpp
+++ b/lib/IR/Attributes.cpp
@@ -38,15 +38,11 @@
break;
case Kind::Array: {
auto elts = cast<ArrayAttr>(this)->getValue();
- if (elts.empty())
- os << "[]";
- else {
- os << "[ ";
- interleave(elts,
- [&](Attribute *attr) { attr->print(os); },
- [&]() { os << ", "; });
- os << " ]";
- }
+ os << '[';
+ interleave(elts,
+ [&](Attribute *attr) { attr->print(os); },
+ [&]() { os << ", "; });
+ os << ']';
break;
}
}
diff --git a/lib/IR/Operation.cpp b/lib/IR/Operation.cpp
new file mode 100644
index 0000000..f157169
--- /dev/null
+++ b/lib/IR/Operation.cpp
@@ -0,0 +1,40 @@
+//===- Operation.cpp - MLIR Operation Class -------------------------------===//
+//
+// Copyright 2019 The MLIR Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// =============================================================================
+
+#include "mlir/IR/Operation.h"
+using namespace mlir;
+
+Operation::Operation(Identifier name, ArrayRef<NamedAttribute> attrs)
+ : name(name), attrs(attrs.begin(), attrs.end()) {
+#ifndef NDEBUG
+ for (auto elt : attrs)
+ assert(elt.second != nullptr && "Attributes cannot have null entries");
+#endif
+}
+
+Operation::~Operation() {
+}
+
+auto Operation::removeAttr(Identifier name) -> RemoveResult {
+ for (unsigned i = 0, e = attrs.size(); i != e; ++i) {
+ if (attrs[i].first == name) {
+ attrs.erase(attrs.begin()+i);
+ return RemoveResult::Removed;
+ }
+ }
+ return RemoveResult::NotFound;
+}