1 //===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "OrcTestCommon.h"
10 #include "llvm/ExecutionEngine/Orc/Legacy.h"
11 #include "gtest/gtest.h"
14 using namespace llvm::orc
;
16 class LegacyAPIsStandardTest
: public CoreAPIsBasedStandardTest
{};
20 TEST_F(LegacyAPIsStandardTest
, TestLambdaSymbolResolver
) {
21 BarSym
.setFlags(BarSym
.getFlags() | JITSymbolFlags::Weak
);
23 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}, {Bar
, BarSym
}})));
25 auto Resolver
= createSymbolResolver(
26 [&](const SymbolNameSet
&Symbols
) {
27 auto FlagsMap
= cantFail(JD
.lookupFlags(Symbols
));
29 for (auto &KV
: FlagsMap
)
30 if (!KV
.second
.isStrong())
31 Result
.insert(KV
.first
);
34 [&](std::shared_ptr
<AsynchronousSymbolQuery
> Q
, SymbolNameSet Symbols
) {
35 return cantFail(JD
.legacyLookup(std::move(Q
), Symbols
));
38 auto RS
= Resolver
->getResponsibilitySet(SymbolNameSet({Bar
, Baz
}));
40 EXPECT_EQ(RS
.size(), 1U)
41 << "getResponsibilitySet returned the wrong number of results";
42 EXPECT_EQ(RS
.count(Bar
), 1U)
43 << "getResponsibilitySet result incorrect. Should be {'bar'}";
45 bool OnCompletionRun
= false;
47 auto OnCompletion
= [&](Expected
<SymbolMap
> Result
) {
48 OnCompletionRun
= true;
49 EXPECT_TRUE(!!Result
) << "Unexpected error";
50 EXPECT_EQ(Result
->size(), 2U) << "Unexpected number of resolved symbols";
51 EXPECT_EQ(Result
->count(Foo
), 1U) << "Missing lookup result for foo";
52 EXPECT_EQ(Result
->count(Bar
), 1U) << "Missing lookup result for bar";
53 EXPECT_EQ((*Result
)[Foo
].getAddress(), FooSym
.getAddress())
54 << "Incorrect address for foo";
55 EXPECT_EQ((*Result
)[Bar
].getAddress(), BarSym
.getAddress())
56 << "Incorrect address for bar";
59 auto Q
= std::make_shared
<AsynchronousSymbolQuery
>(
60 SymbolNameSet({Foo
, Bar
}), SymbolState::Resolved
, OnCompletion
);
62 Resolver
->lookup(std::move(Q
), SymbolNameSet({Foo
, Bar
, Baz
}));
64 EXPECT_EQ(Unresolved
.size(), 1U) << "Expected one unresolved symbol";
65 EXPECT_EQ(Unresolved
.count(Baz
), 1U) << "Expected baz to not be resolved";
66 EXPECT_TRUE(OnCompletionRun
) << "OnCompletion was never run";
69 TEST_F(LegacyAPIsStandardTest
, LegacyLookupHelpersFn
) {
70 bool BarMaterialized
= false;
71 BarSym
.setFlags(BarSym
.getFlags() | JITSymbolFlags::Weak
);
73 auto LegacyLookup
= [&](const std::string
&Name
) -> JITSymbol
{
78 auto BarMaterializer
= [&]() -> Expected
<JITTargetAddress
> {
79 BarMaterialized
= true;
83 return {BarMaterializer
, BarSym
.getFlags()};
90 getResponsibilitySetWithLegacyFn(SymbolNameSet({Bar
, Baz
}), LegacyLookup
);
92 EXPECT_TRUE(!!RS
) << "Expected getResponsibilitySetWithLegacyFn to succeed";
93 EXPECT_EQ(RS
->size(), 1U) << "Wrong number of symbols returned";
94 EXPECT_EQ(RS
->count(Bar
), 1U) << "Incorrect responsibility set returned";
95 EXPECT_FALSE(BarMaterialized
)
96 << "lookupFlags should not have materialized bar";
98 bool OnCompletionRun
= false;
99 auto OnCompletion
= [&](Expected
<SymbolMap
> Result
) {
100 OnCompletionRun
= true;
101 EXPECT_TRUE(!!Result
) << "lookuWithLegacy failed to resolve";
103 EXPECT_EQ(Result
->size(), 2U) << "Wrong number of symbols resolved";
104 EXPECT_EQ(Result
->count(Foo
), 1U) << "Result for foo missing";
105 EXPECT_EQ(Result
->count(Bar
), 1U) << "Result for bar missing";
106 EXPECT_EQ((*Result
)[Foo
].getAddress(), FooAddr
) << "Wrong address for foo";
107 EXPECT_EQ((*Result
)[Foo
].getFlags(), FooSym
.getFlags())
108 << "Wrong flags for foo";
109 EXPECT_EQ((*Result
)[Bar
].getAddress(), BarAddr
) << "Wrong address for bar";
110 EXPECT_EQ((*Result
)[Bar
].getFlags(), BarSym
.getFlags())
111 << "Wrong flags for bar";
114 AsynchronousSymbolQuery
Q({Foo
, Bar
}, SymbolState::Resolved
, OnCompletion
);
116 lookupWithLegacyFn(ES
, Q
, SymbolNameSet({Foo
, Bar
, Baz
}), LegacyLookup
);
118 EXPECT_TRUE(OnCompletionRun
) << "OnCompletion was not run";
119 EXPECT_EQ(Unresolved
.size(), 1U) << "Expected one unresolved symbol";
120 EXPECT_EQ(Unresolved
.count(Baz
), 1U) << "Expected baz to be unresolved";