llvm.org GIT mirror llvm / 734c480
[ORC] Switch to shared_ptr ownership for SymbolSources in VSOs. This makes it easy to free a SymbolSource (and any related resources) when the last reference in a VSO is dropped. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325727 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 2 years ago
4 changed file(s) with 89 addition(s) and 80 deletion(s). Raw diff Collapse all Expand all
5151 Common = 1U << 2,
5252 Absolute = 1U << 3,
5353 Exported = 1U << 4,
54 NotMaterialized = 1U << 5,
55 Materializing = 1U << 6
54 NotMaterialized = 1U << 5
5655 };
5756
5857 static JITSymbolFlags stripTransientFlags(JITSymbolFlags Orig) {
59 return static_cast(Orig.Flags &
60 ~(NotMaterialized | Materializing));
58 return static_cast(Orig.Flags & ~NotMaterialized);
6159 }
6260
6361 /// @brief Default-construct a JITSymbolFlags instance.
7977 /// @brief Returns true if this symbol has been fully materialized (i.e. is
8078 /// callable).
8179 bool isMaterialized() const { return !(Flags & NotMaterialized); }
82
83 /// @brief Returns true if this symbol is in the process of being
84 /// materialized. This is generally only of interest as an
85 /// implementation detail to JIT infrastructure.
86 bool isMaterializing() const { return Flags & Materializing; }
8780
8881 /// @brief Returns true if the Weak flag is set.
8982 bool isWeak() const {
200200
201201 using SetDefinitionsResult =
202202 std::map;
203 using SourceWorkMap = std::map<SymbolSource *, SymbolNameSet>;
203 using SourceWorkMap = std::map<std::shared_ptr, SymbolNameSet>;
204204
205205 struct LookupResult {
206206 SourceWorkMap MaterializationWork;
230230 Error define(SymbolMap NewSymbols);
231231
232232 /// @brief Adds the given symbols to the mapping as lazy symbols.
233 Error defineLazy(const SymbolFlagsMap &NewSymbols, SymbolSource &Source);
233 Error defineLazy(const SymbolFlagsMap &NewSymbols,
234 std::shared_ptr Source);
234235
235236 /// @brief Add the given symbol/address mappings to the dylib, but do not
236237 /// mark the symbols as finalized yet.
264265 class MaterializationInfo {
265266 public:
266267 MaterializationInfo(JITSymbolFlags Flags,
267 std::shared_ptr<AsynchronousSymbolQuery> Query);
268 std::shared_ptr<SymbolSource> Query);
268269 JITSymbolFlags getFlags() const;
269270 JITTargetAddress getAddress() const;
270 void query(SymbolStringPtr Name,
271 std::shared_ptr Query);
272 void resolve(SymbolStringPtr Name, JITEvaluatedSymbol Sym);
271 void replaceWithSource(VSO &V, SymbolStringPtr Name,
272 JITSymbolFlags NewFlags,
273 std::shared_ptr NewSource);
274 std::shared_ptr
275 query(SymbolStringPtr Name, std::shared_ptr Query);
276 void resolve(VSO &V, SymbolStringPtr Name, JITEvaluatedSymbol Sym);
273277 void finalize();
274278
275279 private:
276280 JITSymbolFlags Flags;
277281 JITTargetAddress Address = 0;
282 std::shared_ptr Source;
278283 std::vector> PendingResolution;
279284 std::vector> PendingFinalization;
280285 };
281286
282287 class SymbolTableEntry {
283288 public:
284 SymbolTableEntry(JITSymbolFlags Flags, SymbolSource &Source);
289 SymbolTableEntry(JITSymbolFlags Flags,
290 std::shared_ptr Source);
285291 SymbolTableEntry(JITEvaluatedSymbol Sym);
286292 SymbolTableEntry(SymbolTableEntry &&Other);
287293 ~SymbolTableEntry();
288294 JITSymbolFlags getFlags() const;
289295 void replaceWithSource(VSO &V, SymbolStringPtr Name, JITSymbolFlags Flags,
290 SymbolSource &NewSource);
291 SymbolSource *query(SymbolStringPtr Name,
292 std::shared_ptr Query);
296 std::shared_ptr NewSource);
297 std::shared_ptr
298 query(SymbolStringPtr Name, std::shared_ptr Query);
293299 void resolve(VSO &V, SymbolStringPtr Name, JITEvaluatedSymbol Sym);
294300 void finalize();
295301
297303 JITSymbolFlags Flags;
298304 union {
299305 JITTargetAddress Address;
300 SymbolSource *Source;
301306 std::unique_ptr MatInfo;
302307 };
303308 };
6464 }
6565
6666 VSO::MaterializationInfo::MaterializationInfo(
67 JITSymbolFlags Flags, std::shared_ptr Query)
68 : Flags(std::move(Flags)), PendingResolution({std::move(Query)}) {}
67 JITSymbolFlags Flags, std::shared_ptr Source)
68 : Flags(std::move(Flags)), Source(std::move(Source)) {}
6969
7070 JITSymbolFlags VSO::MaterializationInfo::getFlags() const { return Flags; }
7171
7373 return Address;
7474 }
7575
76 void VSO::MaterializationInfo::query(
76 void VSO::MaterializationInfo::replaceWithSource(
77 VSO &V, SymbolStringPtr Name, JITSymbolFlags NewFlags,
78 std::shared_ptr NewSource) {
79 assert(Address == 0 && PendingResolution.empty() &&
80 PendingFinalization.empty() &&
81 "Cannot replace source during or after materialization");
82 Source->discard(V, Name);
83 Flags = std::move(NewFlags);
84 Source = std::move(NewSource);
85 }
86
87 std::shared_ptr VSO::MaterializationInfo::query(
7788 SymbolStringPtr Name, std::shared_ptr Query) {
78 if (Address != 0) {
79 Query->setDefinition(Name, JITEvaluatedSymbol(Address, Flags));
80 PendingFinalization.push_back(std::move(Query));
81 } else
89 if (Address == 0) {
8290 PendingResolution.push_back(std::move(Query));
83 }
84
85 void VSO::MaterializationInfo::resolve(SymbolStringPtr Name,
91 auto S = std::move(Source);
92 Source = nullptr;
93 return S;
94 }
95
96 Query->setDefinition(Name, JITEvaluatedSymbol(Address, Flags));
97 PendingFinalization.push_back(std::move(Query));
98 return nullptr;
99 }
100
101 void VSO::MaterializationInfo::resolve(VSO &V, SymbolStringPtr Name,
86102 JITEvaluatedSymbol Sym) {
103 if (Source) {
104 Source->discard(V, Name);
105 Source = nullptr;
106 }
107
87108 // FIXME: Sanity check flags?
88109 Flags = Sym.getFlags();
89110 Address = Sym.getAddress();
101122 }
102123
103124 VSO::SymbolTableEntry::SymbolTableEntry(JITSymbolFlags Flags,
104 SymbolSource &Source)
125 std::shared_ptr Source)
105126 : Flags(JITSymbolFlags::FlagNames(Flags | JITSymbolFlags::NotMaterialized)),
106 Source(&Source) {
127 MatInfo(
128 llvm::make_unique(Flags, std::move(Source))) {
107129 // FIXME: Assert flag sanity.
108130 }
109131
114136
115137 VSO::SymbolTableEntry::SymbolTableEntry(SymbolTableEntry &&Other)
116138 : Flags(Other.Flags), Address(0) {
117 if (Flags.isMaterializing())
139 if (Flags.isMaterialized())
140 Address = Other.Address;
141 else
118142 MatInfo = std::move(Other.MatInfo);
143 }
144
145 VSO::SymbolTableEntry::~SymbolTableEntry() {
146 if (!Flags.isMaterialized())
147 MatInfo.std::unique_ptr::~unique_ptr();
148 }
149
150 JITSymbolFlags VSO::SymbolTableEntry::getFlags() const { return Flags; }
151
152 void VSO::SymbolTableEntry::replaceWithSource(
153 VSO &V, SymbolStringPtr Name, JITSymbolFlags NewFlags,
154 std::shared_ptr NewSource) {
155 bool ReplaceExisting = !Flags.isMaterialized();
156 Flags = NewFlags;
157 if (ReplaceExisting)
158 MatInfo->replaceWithSource(V, Name, Flags, std::move(NewSource));
119159 else
120 Source = Other.Source;
121 }
122
123 VSO::SymbolTableEntry::~SymbolTableEntry() {
124 assert(!Flags.isMaterializing() &&
125 "Symbol table entry destroyed while symbol was being materialized");
126 }
127
128 JITSymbolFlags VSO::SymbolTableEntry::getFlags() const { return Flags; }
129
130 void VSO::SymbolTableEntry::replaceWithSource(VSO &V, SymbolStringPtr Name,
131 JITSymbolFlags Flags,
132 SymbolSource &NewSource) {
133 assert(!this->Flags.isMaterializing() &&
134 "Attempted to replace symbol with lazy definition during "
135 "materialization");
136 if (!this->Flags.isMaterialized())
137 Source->discard(V, Name);
138 this->Flags = Flags;
139 this->Source = &NewSource;
140 }
141
142 SymbolSource *
160 new (&MatInfo) std::unique_ptr(
161 llvm::make_unique(Flags, std::move(NewSource)));
162 }
163
164 std::shared_ptr
143165 VSO::SymbolTableEntry::query(SymbolStringPtr Name,
144166 std::shared_ptr Query) {
145 if (Flags.isMaterializing()) {
146 MatInfo->query(std::move(Name), std::move(Query));
147 return nullptr;
148 } else if (Flags.isMaterialized()) {
167 if (Flags.isMaterialized()) {
149168 Query->setDefinition(std::move(Name), JITEvaluatedSymbol(Address, Flags));
150169 Query->notifySymbolFinalized();
151170 return nullptr;
152 }
153 SymbolSource *S = Source;
154 new (&MatInfo) std::unique_ptr(
155 llvm::make_unique(Flags, std::move(Query)));
156 Flags |= JITSymbolFlags::Materializing;
157 return S;
171 } else
172 return MatInfo->query(std::move(Name), std::move(Query));
158173 }
159174
160175 void VSO::SymbolTableEntry::resolve(VSO &V, SymbolStringPtr Name,
161176 JITEvaluatedSymbol Sym) {
162 if (Flags.isMaterializing())
163 MatInfo->resolve(std::move(Name), std::move(Sym));
164 else {
165 // If there's a layer for this symbol.
166 if (!Flags.isMaterialized())
167 Source->discard(V, Name);
168
177 if (Flags.isMaterialized()) {
169178 // FIXME: Should we assert flag state here (flags must match except for
170179 // materialization state, overrides must be legal) or in the caller
171180 // in VSO?
172181 Flags = Sym.getFlags();
173182 Address = Sym.getAddress();
174 }
183 } else
184 MatInfo->resolve(V, std::move(Name), std::move(Sym));
175185 }
176186
177187 void VSO::SymbolTableEntry::finalize() {
178 if (Flags.isMaterializing()) {
188 if (!Flags.isMaterialized()) {
179189 auto TmpMatInfo = std::move(MatInfo);
180190 MatInfo.std::unique_ptr::~unique_ptr();
181191 // FIXME: Assert flag sanity?
242252 return Err;
243253 }
244254
245 Error VSO::defineLazy(const SymbolFlagsMap &NewSymbols, SymbolSource &Source) {
255 Error VSO::defineLazy(const SymbolFlagsMap &NewSymbols,
256 std::shared_ptr Source) {
246257 Error Err = Error::success();
247258 for (auto &KV : NewSymbols) {
248259 auto I = Symbols.find(KV.first);
254265
255266 // Discard weaker definitions.
256267 if (LinkageResult == ExistingDefinitionIsStronger)
257 Source.discard(*this, KV.first);
268 Source->discard(*this, KV.first);
258269
259270 // Report duplicate definition errors.
260271 if (LinkageResult == DuplicateDefinition) {
325336 // Forward the query to the given SymbolTableEntry, and if it return a
326337 // layer to perform materialization with, add that to the
327338 // MaterializationWork map.
328 if (auto *Source = SymI->second.query(SymI->first, Query))
339 if (auto Source = SymI->second.query(SymI->first, Query))
329340 MaterializationWork[Source].insert(SymI->first);
330341 }
331342
159159 cantFail(V.define(std::move(InitialDefs)));
160160
161161 SymbolFlagsMap InitialLazyDefs({{Bar, BarFlags}});
162 cantFail(V.defineLazy(InitialLazyDefs, *Source));
162 cantFail(V.defineLazy(InitialLazyDefs, Source));
163163
164164 SymbolNameSet Names({Foo, Bar, Baz});
165165
216216 {{Foo, JITSymbolFlags::Exported},
217217 {Bar, static_cast(JITSymbolFlags::Exported |
218218 JITSymbolFlags::Weak)}});
219 cantFail(V.defineLazy(InitialSymbols, *Source));
219 cantFail(V.defineLazy(InitialSymbols, Source));
220220
221221 SymbolMap BarOverride;
222222 BarOverride[Bar] = JITEvaluatedSymbol(FakeBarAddr, JITSymbolFlags::Exported);