[Mlir-commits] [mlir] [mlir][sparse] Add verification for explicit/implicit value (PR #90111)

Yinying Li llvmlistbot at llvm.org
Fri Apr 26 09:31:47 PDT 2024


================
@@ -907,6 +907,38 @@ LogicalResult SparseTensorEncodingAttr::verifyEncoding(
     return emitError()
            << "dimension-rank mismatch between encoding and tensor shape: "
            << getDimRank() << " != " << dimRank;
+  if (getExplicitVal()) {
+    if (auto typedAttr = llvm::dyn_cast<TypedAttr>(getExplicitVal())) {
+      Type attrType = typedAttr.getType();
+      if (attrType != elementType) {
+        return emitError()
+               << "explicit value type mismatch between encoding and "
+               << "tensor element type: " << attrType << " != " << elementType;
+      }
+    } else {
+      return emitError() << "expected typed explicit value";
+    }
+  }
+  if (getImplicitVal()) {
+    auto impVal = getImplicitVal();
+    if (auto typedAttr = llvm::dyn_cast<TypedAttr>(getImplicitVal())) {
+      Type attrType = typedAttr.getType();
+      if (attrType != elementType) {
+        return emitError()
+               << "implicit value type mismatch between encoding and "
+               << "tensor element type: " << attrType << " != " << elementType;
+      }
+    } else {
+      return emitError() << "expected typed implicit value";
+    }
+    // Currently, we only support zero as the implicit value.
+    auto impFVal = llvm::dyn_cast<FloatAttr>(impVal);
+    auto impIntVal = llvm::dyn_cast<IntegerAttr>(impVal);
----------------
yinying-lisa-li wrote:

Thanks for the feedback! When we parse the sparse encoding, we would raise an error for non-numeric implicit values, so during verification it would only be int or float when implicitVal is set in the encoding. Here is the code:
https://github.com/llvm/llvm-project/blob/37a92f9f60fc2f77264b06c5602a61aaa5196edb/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp#L682-L683

https://github.com/llvm/llvm-project/pull/90111


More information about the Mlir-commits mailing list