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