forked from llvm-mirror/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Kaleidoscope][BuildingAJIT] Add code for the 2nd chapter of the Buil…
…dingAJIT tutorial. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270794 91177308-0d34-0410-b5e6-96231b3b80d8
- Loading branch information
Showing
4 changed files
with
1,369 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
add_subdirectory(Chapter1) | ||
add_subdirectory(Chapter2) | ||
|
17 changes: 17 additions & 0 deletions
17
examples/Kaleidoscope/BuildingAJIT/Chapter2/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
set(LLVM_LINK_COMPONENTS | ||
Analysis | ||
Core | ||
ExecutionEngine | ||
InstCombine | ||
Object | ||
RuntimeDyld | ||
ScalarOpts | ||
Support | ||
native | ||
) | ||
|
||
add_kaleidoscope_chapter(BuildingAJIT-Ch2 | ||
toy.cpp | ||
) | ||
|
||
export_executable_symbols(BuildingAJIT-Ch2) |
131 changes: 131 additions & 0 deletions
131
examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
//===----- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope ----*- C++ -*-===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Contains a simple JIT definition for use in the kaleidoscope tutorials. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H | ||
#define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H | ||
|
||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/ExecutionEngine/ExecutionEngine.h" | ||
#include "llvm/ExecutionEngine/RuntimeDyld.h" | ||
#include "llvm/ExecutionEngine/SectionMemoryManager.h" | ||
#include "llvm/ExecutionEngine/Orc/CompileUtils.h" | ||
#include "llvm/ExecutionEngine/Orc/JITSymbol.h" | ||
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" | ||
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h" | ||
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h" | ||
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" | ||
#include "llvm/IR/DataLayout.h" | ||
#include "llvm/IR/Mangler.h" | ||
#include "llvm/Support/DynamicLibrary.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
#include "llvm/Target/TargetMachine.h" | ||
#include <algorithm> | ||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace llvm { | ||
namespace orc { | ||
|
||
class KaleidoscopeJIT { | ||
private: | ||
std::unique_ptr<TargetMachine> TM; | ||
const DataLayout DL; | ||
ObjectLinkingLayer<> ObjectLayer; | ||
IRCompileLayer<decltype(ObjectLayer)> CompileLayer; | ||
|
||
typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)> | ||
OptimizeFunction; | ||
|
||
IRTransformLayer<decltype(CompileLayer), OptimizeFunction> OptimizeLayer; | ||
|
||
public: | ||
typedef decltype(OptimizeLayer)::ModuleSetHandleT ModuleHandle; | ||
|
||
KaleidoscopeJIT() | ||
: TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), | ||
CompileLayer(ObjectLayer, SimpleCompiler(*TM)), | ||
OptimizeLayer(CompileLayer, | ||
[this](std::unique_ptr<Module> M) { | ||
return optimizeModule(std::move(M)); | ||
}) { | ||
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr); | ||
} | ||
|
||
TargetMachine &getTargetMachine() { return *TM; } | ||
|
||
ModuleHandle addModule(std::unique_ptr<Module> M) { | ||
// Build our symbol resolver: | ||
// Lambda 1: Look back into the JIT itself to find symbols that are part of | ||
// the same "logical dylib". | ||
// Lambda 2: Search for external symbols in the host process. | ||
auto Resolver = createLambdaResolver( | ||
[&](const std::string &Name) { | ||
if (auto Sym = CompileLayer.findSymbol(Name, false)) | ||
return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags()); | ||
return RuntimeDyld::SymbolInfo(nullptr); | ||
}, | ||
[](const std::string &Name) { | ||
if (auto SymAddr = | ||
RTDyldMemoryManager::getSymbolAddressInProcess(Name)) | ||
return RuntimeDyld::SymbolInfo(SymAddr, JITSymbolFlags::Exported); | ||
return RuntimeDyld::SymbolInfo(nullptr); | ||
}); | ||
|
||
// Build a singlton module set to hold our module. | ||
std::vector<std::unique_ptr<Module>> Ms; | ||
Ms.push_back(std::move(M)); | ||
|
||
// Add the set to the JIT with the resolver we created above and a newly | ||
// created SectionMemoryManager. | ||
return OptimizeLayer.addModuleSet(std::move(Ms), | ||
make_unique<SectionMemoryManager>(), | ||
std::move(Resolver)); | ||
} | ||
|
||
JITSymbol findSymbol(const std::string Name) { | ||
std::string MangledName; | ||
raw_string_ostream MangledNameStream(MangledName); | ||
Mangler::getNameWithPrefix(MangledNameStream, Name, DL); | ||
return OptimizeLayer.findSymbol(MangledNameStream.str(), true); | ||
} | ||
|
||
void removeModule(ModuleHandle H) { | ||
OptimizeLayer.removeModuleSet(H); | ||
} | ||
|
||
std::unique_ptr<Module> optimizeModule(std::unique_ptr<Module> M) { | ||
// Create a function pass manager. | ||
auto FPM = llvm::make_unique<legacy::FunctionPassManager>(M.get()); | ||
|
||
// Add some optimizations. | ||
FPM->add(createInstructionCombiningPass()); | ||
FPM->add(createReassociatePass()); | ||
FPM->add(createGVNPass()); | ||
FPM->add(createCFGSimplificationPass()); | ||
FPM->doInitialization(); | ||
|
||
// Run the optimizations over all functions in the module being added to | ||
// the JIT. | ||
for (auto &F : *M) | ||
FPM->run(F); | ||
|
||
return M; | ||
} | ||
|
||
}; | ||
|
||
} // end namespace orc | ||
} // end namespace llvm | ||
|
||
#endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H |
Oops, something went wrong.