Issue 239278
Summary [mlir][tosa] `tosa-to-linalg` crashes with `cast<Ty>` assertion when lowering `tosa.reduce_all` with dynamic result shape
Labels mlir
Assignees
Reporter lin334141-cell
    # [mlir][tosa] `tosa-to-linalg` crashes with `cast<Ty>` assertion when lowering `tosa.reduce_all` with dynamic result shape

## Summary

In `mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp`, lowering `tosa.reduce_all` (and other reduction ops using `reduceMatchAndRewriteHelper`) crashes with a casting assertion failure during shape inference if the declared result type leaves the reduced axis dynamic (`?`):

```text
Assertion failed: (isa<To>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file Casting.h, line 560.
```

Per the TOSA specification, the reduced dimension always has size 1 (i.e. reducing `tensor<2x3x?x?xi1>` along axis 1 has inferred shape `tensor<2x1x?x?xi1>`). When the declared result type is dynamic `tensor<?x?x?x?xi1>`, it passes verification because `verifyCompatibleShape` treats `?` as compatible with `1`.

However, during `tosa-to-linalg`, `reduceMatchAndRewriteHelper` attempts to construct a `tensor.expand_shape` using the **declared** dynamic result type rather than the inferred precise shape, causing `inferExpandShapeOutputShape` to fail and crash in `cast<Value>`.

## Minimal Reproducer

```mlir
// min.mlir
func.func @test_reduce(%arg0: tensor<2x3x?x?xi1>) -> tensor<?x?x?x?xi1> {
  %0 = "tosa.reduce_all"(%arg0) <{axis = 1 : i32}> : (tensor<2x3x?x?xi1>) -> tensor<?x?x?x?xi1>
  return %0 : tensor<?x?x?x?xi1>
}
```

Run lowering:
```bash
mlir-opt min.mlir --tosa-to-linalg
```

### Actual Behavior (`mlir-opt` aborts with exit code 134)

```text
Assertion failed: (isa<To>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file Casting.h, line 560.
...
#12 ReduceConverter<mlir::tosa::ReduceAllOp>::matchAndRewrite(...)
#21 mlir::TosaToLinalg::runOnOperation()
```
*(In release builds without assertions, this manifests as a SIGSEGV in the same frame).*

### Expected Behavior
`mlir-opt` should either:
1. Successfully lower the reduction by deriving the output shape from the inferred type (`tensor<2x1x?x?xi1>`) followed by a `tensor.cast` to the declared type `tensor<?x?x?x?xi1>`, or
2. Gracefully report a pass failure with an actionable diagnostic rather than crashing.

## Controls & Workaround

* **Precise Shape Works Cleanly**:
 Changing the declared return type to the statically inferred shape (`tensor<2x1x?x?xi1>`) succeeds without issue:
  ```mlir
  func.func @test_reduce_precise(%arg0: tensor<2x3x?x?xi1>) -> tensor<2x1x?x?xi1> {
 %0 = "tosa.reduce_all"(%arg0) <{axis = 1 : i32}> : (tensor<2x3x?x?xi1>) -> tensor<2x1x?x?xi1>
    return %0 : tensor<2x1x?x?xi1>
  }
  ```
* **Workaround via `tosa-infer-shapes`**:
  Running `mlir-opt min.mlir --tosa-infer-shapes --tosa-to-linalg` works because `--tosa-infer-shapes` refines the result type to `tensor<2x1x?x?xi1>` prior to conversion.

## Root Cause

In `reduceMatchAndRewriteHelper` (`mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp`):
The pass builds a `tensor::ExpandShapeOp` targeting the declared result type `resultTy` (`tensor<?x?x?x?xi1>`). Because the declared type has dynamic dimensions across reassociation groups where the reduction fixed the extent to 1, the builder's shape inference (`inferExpandShapeOutputShape`) fails to resolve the dynamic dimensions, leading to a failed cast.

## Suggested Fix Direction

1. In `reduceMatchAndRewriteHelper`, construct the intermediate `tensor.expand_shape` using the precise inferred shape (with extent 1 at `axis`), and then emit a `tensor.cast` to the declared `resultTy` if they differ.
2. Ensure `tensor.expand_shape` builder checks are guarded so that failed shape inferences return `reloop/failure()` rather than triggering a hard assertion.

## Tested Versions
- LLVM `main` at commit `069e9c56b29b4b16e34ca3b3c8c4afe0c879b7cc` (`TosaToLinalg.cpp`)
- LLVM `24.0.0git` (`67005b724c917e1bdca50c82ecaa1b21a69244ed`, macOS arm64)
- LLVM 22.1.6 Release (SIGSEGV)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to