brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.4 KiB · 1789ab1 Raw
135 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/IRTransformLayer.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 "llvm/IR/LegacyPassManager.h"31#include "llvm/Transforms/InstCombine/InstCombine.h"32#include "llvm/Transforms/Scalar.h"33#include "llvm/Transforms/Scalar/GVN.h"34#include <memory>35 36namespace llvm {37namespace orc {38 39class KaleidoscopeJIT {40private:41  std::unique_ptr<ExecutionSession> ES;42 43  DataLayout DL;44  MangleAndInterner Mangle;45 46  RTDyldObjectLinkingLayer ObjectLayer;47  IRCompileLayer CompileLayer;48  IRTransformLayer OptimizeLayer;49 50  JITDylib &MainJD;51 52public:53  KaleidoscopeJIT(std::unique_ptr<ExecutionSession> ES,54                  JITTargetMachineBuilder JTMB, DataLayout DL)55      : ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL),56        ObjectLayer(*this->ES,57                    [](const MemoryBuffer &) {58                      return std::make_unique<SectionMemoryManager>();59                    }),60        CompileLayer(*this->ES, ObjectLayer,61                     std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),62        OptimizeLayer(*this->ES, CompileLayer, optimizeModule),63        MainJD(this->ES->createBareJITDylib("<main>")) {64    MainJD.addGenerator(65        cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(66            DL.getGlobalPrefix())));67  }68 69  ~KaleidoscopeJIT() {70    if (auto Err = ES->endSession())71      ES->reportError(std::move(Err));72  }73 74  static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() {75    auto EPC = SelfExecutorProcessControl::Create();76    if (!EPC)77      return EPC.takeError();78 79    auto ES = std::make_unique<ExecutionSession>(std::move(*EPC));80 81    JITTargetMachineBuilder JTMB(82        ES->getExecutorProcessControl().getTargetTriple());83 84    auto DL = JTMB.getDefaultDataLayoutForTarget();85    if (!DL)86      return DL.takeError();87 88    return std::make_unique<KaleidoscopeJIT>(std::move(ES), std::move(JTMB),89                                             std::move(*DL));90  }91 92  const DataLayout &getDataLayout() const { return DL; }93 94  JITDylib &getMainJITDylib() { return MainJD; }95 96  Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) {97    if (!RT)98      RT = MainJD.getDefaultResourceTracker();99 100    return OptimizeLayer.add(RT, std::move(TSM));101  }102 103  Expected<ExecutorSymbolDef> lookup(StringRef Name) {104    return ES->lookup({&MainJD}, Mangle(Name.str()));105  }106 107private:108  static Expected<ThreadSafeModule>109  optimizeModule(ThreadSafeModule TSM, const MaterializationResponsibility &R) {110    TSM.withModuleDo([](Module &M) {111      // Create a function pass manager.112      auto FPM = std::make_unique<legacy::FunctionPassManager>(&M);113 114      // Add some optimizations.115      FPM->add(createInstructionCombiningPass());116      FPM->add(createReassociatePass());117      FPM->add(createGVNPass());118      FPM->add(createCFGSimplificationPass());119      FPM->doInitialization();120 121      // Run the optimizations over all functions in the module being added to122      // the JIT.123      for (auto &F : M)124        FPM->run(F);125    });126 127    return std::move(TSM);128  }129};130 131} // end namespace orc132} // end namespace llvm133 134#endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H135