llvm-project/llvm/unittests/ExecutionEngine/Orc/LegacyAPIInteropTest.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
4.7 KiB
C++
Raw Normal View History

//===----------- 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) {
[ORC][JITLink] Add support for weak references, and improve handling of static libraries. This patch substantially updates ORCv2's lookup API in order to support weak references, and to better support static archives. Key changes: -- Each symbol being looked for is now associated with a SymbolLookupFlags value. If the associated value is SymbolLookupFlags::RequiredSymbol then the symbol must be defined in one of the JITDylibs being searched (or be able to be generated in one of these JITDylibs via an attached definition generator) or the lookup will fail with an error. If the associated value is SymbolLookupFlags::WeaklyReferencedSymbol then the symbol is permitted to be undefined, in which case it will simply not appear in the resulting SymbolMap if the rest of the lookup succeeds. Since lookup now requires these flags for each symbol, the lookup method now takes an instance of a new SymbolLookupSet type rather than a SymbolNameSet. SymbolLookupSet is a vector-backed set of (name, flags) pairs. Clients are responsible for ensuring that the set property (i.e. unique elements) holds, though this is usually simple and SymbolLookupSet provides convenience methods to support this. -- Lookups now have an associated LookupKind value, which is either LookupKind::Static or LookupKind::DLSym. Definition generators can inspect the lookup kind when determining whether or not to generate new definitions. The StaticLibraryDefinitionGenerator is updated to only pull in new objects from the archive if the lookup kind is Static. This allows lookup to be re-used to emulate dlsym for JIT'd symbols without pulling in new objects from archives (which would not happen in a normal dlsym call). -- JITLink is updated to allow externals to be assigned weak linkage, and weak externals now use the SymbolLookupFlags::WeaklyReferencedSymbol value for lookups. Unresolved weak references will be assigned the default value of zero. Since this patch was modifying the lookup API anyway, it alo replaces all of the "MatchNonExported" boolean arguments with a "JITDylibLookupFlags" enum for readability. If a JITDylib's associated value is JITDylibLookupFlags::MatchExportedSymbolsOnly then the lookup will only match against exported (non-hidden) symbols in that JITDylib. If a JITDylib's associated value is JITDylibLookupFlags::MatchAllSymbols then the lookup will match against any symbol defined in the JITDylib.
2019-11-26 13:57:27 +08:00
auto FlagsMap = cantFail(JD.lookupFlags(
LookupKind::Static, JITDylibLookupFlags::MatchExportedSymbolsOnly,
SymbolLookupSet(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>(
[ORC][JITLink] Add support for weak references, and improve handling of static libraries. This patch substantially updates ORCv2's lookup API in order to support weak references, and to better support static archives. Key changes: -- Each symbol being looked for is now associated with a SymbolLookupFlags value. If the associated value is SymbolLookupFlags::RequiredSymbol then the symbol must be defined in one of the JITDylibs being searched (or be able to be generated in one of these JITDylibs via an attached definition generator) or the lookup will fail with an error. If the associated value is SymbolLookupFlags::WeaklyReferencedSymbol then the symbol is permitted to be undefined, in which case it will simply not appear in the resulting SymbolMap if the rest of the lookup succeeds. Since lookup now requires these flags for each symbol, the lookup method now takes an instance of a new SymbolLookupSet type rather than a SymbolNameSet. SymbolLookupSet is a vector-backed set of (name, flags) pairs. Clients are responsible for ensuring that the set property (i.e. unique elements) holds, though this is usually simple and SymbolLookupSet provides convenience methods to support this. -- Lookups now have an associated LookupKind value, which is either LookupKind::Static or LookupKind::DLSym. Definition generators can inspect the lookup kind when determining whether or not to generate new definitions. The StaticLibraryDefinitionGenerator is updated to only pull in new objects from the archive if the lookup kind is Static. This allows lookup to be re-used to emulate dlsym for JIT'd symbols without pulling in new objects from archives (which would not happen in a normal dlsym call). -- JITLink is updated to allow externals to be assigned weak linkage, and weak externals now use the SymbolLookupFlags::WeaklyReferencedSymbol value for lookups. Unresolved weak references will be assigned the default value of zero. Since this patch was modifying the lookup API anyway, it alo replaces all of the "MatchNonExported" boolean arguments with a "JITDylibLookupFlags" enum for readability. If a JITDylib's associated value is JITDylibLookupFlags::MatchExportedSymbolsOnly then the lookup will only match against exported (non-hidden) symbols in that JITDylib. If a JITDylib's associated value is JITDylibLookupFlags::MatchAllSymbols then the lookup will match against any symbol defined in the JITDylib.
2019-11-26 13:57:27 +08:00
SymbolLookupSet({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 = [&](StringRef 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";
};
[ORC][JITLink] Add support for weak references, and improve handling of static libraries. This patch substantially updates ORCv2's lookup API in order to support weak references, and to better support static archives. Key changes: -- Each symbol being looked for is now associated with a SymbolLookupFlags value. If the associated value is SymbolLookupFlags::RequiredSymbol then the symbol must be defined in one of the JITDylibs being searched (or be able to be generated in one of these JITDylibs via an attached definition generator) or the lookup will fail with an error. If the associated value is SymbolLookupFlags::WeaklyReferencedSymbol then the symbol is permitted to be undefined, in which case it will simply not appear in the resulting SymbolMap if the rest of the lookup succeeds. Since lookup now requires these flags for each symbol, the lookup method now takes an instance of a new SymbolLookupSet type rather than a SymbolNameSet. SymbolLookupSet is a vector-backed set of (name, flags) pairs. Clients are responsible for ensuring that the set property (i.e. unique elements) holds, though this is usually simple and SymbolLookupSet provides convenience methods to support this. -- Lookups now have an associated LookupKind value, which is either LookupKind::Static or LookupKind::DLSym. Definition generators can inspect the lookup kind when determining whether or not to generate new definitions. The StaticLibraryDefinitionGenerator is updated to only pull in new objects from the archive if the lookup kind is Static. This allows lookup to be re-used to emulate dlsym for JIT'd symbols without pulling in new objects from archives (which would not happen in a normal dlsym call). -- JITLink is updated to allow externals to be assigned weak linkage, and weak externals now use the SymbolLookupFlags::WeaklyReferencedSymbol value for lookups. Unresolved weak references will be assigned the default value of zero. Since this patch was modifying the lookup API anyway, it alo replaces all of the "MatchNonExported" boolean arguments with a "JITDylibLookupFlags" enum for readability. If a JITDylib's associated value is JITDylibLookupFlags::MatchExportedSymbolsOnly then the lookup will only match against exported (non-hidden) symbols in that JITDylib. If a JITDylib's associated value is JITDylibLookupFlags::MatchAllSymbols then the lookup will match against any symbol defined in the JITDylib.
2019-11-26 13:57:27 +08:00
AsynchronousSymbolQuery Q(SymbolLookupSet({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