1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
|
//===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "OrcTestCommon.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/OrcError.h"
#include <set>
#include <thread>
using namespace llvm;
using namespace llvm::orc;
class CoreAPIsStandardTest : public CoreAPIsBasedStandardTest {};
namespace {
class SimpleMaterializationUnit : public MaterializationUnit {
public:
using MaterializeFunction =
std::function<void(MaterializationResponsibility)>;
using DiscardFunction = std::function<void(const VSO &, SymbolStringPtr)>;
using DestructorFunction = std::function<void()>;
SimpleMaterializationUnit(
SymbolFlagsMap SymbolFlags, MaterializeFunction Materialize,
DiscardFunction Discard = DiscardFunction(),
DestructorFunction Destructor = DestructorFunction())
: MaterializationUnit(std::move(SymbolFlags)),
Materialize(std::move(Materialize)), Discard(std::move(Discard)),
Destructor(std::move(Destructor)) {}
~SimpleMaterializationUnit() override {
if (Destructor)
Destructor();
}
void materialize(MaterializationResponsibility R) override {
Materialize(std::move(R));
}
void discard(const VSO &V, SymbolStringPtr Name) override {
if (Discard)
Discard(V, std::move(Name));
else
llvm_unreachable("Discard not supported");
}
private:
MaterializeFunction Materialize;
DiscardFunction Discard;
DestructorFunction Destructor;
};
TEST_F(CoreAPIsStandardTest, BasicSuccessfulLookup) {
bool OnResolutionRun = false;
bool OnReadyRun = false;
auto OnResolution = [&](Expected<SymbolMap> Result) {
EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";
auto &Resolved = *Result;
auto I = Resolved.find(Foo);
EXPECT_NE(I, Resolved.end()) << "Could not find symbol definition";
EXPECT_EQ(I->second.getAddress(), FooAddr)
<< "Resolution returned incorrect result";
OnResolutionRun = true;
};
auto OnReady = [&](Error Err) {
cantFail(std::move(Err));
OnReadyRun = true;
};
std::shared_ptr<MaterializationResponsibility> FooMR;
cantFail(V.define(llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
[&](MaterializationResponsibility R) {
FooMR = std::make_shared<MaterializationResponsibility>(std::move(R));
})));
ES.lookup({&V}, {Foo}, OnResolution, OnReady, NoDependenciesToRegister);
EXPECT_FALSE(OnResolutionRun) << "Should not have been resolved yet";
EXPECT_FALSE(OnReadyRun) << "Should not have been marked ready yet";
FooMR->resolve({{Foo, FooSym}});
EXPECT_TRUE(OnResolutionRun) << "Should have been resolved";
EXPECT_FALSE(OnReadyRun) << "Should not have been marked ready yet";
FooMR->finalize();
EXPECT_TRUE(OnReadyRun) << "Should have been marked ready";
}
TEST_F(CoreAPIsStandardTest, ExecutionSessionFailQuery) {
bool OnResolutionRun = false;
bool OnReadyRun = false;
auto OnResolution = [&](Expected<SymbolMap> Result) {
EXPECT_FALSE(!!Result) << "Resolution unexpectedly returned success";
auto Msg = toString(Result.takeError());
EXPECT_EQ(Msg, "xyz") << "Resolution returned incorrect result";
OnResolutionRun = true;
};
auto OnReady = [&](Error Err) {
cantFail(std::move(Err));
OnReadyRun = true;
};
AsynchronousSymbolQuery Q(SymbolNameSet({Foo}), OnResolution, OnReady);
ES.legacyFailQuery(Q,
make_error<StringError>("xyz", inconvertibleErrorCode()));
EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
EXPECT_FALSE(OnReadyRun) << "OnReady unexpectedly run";
}
TEST_F(CoreAPIsStandardTest, EmptyLookup) {
bool OnResolvedRun = false;
bool OnReadyRun = false;
auto OnResolution = [&](Expected<SymbolMap> Result) {
cantFail(std::move(Result));
OnResolvedRun = true;
};
auto OnReady = [&](Error Err) {
cantFail(std::move(Err));
OnReadyRun = true;
};
ES.lookup({&V}, {}, OnResolution, OnReady, NoDependenciesToRegister);
EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run for empty query";
EXPECT_TRUE(OnReadyRun) << "OnReady was not run for empty query";
}
TEST_F(CoreAPIsStandardTest, ChainedVSOLookup) {
cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
auto &V2 = ES.createVSO("V2");
bool OnResolvedRun = false;
bool OnReadyRun = false;
auto Q = std::make_shared<AsynchronousSymbolQuery>(
SymbolNameSet({Foo}),
[&](Expected<SymbolMap> Result) {
cantFail(std::move(Result));
OnResolvedRun = true;
},
[&](Error Err) {
cantFail(std::move(Err));
OnReadyRun = true;
});
V2.legacyLookup(Q, V.legacyLookup(Q, {Foo}));
EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run for empty query";
EXPECT_TRUE(OnReadyRun) << "OnReady was not run for empty query";
}
TEST_F(CoreAPIsStandardTest, LookupFlagsTest) {
// Test that lookupFlags works on a predefined symbol, and does not trigger
// materialization of a lazy symbol. Make the lazy symbol weak to test that
// the weak flag is propagated correctly.
BarSym.setFlags(static_cast<JITSymbolFlags::FlagNames>(
JITSymbolFlags::Exported | JITSymbolFlags::Weak));
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
[](MaterializationResponsibility R) {
llvm_unreachable("Symbol materialized on flags lookup");
});
cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
cantFail(V.define(std::move(MU)));
SymbolNameSet Names({Foo, Bar, Baz});
auto SymbolFlags = V.lookupFlags(Names);
EXPECT_EQ(SymbolFlags.size(), 2U)
<< "Returned symbol flags contains unexpected results";
EXPECT_EQ(SymbolFlags.count(Foo), 1U) << "Missing lookupFlags result for Foo";
EXPECT_EQ(SymbolFlags[Foo], FooSym.getFlags())
<< "Incorrect flags returned for Foo";
EXPECT_EQ(SymbolFlags.count(Bar), 1U)
<< "Missing lookupFlags result for Bar";
EXPECT_EQ(SymbolFlags[Bar], BarSym.getFlags())
<< "Incorrect flags returned for Bar";
}
TEST_F(CoreAPIsStandardTest, TestBasicAliases) {
cantFail(V.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
cantFail(V.define(symbolAliases({{Baz, {Foo, JITSymbolFlags::Exported}},
{Qux, {Bar, JITSymbolFlags::Weak}}})));
cantFail(V.define(absoluteSymbols({{Qux, QuxSym}})));
auto Result = lookup({&V}, {Baz, Qux});
EXPECT_TRUE(!!Result) << "Unexpected lookup failure";
EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";
EXPECT_EQ(Result->count(Qux), 1U) << "No result for \"qux\"";
EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())
<< "\"Baz\"'s address should match \"Foo\"'s";
EXPECT_EQ((*Result)[Qux].getAddress(), QuxSym.getAddress())
<< "The \"Qux\" alias should have been overriden";
}
TEST_F(CoreAPIsStandardTest, TestChainedAliases) {
cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
cantFail(V.define(symbolAliases(
{{Baz, {Bar, BazSym.getFlags()}}, {Bar, {Foo, BarSym.getFlags()}}})));
auto Result = lookup({&V}, {Bar, Baz});
EXPECT_TRUE(!!Result) << "Unexpected lookup failure";
EXPECT_EQ(Result->count(Bar), 1U) << "No result for \"bar\"";
EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";
EXPECT_EQ((*Result)[Bar].getAddress(), FooSym.getAddress())
<< "\"Bar\"'s address should match \"Foo\"'s";
EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())
<< "\"Baz\"'s address should match \"Foo\"'s";
}
TEST_F(CoreAPIsStandardTest, TestBasicReExports) {
// Test that the basic use case of re-exporting a single symbol from another
// VSO works.
cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
auto &V2 = ES.createVSO("V2");
cantFail(V2.define(reexports(V, {{Bar, {Foo, BarSym.getFlags()}}})));
auto Result = cantFail(lookup({&V2}, Bar));
EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
<< "Re-export Bar for symbol Foo should match FooSym's address";
}
TEST_F(CoreAPIsStandardTest, TestThatReExportsDontUnnecessarilyMaterialize) {
// Test that re-exports do not materialize symbols that have not been queried
// for.
cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
bool BarMaterialized = false;
auto BarMU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
[&](MaterializationResponsibility R) {
BarMaterialized = true;
R.resolve({{Bar, BarSym}});
R.finalize();
});
cantFail(V.define(BarMU));
auto &V2 = ES.createVSO("V2");
cantFail(V2.define(reexports(
V, {{Baz, {Foo, BazSym.getFlags()}}, {Qux, {Bar, QuxSym.getFlags()}}})));
auto Result = cantFail(lookup({&V2}, Baz));
EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
<< "Re-export Baz for symbol Foo should match FooSym's address";
EXPECT_FALSE(BarMaterialized) << "Bar should not have been materialized";
}
TEST_F(CoreAPIsStandardTest, TestTrivialCircularDependency) {
Optional<MaterializationResponsibility> FooR;
auto FooMU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
[&](MaterializationResponsibility R) { FooR.emplace(std::move(R)); });
cantFail(V.define(FooMU));
bool FooReady = false;
auto OnResolution = [](Expected<SymbolMap> R) { cantFail(std::move(R)); };
auto OnReady = [&](Error Err) {
cantFail(std::move(Err));
FooReady = true;
};
ES.lookup({&V}, {Foo}, std::move(OnResolution), std::move(OnReady),
NoDependenciesToRegister);
FooR->resolve({{Foo, FooSym}});
FooR->finalize();
EXPECT_TRUE(FooReady)
<< "Self-dependency prevented symbol from being marked ready";
}
TEST_F(CoreAPIsStandardTest, TestCircularDependenceInOneVSO) {
// Test that a circular symbol dependency between three symbols in a VSO does
// not prevent any symbol from becoming 'ready' once all symbols are
// finalized.
// Create three MaterializationResponsibility objects: one for each of Foo,
// Bar and Baz. These are optional because MaterializationResponsibility
// does not have a default constructor).
Optional<MaterializationResponsibility> FooR;
Optional<MaterializationResponsibility> BarR;
Optional<MaterializationResponsibility> BazR;
// Create a MaterializationUnit for each symbol that moves the
// MaterializationResponsibility into one of the locals above.
auto FooMU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
[&](MaterializationResponsibility R) { FooR.emplace(std::move(R)); });
auto BarMU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
[&](MaterializationResponsibility R) { BarR.emplace(std::move(R)); });
auto BazMU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Baz, BazSym.getFlags()}}),
[&](MaterializationResponsibility R) { BazR.emplace(std::move(R)); });
// Define the symbols.
cantFail(V.define(FooMU));
cantFail(V.define(BarMU));
cantFail(V.define(BazMU));
// Query each of the symbols to trigger materialization.
bool FooResolved = false;
bool FooReady = false;
auto OnFooResolution = [&](Expected<SymbolMap> Result) {
cantFail(std::move(Result));
FooResolved = true;
};
auto OnFooReady = [&](Error Err) {
cantFail(std::move(Err));
FooReady = true;
};
// Issue a lookup for Foo. Use NoDependenciesToRegister: We're going to add
// the dependencies manually below.
ES.lookup({&V}, {Foo}, std::move(OnFooResolution), std::move(OnFooReady),
NoDependenciesToRegister);
bool BarResolved = false;
bool BarReady = false;
auto OnBarResolution = [&](Expected<SymbolMap> Result) {
cantFail(std::move(Result));
BarResolved = true;
};
auto OnBarReady = [&](Error Err) {
cantFail(std::move(Err));
BarReady = true;
};
ES.lookup({&V}, {Bar}, std::move(OnBarResolution), std::move(OnBarReady),
NoDependenciesToRegister);
bool BazResolved = false;
bool BazReady = false;
auto OnBazResolution = [&](Expected<SymbolMap> Result) {
cantFail(std::move(Result));
BazResolved = true;
};
auto OnBazReady = [&](Error Err) {
cantFail(std::move(Err));
BazReady = true;
};
ES.lookup({&V}, {Baz}, std::move(OnBazResolution), std::move(OnBazReady),
NoDependenciesToRegister);
// Add a circular dependency: Foo -> Bar, Bar -> Baz, Baz -> Foo.
FooR->addDependenciesForAll({{&V, SymbolNameSet({Bar})}});
BarR->addDependenciesForAll({{&V, SymbolNameSet({Baz})}});
BazR->addDependenciesForAll({{&V, SymbolNameSet({Foo})}});
// Add self-dependencies for good measure. This tests that the implementation
// of addDependencies filters these out.
FooR->addDependenciesForAll({{&V, SymbolNameSet({Foo})}});
BarR->addDependenciesForAll({{&V, SymbolNameSet({Bar})}});
BazR->addDependenciesForAll({{&V, SymbolNameSet({Baz})}});
// Check that nothing has been resolved yet.
EXPECT_FALSE(FooResolved) << "\"Foo\" should not be resolved yet";
EXPECT_FALSE(BarResolved) << "\"Bar\" should not be resolved yet";
EXPECT_FALSE(BazResolved) << "\"Baz\" should not be resolved yet";
// Resolve the symbols (but do not finalized them).
FooR->resolve({{Foo, FooSym}});
BarR->resolve({{Bar, BarSym}});
BazR->resolve({{Baz, BazSym}});
// Verify that the symbols have been resolved, but are not ready yet.
EXPECT_TRUE(FooResolved) << "\"Foo\" should be resolved now";
EXPECT_TRUE(BarResolved) << "\"Bar\" should be resolved now";
EXPECT_TRUE(BazResolved) << "\"Baz\" should be resolved now";
EXPECT_FALSE(FooReady) << "\"Foo\" should not be ready yet";
EXPECT_FALSE(BarReady) << "\"Bar\" should not be ready yet";
EXPECT_FALSE(BazReady) << "\"Baz\" should not be ready yet";
// Finalize two of the symbols.
FooR->finalize();
BarR->finalize();
// Verify that nothing is ready until the circular dependence is resolved.
EXPECT_FALSE(FooReady) << "\"Foo\" still should not be ready";
EXPECT_FALSE(BarReady) << "\"Bar\" still should not be ready";
EXPECT_FALSE(BazReady) << "\"Baz\" still should not be ready";
// Finalize the last symbol.
BazR->finalize();
// Verify that everything becomes ready once the circular dependence resolved.
EXPECT_TRUE(FooReady) << "\"Foo\" should be ready now";
EXPECT_TRUE(BarReady) << "\"Bar\" should be ready now";
EXPECT_TRUE(BazReady) << "\"Baz\" should be ready now";
}
TEST_F(CoreAPIsStandardTest, DropMaterializerWhenEmpty) {
bool DestructorRun = false;
JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
WeakExported |= JITSymbolFlags::Weak;
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, WeakExported}, {Bar, WeakExported}}),
[](MaterializationResponsibility R) {
llvm_unreachable("Unexpected call to materialize");
},
[&](const VSO &V, SymbolStringPtr Name) {
EXPECT_TRUE(Name == Foo || Name == Bar)
<< "Discard of unexpected symbol?";
},
[&]() { DestructorRun = true; });
cantFail(V.define(MU));
cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
EXPECT_FALSE(DestructorRun)
<< "MaterializationUnit should not have been destroyed yet";
cantFail(V.define(absoluteSymbols({{Bar, BarSym}})));
EXPECT_TRUE(DestructorRun)
<< "MaterializationUnit should have been destroyed";
}
TEST_F(CoreAPIsStandardTest, AddAndMaterializeLazySymbol) {
bool FooMaterialized = false;
bool BarDiscarded = false;
JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
WeakExported |= JITSymbolFlags::Weak;
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}, {Bar, WeakExported}}),
[&](MaterializationResponsibility R) {
assert(BarDiscarded && "Bar should have been discarded by this point");
R.resolve(SymbolMap({{Foo, FooSym}}));
R.finalize();
FooMaterialized = true;
},
[&](const VSO &V, SymbolStringPtr Name) {
EXPECT_EQ(Name, Bar) << "Expected Name to be Bar";
BarDiscarded = true;
});
cantFail(V.define(MU));
cantFail(V.define(absoluteSymbols({{Bar, BarSym}})));
SymbolNameSet Names({Foo});
bool OnResolutionRun = false;
bool OnReadyRun = false;
auto OnResolution = [&](Expected<SymbolMap> Result) {
EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";
auto I = Result->find(Foo);
EXPECT_NE(I, Result->end()) << "Could not find symbol definition";
EXPECT_EQ(I->second.getAddress(), FooSym.getAddress())
<< "Resolution returned incorrect result";
OnResolutionRun = true;
};
auto OnReady = [&](Error Err) {
cantFail(std::move(Err));
OnReadyRun = true;
};
ES.lookup({&V}, Names, std::move(OnResolution), std::move(OnReady),
NoDependenciesToRegister);
EXPECT_TRUE(FooMaterialized) << "Foo was not materialized";
EXPECT_TRUE(BarDiscarded) << "Bar was not discarded";
EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
EXPECT_TRUE(OnReadyRun) << "OnReady was not run";
}
TEST_F(CoreAPIsStandardTest, DefineMaterializingSymbol) {
bool ExpectNoMoreMaterialization = false;
ES.setDispatchMaterialization(
[&](VSO &V, std::unique_ptr<MaterializationUnit> MU) {
if (ExpectNoMoreMaterialization)
ADD_FAILURE() << "Unexpected materialization";
MU->doMaterialize(V);
});
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
[&](MaterializationResponsibility R) {
cantFail(
R.defineMaterializing(SymbolFlagsMap({{Bar, BarSym.getFlags()}})));
R.resolve(SymbolMap({{Foo, FooSym}, {Bar, BarSym}}));
R.finalize();
});
cantFail(V.define(MU));
cantFail(lookup({&V}, Foo));
// Assert that materialization is complete by now.
ExpectNoMoreMaterialization = true;
// Look up bar to verify that no further materialization happens.
auto BarResult = cantFail(lookup({&V}, Bar));
EXPECT_EQ(BarResult.getAddress(), BarSym.getAddress())
<< "Expected Bar == BarSym";
}
TEST_F(CoreAPIsStandardTest, FallbackDefinitionGeneratorTest) {
cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
V.setFallbackDefinitionGenerator([&](VSO &W, const SymbolNameSet &Names) {
cantFail(W.define(absoluteSymbols({{Bar, BarSym}})));
return SymbolNameSet({Bar});
});
auto Result = cantFail(lookup({&V}, {Foo, Bar}));
EXPECT_EQ(Result.count(Bar), 1U) << "Expected to find fallback def for 'bar'";
EXPECT_EQ(Result[Bar].getAddress(), BarSym.getAddress())
<< "Expected fallback def for Bar to be equal to BarSym";
}
TEST_F(CoreAPIsStandardTest, FailResolution) {
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap(
{{Foo, JITSymbolFlags::Weak}, {Bar, JITSymbolFlags::Weak}}),
[&](MaterializationResponsibility R) { R.failMaterialization(); });
cantFail(V.define(MU));
SymbolNameSet Names({Foo, Bar});
auto Result = lookup({&V}, Names);
EXPECT_FALSE(!!Result) << "Expected failure";
if (!Result) {
handleAllErrors(Result.takeError(),
[&](FailedToMaterialize &F) {
EXPECT_EQ(F.getSymbols(), Names)
<< "Expected to fail on symbols in Names";
},
[](ErrorInfoBase &EIB) {
std::string ErrMsg;
{
raw_string_ostream ErrOut(ErrMsg);
EIB.log(ErrOut);
}
ADD_FAILURE()
<< "Expected a FailedToResolve error. Got:\n"
<< ErrMsg;
});
}
}
TEST_F(CoreAPIsStandardTest, TestLookupWithUnthreadedMaterialization) {
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
[&](MaterializationResponsibility R) {
R.resolve({{Foo, FooSym}});
R.finalize();
});
cantFail(V.define(MU));
auto FooLookupResult = cantFail(lookup({&V}, Foo));
EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())
<< "lookup returned an incorrect address";
EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
<< "lookup returned incorrect flags";
}
TEST_F(CoreAPIsStandardTest, TestLookupWithThreadedMaterialization) {
#if LLVM_ENABLE_THREADS
std::thread MaterializationThread;
ES.setDispatchMaterialization(
[&](VSO &V, std::unique_ptr<MaterializationUnit> MU) {
auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU));
MaterializationThread =
std::thread([SharedMU, &V]() { SharedMU->doMaterialize(V); });
});
cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
auto FooLookupResult = cantFail(lookup({&V}, Foo));
EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())
<< "lookup returned an incorrect address";
EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
<< "lookup returned incorrect flags";
MaterializationThread.join();
#endif
}
TEST_F(CoreAPIsStandardTest, TestGetRequestedSymbolsAndReplace) {
// Test that GetRequestedSymbols returns the set of symbols that currently
// have pending queries, and test that MaterializationResponsibility's
// replace method can be used to return definitions to the VSO in a new
// MaterializationUnit.
SymbolNameSet Names({Foo, Bar});
bool FooMaterialized = false;
bool BarMaterialized = false;
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
[&](MaterializationResponsibility R) {
auto Requested = R.getRequestedSymbols();
EXPECT_EQ(Requested.size(), 1U) << "Expected one symbol requested";
EXPECT_EQ(*Requested.begin(), Foo) << "Expected \"Foo\" requested";
auto NewMU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
[&](MaterializationResponsibility R2) {
R2.resolve(SymbolMap({{Bar, BarSym}}));
R2.finalize();
BarMaterialized = true;
});
R.replace(std::move(NewMU));
R.resolve(SymbolMap({{Foo, FooSym}}));
R.finalize();
FooMaterialized = true;
});
cantFail(V.define(MU));
EXPECT_FALSE(FooMaterialized) << "Foo should not be materialized yet";
EXPECT_FALSE(BarMaterialized) << "Bar should not be materialized yet";
auto FooSymResult = cantFail(lookup({&V}, Foo));
EXPECT_EQ(FooSymResult.getAddress(), FooSym.getAddress())
<< "Address mismatch for Foo";
EXPECT_TRUE(FooMaterialized) << "Foo should be materialized now";
EXPECT_FALSE(BarMaterialized) << "Bar still should not be materialized";
auto BarSymResult = cantFail(lookup({&V}, Bar));
EXPECT_EQ(BarSymResult.getAddress(), BarSym.getAddress())
<< "Address mismatch for Bar";
EXPECT_TRUE(BarMaterialized) << "Bar should be materialized now";
}
TEST_F(CoreAPIsStandardTest, TestMaterializationResponsibilityDelegation) {
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
[&](MaterializationResponsibility R) {
auto R2 = R.delegate({Bar});
R.resolve({{Foo, FooSym}});
R.finalize();
R2.resolve({{Bar, BarSym}});
R2.finalize();
});
cantFail(V.define(MU));
auto Result = lookup({&V}, {Foo, Bar});
EXPECT_TRUE(!!Result) << "Result should be a success value";
EXPECT_EQ(Result->count(Foo), 1U) << "\"Foo\" entry missing";
EXPECT_EQ(Result->count(Bar), 1U) << "\"Bar\" entry missing";
EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress())
<< "Address mismatch for \"Foo\"";
EXPECT_EQ((*Result)[Bar].getAddress(), BarSym.getAddress())
<< "Address mismatch for \"Bar\"";
}
TEST_F(CoreAPIsStandardTest, TestMaterializeWeakSymbol) {
// Confirm that once a weak definition is selected for materialization it is
// treated as strong.
JITSymbolFlags WeakExported = JITSymbolFlags::Exported;
WeakExported &= JITSymbolFlags::Weak;
std::unique_ptr<MaterializationResponsibility> FooResponsibility;
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
[&](MaterializationResponsibility R) {
FooResponsibility =
llvm::make_unique<MaterializationResponsibility>(std::move(R));
});
cantFail(V.define(MU));
auto OnResolution = [](Expected<SymbolMap> Result) {
cantFail(std::move(Result));
};
auto OnReady = [](Error Err) { cantFail(std::move(Err)); };
ES.lookup({&V}, {Foo}, std::move(OnResolution), std::move(OnReady),
NoDependenciesToRegister);
auto MU2 = llvm::make_unique<SimpleMaterializationUnit>(
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
[](MaterializationResponsibility R) {
llvm_unreachable("This unit should never be materialized");
});
auto Err = V.define(MU2);
EXPECT_TRUE(!!Err) << "Expected failure value";
EXPECT_TRUE(Err.isA<DuplicateDefinition>())
<< "Expected a duplicate definition error";
consumeError(std::move(Err));
FooResponsibility->resolve(SymbolMap({{Foo, FooSym}}));
FooResponsibility->finalize();
}
} // namespace
|