[clang-tools-extra] [mlir] Verify TestBuiltinAttributeInterfaces eltype (PR #69878)

Rik Huijzer via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 22 05:53:34 PDT 2023


https://github.com/rikhuijzer created https://github.com/llvm/llvm-project/pull/69878

Fixes https://github.com/llvm/llvm-project/issues/61871.

This PR fixes two small things. First and foremost, it throws a clear error in the `-test-elements-attr-interface` when those tests are called on elements which are not an integer. I've looked through the introduction of the attribute interface (https://reviews.llvm.org/D109190) and later commits and see no evidence that the interface (`attr.tryGetValues<T>()`) is expected to handle mismatching types. 

For example, the case which is given in the issue is:
```mlir
arith.constant sparse<[[0, 0, 5]],  -2.0> : vector<1x1x10xf16>
```
So, a sparse vector containing `f16` elements. This will crash at various locations when called in the test because the test introduces integer types (`int64_t`, `uint64_t`, `APInt`, `IntegerAttr`), but as I said in the previous paragraph: I see no reason to believe that the interface is wrong here. The interface assumes that clients don't do things like
`attr.tryGetValues<APInt>()` on a floating point `attr`.

Also I've added a test for the implementation of this interface by the `sparse` dialect. There were no problems there. Still, probably good to increase code coverage on that one (also, to avoid new issue reports like https://github.com/llvm/llvm-project/issues/61871).

>From 1d27516523cde11767705396b0ea5a9e1f264f50 Mon Sep 17 00:00:00 2001
From: Rik Huijzer <github at huijzer.xyz>
Date: Sun, 22 Oct 2023 14:36:59 +0200
Subject: [PATCH] [mlir] Verify TestBuiltinAttributeInterfaces eltype

---
 mlir/test/IR/elements-attr-interface.mlir           | 13 +++++++++++++
 mlir/test/lib/IR/TestBuiltinAttributeInterfaces.cpp |  5 +++++
 2 files changed, 18 insertions(+)

diff --git a/mlir/test/IR/elements-attr-interface.mlir b/mlir/test/IR/elements-attr-interface.mlir
index e5f17d043f1aace..5234c81bd841e39 100644
--- a/mlir/test/IR/elements-attr-interface.mlir
+++ b/mlir/test/IR/elements-attr-interface.mlir
@@ -20,6 +20,13 @@ arith.constant #test.i64_elements<[10, 11, 12, 13, 14]> : tensor<5xi64>
 // expected-error at below {{Test iterating `IntegerAttr`: 10 : i64, 11 : i64, 12 : i64, 13 : i64, 14 : i64}}
 arith.constant dense<[10, 11, 12, 13, 14]> : tensor<5xi64>
 
+// This test is expected to only be called on integer elements.
+// expected-error at below {{Test iterating `int64_t`: expected element type to be an integer type}}
+// expected-error at below {{Test iterating `uint64_t`: expected element type to be an integer type}}
+// expected-error at below {{Test iterating `APInt`: expected element type to be an integer type}}
+// expected-error at below {{Test iterating `IntegerAttr`: expected element type to be an integer type}}
+arith.constant dense<[1.1, 1.2, 1.3]> : tensor<3xf32>
+
 // Check that we don't crash on empty element attributes.
 // expected-error at below {{Test iterating `int64_t`: }}
 // expected-error at below {{Test iterating `uint64_t`: }}
@@ -41,3 +48,9 @@ arith.constant #test.e1di64_elements<blob1> : tensor<3xi64>
     }
   }
 #-}
+
+// expected-error at below {{Test iterating `int64_t`: 0, 0, 1}}
+// expected-error at below {{Test iterating `uint64_t`: 0, 0, 1}}
+// expected-error at below {{Test iterating `APInt`: 0, 0, 1}}
+// expected-error at below {{Test iterating `IntegerAttr`: 0 : i64, 0 : i64, 1 : i64}}
+arith.constant sparse<[[0, 0, 2]], 1> : vector <1x1x3xi64>
diff --git a/mlir/test/lib/IR/TestBuiltinAttributeInterfaces.cpp b/mlir/test/lib/IR/TestBuiltinAttributeInterfaces.cpp
index 498de3d87bd4bc3..71ed30bfbe34cd0 100644
--- a/mlir/test/lib/IR/TestBuiltinAttributeInterfaces.cpp
+++ b/mlir/test/lib/IR/TestBuiltinAttributeInterfaces.cpp
@@ -51,6 +51,11 @@ struct TestElementsAttrInterface
     InFlightDiagnostic diag = op->emitError()
                               << "Test iterating `" << type << "`: ";
 
+    if (!attr.getElementType().isa<mlir::IntegerType>()) {
+      diag << "expected element type to be an integer type";
+      return;
+    }
+
     auto values = attr.tryGetValues<T>();
     if (!values) {
       diag << "unable to iterate type";



More information about the cfe-commits mailing list