diff --git a/mlir/include/mlir/IR/BuiltinAttributeInterfaces.td b/mlir/include/mlir/IR/BuiltinAttributeInterfaces.td --- a/mlir/include/mlir/IR/BuiltinAttributeInterfaces.td +++ b/mlir/include/mlir/IR/BuiltinAttributeInterfaces.td @@ -417,8 +417,6 @@ /// return the iterable range. Otherwise, return std::nullopt. template DefaultValueCheckT>> tryGetValues() const { - if (empty()) - return std::nullopt; if (std::optional> beginIt = try_value_begin()) return iterator_range(getShapedType(), *beginIt, value_end()); return std::nullopt; @@ -431,14 +429,13 @@ template > std::optional> tryGetValues() const { auto values = tryGetValues(); - if (empty()) - return std::nullopt; if (!values) return std::nullopt; // Check that an element can be casted as 'T', which is a derived attribute. - if (!(*(value_begin())).dyn_cast()) - return std::nullopt; + if (!empty()) + if (!(*(value_begin())).dyn_cast()) + return std::nullopt; auto castFn = [](Attribute attr) { return ::llvm::cast(attr); }; return DerivedAttrValueIteratorRange( diff --git a/mlir/test/IR/elements-attr-interface.mlir b/mlir/test/IR/elements-attr-interface.mlir --- a/mlir/test/IR/elements-attr-interface.mlir +++ b/mlir/test/IR/elements-attr-interface.mlir @@ -27,10 +27,10 @@ arith.constant dense<[10, 11, 12, 13, 14]> : tensor<5xi64> // Check that we don't crash on empty element attributes. -// expected-error@below {{Test iterating `int64_t`: unable to iterate type}} -// expected-error@below {{Test iterating `uint64_t`: unable to iterate type}} -// expected-error@below {{Test iterating `APInt`: unable to iterate type}} -// expected-error@below {{Test iterating `IntegerAttr`: unable to iterate type}} +// expected-error@below {{Test iterating `int64_t`: }} +// expected-error@below {{Test iterating `uint64_t`: }} +// expected-error@below {{Test iterating `APInt`: }} +// expected-error@below {{Test iterating `IntegerAttr`: }} arith.constant dense<> : tensor<0xi64> // Check that we handle an external constant parsed from the config.