diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h --- a/mlir/include/mlir/IR/Operation.h +++ b/mlir/include/mlir/IR/Operation.h @@ -74,12 +74,21 @@ private llvm::TrailingObjects { public: - /// Create a new Operation with the specific fields. + /// Create a new Operation with the specific fields. This constructor + /// populates the provided attribute list with default attributes if + /// necessary. static Operation *create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, BlockRange successors, unsigned numRegions); + /// Create a new Operation with the specific fields. This constructor uses an + /// existing attribute dictionary to avoid uniquing a list of attributes. + static Operation *create(Location location, OperationName name, + TypeRange resultTypes, ValueRange operands, + DictionaryAttr attributes, BlockRange successors, + unsigned numRegions); + /// Create a new Operation from the fields stored in `state`. static Operation *create(const OperationState &state); @@ -506,9 +515,9 @@ /// Sets default attributes on unset attributes. void populateDefaultAttrs() { - NamedAttrList attrs(getAttrDictionary()); - name.populateDefaultAttrs(attrs); - setAttrs(attrs.getDictionary(getContext())); + NamedAttrList attrs(getAttrDictionary()); + name.populateDefaultAttrs(attrs); + setAttrs(attrs.getDictionary(getContext())); } //===--------------------------------------------------------------------===// diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp --- a/mlir/lib/IR/Operation.cpp +++ b/mlir/lib/IR/Operation.cpp @@ -44,11 +44,24 @@ return op; } +/// Create a new Operation with the specific fields. +Operation *Operation::create(Location location, OperationName name, + TypeRange resultTypes, ValueRange operands, + NamedAttrList &&attributes, BlockRange successors, + unsigned numRegions) { + // Populate default attributes. + name.populateDefaultAttrs(attributes); + + return create(location, name, resultTypes, operands, + attributes.getDictionary(location.getContext()), successors, + numRegions); +} + /// Overload of create that takes an existing DictionaryAttr to avoid /// unnecessarily uniquing a list of attributes. Operation *Operation::create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, - NamedAttrList &&attributes, BlockRange successors, + DictionaryAttr attributes, BlockRange successors, unsigned numRegions) { assert(llvm::all_of(resultTypes, [](Type t) { return t; }) && "unexpected null result type"); @@ -77,13 +90,10 @@ char *mallocMem = reinterpret_cast(malloc(byteSize + prefixByteSize)); void *rawMem = mallocMem + prefixByteSize; - // Populate default attributes. - name.populateDefaultAttrs(attributes); - // Create the new Operation. - Operation *op = ::new (rawMem) Operation( - location, name, numResults, numSuccessors, numRegions, - attributes.getDictionary(location.getContext()), needsOperandStorage); + Operation *op = + ::new (rawMem) Operation(location, name, numResults, numSuccessors, + numRegions, attributes, needsOperandStorage); assert((numSuccessors == 0 || op->mightHaveTrait()) && "unexpected successors in a non-terminator operation");