Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- CompileUtils.h - Utilities for compiling IR in the JIT ---*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/Orc/Layer.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
| 21 | namespace llvm { |
| 22 | |
| 23 | class MCContext; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 24 | class MemoryBuffer; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 25 | class Module; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 26 | class ObjectCache; |
| 27 | class TargetMachine; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 28 | |
| 29 | namespace orc { |
| 30 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 31 | IRSymbolMapper::ManglingOptions |
| 32 | irManglingOptionsFromTargetOptions(const TargetOptions &Opts); |
| 33 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 34 | /// Simple compile functor: Takes a single IR module and returns an ObjectFile. |
| 35 | /// This compiler supports a single compilation thread and LLVMContext only. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 36 | /// For multithreaded compilation, use ConcurrentIRCompiler below. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 37 | class SimpleCompiler : public IRCompileLayer::IRCompiler { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 38 | public: |
| 39 | using CompileResult = std::unique_ptr<MemoryBuffer>; |
| 40 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 41 | /// Construct a simple compile functor with the given target. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 43 | : IRCompiler(irManglingOptionsFromTargetOptions(TM.Options)), TM(TM), |
| 44 | ObjCache(ObjCache) {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 45 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 46 | /// Set an ObjectCache to query before compiling. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 47 | void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; } |
| 48 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 49 | /// Compile a Module to an ObjectFile. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 50 | Expected<CompileResult> operator()(Module &M) override; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 51 | |
| 52 | private: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 53 | IRSymbolMapper::ManglingOptions |
| 54 | manglingOptionsForTargetMachine(const TargetMachine &TM); |
| 55 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 56 | CompileResult tryToLoadFromObjectCache(const Module &M); |
| 57 | void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 58 | |
| 59 | TargetMachine &TM; |
| 60 | ObjectCache *ObjCache = nullptr; |
| 61 | }; |
| 62 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 63 | /// A SimpleCompiler that owns its TargetMachine. |
| 64 | /// |
| 65 | /// This convenient for clients who don't want to own their TargetMachines, |
| 66 | /// e.g. LLJIT. |
| 67 | class TMOwningSimpleCompiler : public SimpleCompiler { |
| 68 | public: |
| 69 | TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM, |
| 70 | ObjectCache *ObjCache = nullptr) |
| 71 | : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {} |
| 72 | |
| 73 | private: |
| 74 | // FIXME: shared because std::functions (and consequently |
| 75 | // IRCompileLayer::CompileFunction) are not moveable. |
| 76 | std::shared_ptr<llvm::TargetMachine> TM; |
| 77 | }; |
| 78 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 79 | /// A thread-safe version of SimpleCompiler. |
| 80 | /// |
| 81 | /// This class creates a new TargetMachine and SimpleCompiler instance for each |
| 82 | /// compile. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 83 | class ConcurrentIRCompiler : public IRCompileLayer::IRCompiler { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 84 | public: |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 85 | ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 86 | ObjectCache *ObjCache = nullptr); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 87 | |
| 88 | void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; } |
| 89 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 90 | Expected<std::unique_ptr<MemoryBuffer>> operator()(Module &M) override; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 91 | |
| 92 | private: |
| 93 | JITTargetMachineBuilder JTMB; |
| 94 | ObjectCache *ObjCache = nullptr; |
| 95 | }; |
| 96 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 97 | } // end namespace orc |
| 98 | |
| 99 | } // end namespace llvm |
| 100 | |
| 101 | #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H |