1 //===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "OrcTestCommon.h"
11 #include "llvm/ExecutionEngine/Orc/Legacy.h"
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15 using namespace llvm::orc;
16
17 class LegacyAPIsStandardTest : public CoreAPIsBasedStandardTest {};
18
19 namespace {
20
TEST_F(LegacyAPIsStandardTest,TestLambdaSymbolResolver)21 TEST_F(LegacyAPIsStandardTest, TestLambdaSymbolResolver) {
22 cantFail(V.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
23
24 auto Resolver = createSymbolResolver(
25 [&](const SymbolNameSet &Symbols) { return V.lookupFlags(Symbols); },
26 [&](std::shared_ptr<AsynchronousSymbolQuery> Q, SymbolNameSet Symbols) {
27 return V.legacyLookup(std::move(Q), Symbols);
28 });
29
30 SymbolNameSet Symbols({Foo, Bar, Baz});
31
32 SymbolFlagsMap SymbolFlags = Resolver->lookupFlags(Symbols);
33
34 EXPECT_EQ(SymbolFlags.size(), 2U)
35 << "lookupFlags returned the wrong number of results";
36 EXPECT_EQ(SymbolFlags.count(Foo), 1U) << "Missing lookupFlags result for foo";
37 EXPECT_EQ(SymbolFlags.count(Bar), 1U) << "Missing lookupFlags result for bar";
38 EXPECT_EQ(SymbolFlags[Foo], FooSym.getFlags())
39 << "Incorrect lookupFlags result for Foo";
40 EXPECT_EQ(SymbolFlags[Bar], BarSym.getFlags())
41 << "Incorrect lookupFlags result for Bar";
42
43 bool OnResolvedRun = false;
44
45 auto OnResolved = [&](Expected<SymbolMap> Result) {
46 OnResolvedRun = true;
47 EXPECT_TRUE(!!Result) << "Unexpected error";
48 EXPECT_EQ(Result->size(), 2U) << "Unexpected number of resolved symbols";
49 EXPECT_EQ(Result->count(Foo), 1U) << "Missing lookup result for foo";
50 EXPECT_EQ(Result->count(Bar), 1U) << "Missing lookup result for bar";
51 EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress())
52 << "Incorrect address for foo";
53 EXPECT_EQ((*Result)[Bar].getAddress(), BarSym.getAddress())
54 << "Incorrect address for bar";
55 };
56 auto OnReady = [&](Error Err) {
57 EXPECT_FALSE(!!Err) << "Finalization should never fail in this test";
58 };
59
60 auto Q = std::make_shared<AsynchronousSymbolQuery>(SymbolNameSet({Foo, Bar}),
61 OnResolved, OnReady);
62 auto Unresolved = Resolver->lookup(std::move(Q), Symbols);
63
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(OnResolvedRun) << "OnResolved was never run";
67 }
68
TEST(LegacyAPIInteropTest,QueryAgainstVSO)69 TEST(LegacyAPIInteropTest, QueryAgainstVSO) {
70
71 ExecutionSession ES(std::make_shared<SymbolStringPool>());
72 auto Foo = ES.getSymbolStringPool().intern("foo");
73
74 auto &V = ES.createVSO("V");
75 JITEvaluatedSymbol FooSym(0xdeadbeef, JITSymbolFlags::Exported);
76 cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
77
78 auto LookupFlags = [&](const SymbolNameSet &Names) {
79 return V.lookupFlags(Names);
80 };
81
82 auto Lookup = [&](std::shared_ptr<AsynchronousSymbolQuery> Query,
83 SymbolNameSet Symbols) {
84 return V.legacyLookup(std::move(Query), Symbols);
85 };
86
87 auto UnderlyingResolver =
88 createSymbolResolver(std::move(LookupFlags), std::move(Lookup));
89 JITSymbolResolverAdapter Resolver(ES, *UnderlyingResolver, nullptr);
90
91 JITSymbolResolver::LookupSet Names{StringRef("foo")};
92
93 auto LFR = Resolver.lookupFlags(Names);
94 EXPECT_TRUE(!!LFR) << "lookupFlags failed";
95 EXPECT_EQ(LFR->size(), 1U)
96 << "lookupFlags returned the wrong number of results";
97 EXPECT_EQ(LFR->count(*Foo), 1U)
98 << "lookupFlags did not contain a result for 'foo'";
99 EXPECT_EQ((*LFR)[*Foo], FooSym.getFlags())
100 << "lookupFlags contained the wrong result for 'foo'";
101
102 auto LR = Resolver.lookup(Names);
103 EXPECT_TRUE(!!LR) << "lookup failed";
104 EXPECT_EQ(LR->size(), 1U) << "lookup returned the wrong number of results";
105 EXPECT_EQ(LR->count(*Foo), 1U) << "lookup did not contain a result for 'foo'";
106 EXPECT_EQ((*LR)[*Foo].getFlags(), FooSym.getFlags())
107 << "lookup returned the wrong result for flags of 'foo'";
108 EXPECT_EQ((*LR)[*Foo].getAddress(), FooSym.getAddress())
109 << "lookup returned the wrong result for address of 'foo'";
110 }
111
TEST(LegacyAPIInteropTset,LegacyLookupHelpersFn)112 TEST(LegacyAPIInteropTset, LegacyLookupHelpersFn) {
113 constexpr JITTargetAddress FooAddr = 0xdeadbeef;
114 JITSymbolFlags FooFlags = JITSymbolFlags::Exported;
115
116 bool BarMaterialized = false;
117 constexpr JITTargetAddress BarAddr = 0xcafef00d;
118 JITSymbolFlags BarFlags = static_cast<JITSymbolFlags::FlagNames>(
119 JITSymbolFlags::Exported | JITSymbolFlags::Weak);
120
121 auto LegacyLookup = [&](const std::string &Name) -> JITSymbol {
122 if (Name == "foo")
123 return {FooAddr, FooFlags};
124
125 if (Name == "bar") {
126 auto BarMaterializer = [&]() -> Expected<JITTargetAddress> {
127 BarMaterialized = true;
128 return BarAddr;
129 };
130
131 return {BarMaterializer, BarFlags};
132 }
133
134 return nullptr;
135 };
136
137 ExecutionSession ES;
138 auto Foo = ES.getSymbolStringPool().intern("foo");
139 auto Bar = ES.getSymbolStringPool().intern("bar");
140 auto Baz = ES.getSymbolStringPool().intern("baz");
141
142 SymbolNameSet Symbols({Foo, Bar, Baz});
143
144 auto SymbolFlags = lookupFlagsWithLegacyFn(Symbols, LegacyLookup);
145
146 EXPECT_TRUE(!!SymbolFlags) << "Expected lookupFlagsWithLegacyFn to succeed";
147 EXPECT_EQ(SymbolFlags->size(), 2U) << "Wrong number of flags returned";
148 EXPECT_EQ(SymbolFlags->count(Foo), 1U) << "Flags for foo missing";
149 EXPECT_EQ(SymbolFlags->count(Bar), 1U) << "Flags for foo missing";
150 EXPECT_EQ((*SymbolFlags)[Foo], FooFlags) << "Wrong flags for foo";
151 EXPECT_EQ((*SymbolFlags)[Bar], BarFlags) << "Wrong flags for foo";
152 EXPECT_FALSE(BarMaterialized)
153 << "lookupFlags should not have materialized bar";
154
155 bool OnResolvedRun = false;
156 bool OnReadyRun = false;
157 auto OnResolved = [&](Expected<SymbolMap> Result) {
158 OnResolvedRun = true;
159 EXPECT_TRUE(!!Result) << "lookuWithLegacy failed to resolve";
160
161 EXPECT_EQ(Result->size(), 2U) << "Wrong number of symbols resolved";
162 EXPECT_EQ(Result->count(Foo), 1U) << "Result for foo missing";
163 EXPECT_EQ(Result->count(Bar), 1U) << "Result for bar missing";
164 EXPECT_EQ((*Result)[Foo].getAddress(), FooAddr) << "Wrong address for foo";
165 EXPECT_EQ((*Result)[Foo].getFlags(), FooFlags) << "Wrong flags for foo";
166 EXPECT_EQ((*Result)[Bar].getAddress(), BarAddr) << "Wrong address for bar";
167 EXPECT_EQ((*Result)[Bar].getFlags(), BarFlags) << "Wrong flags for bar";
168 };
169 auto OnReady = [&](Error Err) {
170 EXPECT_FALSE(!!Err) << "Finalization unexpectedly failed";
171 OnReadyRun = true;
172 };
173
174 AsynchronousSymbolQuery Q({Foo, Bar}, OnResolved, OnReady);
175 auto Unresolved = lookupWithLegacyFn(ES, Q, Symbols, LegacyLookup);
176
177 EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run";
178 EXPECT_TRUE(OnReadyRun) << "OnReady was not run";
179 EXPECT_EQ(Unresolved.size(), 1U) << "Expected one unresolved symbol";
180 EXPECT_EQ(Unresolved.count(Baz), 1U) << "Expected baz to be unresolved";
181 }
182
183 } // namespace
184