-
Notifications
You must be signed in to change notification settings - Fork 15.7k
[MLIR][Python] Add GreedyRewriteDriverConfig parameter to apply_patterns_and_fold_greedily
#174785
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -509,26 +509,42 @@ void populateRewriteSubmodule(nb::module_ &m) { | |
| &PyFrozenRewritePatternSet::createFromCapsule); | ||
| m.def( | ||
| "apply_patterns_and_fold_greedily", | ||
| [](PyModule &module, PyFrozenRewritePatternSet &set) { | ||
| [](PyModule &module, PyFrozenRewritePatternSet &set, nb::object config) { | ||
| if (config.is_none()) { | ||
| config = nb::cast(PyGreedyRewriteDriverConfig()); | ||
| } | ||
|
|
||
| auto status = mlirApplyPatternsAndFoldGreedily( | ||
| module.get(), set.get(), mlirGreedyRewriteDriverConfigCreate()); | ||
| module.get(), set.get(), | ||
| nb::cast<PyGreedyRewriteDriverConfig &>(config).get()); | ||
| if (mlirLogicalResultIsFailure(status)) | ||
| throw std::runtime_error("pattern application failed to converge"); | ||
| }, | ||
| "module"_a, "set"_a, | ||
| "module"_a, "set"_a, "config"_a = nb::none(), | ||
| // clang-format off | ||
| nb::sig("def apply_patterns_and_fold_greedily(module: " MAKE_MLIR_PYTHON_QUALNAME("ir.Module") ", set: FrozenRewritePatternSet, config: GreedyRewriteDriverConfig | None = None) -> None"), | ||
| // clang-format on | ||
| "Applys the given patterns to the given module greedily while folding " | ||
| "results.") | ||
| .def( | ||
| "apply_patterns_and_fold_greedily", | ||
| [](PyOperationBase &op, PyFrozenRewritePatternSet &set) { | ||
| [](PyOperationBase &op, PyFrozenRewritePatternSet &set, | ||
| nb::object config) { | ||
| if (config.is_none()) { | ||
| config = nb::cast(PyGreedyRewriteDriverConfig()); | ||
| } | ||
|
|
||
| auto status = mlirApplyPatternsAndFoldGreedilyWithOp( | ||
| op.getOperation(), set.get(), | ||
| mlirGreedyRewriteDriverConfigCreate()); | ||
| nb::cast<PyGreedyRewriteDriverConfig &>(config).get()); | ||
| if (mlirLogicalResultIsFailure(status)) | ||
| throw std::runtime_error( | ||
| "pattern application failed to converge"); | ||
| }, | ||
| "op"_a, "set"_a, | ||
| "op"_a, "set"_a, "config"_a = nb::none(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here as above |
||
| // clang-format off | ||
| nb::sig("def apply_patterns_and_fold_greedily(op: " MAKE_MLIR_PYTHON_QUALNAME("ir._OperationBase") ", set: FrozenRewritePatternSet, config: GreedyRewriteDriverConfig | None = None) -> None"), | ||
| // clang-format on | ||
| "Applys the given patterns to the given op greedily while folding " | ||
| "results.") | ||
| .def( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You can just do
std::optionaland it'll automatically turn into this same kind of thing (default will beNone). Also you don't need to usenb::siganymore after #171775 because now all the API types are the actual binded classes.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.
ahh sounds good.
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.
hmm it seems that this requires that
PyGreedyRewriteDriverConfigcan be copy-constructed (T(const T&)), but now it can only be move-constructed (T(T&&)) instead. 🤔code:
m.def( "apply_patterns_and_fold_greedily", [](PyModule &module, PyFrozenRewritePatternSet &set, std::optional<PyGreedyRewriteDriverConfig> config) { if (!config) { config.emplace(PyGreedyRewriteDriverConfig()); } auto status = mlirApplyPatternsAndFoldGreedily(module.get(), set.get(), config->get()); if (mlirLogicalResultIsFailure(status)) throw std::runtime_error("pattern application failed to converge"); }, "module"_a, "set"_a, "config"_a = nb::none(), "Applys the given patterns to the given module greedily while folding " "results.")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.
Is it not possible to add a copy ctor?
Uh oh!
There was an error while loading. Please reload this page.
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.
currently PyGreedyRewriteDriverConfig acts like a "unique_ptr", i.e. it holds a raw pointer from C API. It is possible but maybe we have to add a new C API for copy constructing the actual C++ object. If we just copy the pointer then double free will happen. 🤔
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.
so then we can change to
std::shared_ptrwith a custom deleter? if you want i can do it but you're gonna have to push this PR up as a branch on this repo (or i can resubmit/take over the PR).Uh oh!
There was an error while loading. Please reload this page.
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.
hmm i tried
std::shared_ptrbut some runtime error appears:feel free to take over/resubmit this PR if you have time : )