[Mlir-commits] [mlir] [mlir][scf] Extend fuse producer to multi-level candidates case (PR #97803)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Sep 5 22:45:19 PDT 2024


================
@@ -949,6 +949,145 @@ mlir::scf::tileAndFuseProducerOfSlice(
                                            tileAndFuseResult->tiledOps};
 }
 
+/// Get the real producer from candidate ExtractSliceOp
+///
+/// ```
+/// %0 = producer
+/// %1 = scf.for(%arg1 = %0)
+///   %2 = extract %arg1
+///   %3 = scf.for(%arg2 = %2)
+///      %4 = extract %args2
+///      ...
+/// ```
+///
+/// @param candidateSliceOp: %4 = extract %args2
+/// @param backwardSlice: in-out parameter populated by backward extractSliceOps
+/// @return OpResult Producer : %0 = producer
+static FailureOr<OpResult> getRealProducerFromExtractSliceOp(
----------------
Yun-Fly wrote:

Let me try to rephrase the whole process step by step:
1. look through to collect all backward candidate slice of tiled operand `%6`, including `%6 = extract %args3`, ` %4 = extract %args2`, `%2 = extract %arg1`.
2. select and filter which candidate to fuse. I.e. ` %4 = extract %args2`.
3. Iteratively do producer fusion, from `%2` to `%4` by two iterations.

Through second thought, I agree that we can replace `step3` with your advice, which firstly combined `%2` and `%4`, then execute only single iteration of producer fusion.

However, as for `step 1` and `step 2`, we need to look through extract slice anyway... As I explained early, we could not fold `%2` and `%4` before these two steps. 

BTW, I dont think original `step3` in iterative fashion adding too much complexity because it is just reusing same logic and code.

On the other side, merging two extract slice in our case seems a little different from [MergeConsecutiveExtractSlice](https://github.com/iree-org/llvm-project/blob/c9f6518f742c88bda309d5331e0a5d4664387f94/mlir/lib/Dialect/Tensor/Transforms/MergeConsecutiveInsertExtractSlicePatterns.cpp#L23) as you pointed out, which actually expects direct use-def relationship:

```
%0 = extract_slice
%1 = extract_slice %0
```
rather than indirect relationship in our example:

```
%0 = extract_slice
scf.for(%1=%0)
  %2 = extract_slice %1
```

In this case, it is also necessary to look through extract slice before we merge these two slices.



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


More information about the Mlir-commits mailing list