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