xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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"
150b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
160b57cec5SDimitry Andric #include "llvm/Support/Error.h"
170b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
180b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
190b57cec5SDimitry Andric #include "llvm/Support/SmallVectorMemoryBuffer.h"
200b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #include <algorithm>
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric namespace llvm {
250b57cec5SDimitry Andric namespace orc {
260b57cec5SDimitry Andric 
27*5ffd83dbSDimitry Andric IRSymbolMapper::ManglingOptions
2813138422SDimitry Andric irManglingOptionsFromTargetOptions(const TargetOptions &Opts) {
29*5ffd83dbSDimitry Andric   IRSymbolMapper::ManglingOptions MO;
3013138422SDimitry Andric 
3113138422SDimitry Andric   MO.EmulatedTLS = Opts.EmulatedTLS;
3213138422SDimitry Andric 
3313138422SDimitry Andric   return MO;
3413138422SDimitry Andric }
3513138422SDimitry Andric 
360b57cec5SDimitry Andric /// Compile a Module to an ObjectFile.
3713138422SDimitry Andric Expected<SimpleCompiler::CompileResult> SimpleCompiler::operator()(Module &M) {
380b57cec5SDimitry Andric   CompileResult CachedObject = tryToLoadFromObjectCache(M);
390b57cec5SDimitry Andric   if (CachedObject)
4013138422SDimitry Andric     return std::move(CachedObject);
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric   SmallVector<char, 0> ObjBufferSV;
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric   {
450b57cec5SDimitry Andric     raw_svector_ostream ObjStream(ObjBufferSV);
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric     legacy::PassManager PM;
480b57cec5SDimitry Andric     MCContext *Ctx;
490b57cec5SDimitry Andric     if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
5013138422SDimitry Andric       return make_error<StringError>("Target does not support MC emission",
5113138422SDimitry Andric                                      inconvertibleErrorCode());
520b57cec5SDimitry Andric     PM.run(M);
530b57cec5SDimitry Andric   }
540b57cec5SDimitry Andric 
558bcb0991SDimitry Andric   auto ObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
56480093f4SDimitry Andric       std::move(ObjBufferSV), M.getModuleIdentifier() + "-jitted-objectbuffer");
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   auto Obj = object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
590b57cec5SDimitry Andric 
6013138422SDimitry Andric   if (!Obj)
6113138422SDimitry Andric     return Obj.takeError();
6213138422SDimitry Andric 
630b57cec5SDimitry Andric   notifyObjectCompiled(M, *ObjBuffer);
640b57cec5SDimitry Andric   return std::move(ObjBuffer);
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric SimpleCompiler::CompileResult
680b57cec5SDimitry Andric SimpleCompiler::tryToLoadFromObjectCache(const Module &M) {
690b57cec5SDimitry Andric   if (!ObjCache)
700b57cec5SDimitry Andric     return CompileResult();
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   return ObjCache->getObject(&M);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric void SimpleCompiler::notifyObjectCompiled(const Module &M,
760b57cec5SDimitry Andric                                           const MemoryBuffer &ObjBuffer) {
770b57cec5SDimitry Andric   if (ObjCache)
780b57cec5SDimitry Andric     ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric ConcurrentIRCompiler::ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
820b57cec5SDimitry Andric                                            ObjectCache *ObjCache)
8313138422SDimitry Andric     : IRCompiler(irManglingOptionsFromTargetOptions(JTMB.getOptions())),
8413138422SDimitry Andric       JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
850b57cec5SDimitry Andric 
8613138422SDimitry Andric Expected<std::unique_ptr<MemoryBuffer>>
8713138422SDimitry Andric ConcurrentIRCompiler::operator()(Module &M) {
880b57cec5SDimitry Andric   auto TM = cantFail(JTMB.createTargetMachine());
890b57cec5SDimitry Andric   SimpleCompiler C(*TM, ObjCache);
900b57cec5SDimitry Andric   return C(M);
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric } // end namespace orc
940b57cec5SDimitry Andric } // end namespace llvm
95