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