Skip to content

[Clang] Make --lto-partitions only default for HIP #133164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions clang/lib/Driver/ToolChains/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
const bool ThinLTO = (C.getDriver().getLTOMode() == LTOK_Thin);
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0], ThinLTO);

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

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

Expand All @@ -729,13 +727,8 @@ static unsigned getFullLTOPartitions(const Driver &D, const ArgList &Args) {
void amdgpu::addFullLTOPartitionOption(const Driver &D,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) {
// TODO: Should this be restricted to fgpu-rdc only ? Currently we'll
// also do it for non gpu-rdc LTO

if (unsigned NumParts = getFullLTOPartitions(D, Args); NumParts > 1) {
CmdArgs.push_back(
Args.MakeArgString("--lto-partitions=" + Twine(NumParts)));
}
CmdArgs.push_back(Args.MakeArgString("--lto-partitions=" +
Twine(getFullLTOPartitions(D, Args))));
}

/// AMDGPU Toolchain
Expand Down
16 changes: 1 addition & 15 deletions clang/test/Driver/amdgpu-toolchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+:sramecc- -nogpulib \
// RUN: -L. -flto -fconvergent-functions %s 2>&1 | FileCheck -check-prefix=LTO %s
// LTO: clang{{.*}} "-flto=full"{{.*}}"-fconvergent-functions"
// LTO: ld.lld{{.*}}"-L."{{.*}}"-plugin-opt=mcpu=gfx90a"{{.*}}"--lto-partitions={{[0-9]+}}"{{.*}}"-plugin-opt=-mattr=-sramecc,+xnack"
// LTO: ld.lld{{.*}}"-L."{{.*}}"-plugin-opt=mcpu=gfx90a"{{.*}}"-plugin-opt=-mattr=-sramecc,+xnack"

// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+:sramecc- -nogpulib \
// RUN: -L. -fconvergent-functions %s 2>&1 | FileCheck -check-prefix=MCPU %s
Expand All @@ -38,17 +38,3 @@
// RUN: %clang -target amdgcn-amd-amdhsa -march=gfx90a -stdlib -startfiles \
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
// STARTUP: ld.lld{{.*}}"-lc" "-lm" "{{.*}}crt1.o"

// Check --flto-partitions

// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a -nogpulib \
// RUN: -L. -flto --flto-partitions=42 %s 2>&1 | FileCheck -check-prefix=LTO_PARTS %s
// LTO_PARTS: ld.lld{{.*}}"-L."{{.*}}"-plugin-opt=mcpu=gfx90a"{{.*}}"--lto-partitions=42"

// RUN: not %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a -nogpulib \
// RUN: -L. -flto --flto-partitions=a %s 2>&1 | FileCheck -check-prefix=LTO_PARTS_INV0 %s
// LTO_PARTS_INV0: clang: error: invalid integral value 'a' in '--flto-partitions=a'

// RUN: not %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a -nogpulib \
// RUN: -L. -flto --flto-partitions=0 %s 2>&1 | FileCheck -check-prefix=LTO_PARTS_INV1 %s
// LTO_PARTS_INV1: clang: error: invalid integral value '0' in '--flto-partitions=0'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move these checks to a hip file then?

18 changes: 18 additions & 0 deletions clang/test/Driver/hip-toolchain-rdc.hip
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,21 @@
// output the executable
// LNX: [[LD:".*ld.*"]] {{.*}}"-o" "a.out" {{.*}} [[A_OBJ_HOST]] [[B_OBJ_HOST]] [[OBJBUNDLE]]
// MSVC: [[LD:".*lld-link.*"]] {{.*}}"-out:a.exe" {{.*}} [[A_OBJ_HOST]] [[B_OBJ_HOST]] [[OBJBUNDLE]]

// Check --flto-partitions

// RUN: %clang -### -fgpu-rdc --offload-arch=gfx90a -nogpulib -nogpuinc \
// RUN: -L. -foffload-lto %s 2>&1 | FileCheck -check-prefix=LTO_DEFAULT %s
// LTO_DEFAULT: lld{{.*}}"--lto-partitions=8"

// RUN: %clang -### -fgpu-rdc --offload-arch=gfx90a -nogpulib -nogpuinc \
// RUN: -L. -foffload-lto --flto-partitions=42 %s 2>&1 | FileCheck -check-prefix=LTO_PARTS %s
// LTO_PARTS: lld{{.*}}"--lto-partitions=42"

// RUN: not %clang -### -fgpu-rdc --offload-arch=gfx90a -nogpulib -nogpuinc \
// RUN: -L. -foffload-lto --flto-partitions=a %s 2>&1 | FileCheck -check-prefix=LTO_PARTS_INV0 %s
// LTO_PARTS_INV0: clang: error: invalid integral value 'a' in '--flto-partitions=a'

// RUN: not %clang -### -fgpu-rdc --offload-arch=gfx90a -nogpulib -nogpuinc \
// RUN: -L. -foffload-lto --flto-partitions=0 %s 2>&1 | FileCheck -check-prefix=LTO_PARTS_INV1 %s
// LTO_PARTS_INV1: clang: error: invalid integral value '0' in '--flto-partitions=0'
10 changes: 2 additions & 8 deletions libc/startup/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,8 @@ function(add_startup_object name)
set_target_properties(${fq_target_name}.exe PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${LIBC_LIBRARY_DIR}
RUNTIME_OUTPUT_NAME ${name}.o)
# FIXME: A bug in the AMDGPU LTO pass is incorrectly removing the kernels.
if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
target_link_options(${fq_target_name}.exe PRIVATE
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
else()
target_link_options(${fq_target_name}.exe PRIVATE
"-r" "-nostdlib" "-Wl,--lto-emit-llvm")
endif()
target_link_options(${fq_target_name}.exe PRIVATE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related, this works around what this patch disables.

"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
endif()
endfunction()

Expand Down
Loading