20 class PyExecutionEngine {
22 PyExecutionEngine(MlirExecutionEngine executionEngine)
23 : executionEngine(executionEngine) {}
24 PyExecutionEngine(PyExecutionEngine &&other) noexcept
25 : executionEngine(other.executionEngine) {
26 other.executionEngine.ptr =
nullptr;
28 ~PyExecutionEngine() {
32 MlirExecutionEngine
get() {
return executionEngine; }
35 executionEngine.ptr =
nullptr;
36 referencedObjects.clear();
38 nb::object getCapsule() {
44 void addReferencedObject(
const nb::object &obj) {
45 referencedObjects.push_back(obj);
48 static nb::object createFromCapsule(nb::object capsule) {
49 MlirExecutionEngine rawPm =
52 throw nb::python_error();
53 return nb::cast(PyExecutionEngine(rawPm), nb::rv_policy::move);
57 MlirExecutionEngine executionEngine;
61 std::vector<nb::object> referencedObjects;
68 m.doc() =
"MLIR Execution Engine";
73 nb::class_<PyExecutionEngine>(m,
"ExecutionEngine")
76 [](PyExecutionEngine &
self, MlirModule module,
int optLevel,
77 const std::vector<std::string> &sharedLibPaths,
78 bool enableObjectDump) {
80 for (
const std::string &path : sharedLibPaths)
81 libPaths.push_back({path.c_str(), path.length()});
82 MlirExecutionEngine executionEngine =
84 libPaths.data(), enableObjectDump);
86 throw std::runtime_error(
87 "Failure while creating the ExecutionEngine.");
88 new (&
self) PyExecutionEngine(executionEngine);
90 nb::arg(
"module"), nb::arg(
"opt_level") = 2,
91 nb::arg(
"shared_libs") = nb::list(),
92 nb::arg(
"enable_object_dump") =
true,
93 "Create a new ExecutionEngine instance for the given Module. The "
94 "module must contain only dialects that can be translated to LLVM. "
95 "Perform transformations and code generation at the optimization "
96 "level `opt_level` if specified, or otherwise at the default "
97 "level of two (-O2). Load a list of libraries specified in "
100 .def(
"_testing_release", &PyExecutionEngine::release,
101 "Releases (leaks) the backing ExecutionEngine (for testing purpose)")
105 [](PyExecutionEngine &executionEngine,
const std::string &func) {
107 executionEngine.get(),
109 return reinterpret_cast<uintptr_t
>(res);
111 nb::arg(
"func_name"),
112 "Lookup function `func` in the ExecutionEngine.")
114 "raw_register_runtime",
115 [](PyExecutionEngine &executionEngine,
const std::string &name,
116 nb::object callbackObj) {
117 executionEngine.addReferencedObject(callbackObj);
119 nb::cast<uintptr_t>(nb::getattr(callbackObj,
"value"));
121 executionEngine.get(),
123 reinterpret_cast<void *
>(rawSym));
125 nb::arg(
"name"), nb::arg(
"callback"),
126 "Register `callback` as the runtime symbol `name`.")
128 "dump_to_object_file",
129 [](PyExecutionEngine &executionEngine,
const std::string &fileName) {
131 executionEngine.get(),
134 nb::arg(
"file_name"),
"Dump ExecutionEngine to an object file.");
NB_MODULE(_mlirExecutionEngine, m)
Create the mlir.execution_engine module here.
static MlirExecutionEngine mlirPythonCapsuleToExecutionEngine(PyObject *capsule)
Extracts an MlirExecutionEngine from a capsule as produced from mlirPythonIntegerSetToCapsule.
#define MLIR_PYTHON_CAPI_PTR_ATTR
Attribute on MLIR Python objects that expose their C-API pointer.
#define MLIR_PYTHON_CAPI_FACTORY_ATTR
Attribute on MLIR Python objects that exposes a factory function for constructing the corresponding P...
static PyObject * mlirPythonExecutionEngineToCapsule(MlirExecutionEngine jit)
Creates a capsule object encapsulating the raw C-API MlirExecutionEngine.
MLIR_CAPI_EXPORTED MlirExecutionEngine mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths, const MlirStringRef *sharedLibPaths, bool enableObjectDump)
Creates an ExecutionEngine for the provided ModuleOp.
MLIR_CAPI_EXPORTED void * mlirExecutionEngineLookupPacked(MlirExecutionEngine jit, MlirStringRef name)
Lookup the wrapper of the native function in the execution engine with the given name,...
MLIR_CAPI_EXPORTED void mlirExecutionEngineDumpToObjectFile(MlirExecutionEngine jit, MlirStringRef fileName)
Dump as an object in fileName.
MLIR_CAPI_EXPORTED void mlirExecutionEngineDestroy(MlirExecutionEngine jit)
Destroy an ExecutionEngine instance.
MLIR_CAPI_EXPORTED void mlirExecutionEngineRegisterSymbol(MlirExecutionEngine jit, MlirStringRef name, void *sym)
Register a symbol with the jit: this symbol will be accessible to the jitted code.
static bool mlirExecutionEngineIsNull(MlirExecutionEngine jit)
Checks whether an execution engine is null.
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...