LLVM 22.0.0git
Speculation.h
Go to the documentation of this file.
1//===-- Speculation.h - Speculative Compilation --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Contains the definition to support speculative compilation when laziness is
10// enabled.
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_SPECULATION_H
14#define LLVM_EXECUTIONENGINE_ORC_SPECULATION_H
15
16#include "llvm/ADT/DenseMap.h"
21#include "llvm/Support/Debug.h"
22#include <mutex>
23#include <utility>
24
25namespace llvm {
26namespace orc {
27
28class Speculator;
29
30// Track the Impls (JITDylib,Symbols) of Symbols while lazy call through
31// trampolines are created. Operations are guarded by locks tp ensure that Imap
32// stays in consistent state after read/write
33
35 friend class Speculator;
36
37public:
38 using AliaseeDetails = std::pair<SymbolStringPtr, JITDylib *>;
41 LLVM_ABI void trackImpls(SymbolAliasMap ImplMaps, JITDylib *SrcJD);
42
43private:
44 // FIX ME: find a right way to distinguish the pre-compile Symbols, and update
45 // the callsite
46 std::optional<AliaseeDetails> getImplFor(const SymbolStringPtr &StubSymbol) {
47 std::lock_guard<std::mutex> Lockit(ConcurrentAccess);
48 auto Position = Maps.find(StubSymbol);
49 if (Position != Maps.end())
50 return Position->getSecond();
51 else
52 return std::nullopt;
53 }
54
55 std::mutex ConcurrentAccess;
56 ImapTy Maps;
57};
58
59// Defines Speculator Concept,
61public:
65
66private:
67 void registerSymbolsWithAddr(TargetFAddr ImplAddr,
68 SymbolNameSet likelySymbols) {
69 std::lock_guard<std::mutex> Lockit(ConcurrentAccess);
70 GlobalSpecMap.insert({ImplAddr, std::move(likelySymbols)});
71 }
72
73 void launchCompile(ExecutorAddr FAddr) {
74 SymbolNameSet CandidateSet;
75 // Copy CandidateSet is necessary, to avoid unsynchronized access to
76 // the datastructure.
77 {
78 std::lock_guard<std::mutex> Lockit(ConcurrentAccess);
79 auto It = GlobalSpecMap.find(FAddr);
80 if (It == GlobalSpecMap.end())
81 return;
82 CandidateSet = It->getSecond();
83 }
84
85 SymbolDependenceMap SpeculativeLookUpImpls;
86
87 for (auto &Callee : CandidateSet) {
88 auto ImplSymbol = AliaseeImplTable.getImplFor(Callee);
89 // try to distinguish already compiled & library symbols
90 if (!ImplSymbol)
91 continue;
92 const auto &ImplSymbolName = ImplSymbol->first;
93 JITDylib *ImplJD = ImplSymbol->second;
94 auto &SymbolsInJD = SpeculativeLookUpImpls[ImplJD];
95 SymbolsInJD.insert(ImplSymbolName);
96 }
97
98 DEBUG_WITH_TYPE("orc", {
99 for (auto &I : SpeculativeLookUpImpls) {
100 llvm::dbgs() << "\n In " << I.first->getName() << " JITDylib ";
101 for (auto &N : I.second)
102 llvm::dbgs() << "\n Likely Symbol : " << N;
103 }
104 });
105
106 // for a given symbol, there may be no symbol qualified for speculatively
107 // compile try to fix this before jumping to this code if possible.
108 for (auto &LookupPair : SpeculativeLookUpImpls)
109 ES.lookup(
111 makeJITDylibSearchOrder(LookupPair.first,
113 SymbolLookupSet(LookupPair.second), SymbolState::Ready,
114 [this](Expected<SymbolMap> Result) {
115 if (auto Err = Result.takeError())
116 ES.reportError(std::move(Err));
117 },
119 }
120
121public:
123 : AliaseeImplTable(Impl), ES(ref), GlobalSpecMap(0) {}
124 Speculator(const Speculator &) = delete;
125 Speculator(Speculator &&) = delete;
126 Speculator &operator=(const Speculator &) = delete;
128
129 /// Define symbols for this Speculator object (__orc_speculator) and the
130 /// speculation runtime entry point symbol (__orc_speculate_for) in the
131 /// given JITDylib.
133
134 // Speculatively compile likely functions for the given Stub Address.
135 // destination of __orc_speculate_for jump
136 void speculateFor(TargetFAddr StubAddr) { launchCompile(StubAddr); }
137
138 // FIXME : Register with Stub Address, after JITLink Fix.
140 for (auto &SymPair : Candidates) {
141 auto Target = SymPair.first;
142 auto Likely = SymPair.second;
143
144 auto OnReadyFixUp = [Likely, Target,
145 this](Expected<SymbolMap> ReadySymbol) {
146 if (ReadySymbol) {
147 auto RDef = (*ReadySymbol)[Target];
148 registerSymbolsWithAddr(RDef.getAddress(), std::move(Likely));
149 } else
150 this->getES().reportError(ReadySymbol.takeError());
151 };
152 // Include non-exported symbols also.
153 ES.lookup(
158 }
159 }
160
161 ExecutionSession &getES() { return ES; }
162
163private:
164 static void speculateForEntryPoint(Speculator *Ptr, uint64_t StubId);
165 std::mutex ConcurrentAccess;
166 ImplSymbolMap &AliaseeImplTable;
168 StubAddrLikelies GlobalSpecMap;
169};
170
172public:
174 std::optional<DenseMap<StringRef, DenseSet<StringRef>>>;
175 using ResultEval = std::function<IRlikiesStrRef(Function &)>;
177
180 : IRLayer(ES, BaseLayer.getManglingOptions()), NextLayer(BaseLayer),
181 S(Spec), Mangle(Mangle), QueryAnalysis(Interpreter) {}
182
183 void emit(std::unique_ptr<MaterializationResponsibility> R,
184 ThreadSafeModule TSM) override;
185
186private:
187 TargetAndLikelies
188 internToJITSymbols(DenseMap<StringRef, DenseSet<StringRef>> IRNames) {
189 assert(!IRNames.empty() && "No IRNames received to Intern?");
190 TargetAndLikelies InternedNames;
191 for (auto &NamePair : IRNames) {
192 DenseSet<SymbolStringPtr> TargetJITNames;
193 for (auto &TargetNames : NamePair.second)
194 TargetJITNames.insert(Mangle(TargetNames));
195 InternedNames[Mangle(NamePair.first)] = std::move(TargetJITNames);
196 }
197 return InternedNames;
198 }
199
200 IRLayer &NextLayer;
201 Speculator &S;
202 MangleAndInterner &Mangle;
203 ResultEval QueryAnalysis;
204};
205
206} // namespace orc
207} // namespace llvm
208
209#endif // LLVM_EXECUTIONENGINE_ORC_SPECULATION_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
#define I(x, y, z)
Definition MD5.cpp:57
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition Debug.h:72
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:205
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
iterator end()
Definition DenseMap.h:81
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Target - Wrapper for Target specific information.
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
An ExecutionSession represents a running JIT program.
Definition Core.h:1342
void reportError(Error Err)
Report a error for this execution session.
Definition Core.h:1477
Represents an address in the executor process.
IRLayer(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions *&MO)
Definition Layer.h:70
const IRSymbolMapper::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
Definition Layer.h:79
IRSpeculationLayer(ExecutionSession &ES, IRLayer &BaseLayer, Speculator &Spec, MangleAndInterner &Mangle, ResultEval Interpreter)
std::function< IRlikiesStrRef(Function &)> ResultEval
std::optional< DenseMap< StringRef, DenseSet< StringRef > > > IRlikiesStrRef
DenseMap< SymbolStringPtr, SymbolNameSet > TargetAndLikelies
LLVM_ABI void trackImpls(SymbolAliasMap ImplMaps, JITDylib *SrcJD)
SymbolStringPtr Alias
Definition Speculation.h:39
DenseMap< Alias, AliaseeDetails > ImapTy
Definition Speculation.h:40
std::pair< SymbolStringPtr, JITDylib * > AliaseeDetails
Definition Speculation.h:38
Represents a JIT'd dynamic library.
Definition Core.h:906
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition Mangling.h:27
Speculator(ImplSymbolMap &Impl, ExecutionSession &ref)
ExecutionSession & getES()
Speculator & operator=(const Speculator &)=delete
Speculator & operator=(Speculator &&)=delete
DenseMap< TargetFAddr, SymbolNameSet > StubAddrLikelies
Definition Speculation.h:64
Speculator(Speculator &&)=delete
LLVM_ABI Error addSpeculationRuntime(JITDylib &JD, MangleAndInterner &Mangle)
Define symbols for this Speculator object (__orc_speculator) and the speculation runtime entry point ...
ExecutorAddr TargetFAddr
Definition Speculation.h:62
DenseMap< SymbolStringPtr, SymbolNameSet > FunctionCandidatesMap
Definition Speculation.h:63
Speculator(const Speculator &)=delete
void registerSymbols(FunctionCandidatesMap Candidates, JITDylib *JD)
void speculateFor(TargetFAddr StubAddr)
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition Core.h:199
Pointer to a pooled string representing a symbol name.
An LLVM Module together with a shared ThreadSafeContext.
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition Core.h:182
LLVM_ABI RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
Definition Core.cpp:38
DenseMap< JITDylib *, SymbolNameSet > SymbolDependenceMap
A map from JITDylibs to sets of symbols.
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
@ Ready
Emitted to memory, but waiting on transitive dependencies.
Definition Core.h:782
DenseMap< SymbolStringPtr, SymbolAliasMapEntry > SymbolAliasMap
A map of Symbols to (Symbol, Flags) pairs.
Definition Core.h:417
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
#define N