Skip to content

Commit

Permalink
mlir/tblgen: use std::optional in generation
Browse files Browse the repository at this point in the history
This is part of an effort to migrate from llvm::Optional to
std::optional. This patch changes the way mlir-tblgen generates .inc
files, and modifies tests and documentation appropriately. It is a "no
compromises" patch, and doesn't leave the user with an unpleasant mix of
llvm::Optional and std::optional.

A non-trivial change has been made to ControlFlowInterfaces to split one
constructor into two, relating to a build failure on Windows.

See also: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716

Signed-off-by: Ramkumar Ramachandra <r@artagnon.com>

Differential Revision: https://reviews.llvm.org/D138934
  • Loading branch information
artagnon committed Dec 17, 2022
1 parent a0f168f commit 2242611
Show file tree
Hide file tree
Showing 128 changed files with 689 additions and 660 deletions.
18 changes: 9 additions & 9 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Expand Up @@ -483,15 +483,15 @@ class fir_SwitchTerminatorOp<string mnemonic, list<Trait> traits = []> :
// The number of blocks that may be branched to
unsigned getNumDest() { return (*this)->getNumSuccessors(); }

llvm::Optional<mlir::OperandRange> getCompareOperands(unsigned cond);
llvm::Optional<llvm::ArrayRef<mlir::Value>> getCompareOperands(
std::optional<mlir::OperandRange> getCompareOperands(unsigned cond);
std::optional<llvm::ArrayRef<mlir::Value>> getCompareOperands(
llvm::ArrayRef<mlir::Value> operands, unsigned cond);
llvm::Optional<mlir::ValueRange> getCompareOperands(
std::optional<mlir::ValueRange> getCompareOperands(
mlir::ValueRange operands, unsigned cond);

llvm::Optional<llvm::ArrayRef<mlir::Value>> getSuccessorOperands(
std::optional<llvm::ArrayRef<mlir::Value>> getSuccessorOperands(
llvm::ArrayRef<mlir::Value> operands, unsigned cond);
llvm::Optional<mlir::ValueRange> getSuccessorOperands(
std::optional<mlir::ValueRange> getSuccessorOperands(
mlir::ValueRange operands, unsigned cond);

// Helper function to deal with Optional operand forms
Expand Down Expand Up @@ -2426,16 +2426,16 @@ def fir_StringLitOp : fir_Op<"string_lit", [NoMemoryEffect]> {
let builders = [
OpBuilder<(ins "fir::CharacterType":$inType,
"llvm::StringRef":$value,
CArg<"llvm::Optional<int64_t>", "{}">:$len)>,
CArg<"std::optional<int64_t>", "{}">:$len)>,
OpBuilder<(ins "fir::CharacterType":$inType,
"llvm::ArrayRef<char>":$xlist,
CArg<"llvm::Optional<int64_t>", "{}">:$len)>,
CArg<"std::optional<int64_t>", "{}">:$len)>,
OpBuilder<(ins "fir::CharacterType":$inType,
"llvm::ArrayRef<char16_t>":$xlist,
CArg<"llvm::Optional<int64_t>", "{}">:$len)>,
CArg<"std::optional<int64_t>", "{}">:$len)>,
OpBuilder<(ins "fir::CharacterType":$inType,
"llvm::ArrayRef<char32_t>":$xlist,
CArg<"llvm::Optional<int64_t>", "{}">:$len)>];
CArg<"std::optional<int64_t>", "{}">:$len)>];

let extraClassDeclaration = [{
static constexpr const char *size() { return "size"; }
Expand Down
Expand Up @@ -36,7 +36,7 @@ def fir_FortranVariableOpInterface : OpInterface<"FortranVariableOpInterface"> {
>,
InterfaceMethod<
/*desc=*/"Get Fortran attributes",
/*retTy=*/"llvm::Optional<fir::FortranVariableFlagsEnum>",
/*retTy=*/"std::optional<fir::FortranVariableFlagsEnum>",
/*methodName=*/"getFortranAttrs",
/*args=*/(ins),
/*methodBody=*/[{}],
Expand Down Expand Up @@ -91,7 +91,7 @@ def fir_FortranVariableOpInterface : OpInterface<"FortranVariableOpInterface"> {
}

/// Return the rank of the entity if it is known at compile time.
llvm::Optional<unsigned> getRank() {
std::optional<unsigned> getRank() {
if (auto sequenceType =
getElementOrSequenceType().dyn_cast<fir::SequenceType>()) {
if (sequenceType.hasUnknownShape())
Expand Down
2 changes: 1 addition & 1 deletion flang/include/flang/Optimizer/HLFIR/HLFIROps.td
Expand Up @@ -202,7 +202,7 @@ def hlfir_DesignateOp : hlfir_Op<"designate", [AttrSizedOperandSegments,
"llvm::StringRef":$component, "mlir::Value":$component_shape,
"llvm::ArrayRef<std::variant<mlir::Value, std::tuple<mlir::Value, mlir::Value, mlir::Value>>>":$subscripts,
CArg<"mlir::ValueRange", "{}">:$substring,
CArg<"llvm::Optional<bool>", "{}">:$complex_part,
CArg<"std::optional<bool>", "{}">:$complex_part,
CArg<"mlir::Value", "{}">:$shape, CArg<"mlir::ValueRange", "{}">:$typeparams,
CArg<"fir::FortranVariableFlagsAttr", "{}">:$fortran_attrs)>,

Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Frontend/FrontendActions.cpp
Expand Up @@ -552,7 +552,7 @@ void CodeGenAction::generateLLVMIR() {
}

// Translate to LLVM IR
llvm::Optional<llvm::StringRef> moduleName = mlirModule->getName();
std::optional<llvm::StringRef> moduleName = mlirModule->getName();
llvmModule = mlir::translateModuleToLLVMIR(
*mlirModule, *llvmCtx, moduleName ? *moduleName : "FIRModule");

Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Lower/ConvertExprToHLFIR.cpp
Expand Up @@ -99,7 +99,7 @@ class HlfirDesignatorBuilder {
else
resultType = fir::ReferenceType::get(resultValueType);

llvm::Optional<bool> complexPart;
std::optional<bool> complexPart;
llvm::SmallVector<mlir::Value> substring;
auto designate = getBuilder().create<hlfir::DesignateOp>(
getLoc(), resultType, partInfo.base.getBase(), "",
Expand Down
20 changes: 10 additions & 10 deletions flang/lib/Optimizer/CodeGen/CodeGen.cpp
Expand Up @@ -1618,7 +1618,7 @@ struct EmboxCommonConversion : public FIROpConversion<OP> {
mlir::Value base, mlir::Value outerOffset,
mlir::ValueRange cstInteriorIndices,
mlir::ValueRange componentIndices,
llvm::Optional<mlir::Value> substringOffset) const {
std::optional<mlir::Value> substringOffset) const {
llvm::SmallVector<mlir::LLVM::GEPArg> gepArgs{outerOffset};
mlir::Type resultTy =
base.getType().cast<mlir::LLVM::LLVMPointerType>().getElementType();
Expand Down Expand Up @@ -1907,7 +1907,7 @@ struct XEmboxOpConversion : public EmboxCommonConversion<fir::cg::XEmboxOp> {
if (hasSlice || hasSubcomp || hasSubstr) {
// Shift the base address.
llvm::SmallVector<mlir::Value> fieldIndices;
llvm::Optional<mlir::Value> substringOffset;
std::optional<mlir::Value> substringOffset;
if (hasSubcomp)
getSubcomponentIndices(xbox, xbox.getMemref(), operands, fieldIndices);
if (hasSubstr)
Expand Down Expand Up @@ -2047,7 +2047,7 @@ struct XReboxOpConversion : public EmboxCommonConversion<fir::cg::XReboxOp> {
base = rewriter.create<mlir::LLVM::BitcastOp>(loc, llvmElePtrTy, base);

llvm::SmallVector<mlir::Value> fieldIndices;
llvm::Optional<mlir::Value> substringOffset;
std::optional<mlir::Value> substringOffset;
if (!rebox.getSubcomponent().empty())
getSubcomponentIndices(rebox, rebox.getBox(), operands, fieldIndices);
if (!rebox.getSubstr().empty())
Expand Down Expand Up @@ -2725,7 +2725,7 @@ struct CoordinateOpConversion
if (hasKnownShape && hasSubdimension) {
offs.push_back(0);
}
llvm::Optional<int> dims;
std::optional<int> dims;
llvm::SmallVector<mlir::Value> arrIdx;
for (std::size_t i = 1, sz = operands.size(); i < sz; ++i) {
mlir::Value nxtOpnd = operands[i];
Expand Down Expand Up @@ -2930,7 +2930,7 @@ struct GlobalOpConversion : public FIROpConversion<fir::GlobalOp> {
// TODO: String comparaison should be avoided. Replace linkName with an
// enumeration.
mlir::LLVM::Linkage
convertLinkage(llvm::Optional<llvm::StringRef> optLinkage) const {
convertLinkage(std::optional<llvm::StringRef> optLinkage) const {
if (optLinkage) {
auto name = *optLinkage;
if (name == "internal")
Expand Down Expand Up @@ -3002,7 +3002,7 @@ struct NoReassocOpConversion : public FIROpConversion<fir::NoReassocOp> {
};

static void genCondBrOp(mlir::Location loc, mlir::Value cmp, mlir::Block *dest,
llvm::Optional<mlir::ValueRange> destOps,
std::optional<mlir::ValueRange> destOps,
mlir::ConversionPatternRewriter &rewriter,
mlir::Block *newBlock) {
if (destOps)
Expand All @@ -3013,7 +3013,7 @@ static void genCondBrOp(mlir::Location loc, mlir::Value cmp, mlir::Block *dest,
}

template <typename A, typename B>
static void genBrOp(A caseOp, mlir::Block *dest, llvm::Optional<B> destOps,
static void genBrOp(A caseOp, mlir::Block *dest, std::optional<B> destOps,
mlir::ConversionPatternRewriter &rewriter) {
if (destOps)
rewriter.replaceOpWithNewOp<mlir::LLVM::BrOp>(caseOp, *destOps, dest);
Expand All @@ -3023,7 +3023,7 @@ static void genBrOp(A caseOp, mlir::Block *dest, llvm::Optional<B> destOps,

static void genCaseLadderStep(mlir::Location loc, mlir::Value cmp,
mlir::Block *dest,
llvm::Optional<mlir::ValueRange> destOps,
std::optional<mlir::ValueRange> destOps,
mlir::ConversionPatternRewriter &rewriter) {
auto *thisBlock = rewriter.getInsertionBlock();
auto *newBlock = createBlock(rewriter, dest);
Expand Down Expand Up @@ -3069,9 +3069,9 @@ struct SelectCaseOpConversion : public FIROpConversion<fir::SelectCaseOp> {
auto loc = caseOp.getLoc();
for (unsigned t = 0; t != conds; ++t) {
mlir::Block *dest = caseOp.getSuccessor(t);
llvm::Optional<mlir::ValueRange> destOps =
std::optional<mlir::ValueRange> destOps =
caseOp.getSuccessorOperands(adaptor.getOperands(), t);
llvm::Optional<mlir::ValueRange> cmpOps =
std::optional<mlir::ValueRange> cmpOps =
*caseOp.getCompareOperands(adaptor.getOperands(), t);
mlir::Value caseArg = *(cmpOps.value().begin());
mlir::Attribute attr = cases[t];
Expand Down
42 changes: 21 additions & 21 deletions flang/lib/Optimizer/Dialect/FIROps.cpp
Expand Up @@ -2529,11 +2529,11 @@ getMutableSuccessorOperands(unsigned pos, mlir::MutableOperandRange operands,
mlir::MutableOperandRange::OperandSegment(pos, targetOffsetAttr));
}

llvm::Optional<mlir::OperandRange> fir::SelectOp::getCompareOperands(unsigned) {
std::optional<mlir::OperandRange> fir::SelectOp::getCompareOperands(unsigned) {
return {};
}

llvm::Optional<llvm::ArrayRef<mlir::Value>>
std::optional<llvm::ArrayRef<mlir::Value>>
fir::SelectOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) {
return {};
}
Expand All @@ -2543,7 +2543,7 @@ mlir::SuccessorOperands fir::SelectOp::getSuccessorOperands(unsigned oper) {
oper, getTargetArgsMutable(), getTargetOffsetAttr()));
}

llvm::Optional<llvm::ArrayRef<mlir::Value>>
std::optional<llvm::ArrayRef<mlir::Value>>
fir::SelectOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
unsigned oper) {
auto a =
Expand All @@ -2553,7 +2553,7 @@ fir::SelectOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
}

llvm::Optional<mlir::ValueRange>
std::optional<mlir::ValueRange>
fir::SelectOp::getSuccessorOperands(mlir::ValueRange operands, unsigned oper) {
auto a =
(*this)->getAttrOfType<mlir::DenseI32ArrayAttr>(getTargetOffsetAttr());
Expand All @@ -2572,14 +2572,14 @@ unsigned fir::SelectOp::targetOffsetSize() {
// SelectCaseOp
//===----------------------------------------------------------------------===//

llvm::Optional<mlir::OperandRange>
std::optional<mlir::OperandRange>
fir::SelectCaseOp::getCompareOperands(unsigned cond) {
auto a =
(*this)->getAttrOfType<mlir::DenseI32ArrayAttr>(getCompareOffsetAttr());
return {getSubOperands(cond, getCompareArgs(), a)};
}

llvm::Optional<llvm::ArrayRef<mlir::Value>>
std::optional<llvm::ArrayRef<mlir::Value>>
fir::SelectCaseOp::getCompareOperands(llvm::ArrayRef<mlir::Value> operands,
unsigned cond) {
auto a =
Expand All @@ -2589,7 +2589,7 @@ fir::SelectCaseOp::getCompareOperands(llvm::ArrayRef<mlir::Value> operands,
return {getSubOperands(cond, getSubOperands(1, operands, segments), a)};
}

llvm::Optional<mlir::ValueRange>
std::optional<mlir::ValueRange>
fir::SelectCaseOp::getCompareOperands(mlir::ValueRange operands,
unsigned cond) {
auto a =
Expand All @@ -2604,7 +2604,7 @@ mlir::SuccessorOperands fir::SelectCaseOp::getSuccessorOperands(unsigned oper) {
oper, getTargetArgsMutable(), getTargetOffsetAttr()));
}

llvm::Optional<llvm::ArrayRef<mlir::Value>>
std::optional<llvm::ArrayRef<mlir::Value>>
fir::SelectCaseOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
unsigned oper) {
auto a =
Expand All @@ -2614,7 +2614,7 @@ fir::SelectCaseOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
}

llvm::Optional<mlir::ValueRange>
std::optional<mlir::ValueRange>
fir::SelectCaseOp::getSuccessorOperands(mlir::ValueRange operands,
unsigned oper) {
auto a =
Expand Down Expand Up @@ -2864,12 +2864,12 @@ void fir::SelectRankOp::print(mlir::OpAsmPrinter &p) {
printIntegralSwitchTerminator(*this, p);
}

llvm::Optional<mlir::OperandRange>
std::optional<mlir::OperandRange>
fir::SelectRankOp::getCompareOperands(unsigned) {
return {};
}

llvm::Optional<llvm::ArrayRef<mlir::Value>>
std::optional<llvm::ArrayRef<mlir::Value>>
fir::SelectRankOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) {
return {};
}
Expand All @@ -2879,7 +2879,7 @@ mlir::SuccessorOperands fir::SelectRankOp::getSuccessorOperands(unsigned oper) {
oper, getTargetArgsMutable(), getTargetOffsetAttr()));
}

llvm::Optional<llvm::ArrayRef<mlir::Value>>
std::optional<llvm::ArrayRef<mlir::Value>>
fir::SelectRankOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
unsigned oper) {
auto a =
Expand All @@ -2889,7 +2889,7 @@ fir::SelectRankOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
}

llvm::Optional<mlir::ValueRange>
std::optional<mlir::ValueRange>
fir::SelectRankOp::getSuccessorOperands(mlir::ValueRange operands,
unsigned oper) {
auto a =
Expand All @@ -2909,12 +2909,12 @@ unsigned fir::SelectRankOp::targetOffsetSize() {
// SelectTypeOp
//===----------------------------------------------------------------------===//

llvm::Optional<mlir::OperandRange>
std::optional<mlir::OperandRange>
fir::SelectTypeOp::getCompareOperands(unsigned) {
return {};
}

llvm::Optional<llvm::ArrayRef<mlir::Value>>
std::optional<llvm::ArrayRef<mlir::Value>>
fir::SelectTypeOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) {
return {};
}
Expand All @@ -2924,7 +2924,7 @@ mlir::SuccessorOperands fir::SelectTypeOp::getSuccessorOperands(unsigned oper) {
oper, getTargetArgsMutable(), getTargetOffsetAttr()));
}

llvm::Optional<llvm::ArrayRef<mlir::Value>>
std::optional<llvm::ArrayRef<mlir::Value>>
fir::SelectTypeOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
unsigned oper) {
auto a =
Expand All @@ -2934,7 +2934,7 @@ fir::SelectTypeOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
}

llvm::Optional<mlir::ValueRange>
std::optional<mlir::ValueRange>
fir::SelectTypeOp::getSuccessorOperands(mlir::ValueRange operands,
unsigned oper) {
auto a =
Expand Down Expand Up @@ -3225,7 +3225,7 @@ mkNamedIntegerAttr(mlir::OpBuilder &builder, llvm::StringRef name, int64_t v) {
void fir::StringLitOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result,
fir::CharacterType inType, llvm::StringRef val,
llvm::Optional<int64_t> len) {
std::optional<int64_t> len) {
auto valAttr = builder.getNamedAttr(value(), builder.getStringAttr(val));
int64_t length = len ? *len : inType.getLen();
auto lenAttr = mkNamedIntegerAttr(builder, size(), length);
Expand All @@ -3247,7 +3247,7 @@ void fir::StringLitOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result,
fir::CharacterType inType,
llvm::ArrayRef<char> vlist,
llvm::Optional<std::int64_t> len) {
std::optional<std::int64_t> len) {
auto valAttr =
builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist));
std::int64_t length = len ? *len : inType.getLen();
Expand All @@ -3260,7 +3260,7 @@ void fir::StringLitOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result,
fir::CharacterType inType,
llvm::ArrayRef<char16_t> vlist,
llvm::Optional<std::int64_t> len) {
std::optional<std::int64_t> len) {
auto valAttr =
builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist));
std::int64_t length = len ? *len : inType.getLen();
Expand All @@ -3273,7 +3273,7 @@ void fir::StringLitOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result,
fir::CharacterType inType,
llvm::ArrayRef<char32_t> vlist,
llvm::Optional<std::int64_t> len) {
std::optional<std::int64_t> len) {
auto valAttr =
builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist));
std::int64_t length = len ? *len : inType.getLen();
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/Dialect/FortranVariableInterface.cpp
Expand Up @@ -57,7 +57,7 @@ fir::FortranVariableOpInterface::verifyDeclareLikeOpImpl(mlir::Value memref) {
shapeRank = shape.getType().cast<fir::ShiftType>().getRank();
}

llvm::Optional<unsigned> rank = getRank();
std::optional<unsigned> rank = getRank();
if (!rank || *rank != shapeRank)
return emitOpError("has conflicting shape and base operand ranks");
} else if (!sourceIsBox) {
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
Expand Up @@ -87,7 +87,7 @@ void hlfir::DesignateOp::build(
mlir::OpBuilder &builder, mlir::OperationState &result,
mlir::Type result_type, mlir::Value memref, llvm::StringRef component,
mlir::Value component_shape, llvm::ArrayRef<Subscript> subscripts,
mlir::ValueRange substring, llvm::Optional<bool> complex_part,
mlir::ValueRange substring, std::optional<bool> complex_part,
mlir::Value shape, mlir::ValueRange typeparams,
fir::FortranVariableFlagsAttr fortran_attrs) {
auto componentAttr =
Expand Down
15 changes: 8 additions & 7 deletions flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp
Expand Up @@ -397,7 +397,7 @@ class CfgSelectTypeConv : public OpConversionPattern<fir::SelectTypeOp> {

for (unsigned idx : orderedTypeGuards) {
auto *dest = selectType.getSuccessor(idx);
llvm::Optional<mlir::ValueRange> destOps =
std::optional<mlir::ValueRange> destOps =
selectType.getSuccessorOperands(operands, idx);
if (typeGuards[idx].dyn_cast<mlir::UnitAttr>())
rewriter.replaceOpWithNewOp<mlir::cf::BranchOp>(selectType, dest);
Expand Down Expand Up @@ -470,12 +470,13 @@ class CfgSelectTypeConv : public OpConversionPattern<fir::SelectTypeOp> {
return 0;
}

mlir::LogicalResult
genTypeLadderStep(mlir::Location loc, mlir::Value selector,
mlir::Attribute attr, mlir::Block *dest,
llvm::Optional<mlir::ValueRange> destOps,
mlir::ModuleOp mod, mlir::PatternRewriter &rewriter,
fir::KindMapping &kindMap) const {
mlir::LogicalResult genTypeLadderStep(mlir::Location loc,
mlir::Value selector,
mlir::Attribute attr, mlir::Block *dest,
std::optional<mlir::ValueRange> destOps,
mlir::ModuleOp mod,
mlir::PatternRewriter &rewriter,
fir::KindMapping &kindMap) const {
mlir::Value cmp;
// TYPE IS type guard comparison are all done inlined.
if (auto a = attr.dyn_cast<fir::ExactTypeAttr>()) {
Expand Down

0 comments on commit 2242611

Please sign in to comment.