Skip to content

Commit 30b8c13

Browse files
committed
Threads becomes workers.
1 parent 432fb04 commit 30b8c13

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

lld/MachO/Config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ struct Configuration {
186186
bool interposable = false;
187187
bool errorForArchMismatch = false;
188188
bool ignoreAutoLink = false;
189-
int readThreads = 0;
189+
int readWorkers = 0;
190190
// ld64 allows invalid auto link options as long as the link succeeds. LLD
191191
// does not, but there are cases in the wild where the invalid linker options
192192
// exist. This allows users to ignore the specific invalid options in the case

lld/MachO/Driver.cpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -334,36 +334,44 @@ class SerialBackgroundQueue {
334334
// This code forces the page-ins on multiple threads so
335335
// the process is not stalled waiting on disk buffer i/o.
336336
void multiThreadedPageInBackground(DeferredFiles &deferred) {
337-
using namespace std::chrono;
338337
static const size_t pageSize = Process::getPageSizeEstimate();
339338
static const size_t largeArchive = 10 * 1024 * 1024;
340-
std::atomic_int index = 0;
341339
#ifndef NDEBUG
340+
using namespace std::chrono;
342341
std::atomic_int numDeferedFilesTouched = 0;
343342
static std::atomic_uint64_t totalBytes = 0;
344343
auto t0 = high_resolution_clock::now();
345344
#endif
346345

347-
parallelFor(0, config->readThreads, [&](size_t I) {
348-
while (true) {
349-
int localIndex = index.fetch_add(1);
350-
if (localIndex >= (int)deferred.size())
351-
break;
352-
const StringRef &buff = deferred[localIndex].buffer.getBuffer();
353-
if (buff.size() > largeArchive)
354-
continue;
346+
auto preloadDeferredFile = [&](const DeferredFile &deferredFile) {
347+
const StringRef &buff = deferredFile.buffer.getBuffer();
348+
if (buff.size() > largeArchive)
349+
return;
355350
#ifndef NDEBUG
356-
totalBytes += buff.size();
357-
numDeferedFilesTouched += 1;
351+
totalBytes += buff.size();
352+
numDeferedFilesTouched += 1;
358353
#endif
359354

360-
// Reference all file's mmap'd pages to load them into memory.
361-
for (const char *page = buff.data(), *end = page + buff.size();
362-
page < end; page += pageSize)
363-
LLVM_ATTRIBUTE_UNUSED volatile char t = *page;
364-
}
365-
});
366-
355+
// Reference all file's mmap'd pages to load them into memory.
356+
for (const char *page = buff.data(), *end = page + buff.size(); page < end;
357+
page += pageSize)
358+
LLVM_ATTRIBUTE_UNUSED volatile char t = *page;
359+
};
360+
#if LLVM_ENABLE_THREADS
361+
{ // Create scope for waiting for the taskGroup
362+
std::atomic_size_t index = 0;
363+
llvm::parallel::TaskGroup taskGroup;
364+
for (int w = 0; w < config->readWorkers; w++)
365+
taskGroup.spawn([&index, &preloadDeferredFile, &deferred]() {
366+
while (true) {
367+
size_t localIndex = index.fetch_add(1);
368+
if (localIndex >= deferred.size())
369+
break;
370+
preloadDeferredFile(deferred[localIndex]);
371+
}
372+
});
373+
}
374+
#endif
367375
#ifndef NDEBUG
368376
auto dt = high_resolution_clock::now() - t0;
369377
if (Process::GetEnv("LLD_MULTI_THREAD_PAGE"))
@@ -556,7 +564,7 @@ static void deferFile(StringRef path, bool isLazy, DeferredFiles &deferred) {
556564
std::optional<MemoryBufferRef> buffer = readFile(path);
557565
if (!buffer)
558566
return;
559-
if (config->readThreads)
567+
if (config->readWorkers)
560568
deferred.push_back({path, isLazy, *buffer});
561569
else
562570
processFile(buffer, nullptr, path, LoadType::CommandLine, isLazy);
@@ -1420,7 +1428,7 @@ static void createFiles(const InputArgList &args) {
14201428
}
14211429
}
14221430

1423-
if (config->readThreads) {
1431+
if (config->readWorkers) {
14241432
multiThreadedPageIn(deferredFiles);
14251433

14261434
DeferredFiles archiveContents;
@@ -1829,13 +1837,13 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
18291837
}
18301838
}
18311839

1832-
if (auto *arg = args.getLastArg(OPT_read_threads)) {
1840+
if (auto *arg = args.getLastArg(OPT_read_workers)) {
18331841
StringRef v(arg->getValue());
18341842
unsigned threads = 0;
18351843
if (!llvm::to_integer(v, threads, 0) || threads < 0)
18361844
error(arg->getSpelling() + ": expected a positive integer, but got '" +
18371845
arg->getValue() + "'");
1838-
config->readThreads = threads;
1846+
config->readWorkers = threads;
18391847
}
18401848
if (auto *arg = args.getLastArg(OPT_threads_eq)) {
18411849
StringRef v(arg->getValue());

lld/MachO/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ def dead_strip : Flag<["-"], "dead_strip">,
396396
def interposable : Flag<["-"], "interposable">,
397397
HelpText<"Indirects access to all exported symbols in an image">,
398398
Group<grp_opts>;
399-
def read_threads : Joined<["--"], "read-threads=">,
400-
HelpText<"Approximate number of threads to use to eagerly preload input files content into memory. Use 0 to disable this feature. Default is disabled.">,
399+
def read_workers : Joined<["--"], "read-workers=">,
400+
HelpText<"Approximate number of workers to use to eagerly preload input files content into memory. Use 0 to disable this feature. Default is disabled.">,
401401
Group<grp_lld>;
402402
def order_file : Separate<["-"], "order_file">,
403403
MetaVarName<"<file>">,

0 commit comments

Comments
 (0)