[Mlir-commits] [mlir] [mlir] Canonicalize tensor.extract_slice (linalg.fill) (PR #112619)

Andrzej Warzyński llvmlistbot at llvm.org
Fri Oct 18 05:22:52 PDT 2024


banach-space wrote:

First, a note:

**BEFORE (1 use):**
```mlir
%empty = tensor.empty
%fill = linalg.fill %empty
%res = tensor.extract_slice
```

**AFTER (1 use):**
```mlir
%empty = tensor.empty
%res = linalg.fill %empty
```

**BEFORE (2 uses)**:
```
%empty = tensor.empty
%fill = linalg.fill %empty
%res_1 = tensor.extract_slice %fill
%res_2 = tensor.extract_slice %fill
```

**AFTER (2 uses)**:
```
%empty_1 = tensor.empty
%res_1 = linalg.fill %empty_1
%empty_2 = tensor.empty
%res_2 = linalg.fill %empty_2
```

Looking at the case with 1 use, this feels like a canonicalization to me: 
* no information is lost and, with fewer Ops, the **AFTER** variant is basically simpler. 

This is no longer so clear to me in the case with 2 uses. Perhaps this should be limited to cases with 1 use?

On a related note, going by the Tensor Op [docs](https://mlir.llvm.org/docs/Dialects/TensorOps)
> [tensor.empty]
> This op can be lowered to a bufferization.alloc_tensor, at which point it turns into an explicit buffer allocation.

> [tensor.extract_slice]
> After buffer allocation, the “extract_slice” op is expected to lower into a memref.subview op.

So, with 2 uses, this would potentially replace (after bufferization):
*  1 allocation (`bufferization.alloc_tensor`) followed by 2 subviews (`memref.subview`), with 
* 2 allocations. 

It's not immediately obvious to me whether one is more "canonical" or "better" than the other. If aliasing is an issue, separate allocations would help. If aliasing is not a problem, re-using one buffer is probably better?

No strong opinion here, mostly just analysing out loud.

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


More information about the Mlir-commits mailing list