2017-05-26 08:00:14 +08:00
|
|
|
//===- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope --------*- C++ -*-===//
|
2016-05-27 04:35:39 +08:00
|
|
|
//
|
|
|
|
// 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"
|
2016-08-02 04:49:11 +08:00
|
|
|
#include "llvm/ExecutionEngine/JITSymbol.h"
|
2017-05-26 08:00:14 +08:00
|
|
|
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
|
2016-05-27 04:35:39 +08:00
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
|
|
|
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
|
2017-02-20 18:07:41 +08:00
|
|
|
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
|
2016-05-27 04:35:39 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2016-11-19 05:57:58 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2016-05-27 04:35:39 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2016-11-19 05:57:58 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Transforms/Scalar/GVN.h"
|
2016-05-27 04:35:39 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
2016-11-19 05:57:58 +08:00
|
|
|
#include <set>
|
2016-05-27 04:35:39 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
|
|
|
|
class KaleidoscopeJIT {
|
|
|
|
private:
|
|
|
|
std::unique_ptr<TargetMachine> TM;
|
|
|
|
const DataLayout DL;
|
2017-06-23 05:06:54 +08:00
|
|
|
RTDyldObjectLinkingLayer ObjectLayer;
|
|
|
|
IRCompileLayer<decltype(ObjectLayer), SimpleCompiler> CompileLayer;
|
2016-05-27 04:35:39 +08:00
|
|
|
|
2017-05-26 08:00:14 +08:00
|
|
|
using OptimizeFunction =
|
2017-06-24 07:25:28 +08:00
|
|
|
std::function<std::shared_ptr<Module>(std::shared_ptr<Module>)>;
|
2016-05-27 04:35:39 +08:00
|
|
|
|
|
|
|
IRTransformLayer<decltype(CompileLayer), OptimizeFunction> OptimizeLayer;
|
2016-07-15 09:39:49 +08:00
|
|
|
|
|
|
|
std::unique_ptr<JITCompileCallbackManager> CompileCallbackManager;
|
2016-05-27 04:35:39 +08:00
|
|
|
CompileOnDemandLayer<decltype(OptimizeLayer)> CODLayer;
|
|
|
|
|
|
|
|
public:
|
2017-06-24 07:25:28 +08:00
|
|
|
using ModuleHandle = decltype(CODLayer)::ModuleHandleT;
|
2016-05-27 04:35:39 +08:00
|
|
|
|
|
|
|
KaleidoscopeJIT()
|
|
|
|
: TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
|
2017-07-04 12:42:30 +08:00
|
|
|
ObjectLayer([]() { return std::make_shared<SectionMemoryManager>(); }),
|
2016-05-27 04:35:39 +08:00
|
|
|
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
|
|
|
|
OptimizeLayer(CompileLayer,
|
2017-06-24 07:25:28 +08:00
|
|
|
[this](std::shared_ptr<Module> M) {
|
2016-05-27 04:35:39 +08:00
|
|
|
return optimizeModule(std::move(M));
|
|
|
|
}),
|
2016-07-15 09:39:49 +08:00
|
|
|
CompileCallbackManager(
|
|
|
|
orc::createLocalCompileCallbackManager(TM->getTargetTriple(), 0)),
|
2016-05-27 04:35:39 +08:00
|
|
|
CODLayer(OptimizeLayer,
|
2017-01-26 16:31:14 +08:00
|
|
|
[](Function &F) { return std::set<Function*>({&F}); },
|
2016-05-27 04:35:39 +08:00
|
|
|
*CompileCallbackManager,
|
|
|
|
orc::createLocalIndirectStubsManagerBuilder(
|
|
|
|
TM->getTargetTriple())) {
|
|
|
|
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 = CODLayer.findSymbol(Name, false))
|
2016-08-02 04:49:11 +08:00
|
|
|
return Sym;
|
|
|
|
return JITSymbol(nullptr);
|
2016-05-27 04:35:39 +08:00
|
|
|
},
|
|
|
|
[](const std::string &Name) {
|
|
|
|
if (auto SymAddr =
|
|
|
|
RTDyldMemoryManager::getSymbolAddressInProcess(Name))
|
2016-08-02 04:49:11 +08:00
|
|
|
return JITSymbol(SymAddr, JITSymbolFlags::Exported);
|
|
|
|
return JITSymbol(nullptr);
|
2016-05-27 04:35:39 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add the set to the JIT with the resolver we created above and a newly
|
|
|
|
// created SectionMemoryManager.
|
2017-07-07 10:59:13 +08:00
|
|
|
return cantFail(CODLayer.addModule(std::move(M), std::move(Resolver)));
|
2016-05-27 04:35:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JITSymbol findSymbol(const std::string Name) {
|
|
|
|
std::string MangledName;
|
|
|
|
raw_string_ostream MangledNameStream(MangledName);
|
|
|
|
Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
|
|
|
|
return CODLayer.findSymbol(MangledNameStream.str(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeModule(ModuleHandle H) {
|
2017-07-07 10:59:13 +08:00
|
|
|
cantFail(CODLayer.removeModule(H));
|
2016-05-27 04:35:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-06-24 07:25:28 +08:00
|
|
|
std::shared_ptr<Module> optimizeModule(std::shared_ptr<Module> M) {
|
2016-05-27 04:35:39 +08:00
|
|
|
// 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
|