Skip to content

Commit bfd490e

Browse files
authored
Revert "[MLIR] Split ExecutionEngine Initialization out of ctor into an explicit method call" (llvm#153477)
Reverts llvm#153373 Sanitizer bot is broken
1 parent 85cd3d9 commit bfd490e

File tree

8 files changed

+12
-141
lines changed

8 files changed

+12
-141
lines changed

mlir/include/mlir-c/ExecutionEngine.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ MLIR_CAPI_EXPORTED MlirExecutionEngine mlirExecutionEngineCreate(
4646
MlirModule op, int optLevel, int numPaths,
4747
const MlirStringRef *sharedLibPaths, bool enableObjectDump);
4848

49-
/// Initialize the ExecutionEngine. Global constructors specified by
50-
/// `llvm.mlir.global_ctors` will be run. One common scenario is that kernel
51-
/// binary compiled from `gpu.module` gets loaded during initialization. Make
52-
/// sure all symbols are resolvable before initialization by calling
53-
/// `mlirExecutionEngineRegisterSymbol` or including shared libraries.
54-
MLIR_CAPI_EXPORTED void mlirExecutionEngineInitialize(MlirExecutionEngine jit);
55-
5649
/// Destroy an ExecutionEngine instance.
5750
MLIR_CAPI_EXPORTED void mlirExecutionEngineDestroy(MlirExecutionEngine jit);
5851

mlir/include/mlir/ExecutionEngine/ExecutionEngine.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,6 @@ class ExecutionEngine {
227227
llvm::function_ref<llvm::orc::SymbolMap(llvm::orc::MangleAndInterner)>
228228
symbolMap);
229229

230-
/// Initialize the ExecutionEngine. Global constructors specified by
231-
/// `llvm.mlir.global_ctors` will be run. One common scenario is that kernel
232-
/// binary compiled from `gpu.module` gets loaded during initialization. Make
233-
/// sure all symbols are resolvable before initialization by calling
234-
/// `registerSymbols` or including shared libraries.
235-
void initialize();
236-
237230
private:
238231
/// Ordering of llvmContext and jit is important for destruction purposes: the
239232
/// jit must be destroyed before the context.
@@ -257,8 +250,6 @@ class ExecutionEngine {
257250
/// Destroy functions in the libraries loaded by the ExecutionEngine that are
258251
/// called when this ExecutionEngine is destructed.
259252
SmallVector<LibraryDestroyFn> destroyFns;
260-
261-
bool isInitialized = false;
262253
};
263254

264255
} // namespace mlir

mlir/lib/Bindings/Python/ExecutionEngineModule.cpp

Lines changed: 1 addition & 12 deletions
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/Nanobind.h"
1110
#include "mlir/Bindings/Python/NanobindAdaptors.h"
11+
#include "mlir/Bindings/Python/Nanobind.h"
1212

1313
namespace nb = nanobind;
1414
using namespace mlir;
@@ -124,17 +124,6 @@ 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.")
138127
.def(
139128
"dump_to_object_file",
140129
[](PyExecutionEngine &executionEngine, const std::string &fileName) {

mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths,
6868
return wrap(jitOrError->release());
6969
}
7070

71-
extern "C" void mlirExecutionEngineInitialize(MlirExecutionEngine jit) {
72-
unwrap(jit)->initialize();
73-
}
74-
7571
extern "C" void mlirExecutionEngineDestroy(MlirExecutionEngine jit) {
7672
delete (unwrap(jit));
7773
}
@@ -110,8 +106,9 @@ extern "C" void mlirExecutionEngineRegisterSymbol(MlirExecutionEngine jit,
110106
void *sym) {
111107
unwrap(jit)->registerSymbols([&](llvm::orc::MangleAndInterner interner) {
112108
llvm::orc::SymbolMap symbolMap;
113-
symbolMap[interner(unwrap(name))] = {llvm::orc::ExecutorAddr::fromPtr(sym),
114-
llvm::JITSymbolFlags::Exported};
109+
symbolMap[interner(unwrap(name))] =
110+
{ llvm::orc::ExecutorAddr::fromPtr(sym),
111+
llvm::JITSymbolFlags::Exported };
115112
return symbolMap;
116113
});
117114
}

mlir/lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void ExecutionEngine::dumpToObjectFile(StringRef filename) {
106106
}
107107
// Compilation is lazy and it doesn't populate object cache unless requested.
108108
// In case object dump is requested before cache is populated, we need to
109-
// force compilation manually.
109+
// force compilation manually.
110110
if (cache->isEmpty()) {
111111
for (std::string &functionName : functionNames) {
112112
auto result = lookupPacked(functionName);
@@ -400,6 +400,13 @@ ExecutionEngine::create(Operation *m, const ExecutionEngineOptions &options,
400400
return symbolMap;
401401
};
402402
engine->registerSymbols(runtimeSymbolMap);
403+
404+
// Execute the global constructors from the module being processed.
405+
// TODO: Allow JIT initialize for AArch64. Currently there's a bug causing a
406+
// crash for AArch64 see related issue #71963.
407+
if (!engine->jit->getTargetTriple().isAArch64())
408+
cantFail(engine->jit->initialize(engine->jit->getMainJITDylib()));
409+
403410
return std::move(engine);
404411
}
405412

@@ -435,7 +442,6 @@ Expected<void *> ExecutionEngine::lookup(StringRef name) const {
435442

436443
Error ExecutionEngine::invokePacked(StringRef name,
437444
MutableArrayRef<void *> args) {
438-
initialize();
439445
auto expectedFPtr = lookupPacked(name);
440446
if (!expectedFPtr)
441447
return expectedFPtr.takeError();
@@ -445,13 +451,3 @@ Error ExecutionEngine::invokePacked(StringRef name,
445451

446452
return Error::success();
447453
}
448-
449-
void ExecutionEngine::initialize() {
450-
if (isInitialized)
451-
return;
452-
// TODO: Allow JIT initialize for AArch64. Currently there's a bug causing a
453-
// crash for AArch64 see related issue #71963.
454-
if (!jit->getTargetTriple().isAArch64())
455-
cantFail(jit->initialize(jit->getMainJITDylib()));
456-
isInitialized = true;
457-
}

mlir/lib/ExecutionEngine/JitRunner.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,6 @@ compileAndExecute(Options &options, Operation *module, StringRef entryPoint,
202202

203203
auto engine = std::move(*expectedEngine);
204204

205-
engine->initialize();
206-
207205
auto expectedFPtr = engine->lookupPacked(entryPoint);
208206
if (!expectedFPtr)
209207
return expectedFPtr.takeError();

mlir/test/CAPI/execution_engine.c

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -137,60 +137,6 @@ void testOmpCreation(void) {
137137
mlirContextDestroy(ctx);
138138
}
139139

140-
// Helper variable to track callback invocations
141-
static int initCnt = 0;
142-
143-
// Callback function that will be called during JIT initialization
144-
static void initCallback(void) { initCnt += 1; }
145-
146-
// CHECK-LABEL: Running test 'testGlobalCtorJitCallback'
147-
void testGlobalCtorJitCallback(void) {
148-
MlirContext ctx = mlirContextCreate();
149-
registerAllUpstreamDialects(ctx);
150-
151-
// Create module with global constructor that calls our callback
152-
MlirModule module = mlirModuleCreateParse(
153-
ctx, mlirStringRefCreateFromCString(
154-
// clang-format off
155-
"module { \n"
156-
" llvm.mlir.global_ctors ctors = [@ctor], priorities = [0 : i32], data = [#llvm.zero] \n"
157-
" llvm.func @ctor() { \n"
158-
" func.call @init_callback() : () -> () \n"
159-
" llvm.return \n"
160-
" } \n"
161-
" func.func private @init_callback() attributes { llvm.emit_c_interface } \n"
162-
"} \n"
163-
// clang-format on
164-
));
165-
166-
lowerModuleToLLVM(ctx, module);
167-
mlirRegisterAllLLVMTranslations(ctx);
168-
169-
// Create execution engine with initialization disabled
170-
MlirExecutionEngine jit = mlirExecutionEngineCreate(
171-
module, /*optLevel=*/2, /*numPaths=*/0, /*sharedLibPaths=*/NULL,
172-
/*enableObjectDump=*/false);
173-
174-
if (mlirExecutionEngineIsNull(jit)) {
175-
fprintf(stderr, "Execution engine creation failed");
176-
exit(2);
177-
}
178-
179-
// Register callback symbol before initialization
180-
mlirExecutionEngineRegisterSymbol(
181-
jit, mlirStringRefCreateFromCString("_mlir_ciface_init_callback"),
182-
(void *)(uintptr_t)initCallback);
183-
184-
mlirExecutionEngineInitialize(jit);
185-
186-
// CHECK: Init count: 1
187-
printf("Init count: %d\n", initCnt);
188-
189-
mlirExecutionEngineDestroy(jit);
190-
mlirModuleDestroy(module);
191-
mlirContextDestroy(ctx);
192-
}
193-
194140
int main(void) {
195141

196142
#define _STRINGIFY(x) #x
@@ -201,6 +147,5 @@ int main(void) {
201147

202148
TEST(testSimpleExecution);
203149
TEST(testOmpCreation);
204-
TEST(testGlobalCtorJitCallback);
205150
return 0;
206151
}

mlir/unittests/ExecutionEngine/Invoke.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -322,42 +322,4 @@ TEST(NativeMemRefJit, MAYBE_JITCallback) {
322322
ASSERT_EQ(elt, coefficient * count++);
323323
}
324324

325-
static int initCnt = 0;
326-
// A helper function that will be called during the JIT's initialization.
327-
static void initCallback() { initCnt += 1; }
328-
329-
TEST(GlobalCtorJit, MAYBE_JITCallback) {
330-
std::string moduleStr = R"mlir(
331-
llvm.mlir.global_ctors ctors = [@ctor], priorities = [0 : i32], data = [#llvm.zero]
332-
llvm.func @ctor() {
333-
func.call @init_callback() : () -> ()
334-
llvm.return
335-
}
336-
func.func private @init_callback() attributes { llvm.emit_c_interface }
337-
)mlir";
338-
339-
DialectRegistry registry;
340-
registerAllDialects(registry);
341-
registerBuiltinDialectTranslation(registry);
342-
registerLLVMDialectTranslation(registry);
343-
MLIRContext context(registry);
344-
auto module = parseSourceString<ModuleOp>(moduleStr, &context);
345-
ASSERT_TRUE(!!module);
346-
ASSERT_TRUE(succeeded(lowerToLLVMDialect(*module)));
347-
ExecutionEngineOptions jitOptions;
348-
auto jitOrError = ExecutionEngine::create(*module, jitOptions);
349-
ASSERT_TRUE(!!jitOrError);
350-
auto jit = std::move(jitOrError.get());
351-
// Define any extra symbols so they're available at initialization.
352-
jit->registerSymbols([&](llvm::orc::MangleAndInterner interner) {
353-
llvm::orc::SymbolMap symbolMap;
354-
symbolMap[interner("_mlir_ciface_init_callback")] = {
355-
llvm::orc::ExecutorAddr::fromPtr(initCallback),
356-
llvm::JITSymbolFlags::Exported};
357-
return symbolMap;
358-
});
359-
jit->initialize();
360-
ASSERT_EQ(initCnt, 1);
361-
}
362-
363325
#endif // _WIN32

0 commit comments

Comments
 (0)