Skip to content

[flang][NFC] Update more FIR op creation to the new APIs #152060

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
Aug 5, 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
6 changes: 3 additions & 3 deletions flang/include/flang/Optimizer/Builder/FIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -530,21 +530,21 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
/// bodies.
IfBuilder genIfOp(mlir::Location loc, mlir::TypeRange results,
mlir::Value cdt, bool withElseRegion) {
auto op = create<fir::IfOp>(loc, results, cdt, withElseRegion);
auto op = fir::IfOp::create(*this, loc, results, cdt, withElseRegion);
return IfBuilder(op, *this);
}

/// Create an IfOp with no "else" region, and no result values.
/// Usage: genIfThen(loc, cdt).genThen(lambda).end();
IfBuilder genIfThen(mlir::Location loc, mlir::Value cdt) {
auto op = create<fir::IfOp>(loc, mlir::TypeRange(), cdt, false);
auto op = fir::IfOp::create(*this, loc, mlir::TypeRange(), cdt, false);
return IfBuilder(op, *this);
}

/// Create an IfOp with an "else" region, and no result values.
/// Usage: genIfThenElse(loc, cdt).genThen(lambda).genElse(lambda).end();
IfBuilder genIfThenElse(mlir::Location loc, mlir::Value cdt) {
auto op = create<fir::IfOp>(loc, mlir::TypeRange(), cdt, true);
auto op = fir::IfOp::create(*this, loc, mlir::TypeRange(), cdt, true);
return IfBuilder(op, *this);
}

Expand Down
75 changes: 36 additions & 39 deletions flang/include/flang/Optimizer/Builder/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ void genCharacterCopy(mlir::Value src, mlir::Value srcLen, mlir::Value dst,
if (!srcLen && !dstLen && srcTy.getFKind() == dstTy.getFKind() &&
srcTy.getLen() == dstTy.getLen()) {
// same size, so just use load and store
auto load = builder.template create<fir::LoadOp>(loc, src);
builder.template create<fir::StoreOp>(loc, load, dst);
auto load = fir::LoadOp::create(builder, loc, src);
fir::StoreOp::create(builder, loc, load, dst);
return;
}
auto zero = builder.template create<mlir::arith::ConstantIndexOp>(loc, 0);
Expand All @@ -70,75 +70,72 @@ void genCharacterCopy(mlir::Value src, mlir::Value srcLen, mlir::Value dst,
if (!srcLen && !dstLen && srcTy.getLen() >= dstTy.getLen()) {
auto upper = builder.template create<mlir::arith::ConstantIndexOp>(
loc, dstTy.getLen() - 1);
auto loop = builder.template create<fir::DoLoopOp>(loc, zero, upper, one);
auto loop = fir::DoLoopOp::create(builder, loc, zero, upper, one);
auto insPt = builder.saveInsertionPoint();
builder.setInsertionPointToStart(loop.getBody());
auto csrcTy = toArrayTy(srcTy);
auto csrc = builder.template create<fir::ConvertOp>(loc, csrcTy, src);
auto in = builder.template create<fir::CoordinateOp>(
loc, toCoorTy(csrcTy), csrc, loop.getInductionVar());
auto load = builder.template create<fir::LoadOp>(loc, in);
auto csrc = fir::ConvertOp::create(builder, loc, csrcTy, src);
auto in = fir::CoordinateOp::create(builder, loc, toCoorTy(csrcTy), csrc,
loop.getInductionVar());
auto load = fir::LoadOp::create(builder, loc, in);
auto cdstTy = toArrayTy(dstTy);
auto cdst = builder.template create<fir::ConvertOp>(loc, cdstTy, dst);
auto out = builder.template create<fir::CoordinateOp>(
loc, toCoorTy(cdstTy), cdst, loop.getInductionVar());
auto cdst = fir::ConvertOp::create(builder, loc, cdstTy, dst);
auto out = fir::CoordinateOp::create(builder, loc, toCoorTy(cdstTy), cdst,
loop.getInductionVar());
mlir::Value cast =
srcTy.getFKind() == dstTy.getFKind()
? load.getResult()
: builder
.template create<fir::ConvertOp>(loc, toEleTy(cdstTy), load)
: fir::ConvertOp::create(builder, loc, toEleTy(cdstTy), load)
.getResult();
builder.template create<fir::StoreOp>(loc, cast, out);
fir::StoreOp::create(builder, loc, cast, out);
builder.restoreInsertionPoint(insPt);
return;
}
auto minusOne = [&](mlir::Value v) -> mlir::Value {
return builder.template create<mlir::arith::SubIOp>(
loc, builder.template create<fir::ConvertOp>(loc, one.getType(), v),
one);
loc, fir::ConvertOp::create(builder, loc, one.getType(), v), one);
};
mlir::Value len = dstLen ? minusOne(dstLen)
: builder
.template create<mlir::arith::ConstantIndexOp>(
loc, dstTy.getLen() - 1)
.getResult();
auto loop = builder.template create<fir::DoLoopOp>(loc, zero, len, one);
auto loop = fir::DoLoopOp::create(builder, loc, zero, len, one);
auto insPt = builder.saveInsertionPoint();
builder.setInsertionPointToStart(loop.getBody());
mlir::Value slen =
srcLen
? builder.template create<fir::ConvertOp>(loc, one.getType(), srcLen)
.getResult()
: builder
.template create<mlir::arith::ConstantIndexOp>(loc,
srcTy.getLen())
.getResult();
srcLen ? fir::ConvertOp::create(builder, loc, one.getType(), srcLen)
.getResult()
: builder
.template create<mlir::arith::ConstantIndexOp>(
loc, srcTy.getLen())
.getResult();
auto cond = builder.template create<mlir::arith::CmpIOp>(
loc, mlir::arith::CmpIPredicate::slt, loop.getInductionVar(), slen);
auto ifOp = builder.template create<fir::IfOp>(loc, cond, /*withElse=*/true);
auto ifOp = fir::IfOp::create(builder, loc, cond, /*withElse=*/true);
builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
auto csrcTy = toArrayTy(srcTy);
auto csrc = builder.template create<fir::ConvertOp>(loc, csrcTy, src);
auto in = builder.template create<fir::CoordinateOp>(
loc, toCoorTy(csrcTy), csrc, loop.getInductionVar());
auto load = builder.template create<fir::LoadOp>(loc, in);
auto csrc = fir::ConvertOp::create(builder, loc, csrcTy, src);
auto in = fir::CoordinateOp::create(builder, loc, toCoorTy(csrcTy), csrc,
loop.getInductionVar());
auto load = fir::LoadOp::create(builder, loc, in);
auto cdstTy = toArrayTy(dstTy);
auto cdst = builder.template create<fir::ConvertOp>(loc, cdstTy, dst);
auto out = builder.template create<fir::CoordinateOp>(
loc, toCoorTy(cdstTy), cdst, loop.getInductionVar());
auto cdst = fir::ConvertOp::create(builder, loc, cdstTy, dst);
auto out = fir::CoordinateOp::create(builder, loc, toCoorTy(cdstTy), cdst,
loop.getInductionVar());
mlir::Value cast =
srcTy.getFKind() == dstTy.getFKind()
? load.getResult()
: builder.template create<fir::ConvertOp>(loc, toEleTy(cdstTy), load)
: fir::ConvertOp::create(builder, loc, toEleTy(cdstTy), load)
.getResult();
builder.template create<fir::StoreOp>(loc, cast, out);
fir::StoreOp::create(builder, loc, cast, out);
builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
auto space = builder.template create<fir::StringLitOp>(
loc, toEleTy(cdstTy), llvm::ArrayRef<char>{' '});
auto cdst2 = builder.template create<fir::ConvertOp>(loc, cdstTy, dst);
auto out2 = builder.template create<fir::CoordinateOp>(
loc, toCoorTy(cdstTy), cdst2, loop.getInductionVar());
builder.template create<fir::StoreOp>(loc, space, out2);
auto space = fir::StringLitOp::create(builder, loc, toEleTy(cdstTy),
llvm::ArrayRef<char>{' '});
auto cdst2 = fir::ConvertOp::create(builder, loc, cdstTy, dst);
auto out2 = fir::CoordinateOp::create(builder, loc, toCoorTy(cdstTy), cdst2,
loop.getInductionVar());
fir::StoreOp::create(builder, loc, space, out2);
builder.restoreInsertionPoint(insPt);
}

Expand Down
Loading