clang/AMDGPU: Do not look for rocm device libs if environment is llvm (#180922)

clang/AMDGPU: Do not look for rocm device libs if environment is llvm

Introduce usage of the llvm environment type. This will be useful as
a switch to eventually stop depending on externally provided libraries,
and only take bitcode from the resource directory.

I wasn't sure how to handle the confusing mess of -no-* flags. Try
to handle them all. I'm not sure --no-offloadlib makes sense for OpenCL
since it's not really offload, but interpret it anyway.
This commit is contained in:
Matt Arsenault 2026-02-11 15:16:26 +01:00 committed by GitHub
parent 6d6feb7655
commit 0b0dca5668
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 68 additions and 12 deletions

View File

@ -230,7 +230,8 @@ void addOpenMPDeviceRTL(const Driver &D, const llvm::opt::ArgList &DriverArgs,
StringRef BitcodeSuffix, const llvm::Triple &Triple,
const ToolChain &HostTC);
void addOpenCLBuiltinsLib(const Driver &D, const llvm::opt::ArgList &DriverArgs,
void addOpenCLBuiltinsLib(const Driver &D, const llvm::Triple &TT,
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args);
void addOutlineAtomicsArgs(const Driver &D, const ToolChain &TC,

View File

@ -842,7 +842,8 @@ bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
const ArgList &Args)
: AMDGPUToolChain(D, Triple, Args) {
RocmInstallation->detectDeviceLibrary();
if (Triple.getEnvironment() != llvm::Triple::LLVM)
RocmInstallation->detectDeviceLibrary();
}
void AMDGPUToolChain::addClangTargetOptions(
@ -872,7 +873,7 @@ void AMDGPUToolChain::addClangTargetOptions(
CC1Args.push_back("-disable-llvm-optzns");
if (DeviceOffloadingKind == Action::OFK_None)
addOpenCLBuiltinsLib(getDriver(), DriverArgs, CC1Args);
addOpenCLBuiltinsLib(getDriver(), getTriple(), DriverArgs, CC1Args);
}
void AMDGPUToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
@ -963,8 +964,15 @@ void ROCMToolChain::addClangTargetOptions(
// For SPIR-V (SPIRVAMDToolChain) we must not link any device libraries so we
// skip it.
if (this->getEffectiveTriple().isSPIRV())
const llvm::Triple &TT = this->getEffectiveTriple();
if (TT.isSPIRV())
return;
// With an LLVM environment, only use libraries provided by the resource
// directory.
if (TT.getEnvironment() == llvm::Triple::LLVM)
return;
// Get the device name and canonicalize it
const StringRef GpuArch = getGPUArch(DriverArgs);
auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);

View File

@ -3068,15 +3068,25 @@ void tools::addOpenMPDeviceRTL(const Driver &D,
}
}
void tools::addOpenCLBuiltinsLib(const Driver &D,
void tools::addOpenCLBuiltinsLib(const Driver &D, const llvm::Triple &TT,
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) {
const Arg *A = DriverArgs.getLastArg(options::OPT_libclc_lib_EQ);
if (!A)
return;
// If the namespec is of the form :filename we use it exactly.
StringRef LibclcNamespec(A->getValue());
StringRef LibclcNamespec;
const Arg *A = DriverArgs.getLastArg(options::OPT_libclc_lib_EQ);
if (A) {
// If the namespec is of the form :filename we use it exactly.
LibclcNamespec = A->getValue();
} else {
if (!TT.isAMDGPU() || TT.getEnvironment() != llvm::Triple::LLVM)
return;
// TODO: Should this accept following -stdlib to override?
if (DriverArgs.hasArg(options::OPT_no_offloadlib,
options::OPT_nodefaultlibs, options::OPT_nostdlib))
return;
}
bool FilenameSearch = LibclcNamespec.consume_front(":");
if (FilenameSearch) {
SmallString<128> LibclcFile(LibclcNamespec);

View File

@ -376,8 +376,11 @@ llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
HIPAMDToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs,
Action::OffloadKind DeviceOffloadingKind) const {
llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
const llvm::Triple &TT = getEffectiveTriple();
if (!DriverArgs.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib,
true) ||
TT.getEnvironment() == llvm::Triple::LLVM ||
getGPUArch(DriverArgs) == "amdgcnspirv")
return {};
ArgStringList LibraryPaths;

View File

@ -0,0 +1,11 @@
// Do not use device library with llvm environment
// RUN: %clang -### --target=x86_64-linux-gnu -nogpuinc \
// RUN: --offload-targets=amdgcn-amd-amdhsa-llvm --offload-arch=gfx90a \
// RUN: -resource-dir=%S/Inputs/rocm_resource_dir \
// RUN: %S/Inputs/hip_multiple_inputs/b.hip \
// RUN: 2>&1 | FileCheck --check-prefix=LLVMENV %s
// LLVMENV-NOT: -mlink-builtin-bitcode
// LLVMENV-NOT: oclc
// LLVMENV-NOT: ocml
// LLVMENV-NOT: ockl

View File

@ -19,3 +19,26 @@
// RUN: -resource-dir %S/Inputs/resource_dir_with_per_target_subdir \
// RUN: %s 2>&1 | FileCheck %s --check-prefix=CHECK-GENERIC
// CHECK-GENERIC: -mlink-builtin-bitcode{{.*}}resource_dir_with_per_target_subdir{{/|\\\\}}lib{{/|\\\\}}amdgcn-amd-amdhsa{{/|\\\\}}libclc.bc
// RUN: %clang -### -target amdgcn-amd-amdhsa-llvm \
// RUN: -resource-dir %S/Inputs/resource_dir_with_per_target_subdir \
// RUN: %s 2>&1 | FileCheck %s --check-prefix=AMDGPU-LLVM-ENV
// AMDGPU-LLVM-ENV: -mlink-builtin-bitcode{{.*}}resource_dir_with_per_target_subdir{{/|\\\\}}lib{{/|\\\\}}amdgcn-amd-amdhsa-llvm{{/|\\\\}}libclc.bc
// RUN: %clang -### -target amdgcn-amd-amdhsa-llvm --no-offloadlib \
// RUN: -resource-dir %S/Inputs/resource_dir_with_per_target_subdir \
// RUN: %s 2>&1 | FileCheck %s --check-prefix=AMDGPU-LLVM-ENV-NO-OFFLOAD-LIB
// AMDGPU-LLVM-ENV-NO-OFFLOAD-LIB-NOT: libclc.bc
// Try with -nostdlib instead of --no-offloadlib
// RUN: %clang -### -target amdgcn-amd-amdhsa-llvm -nostdlib \
// RUN: -resource-dir %S/Inputs/resource_dir_with_per_target_subdir \
// RUN: %s 2>&1 | FileCheck %s --check-prefix=AMDGPU-LLVM-ENV-NO-OFFLOAD-LIB
// AMDGPU-LLVM-ENV-NO-OFFLOAD-LIB-NOT: libclc.bc
// Try with -nodefaultlibs instead of -nostdlib
// RUN: %clang -### -target amdgcn-amd-amdhsa-llvm -nodefaultlibs \
// RUN: -resource-dir %S/Inputs/resource_dir_with_per_target_subdir \
// RUN: %s 2>&1 | FileCheck %s --check-prefix=AMDGPU-LLVM-ENV-NO-OFFLOAD-LIB
// AMDGPU-LLVM-ENV-NO-OFFLOAD-LIB-NOT: libclc.bc

View File

@ -154,7 +154,7 @@ endif()
# List of all targets. Note that some are added dynamically below.
set( LIBCLC_TARGETS_ALL
amdgcn--
amdgcn-amd-amdhsa
amdgcn-amd-amdhsa-llvm
clspv--
clspv64--
r600--
@ -207,7 +207,7 @@ include_directories( ${LLVM_INCLUDE_DIRS} )
set( r600--_devices cedar cypress barts cayman )
set( amdgcn--_devices tahiti )
set( amdgcn-mesa-mesa3d_devices ${amdgcn--_devices} )
set( amdgcn-amd-amdhsa_devices none )
set( amdgcn-amd-amdhsa-llvm_devices none )
set( clspv--_devices none )
set( clspv64--_devices none )
set( nvptx64--_devices none )