Skip to content

Commit 13014d3

Browse files
committed
[ORC] Add a special 'main' JITDylib that is created on ExecutionSession
construction, a new convenience lookup method, and add-to layer methods. ExecutionSession now creates a special 'main' JITDylib upon construction. All subsequently created JITDylibs are added to the main JITDylib's search order by default (controlled by the AddToMainDylibSearchOrder parameter to ExecutionSession::createDylib). The main JITDylib's search order will be used in the future to properly handle cross-JITDylib weak symbols, with the first definition in this search order selected. This commit also adds a new ExecutionSession::lookup convenience method that performs a blocking lookup using the main JITDylib's search order, as this will be a very common operation for clients. Finally, new convenience overloads of IRLayer and ObjectLayer's add methods are introduced that add the given program representations to the main dylib, which is likely to be the common case. llvm-svn: 342086
1 parent 9d0f9ce commit 13014d3

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

llvm/include/llvm/ExecutionEngine/Orc/Core.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,14 +761,31 @@ class JITDylib {
761761
/// An ExecutionSession represents a running JIT program.
762762
class ExecutionSession : public ExecutionSessionBase {
763763
public:
764+
using ExecutionSessionBase::lookup;
765+
764766
/// Construct an ExecutionEngine.
765767
///
766768
/// SymbolStringPools may be shared between ExecutionSessions.
767-
ExecutionSession(std::shared_ptr<SymbolStringPool> SSP = nullptr)
768-
: ExecutionSessionBase(std::move(SSP)) {}
769+
ExecutionSession(std::shared_ptr<SymbolStringPool> SSP = nullptr);
770+
771+
/// Get the "main" JITDylib, which is created automatically on construction of
772+
/// the ExecutionSession.
773+
JITDylib &getMainJITDylib();
769774

770775
/// Add a new JITDylib to this ExecutionSession.
771-
JITDylib &createJITDylib(std::string Name);
776+
JITDylib &createJITDylib(std::string Name,
777+
bool AddToMainDylibSearchOrder = true);
778+
779+
/// Convenience version of the blocking version of lookup in
780+
/// ExecutionSessionBase. Uses the main JITDylib's search order as the lookup
781+
/// order, and registers no dependencies.
782+
Expected<SymbolMap> lookup(const SymbolNameSet &Symbols) {
783+
return getMainJITDylib().withSearchOrderDo(
784+
[&](const JITDylibList &SearchOrder) {
785+
return ExecutionSessionBase::lookup(SearchOrder, Symbols,
786+
NoDependenciesToRegister, true);
787+
});
788+
}
772789

773790
private:
774791
std::vector<std::unique_ptr<JITDylib>> JDs;

llvm/include/llvm/ExecutionEngine/Orc/Layer.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ class IRLayer {
3434
/// JITDylib.
3535
virtual Error add(JITDylib &JD, VModuleKey K, std::unique_ptr<Module> M);
3636

37+
/// Adds a MaterializationUnit representing the given IR to the main
38+
/// JITDylib.
39+
Error add(VModuleKey K, std::unique_ptr<Module> M) {
40+
return add(ES.getMainJITDylib(), K, std::move(M));
41+
}
42+
3743
/// Emit should materialize the given IR.
3844
virtual void emit(MaterializationResponsibility R, VModuleKey K,
3945
std::unique_ptr<Module> M) = 0;
@@ -97,6 +103,12 @@ class ObjectLayer {
97103
/// JITDylib.
98104
virtual Error add(JITDylib &JD, VModuleKey K, std::unique_ptr<MemoryBuffer> O);
99105

106+
/// Adds a MaterializationUnit representing the given object to the main
107+
/// JITDylib.
108+
Error add(VModuleKey K, std::unique_ptr<MemoryBuffer> O) {
109+
return add(ES.getMainJITDylib(), K, std::move(O));
110+
}
111+
100112
/// Emit should materialize the given IR.
101113
virtual void emit(MaterializationResponsibility R, VModuleKey K,
102114
std::unique_ptr<MemoryBuffer> O) = 0;

llvm/lib/ExecutionEngine/Orc/Core.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1677,10 +1677,23 @@ void JITDylib::transferEmittedNodeDependencies(
16771677
}
16781678
}
16791679

1680-
JITDylib &ExecutionSession::createJITDylib(std::string Name) {
1680+
ExecutionSession::ExecutionSession(std::shared_ptr<SymbolStringPool> SSP)
1681+
: ExecutionSessionBase(std::move(SSP)) {
1682+
// Construct the main dylib.
1683+
JDs.push_back(std::unique_ptr<JITDylib>(new JITDylib(*this, "<main>")));
1684+
}
1685+
1686+
JITDylib &ExecutionSession::getMainJITDylib() {
1687+
return runSessionLocked([this]() -> JITDylib & { return *JDs.front(); });
1688+
}
1689+
1690+
JITDylib &ExecutionSession::createJITDylib(std::string Name,
1691+
bool AddToMainDylibSearchOrder) {
16811692
return runSessionLocked([&, this]() -> JITDylib & {
16821693
JDs.push_back(
16831694
std::unique_ptr<JITDylib>(new JITDylib(*this, std::move(Name))));
1695+
if (AddToMainDylibSearchOrder)
1696+
JDs.front()->addToSearchOrder(*JDs.back());
16841697
return *JDs.back();
16851698
});
16861699
}

llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,4 +815,14 @@ TEST_F(CoreAPIsStandardTest, TestMaterializeWeakSymbol) {
815815
FooResponsibility->emit();
816816
}
817817

818+
TEST_F(CoreAPIsStandardTest, TestMainJITDylibAndDefaultLookupOrder) {
819+
cantFail(ES.getMainJITDylib().define(absoluteSymbols({{Foo, FooSym}})));
820+
auto Results = cantFail(ES.lookup({Foo}));
821+
822+
EXPECT_EQ(Results.size(), 1U) << "Incorrect number of results";
823+
EXPECT_EQ(Results.count(Foo), 1U) << "Expected result for 'Foo'";
824+
EXPECT_EQ(Results[Foo].getAddress(), FooSym.getAddress())
825+
<< "Expected result address to match Foo's address";
826+
}
827+
818828
} // namespace

llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ namespace orc {
4646
// linkage and non-hidden visibility.
4747
// (5) V -- A JITDylib associated with ES.
4848
class CoreAPIsBasedStandardTest : public testing::Test {
49-
public:
5049
protected:
51-
ExecutionSession ES;
50+
std::shared_ptr<SymbolStringPool> SSP = std::make_shared<SymbolStringPool>();
51+
ExecutionSession ES{SSP};
5252
JITDylib &JD = ES.createJITDylib("JD");
5353
SymbolStringPtr Foo = ES.getSymbolStringPool().intern("foo");
5454
SymbolStringPtr Bar = ES.getSymbolStringPool().intern("bar");

0 commit comments

Comments
 (0)