Skip to content

[mlir] Verify TestBuiltinAttributeInterfaces eltype #69878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2023
Merged

[mlir] Verify TestBuiltinAttributeInterfaces eltype #69878

merged 2 commits into from
Oct 23, 2023

Conversation

rikhuijzer
Copy link
Member

@rikhuijzer rikhuijzer commented Oct 22, 2023

Fixes #61871 and fixes #60581.

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 #61871 is:

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 implementation of the interface is wrong here. The interface just 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.

@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2023

@llvm/pr-subscribers-mlir

Author: Rik Huijzer (rikhuijzer)

Changes

Fixes #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&lt;T&gt;()) is expected to handle mismatching types.

For example, the case which is given in the issue is:

arith.constant sparse&lt;[[0, 0, 5]],  -2.0&gt; : vector&lt;1x1x10xf16&gt;

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&lt;APInt&gt;() 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 #61871).


Full diff: https://github.com/llvm/llvm-project/pull/69878.diff

2 Files Affected:

  • (modified) mlir/test/IR/elements-attr-interface.mlir (+13)
  • (modified) mlir/test/lib/IR/TestBuiltinAttributeInterfaces.cpp (+5)
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@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@below {{Test iterating `int64_t`: expected element type to be an integer type}}
+// expected-error@below {{Test iterating `uint64_t`: expected element type to be an integer type}}
+// expected-error@below {{Test iterating `APInt`: expected element type to be an integer type}}
+// expected-error@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@below {{Test iterating `int64_t`: }}
 // expected-error@below {{Test iterating `uint64_t`: }}
@@ -41,3 +48,9 @@ arith.constant #test.e1di64_elements<blob1> : tensor<3xi64>
     }
   }
 #-}
+
+// expected-error@below {{Test iterating `int64_t`: 0, 0, 1}}
+// expected-error@below {{Test iterating `uint64_t`: 0, 0, 1}}
+// expected-error@below {{Test iterating `APInt`: 0, 0, 1}}
+// expected-error@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";

@rikhuijzer rikhuijzer merged commit c836b4a into llvm:main Oct 23, 2023
@rikhuijzer rikhuijzer deleted the rh/sparse-attribute-interface branch October 23, 2023 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants