-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[mlir][Vector] add vector.insert canonicalization pattern to convert a chain of insertions to vector.from_elements #142944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir][Vector] add vector.insert canonicalization pattern to convert a chain of insertions to vector.from_elements #142944
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-mlir-spirv @llvm/pr-subscribers-mlir Author: Yang Bai (yangtetris) ChangesDescriptionThis change introduces a new canonicalization pattern for the MLIR Vector dialect that optimizes chains of constant insertions into vectors initialized with Please be aware that the new pattern doesn't work for poison vectors where only some elements are set, as MLIR doesn't support partial poison vectors for now. New Pattern: InsertConstantToPoison
Refactored Helper Function
ExampleIt also works for multidimensional vectors. |
dcaballe
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comments. Otherwise, it LGTM!
|
Almost all of the changes have been completed. However, while updating the test base, I found that VectorFromElementsLowering currently does not support vectors with rank > 1. So, replacing insert chains with |
|
What is the current state of this? Any blockers? |
It is still blocked due to the missing from_elements to llvm conversion for multi-dim vectors. I think we can
|
It sounds like this shouldn't be too complicated. Is that something you could help with? |
I'm not familiar with that part, but I think I can give it a try. |
|
Hey @Groverkss, could you take another look/consider removing the blocker? Thanks! |
Groverkss
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very well implemented. LGTM!
|
Are we waiting on something/ someone before merging this? |
The dependent PR was just merged yesterday. Please help merge this PR. Thanks. |
done! |
|
@yangtetris Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Oh thanks for sharing this, I didn't notice the other PR. Do we know if from_elements unrolling works for SPIR-V? When we start canonicalizing to from_elements, we will have to support lowering to SPIR-V too. We do have support for converting from_elemtents 1d vectors to |
Theoretically it should work, but I haven't tested this before. I will add a test in the next PR, perhaps along with the flattening-based N-D -> 1-D transformation that we discussed in the multi-dimensional from_elements PR. |
|
Hey, I just want to flag that this breaks our downstream project, and I'm still triaging the issue. The error seems to happen in LLVM conversion. WARNING: ConvertTypesPass (--iree-input-demote-*-to-*) changed public function signatures; callers at runtime must match the new expected I/O types:
Old signature:
│ @matmul_f64f64f64_dynamic(tensor<?x?xf64>, tensor<?x?xf64>, tensor<?x?xf64>) -> tensor<?x?xf64>
New signature:
│ @matmul_f64f64f64_dynamic(tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32>) -> tensor<?x?xf32>
../matmul_dyn.mlir:3:8: error: cannot be converted to LLVM IR: missing `LLVMTranslationDialectInterface` registration for dialect for op: vector.from_elements
%0 = linalg.matmul ins(%arg0, %arg1 : tensor<?x?xf64>, tensor<?x?xf64>)My guess is that it introduces 2-D vector before LLVM conversion because I'm seeing below in the IR, while the pattern requires 1-D vector. %102 = vector.from_elements ... : vector<16x256xf32> |
|
Based on the review comments, it looks like we'll need |
@hanhanW Sorry for the breakage. Yeah, FYI, here's some discussion about why we didn't add this pattern to upstream LLVM conversion. |
|
@kuhar I'm trying to check whether the from_elements unrolling works for SPIR-V. However, it's not going smoothly since I'm completely new to SPIR-V. Do you know how we typically convert the following ops to SPIR-V? The |
…convert a chain of insertions to vector.from_elements (llvm#142944)" This reverts commit b4c31dc.
|
It seems this PR is triggering another canonicalization pattern that was added in f3cc854 Let's take this IR: This gets now canonicalized to: And then the other pattern applies and folds this into returning %arg0. I believe that pattern is wrong and should only be applied if the base argument is the same (so that we read and write to/from the same memory). Given this PR only triggers this potential bug, I guess I will have to look into fixing this myself. Another issue I noticed is that apparently there are some GPU related tests that do not run by default which are still broken. These are the tests: mlir/test/Integration/GPU/CUDA/TensorCore/sm80/transform-mma-sync-matmul-f16-f16-accum.mlir Most likely the GPU related pipeline that they use needs some adjustment to handle vector::FromElementOp with rank > 1. |
The pattern is applying correctly. tensors have value semantics, there is no "memory" here. The transfer_write here does a complete overlap on the destination, so we can fold completly here without needing to use the destination tensor values. |
Thank you for the notification. I created this PR to fix these two tests. BTW, could you please let me know if we have an option to enable all tests in local environments? |
…4774) ### Problem PR #142944 introduced a new canonicalization pattern which caused failures in the following GPU-related integration tests: - mlir/test/Integration/GPU/CUDA/TensorCore/sm80/transform-mma-sync-matmul-f16-f16-accum.mlir - mlir/test/Integration/GPU/CUDA/TensorCore/sm80/transform-mma-sync-matmul-f32.mlir The issue occurs because the new canonicalization pattern can generate multi-dimensional `vector.from_elements` operations (rank > 1), but the GPU lowering pipelines were not equipped to handle these during the conversion to LLVM. ### Fix This PR adds `vector::populateVectorFromElementsLoweringPatterns` to the GPU lowering passes that are integrated in `gpu-lower-to-nvvm-pipeline`: - `GpuToLLVMConversionPass`: the general GPU-to-LLVM conversion pass. - `LowerGpuOpsToNVVMOpsPass`: the NVVM-specific lowering pass. Co-authored-by: Yang Bai <[email protected]>
@Groverkss Does it mean the pre-canonicalization IR we use is wrong? We want to copy memory from one location to another location. What would be the proper way to do that? Edit: thinking more about this, I guess the way to do that would be to use memrefs. Because for that IR snippet we do already have buffers assigned (we start our MLIR lowering pipeline after assigning buffers to XLA HLO IR). It seems we relied on some assumption so far that happened to work out fine because not everything between vector.transfer_read and vector.transfer_write was simplified away. |
…convert a chain of insertions to vector.from_elements (llvm#142944)" This reverts commit b4c31dc.
…convert a chain of insertions to vector.from_elements (llvm#142944)" This reverts commit b4c31dc.
…convert a chain of insertions to vector.from_elements (llvm#142944)" This reverts commit b4c31dc.
…convert a chain of insertions to vector.from_elements (llvm#142944)" This reverts commit b4c31dc.
…convert a chain of insertions to vector.from_elements (llvm#142944)" This reverts commit b4c31dc.
Description
This change introduces a new canonicalization pattern for the MLIR Vector dialect that optimizes chains of insertions. The optimization identifies when a vector is completely initialized through a series of vector.insert operations and replaces the entire chain with a single
vector.from_elementsoperation.Please be aware that the new pattern doesn't work for poison vectors where only some elements are set, as MLIR doesn't support partial poison vectors for now.
New Pattern: InsertChainFullyInitialized
Refactored Helper Function
calculateInsertPositionfromfoldDenseElementsAttrDestInsertOpto avoid code duplication.Example
It also works for multidimensional vectors.