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");
32 WFs[Mangle(
"__orc_rt_reoptimize_tag")] =
34 &ReOptimizeLayer::rt_reoptimize);
40 auto &JD = R->getTargetJITDylib();
42 bool HasNonCallable =
false;
43 for (
auto &KV : R->getSymbols()) {
44 auto &Flags = KV.second;
45 if (!Flags.isCallable())
46 HasNonCallable =
true;
50 BaseLayer.
emit(std::move(R), std::move(TSM));
54 auto &MUState = createMaterializationUnitState(TSM);
56 if (
auto Err = R->withResourceKeyDo([&](
ResourceKey Key) {
57 registerMaterializationUnitResource(Key, MUState);
60 R->failMaterialization();
65 ProfilerFunc(*
this, MUState.getID(), MUState.getCurVersion(), TSM)) {
67 R->failMaterialization();
72 emitMUImplSymbols(MUState, MUState.getCurVersion(), JD, std::move(TSM));
75 R->failMaterialization();
91 auto ArgBufferConst = createReoptimizeArgBuffer(M, MUID, CurVersion);
92 if (
auto Err = ArgBufferConst.takeError())
98 if (
F.isDeclaration())
100 auto &BB =
F.getEntryBlock();
101 auto *IP = &*BB.getFirstInsertionPt();
117ReOptimizeLayer::emitMUImplSymbols(ReOptMaterializationUnitState &MUState,
122 MangleAndInterner Mangle(ES, M.getDataLayout());
124 if (!F.isDeclaration()) {
125 std::string NewName =
126 (F.getName() +
".__def__." + Twine(Version)).str();
127 RenamedMap[Mangle(F.getName())] = Mangle(NewName);
133 auto RT = JD.createResourceTracker();
135 JD.define(std::make_unique<BasicIRLayerMaterializationUnit>(
136 BaseLayer, *getManglingOptions(), std::move(TSM)),
139 MUState.setResourceTracker(RT);
142 for (
auto [K, V] : RenamedMap)
143 LookupSymbols.
add(V);
148 if (
auto Err = ImplSymbols.takeError())
152 for (
auto [K, V] : RenamedMap)
158void ReOptimizeLayer::rt_reoptimize(SendErrorFn SendResult,
159 ReOptMaterializationUnitID MUID,
161 auto &MUState = getMaterializationUnitState(MUID);
162 if (CurVersion < MUState.getCurVersion() || !MUState.tryStartReoptimize()) {
168 auto OldRT = MUState.getResourceTracker();
169 auto &JD = OldRT->getJITDylib();
171 if (
auto Err = ReOptFunc(*
this, MUID, CurVersion + 1, OldRT, TSM)) {
173 MUState.reoptimizeFailed();
179 emitMUImplSymbols(MUState, CurVersion + 1, JD, std::move(TSM));
182 MUState.reoptimizeFailed();
187 if (
auto Err = RSManager.
redirect(JD, std::move(*SymbolDests))) {
189 MUState.reoptimizeFailed();
194 MUState.reoptimizeSucceeded();
199 Module &M, ReOptMaterializationUnitID MUID,
uint32_t CurVersion) {
200 size_t ArgBufferSize = SPSReoptimizeArgList::size(MUID, CurVersion);
201 std::vector<char> ArgBuffer(ArgBufferSize);
203 if (!SPSReoptimizeArgList::serialize(OB, MUID, CurVersion))
204 return make_error<StringError>(
"Could not serealize args list",
212 M.getGlobalVariable(
"__orc_rt_jit_dispatch_ctx");
216 nullptr,
"__orc_rt_jit_dispatch_ctx");
218 M.getGlobalVariable(
"__orc_rt_reoptimize_tag");
222 nullptr,
"__orc_rt_reoptimize_tag");
223 Function *DispatchFunc = M.getFunction(
"__orc_rt_jit_dispatch");
232 "__orc_rt_jit_dispatch", &M);
234 size_t ArgBufferSizeConst =
236 Constant *ArgBufferSize = ConstantInt::get(
240 {DispatchCtx, ReoptimizeTag, ArgBuffer, ArgBufferSize});
243ReOptimizeLayer::ReOptMaterializationUnitState &
244ReOptimizeLayer::createMaterializationUnitState(
const ThreadSafeModule &TSM) {
245 std::unique_lock<std::mutex> Lock(
Mutex);
247 MUStates.emplace(MUID,
250 return MUStates.at(MUID);
253ReOptimizeLayer::ReOptMaterializationUnitState &
254ReOptimizeLayer::getMaterializationUnitState(ReOptMaterializationUnitID MUID) {
255 std::unique_lock<std::mutex> Lock(
Mutex);
256 return MUStates.at(MUID);
259void ReOptimizeLayer::registerMaterializationUnitResource(
260 ResourceKey Key, ReOptMaterializationUnitState &State) {
261 std::unique_lock<std::mutex> Lock(
Mutex);
262 MUResources[
Key].insert(State.getID());
266 std::unique_lock<std::mutex> Lock(
Mutex);
267 for (
auto MUID : MUResources[K])
268 MUStates.erase(MUID);
270 MUResources.erase(K);
276 std::unique_lock<std::mutex> Lock(
Mutex);
277 MUResources[DstK].insert(MUResources[SrcK].begin(), MUResources[SrcK].end());
278 MUResources.erase(SrcK);
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
This is an important base class in LLVM.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
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 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)
@ 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 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 * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
The instances of the Type class are immutable: once they are created, they are never changed.
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
LLVM Value Representation.
void reportError(Error Err)
Report a error for this execution session.
static JITDispatchHandlerFunction wrapAsyncWithSPS(HandlerT &&H)
Wrap a handler that takes concrete argument types (and a sender for a concrete return type) to produc...
Error registerJITDispatchHandlers(JITDylib &JD, JITDispatchHandlerAssociationMap WFs)
For each tag symbol name, associate the corresponding AsyncHandlerWrapperFunction with the address of...
virtual void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM)=0
Emit should materialize the given IR.
Represents a JIT'd dynamic library.
void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM) override
Emits the given module.
Error reigsterRuntimeFunctions(JITDylib &PlatformJD)
Registers reoptimize runtime dispatch handlers to given PlatformJD.
static void createReoptimizeCall(Module &M, Instruction &IP, GlobalVariable *ArgBuffer)
uint64_t ReOptMaterializationUnitID
void handleTransferResources(JITDylib &JD, ResourceKey DstK, ResourceKey SrcK) override
This function will be called inside the session lock.
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
virtual void emitRedirectableSymbols(std::unique_ptr< MaterializationResponsibility > MR, SymbolMap InitialDests)=0
Emit redirectable symbol.
virtual Error redirect(JITDylib &JD, const SymbolMap &NewDests)=0
Change the redirection destination of given symbols to new destination symbols.
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.
Output char buffer with overflow check.
@ OB
OB - OneByte - Set if this instruction has a one byte opcode.
ThreadSafeModule cloneToNewContext(const ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
@ Resolved
Queried, materialization begun.
This is an optimization pass for GlobalISel generic memory operations.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
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 ...