-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[lld][MachO]Multi-threaded i/o. Twice as fast linking a large project. #147134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c55b5b2
3d11a33
02fb145
a8eeead
55e26a8
817036b
c07e168
eb4827c
ce93ae3
c47e5c3
5caf5a6
890c492
9714785
85fd77f
6f5f7cb
e3e0369
febf5a9
a5f7a42
6b874b2
84154d4
ed9f07e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
#include "lld/Common/Reproduce.h" | ||
#include "lld/Common/Version.h" | ||
#include "llvm/ADT/DenseSet.h" | ||
#include "llvm/ADT/ScopeExit.h" | ||
#include "llvm/ADT/StringExtras.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/BinaryFormat/MachO.h" | ||
|
@@ -41,11 +42,14 @@ | |
#include "llvm/Object/Archive.h" | ||
#include "llvm/Option/ArgList.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/Debug.h" | ||
#include "llvm/Support/FileSystem.h" | ||
#include "llvm/Support/Parallel.h" | ||
#include "llvm/Support/Path.h" | ||
#include "llvm/Support/Process.h" | ||
#include "llvm/Support/TarWriter.h" | ||
#include "llvm/Support/TargetSelect.h" | ||
#include "llvm/Support/Threading.h" | ||
#include "llvm/Support/TimeProfiler.h" | ||
#include "llvm/TargetParser/Host.h" | ||
#include "llvm/TextAPI/Architecture.h" | ||
|
@@ -282,11 +286,100 @@ static void saveThinArchiveToRepro(ArchiveFile const *file) { | |
": Archive::children failed: " + toString(std::move(e))); | ||
} | ||
|
||
static InputFile *addFile(StringRef path, LoadType loadType, | ||
bool isLazy = false, bool isExplicit = true, | ||
bool isBundleLoader = false, | ||
bool isForceHidden = false) { | ||
std::optional<MemoryBufferRef> buffer = readFile(path); | ||
class DeferredFile { | ||
public: | ||
DeferredFile(StringRef path, bool isLazy, MemoryBufferRef buffer) | ||
: path(path), isLazy(isLazy), buffer(buffer) {} | ||
StringRef path; | ||
bool isLazy; | ||
MemoryBufferRef buffer; | ||
}; | ||
using DeferredFiles = std::vector<DeferredFile>; | ||
|
||
// Most input files have been mapped but not yet paged in. | ||
// This code forces the page-ins on multiple threads so | ||
// the process is not stalled waiting on disk buffer i/o. | ||
void multiThreadedPageInBackground(DeferredFiles &deferred) { | ||
static const size_t pageSize = Process::getPageSizeEstimate(); | ||
#if 0 | ||
ThreadPoolStrategy oldStrategy = llvm::parallel::strategy; | ||
(void)llvm::make_scope_exit([&]() { llvm::parallel::strategy = oldStrategy; }); | ||
llvm::parallel::strategy = llvm::hardware_concurrency(config->readThreads); | ||
|
||
size_t totalBytes = parallelTransformReduce(deferred, 0, | ||
[](size_t acc, size_t size) { return acc + size; }, | ||
[&](DeferredFile &file) { | ||
const StringRef &buffer = file.buffer.getBuffer(); | ||
for (const char *page = buffer.data(), *end = page + buffer.size(); | ||
page < end; page += pageSize) | ||
LLVM_ATTRIBUTE_UNUSED volatile char t = *page; | ||
return buffer.size(); | ||
} | ||
); | ||
#else | ||
static size_t totalBytes = 0; | ||
std::atomic_int index = 0; | ||
|
||
parallelFor(0, config->readThreads, [&](size_t I) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of how you are using Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my other comment. I'd missed yours. I'm only following the benchmarks as this is the thrust of this PR. |
||
while (true) { | ||
int localIndex = index.fetch_add(1); | ||
if (localIndex >= (int)deferred.size()) | ||
break; | ||
const StringRef &buff = deferred[localIndex].buffer.getBuffer(); | ||
totalBytes += buff.size(); | ||
|
||
// Reference all file's mmap'd pages to load them into memory. | ||
for (const char *page = buff.data(), *end = page + buff.size(); | ||
page < end; page += pageSize) | ||
LLVM_ATTRIBUTE_UNUSED volatile char t = *page; | ||
} | ||
}); | ||
#endif | ||
if (getenv("LLD_MULTI_THREAD_PAGE")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we guard this with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reverted this as it was generating a warning: totalBytes set but not used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using a RelWithDebugInfo build so I'd like to leave those messages in for now. I personally think they could stay in as they do not involve significant computation, are unlocked by an improbable environment variable and could be useful in a release build but once we've tuned and decided on the inner loop we can decide on this. |
||
llvm::dbgs() << "multiThreadedPageIn " << totalBytes << "/" | ||
<< deferred.size() << "\n"; | ||
} | ||
|
||
static void multiThreadedPageIn(const DeferredFiles &deferred) { | ||
static std::deque<std::unique_ptr<DeferredFiles>> queue; | ||
static std::thread *running; | ||
static std::mutex mutex; | ||
|
||
mutex.lock(); | ||
if (running && (queue.empty() || deferred.empty())) { | ||
mutex.unlock(); | ||
running->join(); | ||
mutex.lock(); | ||
delete running; | ||
running = nullptr; | ||
} | ||
|
||
if (!deferred.empty()) { | ||
queue.emplace_back(std::make_unique<DeferredFiles>(deferred)); | ||
if (!running) | ||
running = new std::thread([&]() { | ||
while (true) { | ||
mutex.lock(); | ||
if (queue.empty()) { | ||
mutex.unlock(); | ||
break; | ||
} | ||
DeferredFiles deferred(*queue.front()); | ||
queue.pop_front(); | ||
mutex.unlock(); | ||
multiThreadedPageInBackground(deferred); | ||
} | ||
}); | ||
} | ||
mutex.unlock(); | ||
} | ||
|
||
static InputFile *processFile(std::optional<MemoryBufferRef> buffer, | ||
DeferredFiles *archiveContents, StringRef path, | ||
LoadType loadType, bool isLazy = false, | ||
bool isExplicit = true, | ||
bool isBundleLoader = false, | ||
bool isForceHidden = false) { | ||
if (!buffer) | ||
return nullptr; | ||
MemoryBufferRef mbref = *buffer; | ||
|
@@ -379,6 +472,8 @@ static InputFile *addFile(StringRef path, LoadType loadType, | |
continue; | ||
} | ||
|
||
if (archiveContents) | ||
archiveContents->emplace_back(path, isLazy, *mb); | ||
if (!hasObjCSection(*mb)) | ||
continue; | ||
if (Error e = file->fetch(c, "-ObjC")) | ||
|
@@ -390,7 +485,8 @@ static InputFile *addFile(StringRef path, LoadType loadType, | |
": Archive::children failed: " + toString(std::move(e))); | ||
} | ||
} | ||
file->addLazySymbols(); | ||
if (!archiveContents || archiveContents->empty()) | ||
file->addLazySymbols(); | ||
loadedArchives[path] = ArchiveFileInfo{file, isCommandLineLoad}; | ||
newFile = file; | ||
break; | ||
|
@@ -441,6 +537,24 @@ static InputFile *addFile(StringRef path, LoadType loadType, | |
return newFile; | ||
} | ||
|
||
static InputFile *addFile(StringRef path, LoadType loadType, | ||
bool isLazy = false, bool isExplicit = true, | ||
bool isBundleLoader = false, | ||
bool isForceHidden = false) { | ||
return processFile(readFile(path), nullptr, path, loadType, isLazy, | ||
isExplicit, isBundleLoader, isForceHidden); | ||
} | ||
|
||
static void deferFile(StringRef path, bool isLazy, DeferredFiles &deferred) { | ||
std::optional<MemoryBufferRef> buffer = readFile(path); | ||
if (!buffer) | ||
return; | ||
if (config->readThreads) | ||
deferred.emplace_back(path, isLazy, *buffer); | ||
else | ||
processFile(buffer, nullptr, path, LoadType::CommandLine, isLazy); | ||
} | ||
|
||
static std::vector<StringRef> missingAutolinkWarnings; | ||
static void addLibrary(StringRef name, bool isNeeded, bool isWeak, | ||
bool isReexport, bool isHidden, bool isExplicit, | ||
|
@@ -564,13 +678,14 @@ void macho::resolveLCLinkerOptions() { | |
} | ||
} | ||
|
||
static void addFileList(StringRef path, bool isLazy) { | ||
static void addFileList(StringRef path, bool isLazy, | ||
DeferredFiles &deferredFiles) { | ||
std::optional<MemoryBufferRef> buffer = readFile(path); | ||
if (!buffer) | ||
return; | ||
MemoryBufferRef mbref = *buffer; | ||
for (StringRef path : args::getLines(mbref)) | ||
addFile(rerootPath(path), LoadType::CommandLine, isLazy); | ||
deferFile(rerootPath(path), isLazy, deferredFiles); | ||
} | ||
|
||
// We expect sub-library names of the form "libfoo", which will match a dylib | ||
|
@@ -1222,14 +1337,16 @@ static void createFiles(const InputArgList &args) { | |
bool isLazy = false; | ||
// If we've processed an opening --start-lib, without a matching --end-lib | ||
bool inLib = false; | ||
DeferredFiles deferredFiles; | ||
|
||
for (const Arg *arg : args) { | ||
const Option &opt = arg->getOption(); | ||
warnIfDeprecatedOption(opt); | ||
warnIfUnimplementedOption(opt); | ||
|
||
switch (opt.getID()) { | ||
case OPT_INPUT: | ||
addFile(rerootPath(arg->getValue()), LoadType::CommandLine, isLazy); | ||
deferFile(rerootPath(arg->getValue()), isLazy, deferredFiles); | ||
break; | ||
case OPT_needed_library: | ||
if (auto *dylibFile = dyn_cast_or_null<DylibFile>( | ||
|
@@ -1249,7 +1366,7 @@ static void createFiles(const InputArgList &args) { | |
dylibFile->forceWeakImport = true; | ||
break; | ||
case OPT_filelist: | ||
addFileList(arg->getValue(), isLazy); | ||
addFileList(arg->getValue(), isLazy, deferredFiles); | ||
break; | ||
case OPT_force_load: | ||
addFile(rerootPath(arg->getValue()), LoadType::CommandLineForce); | ||
|
@@ -1295,6 +1412,28 @@ static void createFiles(const InputArgList &args) { | |
break; | ||
} | ||
} | ||
|
||
if (config->readThreads) { | ||
multiThreadedPageIn(deferredFiles); | ||
|
||
DeferredFiles archiveContents; | ||
std::vector<ArchiveFile *> archives; | ||
for (auto &file : deferredFiles) { | ||
auto inputFile = processFile(file.buffer, &archiveContents, file.path, | ||
LoadType::CommandLine, file.isLazy); | ||
if (ArchiveFile *archive = dyn_cast<ArchiveFile>(inputFile)) | ||
archives.push_back(archive); | ||
} | ||
|
||
if (!archiveContents.empty()) { | ||
multiThreadedPageIn(archiveContents); | ||
for (auto *archive : archives) | ||
archive->addLazySymbols(); | ||
} | ||
johnno1962 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
DeferredFiles reapThreads; | ||
multiThreadedPageIn(reapThreads); | ||
} | ||
} | ||
|
||
static void gatherInputSections() { | ||
|
@@ -1687,6 +1826,14 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS, | |
} | ||
} | ||
|
||
if (auto *arg = args.getLastArg(OPT_read_threads)) { | ||
StringRef v(arg->getValue()); | ||
unsigned threads = 0; | ||
if (!llvm::to_integer(v, threads, 0) || threads < 0) | ||
johnno1962 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
error(arg->getSpelling() + ": expected a positive integer, but got '" + | ||
arg->getValue() + "'"); | ||
config->readThreads = threads; | ||
} | ||
if (auto *arg = args.getLastArg(OPT_threads_eq)) { | ||
StringRef v(arg->getValue()); | ||
unsigned threads = 0; | ||
|
Uh oh!
There was an error while loading. Please reload this page.