[mlir][Linalg] Introduce folding patterns to remove certain MemRefCastOp Summary: Canonicalization and folding patterns in StandardOps may interfere with the needs of Linalg. This revision introduces specific foldings for dynamic memrefs that can be proven to be static. Very concretely: Determines whether it is possible to fold it away in the parent Linalg op: ```mlir %1 = memref_cast %0 : memref<8x16xf32> to memref<?x?xf32> %2 = linalg.slice %1 ... : memref<?x?xf32> ... // or %1 = memref_cast %0 : memref<8x16xf32, affine_map<(i, j)->(16 * i + j)>> to memref<?x?xf32> linalg.generic(%1 ...) : memref<?x?xf32> ... ``` into ```mlir %2 = linalg.slice %0 ... : memref<8x16xf32> ... // or linalg.generic(%0 ... : memref<8x16xf32, affine_map<(i, j)->(16 * i + j)>> ```
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
Unit tests: pass. 62018 tests passed, 0 failed and 783 were skipped.
clang-tidy: unknown.
clang-format: pass.
Build artifacts: diff.json, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml
Unit tests: pass. 62018 tests passed, 0 failed and 783 were skipped.
clang-tidy: unknown.
clang-format: pass.
Build artifacts: diff.json, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml
Unit tests: pass. 62018 tests passed, 0 failed and 783 were skipped.
clang-tidy: pass.
clang-format: fail. Please format your changes with clang-format by running git-clang-format HEAD^ or applying this patch.
Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml
mlir/test/Dialect/Linalg/matmul-to-vector.mlir | ||
---|---|---|
3 ↗ | (On Diff #239756) | Is this comment relevant to the test? |
25 ↗ | (On Diff #239756) | Could you describe where do this magic sizes come from? |
mlir/test/lib/DeclarativeTransforms/TestLinalgMatmulToVectorPatterns.td | ||
3 ↗ | (On Diff #239756) | MLIR Project |
mlir/test/lib/Transforms/TestLinalgMatmulToVector.cpp | ||
27 ↗ | (On Diff #239756) | a MemrefCastOp, you don't change the LinalgOp |
39 ↗ | (On Diff #239756) | I don't understand this comment. If you have a memref_cast %0 fed by %0 = %0 = memref_cast %? : ... to memref<?x?xf32>, why do we convert it to memref_cast %0 : ... to memref<8x16xf32>`. There's something wrong with the order in your description. I understand it even less after reading the code. |
48 ↗ | (On Diff #239756) | Nit: cast |
56 ↗ | (On Diff #239756) | Can verified memref_cast apply to non-memrefs? |
81 ↗ | (On Diff #239756) | Leftover comment |
83 ↗ | (On Diff #239756) | Nit: I'd put it before iterating over the use list |
95 ↗ | (On Diff #239756) | Please add a comment, it looks like this block is useless (it isn't the trick is in the "linear" part). |
112 ↗ | (On Diff #239756) | I understand this is a test code, but having a separate test that exercises this pattern specifically would definitely help. One one hand, avoid spurious failures in a bigger test. On the other hand, better see what kind of transformation you are doing here. |
Thanks @ftynse I'll split out the new pattern in an independent test and rebase.
Note that comments in this diff are also relevant to the problem at hand: https://reviews.llvm.org/D73295.
mlir/test/Dialect/Linalg/matmul-to-vector.mlir | ||
---|---|---|
25 ↗ | (On Diff #239756) | I think they come from def : Pat<(MatmulOp:$op $_, $_, $_), (TileLinalgOp<[8, 12, 16], "L1__with_perm__", [1, 0, 2]>), [(Constraint<HasLinalgTransformMarker<"L2__with_perm__">>)]>; in TestLinalgMatmulToVectorPatterns.td They are fixed tile sizes right now for testing, but should somehow be computed in the futre |
mlir/test/lib/Transforms/TestLinalgMatmulToVector.cpp | ||
---|---|---|
3 ↗ | (On Diff #239756) | Thanks for noticing. I just mass-updated all the MLIR files in the repo to mention "Part of the LLVM project". |
Apologies for the delay, I recast this diff as an independent linalg canonicalization + test only as @ftynse suggested.
I'll be rebasing the rest on top of this in a new diff.
Unit tests: fail. 62196 tests passed, 1 failed and 815 were skipped.
failed: MLIR.mlir-cpu-runner/unranked_memref.mlir
clang-tidy: pass.
clang-format: pass.
Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml
Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.
mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp | ||
---|---|---|
78 | incorrectly carried over, case removed. |
Unit tests: pass. 62197 tests passed, 0 failed and 815 were skipped.
clang-tidy: pass.
clang-format: pass.
Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml
Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.
This landed here: https://github.com/llvm/llvm-project/commit/ea1e3369f7a8aa9729f8e2fc208b8f6a79392874
But since I made a mistake with phab I need to close manually.
When will this ever be hit? The desiredResultType doesn't appear to have any maps.