From e6ebf8f99ba27d60026c7473b0cd5e24c855b018 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sat, 21 Jun 2025 08:20:49 -0700 Subject: [PATCH] [mlir] Migrate away from ArrayRef(std::nullopt) (NFC) (#145140) 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 takes care of the mlir side of the migration, starting with straightforward places like "return std::nullopt;" and ternally expressions involving std::nullopt. --- mlir/include/mlir/CAPI/Wrap.h | 2 +- mlir/include/mlir/Dialect/PDL/IR/PDLOps.td | 13 +++++++------ mlir/include/mlir/IR/BuiltinAttributes.td | 5 ++--- mlir/include/mlir/Support/StorageUniquer.h | 2 +- mlir/lib/Interfaces/FunctionInterfaces.cpp | 4 ++-- mlir/lib/Tools/PDLL/Parser/Parser.cpp | 9 ++++++--- mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp | 6 ++++-- mlir/test/lib/Dialect/Test/TestAttributes.cpp | 2 +- 8 files changed, 24 insertions(+), 19 deletions(-) diff --git a/mlir/include/mlir/CAPI/Wrap.h b/mlir/include/mlir/CAPI/Wrap.h index 5b68f417a3df..fd5b6e18d495 100644 --- a/mlir/include/mlir/CAPI/Wrap.h +++ b/mlir/include/mlir/CAPI/Wrap.h @@ -44,7 +44,7 @@ static llvm::ArrayRef unwrapList(size_t size, CTy *first, "incompatible C and C++ types"); if (size == 0) - return std::nullopt; + return {}; assert(storage.empty() && "expected to populate storage"); storage.reserve(size); diff --git a/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td b/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td index 1e108c3d8ac7..6ee638c19d1a 100644 --- a/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td +++ b/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td @@ -360,12 +360,13 @@ def PDL_OperationOp : PDL_Op<"operation", [AttrSizedOperandSegments]> { (`->` `(` $typeValues^ `:` type($typeValues) `)`)? attr-dict }]; - let builders = [ - OpBuilder<(ins CArg<"std::optional", "std::nullopt">:$name, - CArg<"ValueRange", "std::nullopt">:$operandValues, - CArg<"ArrayRef", "std::nullopt">:$attrNames, - CArg<"ValueRange", "std::nullopt">:$attrValues, - CArg<"ValueRange", "std::nullopt">:$resultTypes), [{ + let builders = + [OpBuilder<(ins CArg<"std::optional", "std::nullopt">:$name, + CArg<"ValueRange", "{}">:$operandValues, + CArg<"ArrayRef", "{}">:$attrNames, + CArg<"ValueRange", "{}">:$attrValues, + CArg<"ValueRange", "{}">:$resultTypes), + [{ auto nameAttr = name ? $_builder.getStringAttr(*name) : StringAttr(); build($_builder, $_state, $_builder.getType(), nameAttr, operandValues, attrValues, $_builder.getStrArrayAttr(attrNames), diff --git a/mlir/include/mlir/IR/BuiltinAttributes.td b/mlir/include/mlir/IR/BuiltinAttributes.td index 885590827650..b67b8f939b9e 100644 --- a/mlir/include/mlir/IR/BuiltinAttributes.td +++ b/mlir/include/mlir/IR/BuiltinAttributes.td @@ -535,9 +535,8 @@ def Builtin_DictionaryAttr : Builtin_Attr<"Dictionary", "dictionary"> { ``` }]; let parameters = (ins ArrayRefParameter<"NamedAttribute", "">:$value); - let builders = [ - AttrBuilder<(ins CArg<"ArrayRef", "std::nullopt">:$value)> - ]; + let builders = [AttrBuilder<( + ins CArg<"ArrayRef", "{}">:$value)>]; let extraClassDeclaration = [{ using ValueType = ArrayRef; diff --git a/mlir/include/mlir/Support/StorageUniquer.h b/mlir/include/mlir/Support/StorageUniquer.h index 6756c4390276..b5f4df680ac6 100644 --- a/mlir/include/mlir/Support/StorageUniquer.h +++ b/mlir/include/mlir/Support/StorageUniquer.h @@ -97,7 +97,7 @@ public: template ArrayRef copyInto(ArrayRef elements) { if (elements.empty()) - return std::nullopt; + return {}; auto result = allocator.Allocate(elements.size()); llvm::uninitialized_copy(elements, result); return ArrayRef(result, elements.size()); diff --git a/mlir/lib/Interfaces/FunctionInterfaces.cpp b/mlir/lib/Interfaces/FunctionInterfaces.cpp index 57a8668117c6..e0f1135e992a 100644 --- a/mlir/lib/Interfaces/FunctionInterfaces.cpp +++ b/mlir/lib/Interfaces/FunctionInterfaces.cpp @@ -44,14 +44,14 @@ function_interface_impl::getResultAttrDict(FunctionOpInterface op, ArrayRef function_interface_impl::getArgAttrs(FunctionOpInterface op, unsigned index) { auto argDict = getArgAttrDict(op, index); - return argDict ? argDict.getValue() : std::nullopt; + return argDict ? argDict.getValue() : ArrayRef(); } ArrayRef function_interface_impl::getResultAttrs(FunctionOpInterface op, unsigned index) { auto resultDict = getResultAttrDict(op, index); - return resultDict ? resultDict.getValue() : std::nullopt; + return resultDict ? resultDict.getValue() : ArrayRef(); } /// Get either the argument or result attributes array. diff --git a/mlir/lib/Tools/PDLL/Parser/Parser.cpp b/mlir/lib/Tools/PDLL/Parser/Parser.cpp index c0e2252bdebc..ad7d71b0bdd2 100644 --- a/mlir/lib/Tools/PDLL/Parser/Parser.cpp +++ b/mlir/lib/Tools/PDLL/Parser/Parser.cpp @@ -2883,8 +2883,9 @@ Parser::validateOperationOperands(SMRange loc, std::optional name, SmallVectorImpl &operands) { return validateOperationOperandsOrResults( "operand", loc, odsOp ? odsOp->getLoc() : std::optional(), name, - operands, odsOp ? odsOp->getOperands() : std::nullopt, valueTy, - valueRangeTy); + operands, + odsOp ? odsOp->getOperands() : ArrayRef(), + valueTy, valueRangeTy); } LogicalResult @@ -2893,7 +2894,9 @@ Parser::validateOperationResults(SMRange loc, std::optional name, SmallVectorImpl &results) { return validateOperationOperandsOrResults( "result", loc, odsOp ? odsOp->getLoc() : std::optional(), name, - results, odsOp ? odsOp->getResults() : std::nullopt, typeTy, typeRangeTy); + results, + odsOp ? odsOp->getResults() : ArrayRef(), + typeTy, typeRangeTy); } void Parser::checkOperationResultTypeInferrence(SMRange loc, StringRef opName, diff --git a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp index c39540da396b..84f529ae1640 100644 --- a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp +++ b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp @@ -1044,7 +1044,8 @@ public: const ods::Operation *odsOp = opName ? odsContext.lookupOperation(*opName) : nullptr; codeCompleteOperationOperandOrResultSignature( - opName, odsOp, odsOp ? odsOp->getOperands() : std::nullopt, + opName, odsOp, + odsOp ? odsOp->getOperands() : ArrayRef(), currentNumOperands, "operand", "Value"); } @@ -1053,7 +1054,8 @@ public: const ods::Operation *odsOp = opName ? odsContext.lookupOperation(*opName) : nullptr; codeCompleteOperationOperandOrResultSignature( - opName, odsOp, odsOp ? odsOp->getResults() : std::nullopt, + opName, odsOp, + odsOp ? odsOp->getResults() : ArrayRef(), currentNumResults, "result", "Type"); } diff --git a/mlir/test/lib/Dialect/Test/TestAttributes.cpp b/mlir/test/lib/Dialect/Test/TestAttributes.cpp index 80661e68754c..4f6655d0b297 100644 --- a/mlir/test/lib/Dialect/Test/TestAttributes.cpp +++ b/mlir/test/lib/Dialect/Test/TestAttributes.cpp @@ -194,7 +194,7 @@ void TestSubElementsAccessAttr::print(::mlir::AsmPrinter &printer) const { ArrayRef TestExtern1DI64ElementsAttr::getElements() const { if (auto *blob = getHandle().getBlob()) return blob->getDataAs(); - return std::nullopt; + return {}; } //===----------------------------------------------------------------------===//