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/IRCompileLayer.h"
17 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
18 #include "llvm/ExecutionEngine/Orc/Layer.h"
30 IRSymbolMapper::ManglingOptions
31 irManglingOptionsFromTargetOptions(const TargetOptions &Opts);
33 /// Simple compile functor: Takes a single IR module and returns an ObjectFile.
34 /// This compiler supports a single compilation thread and LLVMContext only.
35 /// For multithreaded compilation, use ConcurrentIRCompiler below.
36 class SimpleCompiler : public IRCompileLayer::IRCompiler {
38 using CompileResult = std::unique_ptr<MemoryBuffer>;
40 /// Construct a simple compile functor with the given target.
41 SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
42 : IRCompiler(irManglingOptionsFromTargetOptions(TM.Options)), TM(TM),
45 /// Set an ObjectCache to query before compiling.
46 void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
48 /// Compile a Module to an ObjectFile.
49 Expected<CompileResult> operator()(Module &M) override;
52 IRSymbolMapper::ManglingOptions
53 manglingOptionsForTargetMachine(const TargetMachine &TM);
55 CompileResult tryToLoadFromObjectCache(const Module &M);
56 void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer);
59 ObjectCache *ObjCache = nullptr;
62 /// A SimpleCompiler that owns its TargetMachine.
64 /// This convenient for clients who don't want to own their TargetMachines,
66 class TMOwningSimpleCompiler : public SimpleCompiler {
68 TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM,
69 ObjectCache *ObjCache = nullptr)
70 : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {}
73 // FIXME: shared because std::functions (and consequently
74 // IRCompileLayer::CompileFunction) are not moveable.
75 std::shared_ptr<llvm::TargetMachine> TM;
78 /// A thread-safe version of SimpleCompiler.
80 /// This class creates a new TargetMachine and SimpleCompiler instance for each
82 class ConcurrentIRCompiler : public IRCompileLayer::IRCompiler {
84 ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
85 ObjectCache *ObjCache = nullptr);
87 void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
89 Expected<std::unique_ptr<MemoryBuffer>> operator()(Module &M) override;
92 JITTargetMachineBuilder JTMB;
93 ObjectCache *ObjCache = nullptr;
96 } // end namespace orc
98 } // end namespace llvm
100 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H