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 @@ -257,6 +257,11 @@ template ::mlir::FailureOr<::mlir::detail::ElementsAttrIndexer> buildValueResult( /*isContiguous*/std::true_type) const { + if ($_attr.empty()) { + return ::mlir::detail::ElementsAttrIndexer::contiguous( + /*isSplat=*/false, nullptr); + } + auto valueIt = $_attr.value_begin_impl(OverloadToken()); return ::mlir::detail::ElementsAttrIndexer::contiguous( $_attr.isSplat(), &*valueIt); 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 @@ -19,3 +19,9 @@ // expected-error@below {{Test iterating `APInt`: unable to iterate type}} // expected-error@below {{Test iterating `IntegerAttr`: unable to iterate type}} std.constant opaque<"_", "0xDEADBEEF"> : tensor<5xi64> + +// Check that we don't crash on empty element attributes. +// expected-error@below {{Test iterating `uint64_t`: }} +// expected-error@below {{Test iterating `APInt`: }} +// expected-error@below {{Test iterating `IntegerAttr`: }} +std.constant dense<> : tensor<0xi64>