1633ea072SLang Hames //===----- LLJITDumpObjects.cpp - How to dump JIT'd objects with LLJIT ----===//
2633ea072SLang Hames //
3633ea072SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4633ea072SLang Hames // See https://llvm.org/LICENSE.txt for license information.
5633ea072SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6633ea072SLang Hames //
7633ea072SLang Hames //===----------------------------------------------------------------------===//
8633ea072SLang Hames 
9633ea072SLang Hames #include "llvm/ADT/StringMap.h"
10633ea072SLang Hames #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
11633ea072SLang Hames #include "llvm/ExecutionEngine/Orc/LLJIT.h"
126edc3fe5SStefan Gränitz #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
13633ea072SLang Hames #include "llvm/Support/InitLLVM.h"
14633ea072SLang Hames #include "llvm/Support/TargetSelect.h"
15633ea072SLang Hames #include "llvm/Support/raw_ostream.h"
16633ea072SLang Hames 
17633ea072SLang Hames #include "../ExampleModules.h"
18633ea072SLang Hames 
19633ea072SLang Hames using namespace llvm;
20633ea072SLang Hames using namespace llvm::orc;
21633ea072SLang Hames 
22633ea072SLang Hames ExitOnError ExitOnErr;
23633ea072SLang Hames 
24633ea072SLang Hames cl::opt<bool> DumpJITdObjects("dump-jitted-objects",
25633ea072SLang Hames                               cl::desc("dump jitted objects"), cl::Optional,
26633ea072SLang Hames                               cl::init(true));
27633ea072SLang Hames 
28633ea072SLang Hames cl::opt<std::string> DumpDir("dump-dir",
29633ea072SLang Hames                              cl::desc("directory to dump objects to"),
30633ea072SLang Hames                              cl::Optional, cl::init(""));
31633ea072SLang Hames 
32633ea072SLang Hames cl::opt<std::string> DumpFileStem("dump-file-stem",
33633ea072SLang Hames                                   cl::desc("Override default dump names"),
34633ea072SLang Hames                                   cl::Optional, cl::init(""));
35633ea072SLang Hames 
main(int argc,char * argv[])36633ea072SLang Hames int main(int argc, char *argv[]) {
37633ea072SLang Hames   // Initialize LLVM.
38633ea072SLang Hames   InitLLVM X(argc, argv);
39633ea072SLang Hames 
40633ea072SLang Hames   InitializeNativeTarget();
41633ea072SLang Hames   InitializeNativeTargetAsmPrinter();
42633ea072SLang Hames 
43633ea072SLang Hames   cl::ParseCommandLineOptions(argc, argv, "LLJITDumpObjects");
44633ea072SLang Hames   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
45633ea072SLang Hames 
46633ea072SLang Hames   outs()
47633ea072SLang Hames       << "Usage notes:\n"
48633ea072SLang Hames          "  Use -debug-only=orc on debug builds to see log messages of objects "
49633ea072SLang Hames          "being dumped\n"
50633ea072SLang Hames          "  Specify -dump-dir to specify a dump directory\n"
51633ea072SLang Hames          "  Specify -dump-file-stem to override the dump file stem\n"
52633ea072SLang Hames          "  Specify -dump-jitted-objects=false to disable dumping\n";
53633ea072SLang Hames 
54633ea072SLang Hames   auto J = ExitOnErr(LLJITBuilder().create());
55633ea072SLang Hames 
56633ea072SLang Hames   if (DumpJITdObjects)
57633ea072SLang Hames     J->getObjTransformLayer().setTransform(DumpObjects(DumpDir, DumpFileStem));
58633ea072SLang Hames 
59633ea072SLang Hames   auto M = ExitOnErr(parseExampleModule(Add1Example, "add1"));
60633ea072SLang Hames 
61633ea072SLang Hames   ExitOnErr(J->addIRModule(std::move(M)));
62633ea072SLang Hames 
63633ea072SLang Hames   // Look up the JIT'd function, cast it to a function pointer, then call it.
64*16dcbb53SLang Hames   auto Add1Addr = ExitOnErr(J->lookup("add1"));
65*16dcbb53SLang Hames   int (*Add1)(int) = Add1Addr.toPtr<int(int)>();
66633ea072SLang Hames 
67633ea072SLang Hames   int Result = Add1(42);
68633ea072SLang Hames   outs() << "add1(42) = " << Result << "\n";
69633ea072SLang Hames 
70633ea072SLang Hames   return 0;
71633ea072SLang Hames }
72