Skip to content

Commit e6ebf8f

Browse files
[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.
1 parent 99af99c commit e6ebf8f

File tree

8 files changed

+24
-19
lines changed

8 files changed

+24
-19
lines changed

mlir/include/mlir/CAPI/Wrap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static llvm::ArrayRef<CppTy> unwrapList(size_t size, CTy *first,
4444
"incompatible C and C++ types");
4545

4646
if (size == 0)
47-
return std::nullopt;
47+
return {};
4848

4949
assert(storage.empty() && "expected to populate storage");
5050
storage.reserve(size);

mlir/include/mlir/Dialect/PDL/IR/PDLOps.td

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,13 @@ def PDL_OperationOp : PDL_Op<"operation", [AttrSizedOperandSegments]> {
360360
(`->` `(` $typeValues^ `:` type($typeValues) `)`)? attr-dict
361361
}];
362362

363-
let builders = [
364-
OpBuilder<(ins CArg<"std::optional<StringRef>", "std::nullopt">:$name,
365-
CArg<"ValueRange", "std::nullopt">:$operandValues,
366-
CArg<"ArrayRef<StringRef>", "std::nullopt">:$attrNames,
367-
CArg<"ValueRange", "std::nullopt">:$attrValues,
368-
CArg<"ValueRange", "std::nullopt">:$resultTypes), [{
363+
let builders =
364+
[OpBuilder<(ins CArg<"std::optional<StringRef>", "std::nullopt">:$name,
365+
CArg<"ValueRange", "{}">:$operandValues,
366+
CArg<"ArrayRef<StringRef>", "{}">:$attrNames,
367+
CArg<"ValueRange", "{}">:$attrValues,
368+
CArg<"ValueRange", "{}">:$resultTypes),
369+
[{
369370
auto nameAttr = name ? $_builder.getStringAttr(*name) : StringAttr();
370371
build($_builder, $_state, $_builder.getType<OperationType>(), nameAttr,
371372
operandValues, attrValues, $_builder.getStrArrayAttr(attrNames),

mlir/include/mlir/IR/BuiltinAttributes.td

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,8 @@ def Builtin_DictionaryAttr : Builtin_Attr<"Dictionary", "dictionary"> {
535535
```
536536
}];
537537
let parameters = (ins ArrayRefParameter<"NamedAttribute", "">:$value);
538-
let builders = [
539-
AttrBuilder<(ins CArg<"ArrayRef<NamedAttribute>", "std::nullopt">:$value)>
540-
];
538+
let builders = [AttrBuilder<(
539+
ins CArg<"ArrayRef<NamedAttribute>", "{}">:$value)>];
541540
let extraClassDeclaration = [{
542541
using ValueType = ArrayRef<NamedAttribute>;
543542

mlir/include/mlir/Support/StorageUniquer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class StorageUniquer {
9797
template <typename T>
9898
ArrayRef<T> copyInto(ArrayRef<T> elements) {
9999
if (elements.empty())
100-
return std::nullopt;
100+
return {};
101101
auto result = allocator.Allocate<T>(elements.size());
102102
llvm::uninitialized_copy(elements, result);
103103
return ArrayRef<T>(result, elements.size());

mlir/lib/Interfaces/FunctionInterfaces.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ function_interface_impl::getResultAttrDict(FunctionOpInterface op,
4444
ArrayRef<NamedAttribute>
4545
function_interface_impl::getArgAttrs(FunctionOpInterface op, unsigned index) {
4646
auto argDict = getArgAttrDict(op, index);
47-
return argDict ? argDict.getValue() : std::nullopt;
47+
return argDict ? argDict.getValue() : ArrayRef<NamedAttribute>();
4848
}
4949

5050
ArrayRef<NamedAttribute>
5151
function_interface_impl::getResultAttrs(FunctionOpInterface op,
5252
unsigned index) {
5353
auto resultDict = getResultAttrDict(op, index);
54-
return resultDict ? resultDict.getValue() : std::nullopt;
54+
return resultDict ? resultDict.getValue() : ArrayRef<NamedAttribute>();
5555
}
5656

5757
/// Get either the argument or result attributes array.

mlir/lib/Tools/PDLL/Parser/Parser.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,8 +2883,9 @@ Parser::validateOperationOperands(SMRange loc, std::optional<StringRef> name,
28832883
SmallVectorImpl<ast::Expr *> &operands) {
28842884
return validateOperationOperandsOrResults(
28852885
"operand", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name,
2886-
operands, odsOp ? odsOp->getOperands() : std::nullopt, valueTy,
2887-
valueRangeTy);
2886+
operands,
2887+
odsOp ? odsOp->getOperands() : ArrayRef<pdll::ods::OperandOrResult>(),
2888+
valueTy, valueRangeTy);
28882889
}
28892890

28902891
LogicalResult
@@ -2893,7 +2894,9 @@ Parser::validateOperationResults(SMRange loc, std::optional<StringRef> name,
28932894
SmallVectorImpl<ast::Expr *> &results) {
28942895
return validateOperationOperandsOrResults(
28952896
"result", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name,
2896-
results, odsOp ? odsOp->getResults() : std::nullopt, typeTy, typeRangeTy);
2897+
results,
2898+
odsOp ? odsOp->getResults() : ArrayRef<pdll::ods::OperandOrResult>(),
2899+
typeTy, typeRangeTy);
28972900
}
28982901

28992902
void Parser::checkOperationResultTypeInferrence(SMRange loc, StringRef opName,

mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,8 @@ class LSPSignatureHelpContext : public CodeCompleteContext {
10441044
const ods::Operation *odsOp =
10451045
opName ? odsContext.lookupOperation(*opName) : nullptr;
10461046
codeCompleteOperationOperandOrResultSignature(
1047-
opName, odsOp, odsOp ? odsOp->getOperands() : std::nullopt,
1047+
opName, odsOp,
1048+
odsOp ? odsOp->getOperands() : ArrayRef<ods::OperandOrResult>(),
10481049
currentNumOperands, "operand", "Value");
10491050
}
10501051

@@ -1053,7 +1054,8 @@ class LSPSignatureHelpContext : public CodeCompleteContext {
10531054
const ods::Operation *odsOp =
10541055
opName ? odsContext.lookupOperation(*opName) : nullptr;
10551056
codeCompleteOperationOperandOrResultSignature(
1056-
opName, odsOp, odsOp ? odsOp->getResults() : std::nullopt,
1057+
opName, odsOp,
1058+
odsOp ? odsOp->getResults() : ArrayRef<ods::OperandOrResult>(),
10571059
currentNumResults, "result", "Type");
10581060
}
10591061

mlir/test/lib/Dialect/Test/TestAttributes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void TestSubElementsAccessAttr::print(::mlir::AsmPrinter &printer) const {
194194
ArrayRef<uint64_t> TestExtern1DI64ElementsAttr::getElements() const {
195195
if (auto *blob = getHandle().getBlob())
196196
return blob->getDataAs<uint64_t>();
197-
return std::nullopt;
197+
return {};
198198
}
199199

200200
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)