1 //===----- LLJITDumpObjects.cpp - How to dump JIT'd objects with LLJIT ----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 /// This example demonstrates how to use LLJIT to:
12 /// - Add absolute symbol definitions.
13 /// - Run static constructors for a JITDylib.
14 /// - Run static destructors for a JITDylib.
16 /// This example does not call any functions (e.g. main or equivalent) between
17 /// running the static constructors and running the static destructors.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
23 #include "llvm/Support/InitLLVM.h"
24 #include "llvm/Support/TargetSelect.h"
25 #include "llvm/Support/raw_ostream.h"
27 #include "../ExampleModules.h"
30 using namespace llvm::orc
;
32 ExitOnError ExitOnErr
;
34 const llvm::StringRef ModuleWithInitializer
=
37 @InitializersRunFlag = external global i32
38 @DeinitializersRunFlag = external global i32
40 declare i32 @__cxa_atexit(void (i8*)*, i8*, i8*)
41 @__dso_handle = external hidden global i8
44 appending global [1 x { i32, void ()*, i8* }]
45 [{ i32, void ()*, i8* } { i32 65535, void ()* @init_func, i8* null }]
47 define internal void @init_func() {
49 store i32 1, i32* @InitializersRunFlag
50 %0 = call i32 @__cxa_atexit(void (i8*)* @deinit_func, i8* null,
55 define internal void @deinit_func(i8* %0) {
56 store i32 1, i32* @DeinitializersRunFlag
62 int main(int argc
, char *argv
[]) {
64 InitLLVM
X(argc
, argv
);
66 InitializeNativeTarget();
67 InitializeNativeTargetAsmPrinter();
69 cl::ParseCommandLineOptions(argc
, argv
, "LLJITWithInitializers");
70 ExitOnErr
.setBanner(std::string(argv
[0]) + ": ");
72 auto J
= ExitOnErr(LLJITBuilder().create());
73 auto M
= ExitOnErr(parseExampleModule(ModuleWithInitializer
, "M"));
76 ExitOnErr(J
->addIRModule(std::move(M
)));
78 int32_t InitializersRunFlag
= 0;
79 int32_t DeinitializersRunFlag
= 0;
81 ExitOnErr(J
->getMainJITDylib().define(
82 absoluteSymbols({{J
->mangleAndIntern("InitializersRunFlag"),
83 {ExecutorAddr::fromPtr(&InitializersRunFlag
),
84 JITSymbolFlags::Exported
}},
85 {J
->mangleAndIntern("DeinitializersRunFlag"),
86 {ExecutorAddr::fromPtr(&DeinitializersRunFlag
),
87 JITSymbolFlags::Exported
}}})));
89 // Run static initializers.
90 ExitOnErr(J
->initialize(J
->getMainJITDylib()));
92 // Run deinitializers.
93 ExitOnErr(J
->deinitialize(J
->getMainJITDylib()));
95 outs() << "InitializerRanFlag = " << InitializersRunFlag
<< "\n"
96 << "DeinitializersRunFlag = " << DeinitializersRunFlag
<< "\n";