75 m.doc() =
"MLIR Execution Engine";
82 nb::class_<PyExecutionEngine>(m,
"ExecutionEngine")
85 [](PyExecutionEngine &self,
PyModule &module,
int optLevel,
86 const std::vector<std::string> &sharedLibPaths,
87 bool enableObjectDump,
bool enablePIC) {
88 std::vector<MlirStringRef> libPaths;
89 libPaths.reserve(sharedLibPaths.size());
90 for (
const std::string &path : sharedLibPaths)
91 libPaths.push_back({path.c_str(), path.length()});
94 MlirModule mlirModule =
module.get();
96 MlirExecutionEngine executionEngine;
98 nb::gil_scoped_release release;
100 mlirModule, optLevel, libPaths.size(), libPaths.data(),
101 enableObjectDump, enablePIC);
104 throw std::runtime_error(
105 "Failure while creating the ExecutionEngine.");
106 new (&self) PyExecutionEngine(executionEngine);
108 nb::arg(
"module"), nb::arg(
"opt_level") = 2,
109 nb::arg(
"shared_libs") = nb::list(),
110 nb::arg(
"enable_object_dump") =
true, nb::arg(
"enable_pic") =
false,
111 "Create a new ExecutionEngine instance for the given Module. The "
112 "module must contain only dialects that can be translated to LLVM. "
113 "Perform transformations and code generation at the optimization "
114 "level `opt_level` if specified, or otherwise at the default "
115 "level of two (-O2). Load a list of libraries specified in "
118 .def(
"_testing_release", &PyExecutionEngine::release,
119 "Releases (leaks) the backing ExecutionEngine (for testing purpose)")
123 [](PyExecutionEngine &executionEngine,
const std::string &
func) {
125 executionEngine.get(),
127 return reinterpret_cast<uintptr_t
>(res);
129 nb::arg(
"func_name"),
130 "Lookup function `func` in the ExecutionEngine.")
132 "raw_register_runtime",
133 [](PyExecutionEngine &executionEngine,
const std::string &name,
134 const nb::object &callbackObj) {
135 executionEngine.addReferencedObject(callbackObj);
137 nb::cast<uintptr_t>(nb::getattr(callbackObj,
"value"));
139 executionEngine.get(),
141 reinterpret_cast<void *
>(rawSym));
143 nb::arg(
"name"), nb::arg(
"callback"),
144 "Register `callback` as the runtime symbol `name`.")
147 [](PyExecutionEngine &executionEngine) {
150 "Initialize the ExecutionEngine. Global constructors specified by "
151 "`llvm.mlir.global_ctors` will be run. One common scenario is that "
152 "kernel binary compiled from `gpu.module` gets loaded during "
153 "initialization. Make sure all symbols are resolvable before "
154 "initialization by calling `register_runtime` or including "
157 "dump_to_object_file",
158 [](PyExecutionEngine &executionEngine,
const std::string &fileName) {
160 executionEngine.get(),
163 nb::arg(
"file_name"),
"Dump ExecutionEngine to an object file.");
void addReferencedObject(const nb::object &obj)
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 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 mlirExecutionEngineRegisterSymbol(MlirExecutionEngine jit, MlirStringRef name, void *sym)
Register a symbol with the jit: this symbol will be accessible to the jitted code.