2018-01-22 11:00:31 +08:00
|
|
|
//===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-01-22 11:00:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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) {
|
2018-09-02 09:28:26 +08:00
|
|
|
BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak);
|
2018-08-29 05:18:05 +08:00
|
|
|
|
2018-08-18 05:18:18 +08:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
|
2018-07-21 02:31:50 +08:00
|
|
|
|
|
|
|
auto Resolver = createSymbolResolver(
|
2018-08-29 05:18:05 +08:00
|
|
|
[&](const SymbolNameSet &Symbols) {
|
2019-11-26 13:57:27 +08:00
|
|
|
auto FlagsMap = cantFail(JD.lookupFlags(
|
|
|
|
LookupKind::Static, JITDylibLookupFlags::MatchExportedSymbolsOnly,
|
|
|
|
SymbolLookupSet(Symbols)));
|
2018-08-29 05:18:05 +08:00
|
|
|
SymbolNameSet Result;
|
|
|
|
for (auto &KV : FlagsMap)
|
|
|
|
if (!KV.second.isStrong())
|
|
|
|
Result.insert(KV.first);
|
|
|
|
return Result;
|
|
|
|
},
|
2018-07-21 02:31:50 +08:00
|
|
|
[&](std::shared_ptr<AsynchronousSymbolQuery> Q, SymbolNameSet Symbols) {
|
2019-04-30 08:03:26 +08:00
|
|
|
return cantFail(JD.legacyLookup(std::move(Q), Symbols));
|
2018-07-21 02:31:50 +08:00
|
|
|
});
|
|
|
|
|
2018-08-29 05:18:05 +08:00
|
|
|
auto RS = Resolver->getResponsibilitySet(SymbolNameSet({Bar, Baz}));
|
2018-07-21 02:31:50 +08:00
|
|
|
|
2018-08-29 05:18:05 +08:00
|
|
|
EXPECT_EQ(RS.size(), 1U)
|
|
|
|
<< "getResponsibilitySet returned the wrong number of results";
|
|
|
|
EXPECT_EQ(RS.count(Bar), 1U)
|
|
|
|
<< "getResponsibilitySet result incorrect. Should be {'bar'}";
|
2018-07-21 02:31:50 +08:00
|
|
|
|
2019-06-08 03:33:51 +08:00
|
|
|
bool OnCompletionRun = false;
|
2018-07-21 02:31:50 +08:00
|
|
|
|
2019-06-08 03:33:51 +08:00
|
|
|
auto OnCompletion = [&](Expected<SymbolMap> Result) {
|
|
|
|
OnCompletionRun = true;
|
2018-07-21 06:22:19 +08:00
|
|
|
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
|
|
|
|
2019-06-08 03:33:51 +08:00
|
|
|
auto Q = std::make_shared<AsynchronousSymbolQuery>(
|
2019-11-26 13:57:27 +08:00
|
|
|
SymbolLookupSet({Foo, Bar}), SymbolState::Resolved, OnCompletion);
|
2018-08-29 05:18:05 +08:00
|
|
|
auto Unresolved =
|
|
|
|
Resolver->lookup(std::move(Q), SymbolNameSet({Foo, Bar, Baz}));
|
2018-07-21 02:31:50 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(Unresolved.size(), 1U) << "Expected one unresolved symbol";
|
|
|
|
EXPECT_EQ(Unresolved.count(Baz), 1U) << "Expected baz to not be resolved";
|
2019-06-08 03:33:51 +08:00
|
|
|
EXPECT_TRUE(OnCompletionRun) << "OnCompletion was never run";
|
2018-07-21 02:31:50 +08:00
|
|
|
}
|
|
|
|
|
2018-08-29 05:18:05 +08:00
|
|
|
TEST_F(LegacyAPIsStandardTest, LegacyLookupHelpersFn) {
|
2018-01-25 07:09:07 +08:00
|
|
|
bool BarMaterialized = false;
|
2018-09-02 09:28:26 +08:00
|
|
|
BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak);
|
2018-01-25 07:09:07 +08:00
|
|
|
|
|
|
|
auto LegacyLookup = [&](const std::string &Name) -> JITSymbol {
|
|
|
|
if (Name == "foo")
|
2018-08-29 05:18:05 +08:00
|
|
|
return FooSym;
|
2018-01-25 07:09:07 +08:00
|
|
|
|
|
|
|
if (Name == "bar") {
|
|
|
|
auto BarMaterializer = [&]() -> Expected<JITTargetAddress> {
|
|
|
|
BarMaterialized = true;
|
|
|
|
return BarAddr;
|
|
|
|
};
|
|
|
|
|
2018-08-29 05:18:05 +08:00
|
|
|
return {BarMaterializer, BarSym.getFlags()};
|
2018-01-25 07:09:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
2018-08-29 05:18:05 +08:00
|
|
|
auto RS =
|
|
|
|
getResponsibilitySetWithLegacyFn(SymbolNameSet({Bar, Baz}), LegacyLookup);
|
2018-07-21 02:31:52 +08:00
|
|
|
|
2018-08-29 05:18:05 +08:00
|
|
|
EXPECT_TRUE(!!RS) << "Expected getResponsibilitySetWithLegacyFn to succeed";
|
|
|
|
EXPECT_EQ(RS->size(), 1U) << "Wrong number of symbols returned";
|
|
|
|
EXPECT_EQ(RS->count(Bar), 1U) << "Incorrect responsibility set returned";
|
2018-01-25 07:09:07 +08:00
|
|
|
EXPECT_FALSE(BarMaterialized)
|
|
|
|
<< "lookupFlags should not have materialized bar";
|
|
|
|
|
2019-06-08 03:33:51 +08:00
|
|
|
bool OnCompletionRun = false;
|
|
|
|
auto OnCompletion = [&](Expected<SymbolMap> Result) {
|
|
|
|
OnCompletionRun = true;
|
2018-07-21 06:22:19 +08:00
|
|
|
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";
|
2018-08-29 05:18:05 +08:00
|
|
|
EXPECT_EQ((*Result)[Foo].getFlags(), FooSym.getFlags())
|
|
|
|
<< "Wrong flags for foo";
|
2018-07-21 06:22:19 +08:00
|
|
|
EXPECT_EQ((*Result)[Bar].getAddress(), BarAddr) << "Wrong address for bar";
|
2018-08-29 05:18:05 +08:00
|
|
|
EXPECT_EQ((*Result)[Bar].getFlags(), BarSym.getFlags())
|
|
|
|
<< "Wrong flags for bar";
|
2018-07-21 06:22:19 +08:00
|
|
|
};
|
2018-01-25 07:09:07 +08:00
|
|
|
|
2019-11-26 13:57:27 +08:00
|
|
|
AsynchronousSymbolQuery Q(SymbolLookupSet({Foo, Bar}), SymbolState::Resolved,
|
|
|
|
OnCompletion);
|
2018-08-29 05:18:05 +08:00
|
|
|
auto Unresolved =
|
|
|
|
lookupWithLegacyFn(ES, Q, SymbolNameSet({Foo, Bar, Baz}), LegacyLookup);
|
2018-01-25 07:09:07 +08:00
|
|
|
|
2019-06-08 03:33:51 +08:00
|
|
|
EXPECT_TRUE(OnCompletionRun) << "OnCompletion was not run";
|
2018-01-25 07:09:07 +08:00
|
|
|
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
|