Skip to content

[mlir][NFC] update include create APIs (3/n) #149687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ void replaceOpWithBufferizedValues(RewriterBase &rewriter, Operation *op,
template <typename OpTy, typename... Args>
OpTy replaceOpWithNewBufferizedOp(RewriterBase &rewriter, Operation *op,
Args &&...args) {
auto newOp = rewriter.create<OpTy>(op->getLoc(), std::forward<Args>(args)...);
auto newOp =
OpTy::create(rewriter, op->getLoc(), std::forward<Args>(args)...);
replaceOpWithBufferizedValues(rewriter, op, newOp->getResults());
return newOp;
}
Expand Down
4 changes: 2 additions & 2 deletions mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ struct BufferResultsToOutParamsOpts {
/// Default memref.alloc is used
AllocationFn allocationFn = [](OpBuilder &builder, Location loc,
MemRefType type) {
return builder.create<memref::AllocOp>(loc, type).getResult();
return memref::AllocOp::create(builder, loc, type).getResult();
};

/// Memcpy function; used to create a copy between two memrefs.
/// Default memref.copy is used.
MemCpyFn memCpyFn = [](OpBuilder &builder, Location loc, Value from,
Value to) {
builder.create<memref::CopyOp>(loc, from, to);
memref::CopyOp::create(builder, loc, from, to);
return success();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def BasicPtxBuilderOpInterface : OpInterface<"BasicPtxBuilderInterface"> {
/*methodBody=*/"",
/*defaultImpl=*/ [{
mlir::Operation* op = $_op;
return rewriter.create<LLVM::ConstantOp>(
return LLVM::ConstantOp::create(rewriter,
op->getLoc(), rewriter.getIntegerType(32), val);
}]
>,
Expand Down
4 changes: 2 additions & 2 deletions mlir/include/mlir/Dialect/Linalg/IR/LinalgStructuredOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def TransposeOp : LinalgStructuredBase_Op<"transpose", [
static void regionBuilder(mlir::ImplicitLocOpBuilder &b, mlir::Block &block,
mlir::ArrayRef<mlir::NamedAttribute>, function_ref<InFlightDiagnostic()> emitError) {
OpBuilder::InsertionGuard guard(b);
b.create<linalg::YieldOp>(b.getLoc(), block.getArgument(0));
linalg::YieldOp::create(b, b.getLoc(), block.getArgument(0));
}

static std::function<void(mlir::ImplicitLocOpBuilder &, mlir::Block &,
Expand Down Expand Up @@ -528,7 +528,7 @@ def BroadcastOp : LinalgStructuredBase_Op<"broadcast", [
mlir::ArrayRef<mlir::NamedAttribute>,
function_ref<InFlightDiagnostic()> emitError) {
OpBuilder::InsertionGuard guard(b);
b.create<linalg::YieldOp>(b.getLoc(), block.getArgument(0));
linalg::YieldOp::create(b, b.getLoc(), block.getArgument(0));
}

static std::function<void(mlir::ImplicitLocOpBuilder &, mlir::Block &,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class SparseTensorType {
/// ambiguity whenever passing a `SparseTensorType` directly to a
/// function which is overloaded to accept either `Type` or `TypeRange`.
/// In particular, this includes `RewriterBase::replaceOpWithNewOp<OpTy>`
/// and `OpBuilder::create<OpTy>` whenever the `OpTy::build` is overloaded
/// and `OpTy::create` whenever the `OpTy::build` is overloaded
/// thus. This happens because the `TypeRange<T>(T&&)` ctor is implicit
/// as well, and there's no SFINAE we can add to this method that would
/// block subsequent application of that ctor. The only way to fix the
Expand Down
4 changes: 2 additions & 2 deletions mlir/include/mlir/Dialect/Tosa/Utils/ConversionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ checkHasDynamicBatchDims(PatternRewriter &rewriter, Op op,
}

dynamicDims.push_back(
rewriter.create<tensor::DimOp>(op->getLoc(), params[0], 0));
tensor::DimOp::create(rewriter, op->getLoc(), params[0], 0));
return dynamicDims;
}

Expand All @@ -91,7 +91,7 @@ namespace {
template <typename TosaOp, typename... Args>
TosaOp createOpAndInferShape(ImplicitLocOpBuilder &builder, Type resultTy,
Args &&...args) {
auto op = builder.create<TosaOp>(resultTy, args...);
auto op = TosaOp::create(builder, resultTy, args...);

InferShapedTypeOpInterface shapeInterface =
dyn_cast<InferShapedTypeOpInterface>(op.getOperation());
Expand Down
2 changes: 1 addition & 1 deletion mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Value getConstTensorInt(OpBuilder &builder, Location loc,
mlir::RankedTensorType const_type =
RankedTensorType::get({count}, element_type);
mlir::DenseElementsAttr const_attr = DenseElementsAttr::get(const_type, vec);
auto const_op = builder.create<tosa::ConstOp>(loc, const_type, const_attr);
auto const_op = tosa::ConstOp::create(builder, loc, const_type, const_attr);
return const_op.getResult();
}

Expand Down
8 changes: 4 additions & 4 deletions mlir/include/mlir/Dialect/Utils/ReshapeOpsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,11 @@ struct ComposeCollapseOfExpandOp : public OpRewritePattern<CollapseOpTy> {
// the first dynamic size.
Value result = dynamicSizes[0];
for (Value v : llvm::drop_begin(dynamicSizes))
result = rewriter.create<arith::MulIOp>(loc, result, v);
result = arith::MulIOp::create(rewriter, loc, result, v);
if (numStaticElems != 1) {
result = rewriter.create<arith::MulIOp>(
loc, result,
rewriter.create<arith::ConstantIndexOp>(loc, numStaticElems));
result = arith::MulIOp::create(
rewriter, loc, result,
arith::ConstantIndexOp::create(rewriter, loc, numStaticElems));
}
newOutputShape.push_back(result);
}
Expand Down
7 changes: 4 additions & 3 deletions mlir/include/mlir/Dialect/Vector/Utils/VectorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ inline auto makeVscaleConstantBuilder(PatternRewriter &rewriter, Location loc) {
Value vscale = nullptr;
return [loc, vscale, &rewriter](int64_t multiplier) mutable {
if (!vscale)
vscale = rewriter.create<vector::VectorScaleOp>(loc);
return rewriter.create<arith::MulIOp>(
loc, vscale, rewriter.create<arith::ConstantIndexOp>(loc, multiplier));
vscale = vector::VectorScaleOp::create(rewriter, loc);
return arith::MulIOp::create(
rewriter, loc, vscale,
arith::ConstantIndexOp::create(rewriter, loc, multiplier));
};
}

Expand Down
4 changes: 2 additions & 2 deletions mlir/include/mlir/IR/Builders.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ class OpBuilder : public Builder {
template <typename OpTy, typename... Args>
std::enable_if_t<OpTy::template hasTrait<OpTrait::ZeroResults>(), OpTy>
createOrFold(Location location, Args &&...args) {
auto op = create<OpTy>(location, std::forward<Args>(args)...);
auto op = OpTy::create(*this, location, std::forward<Args>(args)...);
SmallVector<Value, 0> unused;
(void)tryFold(op.getOperation(), unused);

Expand Down Expand Up @@ -662,7 +662,7 @@ class ImplicitLocOpBuilder : public mlir::OpBuilder {
/// location.
template <typename OpTy, typename... Args>
OpTy create(Args &&...args) {
return OpBuilder::create<OpTy>(curLoc, std::forward<Args>(args)...);
return OpTy::create(*this, curLoc, std::forward<Args>(args)...);
}

/// Create an operation of specific op type at the current insertion point,
Expand Down
4 changes: 3 additions & 1 deletion mlir/include/mlir/IR/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,9 @@ class RewriterBase : public OpBuilder {
/// ops must match. The original op is erased.
template <typename OpTy, typename... Args>
OpTy replaceOpWithNewOp(Operation *op, Args &&...args) {
auto newOp = create<OpTy>(op->getLoc(), std::forward<Args>(args)...);
auto builder = static_cast<OpBuilder *>(this);
auto newOp =
OpTy::create(*builder, op->getLoc(), std::forward<Args>(args)...);
replaceOp(op, newOp.getOperation());
return newOp;
}
Expand Down
4 changes: 2 additions & 2 deletions mlir/include/mlir/Interfaces/ViewLikeInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ class OpWithOffsetSizesAndStridesConstantArgumentFolder final

// Create the new op in canonical form.
auto newOp =
rewriter.create<OpType>(op.getLoc(), resultType, op.getSource(),
mixedOffsets, mixedSizes, mixedStrides);
OpType::create(rewriter, op.getLoc(), resultType, op.getSource(),
mixedOffsets, mixedSizes, mixedStrides);
CastOpFunc()(rewriter, op, newOp);

return success();
Expand Down
2 changes: 1 addition & 1 deletion mlir/include/mlir/Parser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ inline OwningOpRef<ContainerOpT> constructContainerOpForParserIfNecessary(
"block that has an implicit terminator or does not require one");

OpBuilder builder(context);
ContainerOpT op = builder.create<ContainerOpT>(sourceFileLoc);
ContainerOpT op = ContainerOpT::create(builder, sourceFileLoc);
OwningOpRef<ContainerOpT> opRef(op);
assert(op->getNumRegions() == 1 &&
llvm::hasSingleElement(op->getRegion(0)) &&
Expand Down
27 changes: 26 additions & 1 deletion mlir/lib/Target/IRDLToCpp/IRDLToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ static void generateOpBuilderDeclarations(irdl::detail::dictionary &dict,
stream << llvm::formatv(
R"(static void build(::mlir::OpBuilder &opBuilder, ::mlir::OperationState &opState, {0} {1} ::llvm::ArrayRef<::mlir::NamedAttribute> attributes = {{});)",
resultParams, operandParams);
stream << "\n";
stream << llvm::formatv(
R"(static {0} create(::mlir::OpBuilder &opBuilder, ::mlir::Location location, {1} {2} ::llvm::ArrayRef<::mlir::NamedAttribute> attributes = {{});)",
opStrings.opCppName, resultParams, operandParams);
stream << "\n";
stream << llvm::formatv(
R"(static {0} create(::mlir::ImplicitLocOpBuilder &opBuilder, {1} {2} ::llvm::ArrayRef<::mlir::NamedAttribute> attributes = {{});)",
opStrings.opCppName, resultParams, operandParams);
stream << "\n";
Comment on lines +229 to +237
Copy link
Contributor Author

@makslevental makslevental Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Moxinilian @math-fehr can you review the changes here and below (enabling create support for irdl-to-cpp).

dict["OP_BUILD_DECLS"] = buildDecls;
}

Expand Down Expand Up @@ -339,9 +348,25 @@ void {0}::build(::mlir::OpBuilder &opBuilder, ::mlir::OperationState &opState, {
{3}
{4}
}

{0} {0}::create(::mlir::OpBuilder &opBuilder, ::mlir::Location location, {1} {2} ::llvm::ArrayRef<::mlir::NamedAttribute> attributes) {{
::mlir::OperationState __state__(location, getOperationName());
build(opBuilder, __state__, {5} {6} attributes);
auto __res__ = ::llvm::dyn_cast<{0}>(opBuilder.create(__state__));
assert(__res__ && "builder didn't return the right type");
return __res__;
}

{0} {0}::create(::mlir::ImplicitLocOpBuilder &opBuilder, {1} {2} ::llvm::ArrayRef<::mlir::NamedAttribute> attributes) {{
return create(opBuilder, opBuilder.getLoc(), {5} {6} attributes);
}
)",
opStrings.opCppName, std::move(resultTypes), std::move(operandTypes),
std::move(operandAdder), std::move(resultAdder));
std::move(operandAdder), std::move(resultAdder),
llvm::join(opStrings.opResultNames, ",") +
(!opStrings.opResultNames.empty() ? "," : ""),
llvm::join(opStrings.opOperandNames, ",") +
(!opStrings.opOperandNames.empty() ? "," : ""));

dict["OP_BUILD_DEFS"] = buildDefinition;

Expand Down
11 changes: 11 additions & 0 deletions mlir/lib/Target/IRDLToCpp/Templates/PerOperationDecl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ public:
::mlir::TypeRange resultTypes,
::mlir::ValueRange operands,
::llvm::ArrayRef<::mlir::NamedAttribute> attributes = {});

static __OP_CPP_NAME__ create(::mlir::OpBuilder &odsBuilder,
::mlir::Location location,
::mlir::TypeRange resultTypes,
::mlir::ValueRange operands,
::llvm::ArrayRef<::mlir::NamedAttribute> attributes = {});

static __OP_CPP_NAME__ create(::mlir::ImplicitLocOpBuilder &odsBuilder,
::mlir::TypeRange resultTypes,
::mlir::ValueRange operands,
::llvm::ArrayRef<::mlir::NamedAttribute> attributes = {});
};


Expand Down
23 changes: 23 additions & 0 deletions mlir/lib/Target/IRDLToCpp/Templates/PerOperationDef.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ void __OP_CPP_NAME__::build(::mlir::OpBuilder &odsBuilder,
odsState.addTypes(resultTypes);
}

__OP_CPP_NAME__
__OP_CPP_NAME__::create(::mlir::OpBuilder &odsBuilder,
::mlir::Location location,
::mlir::TypeRange resultTypes,
::mlir::ValueRange operands,
::llvm::ArrayRef<::mlir::NamedAttribute> attributes)
{
::mlir::OperationState state(location, getOperationName());
build(odsBuilder, state, resultTypes, operands, attributes);
auto res = ::llvm::dyn_cast<__OP_CPP_NAME__>(odsBuilder.create(state));
assert(res && "builder didn't return the right type");
return res;
}

__OP_CPP_NAME__
__OP_CPP_NAME__::create(::mlir::ImplicitLocOpBuilder &odsBuilder,
::mlir::TypeRange resultTypes,
::mlir::ValueRange operands,
::llvm::ArrayRef<::mlir::NamedAttribute> attributes)
{
return create(odsBuilder, odsBuilder.getLoc(), resultTypes, operands, attributes);
}


__NAMESPACE_CLOSE__

Expand Down
Loading