brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · 1fc89fc Raw
109 lines · c
1//===- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope --------*- C++ -*-===//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-exception6//7//===----------------------------------------------------------------------===//8//9// Contains a simple JIT definition for use in the kaleidoscope tutorials.10//11//===----------------------------------------------------------------------===//12 13#ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H14#define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H15 16#include "llvm/ADT/StringRef.h"17#include "llvm/ExecutionEngine/JITSymbol.h"18#include "llvm/ExecutionEngine/Orc/CompileUtils.h"19#include "llvm/ExecutionEngine/Orc/Core.h"20#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"21#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"22#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"23#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"24#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"25#include "llvm/ExecutionEngine/Orc/SelfExecutorProcessControl.h"26#include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"27#include "llvm/ExecutionEngine/SectionMemoryManager.h"28#include "llvm/IR/DataLayout.h"29#include "llvm/IR/LLVMContext.h"30#include <memory>31 32namespace llvm {33namespace orc {34 35class KaleidoscopeJIT {36private:37  std::unique_ptr<ExecutionSession> ES;38 39  DataLayout DL;40  MangleAndInterner Mangle;41 42  RTDyldObjectLinkingLayer ObjectLayer;43  IRCompileLayer CompileLayer;44 45  JITDylib &MainJD;46 47public:48  KaleidoscopeJIT(std::unique_ptr<ExecutionSession> ES,49                  JITTargetMachineBuilder JTMB, DataLayout DL)50      : ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL),51        ObjectLayer(*this->ES,52                    [](const MemoryBuffer &) {53                      return std::make_unique<SectionMemoryManager>();54                    }),55        CompileLayer(*this->ES, ObjectLayer,56                     std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),57        MainJD(this->ES->createBareJITDylib("<main>")) {58    MainJD.addGenerator(59        cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(60            DL.getGlobalPrefix())));61    if (JTMB.getTargetTriple().isOSBinFormatCOFF()) {62      ObjectLayer.setOverrideObjectFlagsWithResponsibilityFlags(true);63      ObjectLayer.setAutoClaimResponsibilityForObjectSymbols(true);64    }65  }66 67  ~KaleidoscopeJIT() {68    if (auto Err = ES->endSession())69      ES->reportError(std::move(Err));70  }71 72  static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() {73    auto EPC = SelfExecutorProcessControl::Create();74    if (!EPC)75      return EPC.takeError();76 77    auto ES = std::make_unique<ExecutionSession>(std::move(*EPC));78 79    JITTargetMachineBuilder JTMB(80        ES->getExecutorProcessControl().getTargetTriple());81 82    auto DL = JTMB.getDefaultDataLayoutForTarget();83    if (!DL)84      return DL.takeError();85 86    return std::make_unique<KaleidoscopeJIT>(std::move(ES), std::move(JTMB),87                                             std::move(*DL));88  }89 90  const DataLayout &getDataLayout() const { return DL; }91 92  JITDylib &getMainJITDylib() { return MainJD; }93 94  Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) {95    if (!RT)96      RT = MainJD.getDefaultResourceTracker();97    return CompileLayer.add(RT, std::move(TSM));98  }99 100  Expected<ExecutorSymbolDef> lookup(StringRef Name) {101    return ES->lookup({&MainJD}, Mangle(Name.str()));102  }103};104 105} // end namespace orc106} // end namespace llvm107 108#endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H109