| 1 | //===------ CompileUtils.cpp - Utilities for compiling IR in the JIT ------===// |
| 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 | #include "llvm/ExecutionEngine/Orc/CompileUtils.h" |
| 10 | |
| 11 | #include "llvm/ADT/SmallVector.h" |
| 12 | #include "llvm/ExecutionEngine/ObjectCache.h" |
| 13 | #include "llvm/IR/LegacyPassManager.h" |
| 14 | #include "llvm/IR/Module.h" |
| 15 | #include "llvm/MC/MCContext.h" |
| 16 | #include "llvm/Object/ObjectFile.h" |
| 17 | #include "llvm/Support/Error.h" |
| 18 | #include "llvm/Support/MemoryBuffer.h" |
| 19 | #include "llvm/Support/SmallVectorMemoryBuffer.h" |
| 20 | #include "llvm/Target/TargetMachine.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | namespace orc { |
| 24 | |
| 25 | IRSymbolMapper::ManglingOptions |
| 26 | irManglingOptionsFromTargetOptions(const TargetOptions &Opts) { |
| 27 | IRSymbolMapper::ManglingOptions MO; |
| 28 | |
| 29 | MO.EmulatedTLS = Opts.EmulatedTLS; |
| 30 | |
| 31 | return MO; |
| 32 | } |
| 33 | |
| 34 | /// Compile a Module to an ObjectFile. |
| 35 | Expected<SimpleCompiler::CompileResult> SimpleCompiler::operator()(Module &M) { |
| 36 | if (M.getDataLayout().isDefault()) |
| 37 | M.setDataLayout(TM.createDataLayout()); |
| 38 | |
| 39 | CompileResult CachedObject = tryToLoadFromObjectCache(M); |
| 40 | if (CachedObject) |
| 41 | return std::move(CachedObject); |
| 42 | |
| 43 | SmallVector<char, 0> ObjBufferSV; |
| 44 | |
| 45 | { |
| 46 | raw_svector_ostream ObjStream(ObjBufferSV); |
| 47 | |
| 48 | legacy::PassManager PM; |
| 49 | MCContext *Ctx; |
| 50 | if (TM.addPassesToEmitMC(PM, Ctx, ObjStream)) |
| 51 | return make_error<StringError>(Args: "Target does not support MC emission" , |
| 52 | Args: inconvertibleErrorCode()); |
| 53 | PM.run(M); |
| 54 | } |
| 55 | |
| 56 | auto ObjBuffer = std::make_unique<SmallVectorMemoryBuffer>( |
| 57 | args: std::move(ObjBufferSV), args: M.getModuleIdentifier() + "-jitted-objectbuffer" , |
| 58 | /*RequiresNullTerminator=*/args: false); |
| 59 | |
| 60 | auto Obj = object::ObjectFile::createObjectFile(Object: ObjBuffer->getMemBufferRef()); |
| 61 | |
| 62 | if (!Obj) |
| 63 | return Obj.takeError(); |
| 64 | |
| 65 | notifyObjectCompiled(M, ObjBuffer: *ObjBuffer); |
| 66 | return std::move(ObjBuffer); |
| 67 | } |
| 68 | |
| 69 | SimpleCompiler::CompileResult |
| 70 | SimpleCompiler::tryToLoadFromObjectCache(const Module &M) { |
| 71 | if (!ObjCache) |
| 72 | return CompileResult(); |
| 73 | |
| 74 | return ObjCache->getObject(M: &M); |
| 75 | } |
| 76 | |
| 77 | void SimpleCompiler::notifyObjectCompiled(const Module &M, |
| 78 | const MemoryBuffer &ObjBuffer) { |
| 79 | if (ObjCache) |
| 80 | ObjCache->notifyObjectCompiled(M: &M, Obj: ObjBuffer.getMemBufferRef()); |
| 81 | } |
| 82 | |
| 83 | ConcurrentIRCompiler::ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, |
| 84 | ObjectCache *ObjCache) |
| 85 | : IRCompiler(irManglingOptionsFromTargetOptions(Opts: JTMB.getOptions())), |
| 86 | JTMB(std::move(JTMB)), ObjCache(ObjCache) {} |
| 87 | |
| 88 | Expected<std::unique_ptr<MemoryBuffer>> |
| 89 | ConcurrentIRCompiler::operator()(Module &M) { |
| 90 | auto TM = cantFail(ValOrErr: JTMB.createTargetMachine()); |
| 91 | SimpleCompiler C(*TM, ObjCache); |
| 92 | return C(M); |
| 93 | } |
| 94 | |
| 95 | } // end namespace orc |
| 96 | } // end namespace llvm |
| 97 | |