xref: /freebsd-src/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric //===------ CompileUtils.cpp - Utilities for compiling IR in the JIT ------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
120b57cec5SDimitry Andric #include "llvm/ExecutionEngine/ObjectCache.h"
130b57cec5SDimitry Andric #include "llvm/IR/LegacyPassManager.h"
140b57cec5SDimitry Andric #include "llvm/IR/Module.h"
15*81ad6265SDimitry Andric #include "llvm/MC/MCContext.h"
160b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
170b57cec5SDimitry Andric #include "llvm/Support/Error.h"
180b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
190b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
200b57cec5SDimitry Andric #include "llvm/Support/SmallVectorMemoryBuffer.h"
210b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #include <algorithm>
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace llvm {
260b57cec5SDimitry Andric namespace orc {
270b57cec5SDimitry Andric 
285ffd83dbSDimitry Andric IRSymbolMapper::ManglingOptions
irManglingOptionsFromTargetOptions(const TargetOptions & Opts)2913138422SDimitry Andric irManglingOptionsFromTargetOptions(const TargetOptions &Opts) {
305ffd83dbSDimitry Andric   IRSymbolMapper::ManglingOptions MO;
3113138422SDimitry Andric 
3213138422SDimitry Andric   MO.EmulatedTLS = Opts.EmulatedTLS;
3313138422SDimitry Andric 
3413138422SDimitry Andric   return MO;
3513138422SDimitry Andric }
3613138422SDimitry Andric 
370b57cec5SDimitry Andric /// Compile a Module to an ObjectFile.
operator ()(Module & M)3813138422SDimitry Andric Expected<SimpleCompiler::CompileResult> SimpleCompiler::operator()(Module &M) {
390b57cec5SDimitry Andric   CompileResult CachedObject = tryToLoadFromObjectCache(M);
400b57cec5SDimitry Andric   if (CachedObject)
4113138422SDimitry Andric     return std::move(CachedObject);
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   SmallVector<char, 0> ObjBufferSV;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   {
460b57cec5SDimitry Andric     raw_svector_ostream ObjStream(ObjBufferSV);
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric     legacy::PassManager PM;
490b57cec5SDimitry Andric     MCContext *Ctx;
500b57cec5SDimitry Andric     if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
5113138422SDimitry Andric       return make_error<StringError>("Target does not support MC emission",
5213138422SDimitry Andric                                      inconvertibleErrorCode());
530b57cec5SDimitry Andric     PM.run(M);
540b57cec5SDimitry Andric   }
550b57cec5SDimitry Andric 
568bcb0991SDimitry Andric   auto ObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
570eae32dcSDimitry Andric       std::move(ObjBufferSV), M.getModuleIdentifier() + "-jitted-objectbuffer",
580eae32dcSDimitry Andric       /*RequiresNullTerminator=*/false);
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   auto Obj = object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
610b57cec5SDimitry Andric 
6213138422SDimitry Andric   if (!Obj)
6313138422SDimitry Andric     return Obj.takeError();
6413138422SDimitry Andric 
650b57cec5SDimitry Andric   notifyObjectCompiled(M, *ObjBuffer);
660b57cec5SDimitry Andric   return std::move(ObjBuffer);
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric SimpleCompiler::CompileResult
tryToLoadFromObjectCache(const Module & M)700b57cec5SDimitry Andric SimpleCompiler::tryToLoadFromObjectCache(const Module &M) {
710b57cec5SDimitry Andric   if (!ObjCache)
720b57cec5SDimitry Andric     return CompileResult();
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   return ObjCache->getObject(&M);
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric 
notifyObjectCompiled(const Module & M,const MemoryBuffer & ObjBuffer)770b57cec5SDimitry Andric void SimpleCompiler::notifyObjectCompiled(const Module &M,
780b57cec5SDimitry Andric                                           const MemoryBuffer &ObjBuffer) {
790b57cec5SDimitry Andric   if (ObjCache)
800b57cec5SDimitry Andric     ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,ObjectCache * ObjCache)830b57cec5SDimitry Andric ConcurrentIRCompiler::ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
840b57cec5SDimitry Andric                                            ObjectCache *ObjCache)
8513138422SDimitry Andric     : IRCompiler(irManglingOptionsFromTargetOptions(JTMB.getOptions())),
8613138422SDimitry Andric       JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
870b57cec5SDimitry Andric 
8813138422SDimitry Andric Expected<std::unique_ptr<MemoryBuffer>>
operator ()(Module & M)8913138422SDimitry Andric ConcurrentIRCompiler::operator()(Module &M) {
900b57cec5SDimitry Andric   auto TM = cantFail(JTMB.createTargetMachine());
910b57cec5SDimitry Andric   SimpleCompiler C(*TM, ObjCache);
920b57cec5SDimitry Andric   return C(M);
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric } // end namespace orc
960b57cec5SDimitry Andric } // end namespace llvm
97