Skip to content

Commit 01dbc5d

Browse files
authored
Reland [mlir][ExecutionEngine] Add support for global constructors and destructors llvm#78070 (llvm#78170)
This patch add support for executing global constructors and destructors in the ExecutionEngine.
1 parent 4aa0424 commit 01dbc5d

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

mlir/lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ ExecutionEngine::ExecutionEngine(bool enableObjectDump,
219219
}
220220

221221
ExecutionEngine::~ExecutionEngine() {
222+
// Execute the global destructors from the module being processed.
223+
// TODO: Allow JIT deinitialize for AArch64. Currently there's a bug causing a
224+
// crash for AArch64 see related issue #71963.
225+
if (jit && !jit->getTargetTriple().isAArch64())
226+
llvm::consumeError(jit->deinitialize(jit->getMainJITDylib()));
222227
// Run all dynamic library destroy callbacks to prepare for the shutdown.
223228
for (LibraryDestroyFn destroy : destroyFns)
224229
destroy();
@@ -396,6 +401,12 @@ ExecutionEngine::create(Operation *m, const ExecutionEngineOptions &options,
396401
};
397402
engine->registerSymbols(runtimeSymbolMap);
398403

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+
399410
return std::move(engine);
400411
}
401412

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// UNSUPPORTED: target=aarch64{{.*}}
2+
// RUN: mlir-cpu-runner %s -e entry -entry-point-result=void \
3+
// RUN: -shared-libs=%mlir_c_runner_utils | \
4+
// RUN: FileCheck %s
5+
6+
// Test that the `ctor` executes before `entry` and that `dtor` executes last.
7+
module {
8+
llvm.func @printNewline()
9+
llvm.func @printI64(i64)
10+
llvm.mlir.global_ctors {ctors = [@ctor], priorities = [0 : i32]}
11+
llvm.mlir.global_dtors {dtors = [@dtor], priorities = [0 : i32]}
12+
llvm.func @ctor() {
13+
%0 = llvm.mlir.constant(1 : i64) : i64
14+
llvm.call @printI64(%0) : (i64) -> ()
15+
llvm.call @printNewline() : () -> ()
16+
// CHECK: 1
17+
llvm.return
18+
}
19+
llvm.func @entry() {
20+
%0 = llvm.mlir.constant(2 : i64) : i64
21+
llvm.call @printI64(%0) : (i64) -> ()
22+
llvm.call @printNewline() : () -> ()
23+
// CHECK: 2
24+
llvm.return
25+
}
26+
llvm.func @dtor() {
27+
%0 = llvm.mlir.constant(3 : i64) : i64
28+
llvm.call @printI64(%0) : (i64) -> ()
29+
llvm.call @printNewline() : () -> ()
30+
// CHECK: 3
31+
llvm.return
32+
}
33+
}

0 commit comments

Comments
 (0)