[lit] Improve lit.Run class
[llvm-complete.git] / examples / Kaleidoscope / BuildingAJIT / Chapter1 / KaleidoscopeJIT.h
bloba7fa3afc470cd73f5353e521c6dee6e21327bfb3
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Contains a simple JIT definition for use in the kaleidoscope tutorials.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
14 #define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
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/IRCompileLayer.h"
22 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
23 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
24 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include <memory>
29 namespace llvm {
30 namespace orc {
32 class KaleidoscopeJIT {
33 private:
34 ExecutionSession ES;
35 RTDyldObjectLinkingLayer ObjectLayer;
36 IRCompileLayer CompileLayer;
38 DataLayout DL;
39 MangleAndInterner Mangle;
40 ThreadSafeContext Ctx;
42 public:
43 KaleidoscopeJIT(JITTargetMachineBuilder JTMB, DataLayout DL)
44 : ObjectLayer(ES,
45 []() { return std::make_unique<SectionMemoryManager>(); }),
46 CompileLayer(ES, ObjectLayer, ConcurrentIRCompiler(std::move(JTMB))),
47 DL(std::move(DL)), Mangle(ES, this->DL),
48 Ctx(std::make_unique<LLVMContext>()) {
49 ES.getMainJITDylib().addGenerator(
50 cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
51 DL.getGlobalPrefix())));
54 static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() {
55 auto JTMB = JITTargetMachineBuilder::detectHost();
57 if (!JTMB)
58 return JTMB.takeError();
60 auto DL = JTMB->getDefaultDataLayoutForTarget();
61 if (!DL)
62 return DL.takeError();
64 return std::make_unique<KaleidoscopeJIT>(std::move(*JTMB), std::move(*DL));
67 const DataLayout &getDataLayout() const { return DL; }
69 LLVMContext &getContext() { return *Ctx.getContext(); }
71 Error addModule(std::unique_ptr<Module> M) {
72 return CompileLayer.add(ES.getMainJITDylib(),
73 ThreadSafeModule(std::move(M), Ctx));
76 Expected<JITEvaluatedSymbol> lookup(StringRef Name) {
77 return ES.lookup({&ES.getMainJITDylib()}, Mangle(Name.str()));
81 } // end namespace orc
82 } // end namespace llvm
84 #endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H