diff --git a/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td b/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td --- a/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td +++ b/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td @@ -1035,7 +1035,7 @@ $_op->getAttrs()); for (Region &r : $_op->getRegions()) r.cloneInto(state.addRegion(), bvm); - return b.createOperation(state); + return b.create(state); }] >, InterfaceMethod< @@ -1056,7 +1056,7 @@ $_op->getAttrs()); for (Region &r : $_op->getRegions()) r.cloneInto(state.addRegion(), bvm); - return b.createOperation(state); + return b.create(state); }] >, InterfaceMethod< @@ -1077,7 +1077,7 @@ $_op->getAttrs()); for (size_t cnt = 0, e = $_op->getNumRegions(); cnt < e; ++cnt) state.addRegion(); - return b.createOperation(state); + return b.create(state); }] >, StaticInterfaceMethod< diff --git a/mlir/include/mlir/IR/Builders.h b/mlir/include/mlir/IR/Builders.h --- a/mlir/include/mlir/IR/Builders.h +++ b/mlir/include/mlir/IR/Builders.h @@ -405,7 +405,13 @@ Operation *insert(Operation *op); /// Creates an operation given the fields represented as an OperationState. - Operation *createOperation(const OperationState &state); + Operation *create(const OperationState &state); + + /// Creates an operation with the given fields. + Operation *create(Location loc, StringAttr opName, ValueRange operands, + TypeRange types, ArrayRef attributes = {}, + BlockRange successors = {}, + MutableArrayRef> regions = {}); private: /// Helper for sanity checking preconditions for create* methods below. @@ -431,7 +437,7 @@ OperationState state(location, getCheckRegisteredInfo(location.getContext())); OpTy::build(*this, state, std::forward(args)...); - auto *op = createOperation(state); + auto *op = create(state); auto result = dyn_cast(op); assert(result && "builder didn't return the right type"); return result; @@ -443,7 +449,7 @@ template void createOrFold(SmallVectorImpl &results, Location location, Args &&...args) { - // Create the operation without using 'createOperation' as we don't want to + // Create the operation without using 'create' as we don't want to // insert it yet. OperationState state(location, getCheckRegisteredInfo(location.getContext())); diff --git a/mlir/lib/Conversion/LLVMCommon/Pattern.cpp b/mlir/lib/Conversion/LLVMCommon/Pattern.cpp --- a/mlir/lib/Conversion/LLVMCommon/Pattern.cpp +++ b/mlir/lib/Conversion/LLVMCommon/Pattern.cpp @@ -11,6 +11,7 @@ #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/Dialect/LLVMIR/LLVMTypes.h" #include "mlir/IR/AffineMap.h" +#include "mlir/IR/BuiltinAttributes.h" using namespace mlir; @@ -319,11 +320,9 @@ } // Create the operation through state since we don't know its C++ type. - OperationState state(op->getLoc(), targetOp); - state.addTypes(packedType); - state.addOperands(operands); - state.addAttributes(op->getAttrs()); - Operation *newOp = rewriter.createOperation(state); + Operation *newOp = + rewriter.create(op->getLoc(), rewriter.getStringAttr(targetOp), operands, + packedType, op->getAttrs()); // If the operation produced 0 or 1 result, return them immediately. if (numResults == 0) diff --git a/mlir/lib/Conversion/LLVMCommon/VectorPattern.cpp b/mlir/lib/Conversion/LLVMCommon/VectorPattern.cpp --- a/mlir/lib/Conversion/LLVMCommon/VectorPattern.cpp +++ b/mlir/lib/Conversion/LLVMCommon/VectorPattern.cpp @@ -130,11 +130,10 @@ auto callback = [op, targetOp, &rewriter](Type llvm1DVectorTy, ValueRange operands) { - OperationState state(op->getLoc(), targetOp); - state.addTypes(llvm1DVectorTy); - state.addOperands(operands); - state.addAttributes(op->getAttrs()); - return rewriter.createOperation(state)->getResult(0); + return rewriter + .create(op->getLoc(), rewriter.getStringAttr(targetOp), operands, + llvm1DVectorTy, op->getAttrs()) + ->getResult(0); }; return handleMultidimensionalVectors(op, operands, typeConverter, callback, diff --git a/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp b/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp --- a/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp +++ b/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp @@ -1408,10 +1408,9 @@ // name that works both in scalar mode and vector mode. // TODO: Is it worth considering an Operation.clone operation which // changes the type so we can promote an Operation with less boilerplate? - OperationState vecOpState(op->getLoc(), op->getName(), vectorOperands, - vectorTypes, op->getAttrs(), /*successors=*/{}, - /*regions=*/{}); - Operation *vecOp = state.builder.createOperation(vecOpState); + Operation *vecOp = + state.builder.create(op->getLoc(), op->getName().getIdentifier(), + vectorOperands, vectorTypes, op->getAttrs()); state.registerOpVectorReplacement(op, vecOp); return vecOp; } diff --git a/mlir/lib/Dialect/Affine/Utils/Utils.cpp b/mlir/lib/Dialect/Affine/Utils/Utils.cpp --- a/mlir/lib/Dialect/Affine/Utils/Utils.cpp +++ b/mlir/lib/Dialect/Affine/Utils/Utils.cpp @@ -1242,7 +1242,7 @@ } // Create the new operation. - auto *repOp = builder.createOperation(state); + auto *repOp = builder.create(state); op->replaceAllUsesWith(repOp); op->erase(); diff --git a/mlir/lib/Dialect/Linalg/Transforms/ElementwiseToLinalg.cpp b/mlir/lib/Dialect/Linalg/Transforms/ElementwiseToLinalg.cpp --- a/mlir/lib/Dialect/Linalg/Transforms/ElementwiseToLinalg.cpp +++ b/mlir/lib/Dialect/Linalg/Transforms/ElementwiseToLinalg.cpp @@ -98,16 +98,14 @@ /*iteratorTypes=*/iteratorTypes, /*bodyBuilder=*/ [&](OpBuilder &builder, Location loc, ValueRange regionArgs) { - OperationState state(loc, op->getName()); - state.addAttributes(op->getAttrs()); - // Only take the input operands in the cloned elementwise op. - state.addOperands(regionArgs.take_front(op->getNumOperands())); auto resultTypes = llvm::to_vector<6>( llvm::map_range(op->getResultTypes(), [](Type type) { return type.cast().getElementType(); })); - state.addTypes(resultTypes); - auto *scalarOp = builder.createOperation(state); + auto *scalarOp = + builder.create(loc, op->getName().getIdentifier(), + regionArgs.take_front(op->getNumOperands()), + resultTypes, op->getAttrs()); builder.create(loc, scalarOp->getResults()); }); return success(); diff --git a/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp b/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp --- a/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp +++ b/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp @@ -299,17 +299,6 @@ return VectorizationResult{VectorizationStatus::NewOp, transposeOp}; } -/// Create a new vectorized verstion of `op` with the given operands and types. -static Operation *createVectorizedOp(OpBuilder &b, Operation *op, - ValueRange newOperands, - ArrayRef types) { - OperationState state(op->getLoc(), op->getName()); - state.addAttributes(op->getAttrs()); - state.addOperands(newOperands); - state.addTypes(types); - return b.createOperation(state); -} - /// Emit reduction operations if the shapes of the value to reduce is different /// that the result shape. static Operation *reduceIfNeeded(OpBuilder &b, LinalgOp linalgOp, Operation *op, @@ -326,7 +315,9 @@ return nullptr; SmallVector reductionMask = getReductionMask(linalgOp); Value reduce = buildMultiDimReduce(b, op, reduceVec, reductionMask); - return createVectorizedOp(b, op, {reduce, outputVec}, reduce.getType()); + return b.create(op->getLoc(), op->getName().getIdentifier(), + /*operands=*/{reduce, outputVec}, reduce.getType(), + op->getAttrs()); } /// Generic vectorization for a single operation `op`, given already vectorized @@ -420,8 +411,9 @@ // Build and return the new op. return VectorizationResult{ VectorizationStatus::NewOp, - createVectorizedOp(b, op, llvm::to_vector<4>(vectorizedOperands), - llvm::to_vector<4>(returnTypes))}; + b.create(op->getLoc(), op->getName().getIdentifier(), + llvm::to_vector<4>(vectorizedOperands), + llvm::to_vector<4>(returnTypes), op->getAttrs())}; } /// Detect whether `r` has only ConstantOp, ElementwiseMappable and YieldOp. diff --git a/mlir/lib/Dialect/MemRef/Transforms/NormalizeMemRefs.cpp b/mlir/lib/Dialect/MemRef/Transforms/NormalizeMemRefs.cpp --- a/mlir/lib/Dialect/MemRef/Transforms/NormalizeMemRefs.cpp +++ b/mlir/lib/Dialect/MemRef/Transforms/NormalizeMemRefs.cpp @@ -517,7 +517,7 @@ Region *newRegion = result.addRegion(); newRegion->takeBody(oldRegion); } - return bb.createOperation(result); + return bb.create(result); } return oldOp; } diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorDropLeadUnitDim.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorDropLeadUnitDim.cpp --- a/mlir/lib/Dialect/Vector/Transforms/VectorDropLeadUnitDim.cpp +++ b/mlir/lib/Dialect/Vector/Transforms/VectorDropLeadUnitDim.cpp @@ -368,11 +368,9 @@ newOperands.push_back(operand); } } - OperationState state(op->getLoc(), op->getName()); - state.addAttributes(op->getAttrs()); - state.addOperands(newOperands); - state.addTypes(newVecType); - Operation *newOp = rewriter.createOperation(state); + Operation *newOp = + rewriter.create(op->getLoc(), op->getName().getIdentifier(), + newOperands, newVecType, op->getAttrs()); rewriter.replaceOpWithNewOp(op, vecType, newOp->getResult(0)); return success(); diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp --- a/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp +++ b/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp @@ -1028,9 +1028,8 @@ Type castResTy = getElementTypeOrSelf(op->getResult(0)); if (auto vecTy = bcastOp.getSourceType().dyn_cast()) castResTy = VectorType::get(vecTy.getShape(), castResTy); - OperationState state(op->getLoc(), op->getName(), bcastOp.source(), - castResTy, op->getAttrs()); - auto castOp = rewriter.createOperation(state); + auto castOp = rewriter.create(op->getLoc(), op->getName().getIdentifier(), + bcastOp.source(), castResTy, op->getAttrs()); rewriter.replaceOpWithNewOp( op, op->getResult(0).getType(), castOp->getResult(0)); return success(); @@ -1068,9 +1067,8 @@ auto castResTy = transpOp.getVectorType(); castResTy = VectorType::get(castResTy.getShape(), getElementTypeOrSelf(op->getResult(0))); - OperationState state(op->getLoc(), op->getName(), transpOp.vector(), - castResTy, op->getAttrs()); - auto castOp = rewriter.createOperation(state); + auto castOp = rewriter.create(op->getLoc(), op->getName().getIdentifier(), + transpOp.vector(), castResTy, op->getAttrs()); rewriter.replaceOpWithNewOp( op, op->getResult(0).getType(), castOp->getResult(0), transpOp.getTransp()); diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorUnrollDistribute.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorUnrollDistribute.cpp --- a/mlir/lib/Dialect/Vector/Transforms/VectorUnrollDistribute.cpp +++ b/mlir/lib/Dialect/Vector/Transforms/VectorUnrollDistribute.cpp @@ -70,8 +70,8 @@ Operation *op, ArrayRef operands, ArrayRef resultTypes) { - OperationState res(loc, op->getName(), operands, resultTypes, op->getAttrs()); - return builder.createOperation(res); + return builder.create(loc, op->getName().getIdentifier(), operands, + resultTypes, op->getAttrs()); } /// Return the target shape for unrolling for the given `op`. Return llvm::None diff --git a/mlir/lib/IR/Builders.cpp b/mlir/lib/IR/Builders.cpp --- a/mlir/lib/IR/Builders.cpp +++ b/mlir/lib/IR/Builders.cpp @@ -377,10 +377,21 @@ } /// Create an operation given the fields represented as an OperationState. -Operation *OpBuilder::createOperation(const OperationState &state) { +Operation *OpBuilder::create(const OperationState &state) { return insert(Operation::create(state)); } +/// Creates an operation with the given fields. +Operation *OpBuilder::create(Location loc, StringAttr opName, + ValueRange operands, TypeRange types, + ArrayRef attributes, + BlockRange successors, + MutableArrayRef> regions) { + OperationState state(loc, opName, operands, types, attributes, successors, + regions); + return create(state); +} + /// Attempts to fold the given operation and places new results within /// 'results'. Returns success if the operation was folded, failure otherwise. /// Note: This function does not erase the operation on a successful fold. diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp --- a/mlir/lib/Parser/Parser.cpp +++ b/mlir/lib/Parser/Parser.cpp @@ -1151,7 +1151,7 @@ return nullptr; // Create the operation and try to parse a location for it. - Operation *op = opBuilder.createOperation(result); + Operation *op = opBuilder.create(result); if (parseTrailingLocationSpecifier(op)) return nullptr; return op; @@ -1756,7 +1756,7 @@ return nullptr; // Otherwise, create the operation and try to parse a location for it. - Operation *op = opBuilder.createOperation(opState); + Operation *op = opBuilder.create(opState); if (parseTrailingLocationSpecifier(op)) return nullptr; return op; diff --git a/mlir/lib/Rewrite/ByteCode.cpp b/mlir/lib/Rewrite/ByteCode.cpp --- a/mlir/lib/Rewrite/ByteCode.cpp +++ b/mlir/lib/Rewrite/ByteCode.cpp @@ -1552,7 +1552,7 @@ break; } - Operation *resultOp = rewriter.createOperation(state); + Operation *resultOp = rewriter.create(state); memory[memIndex] = resultOp; LLVM_DEBUG({ diff --git a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp --- a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp @@ -509,12 +509,12 @@ // We don't expect to see instructions in dominator order. If we haven't seen // this instruction yet, create an unknown op and remap it later. if (isa(value)) { - OperationState state(UnknownLoc::get(context), "llvm.unknown"); Type type = processType(value->getType()); if (!type) return nullptr; - state.addTypes(type); - unknownInstMap[value] = b.createOperation(state); + unknownInstMap[value] = + b.create(UnknownLoc::get(context), b.getStringAttr("llvm.unknown"), + /*operands=*/{}, type); return unknownInstMap[value]->getResult(0); } @@ -705,7 +705,7 @@ return failure(); state.addTypes(type); } - Operation *op = b.createOperation(state); + Operation *op = b.create(state); if (!inst->getType()->isVoidTy()) v = op->getResult(0); return success(); @@ -747,7 +747,7 @@ b.getI32VectorAttr(operandSegmentSizes)); } - b.createOperation(state); + b.create(state); return success(); } case llvm::Instruction::PHI: { diff --git a/mlir/lib/Target/SPIRV/Deserialization/DeserializeOps.cpp b/mlir/lib/Target/SPIRV/Deserialization/DeserializeOps.cpp --- a/mlir/lib/Target/SPIRV/Deserialization/DeserializeOps.cpp +++ b/mlir/lib/Target/SPIRV/Deserialization/DeserializeOps.cpp @@ -283,7 +283,7 @@ if (hasResult) opState.addTypes(resultTypes); opState.addAttributes(attributes); - Operation *op = opBuilder.createOperation(opState); + Operation *op = opBuilder.create(opState); if (hasResult) valueMap[valueID] = op->getResult(0); diff --git a/mlir/test/lib/Dialect/Test/TestDialect.cpp b/mlir/test/lib/Dialect/Test/TestDialect.cpp --- a/mlir/test/lib/Dialect/Test/TestDialect.cpp +++ b/mlir/test/lib/Dialect/Test/TestDialect.cpp @@ -789,7 +789,7 @@ if (failed(parseOpNameInfo)) return failure(); - StringRef innerOpName = parseOpNameInfo->getStringRef(); + StringAttr innerOpName = parseOpNameInfo->getIdentifier(); FunctionType opFntype; Optional explicitLoc; @@ -823,12 +823,8 @@ OpBuilder builder(parser.getBuilder().getContext()); builder.setInsertionPointToStart(&block); - OperationState innerOpState(opLoc, innerOpName); - innerOpState.operands.push_back(lhs); - innerOpState.operands.push_back(rhs); - innerOpState.addTypes(innerOpType); - - Operation *innerOp = builder.createOperation(innerOpState); + Operation *innerOp = + builder.create(opLoc, innerOpName, /*operands=*/{lhs, rhs}, innerOpType); // Insert a return statement in the block returning the inner-op's result. builder.create(innerOp->getLoc(), innerOp->getResults()); diff --git a/mlir/test/lib/Dialect/Test/TestPatterns.cpp b/mlir/test/lib/Dialect/Test/TestPatterns.cpp --- a/mlir/test/lib/Dialect/Test/TestPatterns.cpp +++ b/mlir/test/lib/Dialect/Test/TestPatterns.cpp @@ -168,7 +168,7 @@ OperationState state(location, OpTy::getOperationName()); // TODO: Expand to regions. OpTy::build(b, state, values, op->getAttrs()); - (void)b.createOperation(state); + (void)b.create(state); } } } @@ -295,7 +295,7 @@ // Create the region operation with an entry block containing arguments. OperationState newRegion(op->getLoc(), "test.region"); newRegion.addRegion(); - auto *regionOp = rewriter.createOperation(newRegion); + auto *regionOp = rewriter.create(newRegion); auto *entryBlock = rewriter.createBlock(®ionOp->getRegion(0)); entryBlock->addArgument(rewriter.getIntegerType(64), rewriter.getUnknownLoc()); diff --git a/mlir/test/lib/Rewrite/TestPDLByteCode.cpp b/mlir/test/lib/Rewrite/TestPDLByteCode.cpp --- a/mlir/test/lib/Rewrite/TestPDLByteCode.cpp +++ b/mlir/test/lib/Rewrite/TestPDLByteCode.cpp @@ -38,9 +38,10 @@ // Custom creator invoked from PDL. static void customCreate(ArrayRef args, PatternRewriter &rewriter, PDLResultList &results) { - results.push_back(rewriter.createOperation( + results.push_back(rewriter.create( OperationState(args[0].cast()->getLoc(), "test.success"))); } + static void customVariadicResultCreate(ArrayRef args, PatternRewriter &rewriter, PDLResultList &results) { @@ -59,7 +60,7 @@ Operation *root = args[0].cast(); OperationState successOpState(root->getLoc(), "test.success"); successOpState.addOperands(args[1].cast()); - rewriter.createOperation(successOpState); + rewriter.create(successOpState); rewriter.eraseOp(root); }