1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-20 03:23:01 +02:00
llvm-mirror/examples/ThinLtoJIT/ThinLtoInstrumentationLayer.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

78 lines
2.3 KiB
C++

#ifndef LLVM_EXAMPLES_THINLTOJIT_DISCOVERYLAYER_H
#define LLVM_EXAMPLES_THINLTOJIT_DISCOVERYLAYER_H
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/Layer.h"
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/raw_ostream.h"
#include "ThinLtoJIT.h"
#include <atomic>
#include <cstdint>
#include <map>
#include <mutex>
#include <vector>
namespace llvm {
namespace orc {
class ThinLtoInstrumentationLayer : public IRLayer {
public:
ThinLtoInstrumentationLayer(ExecutionSession &ES, IRCompileLayer &BaseLayer,
ThinLtoJIT::ExplicitMemoryBarrier MemFence,
unsigned FlagsPerBucket)
: IRLayer(ES, BaseLayer.getManglingOptions()), BaseLayer(BaseLayer),
MemFence(MemFence) {
// TODO: So far we only allocate one bucket.
allocateDiscoveryFlags(FlagsPerBucket);
}
~ThinLtoInstrumentationLayer() override;
void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override;
unsigned reserveDiscoveryFlags(unsigned Count);
void registerDiscoveryFlagOwners(std::vector<GlobalValue::GUID> Guids,
unsigned FirstIdx);
void nudgeIntoDiscovery(std::vector<GlobalValue::GUID> Functions);
std::vector<unsigned> takeFlagsThatFired();
std::vector<GlobalValue::GUID> takeFlagOwners(std::vector<unsigned> Indexes);
void dump(raw_ostream &OS);
private:
IRCompileLayer &BaseLayer;
ThinLtoJIT::ExplicitMemoryBarrier MemFence;
enum Flag : uint8_t { Clear = 0, Fired = 1 };
// Lock-free read access.
uint8_t *FlagsStorage;
Flag *FlagsIncoming; // lock-free write by design
Flag *FlagsHandled;
unsigned NumFlagsAllocated;
std::atomic<unsigned> NumFlagsUsed; // spin-lock
// Acquire/release sync between writers and reader
std::atomic<uint64_t> FlagsSync;
// STL container requires locking for both, read and write access.
mutable std::mutex DiscoveryFlagsInfoLock;
std::map<unsigned, GlobalValue::GUID> FlagOwnersMap;
void allocateDiscoveryFlags(unsigned MinFlags);
void compileFunctionReachedFlagSetter(IRBuilder<> &B, Flag *F);
};
} // namespace orc
} // namespace llvm
#endif