-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[mlir] Add PDL C & Python usage #94714
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
Merged
Merged
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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//===-- mlir-c/Rewrite.h - Helpers for C API to Rewrites ----------*- C -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This header declares the registration and creation method for | ||
// rewrite patterns. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_C_REWRITE_H | ||
#define MLIR_C_REWRITE_H | ||
|
||
#include "mlir-c/IR.h" | ||
#include "mlir-c/Support.h" | ||
#include "mlir/Config/mlir-config.h" | ||
|
||
//===----------------------------------------------------------------------===// | ||
/// Opaque type declarations (see mlir-c/IR.h for more details). | ||
//===----------------------------------------------------------------------===// | ||
|
||
#define DEFINE_C_API_STRUCT(name, storage) \ | ||
struct name { \ | ||
storage *ptr; \ | ||
}; \ | ||
typedef struct name name | ||
|
||
DEFINE_C_API_STRUCT(MlirFrozenRewritePatternSet, void); | ||
DEFINE_C_API_STRUCT(MlirGreedyRewriteDriverConfig, void); | ||
DEFINE_C_API_STRUCT(MlirRewritePatternSet, void); | ||
|
||
MLIR_CAPI_EXPORTED MlirFrozenRewritePatternSet | ||
mlirFreezeRewritePattern(MlirRewritePatternSet op); | ||
|
||
MLIR_CAPI_EXPORTED void | ||
mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet op); | ||
|
||
MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPatternsAndFoldGreedily( | ||
MlirModule op, MlirFrozenRewritePatternSet patterns, | ||
MlirGreedyRewriteDriverConfig); | ||
|
||
#if MLIR_ENABLE_PDL_IN_PATTERNMATCH | ||
DEFINE_C_API_STRUCT(MlirPDLPatternModule, void); | ||
|
||
MLIR_CAPI_EXPORTED MlirPDLPatternModule | ||
mlirPDLPatternModuleFromModule(MlirModule op); | ||
|
||
MLIR_CAPI_EXPORTED void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op); | ||
|
||
MLIR_CAPI_EXPORTED MlirRewritePatternSet | ||
mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op); | ||
#endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH | ||
|
||
#undef DEFINE_C_API_STRUCT | ||
|
||
#endif // MLIR_C_REWRITE_H |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//===- Rewrite.cpp - Rewrite ----------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Rewrite.h" | ||
|
||
#include "IRModule.h" | ||
#include "mlir-c/Bindings/Python/Interop.h" | ||
#include "mlir-c/Rewrite.h" | ||
#include "mlir/Config/mlir-config.h" | ||
|
||
namespace py = pybind11; | ||
using namespace mlir; | ||
using namespace py::literals; | ||
using namespace mlir::python; | ||
|
||
namespace { | ||
|
||
#if MLIR_ENABLE_PDL_IN_PATTERNMATCH | ||
/// Owning Wrapper around a PDLPatternModule. | ||
class PyPDLPatternModule { | ||
public: | ||
PyPDLPatternModule(MlirPDLPatternModule module) : module(module) {} | ||
PyPDLPatternModule(PyPDLPatternModule &&other) noexcept | ||
: module(other.module) { | ||
other.module.ptr = nullptr; | ||
} | ||
~PyPDLPatternModule() { | ||
if (module.ptr != nullptr) | ||
mlirPDLPatternModuleDestroy(module); | ||
} | ||
MlirPDLPatternModule get() { return module; } | ||
|
||
private: | ||
MlirPDLPatternModule module; | ||
}; | ||
#endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH | ||
|
||
/// Owning Wrapper around a FrozenRewritePatternSet. | ||
class PyFrozenRewritePatternSet { | ||
public: | ||
PyFrozenRewritePatternSet(MlirFrozenRewritePatternSet set) : set(set) {} | ||
PyFrozenRewritePatternSet(PyFrozenRewritePatternSet &&other) noexcept | ||
: set(other.set) { | ||
other.set.ptr = nullptr; | ||
} | ||
~PyFrozenRewritePatternSet() { | ||
if (set.ptr != nullptr) | ||
mlirFrozenRewritePatternSetDestroy(set); | ||
} | ||
MlirFrozenRewritePatternSet get() { return set; } | ||
|
||
pybind11::object getCapsule() { | ||
return py::reinterpret_steal<py::object>( | ||
mlirPythonFrozenRewritePatternSetToCapsule(get())); | ||
} | ||
|
||
static pybind11::object createFromCapsule(pybind11::object capsule) { | ||
MlirFrozenRewritePatternSet rawPm = | ||
mlirPythonCapsuleToFrozenRewritePatternSet(capsule.ptr()); | ||
if (rawPm.ptr == nullptr) | ||
throw py::error_already_set(); | ||
return py::cast(PyFrozenRewritePatternSet(rawPm), | ||
py::return_value_policy::move); | ||
} | ||
|
||
private: | ||
MlirFrozenRewritePatternSet set; | ||
}; | ||
|
||
} // namespace | ||
|
||
/// Create the `mlir.rewrite` here. | ||
void mlir::python::populateRewriteSubmodule(py::module &m) { | ||
//---------------------------------------------------------------------------- | ||
// Mapping of the top-level PassManager | ||
//---------------------------------------------------------------------------- | ||
#if MLIR_ENABLE_PDL_IN_PATTERNMATCH | ||
py::class_<PyPDLPatternModule>(m, "PDLModule", py::module_local()) | ||
.def(py::init<>([](MlirModule module) { | ||
return mlirPDLPatternModuleFromModule(module); | ||
}), | ||
"module"_a, "Create a PDL module from the given module.") | ||
.def("freeze", [](PyPDLPatternModule &self) { | ||
return new PyFrozenRewritePatternSet(mlirFreezeRewritePattern( | ||
mlirRewritePatternSetFromPDLPatternModule(self.get()))); | ||
}); | ||
#endif // MLIR_ENABLE_PDL_IN_PATTERNMATCg | ||
py::class_<PyFrozenRewritePatternSet>(m, "FrozenRewritePatternSet", | ||
py::module_local()) | ||
.def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, | ||
&PyFrozenRewritePatternSet::getCapsule) | ||
.def(MLIR_PYTHON_CAPI_FACTORY_ATTR, | ||
&PyFrozenRewritePatternSet::createFromCapsule); | ||
m.def( | ||
"apply_patterns_and_fold_greedily", | ||
[](MlirModule module, MlirFrozenRewritePatternSet set) { | ||
auto status = mlirApplyPatternsAndFoldGreedily(module, set, {}); | ||
if (mlirLogicalResultIsFailure(status)) | ||
// FIXME: Not sure this is the right error to throw here. | ||
throw py::value_error("pattern application failed to converge"); | ||
}, | ||
"module"_a, "set"_a, | ||
"Applys the given patterns to the given module greedily while folding " | ||
"results."); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===- Rewrite.h - Rewrite Submodules of pybind module --------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_BINDINGS_PYTHON_REWRITE_H | ||
#define MLIR_BINDINGS_PYTHON_REWRITE_H | ||
|
||
#include "PybindUtils.h" | ||
|
||
namespace mlir { | ||
namespace python { | ||
|
||
void populateRewriteSubmodule(pybind11::module &m); | ||
|
||
} // namespace python | ||
} // namespace mlir | ||
|
||
#endif // MLIR_BINDINGS_PYTHON_REWRITE_H |
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
add_mlir_upstream_c_api_library(MLIRCAPITransforms | ||
Passes.cpp | ||
Rewrite.cpp | ||
|
||
LINK_LIBS PUBLIC | ||
MLIRIR | ||
MLIRTransforms | ||
MLIRTransformUtils | ||
) |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//===- Rewrite.cpp - C API for Rewrite Patterns ---------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir-c/Rewrite.h" | ||
#include "mlir-c/Transforms.h" | ||
#include "mlir/CAPI/IR.h" | ||
#include "mlir/CAPI/Support.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Rewrite/FrozenRewritePatternSet.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
||
using namespace mlir; | ||
|
||
inline mlir::RewritePatternSet &unwrap(MlirRewritePatternSet module) { | ||
assert(module.ptr && "unexpected null module"); | ||
return *(static_cast<mlir::RewritePatternSet *>(module.ptr)); | ||
} | ||
|
||
inline MlirRewritePatternSet wrap(mlir::RewritePatternSet *module) { | ||
return {module}; | ||
} | ||
|
||
inline mlir::FrozenRewritePatternSet * | ||
unwrap(MlirFrozenRewritePatternSet module) { | ||
assert(module.ptr && "unexpected null module"); | ||
return static_cast<mlir::FrozenRewritePatternSet *>(module.ptr); | ||
} | ||
|
||
inline MlirFrozenRewritePatternSet wrap(mlir::FrozenRewritePatternSet *module) { | ||
return {module}; | ||
} | ||
|
||
MlirFrozenRewritePatternSet mlirFreezeRewritePattern(MlirRewritePatternSet op) { | ||
auto *m = new mlir::FrozenRewritePatternSet(std::move(unwrap(op))); | ||
op.ptr = nullptr; | ||
return wrap(m); | ||
} | ||
|
||
void mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet op) { | ||
delete unwrap(op); | ||
op.ptr = nullptr; | ||
} | ||
|
||
MlirLogicalResult | ||
mlirApplyPatternsAndFoldGreedily(MlirModule op, | ||
MlirFrozenRewritePatternSet patterns, | ||
MlirGreedyRewriteDriverConfig) { | ||
return wrap( | ||
mlir::applyPatternsAndFoldGreedily(unwrap(op), *unwrap(patterns))); | ||
} | ||
|
||
#if MLIR_ENABLE_PDL_IN_PATTERNMATCH | ||
inline mlir::PDLPatternModule *unwrap(MlirPDLPatternModule module) { | ||
assert(module.ptr && "unexpected null module"); | ||
return static_cast<mlir::PDLPatternModule *>(module.ptr); | ||
} | ||
|
||
inline MlirPDLPatternModule wrap(mlir::PDLPatternModule *module) { | ||
return {module}; | ||
} | ||
|
||
MlirPDLPatternModule mlirPDLPatternModuleFromModule(MlirModule op) { | ||
return wrap(new mlir::PDLPatternModule( | ||
mlir::OwningOpRef<mlir::ModuleOp>(unwrap(op)))); | ||
} | ||
|
||
void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op) { | ||
delete unwrap(op); | ||
op.ptr = nullptr; | ||
} | ||
|
||
MlirRewritePatternSet | ||
mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op) { | ||
auto *m = new mlir::RewritePatternSet(std::move(*unwrap(op))); | ||
op.ptr = nullptr; | ||
return wrap(m); | ||
} | ||
#endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.