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"
|
2016-05-27 04:35:39 +08:00
|
|
|
#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"
|
2018-02-07 05:25:11 +08:00
|
|
|
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
|
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
|
|
|
#include "llvm/ExecutionEngine/SectionMemoryManager.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"
|
2018-04-24 08:58:57 +08:00
|
|
|
#include "llvm/Transforms/InstCombine/InstCombine.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>
|
2018-02-07 05:25:11 +08:00
|
|
|
#include <map>
|
2016-05-27 04:35:39 +08:00
|
|
|
#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:
|
2018-02-07 05:25:11 +08:00
|
|
|
ExecutionSession ES;
|
|
|
|
std::map<VModuleKey, std::shared_ptr<SymbolResolver>> Resolvers;
|
2016-05-27 04:35:39 +08:00
|
|
|
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 =
|
2018-03-15 08:30:14 +08:00
|
|
|
std::function<std::unique_ptr<Module>(std::unique_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:
|
|
|
|
KaleidoscopeJIT()
|
2018-04-03 04:57:56 +08:00
|
|
|
: TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
|
2018-02-15 06:13:02 +08:00
|
|
|
ObjectLayer(ES,
|
|
|
|
[this](VModuleKey K) {
|
|
|
|
return RTDyldObjectLinkingLayer::Resources{
|
|
|
|
std::make_shared<SectionMemoryManager>(),
|
|
|
|
Resolvers[K]};
|
|
|
|
}),
|
2016-05-27 04:35:39 +08:00
|
|
|
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
|
|
|
|
OptimizeLayer(CompileLayer,
|
2018-03-15 08:30:14 +08:00
|
|
|
[this](std::unique_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)),
|
2018-02-07 05:25:11 +08:00
|
|
|
CODLayer(ES, OptimizeLayer,
|
|
|
|
[&](orc::VModuleKey K) { return Resolvers[K]; },
|
|
|
|
[&](orc::VModuleKey K, std::shared_ptr<SymbolResolver> R) {
|
|
|
|
Resolvers[K] = std::move(R);
|
|
|
|
},
|
|
|
|
[](Function &F) { return std::set<Function *>({&F}); },
|
2016-05-27 04:35:39 +08:00
|
|
|
*CompileCallbackManager,
|
|
|
|
orc::createLocalIndirectStubsManagerBuilder(
|
2018-02-07 05:25:11 +08:00
|
|
|
TM->getTargetTriple())) {
|
2016-05-27 04:35:39 +08:00
|
|
|
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TargetMachine &getTargetMachine() { return *TM; }
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
VModuleKey addModule(std::unique_ptr<Module> M) {
|
2018-02-07 05:25:11 +08:00
|
|
|
// Create a new VModuleKey.
|
|
|
|
VModuleKey K = ES.allocateVModule();
|
|
|
|
|
|
|
|
// Build a resolver and associate it with the new key.
|
|
|
|
Resolvers[K] = createLegacyLookupResolver(
|
2018-05-17 06:24:30 +08:00
|
|
|
ES,
|
2018-02-07 05:25:11 +08:00
|
|
|
[this](const std::string &Name) -> JITSymbol {
|
|
|
|
if (auto Sym = CompileLayer.findSymbol(Name, false))
|
2016-08-02 04:49:11 +08:00
|
|
|
return Sym;
|
2018-02-07 05:25:11 +08:00
|
|
|
else if (auto Err = Sym.takeError())
|
|
|
|
return std::move(Err);
|
2016-05-27 04:35:39 +08:00
|
|
|
if (auto SymAddr =
|
2018-02-07 05:25:11 +08:00
|
|
|
RTDyldMemoryManager::getSymbolAddressInProcess(Name))
|
2016-08-02 04:49:11 +08:00
|
|
|
return JITSymbol(SymAddr, JITSymbolFlags::Exported);
|
2018-02-07 05:25:11 +08:00
|
|
|
return nullptr;
|
|
|
|
},
|
|
|
|
[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); });
|
2016-05-27 04:35:39 +08:00
|
|
|
|
2018-02-07 05:25:11 +08:00
|
|
|
// Add the module to the JIT with the new key.
|
2018-02-09 10:30:40 +08:00
|
|
|
cantFail(CODLayer.addModule(K, std::move(M)));
|
|
|
|
return K;
|
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);
|
|
|
|
}
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
void removeModule(VModuleKey K) {
|
|
|
|
cantFail(CODLayer.removeModule(K));
|
2016-05-27 04:35:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-03-15 08:30:14 +08:00
|
|
|
std::unique_ptr<Module> optimizeModule(std::unique_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
|