3#include <llvm/Bitcode/BitcodeReader.h>
4#include <llvm/IR/Module.h>
5#include <llvm/Support/Error.h>
6#include <llvm/Support/FileSystem.h>
7#include <llvm/Support/MemoryBuffer.h>
19 using std::runtime_error::runtime_error;
21 : std::runtime_error{
"JIT error"}
23 llvm::handleAllErrors(
24 std::move(E), [&](
const llvm::ErrorInfoBase& EI) { m_err = EI.message(); });
27 const char* what()
const noexcept override {
return m_err.c_str(); }
37inline void parkCompiler(std::shared_ptr<void> compiler)
39 static std::list<std::shared_ptr<void>> parked;
41 parked.push_back(std::move(compiler));
46 std::chrono::high_resolution_clock::time_point t0;
47 Timer() { t0 =
decltype(t0)::clock::now(); }
50 auto t1 =
decltype(t0)::clock::now();
51 std::cerr <<
"Took time: "
52 << std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count()
57inline llvm::Expected<std::unique_ptr<llvm::Module>>
58readModuleFromBitcodeFile(llvm::StringRef bc, llvm::LLVMContext& context)
60 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer
61 = llvm::MemoryBuffer::getFile(bc);
63 return llvm::errorCodeToError(buffer.getError());
65 return llvm::parseBitcodeFile(buffer.get()->getMemBufferRef(), context);
68static inline std::string replaceExtension(llvm::StringRef name, llvm::StringRef ext)
70 return name.substr(0, name.find_last_of(
'.') + 1).str() + ext.str();
73static inline llvm::Error return_code_error(llvm::StringRef message,
int returnCode)
75 return llvm::make_error<llvm::StringError>(
76 message, std::error_code(returnCode, std::system_category()));
79static inline llvm::Expected<std::string> saveSourceFile(
const std::string& content)
81 using llvm::sys::fs::createTemporaryFile;
84 llvm::SmallString<128> name;
85 if(
auto ec = createTemporaryFile(
"score-addon-cpp",
"cpp", fd, name))
86 return llvm::errorCodeToError(ec);
88 constexpr bool shouldClose =
true;
89 constexpr bool unbuffered =
true;
90 llvm::raw_fd_ostream os(fd, shouldClose, unbuffered);
93 return name.str().str();
Definition JitUtils.hpp:18
Definition JitUtils.hpp:45