@@ -36,6 +36,7 @@ class MaterializationUnit;
36
36
class MaterializationResponsibility ;
37
37
class JITDylib ;
38
38
class ResourceTracker ;
39
+ class InProgressLookupState ;
39
40
40
41
enum class SymbolState : uint8_t ;
41
42
@@ -215,9 +216,19 @@ class SymbolLookupSet {
215
216
216
217
// / Add an element to the set. The client is responsible for checking that
217
218
// / duplicates are not added.
218
- void add (SymbolStringPtr Name,
219
- SymbolLookupFlags Flags = SymbolLookupFlags::RequiredSymbol) {
219
+ SymbolLookupSet &
220
+ add (SymbolStringPtr Name,
221
+ SymbolLookupFlags Flags = SymbolLookupFlags::RequiredSymbol) {
220
222
Symbols.push_back (std::make_pair (std::move (Name), Flags));
223
+ return *this ;
224
+ }
225
+
226
+ // / Quickly append one lookup set to another.
227
+ SymbolLookupSet &append (SymbolLookupSet Other) {
228
+ Symbols.reserve (Symbols.size () + Other.size ());
229
+ for (auto &KV : Other)
230
+ Symbols.push_back (std::move (KV));
231
+ return *this ;
221
232
}
222
233
223
234
bool empty () const { return Symbols.empty (); }
@@ -783,6 +794,7 @@ enum class SymbolState : uint8_t {
783
794
// / makes a callback when all symbols are available.
784
795
class AsynchronousSymbolQuery {
785
796
friend class ExecutionSession ;
797
+ friend class InProgressFullLookupState ;
786
798
friend class JITDylib ;
787
799
friend class JITSymbolResolverAdapter ;
788
800
friend class MaterializationResponsibility ;
@@ -829,6 +841,22 @@ class AsynchronousSymbolQuery {
829
841
SymbolState RequiredState;
830
842
};
831
843
844
+ // / Wraps state for a lookup-in-progress.
845
+ // / DefinitionGenerators can optionally take ownership of a LookupState object
846
+ // / to suspend a lookup-in-progress while they search for definitions.
847
+ class LookupState {
848
+ friend class ExecutionSession ;
849
+
850
+ public:
851
+ // / Continue the lookup. This can be called by DefinitionGenerators
852
+ // / to re-start a captured query-application operation.
853
+ void continueLookup (Error Err);
854
+
855
+ private:
856
+ LookupState (std::unique_ptr<InProgressLookupState> IPLS);
857
+ std::unique_ptr<InProgressLookupState> IPLS;
858
+ };
859
+
832
860
// / Definition generators can be attached to JITDylibs to generate new
833
861
// / definitions for otherwise unresolved symbols during lookup.
834
862
class DefinitionGenerator {
@@ -841,7 +869,7 @@ class DefinitionGenerator {
841
869
// / JDLookupFlags specifies whether the search should match against
842
870
// / hidden symbols. Finally, Symbols describes the set of unresolved
843
871
// / symbols and their associated lookup flags.
844
- virtual Error tryToGenerate (LookupKind K, JITDylib &JD,
872
+ virtual Error tryToGenerate (LookupState &LS, LookupKind K, JITDylib &JD,
845
873
JITDylibLookupFlags JDLookupFlags,
846
874
const SymbolLookupSet &LookupSet) = 0;
847
875
};
@@ -979,13 +1007,6 @@ class JITDylib : public ThreadSafeRefCountedBase<JITDylib> {
979
1007
// / left unmodified (no symbols are removed).
980
1008
Error remove (const SymbolNameSet &Names);
981
1009
982
- // / Search the given JITDylib for the symbols in Symbols. If found, store
983
- // / the flags for each symbol in Flags. If any required symbols are not found
984
- // / then an error will be returned.
985
- Expected<SymbolFlagsMap> lookupFlags (LookupKind K,
986
- JITDylibLookupFlags JDLookupFlags,
987
- SymbolLookupSet LookupSet);
988
-
989
1010
// / Dump current JITDylib state to OS.
990
1011
void dump (raw_ostream &OS);
991
1012
@@ -1104,19 +1125,19 @@ class JITDylib : public ThreadSafeRefCountedBase<JITDylib> {
1104
1125
void installMaterializationUnit (std::unique_ptr<MaterializationUnit> MU,
1105
1126
ResourceTracker &RT);
1106
1127
1107
- void lookupFlagsImpl (SymbolFlagsMap &Result, LookupKind K,
1108
- JITDylibLookupFlags JDLookupFlags,
1109
- SymbolLookupSet &Unresolved);
1128
+ // void lookupFlagsImpl(SymbolFlagsMap &Result, LookupKind K,
1129
+ // JITDylibLookupFlags JDLookupFlags,
1130
+ // SymbolLookupSet &Unresolved);
1110
1131
1111
- Error lodgeQuery (UnmaterializedInfosList &UMIs,
1112
- std::shared_ptr<AsynchronousSymbolQuery> &Q, LookupKind K,
1113
- JITDylibLookupFlags JDLookupFlags,
1114
- SymbolLookupSet &Unresolved);
1132
+ // Error lodgeQuery(UnmaterializedInfosList &UMIs,
1133
+ // std::shared_ptr<AsynchronousSymbolQuery> &Q, LookupKind K,
1134
+ // JITDylibLookupFlags JDLookupFlags,
1135
+ // SymbolLookupSet &Unresolved);
1115
1136
1116
- Error lodgeQueryImpl (UnmaterializedInfosList &UMIs,
1117
- std::shared_ptr<AsynchronousSymbolQuery> &Q,
1118
- LookupKind K, JITDylibLookupFlags JDLookupFlags,
1119
- SymbolLookupSet &Unresolved);
1137
+ // Error lodgeQueryImpl(UnmaterializedInfosList &UMIs,
1138
+ // std::shared_ptr<AsynchronousSymbolQuery> &Q,
1139
+ // LookupKind K, JITDylibLookupFlags JDLookupFlags,
1140
+ // SymbolLookupSet &Unresolved);
1120
1141
1121
1142
void detachQueryHelper (AsynchronousSymbolQuery &Q,
1122
1143
const SymbolNameSet &QuerySymbols);
@@ -1154,11 +1175,12 @@ class JITDylib : public ThreadSafeRefCountedBase<JITDylib> {
1154
1175
1155
1176
ExecutionSession &ES;
1156
1177
std::string JITDylibName;
1178
+ std::mutex GeneratorsMutex;
1157
1179
bool Open = true ;
1158
1180
SymbolTable Symbols;
1159
1181
UnmaterializedInfosMap UnmaterializedInfos;
1160
1182
MaterializingInfosMap MaterializingInfos;
1161
- std::vector<std::unique_ptr <DefinitionGenerator>> DefGenerators;
1183
+ std::vector<std::shared_ptr <DefinitionGenerator>> DefGenerators;
1162
1184
JITDylibSearchOrder LinkOrder;
1163
1185
ResourceTrackerSP DefaultTracker;
1164
1186
@@ -1199,7 +1221,10 @@ class Platform {
1199
1221
1200
1222
// / An ExecutionSession represents a running JIT program.
1201
1223
class ExecutionSession {
1224
+ friend class InProgressLookupFlagsState ;
1225
+ friend class InProgressFullLookupState ;
1202
1226
friend class JITDylib ;
1227
+ friend class LookupState ;
1203
1228
friend class MaterializationResponsibility ;
1204
1229
friend class ResourceTracker ;
1205
1230
@@ -1290,7 +1315,18 @@ class ExecutionSession {
1290
1315
return *this ;
1291
1316
}
1292
1317
1293
- // / Search the given JITDylib list for the given symbols.
1318
+ // / Search the given JITDylibs to find the flags associated with each of the
1319
+ // / given symbols.
1320
+ void lookupFlags (LookupKind K, JITDylibSearchOrder SearchOrder,
1321
+ SymbolLookupSet Symbols,
1322
+ unique_function<void (Expected<SymbolFlagsMap>)> OnComplete);
1323
+
1324
+ // / Blocking version of lookupFlags.
1325
+ Expected<SymbolFlagsMap> lookupFlags (LookupKind K,
1326
+ JITDylibSearchOrder SearchOrder,
1327
+ SymbolLookupSet Symbols);
1328
+
1329
+ // / Search the given JITDylibs for the given symbols.
1294
1330
// /
1295
1331
// / SearchOrder lists the JITDylibs to search. For each dylib, the associated
1296
1332
// / boolean indicates whether the search should match against non-exported
@@ -1372,7 +1408,7 @@ class ExecutionSession {
1372
1408
MU->materialize (std::move (MR));
1373
1409
}
1374
1410
1375
- void runOutstandingMUs ();
1411
+ void dispatchOutstandingMUs ();
1376
1412
1377
1413
static std::unique_ptr<MaterializationResponsibility>
1378
1414
createMaterializationResponsibility (ResourceTracker &RT,
@@ -1390,8 +1426,36 @@ class ExecutionSession {
1390
1426
void transferResourceTracker (ResourceTracker &DstRT, ResourceTracker &SrcRT);
1391
1427
void destroyResourceTracker (ResourceTracker &RT);
1392
1428
1393
-
1394
- // / State machine functions for MaterializationResponsibility.
1429
+ // State machine functions for query application..
1430
+
1431
+ // / IL_updateCandidatesFor is called to remove already-defined symbols that
1432
+ // / match a given query from the set of candidate symbols to generate
1433
+ // / definitions for (no need to generate a definition if one already exists).
1434
+ Error IL_updateCandidatesFor (JITDylib &JD, JITDylibLookupFlags JDLookupFlags,
1435
+ SymbolLookupSet &Candidates,
1436
+ SymbolLookupSet *NonCandidates);
1437
+
1438
+ // / OL_applyQueryPhase1 is an optionally re-startable loop for triggering
1439
+ // / definition generation. It is called when a lookup is performed, and again
1440
+ // / each time that LookupState::continueLookup is called.
1441
+ void OL_applyQueryPhase1 (std::unique_ptr<InProgressLookupState> IPLS,
1442
+ Error Err);
1443
+
1444
+ // / OL_completeLookup is run once phase 1 successfully completes for a lookup
1445
+ // / call. It attempts to attach the symbol to all symbol table entries and
1446
+ // / collect all MaterializationUnits to dispatch. If this method fails then
1447
+ // / all MaterializationUnits will be left un-materialized.
1448
+ void OL_completeLookup (std::unique_ptr<InProgressLookupState> IPLS,
1449
+ std::shared_ptr<AsynchronousSymbolQuery> Q,
1450
+ RegisterDependenciesFunction RegisterDependencies);
1451
+
1452
+ // / OL_completeLookupFlags is run once phase 1 successfully completes for a
1453
+ // / lookupFlags call.
1454
+ void OL_completeLookupFlags (
1455
+ std::unique_ptr<InProgressLookupState> IPLS,
1456
+ unique_function<void (Expected<SymbolFlagsMap>)> OnComplete);
1457
+
1458
+ // State machine functions for MaterializationResponsibility.
1395
1459
void OL_destroyMaterializationResponsibility (
1396
1460
MaterializationResponsibility &MR);
1397
1461
SymbolNameSet OL_getRequestedSymbols (const MaterializationResponsibility &MR);
@@ -1454,8 +1518,8 @@ Error MaterializationResponsibility::withResourceKeyDo(Func &&F) const {
1454
1518
template <typename GeneratorT>
1455
1519
GeneratorT &JITDylib::addGenerator (std::unique_ptr<GeneratorT> DefGenerator) {
1456
1520
auto &G = *DefGenerator;
1457
- ES. runSessionLocked (
1458
- [&]() { DefGenerators.push_back (std::move (DefGenerator)); } );
1521
+ std::lock_guard<std::mutex> Lock (GeneratorsMutex);
1522
+ DefGenerators.push_back (std::move (DefGenerator));
1459
1523
return G;
1460
1524
}
1461
1525
@@ -1546,7 +1610,7 @@ class ReexportsGenerator : public DefinitionGenerator {
1546
1610
JITDylibLookupFlags SourceJDLookupFlags,
1547
1611
SymbolPredicate Allow = SymbolPredicate());
1548
1612
1549
- Error tryToGenerate (LookupKind K, JITDylib &JD,
1613
+ Error tryToGenerate (LookupState &LS, LookupKind K, JITDylib &JD,
1550
1614
JITDylibLookupFlags JDLookupFlags,
1551
1615
const SymbolLookupSet &LookupSet) override ;
1552
1616
0 commit comments