@@ -334,36 +334,44 @@ class SerialBackgroundQueue {
334
334
// This code forces the page-ins on multiple threads so
335
335
// the process is not stalled waiting on disk buffer i/o.
336
336
void multiThreadedPageInBackground (DeferredFiles &deferred) {
337
- using namespace std ::chrono;
338
337
static const size_t pageSize = Process::getPageSizeEstimate ();
339
338
static const size_t largeArchive = 10 * 1024 * 1024 ;
340
- std::atomic_int index = 0 ;
341
339
#ifndef NDEBUG
340
+ using namespace std ::chrono;
342
341
std::atomic_int numDeferedFilesTouched = 0 ;
343
342
static std::atomic_uint64_t totalBytes = 0 ;
344
343
auto t0 = high_resolution_clock::now ();
345
344
#endif
346
345
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 ;
355
350
#ifndef NDEBUG
356
- totalBytes += buff.size ();
357
- numDeferedFilesTouched += 1 ;
351
+ totalBytes += buff.size ();
352
+ numDeferedFilesTouched += 1 ;
358
353
#endif
359
354
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
367
375
#ifndef NDEBUG
368
376
auto dt = high_resolution_clock::now () - t0;
369
377
if (Process::GetEnv (" LLD_MULTI_THREAD_PAGE" ))
@@ -556,7 +564,7 @@ static void deferFile(StringRef path, bool isLazy, DeferredFiles &deferred) {
556
564
std::optional<MemoryBufferRef> buffer = readFile (path);
557
565
if (!buffer)
558
566
return ;
559
- if (config->readThreads )
567
+ if (config->readWorkers )
560
568
deferred.push_back ({path, isLazy, *buffer});
561
569
else
562
570
processFile (buffer, nullptr , path, LoadType::CommandLine, isLazy);
@@ -1420,7 +1428,7 @@ static void createFiles(const InputArgList &args) {
1420
1428
}
1421
1429
}
1422
1430
1423
- if (config->readThreads ) {
1431
+ if (config->readWorkers ) {
1424
1432
multiThreadedPageIn (deferredFiles);
1425
1433
1426
1434
DeferredFiles archiveContents;
@@ -1829,13 +1837,13 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
1829
1837
}
1830
1838
}
1831
1839
1832
- if (auto *arg = args.getLastArg (OPT_read_threads )) {
1840
+ if (auto *arg = args.getLastArg (OPT_read_workers )) {
1833
1841
StringRef v (arg->getValue ());
1834
1842
unsigned threads = 0 ;
1835
1843
if (!llvm::to_integer (v, threads, 0 ) || threads < 0 )
1836
1844
error (arg->getSpelling () + " : expected a positive integer, but got '" +
1837
1845
arg->getValue () + " '" );
1838
- config->readThreads = threads;
1846
+ config->readWorkers = threads;
1839
1847
}
1840
1848
if (auto *arg = args.getLastArg (OPT_threads_eq)) {
1841
1849
StringRef v (arg->getValue ());
0 commit comments