2018-01-22 11:00:31 +08:00
|
|
|
//===----------- 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/ExecutionEngine/Orc/Legacy.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::orc;
|
|
|
|
|
2018-07-21 02:31:50 +08:00
|
|
|
class LegacyAPIsStandardTest : public CoreAPIsBasedStandardTest {};
|
|
|
|
|
2018-01-22 11:00:31 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-07-21 02:31:50 +08:00
|
|
|
TEST_F(LegacyAPIsStandardTest, TestLambdaSymbolResolver) {
|
|
|
|
cantFail(V.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
|
|
|
|
|
|
|
|
auto Resolver = createSymbolResolver(
|
2018-07-21 02:31:52 +08:00
|
|
|
[&](const SymbolNameSet &Symbols) { return V.lookupFlags(Symbols); },
|
2018-07-21 02:31:50 +08:00
|
|
|
[&](std::shared_ptr<AsynchronousSymbolQuery> Q, SymbolNameSet Symbols) {
|
2018-07-21 06:22:19 +08:00
|
|
|
return V.legacyLookup(std::move(Q), Symbols);
|
2018-07-21 02:31:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
SymbolNameSet Symbols({Foo, Bar, Baz});
|
|
|
|
|
2018-07-21 02:31:52 +08:00
|
|
|
SymbolFlagsMap SymbolFlags = Resolver->lookupFlags(Symbols);
|
2018-07-21 02:31:50 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(SymbolFlags.size(), 2U)
|
|
|
|
<< "lookupFlags returned the wrong number of results";
|
|
|
|
EXPECT_EQ(SymbolFlags.count(Foo), 1U) << "Missing lookupFlags result for foo";
|
|
|
|
EXPECT_EQ(SymbolFlags.count(Bar), 1U) << "Missing lookupFlags result for bar";
|
|
|
|
EXPECT_EQ(SymbolFlags[Foo], FooSym.getFlags())
|
|
|
|
<< "Incorrect lookupFlags result for Foo";
|
|
|
|
EXPECT_EQ(SymbolFlags[Bar], BarSym.getFlags())
|
|
|
|
<< "Incorrect lookupFlags result for Bar";
|
|
|
|
|
|
|
|
bool OnResolvedRun = false;
|
|
|
|
|
2018-07-21 06:22:19 +08:00
|
|
|
auto OnResolved = [&](Expected<SymbolMap> Result) {
|
|
|
|
OnResolvedRun = true;
|
|
|
|
EXPECT_TRUE(!!Result) << "Unexpected error";
|
|
|
|
EXPECT_EQ(Result->size(), 2U) << "Unexpected number of resolved symbols";
|
|
|
|
EXPECT_EQ(Result->count(Foo), 1U) << "Missing lookup result for foo";
|
|
|
|
EXPECT_EQ(Result->count(Bar), 1U) << "Missing lookup result for bar";
|
|
|
|
EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress())
|
|
|
|
<< "Incorrect address for foo";
|
|
|
|
EXPECT_EQ((*Result)[Bar].getAddress(), BarSym.getAddress())
|
|
|
|
<< "Incorrect address for bar";
|
|
|
|
};
|
2018-07-21 02:31:50 +08:00
|
|
|
auto OnReady = [&](Error Err) {
|
|
|
|
EXPECT_FALSE(!!Err) << "Finalization should never fail in this test";
|
|
|
|
};
|
|
|
|
|
|
|
|
auto Q = std::make_shared<AsynchronousSymbolQuery>(SymbolNameSet({Foo, Bar}),
|
|
|
|
OnResolved, OnReady);
|
|
|
|
auto Unresolved = Resolver->lookup(std::move(Q), Symbols);
|
|
|
|
|
|
|
|
EXPECT_EQ(Unresolved.size(), 1U) << "Expected one unresolved symbol";
|
|
|
|
EXPECT_EQ(Unresolved.count(Baz), 1U) << "Expected baz to not be resolved";
|
|
|
|
EXPECT_TRUE(OnResolvedRun) << "OnResolved was never run";
|
|
|
|
}
|
|
|
|
|
2018-01-22 11:00:31 +08:00
|
|
|
TEST(LegacyAPIInteropTest, QueryAgainstVSO) {
|
|
|
|
|
2018-04-03 04:57:56 +08:00
|
|
|
ExecutionSession ES(std::make_shared<SymbolStringPool>());
|
|
|
|
auto Foo = ES.getSymbolStringPool().intern("foo");
|
2018-01-22 11:00:31 +08:00
|
|
|
|
2018-05-17 06:24:30 +08:00
|
|
|
auto &V = ES.createVSO("V");
|
2018-01-22 11:00:31 +08:00
|
|
|
JITEvaluatedSymbol FooSym(0xdeadbeef, JITSymbolFlags::Exported);
|
2018-05-17 06:24:30 +08:00
|
|
|
cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-01-22 11:00:31 +08:00
|
|
|
|
2018-07-21 02:31:52 +08:00
|
|
|
auto LookupFlags = [&](const SymbolNameSet &Names) {
|
|
|
|
return V.lookupFlags(Names);
|
2018-01-22 11:00:31 +08:00
|
|
|
};
|
|
|
|
|
2018-02-15 06:12:56 +08:00
|
|
|
auto Lookup = [&](std::shared_ptr<AsynchronousSymbolQuery> Query,
|
|
|
|
SymbolNameSet Symbols) {
|
2018-07-21 06:22:19 +08:00
|
|
|
return V.legacyLookup(std::move(Query), Symbols);
|
2018-01-22 11:00:31 +08:00
|
|
|
};
|
|
|
|
|
2018-02-15 06:12:56 +08:00
|
|
|
auto UnderlyingResolver =
|
|
|
|
createSymbolResolver(std::move(LookupFlags), std::move(Lookup));
|
2018-05-17 06:24:30 +08:00
|
|
|
JITSymbolResolverAdapter Resolver(ES, *UnderlyingResolver, nullptr);
|
2018-01-22 11:00:31 +08:00
|
|
|
|
|
|
|
JITSymbolResolver::LookupSet Names{StringRef("foo")};
|
|
|
|
|
|
|
|
auto LFR = Resolver.lookupFlags(Names);
|
|
|
|
EXPECT_TRUE(!!LFR) << "lookupFlags failed";
|
|
|
|
EXPECT_EQ(LFR->size(), 1U)
|
|
|
|
<< "lookupFlags returned the wrong number of results";
|
|
|
|
EXPECT_EQ(LFR->count(*Foo), 1U)
|
|
|
|
<< "lookupFlags did not contain a result for 'foo'";
|
|
|
|
EXPECT_EQ((*LFR)[*Foo], FooSym.getFlags())
|
|
|
|
<< "lookupFlags contained the wrong result for 'foo'";
|
|
|
|
|
|
|
|
auto LR = Resolver.lookup(Names);
|
|
|
|
EXPECT_TRUE(!!LR) << "lookup failed";
|
|
|
|
EXPECT_EQ(LR->size(), 1U) << "lookup returned the wrong number of results";
|
|
|
|
EXPECT_EQ(LR->count(*Foo), 1U) << "lookup did not contain a result for 'foo'";
|
|
|
|
EXPECT_EQ((*LR)[*Foo].getFlags(), FooSym.getFlags())
|
|
|
|
<< "lookup returned the wrong result for flags of 'foo'";
|
|
|
|
EXPECT_EQ((*LR)[*Foo].getAddress(), FooSym.getAddress())
|
|
|
|
<< "lookup returned the wrong result for address of 'foo'";
|
|
|
|
}
|
|
|
|
|
2018-01-25 07:09:07 +08:00
|
|
|
TEST(LegacyAPIInteropTset, LegacyLookupHelpersFn) {
|
|
|
|
constexpr JITTargetAddress FooAddr = 0xdeadbeef;
|
|
|
|
JITSymbolFlags FooFlags = JITSymbolFlags::Exported;
|
|
|
|
|
|
|
|
bool BarMaterialized = false;
|
|
|
|
constexpr JITTargetAddress BarAddr = 0xcafef00d;
|
|
|
|
JITSymbolFlags BarFlags = static_cast<JITSymbolFlags::FlagNames>(
|
|
|
|
JITSymbolFlags::Exported | JITSymbolFlags::Weak);
|
|
|
|
|
|
|
|
auto LegacyLookup = [&](const std::string &Name) -> JITSymbol {
|
|
|
|
if (Name == "foo")
|
|
|
|
return {FooAddr, FooFlags};
|
|
|
|
|
|
|
|
if (Name == "bar") {
|
|
|
|
auto BarMaterializer = [&]() -> Expected<JITTargetAddress> {
|
|
|
|
BarMaterialized = true;
|
|
|
|
return BarAddr;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {BarMaterializer, BarFlags};
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
2018-05-17 06:24:30 +08:00
|
|
|
ExecutionSession ES;
|
|
|
|
auto Foo = ES.getSymbolStringPool().intern("foo");
|
|
|
|
auto Bar = ES.getSymbolStringPool().intern("bar");
|
|
|
|
auto Baz = ES.getSymbolStringPool().intern("baz");
|
2018-01-25 07:09:07 +08:00
|
|
|
|
|
|
|
SymbolNameSet Symbols({Foo, Bar, Baz});
|
|
|
|
|
2018-07-21 02:31:52 +08:00
|
|
|
auto SymbolFlags = lookupFlagsWithLegacyFn(Symbols, LegacyLookup);
|
|
|
|
|
|
|
|
EXPECT_TRUE(!!SymbolFlags) << "Expected lookupFlagsWithLegacyFn to succeed";
|
|
|
|
EXPECT_EQ(SymbolFlags->size(), 2U) << "Wrong number of flags returned";
|
|
|
|
EXPECT_EQ(SymbolFlags->count(Foo), 1U) << "Flags for foo missing";
|
|
|
|
EXPECT_EQ(SymbolFlags->count(Bar), 1U) << "Flags for foo missing";
|
|
|
|
EXPECT_EQ((*SymbolFlags)[Foo], FooFlags) << "Wrong flags for foo";
|
|
|
|
EXPECT_EQ((*SymbolFlags)[Bar], BarFlags) << "Wrong flags for foo";
|
2018-01-25 07:09:07 +08:00
|
|
|
EXPECT_FALSE(BarMaterialized)
|
|
|
|
<< "lookupFlags should not have materialized bar";
|
|
|
|
|
|
|
|
bool OnResolvedRun = false;
|
|
|
|
bool OnReadyRun = false;
|
2018-07-21 06:22:19 +08:00
|
|
|
auto OnResolved = [&](Expected<SymbolMap> Result) {
|
|
|
|
OnResolvedRun = true;
|
|
|
|
EXPECT_TRUE(!!Result) << "lookuWithLegacy failed to resolve";
|
|
|
|
|
|
|
|
EXPECT_EQ(Result->size(), 2U) << "Wrong number of symbols resolved";
|
|
|
|
EXPECT_EQ(Result->count(Foo), 1U) << "Result for foo missing";
|
|
|
|
EXPECT_EQ(Result->count(Bar), 1U) << "Result for bar missing";
|
|
|
|
EXPECT_EQ((*Result)[Foo].getAddress(), FooAddr) << "Wrong address for foo";
|
|
|
|
EXPECT_EQ((*Result)[Foo].getFlags(), FooFlags) << "Wrong flags for foo";
|
|
|
|
EXPECT_EQ((*Result)[Bar].getAddress(), BarAddr) << "Wrong address for bar";
|
|
|
|
EXPECT_EQ((*Result)[Bar].getFlags(), BarFlags) << "Wrong flags for bar";
|
|
|
|
};
|
2018-01-25 07:09:07 +08:00
|
|
|
auto OnReady = [&](Error Err) {
|
|
|
|
EXPECT_FALSE(!!Err) << "Finalization unexpectedly failed";
|
|
|
|
OnReadyRun = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
AsynchronousSymbolQuery Q({Foo, Bar}, OnResolved, OnReady);
|
2018-05-17 06:24:30 +08:00
|
|
|
auto Unresolved = lookupWithLegacyFn(ES, Q, Symbols, LegacyLookup);
|
2018-01-25 07:09:07 +08:00
|
|
|
|
|
|
|
EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run";
|
|
|
|
EXPECT_TRUE(OnReadyRun) << "OnReady was not run";
|
|
|
|
EXPECT_EQ(Unresolved.size(), 1U) << "Expected one unresolved symbol";
|
|
|
|
EXPECT_EQ(Unresolved.count(Baz), 1U) << "Expected baz to be unresolved";
|
|
|
|
}
|
|
|
|
|
2018-01-22 11:00:31 +08:00
|
|
|
} // namespace
|