Add create method that takes equivalent of OperationState with NamedAttributeList
authorJacques Pienaar <jpienaar@google.com>
Tue, 26 Nov 2019 23:30:04 +0000 (15:30 -0800)
committerA. Unique TensorFlower <gardener@tensorflow.org>
Tue, 26 Nov 2019 23:30:35 +0000 (15:30 -0800)
This method is close to creating an OperationState first and then unpacking it
but avoids creating the OperationState and takes a NamedAttributeList for
attributes rather than array of NamedAttribute (to enable reusing an already
created NamedAttributeList).

Reuse this new method via create that takes OperationState. I'll update inferReturnTypes in follow up to also take NamedAttributeList and so a build method that uses both inferReturnTypes and create can reuse the same list.

PiperOrigin-RevId: 282651642

mlir/include/mlir/IR/Operation.h
mlir/lib/IR/Operation.cpp

index ab14e66..8a7bad1 100644 (file)
@@ -70,6 +70,15 @@ public:
   /// Create a new Operation from the fields stored in `state`.
   static Operation *create(const OperationState &state);
 
+  /// Create a new Operation with the specific fields.
+  static Operation *create(Location location, OperationName name,
+                           ArrayRef<Type> resultTypes,
+                           ArrayRef<Value *> operands,
+                           const NamedAttributeList &attributes,
+                           ArrayRef<Block *> successors = {},
+                           ArrayRef<std::unique_ptr<Region>> regions = {},
+                           bool resizableOperandList = false);
+
   /// The name of an operation is the key identifier for it.
   OperationName getName() { return name; }
 
index 973b833..e5ec43c 100644 (file)
@@ -125,13 +125,26 @@ Operation *Operation::create(Location location, OperationName name,
 
 /// Create a new Operation from operation state.
 Operation *Operation::create(const OperationState &state) {
-  unsigned numRegions = state.regions.size();
-  Operation *op = create(state.location, state.name, state.types,
-                         state.operands, state.attributes, state.successors,
-                         numRegions, state.resizableOperandList);
+  return Operation::create(
+      state.location, state.name, state.types, state.operands,
+      NamedAttributeList(state.attributes).getDictionary(), state.successors,
+      state.regions, state.resizableOperandList);
+}
+
+/// Create a new Operation with the specific fields.
+Operation *Operation::create(Location location, OperationName name,
+                             ArrayRef<Type> resultTypes,
+                             ArrayRef<Value *> operands,
+                             const NamedAttributeList &attributes,
+                             ArrayRef<Block *> successors,
+                             ArrayRef<std::unique_ptr<Region>> regions,
+                             bool resizableOperandList) {
+  unsigned numRegions = regions.size();
+  Operation *op = create(location, name, resultTypes, operands, attributes,
+                         successors, numRegions, resizableOperandList);
   for (unsigned i = 0; i < numRegions; ++i)
-    if (state.regions[i])
-      op->getRegion(i).takeBody(*state.regions[i]);
+    if (regions[i])
+      op->getRegion(i).takeBody(*regions[i]);
   return op;
 }