[Mlir-commits] [mlir] c836b4a - [mlir] Verify TestBuiltinAttributeInterfaces eltype (#69878)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Oct 23 08:02:57 PDT 2023


Author: Rik Huijzer
Date: 2023-10-23T17:02:52+02:00
New Revision: c836b4ad6cd005766dfe1b29c81fe1b667e50e1c

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

LOG: [mlir] Verify TestBuiltinAttributeInterfaces eltype (#69878)

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:
```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
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.

Added: 
    

Modified: 
    mlir/test/IR/elements-attr-interface.mlir
    mlir/test/lib/IR/TestBuiltinAttributeInterfaces.cpp

Removed: 
    


################################################################################
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 Mlir-commits mailing list