LLVM  15.0.0git
CompileUtils.h
Go to the documentation of this file.
1 //===- CompileUtils.h - Utilities for compiling IR in the JIT ---*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Contains utilities for compiling IR to object files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
14 #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
15 
19 #include <memory>
20 
21 namespace llvm {
22 
23 class MemoryBuffer;
24 class Module;
25 class ObjectCache;
26 class TargetMachine;
27 
28 namespace orc {
29 
30 IRSymbolMapper::ManglingOptions
31 irManglingOptionsFromTargetOptions(const TargetOptions &Opts);
32 
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.
37 public:
38  using CompileResult = std::unique_ptr<MemoryBuffer>;
39 
40  /// Construct a simple compile functor with the given target.
41  SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
43  ObjCache(ObjCache) {}
44 
45  /// Set an ObjectCache to query before compiling.
46  void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
47 
48  /// Compile a Module to an ObjectFile.
50 
51 private:
53  manglingOptionsForTargetMachine(const TargetMachine &TM);
54 
55  CompileResult tryToLoadFromObjectCache(const Module &M);
56  void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer);
57 
58  TargetMachine &TM;
59  ObjectCache *ObjCache = nullptr;
60 };
61 
62 /// A SimpleCompiler that owns its TargetMachine.
63 ///
64 /// This convenient for clients who don't want to own their TargetMachines,
65 /// e.g. LLJIT.
67 public:
68  TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM,
69  ObjectCache *ObjCache = nullptr)
70  : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {}
71 
72 private:
73  // FIXME: shared because std::functions (and consequently
74  // IRCompileLayer::CompileFunction) are not moveable.
75  std::shared_ptr<llvm::TargetMachine> TM;
76 };
77 
78 /// A thread-safe version of SimpleCompiler.
79 ///
80 /// This class creates a new TargetMachine and SimpleCompiler instance for each
81 /// compile.
83 public:
85  ObjectCache *ObjCache = nullptr);
86 
87  void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
88 
90 
91 private:
93  ObjectCache *ObjCache = nullptr;
94 };
95 
96 } // end namespace orc
97 
98 } // end namespace llvm
99 
100 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
llvm::orc::ConcurrentIRCompiler::operator()
Expected< std::unique_ptr< MemoryBuffer > > operator()(Module &M) override
Definition: CompileUtils.cpp:89
llvm::orc::JITTargetMachineBuilder
A utility class for building TargetMachines for JITs.
Definition: JITTargetMachineBuilder.h:34
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::orc::TMOwningSimpleCompiler
A SimpleCompiler that owns its TargetMachine.
Definition: CompileUtils.h:66
llvm::orc::ConcurrentIRCompiler::ConcurrentIRCompiler
ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, ObjectCache *ObjCache=nullptr)
Definition: CompileUtils.cpp:83
llvm::orc::SimpleCompiler::setObjectCache
void setObjectCache(ObjectCache *NewCache)
Set an ObjectCache to query before compiling.
Definition: CompileUtils.h:46
llvm::ObjectCache
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:23
llvm::orc::SimpleCompiler::CompileResult
std::unique_ptr< MemoryBuffer > CompileResult
Definition: CompileUtils.h:38
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
Layer.h
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::orc::SimpleCompiler
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:36
llvm::orc::IRCompileLayer::IRCompiler::IRCompiler
IRCompiler(IRSymbolMapper::ManglingOptions MO)
Definition: IRCompileLayer.h:35
llvm::orc::SimpleCompiler::SimpleCompiler
SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache=nullptr)
Construct a simple compile functor with the given target.
Definition: CompileUtils.h:41
llvm::orc::IRCompileLayer::IRCompiler
Definition: IRCompileLayer.h:33
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
IRCompileLayer.h
llvm::orc::irManglingOptionsFromTargetOptions
IRSymbolMapper::ManglingOptions irManglingOptionsFromTargetOptions(const TargetOptions &Opts)
Definition: CompileUtils.cpp:29
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1675
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::orc::IRSymbolMapper::ManglingOptions
Definition: Mangling.h:41
std
Definition: BitVector.h:851
llvm::orc::ConcurrentIRCompiler::setObjectCache
void setObjectCache(ObjectCache *ObjCache)
Definition: CompileUtils.h:87
llvm::orc::SimpleCompiler::operator()
Expected< CompileResult > operator()(Module &M) override
Compile a Module to an ObjectFile.
Definition: CompileUtils.cpp:38
llvm::orc::ConcurrentIRCompiler
A thread-safe version of SimpleCompiler.
Definition: CompileUtils.h:82
llvm::orc::TMOwningSimpleCompiler::TMOwningSimpleCompiler
TMOwningSimpleCompiler(std::unique_ptr< TargetMachine > TM, ObjectCache *ObjCache=nullptr)
Definition: CompileUtils.h:68
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
JITTargetMachineBuilder.h