Skip to content

Commit d0aa1f9

Browse files
authored
[Clang] Make --lto-partitions only default for HIP (#133164)
Summary: The default behavior for LTO on other targets does not specify the number of LTO partitions. Recent changes made this default to 8 on AMDGPU which had some issues with the `libc` project. The option to disable this is HIP only so I think for now we should restrict this just to HIP. I'm definitely on board with getting some more parallelism here, but I think it should probably be restricted to just offloading languages. The new driver goes through the `--target=amdgcn-amd-amdhsa` for its output, which means we'd need to forward the default somehow.
1 parent 17d0569 commit d0aa1f9

File tree

4 files changed

+28
-37
lines changed

4 files changed

+28
-37
lines changed

Diff for: clang/lib/Driver/ToolChains/AMDGPU.cpp

+7-14
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
633633
const bool ThinLTO = (C.getDriver().getLTOMode() == LTOK_Thin);
634634
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0], ThinLTO);
635635

636-
if (!ThinLTO)
636+
if (!ThinLTO && JA.getOffloadingDeviceKind() == Action::OFK_HIP)
637637
addFullLTOPartitionOption(C.getDriver(), Args, CmdArgs);
638638
} else if (Args.hasArg(options::OPT_mcpu_EQ)) {
639639
CmdArgs.push_back(Args.MakeArgString(
@@ -712,14 +712,12 @@ void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
712712
}
713713

714714
static unsigned getFullLTOPartitions(const Driver &D, const ArgList &Args) {
715-
const Arg *A = Args.getLastArg(options::OPT_flto_partitions_EQ);
716-
// In the absence of an option, use 8 as the default.
717-
if (!A)
718-
return 8;
719715
int Value = 0;
720-
if (StringRef(A->getValue()).getAsInteger(10, Value) || (Value < 1)) {
716+
StringRef A = Args.getLastArgValue(options::OPT_flto_partitions_EQ, "8");
717+
if (A.getAsInteger(10, Value) || (Value < 1)) {
718+
Arg *Arg = Args.getLastArg(options::OPT_flto_partitions_EQ);
721719
D.Diag(diag::err_drv_invalid_int_value)
722-
<< A->getAsString(Args) << A->getValue();
720+
<< Arg->getAsString(Args) << Arg->getValue();
723721
return 1;
724722
}
725723

@@ -729,13 +727,8 @@ static unsigned getFullLTOPartitions(const Driver &D, const ArgList &Args) {
729727
void amdgpu::addFullLTOPartitionOption(const Driver &D,
730728
const llvm::opt::ArgList &Args,
731729
llvm::opt::ArgStringList &CmdArgs) {
732-
// TODO: Should this be restricted to fgpu-rdc only ? Currently we'll
733-
// also do it for non gpu-rdc LTO
734-
735-
if (unsigned NumParts = getFullLTOPartitions(D, Args); NumParts > 1) {
736-
CmdArgs.push_back(
737-
Args.MakeArgString("--lto-partitions=" + Twine(NumParts)));
738-
}
730+
CmdArgs.push_back(Args.MakeArgString("--lto-partitions=" +
731+
Twine(getFullLTOPartitions(D, Args))));
739732
}
740733

741734
/// AMDGPU Toolchain

Diff for: clang/test/Driver/amdgpu-toolchain.c

+1-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+:sramecc- -nogpulib \
2222
// RUN: -L. -flto -fconvergent-functions %s 2>&1 | FileCheck -check-prefix=LTO %s
2323
// LTO: clang{{.*}} "-flto=full"{{.*}}"-fconvergent-functions"
24-
// LTO: ld.lld{{.*}}"-L."{{.*}}"-plugin-opt=mcpu=gfx90a"{{.*}}"--lto-partitions={{[0-9]+}}"{{.*}}"-plugin-opt=-mattr=-sramecc,+xnack"
24+
// LTO: ld.lld{{.*}}"-L."{{.*}}"-plugin-opt=mcpu=gfx90a"{{.*}}"-plugin-opt=-mattr=-sramecc,+xnack"
2525

2626
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+:sramecc- -nogpulib \
2727
// RUN: -L. -fconvergent-functions %s 2>&1 | FileCheck -check-prefix=MCPU %s
@@ -38,17 +38,3 @@
3838
// RUN: %clang -target amdgcn-amd-amdhsa -march=gfx90a -stdlib -startfiles \
3939
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
4040
// STARTUP: ld.lld{{.*}}"-lc" "-lm" "{{.*}}crt1.o"
41-
42-
// Check --flto-partitions
43-
44-
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a -nogpulib \
45-
// RUN: -L. -flto --flto-partitions=42 %s 2>&1 | FileCheck -check-prefix=LTO_PARTS %s
46-
// LTO_PARTS: ld.lld{{.*}}"-L."{{.*}}"-plugin-opt=mcpu=gfx90a"{{.*}}"--lto-partitions=42"
47-
48-
// RUN: not %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a -nogpulib \
49-
// RUN: -L. -flto --flto-partitions=a %s 2>&1 | FileCheck -check-prefix=LTO_PARTS_INV0 %s
50-
// LTO_PARTS_INV0: clang: error: invalid integral value 'a' in '--flto-partitions=a'
51-
52-
// RUN: not %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a -nogpulib \
53-
// RUN: -L. -flto --flto-partitions=0 %s 2>&1 | FileCheck -check-prefix=LTO_PARTS_INV1 %s
54-
// LTO_PARTS_INV1: clang: error: invalid integral value '0' in '--flto-partitions=0'

Diff for: clang/test/Driver/hip-toolchain-rdc.hip

+18
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,21 @@
161161
// output the executable
162162
// LNX: [[LD:".*ld.*"]] {{.*}}"-o" "a.out" {{.*}} [[A_OBJ_HOST]] [[B_OBJ_HOST]] [[OBJBUNDLE]]
163163
// MSVC: [[LD:".*lld-link.*"]] {{.*}}"-out:a.exe" {{.*}} [[A_OBJ_HOST]] [[B_OBJ_HOST]] [[OBJBUNDLE]]
164+
165+
// Check --flto-partitions
166+
167+
// RUN: %clang -### -fgpu-rdc --offload-arch=gfx90a -nogpulib -nogpuinc \
168+
// RUN: -L. -foffload-lto %s 2>&1 | FileCheck -check-prefix=LTO_DEFAULT %s
169+
// LTO_DEFAULT: lld{{.*}}"--lto-partitions=8"
170+
171+
// RUN: %clang -### -fgpu-rdc --offload-arch=gfx90a -nogpulib -nogpuinc \
172+
// RUN: -L. -foffload-lto --flto-partitions=42 %s 2>&1 | FileCheck -check-prefix=LTO_PARTS %s
173+
// LTO_PARTS: lld{{.*}}"--lto-partitions=42"
174+
175+
// RUN: not %clang -### -fgpu-rdc --offload-arch=gfx90a -nogpulib -nogpuinc \
176+
// RUN: -L. -foffload-lto --flto-partitions=a %s 2>&1 | FileCheck -check-prefix=LTO_PARTS_INV0 %s
177+
// LTO_PARTS_INV0: clang: error: invalid integral value 'a' in '--flto-partitions=a'
178+
179+
// RUN: not %clang -### -fgpu-rdc --offload-arch=gfx90a -nogpulib -nogpuinc \
180+
// RUN: -L. -foffload-lto --flto-partitions=0 %s 2>&1 | FileCheck -check-prefix=LTO_PARTS_INV1 %s
181+
// LTO_PARTS_INV1: clang: error: invalid integral value '0' in '--flto-partitions=0'

Diff for: libc/startup/gpu/CMakeLists.txt

+2-8
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,8 @@ function(add_startup_object name)
3333
set_target_properties(${fq_target_name}.exe PROPERTIES
3434
RUNTIME_OUTPUT_DIRECTORY ${LIBC_LIBRARY_DIR}
3535
RUNTIME_OUTPUT_NAME ${name}.o)
36-
# FIXME: A bug in the AMDGPU LTO pass is incorrectly removing the kernels.
37-
if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
38-
target_link_options(${fq_target_name}.exe PRIVATE
39-
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
40-
else()
41-
target_link_options(${fq_target_name}.exe PRIVATE
42-
"-r" "-nostdlib" "-Wl,--lto-emit-llvm")
43-
endif()
36+
target_link_options(${fq_target_name}.exe PRIVATE
37+
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
4438
endif()
4539
endfunction()
4640

0 commit comments

Comments
 (0)