1 | //===----- LLJITDumpObjects.cpp - How to dump JIT'd objects with LLJIT ----===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "llvm/ADT/StringMap.h" |
10 | #include "llvm/ExecutionEngine/Orc/DebugUtils.h" |
11 | #include "llvm/ExecutionEngine/Orc/LLJIT.h" |
12 | #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h" |
13 | #include "llvm/Support/InitLLVM.h" |
14 | #include "llvm/Support/TargetSelect.h" |
15 | #include "llvm/Support/raw_ostream.h" |
16 | |
17 | #include "../ExampleModules.h" |
18 | |
19 | using namespace llvm; |
20 | using namespace llvm::orc; |
21 | |
22 | ExitOnError ExitOnErr; |
23 | |
24 | cl::opt<bool> DumpJITdObjects("dump-jitted-objects" , |
25 | cl::desc("dump jitted objects" ), cl::Optional, |
26 | cl::init(Val: true)); |
27 | |
28 | cl::opt<std::string> DumpDir("dump-dir" , |
29 | cl::desc("directory to dump objects to" ), |
30 | cl::Optional, cl::init(Val: "" )); |
31 | |
32 | cl::opt<std::string> DumpFileStem("dump-file-stem" , |
33 | cl::desc("Override default dump names" ), |
34 | cl::Optional, cl::init(Val: "" )); |
35 | |
36 | int main(int argc, char *argv[]) { |
37 | // Initialize LLVM. |
38 | InitLLVM X(argc, argv); |
39 | |
40 | InitializeNativeTarget(); |
41 | InitializeNativeTargetAsmPrinter(); |
42 | |
43 | cl::ParseCommandLineOptions(argc, argv, Overview: "LLJITDumpObjects" ); |
44 | ExitOnErr.setBanner(std::string(argv[0]) + ": " ); |
45 | |
46 | outs() |
47 | << "Usage notes:\n" |
48 | " Use -debug-only=orc on debug builds to see log messages of objects " |
49 | "being dumped\n" |
50 | " Specify -dump-dir to specify a dump directory\n" |
51 | " Specify -dump-file-stem to override the dump file stem\n" |
52 | " Specify -dump-jitted-objects=false to disable dumping\n" ; |
53 | |
54 | auto J = ExitOnErr(LLJITBuilder().create()); |
55 | |
56 | if (DumpJITdObjects) |
57 | J->getObjTransformLayer().setTransform(DumpObjects(DumpDir, DumpFileStem)); |
58 | |
59 | auto M = ExitOnErr(parseExampleModule(Source: Add1Example, Name: "add1" )); |
60 | |
61 | ExitOnErr(J->addIRModule(TSM: std::move(M))); |
62 | |
63 | // Look up the JIT'd function, cast it to a function pointer, then call it. |
64 | auto Add1Addr = ExitOnErr(J->lookup(UnmangledName: "add1" )); |
65 | int (*Add1)(int) = Add1Addr.toPtr<int(int)>(); |
66 | |
67 | int Result = Add1(42); |
68 | outs() << "add1(42) = " << Result << "\n" ; |
69 | |
70 | return 0; |
71 | } |
72 | |