Skip to content

[mlir][NFC] update flang create APIs (13/n) #149913

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 2 commits into from
Jul 24, 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
44 changes: 23 additions & 21 deletions flang/include/flang/Lower/DirectivesCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,27 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Value box =
!fir::isBoxAddress(info.addr.getType())
? info.addr
: builder.create<fir::LoadOp>(loc, info.addr);
: fir::LoadOp::create(builder, loc, info.addr);
mlir::Value d =
builder.createIntegerConstant(loc, idxTy, dimension);
auto dimInfo = builder.create<fir::BoxDimsOp>(
loc, idxTy, idxTy, idxTy, box, d);
builder.create<fir::ResultOp>(loc, dimInfo.getByteStride());
auto dimInfo = fir::BoxDimsOp::create(builder, loc, idxTy,
idxTy, idxTy, box, d);
fir::ResultOp::create(builder, loc,
dimInfo.getByteStride());
})
.genElse([&] {
mlir::Value zero =
builder.createIntegerConstant(loc, idxTy, 0);
builder.create<fir::ResultOp>(loc, zero);
fir::ResultOp::create(builder, loc, zero);
})
.getResults()[0];
} else {
mlir::Value box = !fir::isBoxAddress(info.addr.getType())
? info.addr
: builder.create<fir::LoadOp>(loc, info.addr);
: fir::LoadOp::create(builder, loc, info.addr);
mlir::Value d = builder.createIntegerConstant(loc, idxTy, dimension);
auto dimInfo =
builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, d);
fir::BoxDimsOp::create(builder, loc, idxTy, idxTy, idxTy, box, d);
stride = dimInfo.getByteStride();
}
strideInBytes = true;
Expand Down Expand Up @@ -242,14 +243,14 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
lbound = builder.createIntegerConstant(loc, idxTy, *lval - 1);
} else {
mlir::Value lb = builder.createIntegerConstant(loc, idxTy, *lval);
lbound = builder.create<mlir::arith::SubIOp>(loc, lb, baseLb);
lbound = mlir::arith::SubIOp::create(builder, loc, lb, baseLb);
}
asFortran << *lval;
} else {
mlir::Value lb =
fir::getBase(converter.genExprValue(loc, *lower, stmtCtx));
lb = builder.createConvert(loc, baseLb.getType(), lb);
lbound = builder.create<mlir::arith::SubIOp>(loc, lb, baseLb);
lbound = mlir::arith::SubIOp::create(builder, loc, lb, baseLb);
asFortran << detail::peelOuterConvert(*lower).AsFortran();
}
} else {
Expand All @@ -276,14 +277,14 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
ubound = builder.createIntegerConstant(loc, idxTy, *uval - 1);
} else {
mlir::Value ub = builder.createIntegerConstant(loc, idxTy, *uval);
ubound = builder.create<mlir::arith::SubIOp>(loc, ub, baseLb);
ubound = mlir::arith::SubIOp::create(builder, loc, ub, baseLb);
}
asFortran << *uval;
} else {
mlir::Value ub =
fir::getBase(converter.genExprValue(loc, *upper, stmtCtx));
ub = builder.createConvert(loc, baseLb.getType(), ub);
ubound = builder.create<mlir::arith::SubIOp>(loc, ub, baseLb);
ubound = mlir::arith::SubIOp::create(builder, loc, ub, baseLb);
asFortran << detail::peelOuterConvert(*upper).AsFortran();
}
}
Expand All @@ -310,12 +311,12 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
.genThen([&]() {
mlir::Value ext = fir::factory::readExtent(
builder, loc, dataExv, dimension);
builder.create<fir::ResultOp>(loc, ext);
fir::ResultOp::create(builder, loc, ext);
})
.genElse([&] {
mlir::Value zero =
builder.createIntegerConstant(loc, idxTy, 0);
builder.create<fir::ResultOp>(loc, zero);
fir::ResultOp::create(builder, loc, zero);
})
.getResults()[0];
} else {
Expand All @@ -326,16 +327,16 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
extent = zero;
if (ubound && lbound) {
mlir::Value diff =
builder.create<mlir::arith::SubIOp>(loc, ubound, lbound);
extent = builder.create<mlir::arith::AddIOp>(loc, diff, one);
mlir::arith::SubIOp::create(builder, loc, ubound, lbound);
extent = mlir::arith::AddIOp::create(builder, loc, diff, one);
}
if (!ubound)
ubound = lbound;
}

if (!ubound) {
// ub = extent - 1
ubound = builder.create<mlir::arith::SubIOp>(loc, extent, one);
ubound = mlir::arith::SubIOp::create(builder, loc, extent, one);
}
}

Expand All @@ -347,8 +348,9 @@ genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
loc, cumulativeExtent, extent);
}

mlir::Value bound = builder.create<BoundsOp>(
loc, boundTy, lbound, ubound, extent, stride, strideInBytes, baseLb);
mlir::Value bound =
BoundsOp::create(builder, loc, boundTy, lbound, ubound, extent,
stride, strideInBytes, baseLb);
bounds.push_back(bound);
++dimension;
}
Expand Down Expand Up @@ -461,8 +463,8 @@ fir::factory::AddrAndBoundsInfo gatherDataOperandAddrAndBounds(
asFortran << designator.AsFortran();

if (semantics::IsOptional(compRef->GetLastSymbol())) {
info.isPresent = builder.create<fir::IsPresentOp>(
operandLocation, builder.getI1Type(), info.rawInput);
info.isPresent = fir::IsPresentOp::create(
builder, operandLocation, builder.getI1Type(), info.rawInput);
}

if (unwrapFirBox) {
Expand All @@ -472,7 +474,7 @@ fir::factory::AddrAndBoundsInfo gatherDataOperandAddrAndBounds(
fir::isPointerType(loadOp.getType())) {
info.boxType = info.addr.getType();
info.addr =
builder.create<fir::BoxAddrOp>(operandLocation, info.addr);
fir::BoxAddrOp::create(builder, operandLocation, info.addr);
}
info.rawInput = info.addr;
}
Expand Down
10 changes: 5 additions & 5 deletions flang/include/flang/Lower/Support/ReductionProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ ReductionProcessor::getReductionOperation(fir::FirOpBuilder &builder,
assert(type.isIntOrIndexOrFloat() &&
"only integer, float and complex types are currently supported");
if (type.isIntOrIndex())
return builder.create<IntegerOp>(loc, op1, op2);
return builder.create<FloatOp>(loc, op1, op2);
return IntegerOp::create(builder, loc, op1, op2);
return FloatOp::create(builder, loc, op1, op2);
}

template <typename FloatOp, typename IntegerOp, typename ComplexOp>
Expand All @@ -154,10 +154,10 @@ ReductionProcessor::getReductionOperation(fir::FirOpBuilder &builder,
assert((type.isIntOrIndexOrFloat() || fir::isa_complex(type)) &&
"only integer, float and complex types are currently supported");
if (type.isIntOrIndex())
return builder.create<IntegerOp>(loc, op1, op2);
return IntegerOp::create(builder, loc, op1, op2);
if (fir::isa_real(type))
return builder.create<FloatOp>(loc, op1, op2);
return builder.create<ComplexOp>(loc, op1, op2);
return FloatOp::create(builder, loc, op1, op2);
return ComplexOp::create(builder, loc, op1, op2);
}

} // namespace omp
Expand Down
8 changes: 4 additions & 4 deletions flang/include/flang/Optimizer/Builder/Complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ class Complex {
protected:
template <Part partId>
mlir::Value extract(mlir::Value cplx) {
return builder.create<fir::ExtractValueOp>(
loc, getComplexPartType(cplx), cplx,
return fir::ExtractValueOp::create(
builder, loc, getComplexPartType(cplx), cplx,
builder.getArrayAttr({builder.getIntegerAttr(
builder.getIndexType(), static_cast<int>(partId))}));
}

template <Part partId>
mlir::Value insert(mlir::Value cplx, mlir::Value part) {
return builder.create<fir::InsertValueOp>(
loc, cplx.getType(), cplx, part,
return fir::InsertValueOp::create(
builder, loc, cplx.getType(), cplx, part,
builder.getArrayAttr({builder.getIntegerAttr(
builder.getIndexType(), static_cast<int>(partId))}));
}
Expand Down
69 changes: 35 additions & 34 deletions flang/include/flang/Optimizer/Builder/DirectivesCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ inline AddrAndBoundsInfo getDataOperandBaseAddr(fir::FirOpBuilder &builder,
mlir::Value isPresent;
if (isOptional)
isPresent =
builder.create<fir::IsPresentOp>(loc, builder.getI1Type(), rawInput);
fir::IsPresentOp::create(builder, loc, builder.getI1Type(), rawInput);

if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(
fir::unwrapRefType(symAddr.getType()))) {
Expand All @@ -82,7 +82,7 @@ inline AddrAndBoundsInfo getDataOperandBaseAddr(fir::FirOpBuilder &builder,
// if branches.
if (unwrapFirBox && mlir::isa<fir::ReferenceType>(symAddr.getType()) &&
!isOptional) {
mlir::Value addr = builder.create<fir::LoadOp>(loc, symAddr);
mlir::Value addr = fir::LoadOp::create(builder, loc, symAddr);
return AddrAndBoundsInfo(addr, rawInput, isPresent, boxTy);
}

Expand All @@ -94,7 +94,7 @@ inline AddrAndBoundsInfo getDataOperandBaseAddr(fir::FirOpBuilder &builder,
if (auto boxCharType = mlir::dyn_cast<fir::BoxCharType>(
fir::unwrapRefType((symAddr.getType())))) {
if (!isOptional && mlir::isa<fir::ReferenceType>(symAddr.getType())) {
mlir::Value boxChar = builder.create<fir::LoadOp>(loc, symAddr);
mlir::Value boxChar = fir::LoadOp::create(builder, loc, symAddr);
return AddrAndBoundsInfo(boxChar, rawInput, isPresent);
}
}
Expand All @@ -117,10 +117,10 @@ gatherBoundsOrBoundValues(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Value baseLb =
fir::factory::readLowerBound(builder, loc, dataExv, dim, one);
auto dimInfo =
builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, d);
fir::BoxDimsOp::create(builder, loc, idxTy, idxTy, idxTy, box, d);
mlir::Value lb = builder.createIntegerConstant(loc, idxTy, 0);
mlir::Value ub =
builder.create<mlir::arith::SubIOp>(loc, dimInfo.getExtent(), one);
mlir::arith::SubIOp::create(builder, loc, dimInfo.getExtent(), one);
if (dim == 0) // First stride is the element size.
byteStride = dimInfo.getByteStride();
if (collectValuesOnly) {
Expand All @@ -130,13 +130,14 @@ gatherBoundsOrBoundValues(fir::FirOpBuilder &builder, mlir::Location loc,
values.push_back(byteStride);
values.push_back(baseLb);
} else {
mlir::Value bound = builder.create<BoundsOp>(
loc, boundTy, lb, ub, dimInfo.getExtent(), byteStride, true, baseLb);
mlir::Value bound =
BoundsOp::create(builder, loc, boundTy, lb, ub, dimInfo.getExtent(),
byteStride, true, baseLb);
values.push_back(bound);
}
// Compute the stride for the next dimension.
byteStride = builder.create<mlir::arith::MulIOp>(loc, byteStride,
dimInfo.getExtent());
byteStride = mlir::arith::MulIOp::create(builder, loc, byteStride,
dimInfo.getExtent());
}
return values;
}
Expand All @@ -162,46 +163,46 @@ genBoundsOpFromBoxChar(fir::FirOpBuilder &builder, mlir::Location loc,
.genThen([&]() {
mlir::Value boxChar =
fir::isa_ref_type(info.addr.getType())
? builder.create<fir::LoadOp>(loc, info.addr)
? fir::LoadOp::create(builder, loc, info.addr)
: info.addr;
fir::BoxCharType boxCharType =
mlir::cast<fir::BoxCharType>(boxChar.getType());
mlir::Type refType = builder.getRefType(boxCharType.getEleTy());
auto unboxed = builder.create<fir::UnboxCharOp>(
loc, refType, lenType, boxChar);
auto unboxed = fir::UnboxCharOp::create(builder, loc, refType,
lenType, boxChar);
mlir::SmallVector<mlir::Value> results = {unboxed.getResult(1),
one};
builder.create<fir::ResultOp>(loc, results);
fir::ResultOp::create(builder, loc, results);
})
.genElse([&]() {
mlir::SmallVector<mlir::Value> results = {zero, zero};
builder.create<fir::ResultOp>(loc, results);
fir::ResultOp::create(builder, loc, results);
})
.getResults();
return {ifRes[0], ifRes[1]};
}
// We have already established that info.addr.getType() is a boxchar
// or a boxchar address. If an address, load the boxchar.
mlir::Value boxChar = fir::isa_ref_type(info.addr.getType())
? builder.create<fir::LoadOp>(loc, info.addr)
? fir::LoadOp::create(builder, loc, info.addr)
: info.addr;
fir::BoxCharType boxCharType =
mlir::cast<fir::BoxCharType>(boxChar.getType());
mlir::Type refType = builder.getRefType(boxCharType.getEleTy());
auto unboxed =
builder.create<fir::UnboxCharOp>(loc, refType, lenType, boxChar);
fir::UnboxCharOp::create(builder, loc, refType, lenType, boxChar);
return {unboxed.getResult(1), one};
}();

mlir::Value ub = builder.create<mlir::arith::SubIOp>(loc, extent, one);
mlir::Value ub = mlir::arith::SubIOp::create(builder, loc, extent, one);
mlir::Type boundTy = builder.getType<BoundsType>();
return builder.create<BoundsOp>(loc, boundTy,
/*lower_bound=*/zero,
/*upper_bound=*/ub,
/*extent=*/extent,
/*stride=*/stride,
/*stride_in_bytes=*/true,
/*start_idx=*/zero);
return BoundsOp::create(builder, loc, boundTy,
/*lower_bound=*/zero,
/*upper_bound=*/ub,
/*extent=*/extent,
/*stride=*/stride,
/*stride_in_bytes=*/true,
/*start_idx=*/zero);
}

/// Generate the bounds operation from the descriptor information.
Expand Down Expand Up @@ -230,12 +231,12 @@ genBoundsOpsFromBox(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Value box =
!fir::isBoxAddress(info.addr.getType())
? info.addr
: builder.create<fir::LoadOp>(loc, info.addr);
: fir::LoadOp::create(builder, loc, info.addr);
llvm::SmallVector<mlir::Value> boundValues =
gatherBoundsOrBoundValues<BoundsOp, BoundsType>(
builder, loc, dataExv, box,
/*collectValuesOnly=*/true);
builder.create<fir::ResultOp>(loc, boundValues);
fir::ResultOp::create(builder, loc, boundValues);
})
.genElse([&] {
// Box is not present. Populate bound values with default values.
Expand All @@ -249,21 +250,21 @@ genBoundsOpsFromBox(fir::FirOpBuilder &builder, mlir::Location loc,
boundValues.push_back(zero); // byteStride
boundValues.push_back(zero); // baseLb
}
builder.create<fir::ResultOp>(loc, boundValues);
fir::ResultOp::create(builder, loc, boundValues);
})
.getResults();
// Create the bound operations outside the if-then-else with the if op
// results.
for (unsigned i = 0; i < ifRes.size(); i += nbValuesPerBound) {
mlir::Value bound = builder.create<BoundsOp>(
loc, boundTy, ifRes[i], ifRes[i + 1], ifRes[i + 2], ifRes[i + 3],
true, ifRes[i + 4]);
mlir::Value bound =
BoundsOp::create(builder, loc, boundTy, ifRes[i], ifRes[i + 1],
ifRes[i + 2], ifRes[i + 3], true, ifRes[i + 4]);
bounds.push_back(bound);
}
} else {
mlir::Value box = !fir::isBoxAddress(info.addr.getType())
? info.addr
: builder.create<fir::LoadOp>(loc, info.addr);
: fir::LoadOp::create(builder, loc, info.addr);
bounds = gatherBoundsOrBoundValues<BoundsOp, BoundsType>(builder, loc,
dataExv, box);
}
Expand Down Expand Up @@ -299,7 +300,7 @@ genBaseBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
ub = lb;
} else {
// ub = extent - 1
ub = builder.create<mlir::arith::SubIOp>(loc, extent, one);
ub = mlir::arith::SubIOp::create(builder, loc, extent, one);
}
mlir::Value stride = one;
if (strideIncludeLowerExtent) {
Expand All @@ -308,8 +309,8 @@ genBaseBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc,
loc, cumulativeExtent, extent);
}

mlir::Value bound = builder.create<BoundsOp>(loc, boundTy, lb, ub, extent,
stride, false, baseLb);
mlir::Value bound = BoundsOp::create(builder, loc, boundTy, lb, ub, extent,
stride, false, baseLb);
bounds.push_back(bound);
}
return bounds;
Expand Down
2 changes: 1 addition & 1 deletion flang/include/flang/Optimizer/CodeGen/FIROpPatterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class ConvertFIRToLLVMPattern : public mlir::ConvertToLLVMPattern {
llvm::SmallVector<mlir::LLVM::GEPArg> cv = {args...};
auto llvmPtrTy =
mlir::LLVM::LLVMPointerType::get(ty.getContext(), /*addressSpace=*/0);
return rewriter.create<mlir::LLVM::GEPOp>(loc, llvmPtrTy, ty, base, cv);
return mlir::LLVM::GEPOp::create(rewriter, loc, llvmPtrTy, ty, base, cv);
}

// Find the Block in which the alloca should be inserted.
Expand Down
11 changes: 10 additions & 1 deletion flang/unittests/Frontend/CodeGenActionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class FakeOp : public ::mlir::Op<FakeOp> {

static void build(
::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState) {}

static FakeOp create(
::mlir::OpBuilder &odsBuilder, ::mlir::Location location) {
::mlir::OperationState state(location, getOperationName());
build(odsBuilder, state);
auto res = ::llvm::dyn_cast<FakeOp>(odsBuilder.create(state));
assert(res && "builder didn't return the right type");
return res;
}
};
} // namespace dummy
} // namespace test
Expand Down Expand Up @@ -77,7 +86,7 @@ class LLVMConversionFailureCodeGenAction : public CodeGenAction {
mlir::OpBuilder builder(mlirCtx.get());
builder.setInsertionPointToStart(&mlirModule->getRegion().front());
// Create a fake op to trip conversion to LLVM.
builder.create<test::dummy::FakeOp>(loc);
test::dummy::FakeOp::create(builder, loc);

llvmCtx = std::make_unique<llvm::LLVMContext>();
}
Expand Down
Loading