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

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

`RescaleConverter` calls `inputTy.getRank()` unconditionally without checking `inputTy.hasRank()`.

## Reproducer

`input.mlir`:

```mlir
func.func @f(%arg0: tensor<*xi8>) -> tensor<?x?x?xi8> {
  %m = "tosa.const"() <{values = dense<1> : tensor<1xi32>}> : () -> tensor<1xi32>
  %s = "tosa.const"() <{values = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
  %izp = "tosa.const"() <{values = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
  %ozp = "tosa.const"() <{values = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
 %r = tosa.rescale %arg0, %m, %s, %izp, %ozp scale32(true) rounding_mode #tosa.rounding_mode<SINGLE_ROUND> per_channel(false) input_unsigned(false) output_unsigned(false) : (tensor<*xi8>, tensor<1xi32>, tensor<1xi8>, tensor<1xi8>, tensor<1xi8>) -> tensor<?x?x?xi8>
  return %r : tensor<?x?x?xi8>
}
```

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

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

## Actual Behavior

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

```text
 #3 0x000000000ad831de mlir::FloatType::getWidth()
 #4 0x000000000ae05eba mlir::Type::getIntOrFloatBitWidth() const
 #5 0x000000000789e2f4 (anonymous namespace)::getExtendZp(mlir::OpBuilder&, mlir::Type, llvm::FailureOr<long>, mlir::Location, mlir::ValueRange, long, bool) TosaToLinalg.cpp:0:0
 #6 0x000000000789e7e2 void llvm::function_ref<void (mlir::OpBuilder&, mlir::Location, mlir::ValueRange)>::callback_fn<(anonymous namespace)::RescaleConverter::matchAndRewrite(mlir::tosa::RescaleOp, mlir::PatternRewriter&) const::'lambda2'(mlir::OpBuilder&, mlir::Location, mlir::ValueRange)>(long, mlir::OpBuilder&, mlir::Location, mlir::ValueRange) TosaToLinalg.cpp:0:0
```

On assertions-enabled builds (LLVM 23 `b3df0665` with LLVM 23 attribute syntax), `mlir-opt` aborts in `ShapedType::getRank()`:

```text
mlir-opt: mlir/include/mlir/IR/BuiltinTypeInterfaces.h.inc:615: int64_t mlir::ShapedType::getRank() const: Assertion `(*this).hasRank() && "cannot query rank of unranked shaped type"' failed.
```

## Expected Behavior

`RescaleConverter` should return `rewriter.notifyMatchFailure` when `inputTy` (or `outputTy`) is unranked, rather than querying `getRank()` on an `UnrankedTensorType`.

## Root Cause

[`RescaleConverter::matchAndRewrite`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp#L1841-L1848) casts the input and output types to `ShapedType` and calls `inputTy.getRank()` without checking `inputTy.hasRank()` (or `outputTy.hasRank()`).

On non-assertions builds, `getRank()` returns `kDynamic` (`INT64_MIN`), producing a rank-0 `indexingMaps` entry for `linalg.generic` and causing [`getExtendZp`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp#L1798-L1804) to call `getIntOrFloatBitWidth()` on an invalid/non-scalar type and segfault.

Checking `if (!inputTy.hasRank() || !outputTy.hasRank()) return rewriter.notifyMatchFailure(...);` at the entry of `RescaleConverter::matchAndRewrite` avoids the crash.

## Version

- LLVM trunk on Compiler Explorer (`mliropttrunk`, LLVM 24.0.0git)
- Homebrew LLVM 23.1.5 (macOS arm64; using `{...}` attribute syntax)
- `llvm-project` `b3df0665` (Linux x86_64, assertions enabled; using `{...}` 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