Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit c06f412

Browse files
[MLIR] Split ExecutionEngine Initialization out of ctor into an explicit method call (#153373)
This PR introduces a mechanism to defer JIT engine initialization, enabling registration of required symbols before global constructor execution. ## Problem Modules containing `gpu.module` generate global constructors (e.g., kernel load/unload) that execute *during* engine creation. This can force premature symbol resolution, causing failures when: - Symbols are registered via `mlirExecutionEngineRegisterSymbol` *after* creation - Global constructors exist (even if not directly using unresolved symbols, e.g., an external function declaration) - GPU modules introduce mandatory binary loading logic ## Usage ```c // Create engine without initialization MlirExecutionEngine jit = mlirExecutionEngineCreate(...); // Register required symbols mlirExecutionEngineRegisterSymbol(jit, ...); // Explicitly initialize (runs global constructors) mlirExecutionEngineInitialize(jit); ``` --------- Co-authored-by: Mehdi Amini <[email protected]>
1 parent efea4ca commit c06f412

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

ExecutionEngineModule.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "mlir-c/ExecutionEngine.h"
10-
#include "mlir/Bindings/Python/NanobindAdaptors.h"
1110
#include "mlir/Bindings/Python/Nanobind.h"
11+
#include "mlir/Bindings/Python/NanobindAdaptors.h"
1212

1313
namespace nb = nanobind;
1414
using namespace mlir;
@@ -124,6 +124,17 @@ NB_MODULE(_mlirExecutionEngine, m) {
124124
},
125125
nb::arg("name"), nb::arg("callback"),
126126
"Register `callback` as the runtime symbol `name`.")
127+
.def(
128+
"initialize",
129+
[](PyExecutionEngine &executionEngine) {
130+
mlirExecutionEngineInitialize(executionEngine.get());
131+
},
132+
"Initialize the ExecutionEngine. Global constructors specified by "
133+
"`llvm.mlir.global_ctors` will be run. One common scenario is that "
134+
"kernel binary compiled from `gpu.module` gets loaded during "
135+
"initialization. Make sure all symbols are resolvable before "
136+
"initialization by calling `raw_register_runtime` or including "
137+
"shared libraries.")
127138
.def(
128139
"dump_to_object_file",
129140
[](PyExecutionEngine &executionEngine, const std::string &fileName) {

0 commit comments

Comments
 (0)