Skip to content

Commit cd7b3ed

Browse files
kazutakahirataAnthony Tran
authored andcommitted
[flang] Migrate away from std::nullopt (NFC) (llvm#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.
1 parent 3a44be2 commit cd7b3ed

File tree

14 files changed

+47
-55
lines changed

14 files changed

+47
-55
lines changed

flang/include/flang/Lower/AbstractConverter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class AbstractConverter {
267267
/// Generate the type from a category and kind and length parameters.
268268
virtual mlir::Type
269269
genType(Fortran::common::TypeCategory tc, int kind,
270-
llvm::ArrayRef<std::int64_t> lenParameters = std::nullopt) = 0;
270+
llvm::ArrayRef<std::int64_t> lenParameters = {}) = 0;
271271
/// Generate the type from a DerivedTypeSpec.
272272
virtual mlir::Type genType(const Fortran::semantics::DerivedTypeSpec &) = 0;
273273
/// Generate the type from a Variable

flang/include/flang/Optimizer/Builder/FIRBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,14 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
537537
/// Create an IfOp with no "else" region, and no result values.
538538
/// Usage: genIfThen(loc, cdt).genThen(lambda).end();
539539
IfBuilder genIfThen(mlir::Location loc, mlir::Value cdt) {
540-
auto op = create<fir::IfOp>(loc, std::nullopt, cdt, false);
540+
auto op = create<fir::IfOp>(loc, mlir::TypeRange(), cdt, false);
541541
return IfBuilder(op, *this);
542542
}
543543

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

flang/include/flang/Optimizer/Dialect/Support/KindMapping.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class KindMapping {
6666
/// of 6 KindTy must be passed. The kinds must be the given in the following
6767
/// order: CHARACTER, COMPLEX, DOUBLE PRECISION, INTEGER, LOGICAL, and REAL.
6868
explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map,
69-
llvm::ArrayRef<KindTy> defs = std::nullopt);
69+
llvm::ArrayRef<KindTy> defs = {});
7070
explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map,
7171
llvm::StringRef defs)
7272
: KindMapping{context, map, toDefaultKinds(defs)} {}

flang/lib/Lower/Allocatable.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,19 +1041,19 @@ createMutableProperties(Fortran::lower::AbstractConverter &converter,
10411041
baseAddrTy = boxType.getEleTy();
10421042
// Allocate and set a variable to hold the address.
10431043
// It will be set to null in setUnallocatedStatus.
1044-
mutableProperties.addr = builder.allocateLocal(
1045-
loc, baseAddrTy, name + ".addr", "",
1046-
/*shape=*/std::nullopt, /*typeparams=*/std::nullopt);
1044+
mutableProperties.addr =
1045+
builder.allocateLocal(loc, baseAddrTy, name + ".addr", "",
1046+
/*shape=*/{}, /*typeparams=*/{});
10471047
// Allocate variables to hold lower bounds and extents.
10481048
int rank = sym.Rank();
10491049
mlir::Type idxTy = builder.getIndexType();
10501050
for (decltype(rank) i = 0; i < rank; ++i) {
1051-
mlir::Value lboundVar = builder.allocateLocal(
1052-
loc, idxTy, name + ".lb" + std::to_string(i), "",
1053-
/*shape=*/std::nullopt, /*typeparams=*/std::nullopt);
1054-
mlir::Value extentVar = builder.allocateLocal(
1055-
loc, idxTy, name + ".ext" + std::to_string(i), "",
1056-
/*shape=*/std::nullopt, /*typeparams=*/std::nullopt);
1051+
mlir::Value lboundVar =
1052+
builder.allocateLocal(loc, idxTy, name + ".lb" + std::to_string(i), "",
1053+
/*shape=*/{}, /*typeparams=*/{});
1054+
mlir::Value extentVar =
1055+
builder.allocateLocal(loc, idxTy, name + ".ext" + std::to_string(i), "",
1056+
/*shape=*/{}, /*typeparams=*/{});
10571057
mutableProperties.lbounds.emplace_back(lboundVar);
10581058
mutableProperties.extents.emplace_back(extentVar);
10591059
}
@@ -1068,10 +1068,9 @@ createMutableProperties(Fortran::lower::AbstractConverter &converter,
10681068
if (record.getNumLenParams() != 0)
10691069
TODO(loc, "deferred length type parameters.");
10701070
if (fir::isa_char(eleTy) && nonDeferredParams.empty()) {
1071-
mlir::Value lenVar =
1072-
builder.allocateLocal(loc, builder.getCharacterLengthType(),
1073-
name + ".len", "", /*shape=*/std::nullopt,
1074-
/*typeparams=*/std::nullopt);
1071+
mlir::Value lenVar = builder.allocateLocal(
1072+
loc, builder.getCharacterLengthType(), name + ".len", "", /*shape=*/{},
1073+
/*typeparams=*/{});
10751074
mutableProperties.deferredParams.emplace_back(lenVar);
10761075
}
10771076
return mutableProperties;

flang/lib/Lower/Bridge.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
701701
}
702702
mlir::Type genType(Fortran::common::TypeCategory tc) override final {
703703
return Fortran::lower::getFIRType(
704-
&getMLIRContext(), tc, bridge.getDefaultKinds().GetDefaultKind(tc),
705-
std::nullopt);
704+
&getMLIRContext(), tc, bridge.getDefaultKinds().GetDefaultKind(tc), {});
706705
}
707706

708707
Fortran::lower::TypeConstructionStack &

flang/lib/Lower/CallInterface.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,15 +1338,13 @@ class Fortran::lower::CallInterfaceImpl {
13381338
getConverter().getFoldingContext(), toEvExpr(*expr)));
13391339
return std::nullopt;
13401340
}
1341-
void addFirOperand(
1342-
mlir::Type type, int entityPosition, Property p,
1343-
llvm::ArrayRef<mlir::NamedAttribute> attributes = std::nullopt) {
1341+
void addFirOperand(mlir::Type type, int entityPosition, Property p,
1342+
llvm::ArrayRef<mlir::NamedAttribute> attributes = {}) {
13441343
interface.inputs.emplace_back(
13451344
FirPlaceHolder{type, entityPosition, p, attributes});
13461345
}
1347-
void
1348-
addFirResult(mlir::Type type, int entityPosition, Property p,
1349-
llvm::ArrayRef<mlir::NamedAttribute> attributes = std::nullopt) {
1346+
void addFirResult(mlir::Type type, int entityPosition, Property p,
1347+
llvm::ArrayRef<mlir::NamedAttribute> attributes = {}) {
13501348
interface.outputs.emplace_back(
13511349
FirPlaceHolder{type, entityPosition, p, attributes});
13521350
}

flang/lib/Lower/ConvertConstant.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ class DenseGlobalBuilder {
150150
auto attrTc = TC == Fortran::common::TypeCategory::Logical
151151
? Fortran::common::TypeCategory::Integer
152152
: TC;
153-
attributeElementType = Fortran::lower::getFIRType(
154-
builder.getContext(), attrTc, KIND, std::nullopt);
153+
attributeElementType =
154+
Fortran::lower::getFIRType(builder.getContext(), attrTc, KIND, {});
155155
for (auto element : constant.values())
156156
attributes.push_back(
157157
convertToAttribute<TC, KIND>(builder, element, attributeElementType));
@@ -230,8 +230,7 @@ static mlir::Value genScalarLit(
230230
TC == Fortran::common::TypeCategory::Unsigned) {
231231
// MLIR requires constants to be signless
232232
mlir::Type ty = Fortran::lower::getFIRType(
233-
builder.getContext(), Fortran::common::TypeCategory::Integer, KIND,
234-
std::nullopt);
233+
builder.getContext(), Fortran::common::TypeCategory::Integer, KIND, {});
235234
if (KIND == 16) {
236235
auto bigInt = llvm::APInt(ty.getIntOrFloatBitWidth(),
237236
TC == Fortran::common::TypeCategory::Unsigned

flang/lib/Lower/ConvertExprToHLFIR.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ struct BinaryOp<Fortran::evaluate::Divide<
10651065
hlfir::Entity lhs, hlfir::Entity rhs) {
10661066
mlir::Type ty = Fortran::lower::getFIRType(
10671067
builder.getContext(), Fortran::common::TypeCategory::Complex, KIND,
1068-
/*params=*/std::nullopt);
1068+
/*params=*/{});
10691069
return hlfir::EntityWithAttributes{
10701070
fir::genDivC(builder, loc, ty, lhs, rhs)};
10711071
}
@@ -1078,7 +1078,7 @@ struct BinaryOp<Fortran::evaluate::Power<Fortran::evaluate::Type<TC, KIND>>> {
10781078
fir::FirOpBuilder &builder, const Op &,
10791079
hlfir::Entity lhs, hlfir::Entity rhs) {
10801080
mlir::Type ty = Fortran::lower::getFIRType(builder.getContext(), TC, KIND,
1081-
/*params=*/std::nullopt);
1081+
/*params=*/{});
10821082
return hlfir::EntityWithAttributes{fir::genPow(builder, loc, ty, lhs, rhs)};
10831083
}
10841084
};
@@ -1092,7 +1092,7 @@ struct BinaryOp<
10921092
fir::FirOpBuilder &builder, const Op &,
10931093
hlfir::Entity lhs, hlfir::Entity rhs) {
10941094
mlir::Type ty = Fortran::lower::getFIRType(builder.getContext(), TC, KIND,
1095-
/*params=*/std::nullopt);
1095+
/*params=*/{});
10961096
return hlfir::EntityWithAttributes{fir::genPow(builder, loc, ty, lhs, rhs)};
10971097
}
10981098
};
@@ -1416,7 +1416,7 @@ struct UnaryOp<Fortran::evaluate::Negate<
14161416
// Like LLVM, integer negation is the binary op "0 - value"
14171417
mlir::Type type = Fortran::lower::getFIRType(
14181418
builder.getContext(), Fortran::common::TypeCategory::Integer, KIND,
1419-
/*params=*/std::nullopt);
1419+
/*params=*/{});
14201420
mlir::Value zero = builder.createIntegerConstant(loc, type, 0);
14211421
return hlfir::EntityWithAttributes{
14221422
builder.create<mlir::arith::SubIOp>(loc, zero, lhs)};
@@ -1517,7 +1517,7 @@ struct UnaryOp<
15171517
return hlfir::convertCharacterKind(loc, builder, lhs, KIND);
15181518
}
15191519
mlir::Type type = Fortran::lower::getFIRType(builder.getContext(), TC1,
1520-
KIND, /*params=*/std::nullopt);
1520+
KIND, /*params=*/{});
15211521
mlir::Value res = builder.convertWithSemantics(loc, type, lhs);
15221522
return hlfir::EntityWithAttributes{res};
15231523
}
@@ -1661,7 +1661,7 @@ class HlfirBuilder {
16611661
} else {
16621662
elementType =
16631663
Fortran::lower::getFIRType(builder.getContext(), R::category, R::kind,
1664-
/*params=*/std::nullopt);
1664+
/*params=*/{});
16651665
}
16661666
mlir::Value shape = hlfir::genShape(loc, builder, left);
16671667
auto genKernel = [&op, &left, &unaryOp](
@@ -1699,7 +1699,7 @@ class HlfirBuilder {
16991699
// Elemental expression.
17001700
mlir::Type elementType =
17011701
Fortran::lower::getFIRType(builder.getContext(), R::category, R::kind,
1702-
/*params=*/std::nullopt);
1702+
/*params=*/{});
17031703
// TODO: "merge" shape, get cst shape from front-end if possible.
17041704
mlir::Value shape;
17051705
if (left.isArray()) {

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,9 +1289,8 @@ instantiateAggregateStore(Fortran::lower::AbstractConverter &converter,
12891289
auto size = std::get<1>(var.getInterval());
12901290
fir::SequenceType::Shape shape(1, size);
12911291
auto seqTy = fir::SequenceType::get(shape, i8Ty);
1292-
mlir::Value local =
1293-
builder.allocateLocal(loc, seqTy, aggName, "", std::nullopt, std::nullopt,
1294-
/*target=*/false);
1292+
mlir::Value local = builder.allocateLocal(loc, seqTy, aggName, "", {}, {},
1293+
/*target=*/false);
12951294
insertAggregateStore(storeMap, var, local);
12961295
}
12971296

@@ -1839,8 +1838,8 @@ static void genDeclareSymbol(Fortran::lower::AbstractConverter &converter,
18391838
Fortran::lower::SymMap &symMap,
18401839
const Fortran::semantics::Symbol &sym,
18411840
mlir::Value base, mlir::Value len = {},
1842-
llvm::ArrayRef<mlir::Value> shape = std::nullopt,
1843-
llvm::ArrayRef<mlir::Value> lbounds = std::nullopt,
1841+
llvm::ArrayRef<mlir::Value> shape = {},
1842+
llvm::ArrayRef<mlir::Value> lbounds = {},
18441843
bool force = false) {
18451844
// In HLFIR, procedure dummy symbols are not added with an hlfir.declare
18461845
// because they are "values", and hlfir.declare is intended for variables. It
@@ -2008,8 +2007,8 @@ genAllocatableOrPointerDeclare(Fortran::lower::AbstractConverter &converter,
20082007
explictLength = box.nonDeferredLenParams()[0];
20092008
}
20102009
genDeclareSymbol(converter, symMap, sym, base, explictLength,
2011-
/*shape=*/std::nullopt,
2012-
/*lbounds=*/std::nullopt, force);
2010+
/*shape=*/{},
2011+
/*lbounds=*/{}, force);
20132012
}
20142013

20152014
/// Map a procedure pointer
@@ -2018,8 +2017,8 @@ static void genProcPointer(Fortran::lower::AbstractConverter &converter,
20182017
const Fortran::semantics::Symbol &sym,
20192018
mlir::Value addr, bool force = false) {
20202019
genDeclareSymbol(converter, symMap, sym, addr, mlir::Value{},
2021-
/*shape=*/std::nullopt,
2022-
/*lbounds=*/std::nullopt, force);
2020+
/*shape=*/{},
2021+
/*lbounds=*/{}, force);
20232022
}
20242023

20252024
/// Map a symbol represented with a runtime descriptor to its FIR fir.box and

flang/lib/Lower/HostAssociations.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ class CapturedArrays : public CapturedSymbols<CapturedArrays> {
449449
}
450450

451451
if (canReadCapturedBoxValue(converter, sym)) {
452-
fir::BoxValue boxValue(box, lbounds, /*explicitParams=*/std::nullopt);
452+
fir::BoxValue boxValue(box, lbounds, /*explicitParams=*/{});
453453
bindCapturedSymbol(sym,
454454
fir::factory::readBoxValue(builder, loc, boxValue),
455455
converter, args.symMap);
@@ -470,7 +470,7 @@ class CapturedArrays : public CapturedSymbols<CapturedArrays> {
470470
box = builder.create<mlir::arith::SelectOp>(loc, isPresent, box,
471471
absentBox);
472472
}
473-
fir::BoxValue boxValue(box, lbounds, /*explicitParams=*/std::nullopt);
473+
fir::BoxValue boxValue(box, lbounds, /*explicitParams=*/{});
474474
bindCapturedSymbol(sym, boxValue, converter, args.symMap);
475475
}
476476
}

flang/lib/Lower/Mangler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ std::string Fortran::lower::mangle::mangleName(
119119
// Mangle external procedure without any scope prefix.
120120
if (!keepExternalInScope &&
121121
Fortran::semantics::IsExternal(ultimateSymbol))
122-
return fir::NameUniquer::doProcedure(std::nullopt, std::nullopt,
123-
symbolName);
122+
return fir::NameUniquer::doProcedure({}, {}, symbolName);
124123
// A separate module procedure must be mangled according to its
125124
// declaration scope, not its definition scope.
126125
const Fortran::semantics::Symbol *interface = &ultimateSymbol;
@@ -142,8 +141,7 @@ std::string Fortran::lower::mangle::mangleName(
142141
}
143142
// Otherwise, this is an external procedure, with or without an
144143
// explicit EXTERNAL attribute. Mangle it without any prefix.
145-
return fir::NameUniquer::doProcedure(std::nullopt, std::nullopt,
146-
symbolName);
144+
return fir::NameUniquer::doProcedure({}, {}, symbolName);
147145
},
148146
[&](const Fortran::semantics::ObjectEntityDetails &) {
149147
return mangleObject();

flang/lib/Optimizer/Builder/Character.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ fir::factory::CharacterExprHelper::createCharacterTemp(mlir::Type type,
380380
if (typeLen == fir::CharacterType::unknownLen())
381381
lenParams.push_back(len);
382382
auto ref = builder.allocateLocal(loc, charTy, "", ".chrtmp",
383-
/*shape=*/std::nullopt, lenParams);
383+
/*shape=*/{}, lenParams);
384384
return {ref, len};
385385
}
386386

flang/lib/Optimizer/Builder/MutableBox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ mlir::Value fir::factory::createUnallocatedBox(
362362
auto zero = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
363363
llvm::SmallVector<mlir::Value> extents(seqTy.getDimension(), zero);
364364
shape = builder.createShape(
365-
loc, fir::ArrayBoxValue{nullAddr, extents, /*lbounds=*/std::nullopt});
365+
loc, fir::ArrayBoxValue{nullAddr, extents, /*lbounds=*/{}});
366366
}
367367
// Provide dummy length parameters if they are dynamic. If a length parameter
368368
// is deferred. It is set to zero here and will be set on allocation.

flang/lib/Optimizer/CodeGen/TypeConverter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ LLVMTypeConverter::LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA,
128128
/*isPacked=*/false);
129129
});
130130
addConversion([&](mlir::NoneType none) {
131-
return mlir::LLVM::LLVMStructType::getLiteral(
132-
none.getContext(), std::nullopt, /*isPacked=*/false);
131+
return mlir::LLVM::LLVMStructType::getLiteral(none.getContext(), {},
132+
/*isPacked=*/false);
133133
});
134134
addConversion([&](fir::DummyScopeType dscope) {
135135
// DummyScopeType values must not have any uses after PreCGRewrite.

0 commit comments

Comments
 (0)