Skip to content

Commit

Permalink
[llvm-jitlink] Wait for reachable files to link before running checks.
Browse files Browse the repository at this point in the history
ORC dependence tracking is fine-grained (i.e. per-symbol), however when running
-check mode we want to wait for all links triggered by the entry point lookup
to complete, regardless of whether the code / data in them is actually
reachable from the entry point. This simplifies test-cases, since authors don't
need to reason about per-symbol dependencies to know that additional files will
be linked (if referenced transitively in any way from the test-case).

The new Session::waitForFilesLinkedFromEntryPointFile utility does _not_ wait
for lazily linked (-lazy) files.

This will be used to fix buildbot errors caused by edca1d9.
  • Loading branch information
lhames committed Dec 23, 2024
1 parent 5712e29 commit 158a600
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions llvm/tools/llvm-jitlink/llvm-jitlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,11 @@ void Session::modifyPassConfig(LinkGraph &G, PassConfiguration &PassConfig) {
return Error::success();
});

PassConfig.PrePrunePasses.push_back([this](LinkGraph &G) {
std::lock_guard<std::mutex> Lock(M);
++ActiveLinks;
return Error::success();
});
PassConfig.PrePrunePasses.push_back(
[this](LinkGraph &G) { return applyHarnessPromotions(*this, G); });

Expand All @@ -1242,6 +1247,13 @@ void Session::modifyPassConfig(LinkGraph &G, PassConfiguration &PassConfig) {

if (AddSelfRelocations)
PassConfig.PostPrunePasses.push_back(addSelfRelocations);

PassConfig.PostFixupPasses.push_back([this](LinkGraph &G) {
std::lock_guard<std::mutex> Lock(M);
if (--ActiveLinks == 0)
ActiveLinksCV.notify_all();
return Error::success();
});
}

Expected<JITDylib *> Session::getOrLoadDynamicLibrary(StringRef LibPath) {
Expand Down Expand Up @@ -2314,6 +2326,8 @@ static Error runChecks(Session &S, Triple TT, SubtargetFeatures Features) {
if (CheckFiles.empty())
return Error::success();

S.waitForFilesLinkedFromEntryPointFile();

LLVM_DEBUG(dbgs() << "Running checks...\n");

auto IsSymbolValid = [&S](StringRef Symbol) {
Expand Down
13 changes: 13 additions & 0 deletions llvm/tools/llvm-jitlink/llvm-jitlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ struct Session {
void modifyPassConfig(jitlink::LinkGraph &G,
jitlink::PassConfiguration &PassConfig);

/// For -check: wait for all files that are referenced (transitively) from
/// the entry point *file* to be linked. (ORC's usual dependence tracking is
/// to fine-grained here: a lookup of the main symbol will return as soon as
/// all reachable symbols have been linked, but testcases may want to
/// inspect side-effects in unreachable symbols)..
void waitForFilesLinkedFromEntryPointFile() {
std::unique_lock<std::mutex> Lock(M);
return ActiveLinksCV.wait(Lock, [this]() { return ActiveLinks == 0; });
}

using MemoryRegionInfo = RuntimeDyldChecker::MemoryRegionInfo;

struct FileInfo {
Expand Down Expand Up @@ -110,6 +120,9 @@ struct Session {

DynLibJDMap DynLibJDs;

std::mutex M;
std::condition_variable ActiveLinksCV;
size_t ActiveLinks = 0;
SymbolInfoMap SymbolInfos;
FileInfoMap FileInfos;

Expand Down

0 comments on commit 158a600

Please sign in to comment.