|
| 1 | +//===- PythonTestPassDemo.cpp -----------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "PythonTestPass.h" |
| 10 | + |
| 11 | +#include "mlir-c/Bindings/Python/Interop.h" |
| 12 | +#include "mlir/CAPI/IR.h" |
| 13 | +#include "mlir/IR/BuiltinDialect.h" |
| 14 | +#include "mlir/Pass/Pass.h" |
| 15 | + |
| 16 | +using namespace mlir; |
| 17 | + |
| 18 | +namespace { |
| 19 | + |
| 20 | +struct PythonTestPassDemo |
| 21 | + : public PassWrapper<PythonTestPassDemo, OperationPass<ModuleOp>> { |
| 22 | + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(PythonTestPassDemo) |
| 23 | + |
| 24 | + PythonTestPassDemo(PyObject *func) : func(func) {} |
| 25 | + StringRef getArgument() const final { return "python-pass-demo"; } |
| 26 | + |
| 27 | + void runOnOperation() override { |
| 28 | + this->getOperation()->walk([this](Operation *op) { |
| 29 | + PyObject *mlirModule = |
| 30 | + PyImport_ImportModule(MAKE_MLIR_PYTHON_QUALNAME("ir")); |
| 31 | + PyObject *cAPIFactory = PyObject_GetAttrString( |
| 32 | + PyObject_GetAttrString(mlirModule, "Operation"), |
| 33 | + MLIR_PYTHON_CAPI_FACTORY_ATTR); |
| 34 | + PyObject *opApiObject = PyObject_CallFunction( |
| 35 | + cAPIFactory, "(O)", mlirPythonOperationToCapsule(wrap(op))); |
| 36 | + (void)PyObject_CallFunction(func, "(O)", opApiObject); |
| 37 | + Py_DECREF(opApiObject); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + PyObject *func; |
| 42 | +}; |
| 43 | + |
| 44 | +std::unique_ptr<OperationPass<ModuleOp>> |
| 45 | +createPythonTestPassDemoPassWithFunc(PyObject *func) { |
| 46 | + return std::make_unique<PythonTestPassDemo>(func); |
| 47 | +} |
| 48 | + |
| 49 | +} // namespace |
| 50 | + |
| 51 | +void registerPythonTestPassDemoPassWithFunc(PyObject *func) { |
| 52 | + registerPass([func]() { return createPythonTestPassDemoPassWithFunc(func); }); |
| 53 | +} |
0 commit comments