1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 11:13:28 +01:00
llvm-mirror/examples/ThinLtoJIT/ThinLtoJIT.h
Stefan Gränitz a7a66b3792 Add ThinLtoJIT example
Summary:
Prototype of a JIT compiler that utilizes ThinLTO summaries to compile modules ahead of time. This is an implementation of the concept I presented in my "ThinLTO Summaries in JIT Compilation" talk at the 2018 Developers' Meeting: http://llvm.org/devmtg/2018-10/talk-abstracts.html#lt8

Upfront the JIT first populates the *combined ThinLTO module index*, which provides fast access to the global call-graph and module paths by function. Next, it loads the main function's module and compiles it. All functions in the module will be emitted with prolog instructions that *fire a discovery flag* once execution reaches them. In parallel, the *discovery thread* is busy-watching the existing flags. Once it detects one has fired, it uses the module index to find all functions that are reachable from it within a given number of calls and submits their defining modules to the compilation pipeline.

While execution continues, more flags are fired and further modules added. Ideally the JIT can be tuned in a way, so that in the majority of cases the code on the execution path can be compiled ahead of time. In cases where it doesn't work, the JIT has a *definition generator* in place that loads modules if missing functions are reached.

Reviewers: lhames, dblaikie, jfb, tejohnson, pree-jackie, AlexDenisov, kavon

Subscribers: mgorny, mehdi_amini, inglorion, hiraditya, steven_wu, dexonsmith, arphaman, jfb, merge_guards_bot, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D72486
2020-02-01 20:25:09 +01:00

112 lines
3.3 KiB
C++

#ifndef LLVM_EXAMPLES_THINLTOJIT_THINLTOJIT_H
#define LLVM_EXAMPLES_THINLTOJIT_THINLTOJIT_H
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ThreadPool.h"
#include <functional>
#include <memory>
#include <string>
#include <thread>
#include <vector>
namespace llvm {
namespace orc {
class ThinLtoDiscoveryThread;
class ThinLtoInstrumentationLayer;
class ThinLtoModuleIndex;
class CompileOnDemandLayer;
class IRCompileLayer;
class RTDyldObjectLinkingLayer;
class JITDylib;
class JITTargetMachineBuilder;
class LazyCallThroughManager;
class MangleAndInterner;
class ThinLtoJIT {
public:
using AddModuleFunction = std::function<Error(ThreadSafeModule)>;
enum ExplicitMemoryBarrier {
NeverFence = 0,
FenceStaticCode = 1,
FenceJITedCode = 2,
AlwaysFence = 3
};
ThinLtoJIT(ArrayRef<std::string> InputFiles, StringRef MainFunctionName,
unsigned LookaheadLevels, unsigned NumCompileThreads,
unsigned NumLoadThreads, unsigned DiscoveryFlagsPerBucket,
ExplicitMemoryBarrier MemFence, bool AllowNudgeIntoDiscovery,
bool PrintStats, Error &Err);
~ThinLtoJIT();
ThinLtoJIT(const ThinLtoJIT &) = delete;
ThinLtoJIT &operator=(const ThinLtoJIT &) = delete;
ThinLtoJIT(ThinLtoJIT &&) = delete;
ThinLtoJIT &operator=(ThinLtoJIT &&) = delete;
Expected<int> main(ArrayRef<std::string> Args) {
auto MainSym = ES.lookup({MainJD}, MainFunctionMangled);
if (!MainSym)
return MainSym.takeError();
using MainFn = int(int, char *[]);
auto Main = jitTargetAddressToFunction<MainFn *>(MainSym->getAddress());
return runAsMain(Main, Args, StringRef("ThinLtoJIT"));
}
private:
ExecutionSession ES;
DataLayout DL{""};
JITDylib *MainJD;
SymbolStringPtr MainFunctionMangled;
std::unique_ptr<ThreadPool> CompileThreads;
std::unique_ptr<ThinLtoModuleIndex> GlobalIndex;
AddModuleFunction AddModule;
std::unique_ptr<RTDyldObjectLinkingLayer> ObjLinkingLayer;
std::unique_ptr<IRCompileLayer> CompileLayer;
std::unique_ptr<ThinLtoInstrumentationLayer> InstrumentationLayer;
std::unique_ptr<CompileOnDemandLayer> OnDemandLayer;
std::atomic<bool> JitRunning;
std::thread DiscoveryThread;
std::unique_ptr<ThinLtoDiscoveryThread> DiscoveryThreadWorker;
std::unique_ptr<MangleAndInterner> Mangle;
std::unique_ptr<LazyCallThroughManager> CallThroughManager;
void setupLayers(JITTargetMachineBuilder JTMB, unsigned NumCompileThreads,
unsigned DiscoveryFlagsPerBucket,
ExplicitMemoryBarrier MemFence);
Error setupJITDylib(JITDylib *JD, bool AllowNudge, bool PrintStats);
void setupDiscovery(JITDylib *MainJD, unsigned LookaheadLevels,
bool PrintStats);
Expected<ThreadSafeModule> setupMainModule(StringRef MainFunction);
Expected<JITTargetMachineBuilder> setupTargetUtils(Module *M);
Error applyDataLayout(Module *M);
static void exitOnLazyCallThroughFailure() {
errs() << "Compilation failed. Aborting.\n";
exit(1);
}
};
} // namespace orc
} // namespace llvm
#endif