7bool ReOptimizeLayer::ReOptMaterializationUnitState::tryStartReoptimize() {
8 std::unique_lock<std::mutex> Lock(Mutex);
16void ReOptimizeLayer::ReOptMaterializationUnitState::reoptimizeSucceeded() {
17 std::unique_lock<std::mutex> Lock(Mutex);
18 assert(Reoptimizing &&
"Tried to mark unstarted reoptimization as done");
23void ReOptimizeLayer::ReOptMaterializationUnitState::reoptimizeFailed() {
24 std::unique_lock<std::mutex> Lock(Mutex);
25 assert(Reoptimizing &&
"Tried to mark unstarted reoptimization as done");
39 if (!SPSArgs::serialize(OB, MUID, CurVersion)) {
41 <<
"Reoptimization error: could not serialize reoptimization arguments";
45 JITDispatch(JITDispatchCtx, Tag, ArgBytes.data(), ArgBytes.size())};
48 errs() <<
"Reoptimization error: " << ErrMsg <<
"\naborting.\n";
55 auto Ctx = std::make_unique<LLVMContext>();
56 auto Mod = std::make_unique<Module>(
"orc-rt-lite-reoptimize.ll", *Ctx);
57 Mod->setDataLayout(
DL);
70 VoidTy, {VoidPtrTy, VoidPtrTy, VoidPtrTy, Int64Ty,
Int32Ty},
false);
75 ConstantInt::get(Int8Ty, 0),
"__orc_rt_reoptimize_tag");
83 "__orc_rt_reoptimize",
Mod.get());
86 auto ArgIt = ReOptimizeFn->arg_begin();
87 Value *MUID = &*ArgIt++;
89 Value *CurVersion = &*ArgIt;
90 CurVersion->
setName(
"CurVersion");
94 Builder.SetInsertPoint(Entry);
101 Type *IntPtrTy =
DL.getIntPtrType(*Ctx);
103 ConstantInt::get(IntPtrTy, JDI.JITDispatchFunction.getValue()),
106 ConstantInt::get(IntPtrTy, JDI.JITDispatchContext.getValue()), VoidPtrTy);
108 ConstantInt::get(IntPtrTy,
reinterpret_cast<uintptr_t
>(
113 Value *ReoptimizeTagPtr = Builder.CreatePointerCast(ReoptimizeTag, VoidPtrTy);
117 HelperFnTy, HelperFnAddr,
118 {JITDispatchPtr, JITDispatchCtxPtr, ReoptimizeTagPtr, MUID, CurVersion});
121 Builder.CreateRetVoid();
123 return BaseLayer.add(PlatformJD,
130 WFs[Mangle(
"__orc_rt_reoptimize_tag")] =
131 ES.wrapAsyncWithSPS<ReoptimizeSPSSig>(
this,
132 &ReOptimizeLayer::rt_reoptimize);
133 return ES.registerJITDispatchHandlers(PlatformJD, std::move(WFs));
138 auto &JD = R->getTargetJITDylib();
140 bool HasNonCallable =
false;
141 for (
auto &KV : R->getSymbols()) {
142 auto &Flags = KV.second;
143 if (!Flags.isCallable())
144 HasNonCallable =
true;
147 if (HasNonCallable) {
148 BaseLayer.emit(std::move(R), std::move(TSM));
152 auto &MUState = createMaterializationUnitState(TSM);
155 registerMaterializationUnitResource(Key, MUState);
157 ES.reportError(std::move(Err));
158 R->failMaterialization();
163 ProfilerFunc(*
this, MUState.getID(), MUState.getCurVersion(), TSM)) {
164 ES.reportError(std::move(Err));
165 R->failMaterialization();
170 emitMUImplSymbols(MUState, MUState.getCurVersion(), JD, std::move(TSM));
172 ES.reportError(InitialDests.takeError());
173 R->failMaterialization();
177 RSManager.emitRedirectableSymbols(std::move(R), std::move(*InitialDests));
190 if (
F.isDeclaration())
192 auto &BB =
F.getEntryBlock();
193 auto *IP = &*BB.getFirstInsertionPt();
209ReOptimizeLayer::emitMUImplSymbols(ReOptMaterializationUnitState &MUState,
214 MangleAndInterner Mangle(ES, M.getDataLayout());
216 if (!F.isDeclaration()) {
217 std::string NewName =
218 (F.getName() +
".__def__." + Twine(Version)).str();
219 RenamedMap[Mangle(F.getName())] = Mangle(NewName);
225 auto RT = JD.createResourceTracker();
227 JD.define(std::make_unique<BasicIRLayerMaterializationUnit>(
231 MUState.setResourceTracker(RT);
234 for (
auto [K, V] : RenamedMap)
235 LookupSymbols.
add(V);
240 if (
auto Err = ImplSymbols.takeError())
244 for (
auto [K, V] : RenamedMap)
250void ReOptimizeLayer::rt_reoptimize(SendErrorFn SendResult,
252 uint32_t CurVersion) {
253 auto &MUState = getMaterializationUnitState(MUID);
254 if (CurVersion < MUState.getCurVersion() || !MUState.tryStartReoptimize()) {
260 auto OldRT = MUState.getResourceTracker();
261 auto &JD = OldRT->getJITDylib();
263 if (
auto Err = ReOptFunc(*
this, MUID, CurVersion + 1, OldRT, TSM)) {
264 ES.reportError(std::move(Err));
265 MUState.reoptimizeFailed();
271 emitMUImplSymbols(MUState, CurVersion + 1, JD, std::move(TSM));
273 ES.reportError(SymbolDests.takeError());
274 MUState.reoptimizeFailed();
279 if (
auto Err = RSManager.redirect(JD, std::move(*SymbolDests))) {
280 ES.reportError(std::move(Err));
281 MUState.reoptimizeFailed();
286 MUState.reoptimizeSucceeded();
296 if (!ReoptimizeFunc) {
297 std::vector<Type *> ArgTys = {MUIDTy, VersionTy};
301 "__orc_rt_reoptimize", &M);
303 Constant *MUIDArg = ConstantInt::get(MUIDTy, MUID,
false);
304 Constant *CurVersionArg = ConstantInt::get(VersionTy, CurVersion,
false);
306 (void)IRB.
CreateCall(ReoptimizeFunc, {MUIDArg, CurVersionArg});
309ReOptimizeLayer::ReOptMaterializationUnitState &
310ReOptimizeLayer::createMaterializationUnitState(
const ThreadSafeModule &TSM) {
311 std::unique_lock<std::mutex> Lock(
Mutex);
313 MUStates.emplace(MUID,
316 return MUStates.at(MUID);
319ReOptimizeLayer::ReOptMaterializationUnitState &
321 std::unique_lock<std::mutex> Lock(
Mutex);
322 return MUStates.at(MUID);
325void ReOptimizeLayer::registerMaterializationUnitResource(
327 std::unique_lock<std::mutex> Lock(
Mutex);
328 MUResources[
Key].insert(State.getID());
332 std::unique_lock<std::mutex> Lock(Mutex);
333 for (
auto MUID : MUResources[K])
334 MUStates.erase(MUID);
336 MUResources.erase(K);
342 std::unique_lock<std::mutex> Lock(Mutex);
343 MUResources[DstK].insert_range(MUResources[SrcK]);
344 MUResources.erase(SrcK);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void orc_rt_lite_reoptimize_helper(shared::CWrapperFunctionBuffer(*JITDispatch)(void *Ctx, void *Tag, const char *Data, size_t Size), void *JITDispatchCtx, void *Tag, uint64_t MUID, uint32_t CurVersion)
LLVM Basic Block Representation.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
This is an important base class in LLVM.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Class to represent function types.
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
const Function & getFunction() const
@ InternalLinkage
Rename collisions when linking (static functions).
@ ExternalLinkage
Externally visible function.
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
A Module instance is used to store all the information related to an LLVM module.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
LLVM Value Representation.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
ExecutorProcessControl & getExecutorProcessControl()
Get the ExecutorProcessControl object associated with this ExecutionSession.
LLVM_ABI void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylibs for the given symbols.
DenseMap< SymbolStringPtr, JITDispatchHandlerFunction > JITDispatchHandlerAssociationMap
A map associating tag names with asynchronous wrapper function implementations in the JIT.
const JITDispatchInfo & getJITDispatchInfo() const
Get the JIT dispatch function and context address for the executor.
const IRSymbolMapper::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
Represents a JIT'd dynamic library.
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Error registerRuntimeFunctions(JITDylib &PlatformJD)
Registers reoptimize runtime dispatch handlers to given PlatformJD.
ReOptimizeLayer(ExecutionSession &ES, DataLayout &DL, IRLayer &BaseLayer, RedirectableSymbolManager &RM)
void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM) override
Emits the given module.
static void createReoptimizeCall(Module &M, Instruction &IP, ReOptMaterializationUnitID MUID, unsigned CurVersion)
uint64_t ReOptMaterializationUnitID
void handleTransferResources(JITDylib &JD, ResourceKey DstK, ResourceKey SrcK) override
This function will be called inside the session lock.
Error addOrcRTLiteSupport(JITDylib &PlatformJD, const DataLayout &DL)
Add ORC Runtime-lite support for reoptimization to PlatformJD.
static Error reoptimizeIfCallFrequent(ReOptimizeLayer &Parent, ReOptMaterializationUnitID MUID, unsigned CurVersion, ThreadSafeModule &TSM)
Basic AddProfilerFunc that reoptimizes the function when the call count exceeds CallCountThreshold.
Error handleRemoveResources(JITDylib &JD, ResourceKey K) override
This function will be called outside the session lock.
static const uint64_t CallCountThreshold
A set of symbols to look up, each associated with a SymbolLookupFlags value.
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
An LLVM Module together with a shared ThreadSafeContext.
decltype(auto) withModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function on the contained Module.
A utility class for serializing to a blob from a variadic list.
Output char buffer with overflow check.
C++ wrapper function buffer: Same as CWrapperFunctionBuffer but auto-releases memory.
const char * getOutOfBandError() const
If this value is an out-of-band error then this returns the error message, otherwise returns nullptr.
static WrapperFunctionBuffer allocate(size_t Size)
Create a WrapperFunctionBuffer with the given size and return a pointer to the underlying memory.
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
LLVM_ABI ThreadSafeModule cloneToNewContext(const ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
@ Resolved
Queried, materialization begun.
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
FunctionAddr VTableAddr uintptr_t uintptr_t Version
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Mod
The access may modify the value stored in memory.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
LLVM_ABI Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...