brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.4 KiB · 7800dca Raw
104 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/Orc/CompileUtils.h"18#include "llvm/ExecutionEngine/Orc/Core.h"19#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"20#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"21#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"22#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"23#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"24#include "llvm/ExecutionEngine/Orc/SelfExecutorProcessControl.h"25#include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"26#include "llvm/ExecutionEngine/SectionMemoryManager.h"27#include "llvm/IR/DataLayout.h"28#include "llvm/IR/LLVMContext.h"29#include <memory>30 31namespace llvm {32namespace orc {33 34class KaleidoscopeJIT {35private:36  std::unique_ptr<ExecutionSession> ES;37 38  DataLayout DL;39  MangleAndInterner Mangle;40 41  RTDyldObjectLinkingLayer ObjectLayer;42  IRCompileLayer CompileLayer;43 44  JITDylib &MainJD;45 46public:47  KaleidoscopeJIT(std::unique_ptr<ExecutionSession> ES,48                  JITTargetMachineBuilder JTMB, DataLayout DL)49      : ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL),50        ObjectLayer(*this->ES,51                    [](const MemoryBuffer &) {52                      return std::make_unique<SectionMemoryManager>();53                    }),54        CompileLayer(*this->ES, ObjectLayer,55                     std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),56        MainJD(this->ES->createBareJITDylib("<main>")) {57    MainJD.addGenerator(58        cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(59            DL.getGlobalPrefix())));60  }61 62  ~KaleidoscopeJIT() {63    if (auto Err = ES->endSession())64      ES->reportError(std::move(Err));65  }66 67  static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() {68    auto EPC = SelfExecutorProcessControl::Create();69    if (!EPC)70      return EPC.takeError();71 72    auto ES = std::make_unique<ExecutionSession>(std::move(*EPC));73 74    JITTargetMachineBuilder JTMB(75        ES->getExecutorProcessControl().getTargetTriple());76 77    auto DL = JTMB.getDefaultDataLayoutForTarget();78    if (!DL)79      return DL.takeError();80 81    return std::make_unique<KaleidoscopeJIT>(std::move(ES), std::move(JTMB),82                                             std::move(*DL));83  }84 85  const DataLayout &getDataLayout() const { return DL; }86 87  JITDylib &getMainJITDylib() { return MainJD; }88 89  Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) {90    if (!RT)91      RT = MainJD.getDefaultResourceTracker();92    return CompileLayer.add(RT, std::move(TSM));93  }94 95  Expected<ExecutorSymbolDef> lookup(StringRef Name) {96    return ES->lookup({&MainJD}, Mangle(Name.str()));97  }98};99 100} // end namespace orc101} // end namespace llvm102 103#endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H104