[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.

GitOrigin-RevId: e6ebf8f99ba27d60026c7473b0cd5e24c855b018
diff --git a/include/mlir/CAPI/Wrap.h b/include/mlir/CAPI/Wrap.h
index 5b68f41..fd5b6e1 100644
--- a/include/mlir/CAPI/Wrap.h
+++ b/include/mlir/CAPI/Wrap.h
@@ -44,7 +44,7 @@
       "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/include/mlir/Dialect/PDL/IR/PDLOps.td b/include/mlir/Dialect/PDL/IR/PDLOps.td
index 1e108c3..6ee638c 100644
--- a/include/mlir/Dialect/PDL/IR/PDLOps.td
+++ b/include/mlir/Dialect/PDL/IR/PDLOps.td
@@ -360,12 +360,13 @@
     (`->` `(` $typeValues^ `:` type($typeValues) `)`)? attr-dict
   }];
 
-  let builders = [
-    OpBuilder<(ins CArg<"std::optional<StringRef>", "std::nullopt">:$name,
-      CArg<"ValueRange", "std::nullopt">:$operandValues,
-      CArg<"ArrayRef<StringRef>", "std::nullopt">:$attrNames,
-      CArg<"ValueRange", "std::nullopt">:$attrValues,
-      CArg<"ValueRange", "std::nullopt">:$resultTypes), [{
+  let builders =
+      [OpBuilder<(ins CArg<"std::optional<StringRef>", "std::nullopt">:$name,
+                     CArg<"ValueRange", "{}">:$operandValues,
+                     CArg<"ArrayRef<StringRef>", "{}">:$attrNames,
+                     CArg<"ValueRange", "{}">:$attrValues,
+                     CArg<"ValueRange", "{}">:$resultTypes),
+                 [{
       auto nameAttr = name ? $_builder.getStringAttr(*name) : StringAttr();
       build($_builder, $_state, $_builder.getType<OperationType>(), nameAttr,
             operandValues, attrValues, $_builder.getStrArrayAttr(attrNames),
diff --git a/include/mlir/IR/BuiltinAttributes.td b/include/mlir/IR/BuiltinAttributes.td
index 8855908..b67b8f9 100644
--- a/include/mlir/IR/BuiltinAttributes.td
+++ b/include/mlir/IR/BuiltinAttributes.td
@@ -535,9 +535,8 @@
     ```
   }];
   let parameters = (ins ArrayRefParameter<"NamedAttribute", "">:$value);
-  let builders = [
-    AttrBuilder<(ins CArg<"ArrayRef<NamedAttribute>", "std::nullopt">:$value)>
-  ];
+  let builders = [AttrBuilder<(
+      ins CArg<"ArrayRef<NamedAttribute>", "{}">:$value)>];
   let extraClassDeclaration = [{
     using ValueType = ArrayRef<NamedAttribute>;
 
diff --git a/include/mlir/Support/StorageUniquer.h b/include/mlir/Support/StorageUniquer.h
index 6756c43..b5f4df6 100644
--- a/include/mlir/Support/StorageUniquer.h
+++ b/include/mlir/Support/StorageUniquer.h
@@ -97,7 +97,7 @@
     template <typename T>
     ArrayRef<T> copyInto(ArrayRef<T> elements) {
       if (elements.empty())
-        return std::nullopt;
+        return {};
       auto result = allocator.Allocate<T>(elements.size());
       llvm::uninitialized_copy(elements, result);
       return ArrayRef<T>(result, elements.size());
diff --git a/lib/Interfaces/FunctionInterfaces.cpp b/lib/Interfaces/FunctionInterfaces.cpp
index 57a8668..e0f1135 100644
--- a/lib/Interfaces/FunctionInterfaces.cpp
+++ b/lib/Interfaces/FunctionInterfaces.cpp
@@ -44,14 +44,14 @@
 ArrayRef<NamedAttribute>
 function_interface_impl::getArgAttrs(FunctionOpInterface op, unsigned index) {
   auto argDict = getArgAttrDict(op, index);
-  return argDict ? argDict.getValue() : std::nullopt;
+  return argDict ? argDict.getValue() : ArrayRef<NamedAttribute>();
 }
 
 ArrayRef<NamedAttribute>
 function_interface_impl::getResultAttrs(FunctionOpInterface op,
                                         unsigned 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.
diff --git a/lib/Tools/PDLL/Parser/Parser.cpp b/lib/Tools/PDLL/Parser/Parser.cpp
index c0e2252..ad7d71b 100644
--- a/lib/Tools/PDLL/Parser/Parser.cpp
+++ b/lib/Tools/PDLL/Parser/Parser.cpp
@@ -2883,8 +2883,9 @@
                                   SmallVectorImpl<ast::Expr *> &operands) {
   return validateOperationOperandsOrResults(
       "operand", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name,
-      operands, odsOp ? odsOp->getOperands() : std::nullopt, valueTy,
-      valueRangeTy);
+      operands,
+      odsOp ? odsOp->getOperands() : ArrayRef<pdll::ods::OperandOrResult>(),
+      valueTy, valueRangeTy);
 }
 
 LogicalResult
@@ -2893,7 +2894,9 @@
                                  SmallVectorImpl<ast::Expr *> &results) {
   return validateOperationOperandsOrResults(
       "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,
diff --git a/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp b/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
index c39540d..84f529a 100644
--- a/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
+++ b/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
@@ -1044,7 +1044,8 @@
     const ods::Operation *odsOp =
         opName ? odsContext.lookupOperation(*opName) : nullptr;
     codeCompleteOperationOperandOrResultSignature(
-        opName, odsOp, odsOp ? odsOp->getOperands() : std::nullopt,
+        opName, odsOp,
+        odsOp ? odsOp->getOperands() : ArrayRef<ods::OperandOrResult>(),
         currentNumOperands, "operand", "Value");
   }
 
@@ -1053,7 +1054,8 @@
     const ods::Operation *odsOp =
         opName ? odsContext.lookupOperation(*opName) : nullptr;
     codeCompleteOperationOperandOrResultSignature(
-        opName, odsOp, odsOp ? odsOp->getResults() : std::nullopt,
+        opName, odsOp,
+        odsOp ? odsOp->getResults() : ArrayRef<ods::OperandOrResult>(),
         currentNumResults, "result", "Type");
   }
 
diff --git a/test/lib/Dialect/Test/TestAttributes.cpp b/test/lib/Dialect/Test/TestAttributes.cpp
index 80661e6..4f6655d 100644
--- a/test/lib/Dialect/Test/TestAttributes.cpp
+++ b/test/lib/Dialect/Test/TestAttributes.cpp
@@ -194,7 +194,7 @@
 ArrayRef<uint64_t> TestExtern1DI64ElementsAttr::getElements() const {
   if (auto *blob = getHandle().getBlob())
     return blob->getDataAs<uint64_t>();
-  return std::nullopt;
+  return {};
 }
 
 //===----------------------------------------------------------------------===//