@@ -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.
336336void 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 ());
0 commit comments