mirror-llvm-project/mlir/test/Dialect/Tensor
ioana ghiban 9ed8b1dff8
[MLGO][EmitC] Scalarize single-element tensor returns (#199686)
Add an EmitC-owned preparation pass,
`mlgo-scalarize-single-element-tensor-return`, that rewrites private
functions returning a statically-shaped ranked tensor with exactly one
element into functions returning the element type directly.

Assisted-by: Codex (refine implementation + tests). I reviewed all code
and tests before submission.

## Example

Before:
```mlir
func.func private @rank1(%arg0: tensor<1xi64>) -> tensor<1xi64> {
  return %arg0 : tensor<1xi64>
}
```

After:
```mlir
func.func private @rank1(%arg0: tensor<1xi64>) -> i64 {
  %c0 = arith.constant 0 : index
  %0 = tensor.extract %arg0[%c0] : tensor<1xi64>
  return %0 : i64
}
```

Rank-0 tensors are handled similarly with `tensor.extract %t[]`, which
canonicalization/constant-folding can simplify further.

## Motivation
`tosa-converter-for-flite` may produce functions that return
tensor-wrapped scalars. These values are semantically scalars, but
remain packaged as single-element tensors in function signatures. This
becomes problematic later in lowering, especially for EmitC, where such
returns eventually bufferize to MemRef arrays and fail with:

```bash
error: 'emitc.func' op cannot return array type
```

This patch fixes the issue while the IR is still in `func`/`tensor`
form, where the scalar intent remains explicit and the transform can
stay semantics-driven.

## Scope
The transform is intentionally conservative: it only handles private
definitions with one single-element ranked tensor result, and it blocks
on public callers, non-call symbol users, unsupported callers, and
recursive cycles.

## Analysis
The pass runs at `ModuleOp` scope because legality depends on transitive
private call users. It first computes module-level scalarization
analysis from a `SymbolUserMap` snapshot, then rewrites only the
precomputed rewritable functions.

## Rewrite
Each rewritten function gets tensor.extract inserted before
`func.return`, its result type is updated with `funcOp.setType(...)`,
and direct `func.call` users are adjusted.
Unchanged private callers that still need the original tensor type are
adapted with `tensor.from_elements`.
2026-06-16 11:03:10 +02:00
..
bubble-reshapes.mlir
bubble-up-extract-slice-op.mlir [mlir][tensor]-Handle Dynamic Offset in BubbleUpSliceOpThroughCollapse (#178921) 2026-02-13 01:18:33 +02:00
bufferize.mlir [mlir][tensor] Fix bug in ConcatOpInterface (#168676) 2025-12-02 11:49:01 +08:00
canonicalize.mlir [mlir][tensor] Fix empty tensor with cast encoding fold (#187963) 2026-03-23 09:06:36 +00:00
decompose-concat.mlir
drop-redundant-insert-slice-rank-expansion.mlir [mlir][tensor] Enhance pattern to fold extract_slice(insert_slice) (#195045) 2026-05-09 09:15:09 +08:00
extract-from-collapse-shape.mlir
extract-slice-from-collapse-shape.mlir
fold-consecutive-insert-extract-slice.mlir [mlir][tensor] Consolidate tensor fold patterns and rename related file (#192820) 2026-04-23 16:03:50 +08:00
fold-constant-extract-slice.mlir [mlir][tensor] Remove hard-coded types from ConstantOpExtractSliceFolder (#184013) 2026-03-01 20:13:58 +02:00
fold-empty-op.mlir [mlir][tensor] Preserve tensor encodings when materializing tensor.empty in some passes (#192411) 2026-04-20 16:44:25 +08:00
fold-into-pack-and-unpack.mlir [MLIR] Fix typo "LABLE" in test checks (#178448) 2026-01-28 16:21:21 +00:00
fold-reassociative-reshapes.mlir
fold-tensor-subset-ops-into-vector-transfers.mlir
fold-tensor-subset-ops.mlir [mlir][tensor] Consolidate tensor fold patterns and rename related file (#192820) 2026-04-23 16:03:50 +08:00
int-range-inference.mlir
invalid.mlir [mlir][IR] Add builtin TokenType (#195640) 2026-06-02 12:33:46 +02:00
one-shot-bufferize-encodings.mlir
one-shot-bufferize-tensor-copy-insertion.mlir
one-shot-bufferize.mlir [mlir][bufferize] Make drop-equivalent-buffer-results support mult blocks (#163388) 2025-10-17 17:51:40 +08:00
ops.mlir
reify-shapes.mlir
resolve-shaped-type-result-dims.mlir
rewrite-as-constant.mlir
scalarize-single-elem-return.mlir [MLGO][EmitC] Scalarize single-element tensor returns (#199686) 2026-06-16 11:03:10 +02:00
shard-partition.mlir
tiling.mlir
tracking-listener.mlir
transform-op-casting.mlir
transform-op-make-loop-independent.mlir
value-bounds-op-interface-impl.mlir Reland "[mlir][tensor] Add ValueBoundsOpInterface for ExpandShapeOp and CollapseShapeOp #173356" (#173857) 2025-12-30 09:26:24 +01:00