Driver.hpp
1 #pragma once
2 #include <JitCpp/Compiler/Compiler.hpp>
3 
4 #include <llvm/ExecutionEngine/ExecutionEngine.h>
5 #include <llvm/Support/PrettyStackTrace.h>
6 
7 namespace Jit
8 {
9 
10 struct Driver
11 {
12  Driver(const std::string& fname)
13  : X{0, nullptr}
14  , ts_ctx{std::make_unique<llvm::LLVMContext>()}
15  , factory_name{fname}
16  {
17  }
18 
19  template <typename Fun_T>
20  std::function<Fun_T> operator()(
21  const std::string& sourceCode, const std::vector<std::string>& flags,
22  CompilerOptions opts)
23  {
24  auto t0 = std::chrono::high_resolution_clock::now();
25 
26  auto sourceFileName = saveSourceFile(sourceCode);
27  if(!sourceFileName)
28  return {};
29 
30  std::string cpp = *sourceFileName;
31  auto filename = QFileInfo(QString::fromStdString(cpp)).fileName();
32 
33  qDebug("Compiling...");
34  jit.compile(cpp, flags, opts, ts_ctx);
35  auto t1 = std::chrono::high_resolution_clock::now();
36 
37  if(!jit.errors().isEmpty())
38  {
39  throw std::runtime_error{jit.errors().toStdString()};
40  }
41 
42  auto jitedFn = jit.getFunction<Fun_T>(factory_name);
43  if(!jitedFn)
44  {
45  throw Exception{jitedFn.takeError()};
46  }
47 
48  llvm::outs().flush();
49  std::cerr << "\n\nADDON BUILD DURATION: "
50  << std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count()
51  << " ms \n\n";
52 
53  return *jitedFn;
54  }
55 
56  llvm::PrettyStackTraceProgram X;
57  llvm::LLVMContext context;
58  llvm::orc::ThreadSafeContext ts_ctx;
59  JitCompiler jit;
60  std::string factory_name;
61 };
62 
63 }
Definition: Compiler.hpp:13
Definition: JitOptions.hpp:7
Definition: Driver.hpp:11
Definition: JitUtils.hpp:16