Issue 243213
Summary [mlir][tosa] `tosa-to-linalg-named` crashes on `tosa.transpose` with unranked tensor input
Labels mlir
Assignees
Reporter quiet-maple-47
    ## Description

`tosa-to-linalg-named` crashes when lowering a `tosa.transpose` operation whose input is an unranked tensor (`tensor<*xi8>`).

`TransposeConverter` calls `tensor::getMixedSizes` on the input without checking that it is a `RankedTensorType`.

## Reproducer

`input.mlir`:

```mlir
func.func @f(%arg0: tensor<1x2x3x2xi8>) -> tensor<1x3x2x2xi8> {
  %0 = tensor.cast %arg0 : tensor<1x2x3x2xi8> to tensor<*xi8>
  %1 = tosa.transpose %0 perms([0, 2, 1, 3]) : (tensor<*xi8>) -> tensor<1x3x2x2xi8>
  return %1 : tensor<1x3x2x2xi8>
}
```

```bash
mlir-opt --pass-pipeline='builtin.module(func.func(tosa-to-linalg-named))' input.mlir
```

[Reproduce on Compiler Explorer (LLVM trunk)](https://godbolt.org/z/z6EG7PExj)

## Actual Behavior

On LLVM trunk, `mlir-opt` crashes with `SIGSEGV` (exit code 139):

```text
 #3 0x000000000a713ffe mlir::tensor::getMixedSizes(mlir::OpBuilder&, mlir::Location, mlir::Value)
 #4 0x00000000078d6025 (anonymous namespace)::TransposeConverter::matchAndRewrite(mlir::tosa::TransposeOp, mlir::PatternRewriter&) const TosaToLinalgNamed.cpp:0:0
```

On assertions-enabled builds (LLVM 23 `b3df0665` with `{perms = ...}` syntax), `mlir-opt` aborts in `llvm::cast<mlir::RankedTensorType>(Type)`:

```text
mlir-opt: llvm/include/llvm/Support/Casting.h:560: decltype(auto) llvm::cast(const From &) [To = mlir::RankedTensorType, From = mlir::Type]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
```

## Expected Behavior

`TransposeConverter` should reject unranked inputs gracefully via `rewriter.notifyMatchFailure` (leaving `tosa.transpose` unconverted) instead of crashing.

## Root Cause

[`TransposeConverter::matchAndRewrite`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Conversion/TosaToLinalg/TosaToLinalgNamed.cpp#L1425-L1446) unconditionally passes `op.getInput1()` to `tensor::getMixedSizes(rewriter, loc, op.getInput1())` at line 1434, which casts the value's type to `RankedTensorType` without checking `isa<RankedTensorType>(op.getInput1().getType())`.

Guarding `matchAndRewrite` with a `RankedTensorType` check on `op.getInput1()` (and `op.getResult()`) avoids the crash.

## Version

- LLVM trunk on Compiler Explorer (`mliropttrunk`, LLVM 24.0.0git)
- Homebrew LLVM 23.1.5 (macOS arm64; using `{perms = ...}` syntax)
- `llvm-project` `b3df0665` (Linux x86_64, assertions enabled; using `{perms = ...}` syntax)
- Relevant code verified on `main` at `e0c9350a3f88` (2026-11-16)

## Claim

This investigation and write-up were AI-assisted. I've reviewed and verified everything above, and I'm happy to answer questions.

**Assisted-by:** Gemini / Antigravity

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

Reply via email to