Skip to content

[mlir][NFC] update mlir/Dialect create APIs (16/n) #149922

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 1 commit 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
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ Operation *complex::ComplexDialect::materializeConstant(OpBuilder &builder,
Type type,
Location loc) {
if (complex::ConstantOp::isBuildableWith(value, type)) {
return builder.create<complex::ConstantOp>(loc, type,
llvm::cast<ArrayAttr>(value));
return complex::ConstantOp::create(builder, loc, type,
llvm::cast<ArrayAttr>(value));
}
return arith::ConstantOp::materialize(builder, value, type, loc);
}
Expand Down
13 changes: 7 additions & 6 deletions mlir/lib/Dialect/ControlFlow/IR/ControlFlowOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ struct SimplifyCondBranchIdenticalSuccessors
if (std::get<0>(it) == std::get<1>(it))
mergedOperands.push_back(std::get<0>(it));
else
mergedOperands.push_back(rewriter.create<arith::SelectOp>(
condbr.getLoc(), condition, std::get<0>(it), std::get<1>(it)));
mergedOperands.push_back(
arith::SelectOp::create(rewriter, condbr.getLoc(), condition,
std::get<0>(it), std::get<1>(it)));
}

rewriter.replaceOpWithNewOp<BranchOp>(condbr, trueDest, mergedOperands);
Expand Down Expand Up @@ -412,8 +413,8 @@ struct CondBranchTruthPropagation : public OpRewritePattern<CondBranchOp> {
replaced = true;

if (!constantTrue)
constantTrue = rewriter.create<arith::ConstantOp>(
condbr.getLoc(), ty, rewriter.getBoolAttr(true));
constantTrue = arith::ConstantOp::create(
rewriter, condbr.getLoc(), ty, rewriter.getBoolAttr(true));

rewriter.modifyOpInPlace(use.getOwner(),
[&] { use.set(constantTrue); });
Expand All @@ -427,8 +428,8 @@ struct CondBranchTruthPropagation : public OpRewritePattern<CondBranchOp> {
replaced = true;

if (!constantFalse)
constantFalse = rewriter.create<arith::ConstantOp>(
condbr.getLoc(), ty, rewriter.getBoolAttr(false));
constantFalse = arith::ConstantOp::create(
rewriter, condbr.getLoc(), ty, rewriter.getBoolAttr(false));

rewriter.modifyOpInPlace(use.getOwner(),
[&] { use.set(constantFalse); });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ struct CondBranchOpInterface
destOperands.getAsOperandRange(), toRetain);
SmallVector<Value> adaptedConditions(
llvm::map_range(conditions, conditionModifier));
auto deallocOp = builder.create<bufferization::DeallocOp>(
condBr.getLoc(), memrefs, adaptedConditions, toRetain);
auto deallocOp = bufferization::DeallocOp::create(
builder, condBr.getLoc(), memrefs, adaptedConditions, toRetain);
state.resetOwnerships(deallocOp.getRetained(), condBr->getBlock());
for (auto [retained, ownership] : llvm::zip(
deallocOp.getRetained(), deallocOp.getUpdatedConditions())) {
Expand All @@ -115,18 +115,19 @@ struct CondBranchOpInterface
DeallocOp thenTakenDeallocOp = insertDeallocForBranch(
condBr.getTrueDest(), condBr.getTrueDestOperandsMutable(),
[&](Value cond) {
return builder.create<arith::AndIOp>(condBr.getLoc(), cond,
condBr.getCondition());
return arith::AndIOp::create(builder, condBr.getLoc(), cond,
condBr.getCondition());
},
thenMapping);
DeallocOp elseTakenDeallocOp = insertDeallocForBranch(
condBr.getFalseDest(), condBr.getFalseDestOperandsMutable(),
[&](Value cond) {
Value trueVal = builder.create<arith::ConstantOp>(
condBr.getLoc(), builder.getBoolAttr(true));
Value negation = builder.create<arith::XOrIOp>(
condBr.getLoc(), trueVal, condBr.getCondition());
return builder.create<arith::AndIOp>(condBr.getLoc(), cond, negation);
Value trueVal = arith::ConstantOp::create(builder, condBr.getLoc(),
builder.getBoolAttr(true));
Value negation = arith::XOrIOp::create(
builder, condBr.getLoc(), trueVal, condBr.getCondition());
return arith::AndIOp::create(builder, condBr.getLoc(), cond,
negation);
},
elseMapping);

Expand All @@ -143,9 +144,9 @@ struct CondBranchOpInterface

for (Value retained : commonValues) {
state.resetOwnerships(retained, condBr->getBlock());
Value combinedOwnership = builder.create<arith::SelectOp>(
condBr.getLoc(), condBr.getCondition(), thenMapping[retained],
elseMapping[retained]);
Value combinedOwnership = arith::SelectOp::create(
builder, condBr.getLoc(), condBr.getCondition(),
thenMapping[retained], elseMapping[retained]);
state.updateOwnership(retained, combinedOwnership, condBr->getBlock());
}

Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/EmitC/IR/EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ void EmitCDialect::initialize() {
Operation *EmitCDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
return builder.create<emitc::ConstantOp>(loc, type, value);
return emitc::ConstantOp::create(builder, loc, type, value);
}

/// Default callback for builders of ops carrying a region. Inserts a yield
/// without arguments.
void mlir::emitc::buildTerminatedBody(OpBuilder &builder, Location loc) {
builder.create<emitc::YieldOp>(loc);
emitc::YieldOp::create(builder, loc);
}

bool mlir::emitc::isSupportedEmitCType(Type type) {
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/EmitC/Transforms/Transforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ExpressionOp createExpression(Operation *op, OpBuilder &builder) {
Location loc = op->getLoc();

builder.setInsertionPointAfter(op);
auto expressionOp = builder.create<emitc::ExpressionOp>(loc, resultType);
auto expressionOp = emitc::ExpressionOp::create(builder, loc, resultType);

// Replace all op's uses with the new expression's result.
result.replaceAllUsesWith(expressionOp.getResult());
Expand All @@ -33,7 +33,7 @@ ExpressionOp createExpression(Operation *op, OpBuilder &builder) {
Region &region = expressionOp.getRegion();
Block &block = region.emplaceBlock();
builder.setInsertionPointToEnd(&block);
auto yieldOp = builder.create<emitc::YieldOp>(loc, result);
auto yieldOp = emitc::YieldOp::create(builder, loc, result);

// Move op into the new expression.
op->moveBefore(yieldOp);
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/EmitC/Transforms/TypeConversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Value materializeAsUnrealizedCast(OpBuilder &builder, Type resultType,
if (inputs.size() != 1)
return Value();

return builder.create<UnrealizedConversionCastOp>(loc, resultType, inputs)
return UnrealizedConversionCastOp::create(builder, loc, resultType, inputs)
.getResult(0);
}

Expand Down
10 changes: 5 additions & 5 deletions mlir/lib/Dialect/EmitC/Transforms/WrapFuncInClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class WrapFuncInClass : public OpRewritePattern<emitc::FuncOp> {
PatternRewriter &rewriter) const override {

auto className = funcOp.getSymNameAttr().str() + "Class";
ClassOp newClassOp = rewriter.create<ClassOp>(funcOp.getLoc(), className);
ClassOp newClassOp = ClassOp::create(rewriter, funcOp.getLoc(), className);

SmallVector<std::pair<StringAttr, TypeAttr>> fields;
rewriter.createBlock(&newClassOp.getBody());
Expand All @@ -67,15 +67,15 @@ class WrapFuncInClass : public OpRewritePattern<emitc::FuncOp> {

TypeAttr typeAttr = TypeAttr::get(val.getType());
fields.push_back({fieldName, typeAttr});
rewriter.create<emitc::FieldOp>(funcOp.getLoc(), fieldName, typeAttr,
argAttr);
emitc::FieldOp::create(rewriter, funcOp.getLoc(), fieldName, typeAttr,
argAttr);
}

rewriter.setInsertionPointToEnd(&newClassOp.getBody().front());
FunctionType funcType = funcOp.getFunctionType();
Location loc = funcOp.getLoc();
FuncOp newFuncOp =
rewriter.create<emitc::FuncOp>(loc, ("execute"), funcType);
emitc::FuncOp::create(rewriter, loc, ("execute"), funcType);

rewriter.createBlock(&newFuncOp.getBody());
newFuncOp.getBody().takeBody(funcOp.getBody());
Expand All @@ -85,7 +85,7 @@ class WrapFuncInClass : public OpRewritePattern<emitc::FuncOp> {
newArguments.reserve(fields.size());
for (auto &[fieldName, attr] : fields) {
GetFieldOp arg =
rewriter.create<emitc::GetFieldOp>(loc, attr.getValue(), fieldName);
emitc::GetFieldOp::create(rewriter, loc, attr.getValue(), fieldName);
newArguments.push_back(arg);
}

Expand Down
3 changes: 2 additions & 1 deletion mlir/lib/Dialect/Func/Extensions/InlinerExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ struct FuncInlinerInterface : public DialectInlinerInterface {

// Replace the return with a branch to the dest.
OpBuilder builder(op);
builder.create<cf::BranchOp>(op->getLoc(), newDest, returnOp.getOperands());
cf::BranchOp::create(builder, op->getLoc(), newDest,
returnOp.getOperands());
op->erase();
}

Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Func/IR/FuncOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ void FuncDialect::initialize() {
Operation *FuncDialect::materializeConstant(OpBuilder &builder, Attribute value,
Type type, Location loc) {
if (ConstantOp::isBuildableWith(value, type))
return builder.create<ConstantOp>(loc, type,
llvm::cast<FlatSymbolRefAttr>(value));
return ConstantOp::create(builder, loc, type,
llvm::cast<FlatSymbolRefAttr>(value));
return nullptr;
}

Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Func/TransformOps/FuncTransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ transform::CastAndCallOp::apply(transform::TransformRewriter &rewriter,
}
}

auto callOp = rewriter.create<func::CallOp>(insertionPoint->getLoc(),
targetFunction, inputs);
auto callOp = func::CallOp::create(rewriter, insertionPoint->getLoc(),
targetFunction, inputs);

// Cast the call results back to the expected types. If any conversions fail
// this is a definite failure as the call has been constructed at this point.
Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Dialect/Func/Transforms/FuncConversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ struct CallOpSignatureConversion : public OpConversionPattern<CallOp> {

// Substitute with the new result types from the corresponding FuncType
// conversion.
auto newCallOp = rewriter.create<CallOp>(
callOp.getLoc(), callOp.getCallee(), convertedResults,
flattenValues(adaptor.getOperands()));
auto newCallOp =
CallOp::create(rewriter, callOp.getLoc(), callOp.getCallee(),
convertedResults, flattenValues(adaptor.getOperands()));
SmallVector<ValueRange> replacements;
size_t offset = 0;
for (int i = 0, e = callOp->getNumResults(); i < e; ++i) {
Expand Down
11 changes: 6 additions & 5 deletions mlir/lib/Dialect/Func/Utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func::replaceFuncWithNewOrder(RewriterBase &rewriter, func::FuncOp funcOp,
for (unsigned int idx : newResultsOrder)
newOutputTypes.push_back(origOutputTypes[idx]);
rewriter.setInsertionPoint(funcOp);
auto newFuncOp = rewriter.create<func::FuncOp>(
funcOp.getLoc(), funcOp.getName(),
auto newFuncOp = func::FuncOp::create(
rewriter, funcOp.getLoc(), funcOp.getName(),
rewriter.getFunctionType(newInputTypes, newOutputTypes));

Region &newRegion = newFuncOp.getBody();
Expand Down Expand Up @@ -80,7 +80,7 @@ func::replaceFuncWithNewOrder(RewriterBase &rewriter, func::FuncOp funcOp,
newReturnValues.push_back(returnOp.getOperand(idx));
rewriter.setInsertionPoint(returnOp);
auto newReturnOp =
rewriter.create<func::ReturnOp>(newFuncOp.getLoc(), newReturnValues);
func::ReturnOp::create(rewriter, newFuncOp.getLoc(), newReturnValues);
newReturnOp->setDiscardableAttrs(returnOp->getDiscardableAttrDictionary());
rewriter.eraseOp(returnOp);

Expand Down Expand Up @@ -109,8 +109,9 @@ func::replaceCallOpWithNewOrder(RewriterBase &rewriter, func::CallOp callOp,
// Replace the kernel call operation with a new one that has the
// reordered arguments.
rewriter.setInsertionPoint(callOp);
auto newCallOp = rewriter.create<func::CallOp>(
callOp.getLoc(), callOp.getCallee(), newResultTypes, newArgsOrderValues);
auto newCallOp =
func::CallOp::create(rewriter, callOp.getLoc(), callOp.getCallee(),
newResultTypes, newArgsOrderValues);
newCallOp.setNoInlineAttr(callOp.getNoInlineAttr());
for (auto &&[newIndex, origIndex] : llvm::enumerate(newResultsOrder))
rewriter.replaceAllUsesWith(callOp.getResult(origIndex),
Expand Down
45 changes: 24 additions & 21 deletions mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ int64_t GPUMappingMaskAttr::getMaxNumPhysicalIds() const { return 64; }
Value GPUMappingMaskAttr::createLogicalLinearMappingId(
OpBuilder &b, Value physicalLinearMappingId) const {
Location loc = physicalLinearMappingId.getLoc();
Value mask = b.create<arith::ConstantOp>(loc, b.getI64IntegerAttr(getMask()));
Value one = b.create<arith::ConstantOp>(loc, b.getI64IntegerAttr(1));
Value filter = b.create<arith::ShLIOp>(loc, one, physicalLinearMappingId);
filter = b.create<arith::SubIOp>(loc, filter, one);
Value filteredId = b.create<arith::AndIOp>(loc, mask, filter);
return b.create<math::CtPopOp>(loc, filteredId);
Value mask =
arith::ConstantOp::create(b, loc, b.getI64IntegerAttr(getMask()));
Value one = arith::ConstantOp::create(b, loc, b.getI64IntegerAttr(1));
Value filter = arith::ShLIOp::create(b, loc, one, physicalLinearMappingId);
filter = arith::SubIOp::create(b, loc, filter, one);
Value filteredId = arith::AndIOp::create(b, loc, mask, filter);
return math::CtPopOp::create(b, loc, filteredId);
}

/// 8 4 0
Expand All @@ -157,12 +158,14 @@ Value GPUMappingMaskAttr::createLogicalLinearMappingId(
Value GPUMappingMaskAttr::createIsActiveIdPredicate(
OpBuilder &b, Value physicalLinearMappingId) const {
Location loc = physicalLinearMappingId.getLoc();
Value mask = b.create<arith::ConstantOp>(loc, b.getI64IntegerAttr(getMask()));
Value one = b.create<arith::ConstantOp>(loc, b.getI64IntegerAttr(1));
Value filter = b.create<arith::ShLIOp>(loc, one, physicalLinearMappingId);
Value filtered = b.create<arith::AndIOp>(loc, mask, filter);
Value zero = b.create<arith::ConstantOp>(loc, b.getI64IntegerAttr(0));
return b.create<arith::CmpIOp>(loc, arith::CmpIPredicate::ne, filtered, zero);
Value mask =
arith::ConstantOp::create(b, loc, b.getI64IntegerAttr(getMask()));
Value one = arith::ConstantOp::create(b, loc, b.getI64IntegerAttr(1));
Value filter = arith::ShLIOp::create(b, loc, one, physicalLinearMappingId);
Value filtered = arith::AndIOp::create(b, loc, mask, filter);
Value zero = arith::ConstantOp::create(b, loc, b.getI64IntegerAttr(0));
return arith::CmpIOp::create(b, loc, arith::CmpIPredicate::ne, filtered,
zero);
}

int64_t GPUMemorySpaceMappingAttr::getMappingId() const {
Expand Down Expand Up @@ -1137,7 +1140,7 @@ struct FoldLaunchArguments : public OpRewritePattern<LaunchOp> {
OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPointToStart(&op.getBody().front());
zero =
rewriter.create<arith::ConstantIndexOp>(op.getLoc(), /*value=*/0);
arith::ConstantIndexOp::create(rewriter, op.getLoc(), /*value=*/0);
}
rewriter.replaceAllUsesWith(id, zero);
simplified = true;
Expand Down Expand Up @@ -1381,10 +1384,10 @@ static void printLaunchFuncOperands(OpAsmPrinter &printer, Operation *,
void ShuffleOp::build(OpBuilder &builder, OperationState &result, Value value,
int32_t offset, int32_t width, ShuffleMode mode) {
build(builder, result, value,
builder.create<arith::ConstantOp>(result.location,
builder.getI32IntegerAttr(offset)),
builder.create<arith::ConstantOp>(result.location,
builder.getI32IntegerAttr(width)),
arith::ConstantOp::create(builder, result.location,
builder.getI32IntegerAttr(offset)),
arith::ConstantOp::create(builder, result.location,
builder.getI32IntegerAttr(width)),
mode);
}

Expand All @@ -1395,10 +1398,10 @@ void ShuffleOp::build(OpBuilder &builder, OperationState &result, Value value,
void RotateOp::build(OpBuilder &builder, OperationState &result, Value value,
int32_t offset, int32_t width) {
build(builder, result, value,
builder.create<arith::ConstantOp>(result.location,
builder.getI32IntegerAttr(offset)),
builder.create<arith::ConstantOp>(result.location,
builder.getI32IntegerAttr(width)));
arith::ConstantOp::create(builder, result.location,
builder.getI32IntegerAttr(offset)),
arith::ConstantOp::create(builder, result.location,
builder.getI32IntegerAttr(width)));
}

LogicalResult RotateOp::verify() {
Expand Down
14 changes: 7 additions & 7 deletions mlir/lib/Dialect/GPU/TransformOps/GPUTransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ static DiagnosedSilenceableFailure rewriteOneForallCommonImpl(
Value predicate;
if (originalBasisWasProvided) {
for (Value tmpPredicate : builderResult.predicateOps) {
predicate = predicate ? rewriter.create<arith::AndIOp>(loc, predicate,
tmpPredicate)
predicate = predicate ? arith::AndIOp::create(rewriter, loc, predicate,
tmpPredicate)
: tmpPredicate;
}
}
Expand All @@ -573,8 +573,8 @@ static DiagnosedSilenceableFailure rewriteOneForallCommonImpl(
Block::iterator insertionPoint;
if (predicate) {
// Step 6.a. If predicated, move at the beginning.
auto ifOp = rewriter.create<scf::IfOp>(loc, predicate,
/*withElseRegion=*/false);
auto ifOp = scf::IfOp::create(rewriter, loc, predicate,
/*withElseRegion=*/false);
targetBlock = ifOp.thenBlock();
insertionPoint = ifOp.thenBlock()->begin();
} else {
Expand Down Expand Up @@ -632,7 +632,7 @@ DiagnosedSilenceableFailure mlir::transform::gpu::mapForallToBlocksImpl(
// the insertion point.
OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPointToStart(parentBlock);
zero = rewriter.create<arith::ConstantIndexOp>(loc, 0);
zero = arith::ConstantIndexOp::create(rewriter, loc, 0);
}

ForallRewriteResult rewriteResult;
Expand Down Expand Up @@ -884,7 +884,7 @@ DiagnosedSilenceableFailure mlir::transform::gpu::mapOneForallToThreadsImpl(
return diag;
// Add a syncthreads if needed. TODO: warpsync
if (syncAfterDistribute)
rewriter.create<BarrierOp>(loc);
BarrierOp::create(rewriter, loc);

return DiagnosedSilenceableFailure::success();
}
Expand All @@ -901,7 +901,7 @@ DiagnosedSilenceableFailure mlir::transform::gpu::mapNestedForallToThreadsImpl(

// Create an early zero index value for replacements.
Location loc = target->getLoc();
Value zero = rewriter.create<arith::ConstantIndexOp>(loc, 0);
Value zero = arith::ConstantIndexOp::create(rewriter, loc, 0);
DiagnosedSilenceableFailure diag = DiagnosedSilenceableFailure::success();
WalkResult walkResult = target->walk([&](scf::ForallOp forallOp) {
diag = mlir::transform::gpu::mapOneForallToThreadsImpl(
Expand Down
Loading
Loading