[Mlir-commits] [mlir] [mlir][tensor] Add more tensor.extract_slice canonicalization (PR #212974)

Tuomas Kärnä llvmlistbot at llvm.org
Tue Aug 4 02:24:45 PDT 2026


tkarna wrote:

Regarding One-Shot Bufferize, I think the pattern 1 is relatively safe. At least the simple fabricated test below always bufferizes to a memref.copy anyway. Applying the reassociative-reshape-folding patterns folds the whole function to a no-op (`return %arg0`).

That said, as the `ReassociativeReshapeFoldingPatterns` pattern set already exists, it seems like a good place for this folding pattern for the time being.

```mlir
func.func @expand_shape_extract_slice_matching_insert_slice(
    %arg0 : tensor<64xf32> {bufferization.writable = true})
  -> (tensor<64xf32>)
{
  %expanded = tensor.expand_shape %arg0 [[0, 1]] output_shape [1, 64] : tensor<64xf32> into tensor<1x64xf32>
  %r0 = tensor.extract_slice %expanded[0, 0][1, 4][1, 1] : tensor<1x64xf32> to tensor<4xf32>
  %r1 = tensor.insert_slice %r0 into %expanded[0, 0][1, 4][1, 1] : tensor<4xf32> into tensor<1x64xf32>
  %collapsed = tensor.collapse_shape %r1 [[0, 1]] : tensor<1x64xf32> into tensor<64xf32>

  return %collapsed: tensor<64xf32>
}
```

@matthias-springer @joker-eph 

https://github.com/llvm/llvm-project/pull/212974


More information about the Mlir-commits mailing list