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 | |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/ExecutionEngine/ObjectCache.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManager.h" |
| 20 | #include "llvm/Object/Binary.h" |
| 21 | #include "llvm/Object/ObjectFile.h" |
| 22 | #include "llvm/Support/Error.h" |
| 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 25 | #include "llvm/Support/SmallVectorMemoryBuffer.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include <algorithm> |
| 29 | #include <memory> |
| 30 | |
| 31 | namespace llvm { |
| 32 | |
| 33 | class MCContext; |
| 34 | class Module; |
| 35 | |
| 36 | namespace orc { |
| 37 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 38 | /// Simple compile functor: Takes a single IR module and returns an ObjectFile. |
| 39 | /// This compiler supports a single compilation thread and LLVMContext only. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 40 | /// For multithreaded compilation, use ConcurrentIRCompiler below. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | class SimpleCompiler { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | public: |
| 43 | using CompileResult = std::unique_ptr<MemoryBuffer>; |
| 44 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 45 | /// Construct a simple compile functor with the given target. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 46 | SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr) |
| 47 | : TM(TM), ObjCache(ObjCache) {} |
| 48 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 49 | /// Set an ObjectCache to query before compiling. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 50 | void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; } |
| 51 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 52 | /// Compile a Module to an ObjectFile. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 53 | CompileResult operator()(Module &M) { |
| 54 | CompileResult CachedObject = tryToLoadFromObjectCache(M); |
| 55 | if (CachedObject) |
| 56 | return CachedObject; |
| 57 | |
| 58 | SmallVector<char, 0> ObjBufferSV; |
| 59 | |
| 60 | { |
| 61 | raw_svector_ostream ObjStream(ObjBufferSV); |
| 62 | |
| 63 | legacy::PassManager PM; |
| 64 | MCContext *Ctx; |
| 65 | if (TM.addPassesToEmitMC(PM, Ctx, ObjStream)) |
| 66 | llvm_unreachable("Target does not support MC emission."); |
| 67 | PM.run(M); |
| 68 | } |
| 69 | |
| 70 | auto ObjBuffer = |
| 71 | llvm::make_unique<SmallVectorMemoryBuffer>(std::move(ObjBufferSV)); |
| 72 | auto Obj = |
| 73 | object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()); |
| 74 | |
| 75 | if (Obj) { |
| 76 | notifyObjectCompiled(M, *ObjBuffer); |
| 77 | return std::move(ObjBuffer); |
| 78 | } |
| 79 | |
| 80 | // TODO: Actually report errors helpfully. |
| 81 | consumeError(Obj.takeError()); |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | |
| 87 | CompileResult tryToLoadFromObjectCache(const Module &M) { |
| 88 | if (!ObjCache) |
| 89 | return CompileResult(); |
| 90 | |
| 91 | return ObjCache->getObject(&M); |
| 92 | } |
| 93 | |
| 94 | void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer) { |
| 95 | if (ObjCache) |
| 96 | ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef()); |
| 97 | } |
| 98 | |
| 99 | TargetMachine &TM; |
| 100 | ObjectCache *ObjCache = nullptr; |
| 101 | }; |
| 102 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 103 | /// A thread-safe version of SimpleCompiler. |
| 104 | /// |
| 105 | /// This class creates a new TargetMachine and SimpleCompiler instance for each |
| 106 | /// compile. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 107 | class ConcurrentIRCompiler { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 108 | public: |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 109 | ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, |
| 110 | ObjectCache *ObjCache = nullptr) |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 111 | : JTMB(std::move(JTMB)), ObjCache(ObjCache) {} |
| 112 | |
| 113 | void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; } |
| 114 | |
| 115 | std::unique_ptr<MemoryBuffer> operator()(Module &M) { |
| 116 | auto TM = cantFail(JTMB.createTargetMachine()); |
| 117 | SimpleCompiler C(*TM, ObjCache); |
| 118 | return C(M); |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | JITTargetMachineBuilder JTMB; |
| 123 | ObjectCache *ObjCache = nullptr; |
| 124 | }; |
| 125 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 126 | } // end namespace orc |
| 127 | |
| 128 | } // end namespace llvm |
| 129 | |
| 130 | #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H |