Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This offers the ability to create a JIT and invoke a function by passing ctypes pointers to the argument and the result. Differential Revision: https://reviews.llvm.org/D97523
- Loading branch information
Showing
14 changed files
with
333 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,10 @@ | ||
################################################################################ | ||
# Build python extension | ||
################################################################################ | ||
|
||
add_mlir_python_extension(MLIRConversionsBindingsPythonExtension _mlirConversions | ||
INSTALL_DIR | ||
python | ||
SOURCES | ||
Conversions.cpp | ||
) |
This file contains 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,24 @@ | ||
//===- Conversions.cpp - Pybind module for the Conversionss library -------===// | ||
// | ||
// 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/Conversion.h" | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace py = pybind11; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Module initialization. | ||
// ----------------------------------------------------------------------------- | ||
|
||
PYBIND11_MODULE(_mlirConversions, m) { | ||
m.doc() = "MLIR Conversions library"; | ||
|
||
// Register all the passes in the Conversions library on load. | ||
mlirRegisterConversionPasses(); | ||
} |
This file contains 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,87 @@ | ||
//===- ExecutionEngine.cpp - Python MLIR ExecutionEngine Bindings ---------===// | ||
// | ||
// 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 "ExecutionEngine.h" | ||
|
||
#include "IRModules.h" | ||
#include "mlir-c/Bindings/Python/Interop.h" | ||
#include "mlir-c/ExecutionEngine.h" | ||
|
||
namespace py = pybind11; | ||
using namespace mlir; | ||
using namespace mlir::python; | ||
|
||
namespace { | ||
|
||
/// Owning Wrapper around an ExecutionEngine. | ||
class PyExecutionEngine { | ||
public: | ||
PyExecutionEngine(MlirExecutionEngine executionEngine) | ||
: executionEngine(executionEngine) {} | ||
PyExecutionEngine(PyExecutionEngine &&other) | ||
: executionEngine(other.executionEngine) { | ||
other.executionEngine.ptr = nullptr; | ||
} | ||
~PyExecutionEngine() { | ||
if (!mlirExecutionEngineIsNull(executionEngine)) | ||
mlirExecutionEngineDestroy(executionEngine); | ||
} | ||
MlirExecutionEngine get() { return executionEngine; } | ||
|
||
void release() { executionEngine.ptr = nullptr; } | ||
pybind11::object getCapsule() { | ||
return py::reinterpret_steal<py::object>( | ||
mlirPythonExecutionEngineToCapsule(get())); | ||
} | ||
|
||
static pybind11::object createFromCapsule(pybind11::object capsule) { | ||
MlirExecutionEngine rawPm = | ||
mlirPythonCapsuleToExecutionEngine(capsule.ptr()); | ||
if (mlirExecutionEngineIsNull(rawPm)) | ||
throw py::error_already_set(); | ||
return py::cast(PyExecutionEngine(rawPm), py::return_value_policy::move); | ||
} | ||
|
||
private: | ||
MlirExecutionEngine executionEngine; | ||
}; | ||
|
||
} // anonymous namespace | ||
|
||
/// Create the `mlir.execution_engine` module here. | ||
void mlir::python::populateExecutionEngineSubmodule(py::module &m) { | ||
//---------------------------------------------------------------------------- | ||
// Mapping of the top-level PassManager | ||
//---------------------------------------------------------------------------- | ||
py::class_<PyExecutionEngine>(m, "ExecutionEngine") | ||
.def(py::init<>([](PyModule &module) { | ||
MlirExecutionEngine executionEngine = | ||
mlirExecutionEngineCreate(module.get()); | ||
if (mlirExecutionEngineIsNull(executionEngine)) | ||
throw std::runtime_error( | ||
"Failure while creating the ExecutionEngine."); | ||
return new PyExecutionEngine(executionEngine); | ||
}), | ||
"Create a new ExecutionEngine instance for the given Module. The " | ||
"module must " | ||
"contain only dialects that can be translated to LLVM.") | ||
.def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, | ||
&PyExecutionEngine::getCapsule) | ||
.def("_testing_release", &PyExecutionEngine::release, | ||
"Releases (leaks) the backing ExecutionEngine (for testing purpose)") | ||
.def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyExecutionEngine::createFromCapsule) | ||
.def( | ||
"raw_lookup", | ||
[](PyExecutionEngine &executionEngine, const std::string &func) { | ||
auto *res = mlirExecutionEngineLookup( | ||
executionEngine.get(), | ||
mlirStringRefCreate(func.c_str(), func.size())); | ||
return (int64_t)res; | ||
}, | ||
"Lookup function `func` in the ExecutionEngine."); | ||
} |
This file contains 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 @@ | ||
//===- ExecutionEngine.h - ExecutionEngine submodule 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_EXECUTIONENGINE_H | ||
#define MLIR_BINDINGS_PYTHON_EXECUTIONENGINE_H | ||
|
||
#include "PybindUtils.h" | ||
|
||
namespace mlir { | ||
namespace python { | ||
|
||
void populateExecutionEngineSubmodule(pybind11::module &m); | ||
|
||
} // namespace python | ||
} // namespace mlir | ||
|
||
#endif // MLIR_BINDINGS_PYTHON_EXECUTIONENGINE_H |
This file contains 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 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 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,8 @@ | ||
# 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 | ||
|
||
# Expose the corresponding C-Extension module with a well-known name at this | ||
# level. | ||
from .. import _load_extension | ||
_cextConversions = _load_extension("_mlirConversions") |
This file contains 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,31 @@ | ||
# 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 | ||
|
||
# Simply a wrapper around the extension module of the same name. | ||
from . import _cext | ||
import ctypes | ||
|
||
class ExecutionEngine(_cext.execution_engine.ExecutionEngine): | ||
|
||
def lookup(self, name): | ||
"""Lookup a function emitted with the `llvm.emit_c_interface` | ||
attribute and returns a ctype callable. | ||
Raise a RuntimeError if the function isn't found. | ||
""" | ||
func = self.raw_lookup("_mlir_ciface_" + name) | ||
if not func: | ||
raise RuntimeError("Unknown function " + name) | ||
prototype = ctypes.CFUNCTYPE(None, ctypes.c_void_p) | ||
return prototype(func) | ||
|
||
def invoke(self, name, *ctypes_args): | ||
"""Invoke a function with the list of ctypes arguments. | ||
All arguments must be pointers. | ||
Raise a RuntimeError if the function isn't found. | ||
""" | ||
func = self.lookup(name) | ||
packed_args = (ctypes.c_void_p * len(ctypes_args))() | ||
for argNum in range(len(ctypes_args)): | ||
packed_args[argNum] = ctypes.cast(ctypes_args[argNum], ctypes.c_void_p) | ||
func(packed_args) |
This file contains 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.