1 //===- CompileUtils.h - Utilities for compiling IR in the JIT ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Contains utilities for compiling IR to object files.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
14 #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
16 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
29 class JITTargetMachineBuilder
;
31 /// Simple compile functor: Takes a single IR module and returns an ObjectFile.
32 /// This compiler supports a single compilation thread and LLVMContext only.
33 /// For multithreaded compilation, use ConcurrentIRCompiler below.
34 class SimpleCompiler
{
36 using CompileResult
= std::unique_ptr
<MemoryBuffer
>;
38 /// Construct a simple compile functor with the given target.
39 SimpleCompiler(TargetMachine
&TM
, ObjectCache
*ObjCache
= nullptr)
40 : TM(TM
), ObjCache(ObjCache
) {}
42 /// Set an ObjectCache to query before compiling.
43 void setObjectCache(ObjectCache
*NewCache
) { ObjCache
= NewCache
; }
45 /// Compile a Module to an ObjectFile.
46 CompileResult
operator()(Module
&M
);
49 CompileResult
tryToLoadFromObjectCache(const Module
&M
);
50 void notifyObjectCompiled(const Module
&M
, const MemoryBuffer
&ObjBuffer
);
53 ObjectCache
*ObjCache
= nullptr;
56 /// A SimpleCompiler that owns its TargetMachine.
58 /// This convenient for clients who don't want to own their TargetMachines,
60 class TMOwningSimpleCompiler
: public SimpleCompiler
{
62 TMOwningSimpleCompiler(std::unique_ptr
<TargetMachine
> TM
,
63 ObjectCache
*ObjCache
= nullptr)
64 : SimpleCompiler(*TM
, ObjCache
), TM(std::move(TM
)) {}
67 // FIXME: shared because std::functions (and consequently
68 // IRCompileLayer::CompileFunction) are not moveable.
69 std::shared_ptr
<llvm::TargetMachine
> TM
;
72 /// A thread-safe version of SimpleCompiler.
74 /// This class creates a new TargetMachine and SimpleCompiler instance for each
76 class ConcurrentIRCompiler
{
78 ConcurrentIRCompiler(JITTargetMachineBuilder JTMB
,
79 ObjectCache
*ObjCache
= nullptr);
81 void setObjectCache(ObjectCache
*ObjCache
) { this->ObjCache
= ObjCache
; }
83 std::unique_ptr
<MemoryBuffer
> operator()(Module
&M
);
86 JITTargetMachineBuilder JTMB
;
87 ObjectCache
*ObjCache
= nullptr;
90 } // end namespace orc
92 } // end namespace llvm
94 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H