TPDE
Loading...
Searching...
No Matches
OrcCompiler.hpp
1// SPDX-FileCopyrightText: 2025 Contributors to TPDE <https://tpde.org>
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3#pragma once
4
5#include <llvm/ExecutionEngine/Orc/IRCompileLayer.h>
6#include <llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h>
7#include <llvm/Support/Error.h>
8#include <llvm/Support/MemoryBuffer.h>
9#include <llvm/Target/TargetMachine.h>
10
11#include <memory>
12
13#include "tpde-llvm/LLVMCompiler.hpp"
14
15namespace tpde_llvm {
16
17/// ORC Compiler functor using TPDE-LLVM, which can transparently fall back to
18/// LLVM's SimpleCompiler (if a target machine is provided). Intended as a
19/// typical drop-in replacement for llvm::orc::SimpleCompiler.
20class OrcCompiler : public llvm::orc::IRCompileLayer::IRCompiler {
21 std::unique_ptr<LLVMCompiler> owned_compiler;
22 LLVMCompiler *compiler;
23 llvm::TargetMachine *tm;
24
25public:
26 /// Constructor. If the TargetMachine non-null, a failure within TPDE
27 /// (e.g., due to unsupported IR constructs) will fall back to LLVM.
28 OrcCompiler(LLVMCompiler *compiler, llvm::TargetMachine *tm = nullptr)
29 : IRCompiler({}), compiler(compiler), tm(tm) {}
30 /// Constructor. If the TargetMachine non-null, a failure within TPDE
31 /// (e.g., due to unsupported IR constructs) will fall back to LLVM.
32 OrcCompiler(const llvm::Triple &triple, llvm::TargetMachine *tm = nullptr)
33 : IRCompiler({}),
34 owned_compiler(LLVMCompiler::create(triple)),
35 compiler(owned_compiler.get()),
36 tm(tm) {}
37 /// Constructor, compatible with llvm::orc::SimpleCompiler.
38 OrcCompiler(llvm::TargetMachine &tm)
39 : IRCompiler({}),
40 owned_compiler(LLVMCompiler::create(tm.getTargetTriple())),
41 compiler(owned_compiler.get()),
42 tm(&tm) {}
43
44 llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
45 operator()(llvm::Module &) override;
46};
47
48/// A very simple thread-safe version of OrcCompiler, intended as a typical
49/// drop-in replacement for llvm::orc::ConcurrentIRCompiler. This is not the
50/// most efficient way, applications could (and probably should) cache a
51/// LLVMCompiler and TargetMachine in thread-local storage.
52class ConcurrentOrcCompiler : public llvm::orc::IRCompileLayer::IRCompiler {
53 llvm::orc::JITTargetMachineBuilder jtmb;
54
55public:
56 /// Constructor. For every compilation, a new LLVMCompiler is constructed
57 /// based on the target triple of the JITTargetMachineBuilder. Likewise, for
58 /// every fallback, a new TargetMachine is constructed.
59 ConcurrentOrcCompiler(llvm::orc::JITTargetMachineBuilder jtmb)
60 : IRCompiler({}), jtmb(std::move(jtmb)) {}
61
62 llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
63 operator()(llvm::Module &) override;
64};
65
66} // namespace tpde_llvm
ConcurrentOrcCompiler(llvm::orc::JITTargetMachineBuilder jtmb)
Compiler for LLVM modules.
static std::unique_ptr< LLVMCompiler > create(const llvm::Triple &triple) noexcept
OrcCompiler(llvm::TargetMachine &tm)
Constructor, compatible with llvm::orc::SimpleCompiler.
OrcCompiler(const llvm::Triple &triple, llvm::TargetMachine *tm=nullptr)
OrcCompiler(LLVMCompiler *compiler, llvm::TargetMachine *tm=nullptr)