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 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
| 19 | namespace llvm { |
| 20 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 21 | class JITTargetMachineBuilder; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 22 | class MCContext; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 23 | class MemoryBuffer; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 24 | class Module; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 25 | class ObjectCache; |
| 26 | class TargetMachine; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 27 | |
| 28 | namespace orc { |
| 29 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 30 | /// Simple compile functor: Takes a single IR module and returns an ObjectFile. |
| 31 | /// This compiler supports a single compilation thread and LLVMContext only. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 32 | /// For multithreaded compilation, use ConcurrentIRCompiler below. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 33 | class SimpleCompiler { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 34 | public: |
| 35 | using CompileResult = std::unique_ptr<MemoryBuffer>; |
| 36 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 37 | /// Construct a simple compile functor with the given target. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 38 | SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr) |
| 39 | : TM(TM), ObjCache(ObjCache) {} |
| 40 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 41 | /// Set an ObjectCache to query before compiling. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; } |
| 43 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 44 | /// Compile a Module to an ObjectFile. |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 45 | CompileResult operator()(Module &M); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 46 | |
| 47 | private: |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 48 | CompileResult tryToLoadFromObjectCache(const Module &M); |
| 49 | void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 50 | |
| 51 | TargetMachine &TM; |
| 52 | ObjectCache *ObjCache = nullptr; |
| 53 | }; |
| 54 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 55 | /// A thread-safe version of SimpleCompiler. |
| 56 | /// |
| 57 | /// This class creates a new TargetMachine and SimpleCompiler instance for each |
| 58 | /// compile. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 59 | class ConcurrentIRCompiler { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 60 | public: |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 61 | ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 62 | ObjectCache *ObjCache = nullptr); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 63 | |
| 64 | void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; } |
| 65 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 66 | std::unique_ptr<MemoryBuffer> operator()(Module &M); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | JITTargetMachineBuilder JTMB; |
| 70 | ObjectCache *ObjCache = nullptr; |
| 71 | }; |
| 72 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | } // end namespace orc |
| 74 | |
| 75 | } // end namespace llvm |
| 76 | |
| 77 | #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H |