[flang] Migrate away from std::nullopt (NFC) (#145928)

ArrayRef has a constructor that accepts std::nullopt.  This
constructor dates back to the days when we still had llvm::Optional.

Since the use of std::nullopt outside the context of std::optional is
kind of abuse and not intuitive to new comers, I would like to move
away from the constructor and eventually remove it.

This patch replaces std::nullopt with {}.  There are a couple of
places where std::nullopt is replaced with TypeRange() to accommodate
perfect forwarding.
This commit is contained in:
Kazu Hirata 2025-06-26 12:41:49 -07:00 committed by GitHub
parent a0c5f1992d
commit 938cdb30f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 47 additions and 55 deletions

View File

@ -267,7 +267,7 @@ public:
/// Generate the type from a category and kind and length parameters.
virtual mlir::Type
genType(Fortran::common::TypeCategory tc, int kind,
llvm::ArrayRef<std::int64_t> lenParameters = std::nullopt) = 0;
llvm::ArrayRef<std::int64_t> lenParameters = {}) = 0;
/// Generate the type from a DerivedTypeSpec.
virtual mlir::Type genType(const Fortran::semantics::DerivedTypeSpec &) = 0;
/// Generate the type from a Variable

View File

@ -537,14 +537,14 @@ public:
/// 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, std::nullopt, cdt, false);
auto op = create<fir::IfOp>(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, std::nullopt, cdt, true);
auto op = create<fir::IfOp>(loc, mlir::TypeRange(), cdt, true);
return IfBuilder(op, *this);
}

View File

@ -66,7 +66,7 @@ public:
/// of 6 KindTy must be passed. The kinds must be the given in the following
/// order: CHARACTER, COMPLEX, DOUBLE PRECISION, INTEGER, LOGICAL, and REAL.
explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map,
llvm::ArrayRef<KindTy> defs = std::nullopt);
llvm::ArrayRef<KindTy> defs = {});
explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map,
llvm::StringRef defs)
: KindMapping{context, map, toDefaultKinds(defs)} {}

View File

@ -1041,19 +1041,19 @@ createMutableProperties(Fortran::lower::AbstractConverter &converter,
baseAddrTy = boxType.getEleTy();
// Allocate and set a variable to hold the address.
// It will be set to null in setUnallocatedStatus.
mutableProperties.addr = builder.allocateLocal(
loc, baseAddrTy, name + ".addr", "",
/*shape=*/std::nullopt, /*typeparams=*/std::nullopt);
mutableProperties.addr =
builder.allocateLocal(loc, baseAddrTy, name + ".addr", "",
/*shape=*/{}, /*typeparams=*/{});
// Allocate variables to hold lower bounds and extents.
int rank = sym.Rank();
mlir::Type idxTy = builder.getIndexType();
for (decltype(rank) i = 0; i < rank; ++i) {
mlir::Value lboundVar = builder.allocateLocal(
loc, idxTy, name + ".lb" + std::to_string(i), "",
/*shape=*/std::nullopt, /*typeparams=*/std::nullopt);
mlir::Value extentVar = builder.allocateLocal(
loc, idxTy, name + ".ext" + std::to_string(i), "",
/*shape=*/std::nullopt, /*typeparams=*/std::nullopt);
mlir::Value lboundVar =
builder.allocateLocal(loc, idxTy, name + ".lb" + std::to_string(i), "",
/*shape=*/{}, /*typeparams=*/{});
mlir::Value extentVar =
builder.allocateLocal(loc, idxTy, name + ".ext" + std::to_string(i), "",
/*shape=*/{}, /*typeparams=*/{});
mutableProperties.lbounds.emplace_back(lboundVar);
mutableProperties.extents.emplace_back(extentVar);
}
@ -1068,10 +1068,9 @@ createMutableProperties(Fortran::lower::AbstractConverter &converter,
if (record.getNumLenParams() != 0)
TODO(loc, "deferred length type parameters.");
if (fir::isa_char(eleTy) && nonDeferredParams.empty()) {
mlir::Value lenVar =
builder.allocateLocal(loc, builder.getCharacterLengthType(),
name + ".len", "", /*shape=*/std::nullopt,
/*typeparams=*/std::nullopt);
mlir::Value lenVar = builder.allocateLocal(
loc, builder.getCharacterLengthType(), name + ".len", "", /*shape=*/{},
/*typeparams=*/{});
mutableProperties.deferredParams.emplace_back(lenVar);
}
return mutableProperties;

View File

@ -701,8 +701,7 @@ public:
}
mlir::Type genType(Fortran::common::TypeCategory tc) override final {
return Fortran::lower::getFIRType(
&getMLIRContext(), tc, bridge.getDefaultKinds().GetDefaultKind(tc),
std::nullopt);
&getMLIRContext(), tc, bridge.getDefaultKinds().GetDefaultKind(tc), {});
}
Fortran::lower::TypeConstructionStack &

View File

@ -1338,15 +1338,13 @@ private:
getConverter().getFoldingContext(), toEvExpr(*expr)));
return std::nullopt;
}
void addFirOperand(
mlir::Type type, int entityPosition, Property p,
llvm::ArrayRef<mlir::NamedAttribute> attributes = std::nullopt) {
void addFirOperand(mlir::Type type, int entityPosition, Property p,
llvm::ArrayRef<mlir::NamedAttribute> attributes = {}) {
interface.inputs.emplace_back(
FirPlaceHolder{type, entityPosition, p, attributes});
}
void
addFirResult(mlir::Type type, int entityPosition, Property p,
llvm::ArrayRef<mlir::NamedAttribute> attributes = std::nullopt) {
void addFirResult(mlir::Type type, int entityPosition, Property p,
llvm::ArrayRef<mlir::NamedAttribute> attributes = {}) {
interface.outputs.emplace_back(
FirPlaceHolder{type, entityPosition, p, attributes});
}

View File

@ -150,8 +150,8 @@ private:
auto attrTc = TC == Fortran::common::TypeCategory::Logical
? Fortran::common::TypeCategory::Integer
: TC;
attributeElementType = Fortran::lower::getFIRType(
builder.getContext(), attrTc, KIND, std::nullopt);
attributeElementType =
Fortran::lower::getFIRType(builder.getContext(), attrTc, KIND, {});
for (auto element : constant.values())
attributes.push_back(
convertToAttribute<TC, KIND>(builder, element, attributeElementType));
@ -230,8 +230,7 @@ static mlir::Value genScalarLit(
TC == Fortran::common::TypeCategory::Unsigned) {
// MLIR requires constants to be signless
mlir::Type ty = Fortran::lower::getFIRType(
builder.getContext(), Fortran::common::TypeCategory::Integer, KIND,
std::nullopt);
builder.getContext(), Fortran::common::TypeCategory::Integer, KIND, {});
if (KIND == 16) {
auto bigInt = llvm::APInt(ty.getIntOrFloatBitWidth(),
TC == Fortran::common::TypeCategory::Unsigned

View File

@ -1065,7 +1065,7 @@ struct BinaryOp<Fortran::evaluate::Divide<
hlfir::Entity lhs, hlfir::Entity rhs) {
mlir::Type ty = Fortran::lower::getFIRType(
builder.getContext(), Fortran::common::TypeCategory::Complex, KIND,
/*params=*/std::nullopt);
/*params=*/{});
return hlfir::EntityWithAttributes{
fir::genDivC(builder, loc, ty, lhs, rhs)};
}
@ -1078,7 +1078,7 @@ struct BinaryOp<Fortran::evaluate::Power<Fortran::evaluate::Type<TC, KIND>>> {
fir::FirOpBuilder &builder, const Op &,
hlfir::Entity lhs, hlfir::Entity rhs) {
mlir::Type ty = Fortran::lower::getFIRType(builder.getContext(), TC, KIND,
/*params=*/std::nullopt);
/*params=*/{});
return hlfir::EntityWithAttributes{fir::genPow(builder, loc, ty, lhs, rhs)};
}
};
@ -1092,7 +1092,7 @@ struct BinaryOp<
fir::FirOpBuilder &builder, const Op &,
hlfir::Entity lhs, hlfir::Entity rhs) {
mlir::Type ty = Fortran::lower::getFIRType(builder.getContext(), TC, KIND,
/*params=*/std::nullopt);
/*params=*/{});
return hlfir::EntityWithAttributes{fir::genPow(builder, loc, ty, lhs, rhs)};
}
};
@ -1416,7 +1416,7 @@ struct UnaryOp<Fortran::evaluate::Negate<
// Like LLVM, integer negation is the binary op "0 - value"
mlir::Type type = Fortran::lower::getFIRType(
builder.getContext(), Fortran::common::TypeCategory::Integer, KIND,
/*params=*/std::nullopt);
/*params=*/{});
mlir::Value zero = builder.createIntegerConstant(loc, type, 0);
return hlfir::EntityWithAttributes{
builder.create<mlir::arith::SubIOp>(loc, zero, lhs)};
@ -1517,7 +1517,7 @@ struct UnaryOp<
return hlfir::convertCharacterKind(loc, builder, lhs, KIND);
}
mlir::Type type = Fortran::lower::getFIRType(builder.getContext(), TC1,
KIND, /*params=*/std::nullopt);
KIND, /*params=*/{});
mlir::Value res = builder.convertWithSemantics(loc, type, lhs);
return hlfir::EntityWithAttributes{res};
}
@ -1661,7 +1661,7 @@ private:
} else {
elementType =
Fortran::lower::getFIRType(builder.getContext(), R::category, R::kind,
/*params=*/std::nullopt);
/*params=*/{});
}
mlir::Value shape = hlfir::genShape(loc, builder, left);
auto genKernel = [&op, &left, &unaryOp](
@ -1699,7 +1699,7 @@ private:
// Elemental expression.
mlir::Type elementType =
Fortran::lower::getFIRType(builder.getContext(), R::category, R::kind,
/*params=*/std::nullopt);
/*params=*/{});
// TODO: "merge" shape, get cst shape from front-end if possible.
mlir::Value shape;
if (left.isArray()) {

View File

@ -1289,9 +1289,8 @@ instantiateAggregateStore(Fortran::lower::AbstractConverter &converter,
auto size = std::get<1>(var.getInterval());
fir::SequenceType::Shape shape(1, size);
auto seqTy = fir::SequenceType::get(shape, i8Ty);
mlir::Value local =
builder.allocateLocal(loc, seqTy, aggName, "", std::nullopt, std::nullopt,
/*target=*/false);
mlir::Value local = builder.allocateLocal(loc, seqTy, aggName, "", {}, {},
/*target=*/false);
insertAggregateStore(storeMap, var, local);
}
@ -1839,8 +1838,8 @@ static void genDeclareSymbol(Fortran::lower::AbstractConverter &converter,
Fortran::lower::SymMap &symMap,
const Fortran::semantics::Symbol &sym,
mlir::Value base, mlir::Value len = {},
llvm::ArrayRef<mlir::Value> shape = std::nullopt,
llvm::ArrayRef<mlir::Value> lbounds = std::nullopt,
llvm::ArrayRef<mlir::Value> shape = {},
llvm::ArrayRef<mlir::Value> lbounds = {},
bool force = false) {
// In HLFIR, procedure dummy symbols are not added with an hlfir.declare
// because they are "values", and hlfir.declare is intended for variables. It
@ -2008,8 +2007,8 @@ genAllocatableOrPointerDeclare(Fortran::lower::AbstractConverter &converter,
explictLength = box.nonDeferredLenParams()[0];
}
genDeclareSymbol(converter, symMap, sym, base, explictLength,
/*shape=*/std::nullopt,
/*lbounds=*/std::nullopt, force);
/*shape=*/{},
/*lbounds=*/{}, force);
}
/// Map a procedure pointer
@ -2018,8 +2017,8 @@ static void genProcPointer(Fortran::lower::AbstractConverter &converter,
const Fortran::semantics::Symbol &sym,
mlir::Value addr, bool force = false) {
genDeclareSymbol(converter, symMap, sym, addr, mlir::Value{},
/*shape=*/std::nullopt,
/*lbounds=*/std::nullopt, force);
/*shape=*/{},
/*lbounds=*/{}, force);
}
/// Map a symbol represented with a runtime descriptor to its FIR fir.box and

View File

@ -449,7 +449,7 @@ public:
}
if (canReadCapturedBoxValue(converter, sym)) {
fir::BoxValue boxValue(box, lbounds, /*explicitParams=*/std::nullopt);
fir::BoxValue boxValue(box, lbounds, /*explicitParams=*/{});
bindCapturedSymbol(sym,
fir::factory::readBoxValue(builder, loc, boxValue),
converter, args.symMap);
@ -470,7 +470,7 @@ public:
box = builder.create<mlir::arith::SelectOp>(loc, isPresent, box,
absentBox);
}
fir::BoxValue boxValue(box, lbounds, /*explicitParams=*/std::nullopt);
fir::BoxValue boxValue(box, lbounds, /*explicitParams=*/{});
bindCapturedSymbol(sym, boxValue, converter, args.symMap);
}
}

View File

@ -119,8 +119,7 @@ std::string Fortran::lower::mangle::mangleName(
// Mangle external procedure without any scope prefix.
if (!keepExternalInScope &&
Fortran::semantics::IsExternal(ultimateSymbol))
return fir::NameUniquer::doProcedure(std::nullopt, std::nullopt,
symbolName);
return fir::NameUniquer::doProcedure({}, {}, symbolName);
// A separate module procedure must be mangled according to its
// declaration scope, not its definition scope.
const Fortran::semantics::Symbol *interface = &ultimateSymbol;
@ -142,8 +141,7 @@ std::string Fortran::lower::mangle::mangleName(
}
// Otherwise, this is an external procedure, with or without an
// explicit EXTERNAL attribute. Mangle it without any prefix.
return fir::NameUniquer::doProcedure(std::nullopt, std::nullopt,
symbolName);
return fir::NameUniquer::doProcedure({}, {}, symbolName);
},
[&](const Fortran::semantics::ObjectEntityDetails &) {
return mangleObject();

View File

@ -380,7 +380,7 @@ fir::factory::CharacterExprHelper::createCharacterTemp(mlir::Type type,
if (typeLen == fir::CharacterType::unknownLen())
lenParams.push_back(len);
auto ref = builder.allocateLocal(loc, charTy, "", ".chrtmp",
/*shape=*/std::nullopt, lenParams);
/*shape=*/{}, lenParams);
return {ref, len};
}

View File

@ -362,7 +362,7 @@ mlir::Value fir::factory::createUnallocatedBox(
auto zero = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
llvm::SmallVector<mlir::Value> extents(seqTy.getDimension(), zero);
shape = builder.createShape(
loc, fir::ArrayBoxValue{nullAddr, extents, /*lbounds=*/std::nullopt});
loc, fir::ArrayBoxValue{nullAddr, extents, /*lbounds=*/{}});
}
// Provide dummy length parameters if they are dynamic. If a length parameter
// is deferred. It is set to zero here and will be set on allocation.

View File

@ -128,8 +128,8 @@ LLVMTypeConverter::LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA,
/*isPacked=*/false);
});
addConversion([&](mlir::NoneType none) {
return mlir::LLVM::LLVMStructType::getLiteral(
none.getContext(), std::nullopt, /*isPacked=*/false);
return mlir::LLVM::LLVMStructType::getLiteral(none.getContext(), {},
/*isPacked=*/false);
});
addConversion([&](fir::DummyScopeType dscope) {
// DummyScopeType values must not have any uses after PreCGRewrite.