[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.
This commit is contained in:
Kazu Hirata 2025-06-21 08:20:49 -07:00 committed by GitHub
parent 99af99c665
commit e6ebf8f99b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 24 additions and 19 deletions

View File

@ -44,7 +44,7 @@ static llvm::ArrayRef<CppTy> unwrapList(size_t size, CTy *first,
"incompatible C and C++ types"); "incompatible C and C++ types");
if (size == 0) if (size == 0)
return std::nullopt; return {};
assert(storage.empty() && "expected to populate storage"); assert(storage.empty() && "expected to populate storage");
storage.reserve(size); storage.reserve(size);

View File

@ -360,12 +360,13 @@ def PDL_OperationOp : PDL_Op<"operation", [AttrSizedOperandSegments]> {
(`->` `(` $typeValues^ `:` type($typeValues) `)`)? attr-dict (`->` `(` $typeValues^ `:` type($typeValues) `)`)? attr-dict
}]; }];
let builders = [ let builders =
OpBuilder<(ins CArg<"std::optional<StringRef>", "std::nullopt">:$name, [OpBuilder<(ins CArg<"std::optional<StringRef>", "std::nullopt">:$name,
CArg<"ValueRange", "std::nullopt">:$operandValues, CArg<"ValueRange", "{}">:$operandValues,
CArg<"ArrayRef<StringRef>", "std::nullopt">:$attrNames, CArg<"ArrayRef<StringRef>", "{}">:$attrNames,
CArg<"ValueRange", "std::nullopt">:$attrValues, CArg<"ValueRange", "{}">:$attrValues,
CArg<"ValueRange", "std::nullopt">:$resultTypes), [{ CArg<"ValueRange", "{}">:$resultTypes),
[{
auto nameAttr = name ? $_builder.getStringAttr(*name) : StringAttr(); auto nameAttr = name ? $_builder.getStringAttr(*name) : StringAttr();
build($_builder, $_state, $_builder.getType<OperationType>(), nameAttr, build($_builder, $_state, $_builder.getType<OperationType>(), nameAttr,
operandValues, attrValues, $_builder.getStrArrayAttr(attrNames), operandValues, attrValues, $_builder.getStrArrayAttr(attrNames),

View File

@ -535,9 +535,8 @@ def Builtin_DictionaryAttr : Builtin_Attr<"Dictionary", "dictionary"> {
``` ```
}]; }];
let parameters = (ins ArrayRefParameter<"NamedAttribute", "">:$value); let parameters = (ins ArrayRefParameter<"NamedAttribute", "">:$value);
let builders = [ let builders = [AttrBuilder<(
AttrBuilder<(ins CArg<"ArrayRef<NamedAttribute>", "std::nullopt">:$value)> ins CArg<"ArrayRef<NamedAttribute>", "{}">:$value)>];
];
let extraClassDeclaration = [{ let extraClassDeclaration = [{
using ValueType = ArrayRef<NamedAttribute>; using ValueType = ArrayRef<NamedAttribute>;

View File

@ -97,7 +97,7 @@ public:
template <typename T> template <typename T>
ArrayRef<T> copyInto(ArrayRef<T> elements) { ArrayRef<T> copyInto(ArrayRef<T> elements) {
if (elements.empty()) if (elements.empty())
return std::nullopt; return {};
auto result = allocator.Allocate<T>(elements.size()); auto result = allocator.Allocate<T>(elements.size());
llvm::uninitialized_copy(elements, result); llvm::uninitialized_copy(elements, result);
return ArrayRef<T>(result, elements.size()); return ArrayRef<T>(result, elements.size());

View File

@ -44,14 +44,14 @@ function_interface_impl::getResultAttrDict(FunctionOpInterface op,
ArrayRef<NamedAttribute> ArrayRef<NamedAttribute>
function_interface_impl::getArgAttrs(FunctionOpInterface op, unsigned index) { function_interface_impl::getArgAttrs(FunctionOpInterface op, unsigned index) {
auto argDict = getArgAttrDict(op, index); auto argDict = getArgAttrDict(op, index);
return argDict ? argDict.getValue() : std::nullopt; return argDict ? argDict.getValue() : ArrayRef<NamedAttribute>();
} }
ArrayRef<NamedAttribute> ArrayRef<NamedAttribute>
function_interface_impl::getResultAttrs(FunctionOpInterface op, function_interface_impl::getResultAttrs(FunctionOpInterface op,
unsigned index) { unsigned index) {
auto resultDict = getResultAttrDict(op, index); auto resultDict = getResultAttrDict(op, index);
return resultDict ? resultDict.getValue() : std::nullopt; return resultDict ? resultDict.getValue() : ArrayRef<NamedAttribute>();
} }
/// Get either the argument or result attributes array. /// Get either the argument or result attributes array.

View File

@ -2883,8 +2883,9 @@ Parser::validateOperationOperands(SMRange loc, std::optional<StringRef> name,
SmallVectorImpl<ast::Expr *> &operands) { SmallVectorImpl<ast::Expr *> &operands) {
return validateOperationOperandsOrResults( return validateOperationOperandsOrResults(
"operand", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name, "operand", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name,
operands, odsOp ? odsOp->getOperands() : std::nullopt, valueTy, operands,
valueRangeTy); odsOp ? odsOp->getOperands() : ArrayRef<pdll::ods::OperandOrResult>(),
valueTy, valueRangeTy);
} }
LogicalResult LogicalResult
@ -2893,7 +2894,9 @@ Parser::validateOperationResults(SMRange loc, std::optional<StringRef> name,
SmallVectorImpl<ast::Expr *> &results) { SmallVectorImpl<ast::Expr *> &results) {
return validateOperationOperandsOrResults( return validateOperationOperandsOrResults(
"result", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name, "result", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name,
results, odsOp ? odsOp->getResults() : std::nullopt, typeTy, typeRangeTy); results,
odsOp ? odsOp->getResults() : ArrayRef<pdll::ods::OperandOrResult>(),
typeTy, typeRangeTy);
} }
void Parser::checkOperationResultTypeInferrence(SMRange loc, StringRef opName, void Parser::checkOperationResultTypeInferrence(SMRange loc, StringRef opName,

View File

@ -1044,7 +1044,8 @@ public:
const ods::Operation *odsOp = const ods::Operation *odsOp =
opName ? odsContext.lookupOperation(*opName) : nullptr; opName ? odsContext.lookupOperation(*opName) : nullptr;
codeCompleteOperationOperandOrResultSignature( codeCompleteOperationOperandOrResultSignature(
opName, odsOp, odsOp ? odsOp->getOperands() : std::nullopt, opName, odsOp,
odsOp ? odsOp->getOperands() : ArrayRef<ods::OperandOrResult>(),
currentNumOperands, "operand", "Value"); currentNumOperands, "operand", "Value");
} }
@ -1053,7 +1054,8 @@ public:
const ods::Operation *odsOp = const ods::Operation *odsOp =
opName ? odsContext.lookupOperation(*opName) : nullptr; opName ? odsContext.lookupOperation(*opName) : nullptr;
codeCompleteOperationOperandOrResultSignature( codeCompleteOperationOperandOrResultSignature(
opName, odsOp, odsOp ? odsOp->getResults() : std::nullopt, opName, odsOp,
odsOp ? odsOp->getResults() : ArrayRef<ods::OperandOrResult>(),
currentNumResults, "result", "Type"); currentNumResults, "result", "Type");
} }

View File

@ -194,7 +194,7 @@ void TestSubElementsAccessAttr::print(::mlir::AsmPrinter &printer) const {
ArrayRef<uint64_t> TestExtern1DI64ElementsAttr::getElements() const { ArrayRef<uint64_t> TestExtern1DI64ElementsAttr::getElements() const {
if (auto *blob = getHandle().getBlob()) if (auto *blob = getHandle().getBlob())
return blob->getDataAs<uint64_t>(); return blob->getDataAs<uint64_t>();
return std::nullopt; return {};
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//