1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 04:32:44 +01:00
llvm-mirror/unittests/ExecutionEngine/Orc/LegacyAPIInteropTest.cpp
Lang Hames 6c25d206a9 [ORC] Update symbol lookup to use a single callback with a required symbol state
rather than two callbacks.

The asynchronous lookup API (which the synchronous lookup API wraps for
convenience) used to take two callbacks: OnResolved (called once all requested
symbols had an address assigned) and OnReady to be called once all requested
symbols were safe to access). This patch updates the asynchronous lookup API to
take a single 'OnComplete' callback and a required state (SymbolState) to
determine when the callback should be made. This simplifies the common use case
(where the client is interested in a specific state) and will generalize neatly
as new states are introduced to track runtime initialization of symbols.

Clients who were making use of both callbacks in a single query will now need to
issue two queries (one for SymbolState::Resolved and another for
SymbolState::Ready). Synchronous lookup API clients who were explicitly passing
the WaitOnReady argument will now need neeed to pass a SymbolState instead (for
'WaitOnReady == true' use SymbolState::Ready, for 'WaitOnReady == false' use
SymbolState::Resolved). Synchronous lookup API clients who were using default
arugment values should see no change.

llvm-svn: 362832
2019-06-07 19:33:51 +00:00

124 lines
4.5 KiB
C++

//===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "OrcTestCommon.h"
#include "llvm/ExecutionEngine/Orc/Legacy.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace llvm::orc;
class LegacyAPIsStandardTest : public CoreAPIsBasedStandardTest {};
namespace {
TEST_F(LegacyAPIsStandardTest, TestLambdaSymbolResolver) {
BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak);
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
auto Resolver = createSymbolResolver(
[&](const SymbolNameSet &Symbols) {
auto FlagsMap = cantFail(JD.lookupFlags(Symbols));
SymbolNameSet Result;
for (auto &KV : FlagsMap)
if (!KV.second.isStrong())
Result.insert(KV.first);
return Result;
},
[&](std::shared_ptr<AsynchronousSymbolQuery> Q, SymbolNameSet Symbols) {
return cantFail(JD.legacyLookup(std::move(Q), Symbols));
});
auto RS = Resolver->getResponsibilitySet(SymbolNameSet({Bar, Baz}));
EXPECT_EQ(RS.size(), 1U)
<< "getResponsibilitySet returned the wrong number of results";
EXPECT_EQ(RS.count(Bar), 1U)
<< "getResponsibilitySet result incorrect. Should be {'bar'}";
bool OnCompletionRun = false;
auto OnCompletion = [&](Expected<SymbolMap> Result) {
OnCompletionRun = 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";
};
auto Q = std::make_shared<AsynchronousSymbolQuery>(
SymbolNameSet({Foo, Bar}), SymbolState::Resolved, OnCompletion);
auto Unresolved =
Resolver->lookup(std::move(Q), SymbolNameSet({Foo, Bar, Baz}));
EXPECT_EQ(Unresolved.size(), 1U) << "Expected one unresolved symbol";
EXPECT_EQ(Unresolved.count(Baz), 1U) << "Expected baz to not be resolved";
EXPECT_TRUE(OnCompletionRun) << "OnCompletion was never run";
}
TEST_F(LegacyAPIsStandardTest, LegacyLookupHelpersFn) {
bool BarMaterialized = false;
BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak);
auto LegacyLookup = [&](const std::string &Name) -> JITSymbol {
if (Name == "foo")
return FooSym;
if (Name == "bar") {
auto BarMaterializer = [&]() -> Expected<JITTargetAddress> {
BarMaterialized = true;
return BarAddr;
};
return {BarMaterializer, BarSym.getFlags()};
}
return nullptr;
};
auto RS =
getResponsibilitySetWithLegacyFn(SymbolNameSet({Bar, Baz}), LegacyLookup);
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";
EXPECT_FALSE(BarMaterialized)
<< "lookupFlags should not have materialized bar";
bool OnCompletionRun = false;
auto OnCompletion = [&](Expected<SymbolMap> Result) {
OnCompletionRun = 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(), FooSym.getFlags())
<< "Wrong flags for foo";
EXPECT_EQ((*Result)[Bar].getAddress(), BarAddr) << "Wrong address for bar";
EXPECT_EQ((*Result)[Bar].getFlags(), BarSym.getFlags())
<< "Wrong flags for bar";
};
AsynchronousSymbolQuery Q({Foo, Bar}, SymbolState::Resolved, OnCompletion);
auto Unresolved =
lookupWithLegacyFn(ES, Q, SymbolNameSet({Foo, Bar, Baz}), LegacyLookup);
EXPECT_TRUE(OnCompletionRun) << "OnCompletion was not run";
EXPECT_EQ(Unresolved.size(), 1U) << "Expected one unresolved symbol";
EXPECT_EQ(Unresolved.count(Baz), 1U) << "Expected baz to be unresolved";
}
} // namespace