| Issue |
240023
|
| Summary |
[MLIR][Tensor] `tensor.collapse_shape` test rewrite creates invalid IR (non-dominating use)
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
pingshiyu
|
Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da, dated 04/Nov/2026.
This program is valid:
```mlir
module {
func.func @f(%arg0: tensor<2x1xi1>) -> (tensor<?xi1>, tensor<1xi1>) {
%collapsed = tensor.collapse_shape %arg0 [[0, 1]]
: tensor<2x1xi1> into tensor<2xi1>
%early = tensor.cast %collapsed : tensor<2xi1> to tensor<?xi1>
%slice = tensor.extract_slice %collapsed[0] [1] [1]
: tensor<2xi1> to tensor<1xi1>
return %early, %slice : tensor<?xi1>, tensor<1xi1>
}
}
```
But the pass fails verification:
```bash
mlir-opt input.mlir \
'--pass-pipeline=builtin.module(func.func(test-tensor-transform-patterns{test-rewrite-extract-slice-from-collapse-shape=true}))' \
-o /dev/null
```
```text
input.mlir:6:14: error: operand #0 does not dominate this use
```
With `--verify-each=false`, the relevant output is:
```mlir
%0 = "tensor.cast"(%1) : (tensor<2xi1>) -> tensor<?xi1>
%1 = "tensor.extract_slice"(%arg0) ...
: (tensor<2x1xi1>) -> tensor<2xi1>
```
The rewrite replaces the earlier `tensor.collapse_shape` with a value inserted at the later matched `tensor.extract_slice`. The earlier `tensor.cast` is therefore redirected to a definition that no longer dominates it.
## Related cases
[LLVM #152037](https://github.com/llvm/llvm-project/issues/152037) also reports an invalid rank-reducing `tensor.extract_slice`, but its `DecomposePackUnpack` path produces inconsistent rank metadata. Its fix, [PR #152960](https://github.com/llvm/llvm-project/pull/152960), changes unpack dimension calculation. It does not involve replacement placement or dominance and is distinct from this case.
[LLVM #53305](https://github.com/llvm/llvm-project/issues/53305) is a folding enhancement for `tensor.extract_slice`, not an invalid transformation, and is contextual only.
## LLM-Suggested Cause and Fix
`RewriteExtractSliceFromCollapseShapeBase::matchAndRewrite` calls `simplifyCollapseShapeWithRankReducingExtractSlice` while the rewriter's insertion point is at the later matched slice. The helper creates the replacement there and globally replaces the earlier collapse.
A proposed fix is for the helper to use an insertion guard and set the insertion point to the `tensor.collapse_shape` before creating its replacement. A regression should give the collapse result another use before the matched slice and verify that the replacement dominates both uses. This proposed fix has not been tested.
## LLM Declaration
LLM was used to prepare the bug report, and the bug itself was found and verified by a human.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs