LLVM 22.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
20#include <memory>
21
22namespace llvm {
23
24class MemoryBuffer;
25class Module;
26class ObjectCache;
27class TargetMachine;
28
29namespace orc {
30
31LLVM_ABI IRSymbolMapper::ManglingOptions
32irManglingOptionsFromTargetOptions(const TargetOptions &Opts);
33
34/// Simple compile functor: Takes a single IR module and returns an ObjectFile.
35/// This compiler supports a single compilation thread and LLVMContext only.
36/// For multithreaded compilation, use ConcurrentIRCompiler below.
38public:
39 using CompileResult = std::unique_ptr<MemoryBuffer>;
40
41 /// Construct a simple compile functor with the given target.
42 SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
43 : IRCompiler(irManglingOptionsFromTargetOptions(TM.Options)), TM(TM),
44 ObjCache(ObjCache) {}
45
46 /// Set an ObjectCache to query before compiling.
47 void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
48
49 /// Compile a Module to an ObjectFile.
50 Expected<CompileResult> operator()(Module &M) override;
51
52private:
54 manglingOptionsForTargetMachine(const TargetMachine &TM);
55
56 CompileResult tryToLoadFromObjectCache(const Module &M);
57 void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer);
58
59 TargetMachine &TM;
60 ObjectCache *ObjCache = nullptr;
61};
62
63/// A SimpleCompiler that owns its TargetMachine.
64///
65/// This is convenient for clients who don't want to own their TargetMachines,
66/// e.g. LLJIT.
68public:
69 TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM,
70 ObjectCache *ObjCache = nullptr)
71 : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {}
72
73private:
74 // FIXME: shared because std::functions (and consequently
75 // IRCompileLayer::CompileFunction) are not moveable.
76 std::shared_ptr<llvm::TargetMachine> TM;
77};
78
79/// A thread-safe version of SimpleCompiler.
80///
81/// This class creates a new TargetMachine and SimpleCompiler instance for each
82/// compile.
84public:
86 ObjectCache *ObjCache = nullptr);
87
88 void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
89
90 Expected<std::unique_ptr<MemoryBuffer>> operator()(Module &M) override;
91
92private:
94 ObjectCache *ObjCache = nullptr;
95};
96
97} // end namespace orc
98
99} // end namespace llvm
100
101#endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
#define LLVM_ABI
Definition: Compiler.h:213
static LVOptions Options
Definition: LVOptions.cpp:25
Machine Check Debug Module
Tagged union holding either a T or a Error.
Definition: Error.h:485
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:52
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:24
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:83
A thread-safe version of SimpleCompiler.
Definition: CompileUtils.h:83
void setObjectCache(ObjectCache *ObjCache)
Definition: CompileUtils.h:88
A utility class for building TargetMachines for JITs.
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:37
std::unique_ptr< MemoryBuffer > CompileResult
Definition: CompileUtils.h:39
SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache=nullptr)
Construct a simple compile functor with the given target.
Definition: CompileUtils.h:42
void setObjectCache(ObjectCache *NewCache)
Set an ObjectCache to query before compiling.
Definition: CompileUtils.h:47
A SimpleCompiler that owns its TargetMachine.
Definition: CompileUtils.h:67
TMOwningSimpleCompiler(std::unique_ptr< TargetMachine > TM, ObjectCache *ObjCache=nullptr)
Definition: CompileUtils.h:69
LLVM_ABI IRSymbolMapper::ManglingOptions irManglingOptionsFromTargetOptions(const TargetOptions &Opts)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856