LLVM 20.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"
20#include "llvm/Support/Debug.h"
21#include <mutex>
22#include <type_traits>
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 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:
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
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition: Debug.h:64
This file defines the DenseMap class.
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:211
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Target - Wrapper for Target specific information.
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:213
An ExecutionSession represents a running JIT program.
Definition: Core.h:1339
void reportError(Error Err)
Report a error for this execution session.
Definition: Core.h:1474
void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylibs for the given symbols.
Definition: Core.cpp:1788
Represents an address in the executor process.
Interface for layers that accept LLVM IR.
Definition: Layer.h:67
const IRSymbolMapper::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
Definition: Layer.h:78
IRSpeculationLayer(ExecutionSession &ES, IRLayer &BaseLayer, Speculator &Spec, MangleAndInterner &Mangle, ResultEval Interpreter)
Definition: Speculation.h:178
std::function< IRlikiesStrRef(Function &)> ResultEval
Definition: Speculation.h:175
DenseMap< SymbolStringPtr, SymbolNameSet > TargetAndLikelies
Definition: Speculation.h:176
std::optional< DenseMap< StringRef, DenseSet< StringRef > > > IRlikiesStrRef
Definition: Speculation.h:174
void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM) override
Emit should materialize the given IR.
Definition: Speculation.cpp:59
void trackImpls(SymbolAliasMap ImplMaps, JITDylib *SrcJD)
Definition: Speculation.cpp:27
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:897
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Mangling.h:26
Speculator(ImplSymbolMap &Impl, ExecutionSession &ref)
Definition: Speculation.h:122
ExecutionSession & getES()
Definition: Speculation.h:161
Speculator & operator=(const Speculator &)=delete
Speculator & operator=(Speculator &&)=delete
DenseMap< TargetFAddr, SymbolNameSet > StubAddrLikelies
Definition: Speculation.h:64
Speculator(Speculator &&)=delete
Error addSpeculationRuntime(JITDylib &JD, MangleAndInterner &Mangle)
Define symbols for this Speculator object (__orc_speculator) and the speculation runtime entry point ...
Definition: Speculation.cpp:44
Speculator(const Speculator &)=delete
void registerSymbols(FunctionCandidatesMap Candidates, JITDylib *JD)
Definition: Speculation.h:139
void speculateFor(TargetFAddr StubAddr)
Definition: Speculation.h:136
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:194
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:177
RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
Definition: Core.cpp:38
@ Ready
Emitted to memory, but waiting on transitive dependencies.
DenseMap< JITDylib *, SymbolNameSet > SymbolDependenceMap
A map from JITDylibs to sets of symbols.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
#define N