-
-
Notifications
You must be signed in to change notification settings - Fork 48
✨ Integrate MLIR PDLL for declarative pattern matching #1194
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
Conversation
8d3e060 to
3343485
Compare
|
Pre-commit won't be hapy here until you add a line for PDLL to core/.license-tools-config.json Lines 13 to 14 in 8e492a9
|
|
@burgholzer this is a first draft and I'd like to discuss with you maybe how to best integrate this new kind of pattern definition. Especially regarding directory structure since it might make sense to add sub-directories The code generation generates this here: namespace {
struct GeneratedPDLLPattern0 : ::mlir::PDLPatternModule {
template <typename... ConfigsT>
GeneratedPDLLPattern0(::mlir::MLIRContext *context, ConfigsT &&...configs)
: ::mlir::PDLPatternModule(::mlir::parseSourceString<::mlir::ModuleOp>(
R"mlir(pdl.pattern : benefit(0) {
%0 = operand loc("<path_to_repo>/core/mlir/lib/Dialect/MQTOpt/Transforms/SwapReconstructionAndElision.pdll":3:30)
%1 = operand loc("<path_to_repo>/core/mlir/lib/Dialect/MQTOpt/Transforms/SwapReconstructionAndElision.pdll":3:43)
%2 = types loc("<path_to_repo>/core/mlir/lib/Dialect/MQTOpt/Transforms/SwapReconstructionAndElision.pdll":3:14)
%3 = operation "mqtopt.swap"(%0, %1 : !pdl.value, !pdl.value) -> (%2 : !pdl.range<type>) loc("<path_to_repo>/core/mlir/lib/Dialect/MQTOpt/Transforms/SwapReconstructionAndElision.pdll":3:14)
rewrite %3 {
replace %3 with(%1, %0 : !pdl.value, !pdl.value) loc("<path_to_repo>/core/mlir/lib/Dialect/MQTOpt/Transforms/SwapReconstructionAndElision.pdll":6:3)
} loc("<path_to_repo>/core/mlir/lib/Dialect/MQTOpt/Transforms/SwapReconstructionAndElision.pdll":6:3)
} loc("<path_to_repo>/core/mlir/lib/Dialect/MQTOpt/Transforms/SwapReconstructionAndElision.pdll":1:1)
)mlir", context), std::forward<ConfigsT>(configs)...) {
}
};
} // end namespace
template <typename... ConfigsT>
static void LLVM_ATTRIBUTE_UNUSED populateGeneratedPDLLPatterns(::mlir::RewritePatternSet &patterns, ConfigsT &&...configs) {
patterns.add<GeneratedPDLLPattern0>(patterns.getContext(), configs...);
}Unfortunately, the So, we basically need to put each pattern group into a separate file and putting a pattern in multiple passes could become a little bit more complicated (we need to e.g. wrap the |
fab95ec to
a95d061
Compare
|
Let me play around with this a little. I'll get back to you 😌 |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Cpp-Linter Report
|
Signed-off-by: burgholzer <[email protected]>
Signed-off-by: burgholzer <[email protected]>
Signed-off-by: burgholzer <[email protected]>
Signed-off-by: burgholzer <[email protected]>
|
Alright. I played around with this a little and made a couple of adjustments. If it turns out that this does indeed not work even for such a simple use case, I believe we should do the following:
|
|
@burgholzer thanks for looking into it and working on this! I continued playing around with it and yes, it is frustrating - especially because the documentation is a little bit sparse at times. However, I wouldn't call it a failure because your issue can be simply fixed by adding a native rewrite embedded in PDLL with: Edit: it is also not fully working yet and I still think there is an easier way to build a new mqtopt operation without the native rewrite thing. Unfortunately I couldn't test the rebased version because I'll need to re-compile LLVM because I'm currently using 20.1.8. :) |
Unfortunately, that can't be working because statements like Hint: if you do not want to rebuild from scratch you can overwrite the minimum required MLIR version via CMake. Just pass |
It really bothered me that this didn't work at all and so I spent some more time yesterday and today on making it work - you can have a look at the current version, it is not the prettiest, but works. I think the inline native code integration is pretty neat and works reasonably well (although I got a lot of issues with
Thanks, I re-compiled it anyway in debug mode (although I'll re-compile it again in release mode - linking times go through the roof in debug mode). I'm on Linux 🐧 |
|
Ah. Nice. I just took a quick look and it does not look too bad complexit-wise. Will have to take a deeper look later or tomorrow to better judge the pro's and con's. If you are on Linux and you want a release build, you can get this pretty much for free by following the steps in our CI; see core/.github/scripts/setup-mlir.sh Lines 48 to 66 in 6957cb0
|
Signed-off-by: burgholzer <[email protected]>
|
I played around with this a little more now. @taminob if you agree with the above, would you mind continuing/resuming the work in #1158 and streamlining it based on all the lessons lernt in here. |
This is meant to better showcase the capabilities and different styles that are possible in PDLL.
|
This PR has been superseded by #1207 which re-implements the functionality in C++. I reverted some of your changes since I think that it can still be used as a reference for how to use PDLL and the different variants serve as a showcase. |
Technically, there would not have been a need for that as the branch contains all the commits and history anyway. Still fine though; just couldn't resist to comment. :o) |
But if someone looks at this in 2 years, I am pretty sure they won't look at the content of each commit (speaking from my own experience). Probably already unlikely anyone will look at this at all. :)
Me too :P |
## Description This PR introduces a new pattern and pass for swap reconstruction from two consecutive reversed CNOT gates and its immediate elision. It replaces #1158 which implemented a more advanced, but also much more complex, variant of the pattern which can also re-construct controlled SWAP gates and replace full three CNOT patterns equivalent to a SWAP. It is equivalent in functionality to #1194 which implemented the same pattern in PDLL. Related to #1122 ## Checklist: <!--- This checklist serves as a reminder of a couple of things that ensure your pull request will be merged swiftly. --> - [x] The pull request only contains commits that are focused and relevant to this change. - [x] I have added appropriate tests that cover the new/changed functionality. - [x] I have updated the documentation to reflect these changes. - [x] I have added entries to the changelog for any noteworthy additions, changes, fixes, or removals. - [x] I have added migration instructions to the upgrade guide (if needed). - [x] The changes follow the project's style guidelines and introduce no new warnings. - [x] The changes are fully tested and pass the CI checks. - [x] I have reviewed my own code changes. --------- Signed-off-by: burgholzer <[email protected]> Signed-off-by: Lukas Burgholzer <[email protected]> Co-authored-by: burgholzer <[email protected]>
Description
This PR introduces the MLIR declarative language for pattern matching and rewriting into the existing build system.
As an example, the
ElidePermutationspattern will be replaced by a PDLL implementation and extended by the functionality of #1158 (with the exclusion of multi-controlled swaps).This has previously been discussed in #1158.
Checklist: