xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
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 
270b57cec5SDimitry Andric /// Compile a Module to an ObjectFile.
280b57cec5SDimitry Andric SimpleCompiler::CompileResult SimpleCompiler::operator()(Module &M) {
290b57cec5SDimitry Andric   CompileResult CachedObject = tryToLoadFromObjectCache(M);
300b57cec5SDimitry Andric   if (CachedObject)
310b57cec5SDimitry Andric     return CachedObject;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric   SmallVector<char, 0> ObjBufferSV;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   {
360b57cec5SDimitry Andric     raw_svector_ostream ObjStream(ObjBufferSV);
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric     legacy::PassManager PM;
390b57cec5SDimitry Andric     MCContext *Ctx;
400b57cec5SDimitry Andric     if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
410b57cec5SDimitry Andric       llvm_unreachable("Target does not support MC emission.");
420b57cec5SDimitry Andric     PM.run(M);
430b57cec5SDimitry Andric   }
440b57cec5SDimitry Andric 
45*8bcb0991SDimitry Andric   auto ObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
460b57cec5SDimitry Andric       std::move(ObjBufferSV),
470b57cec5SDimitry Andric       "<in memory object compiled from " + M.getModuleIdentifier() + ">");
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   auto Obj = object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   if (Obj) {
520b57cec5SDimitry Andric     notifyObjectCompiled(M, *ObjBuffer);
530b57cec5SDimitry Andric     return std::move(ObjBuffer);
540b57cec5SDimitry Andric   }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   // TODO: Actually report errors helpfully.
570b57cec5SDimitry Andric   consumeError(Obj.takeError());
580b57cec5SDimitry Andric   return nullptr;
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric SimpleCompiler::CompileResult
620b57cec5SDimitry Andric SimpleCompiler::tryToLoadFromObjectCache(const Module &M) {
630b57cec5SDimitry Andric   if (!ObjCache)
640b57cec5SDimitry Andric     return CompileResult();
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   return ObjCache->getObject(&M);
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric void SimpleCompiler::notifyObjectCompiled(const Module &M,
700b57cec5SDimitry Andric                                           const MemoryBuffer &ObjBuffer) {
710b57cec5SDimitry Andric   if (ObjCache)
720b57cec5SDimitry Andric     ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric ConcurrentIRCompiler::ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
760b57cec5SDimitry Andric                                            ObjectCache *ObjCache)
770b57cec5SDimitry Andric     : JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> ConcurrentIRCompiler::operator()(Module &M) {
800b57cec5SDimitry Andric   auto TM = cantFail(JTMB.createTargetMachine());
810b57cec5SDimitry Andric   SimpleCompiler C(*TM, ObjCache);
820b57cec5SDimitry Andric   return C(M);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric } // end namespace orc
860b57cec5SDimitry Andric } // end namespace llvm
87