Issue 243225
Summary [mlir][tosa] `--tosa-to-tensor` emits invalid `tensor` ops / crashes on unranked tensor inputs
Labels mlir
Assignees
Reporter quiet-maple-47
    ## Description

`--tosa-to-tensor` does not check that input tensor operands are ranked across all four conversion patterns in `TosaToTensor.cpp` (`ReshapeConverter`, `SliceConverter`, `PadConverter`, and `ConcatConverter`).

When an input operand is an `UnrankedTensorType` (`tensor<*x...>`), the pass either emits invalid `tensor` dialect operations with unranked operands (failing verification) or aborts in `cast<RankedTensorType>` on assertions-enabled builds.

## Reproducer

`input.mlir`:

```mlir
func.func @reshape_unranked(%arg0: tensor<*xi1>) -> tensor<1x3xi1> {
  %0 = tosa.const_shape values(dense<[1, 3]> : tensor<2xindex>) : () -> !tosa.shape<2>
  %1 = tosa.reshape %arg0, %0 : (tensor<*xi1>, !tosa.shape<2>) -> tensor<1x3xi1>
  return %1 : tensor<1x3xi1>
}

func.func @slice_unranked(%arg0: tensor<*xi8>) -> tensor<3x3x3xi8> {
  %start = tosa.const_shape values(dense<0> : tensor<3xindex>) : () -> !tosa.shape<3>
  %size = tosa.const_shape values(dense<3> : tensor<3xindex>) : () -> !tosa.shape<3>
  %0 = tosa.slice %arg0, %start, %size : (tensor<*xi8>, !tosa.shape<3>, !tosa.shape<3>) -> tensor<3x3x3xi8>
  return %0 : tensor<3x3x3xi8>
}

func.func @pad_unranked(%arg0: tensor<*xi8>) -> tensor<?x?xi8> {
  %pad = tosa.const_shape values(dense<[1, 1, 1, 1]> : tensor<4xindex>) : () -> !tosa.shape<4>
  %c = "tosa.const"() <{values = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
  %0 = tosa.pad %arg0, %pad, %c : (tensor<*xi8>, !tosa.shape<4>, tensor<1xi8>) -> tensor<?x?xi8>
  return %0 : tensor<?x?xi8>
}

func.func @concat_unranked(%arg0: tensor<*xi8>, %arg1: tensor<2x2xi8>) -> tensor<?x2xi8> {
  %0 = tosa.concat %arg0, %arg1 axis(0) : (tensor<*xi8>, tensor<2x2xi8>) -> tensor<?x2xi8>
  return %0 : tensor<?x2xi8>
}
```

```bash
mlir-opt --tosa-to-tensor input.mlir
```

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

## Actual Behavior

On LLVM trunk (non-assertions), every function emits an invalid `tensor.*` op with an unranked operand that fails verification:

```text
input.mlir:3:8: error: 'tensor.collapse_shape' op operand #0 must be ranked tensor of any non-token type values, but got 'tensor<*xi1>'
input.mlir:10:8: error: 'tensor.extract_slice' op operand #0 must be ranked tensor of any non-token type values, but got 'tensor<*xi8>'
input.mlir:17:8: error: 'tensor.pad' op operand #0 must be ranked tensor of any non-token type values, but got 'tensor<*xi8>'
input.mlir:22:8: error: 'tensor.concat' op operand #0 must be variadic of ranked tensor of any non-token type values, but got 'tensor<*xi8>'
```

On assertions-enabled builds (LLVM 23 `b3df0665` with LLVM 23 attribute syntax), `@reshape_unranked` aborts immediately in `llvm::cast<mlir::RankedTensorType>(TensorType)`:

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

## Expected Behavior

All patterns in `--tosa-to-tensor` should reject unranked input tensors via `notifyMatchFailure` / `failure()`, leaving those ops unconverted instead of crashing or producing invalid IR.

## Root Cause

Every pattern in [`TosaToTensor.cpp`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp#L225-L421) targets `tensor` dialect ops that require `RankedTensorType` operands, without checking `isa<RankedTensorType>` on the input(s):

- [`ReshapeConverter::matchAndRewrite`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp#L229-L269) only checks `TypedValue<TensorType>` and passes unranked inputs into `inferReshapeCollapsedType` / `createCollapse`.
- [`SliceConverter::matchAndRewrite`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp#L276-L321) checks `isa<UnrankedTensorType>(resultType)` at line 282, but omits checking `input.getType()`.
- [`PadConverter::matchAndRewrite`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp#L342-L401) casts `input.getType()` to `ShapedType` and calls `inputTy.getRank()` at line 369 (`kDynamic` on non-assertions builds, producing `static_low = []` / `static_high = []`) without checking `hasRank()`.
- [`ConcatConverter::matchAndRewrite`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp#L408-L420) checks only `resultType` and forwards unranked `adaptor.getInput1()` operands directly to `tensor::ConcatOp`.

Guarding all four patterns with `isa<RankedTensorType>` checks on their tensor input operand(s) (or adding legality checks in `populateTosaToTensorConversionPatterns` / target) resolves all four failures.

## Version

- LLVM trunk on Compiler Explorer (`mliropttrunk`, LLVM 24.0.0git)
- `llvm-project` `b3df0665` (Linux x86_64, assertions enabled; using LLVM 23 attribute 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