[Mlir-commits] [mlir] ef764ee - [mlir:ElementsAttr] Avoid crash on empty contiguous ranges

River Riddle llvmlistbot at llvm.org
Fri Sep 24 16:57:10 PDT 2021


Author: River Riddle
Date: 2021-09-24T23:48:51Z
New Revision: ef764eeeb99a62f29d0a20c424328e7be2f5bfb8

URL: https://github.com/llvm/llvm-project/commit/ef764eeeb99a62f29d0a20c424328e7be2f5bfb8
DIFF: https://github.com/llvm/llvm-project/commit/ef764eeeb99a62f29d0a20c424328e7be2f5bfb8.diff

LOG: [mlir:ElementsAttr] Avoid crash on empty contiguous ranges

We currently, incorrectly, assume that a range always has at least
one element when building a contiguous range. This commit adds
a proper empty check to avoid crashing.

Differential Revision: https://reviews.llvm.org/D110457

Added: 
    

Modified: 
    mlir/include/mlir/IR/BuiltinAttributeInterfaces.td
    mlir/test/IR/elements-attr-interface.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/BuiltinAttributeInterfaces.td b/mlir/include/mlir/IR/BuiltinAttributeInterfaces.td
index 2df7603c004a..fa8cb28223a0 100644
--- a/mlir/include/mlir/IR/BuiltinAttributeInterfaces.td
+++ b/mlir/include/mlir/IR/BuiltinAttributeInterfaces.td
@@ -257,6 +257,11 @@ def ElementsAttrInterface : AttrInterface<"ElementsAttr"> {
     template <typename T>
     ::mlir::FailureOr<::mlir::detail::ElementsAttrIndexer> buildValueResult(
         /*isContiguous*/std::true_type) const {
+      if ($_attr.empty()) {
+        return ::mlir::detail::ElementsAttrIndexer::contiguous<T>(
+          /*isSplat=*/false, nullptr);
+      }
+
       auto valueIt = $_attr.value_begin_impl(OverloadToken<T>());
       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
index a9b00d2339b0..3191b647dcf7 100644
--- a/mlir/test/IR/elements-attr-interface.mlir
+++ b/mlir/test/IR/elements-attr-interface.mlir
@@ -19,3 +19,9 @@ std.constant dense<[10, 11, 12, 13, 14]> : tensor<5xi64>
 // expected-error at below {{Test iterating `APInt`: unable to iterate type}}
 // expected-error at 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 at below {{Test iterating `uint64_t`: }}
+// expected-error at below {{Test iterating `APInt`: }}
+// expected-error at below {{Test iterating `IntegerAttr`: }}
+std.constant dense<> : tensor<0xi64>


        


More information about the Mlir-commits mailing list