diff --git a/lld/MachO/Config.h b/lld/MachO/Config.h --- a/lld/MachO/Config.h +++ b/lld/MachO/Config.h @@ -92,6 +92,7 @@ llvm::StringRef mapFile; llvm::StringRef outputFile; llvm::StringRef ltoObjPath; + llvm::StringRef thinLTOJobs; bool demangle = false; llvm::MachO::Target target; PlatformInfo platformInfo; diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -869,8 +869,12 @@ error(arg->getSpelling() + ": expected a positive integer, but got '" + arg->getValue() + "'"); parallel::strategy = hardware_concurrency(threads); - // FIXME: use this to configure ThinLTO concurrency too + config->thinLTOJobs = v; } + if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq)) + config->thinLTOJobs = arg->getValue(); + if (!get_threadpool_strategy(config->thinLTOJobs)) + error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs); config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"), /*file=*/nullptr, diff --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp --- a/lld/MachO/LTO.cpp +++ b/lld/MachO/LTO.cpp @@ -48,8 +48,8 @@ } BitcodeCompiler::BitcodeCompiler() { - lto::ThinBackend backend = - lto::createInProcessThinBackend(heavyweight_hardware_concurrency()); + lto::ThinBackend backend = lto::createInProcessThinBackend( + heavyweight_hardware_concurrency(config->thinLTOJobs)); ltoObj = std::make_unique(createConfig(), backend); } diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -23,6 +23,9 @@ def threads_eq : Joined<["--"], "threads=">, HelpText<"Number of threads. '1' disables multi-threading. By default all available hardware threads are used">, Group; +def thinlto_jobs_eq : Joined<["--"], "thinlto-jobs=">, + HelpText<"Number of ThinLTO jobs. Default to --threads=">, + Group; def reproduce: Separate<["--"], "reproduce">, Group; def reproduce_eq: Joined<["--"], "reproduce=">, diff --git a/lld/test/MachO/Inputs/time_trace_tid_count.py b/lld/test/MachO/Inputs/time_trace_tid_count.py new file mode 100644 --- /dev/null +++ b/lld/test/MachO/Inputs/time_trace_tid_count.py @@ -0,0 +1,12 @@ +import json, sys + +""" +Usage: time_trace_tid_count.py < input.json + +Returns the number of unique thread IDs that contain the given event of +interest. +""" + +event_name = sys.argv[1] +events = json.loads(sys.stdin.read())['traceEvents'] +print(len(set(event['tid'] for event in events if event['name'] == event_name))) diff --git a/lld/test/MachO/thinlto-jobs.ll b/lld/test/MachO/thinlto-jobs.ll new file mode 100644 --- /dev/null +++ b/lld/test/MachO/thinlto-jobs.ll @@ -0,0 +1,53 @@ +; REQUIRES: x86 +; RUN: rm -rf %t; split-file %s %t + +; RUN: opt -module-summary %t/f.s -o %t/f.o +; RUN: opt -module-summary %t/g.s -o %t/g.o + +;; First force single-threaded mode +; RUN: %lld --time-trace --thinlto-jobs=1 -dylib %t/f.o %t/g.o -o %t/out +; RUN: %python %S/Inputs/time_trace_tid_count.py OptModule < %t/out.time-trace | FileCheck %s --check-prefix=SINGLE + +;; Next force multi-threaded mode +; RUN: %lld --time-trace --thinlto-jobs=2 -dylib %t/f.o %t/g.o -o %t/out +; RUN: %python %S/Inputs/time_trace_tid_count.py OptModule < %t/out.time-trace | FileCheck %s --check-prefix=MULTI + +;; --thinlto-jobs= defaults to --threads=. +; RUN: %lld --time-trace --threads=2 -dylib %t/f.o %t/g.o -o %t/out +; RUN: %python %S/Inputs/time_trace_tid_count.py OptModule < %t/out.time-trace | FileCheck %s --check-prefix=MULTI + +;; --thinlto-jobs= overrides --threads=. +; RUN: %lld --time-trace --threads=1 --thinlto-jobs=2 -dylib %t/f.o %t/g.o -o %t/out +; RUN: %python %S/Inputs/time_trace_tid_count.py OptModule < %t/out.time-trace | FileCheck %s --check-prefix=MULTI + +; SINGLE: 1 +; MULTI: 2 + +;; Test that we parse `--thinlto-jobs=all`. Not checking the exact number of +;; threads here since we may be running this test on a single-core machine. +; RUN: %lld --thinlto-jobs=all -dylib %t/f.o %t/g.o -o /dev/null + +;; Test with a bad value +; RUN: not %lld --thinlto-jobs=foo -dylib %t/f.o %t/g.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=BAD-JOBS +; BAD-JOBS: error: --thinlto-jobs: invalid job count: foo + +;--- f.s +target triple = "x86_64-apple-darwin" +target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" + +declare void @g(...) + +define void @f() { +entry: + call void (...) @g() + ret void +} + +;--- g.s +target triple = "x86_64-apple-darwin" +target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" + +define void @g() { +entry: + ret void +}