Skip to content

Commit

Permalink
Add create method that takes equivalent of OperationState with NamedA…
Browse files Browse the repository at this point in the history
…ttributeList

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
Change-Id: Id9028a794fb1790f66ba4d400d7258196d363c73
  • Loading branch information
jpienaar authored and tensorflower-gardener committed Nov 26, 2019
1 parent 318f420 commit 3d77f8d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
9 changes: 9 additions & 0 deletions third_party/mlir/include/mlir/IR/Operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ class Operation final
/// 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; }

Expand Down
25 changes: 19 additions & 6 deletions third_party/mlir/lib/IR/Operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 3d77f8d

Please sign in to comment.