[Mlir-commits] [mlir] c06ea02 - [MLIR] Graceful handling of uninitialized sparse tensor encodings with `sparse-tensor-codegen` (#181145)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Feb 20 10:32:42 PST 2026
Author: Arjun Bhamra
Date: 2026-02-20T18:32:37Z
New Revision: c06ea02bfd3f78a35164b5c57f7d2c5b1b2736c2
URL: https://github.com/llvm/llvm-project/commit/c06ea02bfd3f78a35164b5c57f7d2c5b1b2736c2
DIFF: https://github.com/llvm/llvm-project/commit/c06ea02bfd3f78a35164b5c57f7d2c5b1b2736c2.diff
LOG: [MLIR] Graceful handling of uninitialized sparse tensor encodings with `sparse-tensor-codegen` (#181145)
This PR handles the case where users call the `--sparse-tensor-codegen`
pass without sufficiently lowering dense tensors to sparse ones (with
passes like `--lower-sparse-ops-to-foreach` and
`--lower-sparse-foreach-to-scf` among others). This results in dense
tensors having a null `SparseTensorEncodingAttr`, which was originally
assumed to be true in the SparseTensor's `ConvertOp` lowering, but is
now checked against.
This PR closes #177779.
Added:
mlir/test/Dialect/SparseTensor/codegen_invalid.mlir
Modified:
mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp
Removed:
################################################################################
diff --git a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp
index 4c28658dec67d..0ca63b46e25a8 100644
--- a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp
+++ b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp
@@ -1150,6 +1150,13 @@ class SparseConvertConverter : public OpConversionPattern<ConvertOp> {
SparseTensorEncodingAttr encDst = getSparseTensorEncoding(op.getType());
SparseTensorEncodingAttr encSrc =
getSparseTensorEncoding(op.getSource().getType());
+
+ // If either the source or the destination don't have a valid sparse
+ // tensor encoding, we should fail to legalize. This should be handled
+ // by another set of passes before reaching here.
+ if (!encSrc || !encDst)
+ return failure();
+
// The output tensor can not be a slice and those cases should have been
// rejected by ConvertOp::verify() already.
assert(!encDst.isSlice() && "Cannot convert to a sparse tensor slices.");
diff --git a/mlir/test/Dialect/SparseTensor/codegen_invalid.mlir b/mlir/test/Dialect/SparseTensor/codegen_invalid.mlir
new file mode 100644
index 0000000000000..792272796a2b5
--- /dev/null
+++ b/mlir/test/Dialect/SparseTensor/codegen_invalid.mlir
@@ -0,0 +1,26 @@
+// RUN: mlir-opt %s -sparse-tensor-codegen -verify-diagnostics
+
+// NOTE: This test has valid IR, however we are testing whether
+// the legalization failure occurs when important passes are
+// missing. Notably, using --lower-sparse-ops-to-foreach
+// followed by --lower-sparse-foreach-to-scf prior to
+// sparse codegen will convert the dense tensor correctly.
+
+#SparseVector = #sparse_tensor.encoding<{
+ map = (d0) -> (d0 : compressed)
+}>
+
+module {
+ func.func @main() -> tensor<8xf32, #SparseVector> {
+ %dense = arith.constant dense<[1.0, 0.0, 0.0, 2.0, 0.0, 3.0, 0.0, 0.0]>
+ : tensor<8xf32>
+
+ // expected-error at +1 {{failed to legalize operation 'sparse_tensor.convert' that was explicitly marked illegal}}
+ %sparse = sparse_tensor.convert %dense
+ : tensor<8xf32> to tensor<8xf32, #SparseVector>
+
+ return %sparse : tensor<8xf32, #SparseVector>
+ }
+}
+
+// -----
More information about the Mlir-commits
mailing list