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 275ffd83dbSDimitry Andric IRSymbolMapper::ManglingOptions 2813138422SDimitry Andric irManglingOptionsFromTargetOptions(const TargetOptions &Opts) { 295ffd83dbSDimitry 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>( 56*0eae32dcSDimitry Andric std::move(ObjBufferSV), M.getModuleIdentifier() + "-jitted-objectbuffer", 57*0eae32dcSDimitry Andric /*RequiresNullTerminator=*/false); 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric auto Obj = object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()); 600b57cec5SDimitry Andric 6113138422SDimitry Andric if (!Obj) 6213138422SDimitry Andric return Obj.takeError(); 6313138422SDimitry Andric 640b57cec5SDimitry Andric notifyObjectCompiled(M, *ObjBuffer); 650b57cec5SDimitry Andric return std::move(ObjBuffer); 660b57cec5SDimitry Andric } 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric SimpleCompiler::CompileResult 690b57cec5SDimitry Andric SimpleCompiler::tryToLoadFromObjectCache(const Module &M) { 700b57cec5SDimitry Andric if (!ObjCache) 710b57cec5SDimitry Andric return CompileResult(); 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric return ObjCache->getObject(&M); 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric void SimpleCompiler::notifyObjectCompiled(const Module &M, 770b57cec5SDimitry Andric const MemoryBuffer &ObjBuffer) { 780b57cec5SDimitry Andric if (ObjCache) 790b57cec5SDimitry Andric ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef()); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric ConcurrentIRCompiler::ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, 830b57cec5SDimitry Andric ObjectCache *ObjCache) 8413138422SDimitry Andric : IRCompiler(irManglingOptionsFromTargetOptions(JTMB.getOptions())), 8513138422SDimitry Andric JTMB(std::move(JTMB)), ObjCache(ObjCache) {} 860b57cec5SDimitry Andric 8713138422SDimitry Andric Expected<std::unique_ptr<MemoryBuffer>> 8813138422SDimitry Andric ConcurrentIRCompiler::operator()(Module &M) { 890b57cec5SDimitry Andric auto TM = cantFail(JTMB.createTargetMachine()); 900b57cec5SDimitry Andric SimpleCompiler C(*TM, ObjCache); 910b57cec5SDimitry Andric return C(M); 920b57cec5SDimitry Andric } 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric } // end namespace orc 950b57cec5SDimitry Andric } // end namespace llvm 96