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
= 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 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 OnResolvedRun
= false;
47 auto OnResolved
= [&](Expected
<SymbolMap
> Result
) {
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";
58 auto OnReady
= [&](Error Err
) {
59 EXPECT_FALSE(!!Err
) << "Finalization should never fail in this test";
62 auto Q
= std::make_shared
<AsynchronousSymbolQuery
>(SymbolNameSet({Foo
, Bar
}),
65 Resolver
->lookup(std::move(Q
), SymbolNameSet({Foo
, Bar
, Baz
}));
67 EXPECT_EQ(Unresolved
.size(), 1U) << "Expected one unresolved symbol";
68 EXPECT_EQ(Unresolved
.count(Baz
), 1U) << "Expected baz to not be resolved";
69 EXPECT_TRUE(OnResolvedRun
) << "OnResolved was never run";
72 TEST_F(LegacyAPIsStandardTest
, LegacyLookupHelpersFn
) {
73 bool BarMaterialized
= false;
74 BarSym
.setFlags(BarSym
.getFlags() | JITSymbolFlags::Weak
);
76 auto LegacyLookup
= [&](const std::string
&Name
) -> JITSymbol
{
81 auto BarMaterializer
= [&]() -> Expected
<JITTargetAddress
> {
82 BarMaterialized
= true;
86 return {BarMaterializer
, BarSym
.getFlags()};
93 getResponsibilitySetWithLegacyFn(SymbolNameSet({Bar
, Baz
}), LegacyLookup
);
95 EXPECT_TRUE(!!RS
) << "Expected getResponsibilitySetWithLegacyFn to succeed";
96 EXPECT_EQ(RS
->size(), 1U) << "Wrong number of symbols returned";
97 EXPECT_EQ(RS
->count(Bar
), 1U) << "Incorrect responsibility set returned";
98 EXPECT_FALSE(BarMaterialized
)
99 << "lookupFlags should not have materialized bar";
101 bool OnResolvedRun
= false;
102 bool OnReadyRun
= false;
103 auto OnResolved
= [&](Expected
<SymbolMap
> Result
) {
104 OnResolvedRun
= true;
105 EXPECT_TRUE(!!Result
) << "lookuWithLegacy failed to resolve";
107 EXPECT_EQ(Result
->size(), 2U) << "Wrong number of symbols resolved";
108 EXPECT_EQ(Result
->count(Foo
), 1U) << "Result for foo missing";
109 EXPECT_EQ(Result
->count(Bar
), 1U) << "Result for bar missing";
110 EXPECT_EQ((*Result
)[Foo
].getAddress(), FooAddr
) << "Wrong address for foo";
111 EXPECT_EQ((*Result
)[Foo
].getFlags(), FooSym
.getFlags())
112 << "Wrong flags for foo";
113 EXPECT_EQ((*Result
)[Bar
].getAddress(), BarAddr
) << "Wrong address for bar";
114 EXPECT_EQ((*Result
)[Bar
].getFlags(), BarSym
.getFlags())
115 << "Wrong flags for bar";
117 auto OnReady
= [&](Error Err
) {
118 EXPECT_FALSE(!!Err
) << "Finalization unexpectedly failed";
122 AsynchronousSymbolQuery
Q({Foo
, Bar
}, OnResolved
, OnReady
);
124 lookupWithLegacyFn(ES
, Q
, SymbolNameSet({Foo
, Bar
, Baz
}), LegacyLookup
);
126 EXPECT_TRUE(OnResolvedRun
) << "OnResolved was not run";
127 EXPECT_TRUE(OnReadyRun
) << "OnReady was not run";
128 EXPECT_EQ(Unresolved
.size(), 1U) << "Expected one unresolved symbol";
129 EXPECT_EQ(Unresolved
.count(Baz
), 1U) << "Expected baz to be unresolved";