Skip to content

Commit 48e8cd8

Browse files
authored
[mlir][ExecutionEngine] Add support for global constructors and destructors (llvm#78070)
This patch add support for executing global constructors and destructors in the `ExecutionEngine`.
1 parent a1eaed7 commit 48e8cd8

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

mlir/lib/ExecutionEngine/ExecutionEngine.cpp

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

221221
ExecutionEngine::~ExecutionEngine() {
222+
// Execute the global destructors from the module being processed.
223+
if (jit)
224+
llvm::consumeError(jit->deinitialize(jit->getMainJITDylib()));
222225
// Run all dynamic library destroy callbacks to prepare for the shutdown.
223226
for (LibraryDestroyFn destroy : destroyFns)
224227
destroy();
@@ -396,6 +399,9 @@ ExecutionEngine::create(Operation *m, const ExecutionEngineOptions &options,
396399
};
397400
engine->registerSymbols(runtimeSymbolMap);
398401

402+
// Execute the global constructors from the module being processed.
403+
cantFail(engine->jit->initialize(engine->jit->getMainJITDylib()));
404+
399405
return std::move(engine);
400406
}
401407

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

0 commit comments

Comments
 (0)