25 : executionEngine(executionEngine) {}
27 : executionEngine(other.executionEngine) {
28 other.executionEngine.ptr =
nullptr;
34 MlirExecutionEngine
get() {
return executionEngine; }
37 executionEngine.ptr =
nullptr;
38 referencedObjects.clear();
47 referencedObjects.push_back(obj);
51 MlirExecutionEngine rawPm =
54 throw nb::python_error();
59 MlirExecutionEngine executionEngine;
63 std::vector<nb::object> referencedObjects;
73 m.doc() =
"MLIR Execution Engine";
80 nb::class_<PyExecutionEngine>(m,
"ExecutionEngine")
83 [](PyExecutionEngine &self,
PyModule &module,
int optLevel,
84 const std::vector<std::string> &sharedLibPaths,
85 bool enableObjectDump,
bool enablePIC) {
87 for (
const std::string &path : sharedLibPaths)
88 libPaths.push_back({path.c_str(), path.length()});
90 module.
get(), optLevel, libPaths.size(), libPaths.data(),
91 enableObjectDump, enablePIC);
93 throw std::runtime_error(
94 "Failure while creating the ExecutionEngine.");
95 new (&self) PyExecutionEngine(executionEngine);
97 nb::arg(
"module"), nb::arg(
"opt_level") = 2,
98 nb::arg(
"shared_libs") = nb::list(),
99 nb::arg(
"enable_object_dump") =
true, nb::arg(
"enable_pic") =
false,
100 "Create a new ExecutionEngine instance for the given Module. The "
101 "module must contain only dialects that can be translated to LLVM. "
102 "Perform transformations and code generation at the optimization "
103 "level `opt_level` if specified, or otherwise at the default "
104 "level of two (-O2). Load a list of libraries specified in "
107 .def(
"_testing_release", &PyExecutionEngine::release,
108 "Releases (leaks) the backing ExecutionEngine (for testing purpose)")
112 [](PyExecutionEngine &executionEngine,
const std::string &
func) {
114 executionEngine.get(),
116 return reinterpret_cast<uintptr_t
>(res);
118 nb::arg(
"func_name"),
119 "Lookup function `func` in the ExecutionEngine.")
121 "raw_register_runtime",
122 [](PyExecutionEngine &executionEngine,
const std::string &name,
123 const nb::object &callbackObj) {
124 executionEngine.addReferencedObject(callbackObj);
126 nb::cast<uintptr_t>(nb::getattr(callbackObj,
"value"));
128 executionEngine.get(),
130 reinterpret_cast<void *
>(rawSym));
132 nb::arg(
"name"), nb::arg(
"callback"),
133 "Register `callback` as the runtime symbol `name`.")
136 [](PyExecutionEngine &executionEngine) {
139 "Initialize the ExecutionEngine. Global constructors specified by "
140 "`llvm.mlir.global_ctors` will be run. One common scenario is that "
141 "kernel binary compiled from `gpu.module` gets loaded during "
142 "initialization. Make sure all symbols are resolvable before "
143 "initialization by calling `register_runtime` or including "
146 "dump_to_object_file",
147 [](PyExecutionEngine &executionEngine,
const std::string &fileName) {
149 executionEngine.get(),
152 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.
static PyObject * mlirPythonExecutionEngineToCapsule(MlirExecutionEngine jit)
Creates a capsule object encapsulating the raw C-API MlirExecutionEngine.
#define MLIR_PYTHON_CAPI_FACTORY_ATTR
Attribute on MLIR Python objects that exposes a factory function for constructing the corresponding P...
MlirModule get()
Gets the backing MlirModule.
void addReferencedObject(const nb::object &obj)
MlirExecutionEngine get()
PyExecutionEngine(MlirExecutionEngine executionEngine)
PyExecutionEngine(PyExecutionEngine &&other) noexcept
static nb::object createFromCapsule(const nb::object &capsule)
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 MlirExecutionEngine mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths, const MlirStringRef *sharedLibPaths, bool enableObjectDump, bool enablePIC)
Creates an ExecutionEngine for the provided ModuleOp.
MLIR_CAPI_EXPORTED void mlirExecutionEngineDestroy(MlirExecutionEngine jit)
Destroy an ExecutionEngine instance.
MLIR_CAPI_EXPORTED void mlirExecutionEngineInitialize(MlirExecutionEngine jit)
Initialize the ExecutionEngine.
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.