2018-01-10 01:09:38 +01:00
|
|
|
//===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// 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
|
2018-01-10 01:09:38 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "OrcTestCommon.h"
|
2018-04-30 16:59:11 +02:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2018-01-10 01:09:38 +01:00
|
|
|
#include "llvm/ExecutionEngine/Orc/Core.h"
|
2021-01-19 05:58:42 +01:00
|
|
|
#include "llvm/ExecutionEngine/Orc/Shared/OrcError.h"
|
2019-04-26 01:31:33 +02:00
|
|
|
#include "llvm/Testing/Support/Error.h"
|
2018-01-10 01:09:38 +01:00
|
|
|
|
|
|
|
#include <set>
|
2018-03-14 05:18:04 +01:00
|
|
|
#include <thread>
|
2018-01-10 01:09:38 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::orc;
|
|
|
|
|
2018-07-20 20:31:50 +02:00
|
|
|
class CoreAPIsStandardTest : public CoreAPIsBasedStandardTest {};
|
|
|
|
|
2018-01-10 01:09:38 +01:00
|
|
|
namespace {
|
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, BasicSuccessfulLookup) {
|
2019-06-07 21:33:51 +02:00
|
|
|
bool OnCompletionRun = false;
|
2018-07-21 00:22:19 +02:00
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
auto OnCompletion = [&](Expected<SymbolMap> Result) {
|
2018-07-21 00:22:19 +02:00
|
|
|
EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";
|
|
|
|
auto &Resolved = *Result;
|
|
|
|
auto I = Resolved.find(Foo);
|
|
|
|
EXPECT_NE(I, Resolved.end()) << "Could not find symbol definition";
|
|
|
|
EXPECT_EQ(I->second.getAddress(), FooAddr)
|
|
|
|
<< "Resolution returned incorrect result";
|
2019-06-07 21:33:51 +02:00
|
|
|
OnCompletionRun = true;
|
2018-01-10 01:09:38 +01:00
|
|
|
};
|
|
|
|
|
2020-09-11 18:23:14 +02:00
|
|
|
std::unique_ptr<MaterializationResponsibility> FooMR;
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(
|
2018-07-21 00:22:19 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
FooMR = std::move(R);
|
2018-07-21 00:22:19 +02:00
|
|
|
})));
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Foo), SymbolState::Ready, OnCompletion,
|
|
|
|
NoDependenciesToRegister);
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
EXPECT_FALSE(OnCompletionRun) << "Should not have been resolved yet";
|
2018-07-20 20:31:53 +02:00
|
|
|
|
2019-08-23 22:37:31 +02:00
|
|
|
cantFail(FooMR->notifyResolved({{Foo, FooSym}}));
|
2018-07-20 20:31:53 +02:00
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
EXPECT_FALSE(OnCompletionRun) << "Should not be ready yet";
|
2018-07-21 00:22:19 +02:00
|
|
|
|
2019-08-23 22:37:31 +02:00
|
|
|
cantFail(FooMR->notifyEmitted());
|
2018-07-21 00:22:19 +02:00
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
EXPECT_TRUE(OnCompletionRun) << "Should have been marked ready";
|
2018-01-10 01:09:38 +01:00
|
|
|
}
|
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, EmptyLookup) {
|
2019-06-07 21:33:51 +02:00
|
|
|
bool OnCompletionRun = false;
|
2018-01-10 01:09:38 +01:00
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
auto OnCompletion = [&](Expected<SymbolMap> Result) {
|
2018-07-21 00:22:19 +02:00
|
|
|
cantFail(std::move(Result));
|
2019-06-07 21:33:51 +02:00
|
|
|
OnCompletionRun = true;
|
2018-01-10 01:09:38 +01:00
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(),
|
|
|
|
SymbolState::Ready, OnCompletion, NoDependenciesToRegister);
|
2018-06-17 20:59:01 +02:00
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
EXPECT_TRUE(OnCompletionRun) << "OnCompletion was not run for empty query";
|
2018-06-17 20:59:01 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:06:13 +01:00
|
|
|
TEST_F(CoreAPIsStandardTest, ResolveUnrequestedSymbol) {
|
|
|
|
// Test that all symbols in a MaterializationUnit materialize corretly when
|
|
|
|
// only a subset of symbols is looked up.
|
|
|
|
// The aim here is to ensure that we're not relying on the query to set up
|
|
|
|
// state needed to materialize the unrequested symbols.
|
|
|
|
|
|
|
|
cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[this](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
cantFail(R->notifyResolved({{Foo, FooSym}, {Bar, BarSym}}));
|
|
|
|
cantFail(R->notifyEmitted());
|
2020-03-26 21:06:13 +01:00
|
|
|
})));
|
|
|
|
|
|
|
|
auto Result =
|
|
|
|
cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo})));
|
|
|
|
EXPECT_EQ(Result.size(), 1U) << "Unexpected number of results";
|
|
|
|
EXPECT_TRUE(Result.count(Foo)) << "Expected result for \"Foo\"";
|
|
|
|
}
|
|
|
|
|
2020-09-01 21:10:23 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, MaterializationSideEffctsOnlyBasic) {
|
2020-03-25 21:07:00 +01:00
|
|
|
// Test that basic materialization-side-effects-only symbols work as expected:
|
|
|
|
// that they can be emitted without being resolved, that queries for them
|
|
|
|
// don't return until they're emitted, and that they don't appear in query
|
|
|
|
// results.
|
|
|
|
|
2020-09-11 18:23:14 +02:00
|
|
|
std::unique_ptr<MaterializationResponsibility> FooR;
|
2020-03-25 21:07:00 +01:00
|
|
|
Optional<SymbolMap> Result;
|
|
|
|
|
|
|
|
cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap(
|
|
|
|
{{Foo, JITSymbolFlags::Exported |
|
|
|
|
JITSymbolFlags::MaterializationSideEffectsOnly}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
FooR = std::move(R);
|
|
|
|
})));
|
2020-03-25 21:07:00 +01:00
|
|
|
|
|
|
|
ES.lookup(
|
|
|
|
LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
2021-01-13 04:20:13 +01:00
|
|
|
SymbolLookupSet(Foo, SymbolLookupFlags::WeaklyReferencedSymbol),
|
2020-03-25 21:07:00 +01:00
|
|
|
SymbolState::Ready,
|
|
|
|
[&](Expected<SymbolMap> LookupResult) {
|
|
|
|
if (LookupResult)
|
|
|
|
Result = std::move(*LookupResult);
|
|
|
|
else
|
|
|
|
ADD_FAILURE() << "Unexpected lookup error: "
|
|
|
|
<< toString(LookupResult.takeError());
|
|
|
|
},
|
|
|
|
NoDependenciesToRegister);
|
|
|
|
|
|
|
|
EXPECT_FALSE(Result) << "Lookup returned unexpectedly";
|
|
|
|
EXPECT_TRUE(FooR) << "Lookup failed to trigger materialization";
|
|
|
|
EXPECT_THAT_ERROR(FooR->notifyEmitted(), Succeeded())
|
|
|
|
<< "Emission of materialization-side-effects-only symbol failed";
|
|
|
|
|
|
|
|
EXPECT_TRUE(Result) << "Lookup failed to return";
|
|
|
|
EXPECT_TRUE(Result->empty()) << "Lookup result contained unexpected value";
|
|
|
|
}
|
|
|
|
|
2020-09-01 21:10:23 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, MaterializationSideEffectsOnlyFailuresPersist) {
|
|
|
|
// Test that when a MaterializationSideEffectsOnly symbol is failed it
|
|
|
|
// remains in the failure state rather than vanishing.
|
|
|
|
|
|
|
|
cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap(
|
|
|
|
{{Foo, JITSymbolFlags::Exported |
|
|
|
|
JITSymbolFlags::MaterializationSideEffectsOnly}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
R->failMaterialization();
|
|
|
|
})));
|
2020-09-01 21:10:23 +02:00
|
|
|
|
|
|
|
EXPECT_THAT_EXPECTED(
|
|
|
|
ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo})),
|
|
|
|
Failed());
|
|
|
|
EXPECT_THAT_EXPECTED(
|
|
|
|
ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo})),
|
|
|
|
Failed());
|
|
|
|
}
|
|
|
|
|
2018-10-07 01:03:59 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, RemoveSymbolsTest) {
|
|
|
|
// Test that:
|
|
|
|
// (1) Missing symbols generate a SymbolsNotFound error.
|
|
|
|
// (2) Materializing symbols generate a SymbolCouldNotBeRemoved error.
|
|
|
|
// (3) Removal of unmaterialized symbols triggers discard on the
|
|
|
|
// materialization unit.
|
|
|
|
// (4) Removal of symbols destroys empty materialization units.
|
|
|
|
// (5) Removal of materialized symbols works.
|
|
|
|
|
|
|
|
// Foo will be fully materialized.
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
|
|
|
|
|
|
|
// Bar will be unmaterialized.
|
|
|
|
bool BarDiscarded = false;
|
|
|
|
bool BarMaterializerDestructed = false;
|
2019-08-15 17:54:37 +02:00
|
|
|
cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(
|
2018-10-07 01:03:59 +02:00
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[this](std::unique_ptr<MaterializationResponsibility> R) {
|
2018-10-07 01:03:59 +02:00
|
|
|
ADD_FAILURE() << "Unexpected materialization of \"Bar\"";
|
2020-09-11 18:23:14 +02:00
|
|
|
cantFail(R->notifyResolved({{Bar, BarSym}}));
|
|
|
|
cantFail(R->notifyEmitted());
|
2018-10-07 01:03:59 +02:00
|
|
|
},
|
[ORC] Add generic initializer/deinitializer support.
Initializers and deinitializers are used to implement C++ static constructors
and destructors, runtime registration for some languages (e.g. with the
Objective-C runtime for Objective-C/C++ code) and other tasks that would
typically be performed when a shared-object/dylib is loaded or unloaded by a
statically compiled program.
MCJIT and ORC have historically provided limited support for discovering and
running initializers/deinitializers by scanning the llvm.global_ctors and
llvm.global_dtors variables and recording the functions to be run. This approach
suffers from several drawbacks: (1) It only works for IR inputs, not for object
files (including cached JIT'd objects). (2) It only works for initializers
described by llvm.global_ctors and llvm.global_dtors, however not all
initializers are described in this way (Objective-C, for example, describes
initializers via specially named metadata sections). (3) To make the
initializer/deinitializer functions described by llvm.global_ctors and
llvm.global_dtors searchable they must be promoted to extern linkage, polluting
the JIT symbol table (extra care must be taken to ensure this promotion does
not result in symbol name clashes).
This patch introduces several interdependent changes to ORCv2 to support the
construction of new initialization schemes, and includes an implementation of a
backwards-compatible llvm.global_ctor/llvm.global_dtor scanning scheme, and a
MachO specific scheme that handles Objective-C runtime registration (if the
Objective-C runtime is available) enabling execution of LLVM IR compiled from
Objective-C and Swift.
The major changes included in this patch are:
(1) The MaterializationUnit and MaterializationResponsibility classes are
extended to describe an optional "initializer" symbol for the module (see the
getInitializerSymbol method on each class). The presence or absence of this
symbol indicates whether the module contains any initializers or
deinitializers. The initializer symbol otherwise behaves like any other:
searching for it triggers materialization.
(2) A new Platform interface is introduced in llvm/ExecutionEngine/Orc/Core.h
which provides the following callback interface:
- Error setupJITDylib(JITDylib &JD): Can be used to install standard symbols
in JITDylibs upon creation. E.g. __dso_handle.
- Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU): Generally
used to record initializer symbols.
- Error notifyRemoving(JITDylib &JD, VModuleKey K): Used to notify a platform
that a module is being removed.
Platform implementations can use these callbacks to track outstanding
initializers and implement a platform-specific approach for executing them. For
example, the MachOPlatform installs a plugin in the JIT linker to scan for both
__mod_inits sections (for C++ static constructors) and ObjC metadata sections.
If discovered, these are processed in the usual platform order: Objective-C
registration is carried out first, then static initializers are executed,
ensuring that calls to Objective-C from static initializers will be safe.
This patch updates LLJIT to use the new scheme for initialization. Two
LLJIT::PlatformSupport classes are implemented: A GenericIR platform and a MachO
platform. The GenericIR platform implements a modified version of the previous
llvm.global-ctor scraping scheme to provide support for Windows and
Linux. LLJIT's MachO platform uses the MachOPlatform class to provide MachO
specific initialization as described above.
Reviewers: sgraenitz, dblaikie
Subscribers: mgorny, hiraditya, mgrang, ributzka, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74300
2019-12-16 11:50:40 +01:00
|
|
|
nullptr,
|
2018-10-07 01:03:59 +02:00
|
|
|
[&](const JITDylib &JD, const SymbolStringPtr &Name) {
|
|
|
|
EXPECT_EQ(Name, Bar) << "Expected \"Bar\" to be discarded";
|
|
|
|
if (Name == Bar)
|
|
|
|
BarDiscarded = true;
|
|
|
|
},
|
|
|
|
[&]() { BarMaterializerDestructed = true; })));
|
|
|
|
|
|
|
|
// Baz will be in the materializing state initially, then
|
|
|
|
// materialized for the final removal attempt.
|
2020-09-11 18:23:14 +02:00
|
|
|
std::unique_ptr<MaterializationResponsibility> BazR;
|
2019-08-15 17:54:37 +02:00
|
|
|
cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(
|
2018-10-07 01:03:59 +02:00
|
|
|
SymbolFlagsMap({{Baz, BazSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
BazR = std::move(R);
|
|
|
|
},
|
[ORC] Add generic initializer/deinitializer support.
Initializers and deinitializers are used to implement C++ static constructors
and destructors, runtime registration for some languages (e.g. with the
Objective-C runtime for Objective-C/C++ code) and other tasks that would
typically be performed when a shared-object/dylib is loaded or unloaded by a
statically compiled program.
MCJIT and ORC have historically provided limited support for discovering and
running initializers/deinitializers by scanning the llvm.global_ctors and
llvm.global_dtors variables and recording the functions to be run. This approach
suffers from several drawbacks: (1) It only works for IR inputs, not for object
files (including cached JIT'd objects). (2) It only works for initializers
described by llvm.global_ctors and llvm.global_dtors, however not all
initializers are described in this way (Objective-C, for example, describes
initializers via specially named metadata sections). (3) To make the
initializer/deinitializer functions described by llvm.global_ctors and
llvm.global_dtors searchable they must be promoted to extern linkage, polluting
the JIT symbol table (extra care must be taken to ensure this promotion does
not result in symbol name clashes).
This patch introduces several interdependent changes to ORCv2 to support the
construction of new initialization schemes, and includes an implementation of a
backwards-compatible llvm.global_ctor/llvm.global_dtor scanning scheme, and a
MachO specific scheme that handles Objective-C runtime registration (if the
Objective-C runtime is available) enabling execution of LLVM IR compiled from
Objective-C and Swift.
The major changes included in this patch are:
(1) The MaterializationUnit and MaterializationResponsibility classes are
extended to describe an optional "initializer" symbol for the module (see the
getInitializerSymbol method on each class). The presence or absence of this
symbol indicates whether the module contains any initializers or
deinitializers. The initializer symbol otherwise behaves like any other:
searching for it triggers materialization.
(2) A new Platform interface is introduced in llvm/ExecutionEngine/Orc/Core.h
which provides the following callback interface:
- Error setupJITDylib(JITDylib &JD): Can be used to install standard symbols
in JITDylibs upon creation. E.g. __dso_handle.
- Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU): Generally
used to record initializer symbols.
- Error notifyRemoving(JITDylib &JD, VModuleKey K): Used to notify a platform
that a module is being removed.
Platform implementations can use these callbacks to track outstanding
initializers and implement a platform-specific approach for executing them. For
example, the MachOPlatform installs a plugin in the JIT linker to scan for both
__mod_inits sections (for C++ static constructors) and ObjC metadata sections.
If discovered, these are processed in the usual platform order: Objective-C
registration is carried out first, then static initializers are executed,
ensuring that calls to Objective-C from static initializers will be safe.
This patch updates LLJIT to use the new scheme for initialization. Two
LLJIT::PlatformSupport classes are implemented: A GenericIR platform and a MachO
platform. The GenericIR platform implements a modified version of the previous
llvm.global-ctor scraping scheme to provide support for Windows and
Linux. LLJIT's MachO platform uses the MachOPlatform class to provide MachO
specific initialization as described above.
Reviewers: sgraenitz, dblaikie
Subscribers: mgorny, hiraditya, mgrang, ributzka, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74300
2019-12-16 11:50:40 +01:00
|
|
|
nullptr,
|
2018-10-07 01:03:59 +02:00
|
|
|
[](const JITDylib &JD, const SymbolStringPtr &Name) {
|
|
|
|
ADD_FAILURE() << "\"Baz\" discarded unexpectedly";
|
|
|
|
})));
|
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
bool OnCompletionRun = false;
|
|
|
|
ES.lookup(
|
2019-11-26 06:57:27 +01:00
|
|
|
LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet({Foo, Baz}), SymbolState::Ready,
|
2019-06-07 21:33:51 +02:00
|
|
|
[&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(Result.takeError());
|
|
|
|
OnCompletionRun = true;
|
|
|
|
},
|
|
|
|
NoDependenciesToRegister);
|
2018-10-07 01:03:59 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
// Attempt 1: Search for a missing symbol, Qux.
|
|
|
|
auto Err = JD.remove({Foo, Bar, Baz, Qux});
|
|
|
|
EXPECT_TRUE(!!Err) << "Expected failure";
|
|
|
|
EXPECT_TRUE(Err.isA<SymbolsNotFound>())
|
|
|
|
<< "Expected a SymbolsNotFound error";
|
2018-10-07 03:08:02 +02:00
|
|
|
consumeError(std::move(Err));
|
2018-10-07 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Attempt 2: Search for a symbol that is still materializing, Baz.
|
|
|
|
auto Err = JD.remove({Foo, Bar, Baz});
|
|
|
|
EXPECT_TRUE(!!Err) << "Expected failure";
|
|
|
|
EXPECT_TRUE(Err.isA<SymbolsCouldNotBeRemoved>())
|
|
|
|
<< "Expected a SymbolsNotFound error";
|
2018-10-07 03:08:02 +02:00
|
|
|
consumeError(std::move(Err));
|
2018-10-07 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 22:37:31 +02:00
|
|
|
cantFail(BazR->notifyResolved({{Baz, BazSym}}));
|
|
|
|
cantFail(BazR->notifyEmitted());
|
2018-10-07 01:03:59 +02:00
|
|
|
{
|
|
|
|
// Attempt 3: Search now that all symbols are fully materialized
|
|
|
|
// (Foo, Baz), or not yet materialized (Bar).
|
|
|
|
auto Err = JD.remove({Foo, Bar, Baz});
|
2020-02-26 01:04:41 +01:00
|
|
|
EXPECT_FALSE(!!Err) << "Expected success";
|
2018-10-07 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_TRUE(BarDiscarded) << "\"Bar\" should have been discarded";
|
|
|
|
EXPECT_TRUE(BarMaterializerDestructed)
|
|
|
|
<< "\"Bar\"'s materializer should have been destructed";
|
2019-06-07 21:33:51 +02:00
|
|
|
EXPECT_TRUE(OnCompletionRun) << "OnCompletion should have been run";
|
2018-10-07 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
2018-10-13 23:53:40 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, LookupWithHiddenSymbols) {
|
|
|
|
auto BarHiddenFlags = BarSym.getFlags() & ~JITSymbolFlags::Exported;
|
|
|
|
auto BarHiddenSym = JITEvaluatedSymbol(BarSym.getAddress(), BarHiddenFlags);
|
|
|
|
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarHiddenSym}})));
|
|
|
|
|
[ORC] Add generic initializer/deinitializer support.
Initializers and deinitializers are used to implement C++ static constructors
and destructors, runtime registration for some languages (e.g. with the
Objective-C runtime for Objective-C/C++ code) and other tasks that would
typically be performed when a shared-object/dylib is loaded or unloaded by a
statically compiled program.
MCJIT and ORC have historically provided limited support for discovering and
running initializers/deinitializers by scanning the llvm.global_ctors and
llvm.global_dtors variables and recording the functions to be run. This approach
suffers from several drawbacks: (1) It only works for IR inputs, not for object
files (including cached JIT'd objects). (2) It only works for initializers
described by llvm.global_ctors and llvm.global_dtors, however not all
initializers are described in this way (Objective-C, for example, describes
initializers via specially named metadata sections). (3) To make the
initializer/deinitializer functions described by llvm.global_ctors and
llvm.global_dtors searchable they must be promoted to extern linkage, polluting
the JIT symbol table (extra care must be taken to ensure this promotion does
not result in symbol name clashes).
This patch introduces several interdependent changes to ORCv2 to support the
construction of new initialization schemes, and includes an implementation of a
backwards-compatible llvm.global_ctor/llvm.global_dtor scanning scheme, and a
MachO specific scheme that handles Objective-C runtime registration (if the
Objective-C runtime is available) enabling execution of LLVM IR compiled from
Objective-C and Swift.
The major changes included in this patch are:
(1) The MaterializationUnit and MaterializationResponsibility classes are
extended to describe an optional "initializer" symbol for the module (see the
getInitializerSymbol method on each class). The presence or absence of this
symbol indicates whether the module contains any initializers or
deinitializers. The initializer symbol otherwise behaves like any other:
searching for it triggers materialization.
(2) A new Platform interface is introduced in llvm/ExecutionEngine/Orc/Core.h
which provides the following callback interface:
- Error setupJITDylib(JITDylib &JD): Can be used to install standard symbols
in JITDylibs upon creation. E.g. __dso_handle.
- Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU): Generally
used to record initializer symbols.
- Error notifyRemoving(JITDylib &JD, VModuleKey K): Used to notify a platform
that a module is being removed.
Platform implementations can use these callbacks to track outstanding
initializers and implement a platform-specific approach for executing them. For
example, the MachOPlatform installs a plugin in the JIT linker to scan for both
__mod_inits sections (for C++ static constructors) and ObjC metadata sections.
If discovered, these are processed in the usual platform order: Objective-C
registration is carried out first, then static initializers are executed,
ensuring that calls to Objective-C from static initializers will be safe.
This patch updates LLJIT to use the new scheme for initialization. Two
LLJIT::PlatformSupport classes are implemented: A GenericIR platform and a MachO
platform. The GenericIR platform implements a modified version of the previous
llvm.global-ctor scraping scheme to provide support for Windows and
Linux. LLJIT's MachO platform uses the MachOPlatform class to provide MachO
specific initialization as described above.
Reviewers: sgraenitz, dblaikie
Subscribers: mgorny, hiraditya, mgrang, ributzka, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74300
2019-12-16 11:50:40 +01:00
|
|
|
auto &JD2 = ES.createBareJITDylib("JD2");
|
2018-10-13 23:53:40 +02:00
|
|
|
cantFail(JD2.define(absoluteSymbols({{Bar, QuxSym}})));
|
|
|
|
|
2018-10-24 01:01:39 +02:00
|
|
|
/// Try a blocking lookup.
|
2019-11-26 06:57:27 +01:00
|
|
|
auto Result = cantFail(ES.lookup(makeJITDylibSearchOrder({&JD, &JD2}),
|
|
|
|
SymbolLookupSet({Foo, Bar})));
|
2018-10-13 23:53:40 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(Result.size(), 2U) << "Unexpected number of results";
|
|
|
|
EXPECT_EQ(Result.count(Foo), 1U) << "Missing result for \"Foo\"";
|
|
|
|
EXPECT_EQ(Result.count(Bar), 1U) << "Missing result for \"Bar\"";
|
|
|
|
EXPECT_EQ(Result[Bar].getAddress(), QuxSym.getAddress())
|
|
|
|
<< "Wrong result for \"Bar\"";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, LookupFlagsTest) {
|
2018-01-21 04:20:39 +01:00
|
|
|
// Test that lookupFlags works on a predefined symbol, and does not trigger
|
2018-07-11 05:09:36 +02:00
|
|
|
// materialization of a lazy symbol. Make the lazy symbol weak to test that
|
|
|
|
// the weak flag is propagated correctly.
|
2018-01-21 04:20:39 +01:00
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
BarSym.setFlags(static_cast<JITSymbolFlags::FlagNames>(
|
|
|
|
JITSymbolFlags::Exported | JITSymbolFlags::Weak));
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[](std::unique_ptr<MaterializationResponsibility> R) {
|
2018-01-21 04:20:39 +01:00
|
|
|
llvm_unreachable("Symbol materialized on flags lookup");
|
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
|
|
|
cantFail(JD.define(std::move(MU)));
|
2018-01-21 04:20:39 +01:00
|
|
|
|
2020-10-14 02:24:18 +02:00
|
|
|
auto SymbolFlags = cantFail(ES.lookupFlags(
|
|
|
|
LookupKind::Static,
|
|
|
|
{{&JD, JITDylibLookupFlags::MatchExportedSymbolsOnly}},
|
|
|
|
SymbolLookupSet({Foo, Bar, Baz},
|
|
|
|
SymbolLookupFlags::WeaklyReferencedSymbol)));
|
2018-01-21 04:20:39 +01:00
|
|
|
|
2018-01-25 02:43:00 +01:00
|
|
|
EXPECT_EQ(SymbolFlags.size(), 2U)
|
2018-01-21 04:20:39 +01:00
|
|
|
<< "Returned symbol flags contains unexpected results";
|
2018-01-25 02:43:00 +01:00
|
|
|
EXPECT_EQ(SymbolFlags.count(Foo), 1U) << "Missing lookupFlags result for Foo";
|
2018-07-11 05:09:36 +02:00
|
|
|
EXPECT_EQ(SymbolFlags[Foo], FooSym.getFlags())
|
|
|
|
<< "Incorrect flags returned for Foo";
|
2018-01-25 02:43:00 +01:00
|
|
|
EXPECT_EQ(SymbolFlags.count(Bar), 1U)
|
2018-01-21 04:20:39 +01:00
|
|
|
<< "Missing lookupFlags result for Bar";
|
2018-07-11 05:09:36 +02:00
|
|
|
EXPECT_EQ(SymbolFlags[Bar], BarSym.getFlags())
|
|
|
|
<< "Incorrect flags returned for Bar";
|
2018-01-21 04:20:39 +01:00
|
|
|
}
|
|
|
|
|
2019-04-30 02:03:26 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, LookupWithGeneratorFailure) {
|
|
|
|
|
2020-10-01 23:40:08 +02:00
|
|
|
class BadGenerator : public DefinitionGenerator {
|
2019-04-30 02:03:26 +02:00
|
|
|
public:
|
2020-10-14 02:24:18 +02:00
|
|
|
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &,
|
|
|
|
JITDylibLookupFlags, const SymbolLookupSet &) override {
|
2019-04-30 02:03:26 +02:00
|
|
|
return make_error<StringError>("BadGenerator", inconvertibleErrorCode());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
JD.addGenerator(std::make_unique<BadGenerator>());
|
2019-04-30 02:03:26 +02:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
EXPECT_THAT_ERROR(
|
2020-10-14 02:24:18 +02:00
|
|
|
ES.lookupFlags(LookupKind::Static,
|
|
|
|
{{&JD, JITDylibLookupFlags::MatchExportedSymbolsOnly}},
|
2019-11-26 06:57:27 +01:00
|
|
|
SymbolLookupSet(Foo))
|
|
|
|
.takeError(),
|
|
|
|
Failed<StringError>())
|
2019-04-30 02:03:26 +02:00
|
|
|
<< "Generator failure did not propagate through lookupFlags";
|
|
|
|
|
|
|
|
EXPECT_THAT_ERROR(
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo)).takeError(),
|
2019-04-30 02:03:26 +02:00
|
|
|
Failed<StringError>())
|
|
|
|
<< "Generator failure did not propagate through lookup";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestBasicAliases) {
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
|
|
|
|
cantFail(JD.define(symbolAliases({{Baz, {Foo, JITSymbolFlags::Exported}},
|
|
|
|
{Qux, {Bar, JITSymbolFlags::Weak}}})));
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Qux, QuxSym}})));
|
2018-06-26 03:22:29 +02:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto Result =
|
|
|
|
ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Baz, Qux}));
|
2018-06-26 03:22:29 +02:00
|
|
|
EXPECT_TRUE(!!Result) << "Unexpected lookup failure";
|
|
|
|
EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";
|
|
|
|
EXPECT_EQ(Result->count(Qux), 1U) << "No result for \"qux\"";
|
|
|
|
EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())
|
|
|
|
<< "\"Baz\"'s address should match \"Foo\"'s";
|
|
|
|
EXPECT_EQ((*Result)[Qux].getAddress(), QuxSym.getAddress())
|
|
|
|
<< "The \"Qux\" alias should have been overriden";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestChainedAliases) {
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
|
|
|
cantFail(JD.define(symbolAliases(
|
2018-07-11 05:09:36 +02:00
|
|
|
{{Baz, {Bar, BazSym.getFlags()}}, {Bar, {Foo, BarSym.getFlags()}}})));
|
2018-07-11 01:34:56 +02:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto Result =
|
|
|
|
ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Bar, Baz}));
|
2018-07-11 01:34:56 +02:00
|
|
|
EXPECT_TRUE(!!Result) << "Unexpected lookup failure";
|
|
|
|
EXPECT_EQ(Result->count(Bar), 1U) << "No result for \"bar\"";
|
|
|
|
EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";
|
|
|
|
EXPECT_EQ((*Result)[Bar].getAddress(), FooSym.getAddress())
|
|
|
|
<< "\"Bar\"'s address should match \"Foo\"'s";
|
|
|
|
EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())
|
|
|
|
<< "\"Baz\"'s address should match \"Foo\"'s";
|
|
|
|
}
|
|
|
|
|
2018-07-21 02:12:05 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestBasicReExports) {
|
|
|
|
// Test that the basic use case of re-exporting a single symbol from another
|
2018-08-17 23:18:18 +02:00
|
|
|
// JITDylib works.
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-07-21 02:12:05 +02:00
|
|
|
|
[ORC] Add generic initializer/deinitializer support.
Initializers and deinitializers are used to implement C++ static constructors
and destructors, runtime registration for some languages (e.g. with the
Objective-C runtime for Objective-C/C++ code) and other tasks that would
typically be performed when a shared-object/dylib is loaded or unloaded by a
statically compiled program.
MCJIT and ORC have historically provided limited support for discovering and
running initializers/deinitializers by scanning the llvm.global_ctors and
llvm.global_dtors variables and recording the functions to be run. This approach
suffers from several drawbacks: (1) It only works for IR inputs, not for object
files (including cached JIT'd objects). (2) It only works for initializers
described by llvm.global_ctors and llvm.global_dtors, however not all
initializers are described in this way (Objective-C, for example, describes
initializers via specially named metadata sections). (3) To make the
initializer/deinitializer functions described by llvm.global_ctors and
llvm.global_dtors searchable they must be promoted to extern linkage, polluting
the JIT symbol table (extra care must be taken to ensure this promotion does
not result in symbol name clashes).
This patch introduces several interdependent changes to ORCv2 to support the
construction of new initialization schemes, and includes an implementation of a
backwards-compatible llvm.global_ctor/llvm.global_dtor scanning scheme, and a
MachO specific scheme that handles Objective-C runtime registration (if the
Objective-C runtime is available) enabling execution of LLVM IR compiled from
Objective-C and Swift.
The major changes included in this patch are:
(1) The MaterializationUnit and MaterializationResponsibility classes are
extended to describe an optional "initializer" symbol for the module (see the
getInitializerSymbol method on each class). The presence or absence of this
symbol indicates whether the module contains any initializers or
deinitializers. The initializer symbol otherwise behaves like any other:
searching for it triggers materialization.
(2) A new Platform interface is introduced in llvm/ExecutionEngine/Orc/Core.h
which provides the following callback interface:
- Error setupJITDylib(JITDylib &JD): Can be used to install standard symbols
in JITDylibs upon creation. E.g. __dso_handle.
- Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU): Generally
used to record initializer symbols.
- Error notifyRemoving(JITDylib &JD, VModuleKey K): Used to notify a platform
that a module is being removed.
Platform implementations can use these callbacks to track outstanding
initializers and implement a platform-specific approach for executing them. For
example, the MachOPlatform installs a plugin in the JIT linker to scan for both
__mod_inits sections (for C++ static constructors) and ObjC metadata sections.
If discovered, these are processed in the usual platform order: Objective-C
registration is carried out first, then static initializers are executed,
ensuring that calls to Objective-C from static initializers will be safe.
This patch updates LLJIT to use the new scheme for initialization. Two
LLJIT::PlatformSupport classes are implemented: A GenericIR platform and a MachO
platform. The GenericIR platform implements a modified version of the previous
llvm.global-ctor scraping scheme to provide support for Windows and
Linux. LLJIT's MachO platform uses the MachOPlatform class to provide MachO
specific initialization as described above.
Reviewers: sgraenitz, dblaikie
Subscribers: mgorny, hiraditya, mgrang, ributzka, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74300
2019-12-16 11:50:40 +01:00
|
|
|
auto &JD2 = ES.createBareJITDylib("JD2");
|
2018-07-21 02:12:05 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD2.define(reexports(JD, {{Bar, {Foo, BarSym.getFlags()}}})));
|
2018-07-21 02:12:05 +02:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto Result = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD2), Bar));
|
2018-07-21 02:12:05 +02:00
|
|
|
EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
|
|
|
|
<< "Re-export Bar for symbol Foo should match FooSym's address";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CoreAPIsStandardTest, TestThatReExportsDontUnnecessarilyMaterialize) {
|
|
|
|
// Test that re-exports do not materialize symbols that have not been queried
|
|
|
|
// for.
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-07-21 02:12:05 +02:00
|
|
|
|
|
|
|
bool BarMaterialized = false;
|
2019-08-15 17:54:37 +02:00
|
|
|
auto BarMU = std::make_unique<SimpleMaterializationUnit>(
|
2018-07-21 02:12:05 +02:00
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
2018-07-21 02:12:05 +02:00
|
|
|
BarMaterialized = true;
|
2020-09-11 18:23:14 +02:00
|
|
|
cantFail(R->notifyResolved({{Bar, BarSym}}));
|
|
|
|
cantFail(R->notifyEmitted());
|
2018-07-21 02:12:05 +02:00
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(BarMU));
|
2018-07-21 02:12:05 +02:00
|
|
|
|
[ORC] Add generic initializer/deinitializer support.
Initializers and deinitializers are used to implement C++ static constructors
and destructors, runtime registration for some languages (e.g. with the
Objective-C runtime for Objective-C/C++ code) and other tasks that would
typically be performed when a shared-object/dylib is loaded or unloaded by a
statically compiled program.
MCJIT and ORC have historically provided limited support for discovering and
running initializers/deinitializers by scanning the llvm.global_ctors and
llvm.global_dtors variables and recording the functions to be run. This approach
suffers from several drawbacks: (1) It only works for IR inputs, not for object
files (including cached JIT'd objects). (2) It only works for initializers
described by llvm.global_ctors and llvm.global_dtors, however not all
initializers are described in this way (Objective-C, for example, describes
initializers via specially named metadata sections). (3) To make the
initializer/deinitializer functions described by llvm.global_ctors and
llvm.global_dtors searchable they must be promoted to extern linkage, polluting
the JIT symbol table (extra care must be taken to ensure this promotion does
not result in symbol name clashes).
This patch introduces several interdependent changes to ORCv2 to support the
construction of new initialization schemes, and includes an implementation of a
backwards-compatible llvm.global_ctor/llvm.global_dtor scanning scheme, and a
MachO specific scheme that handles Objective-C runtime registration (if the
Objective-C runtime is available) enabling execution of LLVM IR compiled from
Objective-C and Swift.
The major changes included in this patch are:
(1) The MaterializationUnit and MaterializationResponsibility classes are
extended to describe an optional "initializer" symbol for the module (see the
getInitializerSymbol method on each class). The presence or absence of this
symbol indicates whether the module contains any initializers or
deinitializers. The initializer symbol otherwise behaves like any other:
searching for it triggers materialization.
(2) A new Platform interface is introduced in llvm/ExecutionEngine/Orc/Core.h
which provides the following callback interface:
- Error setupJITDylib(JITDylib &JD): Can be used to install standard symbols
in JITDylibs upon creation. E.g. __dso_handle.
- Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU): Generally
used to record initializer symbols.
- Error notifyRemoving(JITDylib &JD, VModuleKey K): Used to notify a platform
that a module is being removed.
Platform implementations can use these callbacks to track outstanding
initializers and implement a platform-specific approach for executing them. For
example, the MachOPlatform installs a plugin in the JIT linker to scan for both
__mod_inits sections (for C++ static constructors) and ObjC metadata sections.
If discovered, these are processed in the usual platform order: Objective-C
registration is carried out first, then static initializers are executed,
ensuring that calls to Objective-C from static initializers will be safe.
This patch updates LLJIT to use the new scheme for initialization. Two
LLJIT::PlatformSupport classes are implemented: A GenericIR platform and a MachO
platform. The GenericIR platform implements a modified version of the previous
llvm.global-ctor scraping scheme to provide support for Windows and
Linux. LLJIT's MachO platform uses the MachOPlatform class to provide MachO
specific initialization as described above.
Reviewers: sgraenitz, dblaikie
Subscribers: mgorny, hiraditya, mgrang, ributzka, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74300
2019-12-16 11:50:40 +01:00
|
|
|
auto &JD2 = ES.createBareJITDylib("JD2");
|
2018-07-21 02:12:05 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD2.define(reexports(
|
|
|
|
JD, {{Baz, {Foo, BazSym.getFlags()}}, {Qux, {Bar, QuxSym.getFlags()}}})));
|
2018-07-21 02:12:05 +02:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto Result = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD2), Baz));
|
2018-07-21 02:12:05 +02:00
|
|
|
EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
|
|
|
|
<< "Re-export Baz for symbol Foo should match FooSym's address";
|
|
|
|
|
|
|
|
EXPECT_FALSE(BarMaterialized) << "Bar should not have been materialized";
|
|
|
|
}
|
|
|
|
|
2018-10-15 07:07:54 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestReexportsGenerator) {
|
|
|
|
// Test that a re-exports generator can dynamically generate reexports.
|
2018-08-02 22:13:58 +02:00
|
|
|
|
[ORC] Add generic initializer/deinitializer support.
Initializers and deinitializers are used to implement C++ static constructors
and destructors, runtime registration for some languages (e.g. with the
Objective-C runtime for Objective-C/C++ code) and other tasks that would
typically be performed when a shared-object/dylib is loaded or unloaded by a
statically compiled program.
MCJIT and ORC have historically provided limited support for discovering and
running initializers/deinitializers by scanning the llvm.global_ctors and
llvm.global_dtors variables and recording the functions to be run. This approach
suffers from several drawbacks: (1) It only works for IR inputs, not for object
files (including cached JIT'd objects). (2) It only works for initializers
described by llvm.global_ctors and llvm.global_dtors, however not all
initializers are described in this way (Objective-C, for example, describes
initializers via specially named metadata sections). (3) To make the
initializer/deinitializer functions described by llvm.global_ctors and
llvm.global_dtors searchable they must be promoted to extern linkage, polluting
the JIT symbol table (extra care must be taken to ensure this promotion does
not result in symbol name clashes).
This patch introduces several interdependent changes to ORCv2 to support the
construction of new initialization schemes, and includes an implementation of a
backwards-compatible llvm.global_ctor/llvm.global_dtor scanning scheme, and a
MachO specific scheme that handles Objective-C runtime registration (if the
Objective-C runtime is available) enabling execution of LLVM IR compiled from
Objective-C and Swift.
The major changes included in this patch are:
(1) The MaterializationUnit and MaterializationResponsibility classes are
extended to describe an optional "initializer" symbol for the module (see the
getInitializerSymbol method on each class). The presence or absence of this
symbol indicates whether the module contains any initializers or
deinitializers. The initializer symbol otherwise behaves like any other:
searching for it triggers materialization.
(2) A new Platform interface is introduced in llvm/ExecutionEngine/Orc/Core.h
which provides the following callback interface:
- Error setupJITDylib(JITDylib &JD): Can be used to install standard symbols
in JITDylibs upon creation. E.g. __dso_handle.
- Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU): Generally
used to record initializer symbols.
- Error notifyRemoving(JITDylib &JD, VModuleKey K): Used to notify a platform
that a module is being removed.
Platform implementations can use these callbacks to track outstanding
initializers and implement a platform-specific approach for executing them. For
example, the MachOPlatform installs a plugin in the JIT linker to scan for both
__mod_inits sections (for C++ static constructors) and ObjC metadata sections.
If discovered, these are processed in the usual platform order: Objective-C
registration is carried out first, then static initializers are executed,
ensuring that calls to Objective-C from static initializers will be safe.
This patch updates LLJIT to use the new scheme for initialization. Two
LLJIT::PlatformSupport classes are implemented: A GenericIR platform and a MachO
platform. The GenericIR platform implements a modified version of the previous
llvm.global-ctor scraping scheme to provide support for Windows and
Linux. LLJIT's MachO platform uses the MachOPlatform class to provide MachO
specific initialization as described above.
Reviewers: sgraenitz, dblaikie
Subscribers: mgorny, hiraditya, mgrang, ributzka, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74300
2019-12-16 11:50:40 +01:00
|
|
|
auto &JD2 = ES.createBareJITDylib("JD2");
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD2.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
|
2018-08-02 22:13:58 +02:00
|
|
|
|
|
|
|
auto Filter = [this](SymbolStringPtr Name) { return Name != Bar; };
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
JD.addGenerator(std::make_unique<ReexportsGenerator>(
|
|
|
|
JD2, JITDylibLookupFlags::MatchExportedSymbolsOnly, Filter));
|
2018-08-02 22:13:58 +02:00
|
|
|
|
2020-10-14 02:24:18 +02:00
|
|
|
auto Flags = cantFail(ES.lookupFlags(
|
|
|
|
LookupKind::Static,
|
|
|
|
{{&JD, JITDylibLookupFlags::MatchExportedSymbolsOnly}},
|
|
|
|
SymbolLookupSet({Foo, Bar, Baz},
|
|
|
|
SymbolLookupFlags::WeaklyReferencedSymbol)));
|
2018-08-02 22:13:58 +02:00
|
|
|
EXPECT_EQ(Flags.size(), 1U) << "Unexpected number of results";
|
|
|
|
EXPECT_EQ(Flags[Foo], FooSym.getFlags()) << "Unexpected flags for Foo";
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto Result = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Foo));
|
2018-08-02 22:13:58 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
|
|
|
|
<< "Incorrect reexported symbol address";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestTrivialCircularDependency) {
|
2020-09-11 18:23:14 +02:00
|
|
|
std::unique_ptr<MaterializationResponsibility> FooR;
|
2019-08-15 17:54:37 +02:00
|
|
|
auto FooMU = std::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
FooR = std::move(R);
|
|
|
|
});
|
2018-06-17 18:59:53 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(FooMU));
|
2018-06-17 18:59:53 +02:00
|
|
|
|
|
|
|
bool FooReady = false;
|
2019-06-07 21:33:51 +02:00
|
|
|
auto OnCompletion = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
2018-07-21 00:22:19 +02:00
|
|
|
FooReady = true;
|
|
|
|
};
|
2018-06-17 18:59:53 +02:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet({Foo}), SymbolState::Ready, OnCompletion,
|
|
|
|
NoDependenciesToRegister);
|
2018-06-17 18:59:53 +02:00
|
|
|
|
2019-08-17 01:20:54 +02:00
|
|
|
FooR->addDependenciesForAll({{&JD, SymbolNameSet({Foo})}});
|
2019-08-23 22:37:31 +02:00
|
|
|
EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Succeeded())
|
|
|
|
<< "No symbols marked failed, but Foo failed to resolve";
|
|
|
|
EXPECT_THAT_ERROR(FooR->notifyEmitted(), Succeeded())
|
|
|
|
<< "No symbols marked failed, but Foo failed to emit";
|
2018-06-17 18:59:53 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(FooReady)
|
|
|
|
<< "Self-dependency prevented symbol from being marked ready";
|
|
|
|
}
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestCircularDependenceInOneJITDylib) {
|
|
|
|
// Test that a circular symbol dependency between three symbols in a JITDylib
|
|
|
|
// does not prevent any symbol from becoming 'ready' once all symbols are
|
2018-08-18 04:06:18 +02:00
|
|
|
// emitted.
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2020-09-11 18:23:14 +02:00
|
|
|
std::unique_ptr<MaterializationResponsibility> FooR;
|
|
|
|
std::unique_ptr<MaterializationResponsibility> BarR;
|
|
|
|
std::unique_ptr<MaterializationResponsibility> BazR;
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
// Create a MaterializationUnit for each symbol that moves the
|
|
|
|
// MaterializationResponsibility into one of the locals above.
|
2019-08-15 17:54:37 +02:00
|
|
|
auto FooMU = std::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
FooR = std::move(R);
|
|
|
|
});
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
auto BarMU = std::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
BarR = std::move(R);
|
|
|
|
});
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
auto BazMU = std::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Baz, BazSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
BazR = std::move(R);
|
|
|
|
});
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
// Define the symbols.
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(FooMU));
|
|
|
|
cantFail(JD.define(BarMU));
|
|
|
|
cantFail(JD.define(BazMU));
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
// Query each of the symbols to trigger materialization.
|
|
|
|
bool FooResolved = false;
|
|
|
|
bool FooReady = false;
|
2018-07-21 00:22:19 +02:00
|
|
|
|
|
|
|
auto OnFooResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
|
|
|
FooResolved = true;
|
|
|
|
};
|
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
auto OnFooReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
2018-07-21 00:22:19 +02:00
|
|
|
FooReady = true;
|
|
|
|
};
|
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
// Issue lookups for Foo. Use NoDependenciesToRegister: We're going to add
|
2018-07-21 00:22:19 +02:00
|
|
|
// the dependencies manually below.
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Foo), SymbolState::Resolved,
|
2019-06-07 21:33:51 +02:00
|
|
|
std::move(OnFooResolution), NoDependenciesToRegister);
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnFooReady),
|
|
|
|
NoDependenciesToRegister);
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
bool BarResolved = false;
|
|
|
|
bool BarReady = false;
|
2018-07-21 00:22:19 +02:00
|
|
|
auto OnBarResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
|
|
|
BarResolved = true;
|
|
|
|
};
|
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
auto OnBarReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
2018-07-21 00:22:19 +02:00
|
|
|
BarReady = true;
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Bar), SymbolState::Resolved,
|
2019-06-07 21:33:51 +02:00
|
|
|
std::move(OnBarResolution), NoDependenciesToRegister);
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnBarReady),
|
|
|
|
NoDependenciesToRegister);
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
bool BazResolved = false;
|
|
|
|
bool BazReady = false;
|
2018-07-21 00:22:19 +02:00
|
|
|
|
|
|
|
auto OnBazResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
|
|
|
BazResolved = true;
|
|
|
|
};
|
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
auto OnBazReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
2018-07-21 00:22:19 +02:00
|
|
|
BazReady = true;
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Baz), SymbolState::Resolved,
|
2019-06-07 21:33:51 +02:00
|
|
|
std::move(OnBazResolution), NoDependenciesToRegister);
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Baz), SymbolState::Ready, std::move(OnBazReady),
|
|
|
|
NoDependenciesToRegister);
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-06-14 17:32:59 +02:00
|
|
|
// Add a circular dependency: Foo -> Bar, Bar -> Baz, Baz -> Foo.
|
2018-08-17 23:18:18 +02:00
|
|
|
FooR->addDependenciesForAll({{&JD, SymbolNameSet({Bar})}});
|
|
|
|
BarR->addDependenciesForAll({{&JD, SymbolNameSet({Baz})}});
|
|
|
|
BazR->addDependenciesForAll({{&JD, SymbolNameSet({Foo})}});
|
2018-06-14 17:32:59 +02:00
|
|
|
|
|
|
|
// Add self-dependencies for good measure. This tests that the implementation
|
|
|
|
// of addDependencies filters these out.
|
2018-08-17 23:18:18 +02:00
|
|
|
FooR->addDependenciesForAll({{&JD, SymbolNameSet({Foo})}});
|
|
|
|
BarR->addDependenciesForAll({{&JD, SymbolNameSet({Bar})}});
|
|
|
|
BazR->addDependenciesForAll({{&JD, SymbolNameSet({Baz})}});
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
// Check that nothing has been resolved yet.
|
2018-05-17 00:24:30 +02:00
|
|
|
EXPECT_FALSE(FooResolved) << "\"Foo\" should not be resolved yet";
|
|
|
|
EXPECT_FALSE(BarResolved) << "\"Bar\" should not be resolved yet";
|
|
|
|
EXPECT_FALSE(BazResolved) << "\"Baz\" should not be resolved yet";
|
|
|
|
|
2018-08-18 04:06:18 +02:00
|
|
|
// Resolve the symbols (but do not emit them).
|
2019-08-23 22:37:31 +02:00
|
|
|
EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Succeeded())
|
|
|
|
<< "No symbols failed, but Foo failed to resolve";
|
|
|
|
EXPECT_THAT_ERROR(BarR->notifyResolved({{Bar, BarSym}}), Succeeded())
|
|
|
|
<< "No symbols failed, but Bar failed to resolve";
|
|
|
|
EXPECT_THAT_ERROR(BazR->notifyResolved({{Baz, BazSym}}), Succeeded())
|
|
|
|
<< "No symbols failed, but Baz failed to resolve";
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
// Verify that the symbols have been resolved, but are not ready yet.
|
2018-05-17 00:24:30 +02:00
|
|
|
EXPECT_TRUE(FooResolved) << "\"Foo\" should be resolved now";
|
|
|
|
EXPECT_TRUE(BarResolved) << "\"Bar\" should be resolved now";
|
|
|
|
EXPECT_TRUE(BazResolved) << "\"Baz\" should be resolved now";
|
|
|
|
|
|
|
|
EXPECT_FALSE(FooReady) << "\"Foo\" should not be ready yet";
|
|
|
|
EXPECT_FALSE(BarReady) << "\"Bar\" should not be ready yet";
|
|
|
|
EXPECT_FALSE(BazReady) << "\"Baz\" should not be ready yet";
|
|
|
|
|
2018-08-18 04:06:18 +02:00
|
|
|
// Emit two of the symbols.
|
2019-08-23 22:37:31 +02:00
|
|
|
EXPECT_THAT_ERROR(FooR->notifyEmitted(), Succeeded())
|
|
|
|
<< "No symbols failed, but Foo failed to emit";
|
|
|
|
EXPECT_THAT_ERROR(BarR->notifyEmitted(), Succeeded())
|
|
|
|
<< "No symbols failed, but Bar failed to emit";
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
// Verify that nothing is ready until the circular dependence is resolved.
|
|
|
|
EXPECT_FALSE(FooReady) << "\"Foo\" still should not be ready";
|
|
|
|
EXPECT_FALSE(BarReady) << "\"Bar\" still should not be ready";
|
|
|
|
EXPECT_FALSE(BazReady) << "\"Baz\" still should not be ready";
|
|
|
|
|
2018-08-18 04:06:18 +02:00
|
|
|
// Emit the last symbol.
|
2019-08-23 22:37:31 +02:00
|
|
|
EXPECT_THAT_ERROR(BazR->notifyEmitted(), Succeeded())
|
|
|
|
<< "No symbols failed, but Baz failed to emit";
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
// Verify that everything becomes ready once the circular dependence resolved.
|
|
|
|
EXPECT_TRUE(FooReady) << "\"Foo\" should be ready now";
|
|
|
|
EXPECT_TRUE(BarReady) << "\"Bar\" should be ready now";
|
|
|
|
EXPECT_TRUE(BazReady) << "\"Baz\" should be ready now";
|
|
|
|
}
|
|
|
|
|
2019-08-23 22:37:31 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, FailureInDependency) {
|
2020-09-11 18:23:14 +02:00
|
|
|
std::unique_ptr<MaterializationResponsibility> FooR;
|
|
|
|
std::unique_ptr<MaterializationResponsibility> BarR;
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
// Create a MaterializationUnit for each symbol that moves the
|
|
|
|
// MaterializationResponsibility into one of the locals above.
|
|
|
|
auto FooMU = std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
FooR = std::move(R);
|
|
|
|
});
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
auto BarMU = std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
BarR = std::move(R);
|
|
|
|
});
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
// Define the symbols.
|
|
|
|
cantFail(JD.define(FooMU));
|
|
|
|
cantFail(JD.define(BarMU));
|
|
|
|
|
|
|
|
bool OnFooReadyRun = false;
|
|
|
|
auto OnFooReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_THAT_EXPECTED(std::move(Result), Failed());
|
|
|
|
OnFooReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnFooReady),
|
|
|
|
NoDependenciesToRegister);
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
bool OnBarReadyRun = false;
|
|
|
|
auto OnBarReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_THAT_EXPECTED(std::move(Result), Failed());
|
|
|
|
OnBarReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnBarReady),
|
|
|
|
NoDependenciesToRegister);
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
// Add a dependency by Foo on Bar.
|
|
|
|
FooR->addDependenciesForAll({{&JD, SymbolNameSet({Bar})}});
|
|
|
|
|
|
|
|
// Fail bar.
|
|
|
|
BarR->failMaterialization();
|
|
|
|
|
|
|
|
// Verify that queries on Bar failed, but queries on Foo have not yet.
|
|
|
|
EXPECT_TRUE(OnBarReadyRun) << "Query for \"Bar\" was not run";
|
|
|
|
EXPECT_FALSE(OnFooReadyRun) << "Query for \"Foo\" was run unexpectedly";
|
|
|
|
|
|
|
|
// Check that we can still resolve Foo (even though it has been failed).
|
|
|
|
EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Failed())
|
|
|
|
<< "Expected resolution for \"Foo\" to fail.";
|
|
|
|
|
|
|
|
FooR->failMaterialization();
|
|
|
|
|
|
|
|
// Verify that queries on Foo have now failed.
|
|
|
|
EXPECT_TRUE(OnFooReadyRun) << "Query for \"Foo\" was not run";
|
|
|
|
|
|
|
|
// Verify that subsequent lookups on Bar and Foo fail.
|
|
|
|
EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Bar}), Failed())
|
|
|
|
<< "Lookup on failed symbol should fail";
|
|
|
|
|
|
|
|
EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Foo}), Failed())
|
|
|
|
<< "Lookup on failed symbol should fail";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CoreAPIsStandardTest, FailureInCircularDependency) {
|
2020-09-11 18:23:14 +02:00
|
|
|
std::unique_ptr<MaterializationResponsibility> FooR;
|
|
|
|
std::unique_ptr<MaterializationResponsibility> BarR;
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
// Create a MaterializationUnit for each symbol that moves the
|
|
|
|
// MaterializationResponsibility into one of the locals above.
|
|
|
|
auto FooMU = std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
FooR = std::move(R);
|
|
|
|
});
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
auto BarMU = std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
BarR = std::move(R);
|
|
|
|
});
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
// Define the symbols.
|
|
|
|
cantFail(JD.define(FooMU));
|
|
|
|
cantFail(JD.define(BarMU));
|
|
|
|
|
|
|
|
bool OnFooReadyRun = false;
|
|
|
|
auto OnFooReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_THAT_EXPECTED(std::move(Result), Failed());
|
|
|
|
OnFooReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnFooReady),
|
|
|
|
NoDependenciesToRegister);
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
bool OnBarReadyRun = false;
|
|
|
|
auto OnBarReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_THAT_EXPECTED(std::move(Result), Failed());
|
|
|
|
OnBarReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnBarReady),
|
|
|
|
NoDependenciesToRegister);
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
// Add a dependency by Foo on Bar and vice-versa.
|
|
|
|
FooR->addDependenciesForAll({{&JD, SymbolNameSet({Bar})}});
|
|
|
|
BarR->addDependenciesForAll({{&JD, SymbolNameSet({Foo})}});
|
|
|
|
|
|
|
|
// Fail bar.
|
|
|
|
BarR->failMaterialization();
|
|
|
|
|
|
|
|
// Verify that queries on Bar failed, but queries on Foo have not yet.
|
|
|
|
EXPECT_TRUE(OnBarReadyRun) << "Query for \"Bar\" was not run";
|
|
|
|
EXPECT_FALSE(OnFooReadyRun) << "Query for \"Foo\" was run unexpectedly";
|
|
|
|
|
|
|
|
// Verify that trying to resolve Foo fails.
|
|
|
|
EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Failed())
|
|
|
|
<< "Expected resolution for \"Foo\" to fail.";
|
|
|
|
|
|
|
|
FooR->failMaterialization();
|
|
|
|
|
|
|
|
// Verify that queries on Foo have now failed.
|
|
|
|
EXPECT_TRUE(OnFooReadyRun) << "Query for \"Foo\" was not run";
|
|
|
|
|
|
|
|
// Verify that subsequent lookups on Bar and Foo fail.
|
|
|
|
EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Bar}), Failed())
|
|
|
|
<< "Lookup on failed symbol should fail";
|
|
|
|
|
|
|
|
EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Foo}), Failed())
|
|
|
|
<< "Lookup on failed symbol should fail";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CoreAPIsStandardTest, AddDependencyOnFailedSymbol) {
|
2020-09-11 18:23:14 +02:00
|
|
|
std::unique_ptr<MaterializationResponsibility> FooR;
|
|
|
|
std::unique_ptr<MaterializationResponsibility> BarR;
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
// Create a MaterializationUnit for each symbol that moves the
|
|
|
|
// MaterializationResponsibility into one of the locals above.
|
|
|
|
auto FooMU = std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
FooR = std::move(R);
|
|
|
|
});
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
auto BarMU = std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
BarR = std::move(R);
|
|
|
|
});
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
// Define the symbols.
|
|
|
|
cantFail(JD.define(FooMU));
|
|
|
|
cantFail(JD.define(BarMU));
|
|
|
|
|
|
|
|
bool OnFooReadyRun = false;
|
|
|
|
auto OnFooReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_THAT_EXPECTED(std::move(Result), Failed());
|
|
|
|
OnFooReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnFooReady),
|
|
|
|
NoDependenciesToRegister);
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
bool OnBarReadyRun = false;
|
|
|
|
auto OnBarReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_THAT_EXPECTED(std::move(Result), Failed());
|
|
|
|
OnBarReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnBarReady),
|
|
|
|
NoDependenciesToRegister);
|
2019-08-23 22:37:31 +02:00
|
|
|
|
|
|
|
// Fail bar.
|
|
|
|
BarR->failMaterialization();
|
|
|
|
|
|
|
|
// We expect Bar's query to fail immediately, but Foo's query not to have run
|
|
|
|
// yet.
|
|
|
|
EXPECT_TRUE(OnBarReadyRun) << "Query for \"Bar\" was not run";
|
|
|
|
EXPECT_FALSE(OnFooReadyRun) << "Query for \"Foo\" should not have run yet";
|
|
|
|
|
|
|
|
// Add dependency of Foo on Bar.
|
|
|
|
FooR->addDependenciesForAll({{&JD, SymbolNameSet({Bar})}});
|
|
|
|
|
|
|
|
// Check that we can still resolve Foo (even though it has been failed).
|
|
|
|
EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Failed())
|
|
|
|
<< "Expected resolution for \"Foo\" to fail.";
|
|
|
|
|
|
|
|
FooR->failMaterialization();
|
|
|
|
|
|
|
|
// Foo's query should have failed before we return from addDependencies.
|
|
|
|
EXPECT_TRUE(OnFooReadyRun) << "Query for \"Foo\" was not run";
|
|
|
|
|
|
|
|
// Verify that subsequent lookups on Bar and Foo fail.
|
|
|
|
EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Bar}), Failed())
|
|
|
|
<< "Lookup on failed symbol should fail";
|
|
|
|
|
|
|
|
EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Foo}), Failed())
|
|
|
|
<< "Lookup on failed symbol should fail";
|
|
|
|
}
|
|
|
|
|
2019-08-26 23:42:51 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, FailAfterMaterialization) {
|
2020-09-11 18:23:14 +02:00
|
|
|
std::unique_ptr<MaterializationResponsibility> FooR;
|
|
|
|
std::unique_ptr<MaterializationResponsibility> BarR;
|
2019-08-26 23:42:51 +02:00
|
|
|
|
|
|
|
// Create a MaterializationUnit for each symbol that moves the
|
|
|
|
// MaterializationResponsibility into one of the locals above.
|
|
|
|
auto FooMU = std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
FooR = std::move(R);
|
|
|
|
});
|
2019-08-26 23:42:51 +02:00
|
|
|
|
|
|
|
auto BarMU = std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
BarR = std::move(R);
|
|
|
|
});
|
2019-08-26 23:42:51 +02:00
|
|
|
|
|
|
|
// Define the symbols.
|
|
|
|
cantFail(JD.define(FooMU));
|
|
|
|
cantFail(JD.define(BarMU));
|
|
|
|
|
|
|
|
bool OnFooReadyRun = false;
|
|
|
|
auto OnFooReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_THAT_EXPECTED(std::move(Result), Failed());
|
|
|
|
OnFooReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnFooReady),
|
|
|
|
NoDependenciesToRegister);
|
2019-08-26 23:42:51 +02:00
|
|
|
|
|
|
|
bool OnBarReadyRun = false;
|
|
|
|
auto OnBarReady = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_THAT_EXPECTED(std::move(Result), Failed());
|
|
|
|
OnBarReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnBarReady),
|
|
|
|
NoDependenciesToRegister);
|
2019-08-26 23:42:51 +02:00
|
|
|
|
|
|
|
// Add a dependency by Foo on Bar and vice-versa.
|
|
|
|
FooR->addDependenciesForAll({{&JD, SymbolNameSet({Bar})}});
|
|
|
|
BarR->addDependenciesForAll({{&JD, SymbolNameSet({Foo})}});
|
|
|
|
|
|
|
|
// Materialize Foo.
|
|
|
|
EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Succeeded())
|
|
|
|
<< "Expected resolution for \"Foo\" to succeed.";
|
|
|
|
EXPECT_THAT_ERROR(FooR->notifyEmitted(), Succeeded())
|
|
|
|
<< "Expected emission for \"Foo\" to succeed.";
|
|
|
|
|
|
|
|
// Fail bar.
|
|
|
|
BarR->failMaterialization();
|
|
|
|
|
|
|
|
// Verify that both queries failed.
|
|
|
|
EXPECT_TRUE(OnFooReadyRun) << "Query for Foo did not run";
|
|
|
|
EXPECT_TRUE(OnBarReadyRun) << "Query for Bar did not run";
|
|
|
|
}
|
|
|
|
|
2019-08-26 23:42:47 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, FailMaterializerWithUnqueriedSymbols) {
|
|
|
|
// Make sure that symbols with no queries aganist them still
|
|
|
|
// fail correctly.
|
|
|
|
|
|
|
|
bool MaterializerRun = false;
|
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap(
|
|
|
|
{{Foo, JITSymbolFlags::Exported}, {Bar, JITSymbolFlags::Exported}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
2019-08-26 23:42:47 +02:00
|
|
|
MaterializerRun = true;
|
2020-09-11 18:23:14 +02:00
|
|
|
R->failMaterialization();
|
2019-08-26 23:42:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
cantFail(JD.define(std::move(MU)));
|
|
|
|
|
|
|
|
// Issue a query for Foo, but not bar.
|
|
|
|
EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Foo}), Failed())
|
|
|
|
<< "Expected lookup to fail.";
|
|
|
|
|
|
|
|
// Check that the materializer (and therefore failMaterialization) ran.
|
|
|
|
EXPECT_TRUE(MaterializerRun) << "Expected materializer to have run by now";
|
|
|
|
|
|
|
|
// Check that subsequent queries against both symbols fail.
|
|
|
|
EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Foo}), Failed())
|
|
|
|
<< "Expected lookup for Foo to fail.";
|
|
|
|
EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Bar}), Failed())
|
|
|
|
<< "Expected lookup for Bar to fail.";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, DropMaterializerWhenEmpty) {
|
2018-04-12 20:35:08 +02:00
|
|
|
bool DestructorRun = false;
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
|
|
|
|
WeakExported |= JITSymbolFlags::Weak;
|
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, WeakExported}, {Bar, WeakExported}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[](std::unique_ptr<MaterializationResponsibility> R) {
|
2018-04-12 20:35:08 +02:00
|
|
|
llvm_unreachable("Unexpected call to materialize");
|
|
|
|
},
|
[ORC] Add generic initializer/deinitializer support.
Initializers and deinitializers are used to implement C++ static constructors
and destructors, runtime registration for some languages (e.g. with the
Objective-C runtime for Objective-C/C++ code) and other tasks that would
typically be performed when a shared-object/dylib is loaded or unloaded by a
statically compiled program.
MCJIT and ORC have historically provided limited support for discovering and
running initializers/deinitializers by scanning the llvm.global_ctors and
llvm.global_dtors variables and recording the functions to be run. This approach
suffers from several drawbacks: (1) It only works for IR inputs, not for object
files (including cached JIT'd objects). (2) It only works for initializers
described by llvm.global_ctors and llvm.global_dtors, however not all
initializers are described in this way (Objective-C, for example, describes
initializers via specially named metadata sections). (3) To make the
initializer/deinitializer functions described by llvm.global_ctors and
llvm.global_dtors searchable they must be promoted to extern linkage, polluting
the JIT symbol table (extra care must be taken to ensure this promotion does
not result in symbol name clashes).
This patch introduces several interdependent changes to ORCv2 to support the
construction of new initialization schemes, and includes an implementation of a
backwards-compatible llvm.global_ctor/llvm.global_dtor scanning scheme, and a
MachO specific scheme that handles Objective-C runtime registration (if the
Objective-C runtime is available) enabling execution of LLVM IR compiled from
Objective-C and Swift.
The major changes included in this patch are:
(1) The MaterializationUnit and MaterializationResponsibility classes are
extended to describe an optional "initializer" symbol for the module (see the
getInitializerSymbol method on each class). The presence or absence of this
symbol indicates whether the module contains any initializers or
deinitializers. The initializer symbol otherwise behaves like any other:
searching for it triggers materialization.
(2) A new Platform interface is introduced in llvm/ExecutionEngine/Orc/Core.h
which provides the following callback interface:
- Error setupJITDylib(JITDylib &JD): Can be used to install standard symbols
in JITDylibs upon creation. E.g. __dso_handle.
- Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU): Generally
used to record initializer symbols.
- Error notifyRemoving(JITDylib &JD, VModuleKey K): Used to notify a platform
that a module is being removed.
Platform implementations can use these callbacks to track outstanding
initializers and implement a platform-specific approach for executing them. For
example, the MachOPlatform installs a plugin in the JIT linker to scan for both
__mod_inits sections (for C++ static constructors) and ObjC metadata sections.
If discovered, these are processed in the usual platform order: Objective-C
registration is carried out first, then static initializers are executed,
ensuring that calls to Objective-C from static initializers will be safe.
This patch updates LLJIT to use the new scheme for initialization. Two
LLJIT::PlatformSupport classes are implemented: A GenericIR platform and a MachO
platform. The GenericIR platform implements a modified version of the previous
llvm.global-ctor scraping scheme to provide support for Windows and
Linux. LLJIT's MachO platform uses the MachOPlatform class to provide MachO
specific initialization as described above.
Reviewers: sgraenitz, dblaikie
Subscribers: mgorny, hiraditya, mgrang, ributzka, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74300
2019-12-16 11:50:40 +01:00
|
|
|
nullptr,
|
2018-08-17 23:18:18 +02:00
|
|
|
[&](const JITDylib &JD, SymbolStringPtr Name) {
|
2018-04-12 20:35:08 +02:00
|
|
|
EXPECT_TRUE(Name == Foo || Name == Bar)
|
|
|
|
<< "Discard of unexpected symbol?";
|
|
|
|
},
|
|
|
|
[&]() { DestructorRun = true; });
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-04-12 20:35:08 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-04-12 20:35:08 +02:00
|
|
|
|
|
|
|
EXPECT_FALSE(DestructorRun)
|
|
|
|
<< "MaterializationUnit should not have been destroyed yet";
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Bar, BarSym}})));
|
2018-04-12 20:35:08 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(DestructorRun)
|
|
|
|
<< "MaterializationUnit should have been destroyed";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, AddAndMaterializeLazySymbol) {
|
2018-01-10 01:09:38 +01:00
|
|
|
bool FooMaterialized = false;
|
|
|
|
bool BarDiscarded = false;
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
|
|
|
|
WeakExported |= JITSymbolFlags::Weak;
|
2018-01-10 01:09:38 +01:00
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}, {Bar, WeakExported}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
2018-03-20 04:49:29 +01:00
|
|
|
assert(BarDiscarded && "Bar should have been discarded by this point");
|
2020-09-11 18:23:14 +02:00
|
|
|
cantFail(R->notifyResolved(SymbolMap({{Foo, FooSym}})));
|
|
|
|
cantFail(R->notifyEmitted());
|
2018-01-10 01:09:38 +01:00
|
|
|
FooMaterialized = true;
|
|
|
|
},
|
[ORC] Add generic initializer/deinitializer support.
Initializers and deinitializers are used to implement C++ static constructors
and destructors, runtime registration for some languages (e.g. with the
Objective-C runtime for Objective-C/C++ code) and other tasks that would
typically be performed when a shared-object/dylib is loaded or unloaded by a
statically compiled program.
MCJIT and ORC have historically provided limited support for discovering and
running initializers/deinitializers by scanning the llvm.global_ctors and
llvm.global_dtors variables and recording the functions to be run. This approach
suffers from several drawbacks: (1) It only works for IR inputs, not for object
files (including cached JIT'd objects). (2) It only works for initializers
described by llvm.global_ctors and llvm.global_dtors, however not all
initializers are described in this way (Objective-C, for example, describes
initializers via specially named metadata sections). (3) To make the
initializer/deinitializer functions described by llvm.global_ctors and
llvm.global_dtors searchable they must be promoted to extern linkage, polluting
the JIT symbol table (extra care must be taken to ensure this promotion does
not result in symbol name clashes).
This patch introduces several interdependent changes to ORCv2 to support the
construction of new initialization schemes, and includes an implementation of a
backwards-compatible llvm.global_ctor/llvm.global_dtor scanning scheme, and a
MachO specific scheme that handles Objective-C runtime registration (if the
Objective-C runtime is available) enabling execution of LLVM IR compiled from
Objective-C and Swift.
The major changes included in this patch are:
(1) The MaterializationUnit and MaterializationResponsibility classes are
extended to describe an optional "initializer" symbol for the module (see the
getInitializerSymbol method on each class). The presence or absence of this
symbol indicates whether the module contains any initializers or
deinitializers. The initializer symbol otherwise behaves like any other:
searching for it triggers materialization.
(2) A new Platform interface is introduced in llvm/ExecutionEngine/Orc/Core.h
which provides the following callback interface:
- Error setupJITDylib(JITDylib &JD): Can be used to install standard symbols
in JITDylibs upon creation. E.g. __dso_handle.
- Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU): Generally
used to record initializer symbols.
- Error notifyRemoving(JITDylib &JD, VModuleKey K): Used to notify a platform
that a module is being removed.
Platform implementations can use these callbacks to track outstanding
initializers and implement a platform-specific approach for executing them. For
example, the MachOPlatform installs a plugin in the JIT linker to scan for both
__mod_inits sections (for C++ static constructors) and ObjC metadata sections.
If discovered, these are processed in the usual platform order: Objective-C
registration is carried out first, then static initializers are executed,
ensuring that calls to Objective-C from static initializers will be safe.
This patch updates LLJIT to use the new scheme for initialization. Two
LLJIT::PlatformSupport classes are implemented: A GenericIR platform and a MachO
platform. The GenericIR platform implements a modified version of the previous
llvm.global-ctor scraping scheme to provide support for Windows and
Linux. LLJIT's MachO platform uses the MachOPlatform class to provide MachO
specific initialization as described above.
Reviewers: sgraenitz, dblaikie
Subscribers: mgorny, hiraditya, mgrang, ributzka, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74300
2019-12-16 11:50:40 +01:00
|
|
|
nullptr,
|
2018-08-17 23:18:18 +02:00
|
|
|
[&](const JITDylib &JD, SymbolStringPtr Name) {
|
2018-01-10 01:09:38 +01:00
|
|
|
EXPECT_EQ(Name, Bar) << "Expected Name to be Bar";
|
|
|
|
BarDiscarded = true;
|
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Bar, BarSym}})));
|
2018-01-10 01:09:38 +01:00
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
bool OnCompletionRun = false;
|
2018-01-10 01:09:38 +01:00
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
auto OnCompletion = [&](Expected<SymbolMap> Result) {
|
2018-07-21 00:22:19 +02:00
|
|
|
EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";
|
|
|
|
auto I = Result->find(Foo);
|
|
|
|
EXPECT_NE(I, Result->end()) << "Could not find symbol definition";
|
|
|
|
EXPECT_EQ(I->second.getAddress(), FooSym.getAddress())
|
|
|
|
<< "Resolution returned incorrect result";
|
2019-06-07 21:33:51 +02:00
|
|
|
OnCompletionRun = true;
|
2018-01-10 01:09:38 +01:00
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnCompletion),
|
|
|
|
NoDependenciesToRegister);
|
2018-01-10 01:09:38 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(FooMaterialized) << "Foo was not materialized";
|
|
|
|
EXPECT_TRUE(BarDiscarded) << "Bar was not discarded";
|
2019-06-07 21:33:51 +02:00
|
|
|
EXPECT_TRUE(OnCompletionRun) << "OnResolutionCallback was not run";
|
2018-01-10 01:09:38 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 23:18:05 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestBasicWeakSymbolMaterialization) {
|
|
|
|
// Test that weak symbols are materialized correctly when we look them up.
|
2018-09-02 03:28:26 +02:00
|
|
|
BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak);
|
2018-08-28 23:18:05 +02:00
|
|
|
|
|
|
|
bool BarMaterialized = false;
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU1 = std::make_unique<SimpleMaterializationUnit>(
|
2018-08-28 23:18:05 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
cantFail(R->notifyResolved(SymbolMap({{Foo, FooSym}, {Bar, BarSym}})));
|
|
|
|
cantFail(R->notifyEmitted());
|
2018-08-28 23:18:05 +02:00
|
|
|
BarMaterialized = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
bool DuplicateBarDiscarded = false;
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU2 = std::make_unique<SimpleMaterializationUnit>(
|
2018-08-28 23:18:05 +02:00
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
2018-08-28 23:18:05 +02:00
|
|
|
ADD_FAILURE() << "Attempt to materialize Bar from the wrong unit";
|
2020-09-11 18:23:14 +02:00
|
|
|
R->failMaterialization();
|
2018-08-28 23:18:05 +02:00
|
|
|
},
|
[ORC] Add generic initializer/deinitializer support.
Initializers and deinitializers are used to implement C++ static constructors
and destructors, runtime registration for some languages (e.g. with the
Objective-C runtime for Objective-C/C++ code) and other tasks that would
typically be performed when a shared-object/dylib is loaded or unloaded by a
statically compiled program.
MCJIT and ORC have historically provided limited support for discovering and
running initializers/deinitializers by scanning the llvm.global_ctors and
llvm.global_dtors variables and recording the functions to be run. This approach
suffers from several drawbacks: (1) It only works for IR inputs, not for object
files (including cached JIT'd objects). (2) It only works for initializers
described by llvm.global_ctors and llvm.global_dtors, however not all
initializers are described in this way (Objective-C, for example, describes
initializers via specially named metadata sections). (3) To make the
initializer/deinitializer functions described by llvm.global_ctors and
llvm.global_dtors searchable they must be promoted to extern linkage, polluting
the JIT symbol table (extra care must be taken to ensure this promotion does
not result in symbol name clashes).
This patch introduces several interdependent changes to ORCv2 to support the
construction of new initialization schemes, and includes an implementation of a
backwards-compatible llvm.global_ctor/llvm.global_dtor scanning scheme, and a
MachO specific scheme that handles Objective-C runtime registration (if the
Objective-C runtime is available) enabling execution of LLVM IR compiled from
Objective-C and Swift.
The major changes included in this patch are:
(1) The MaterializationUnit and MaterializationResponsibility classes are
extended to describe an optional "initializer" symbol for the module (see the
getInitializerSymbol method on each class). The presence or absence of this
symbol indicates whether the module contains any initializers or
deinitializers. The initializer symbol otherwise behaves like any other:
searching for it triggers materialization.
(2) A new Platform interface is introduced in llvm/ExecutionEngine/Orc/Core.h
which provides the following callback interface:
- Error setupJITDylib(JITDylib &JD): Can be used to install standard symbols
in JITDylibs upon creation. E.g. __dso_handle.
- Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU): Generally
used to record initializer symbols.
- Error notifyRemoving(JITDylib &JD, VModuleKey K): Used to notify a platform
that a module is being removed.
Platform implementations can use these callbacks to track outstanding
initializers and implement a platform-specific approach for executing them. For
example, the MachOPlatform installs a plugin in the JIT linker to scan for both
__mod_inits sections (for C++ static constructors) and ObjC metadata sections.
If discovered, these are processed in the usual platform order: Objective-C
registration is carried out first, then static initializers are executed,
ensuring that calls to Objective-C from static initializers will be safe.
This patch updates LLJIT to use the new scheme for initialization. Two
LLJIT::PlatformSupport classes are implemented: A GenericIR platform and a MachO
platform. The GenericIR platform implements a modified version of the previous
llvm.global-ctor scraping scheme to provide support for Windows and
Linux. LLJIT's MachO platform uses the MachOPlatform class to provide MachO
specific initialization as described above.
Reviewers: sgraenitz, dblaikie
Subscribers: mgorny, hiraditya, mgrang, ributzka, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74300
2019-12-16 11:50:40 +01:00
|
|
|
nullptr,
|
2018-08-28 23:18:05 +02:00
|
|
|
[&](const JITDylib &JD, SymbolStringPtr Name) {
|
|
|
|
EXPECT_EQ(Name, Bar) << "Expected \"Bar\" to be discarded";
|
|
|
|
DuplicateBarDiscarded = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
cantFail(JD.define(MU1));
|
|
|
|
cantFail(JD.define(MU2));
|
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
bool OnCompletionRun = false;
|
2018-08-28 23:18:05 +02:00
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
auto OnCompletion = [&](Expected<SymbolMap> Result) {
|
2018-08-28 23:18:05 +02:00
|
|
|
cantFail(std::move(Result));
|
2019-06-07 21:33:51 +02:00
|
|
|
OnCompletionRun = true;
|
2018-08-28 23:18:05 +02:00
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnCompletion),
|
|
|
|
NoDependenciesToRegister);
|
2018-08-28 23:18:05 +02:00
|
|
|
|
2019-06-07 21:33:51 +02:00
|
|
|
EXPECT_TRUE(OnCompletionRun) << "OnCompletion not run";
|
2018-08-28 23:18:05 +02:00
|
|
|
EXPECT_TRUE(BarMaterialized) << "Bar was not materialized at all";
|
|
|
|
EXPECT_TRUE(DuplicateBarDiscarded)
|
|
|
|
<< "Duplicate bar definition not discarded";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, DefineMaterializingSymbol) {
|
2018-05-17 00:24:30 +02:00
|
|
|
bool ExpectNoMoreMaterialization = false;
|
2021-05-09 20:20:54 +02:00
|
|
|
ES.setDispatchTask([&](std::unique_ptr<Task> T) {
|
|
|
|
if (ExpectNoMoreMaterialization && isa<MaterializationTask>(*T))
|
|
|
|
ADD_FAILURE() << "Unexpected materialization";
|
|
|
|
T->run();
|
|
|
|
});
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
2018-05-17 00:24:30 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
2018-05-17 00:24:30 +02:00
|
|
|
cantFail(
|
2020-09-11 18:23:14 +02:00
|
|
|
R->defineMaterializing(SymbolFlagsMap({{Bar, BarSym.getFlags()}})));
|
|
|
|
cantFail(R->notifyResolved(SymbolMap({{Foo, FooSym}, {Bar, BarSym}})));
|
|
|
|
cantFail(R->notifyEmitted());
|
2018-05-17 00:24:30 +02:00
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2019-11-26 06:57:27 +01:00
|
|
|
cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Foo));
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
// Assert that materialization is complete by now.
|
2018-05-17 00:24:30 +02:00
|
|
|
ExpectNoMoreMaterialization = true;
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
// Look up bar to verify that no further materialization happens.
|
2019-11-26 06:57:27 +01:00
|
|
|
auto BarResult = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Bar));
|
2018-07-11 05:09:36 +02:00
|
|
|
EXPECT_EQ(BarResult.getAddress(), BarSym.getAddress())
|
|
|
|
<< "Expected Bar == BarSym";
|
2018-05-17 00:24:30 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 07:07:54 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, GeneratorTest) {
|
2020-10-14 02:24:18 +02:00
|
|
|
JITEvaluatedSymbol BazHiddenSym(
|
|
|
|
BazSym.getAddress(), BazSym.getFlags() & ~JITSymbolFlags::Exported);
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Baz, BazHiddenSym}})));
|
2018-06-12 22:43:18 +02:00
|
|
|
|
2020-10-01 23:40:08 +02:00
|
|
|
class TestGenerator : public DefinitionGenerator {
|
2019-08-13 18:05:18 +02:00
|
|
|
public:
|
|
|
|
TestGenerator(SymbolMap Symbols) : Symbols(std::move(Symbols)) {}
|
2020-10-14 02:24:18 +02:00
|
|
|
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
|
2019-11-26 06:57:27 +01:00
|
|
|
JITDylibLookupFlags JDLookupFlags,
|
2020-07-17 05:36:46 +02:00
|
|
|
const SymbolLookupSet &Names) override {
|
2019-08-13 18:05:18 +02:00
|
|
|
SymbolMap NewDefs;
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
for (const auto &KV : Names) {
|
|
|
|
const auto &Name = KV.first;
|
|
|
|
if (Symbols.count(Name))
|
2019-08-13 18:05:18 +02:00
|
|
|
NewDefs[Name] = Symbols[Name];
|
|
|
|
}
|
2019-11-26 06:57:27 +01:00
|
|
|
|
2019-08-13 18:05:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols(std::move(NewDefs))));
|
2019-11-26 06:57:27 +01:00
|
|
|
return Error::success();
|
2019-08-13 18:05:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
SymbolMap Symbols;
|
|
|
|
};
|
|
|
|
|
2020-10-14 02:24:18 +02:00
|
|
|
JD.addGenerator(std::make_unique<TestGenerator>(
|
|
|
|
SymbolMap({{Bar, BarSym}, {Baz, BazSym}})));
|
2018-06-12 22:43:18 +02:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto Result = cantFail(
|
2020-10-14 02:24:18 +02:00
|
|
|
ES.lookup(makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet({Foo, Bar})
|
|
|
|
.add(Baz, SymbolLookupFlags::WeaklyReferencedSymbol)));
|
2018-06-12 22:43:18 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(Result.count(Bar), 1U) << "Expected to find fallback def for 'bar'";
|
2018-07-11 05:09:36 +02:00
|
|
|
EXPECT_EQ(Result[Bar].getAddress(), BarSym.getAddress())
|
|
|
|
<< "Expected fallback def for Bar to be equal to BarSym";
|
2018-06-12 22:43:18 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 04:20:13 +01:00
|
|
|
TEST_F(CoreAPIsStandardTest, AsynchronousGeneratorTest) {
|
|
|
|
class TestGenerator : public DefinitionGenerator {
|
|
|
|
public:
|
|
|
|
TestGenerator(LookupState &TLS) : TLS(TLS) {}
|
|
|
|
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
|
|
|
|
JITDylibLookupFlags JDLookupFlags,
|
|
|
|
const SymbolLookupSet &Name) override {
|
|
|
|
TLS = std::move(LS);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
LookupState &TLS;
|
|
|
|
};
|
|
|
|
|
|
|
|
LookupState LS;
|
|
|
|
JD.addGenerator(std::make_unique<TestGenerator>(LS));
|
|
|
|
|
|
|
|
bool LookupCompleted = false;
|
|
|
|
|
|
|
|
ES.lookup(
|
|
|
|
LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo),
|
|
|
|
SymbolState::Ready,
|
|
|
|
[&](Expected<SymbolMap> Result) {
|
|
|
|
LookupCompleted = true;
|
|
|
|
if (!Result) {
|
|
|
|
ADD_FAILURE() << "Lookup failed unexpected";
|
|
|
|
logAllUnhandledErrors(Result.takeError(), errs(), "");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(Result->size(), 1U) << "Unexpected number of results";
|
|
|
|
EXPECT_EQ(Result->count(Foo), 1U) << "Expected result for Foo";
|
|
|
|
EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress())
|
|
|
|
<< "Bad result for Foo";
|
|
|
|
},
|
|
|
|
NoDependenciesToRegister);
|
|
|
|
|
|
|
|
EXPECT_FALSE(LookupCompleted);
|
|
|
|
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
|
|
|
|
|
|
|
LS.continueLookup(Error::success());
|
|
|
|
|
|
|
|
EXPECT_TRUE(LookupCompleted);
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, FailResolution) {
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
2018-10-13 23:53:40 +02:00
|
|
|
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported | JITSymbolFlags::Weak},
|
|
|
|
{Bar, JITSymbolFlags::Exported | JITSymbolFlags::Weak}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
R->failMaterialization();
|
2019-06-07 21:33:51 +02:00
|
|
|
});
|
2018-04-12 20:35:08 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-04-12 20:35:08 +02:00
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolNameSet Names({Foo, Bar});
|
2019-11-26 06:57:27 +01:00
|
|
|
auto Result = ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet(Names));
|
2018-07-11 05:09:36 +02:00
|
|
|
|
|
|
|
EXPECT_FALSE(!!Result) << "Expected failure";
|
|
|
|
if (!Result) {
|
2019-08-23 22:37:31 +02:00
|
|
|
handleAllErrors(
|
|
|
|
Result.takeError(),
|
|
|
|
[&](FailedToMaterialize &F) {
|
|
|
|
EXPECT_TRUE(F.getSymbols().count(&JD))
|
|
|
|
<< "Expected to fail on JITDylib JD";
|
|
|
|
EXPECT_EQ(F.getSymbols().find(&JD)->second, Names)
|
|
|
|
<< "Expected to fail on symbols in Names";
|
|
|
|
},
|
|
|
|
[](ErrorInfoBase &EIB) {
|
|
|
|
std::string ErrMsg;
|
|
|
|
{
|
|
|
|
raw_string_ostream ErrOut(ErrMsg);
|
|
|
|
EIB.log(ErrOut);
|
|
|
|
}
|
|
|
|
ADD_FAILURE() << "Expected a FailedToResolve error. Got:\n" << ErrMsg;
|
|
|
|
});
|
2018-07-11 05:09:36 +02:00
|
|
|
}
|
2018-04-12 20:35:08 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 22:37:32 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, FailEmissionAfterResolution) {
|
2019-04-26 01:31:33 +02:00
|
|
|
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Baz, BazSym}})));
|
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
2019-04-26 01:31:33 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
cantFail(R->notifyResolved(SymbolMap({{Foo, FooSym}, {Bar, BarSym}})));
|
2019-04-26 01:31:33 +02:00
|
|
|
|
|
|
|
ES.lookup(
|
2019-11-26 06:57:27 +01:00
|
|
|
LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet({Baz}), SymbolState::Resolved,
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](Expected<SymbolMap> Result) {
|
2019-04-26 01:31:33 +02:00
|
|
|
// Called when "baz" is resolved. We don't actually depend
|
|
|
|
// on or care about baz, but use it to trigger failure of
|
|
|
|
// this materialization before Baz has been finalized in
|
|
|
|
// order to test that error propagation is correct in this
|
|
|
|
// scenario.
|
|
|
|
cantFail(std::move(Result));
|
2020-09-11 18:23:14 +02:00
|
|
|
R->failMaterialization();
|
2019-04-26 01:31:33 +02:00
|
|
|
},
|
|
|
|
[&](const SymbolDependenceMap &Deps) {
|
2020-09-11 18:23:14 +02:00
|
|
|
R->addDependenciesForAll(Deps);
|
2019-04-26 01:31:33 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
cantFail(JD.define(MU));
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto Result =
|
|
|
|
ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo, Bar}));
|
2019-04-26 01:31:33 +02:00
|
|
|
|
|
|
|
EXPECT_THAT_EXPECTED(std::move(Result), Failed())
|
|
|
|
<< "Unexpected success while trying to test error propagation";
|
|
|
|
}
|
|
|
|
|
2019-08-23 22:37:32 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, FailAfterPartialResolution) {
|
|
|
|
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
|
|
|
|
|
|
|
// Fail materialization of bar.
|
|
|
|
auto BarMU = std::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
R->failMaterialization();
|
|
|
|
});
|
2019-08-23 22:37:32 +02:00
|
|
|
|
|
|
|
cantFail(JD.define(std::move(BarMU)));
|
|
|
|
|
|
|
|
bool QueryHandlerRun = false;
|
|
|
|
ES.lookup(
|
2019-11-26 06:57:27 +01:00
|
|
|
LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet({Foo, Bar}), SymbolState::Resolved,
|
2019-08-23 22:37:32 +02:00
|
|
|
[&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_THAT_EXPECTED(std::move(Result), Failed())
|
|
|
|
<< "Expected query to fail";
|
|
|
|
QueryHandlerRun = true;
|
|
|
|
},
|
|
|
|
NoDependenciesToRegister);
|
|
|
|
EXPECT_TRUE(QueryHandlerRun) << "Query handler never ran";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestLookupWithUnthreadedMaterialization) {
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
2018-05-17 00:24:30 +02:00
|
|
|
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
cantFail(R->notifyResolved({{Foo, FooSym}}));
|
|
|
|
cantFail(R->notifyEmitted());
|
2018-03-14 05:18:04 +01:00
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-03-14 05:18:04 +01:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto FooLookupResult = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Foo));
|
2018-03-14 05:18:04 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())
|
|
|
|
<< "lookup returned an incorrect address";
|
|
|
|
EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
|
|
|
|
<< "lookup returned incorrect flags";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestLookupWithThreadedMaterialization) {
|
2018-03-14 05:18:04 +01:00
|
|
|
#if LLVM_ENABLE_THREADS
|
|
|
|
|
2021-05-22 05:50:22 +02:00
|
|
|
std::mutex WorkThreadsMutex;
|
2021-05-09 20:20:54 +02:00
|
|
|
std::vector<std::thread> WorkThreads;
|
|
|
|
ES.setDispatchTask([&](std::unique_ptr<Task> T) {
|
2021-05-22 16:55:12 +02:00
|
|
|
std::promise<void> WaitP;
|
2021-05-22 05:50:22 +02:00
|
|
|
std::lock_guard<std::mutex> Lock(WorkThreadsMutex);
|
2021-05-09 20:20:54 +02:00
|
|
|
WorkThreads.push_back(
|
2021-05-22 16:55:12 +02:00
|
|
|
std::thread([T = std::move(T), WaitF = WaitP.get_future()]() mutable {
|
|
|
|
WaitF.get();
|
|
|
|
T->run();
|
|
|
|
}));
|
|
|
|
WaitP.set_value();
|
2021-05-09 20:20:54 +02:00
|
|
|
});
|
2018-03-14 05:18:04 +01:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-03-14 05:18:04 +01:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto FooLookupResult = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Foo));
|
2018-03-14 05:18:04 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())
|
|
|
|
<< "lookup returned an incorrect address";
|
|
|
|
EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
|
|
|
|
<< "lookup returned incorrect flags";
|
2021-05-09 20:20:54 +02:00
|
|
|
|
|
|
|
for (auto &WT : WorkThreads)
|
|
|
|
WT.join();
|
2018-03-14 05:18:04 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestGetRequestedSymbolsAndReplace) {
|
|
|
|
// Test that GetRequestedSymbols returns the set of symbols that currently
|
|
|
|
// have pending queries, and test that MaterializationResponsibility's
|
2018-08-17 23:18:18 +02:00
|
|
|
// replace method can be used to return definitions to the JITDylib in a new
|
2018-07-11 05:09:36 +02:00
|
|
|
// MaterializationUnit.
|
2018-05-31 21:29:03 +02:00
|
|
|
SymbolNameSet Names({Foo, Bar});
|
|
|
|
|
|
|
|
bool FooMaterialized = false;
|
|
|
|
bool BarMaterialized = false;
|
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
2018-05-31 21:29:03 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
auto Requested = R->getRequestedSymbols();
|
2018-05-31 21:29:03 +02:00
|
|
|
EXPECT_EQ(Requested.size(), 1U) << "Expected one symbol requested";
|
|
|
|
EXPECT_EQ(*Requested.begin(), Foo) << "Expected \"Foo\" requested";
|
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
auto NewMU = std::make_unique<SimpleMaterializationUnit>(
|
2018-05-31 21:29:03 +02:00
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R2) {
|
|
|
|
cantFail(R2->notifyResolved(SymbolMap({{Bar, BarSym}})));
|
|
|
|
cantFail(R2->notifyEmitted());
|
2018-05-31 21:29:03 +02:00
|
|
|
BarMaterialized = true;
|
|
|
|
});
|
|
|
|
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
cantFail(R->replace(std::move(NewMU)));
|
2018-05-31 21:29:03 +02:00
|
|
|
|
2020-09-11 18:23:14 +02:00
|
|
|
cantFail(R->notifyResolved(SymbolMap({{Foo, FooSym}})));
|
|
|
|
cantFail(R->notifyEmitted());
|
2018-05-31 21:29:03 +02:00
|
|
|
|
|
|
|
FooMaterialized = true;
|
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-05-31 21:29:03 +02:00
|
|
|
|
|
|
|
EXPECT_FALSE(FooMaterialized) << "Foo should not be materialized yet";
|
|
|
|
EXPECT_FALSE(BarMaterialized) << "Bar should not be materialized yet";
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto FooSymResult = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Foo));
|
2018-05-31 21:29:03 +02:00
|
|
|
EXPECT_EQ(FooSymResult.getAddress(), FooSym.getAddress())
|
|
|
|
<< "Address mismatch for Foo";
|
|
|
|
|
|
|
|
EXPECT_TRUE(FooMaterialized) << "Foo should be materialized now";
|
|
|
|
EXPECT_FALSE(BarMaterialized) << "Bar still should not be materialized";
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto BarSymResult = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Bar));
|
2018-05-31 21:29:03 +02:00
|
|
|
EXPECT_EQ(BarSymResult.getAddress(), BarSym.getAddress())
|
|
|
|
<< "Address mismatch for Bar";
|
|
|
|
EXPECT_TRUE(BarMaterialized) << "Bar should be materialized now";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestMaterializationResponsibilityDelegation) {
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
2018-07-09 22:54:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
auto R2 = cantFail(R->delegate({Bar}));
|
2018-07-09 22:54:36 +02:00
|
|
|
|
2020-09-11 18:23:14 +02:00
|
|
|
cantFail(R->notifyResolved({{Foo, FooSym}}));
|
|
|
|
cantFail(R->notifyEmitted());
|
|
|
|
cantFail(R2->notifyResolved({{Bar, BarSym}}));
|
|
|
|
cantFail(R2->notifyEmitted());
|
2018-07-09 22:54:36 +02:00
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-07-09 22:54:36 +02:00
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
auto Result =
|
|
|
|
ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo, Bar}));
|
2018-07-09 22:54:36 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(!!Result) << "Result should be a success value";
|
|
|
|
EXPECT_EQ(Result->count(Foo), 1U) << "\"Foo\" entry missing";
|
|
|
|
EXPECT_EQ(Result->count(Bar), 1U) << "\"Bar\" entry missing";
|
|
|
|
EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress())
|
|
|
|
<< "Address mismatch for \"Foo\"";
|
|
|
|
EXPECT_EQ((*Result)[Bar].getAddress(), BarSym.getAddress())
|
|
|
|
<< "Address mismatch for \"Bar\"";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestMaterializeWeakSymbol) {
|
2018-06-14 23:16:29 +02:00
|
|
|
// Confirm that once a weak definition is selected for materialization it is
|
|
|
|
// treated as strong.
|
2018-07-11 05:09:36 +02:00
|
|
|
JITSymbolFlags WeakExported = JITSymbolFlags::Exported;
|
|
|
|
WeakExported &= JITSymbolFlags::Weak;
|
2018-06-14 23:16:29 +02:00
|
|
|
|
2020-09-11 18:23:14 +02:00
|
|
|
std::unique_ptr<MaterializationResponsibility> FooR;
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU = std::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[&](std::unique_ptr<MaterializationResponsibility> R) {
|
|
|
|
FooR = std::move(R);
|
2018-06-14 23:16:29 +02:00
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2019-06-07 21:33:51 +02:00
|
|
|
auto OnCompletion = [](Expected<SymbolMap> Result) {
|
2018-07-21 00:22:19 +02:00
|
|
|
cantFail(std::move(Result));
|
|
|
|
};
|
|
|
|
|
2019-11-26 06:57:27 +01:00
|
|
|
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
|
|
|
|
SymbolLookupSet({Foo}), SymbolState::Ready, std::move(OnCompletion),
|
|
|
|
NoDependenciesToRegister);
|
2018-06-14 23:16:29 +02:00
|
|
|
|
2019-08-15 17:54:37 +02:00
|
|
|
auto MU2 = std::make_unique<SimpleMaterializationUnit>(
|
2018-06-14 23:16:29 +02:00
|
|
|
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
|
2020-09-11 18:23:14 +02:00
|
|
|
[](std::unique_ptr<MaterializationResponsibility> R) {
|
2018-06-14 23:16:29 +02:00
|
|
|
llvm_unreachable("This unit should never be materialized");
|
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
auto Err = JD.define(MU2);
|
2018-06-14 23:16:29 +02:00
|
|
|
EXPECT_TRUE(!!Err) << "Expected failure value";
|
|
|
|
EXPECT_TRUE(Err.isA<DuplicateDefinition>())
|
|
|
|
<< "Expected a duplicate definition error";
|
|
|
|
consumeError(std::move(Err));
|
|
|
|
|
2019-08-23 22:37:31 +02:00
|
|
|
// No dependencies registered, can't fail:
|
2020-09-11 18:23:14 +02:00
|
|
|
cantFail(FooR->notifyResolved(SymbolMap({{Foo, FooSym}})));
|
|
|
|
cantFail(FooR->notifyEmitted());
|
2018-06-14 23:16:29 +02:00
|
|
|
}
|
|
|
|
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
static bool linkOrdersEqual(const std::vector<JITDylibSP> &LHS,
|
2020-08-29 23:05:04 +02:00
|
|
|
ArrayRef<JITDylib *> RHS) {
|
|
|
|
if (LHS.size() != RHS.size())
|
|
|
|
return false;
|
|
|
|
auto *RHSE = RHS.begin();
|
|
|
|
for (auto &LHSE : LHS)
|
|
|
|
if (LHSE.get() != *RHSE)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
++RHSE;
|
|
|
|
return true;
|
2020-08-31 21:25:25 +02:00
|
|
|
}
|
2020-08-29 23:05:04 +02:00
|
|
|
|
|
|
|
TEST(JITDylibTest, GetDFSLinkOrderTree) {
|
|
|
|
// Test that DFS ordering behaves as expected when the linkage relationships
|
|
|
|
// form a tree.
|
|
|
|
|
|
|
|
ExecutionSession ES;
|
|
|
|
|
|
|
|
auto &LibA = ES.createBareJITDylib("A");
|
|
|
|
auto &LibB = ES.createBareJITDylib("B");
|
|
|
|
auto &LibC = ES.createBareJITDylib("C");
|
|
|
|
auto &LibD = ES.createBareJITDylib("D");
|
|
|
|
auto &LibE = ES.createBareJITDylib("E");
|
|
|
|
auto &LibF = ES.createBareJITDylib("F");
|
|
|
|
|
|
|
|
// Linkage relationships:
|
|
|
|
// A --- B -- D
|
|
|
|
// \ \- E
|
|
|
|
// \- C -- F
|
|
|
|
LibA.setLinkOrder(makeJITDylibSearchOrder({&LibB, &LibC}));
|
|
|
|
LibB.setLinkOrder(makeJITDylibSearchOrder({&LibD, &LibE}));
|
|
|
|
LibC.setLinkOrder(makeJITDylibSearchOrder({&LibF}));
|
|
|
|
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
auto DFSOrderFromB = JITDylib::getDFSLinkOrder({&LibB});
|
2020-08-29 23:05:04 +02:00
|
|
|
EXPECT_TRUE(linkOrdersEqual(DFSOrderFromB, {&LibB, &LibD, &LibE}))
|
|
|
|
<< "Incorrect DFS link order for LibB";
|
|
|
|
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
auto DFSOrderFromA = JITDylib::getDFSLinkOrder({&LibA});
|
2020-08-29 23:05:04 +02:00
|
|
|
EXPECT_TRUE(linkOrdersEqual(DFSOrderFromA,
|
|
|
|
{&LibA, &LibB, &LibD, &LibE, &LibC, &LibF}))
|
|
|
|
<< "Incorrect DFS link order for libA";
|
|
|
|
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
auto DFSOrderFromAB = JITDylib::getDFSLinkOrder({&LibA, &LibB});
|
2020-08-29 23:05:04 +02:00
|
|
|
EXPECT_TRUE(linkOrdersEqual(DFSOrderFromAB,
|
|
|
|
{&LibA, &LibB, &LibD, &LibE, &LibC, &LibF}))
|
|
|
|
<< "Incorrect DFS link order for { libA, libB }";
|
|
|
|
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
auto DFSOrderFromBA = JITDylib::getDFSLinkOrder({&LibB, &LibA});
|
2020-08-29 23:05:04 +02:00
|
|
|
EXPECT_TRUE(linkOrdersEqual(DFSOrderFromBA,
|
|
|
|
{&LibB, &LibD, &LibE, &LibA, &LibC, &LibF}))
|
|
|
|
<< "Incorrect DFS link order for { libB, libA }";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(JITDylibTest, GetDFSLinkOrderDiamond) {
|
|
|
|
// Test that DFS ordering behaves as expected when the linkage relationships
|
|
|
|
// contain a diamond.
|
|
|
|
|
|
|
|
ExecutionSession ES;
|
|
|
|
auto &LibA = ES.createBareJITDylib("A");
|
|
|
|
auto &LibB = ES.createBareJITDylib("B");
|
|
|
|
auto &LibC = ES.createBareJITDylib("C");
|
|
|
|
auto &LibD = ES.createBareJITDylib("D");
|
|
|
|
|
|
|
|
// Linkage relationships:
|
|
|
|
// A -- B --- D
|
|
|
|
// \-- C --/
|
|
|
|
LibA.setLinkOrder(makeJITDylibSearchOrder({&LibB, &LibC}));
|
|
|
|
LibB.setLinkOrder(makeJITDylibSearchOrder({&LibD}));
|
|
|
|
LibC.setLinkOrder(makeJITDylibSearchOrder({&LibD}));
|
|
|
|
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
auto DFSOrderFromA = JITDylib::getDFSLinkOrder({&LibA});
|
2020-08-29 23:05:04 +02:00
|
|
|
EXPECT_TRUE(linkOrdersEqual(DFSOrderFromA, {&LibA, &LibB, &LibD, &LibC}))
|
|
|
|
<< "Incorrect DFS link order for libA";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(JITDylibTest, GetDFSLinkOrderCycle) {
|
|
|
|
// Test that DFS ordering behaves as expected when the linkage relationships
|
|
|
|
// contain a cycle.
|
|
|
|
|
|
|
|
ExecutionSession ES;
|
|
|
|
auto &LibA = ES.createBareJITDylib("A");
|
|
|
|
auto &LibB = ES.createBareJITDylib("B");
|
|
|
|
auto &LibC = ES.createBareJITDylib("C");
|
|
|
|
|
|
|
|
// Linkage relationships:
|
|
|
|
// A -- B --- C -- A
|
|
|
|
LibA.setLinkOrder(makeJITDylibSearchOrder({&LibB}));
|
|
|
|
LibB.setLinkOrder(makeJITDylibSearchOrder({&LibC}));
|
|
|
|
LibC.setLinkOrder(makeJITDylibSearchOrder({&LibA}));
|
|
|
|
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
auto DFSOrderFromA = JITDylib::getDFSLinkOrder({&LibA});
|
2020-08-29 23:05:04 +02:00
|
|
|
EXPECT_TRUE(linkOrdersEqual(DFSOrderFromA, {&LibA, &LibB, &LibC}))
|
|
|
|
<< "Incorrect DFS link order for libA";
|
|
|
|
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
auto DFSOrderFromB = JITDylib::getDFSLinkOrder({&LibB});
|
2020-08-29 23:05:04 +02:00
|
|
|
EXPECT_TRUE(linkOrdersEqual(DFSOrderFromB, {&LibB, &LibC, &LibA}))
|
|
|
|
<< "Incorrect DFS link order for libB";
|
|
|
|
|
[ORC] Add support for resource tracking/removal (removable code).
This patch introduces new APIs to support resource tracking and removal in Orc.
It is intended as a thread-safe generalization of the removeModule concept from
OrcV1.
Clients can now create ResourceTracker objects (using
JITDylib::createResourceTracker) to track resources for each MaterializationUnit
(code, data, aliases, absolute symbols, etc.) added to the JIT. Every
MaterializationUnit will be associated with a ResourceTracker, and
ResourceTrackers can be re-used for multiple MaterializationUnits. Each JITDylib
has a default ResourceTracker that will be used for MaterializationUnits added
to that JITDylib if no ResourceTracker is explicitly specified.
Two operations can be performed on ResourceTrackers: transferTo and remove. The
transferTo operation transfers tracking of the resources to a different
ResourceTracker object, allowing ResourceTrackers to be merged to reduce
administrative overhead (the source tracker is invalidated in the process). The
remove operation removes all resources associated with a ResourceTracker,
including any symbols defined by MaterializationUnits associated with the
tracker, and also invalidates the tracker. These operations are thread safe, and
should work regardless of the the state of the MaterializationUnits. In the case
of resource transfer any existing resources associated with the source tracker
will be transferred to the destination tracker, and all future resources for
those units will be automatically associated with the destination tracker. In
the case of resource removal all already-allocated resources will be
deallocated, any if any program representations associated with the tracker have
not been compiled yet they will be destroyed. If any program representations are
currently being compiled then they will be prevented from completing: their
MaterializationResponsibility will return errors on any attempt to update the
JIT state.
Clients (usually Layer writers) wishing to track resources can implement the
ResourceManager API to receive notifications when ResourceTrackers are
transferred or removed. The MaterializationResponsibility::withResourceKeyDo
method can be used to create associations between the key for a ResourceTracker
and an allocated resource in a thread-safe way.
RTDyldObjectLinkingLayer and ObjectLinkingLayer are updated to use the
ResourceManager API to enable tracking and removal of memory allocated by the
JIT linker.
The new JITDylib::clear method can be used to trigger removal of every
ResourceTracker associated with the JITDylib (note that this will only
remove resources for the JITDylib, it does not run static destructors).
This patch includes unit tests showing basic usage. A follow-up patch will
update the Kaleidoscope and BuildingAJIT tutorial series to OrcV2 and will
use this API to release code associated with anonymous expressions.
2020-09-11 18:50:41 +02:00
|
|
|
auto DFSOrderFromC = JITDylib::getDFSLinkOrder({&LibC});
|
2020-08-29 23:05:04 +02:00
|
|
|
EXPECT_TRUE(linkOrdersEqual(DFSOrderFromC, {&LibC, &LibA, &LibB}))
|
|
|
|
<< "Incorrect DFS link order for libC";
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:09:38 +01:00
|
|
|
} // namespace
|