Skip to content

Commit aaa6b0b

Browse files
committed
[Clang] add option --offload-jobs=N
for specifying number of threads for clang-linker-wrapper. By default it uses half of hardware threads.
1 parent 337a4d5 commit aaa6b0b

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,10 @@ def offload_compression_level_EQ : Joined<["--"], "offload-compression-level=">,
12331233
Flags<[HelpHidden]>,
12341234
HelpText<"Compression level for offload device binaries (HIP only)">;
12351235

1236+
def offload_jobs_EQ : Joined<["--"], "offload-jobs=">,
1237+
HelpText<"Set the number of threads for the clang-linker-wrapper. Defaults to"
1238+
" half of the available hardware threads if not specified.">;
1239+
12361240
defm offload_via_llvm : BoolFOption<"offload-via-llvm",
12371241
LangOpts<"OffloadViaLLVM">, DefaultFalse,
12381242
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Use">,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "llvm/Support/FileSystem.h"
5858
#include "llvm/Support/Path.h"
5959
#include "llvm/Support/Process.h"
60+
#include "llvm/Support/ThreadPool.h"
6061
#include "llvm/Support/YAMLParser.h"
6162
#include "llvm/TargetParser/AArch64TargetParser.h"
6263
#include "llvm/TargetParser/ARMTargetParserCommon.h"
@@ -1031,6 +1032,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
10311032
if (JA.isOffloading(Action::OFK_HIP)) {
10321033
Args.ClaimAllArgs(options::OPT_offload_compress);
10331034
Args.ClaimAllArgs(options::OPT_no_offload_compress);
1035+
Args.ClaimAllArgs(options::OPT_offload_jobs_EQ);
10341036
}
10351037

10361038
bool HasTarget = false;
@@ -9360,6 +9362,19 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
93609362
CmdArgs.push_back(LinkArg);
93619363

93629364
addOffloadCompressArgs(Args, CmdArgs);
9365+
9366+
// Default to half of hardware threads if users do not specify it.
9367+
if (Arg *A = Args.getLastArg(options::OPT_offload_jobs_EQ))
9368+
CmdArgs.push_back(
9369+
Args.MakeArgString(Twine("--wrapper-jobs=") + A->getValue()));
9370+
else {
9371+
unsigned NumThreads =
9372+
llvm::hardware_concurrency().compute_thread_count() / 2;
9373+
if (NumThreads > 1)
9374+
CmdArgs.push_back(
9375+
Args.MakeArgString(Twine("--wrapper-jobs=") + Twine(NumThreads)));
9376+
}
9377+
93639378
const char *Exec =
93649379
Args.MakeArgString(getToolChain().GetProgramPath("clang-linker-wrapper"));
93659380

clang/test/Driver/hip-options.hip

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@
243243
// NO-WARN-ATOMIC-NOT: clang{{.*}} "-triple" "x86_64-unknown-linux-gnu" {{.*}} "-Werror=atomic-alignment"
244244
// NO-WARN-ATOMIC-NOT: clang{{.*}} "-triple" "x86_64-unknown-linux-gnu" {{.*}} "-Wno-error=atomic-alignment"
245245

246-
// Check --offload-compress does not cause warning.
246+
// Check --offload-compress --offload-jobs=N does not cause warning.
247247
// RUN: %clang -### -Werror --target=x86_64-unknown-linux-gnu -nogpuinc -nogpulib \
248-
// RUN: --offload-arch=gfx1100 --offload-compress --offload-host-only -M %s
248+
// RUN: --offload-arch=gfx1100 --offload-compress --offload-host-only -M %s \
249+
// RUN: --offload-jobs=4
250+
251+
// Check --offload-jobs=N passed to linker wrapper.
252+
// RUN: %clang -### -Werror --target=x86_64-unknown-linux-gnu -nogpuinc -nogpulib \
253+
// RUN: --offload-arch=gfx1100 --offload-new-driver --offload-jobs=4 %s 2>&1 | \
254+
// RUN: FileCheck -check-prefix=JOBS %s
255+
// JOBS: clang-linker-wrapper{{.*}} "--wrapper-jobs=4"

0 commit comments

Comments
 (0)