Skip to content

Commit d1923cb

Browse files
pdhaliwal-amdtstellar
authored andcommitted
[AMDGPU][OpenMP] Support linking of math libraries
Math libraries are linked only when -lm is specified. This is because host system could be missing rocm-device-libs. Reviewed By: JonChesterfield, yaxunl Differential Revision: https://reviews.llvm.org/D105981 (cherry picked from commit 9830f90)
1 parent 7f2f829 commit d1923cb

File tree

5 files changed

+68
-31
lines changed

5 files changed

+68
-31
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,3 +893,38 @@ bool AMDGPUToolChain::shouldSkipArgument(const llvm::opt::Arg *A) const {
893893
return true;
894894
return false;
895895
}
896+
897+
llvm::SmallVector<std::string, 12>
898+
ROCMToolChain::getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
899+
const std::string &GPUArch) const {
900+
auto Kind = llvm::AMDGPU::parseArchAMDGCN(GPUArch);
901+
const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
902+
903+
std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch);
904+
if (LibDeviceFile.empty()) {
905+
getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GPUArch;
906+
return {};
907+
}
908+
909+
// If --hip-device-lib is not set, add the default bitcode libraries.
910+
// TODO: There are way too many flags that change this. Do we need to check
911+
// them all?
912+
bool DAZ = DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
913+
options::OPT_fno_gpu_flush_denormals_to_zero,
914+
getDefaultDenormsAreZeroForTarget(Kind));
915+
bool FiniteOnly = DriverArgs.hasFlag(
916+
options::OPT_ffinite_math_only, options::OPT_fno_finite_math_only, false);
917+
bool UnsafeMathOpt =
918+
DriverArgs.hasFlag(options::OPT_funsafe_math_optimizations,
919+
options::OPT_fno_unsafe_math_optimizations, false);
920+
bool FastRelaxedMath = DriverArgs.hasFlag(options::OPT_ffast_math,
921+
options::OPT_fno_fast_math, false);
922+
bool CorrectSqrt = DriverArgs.hasFlag(
923+
options::OPT_fhip_fp32_correctly_rounded_divide_sqrt,
924+
options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt);
925+
bool Wave64 = isWave64(DriverArgs, Kind);
926+
927+
return RocmInstallation.getCommonBitcodeLibs(
928+
DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
929+
FastRelaxedMath, CorrectSqrt);
930+
}

clang/lib/Driver/ToolChains/AMDGPU.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
136136
addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
137137
llvm::opt::ArgStringList &CC1Args,
138138
Action::OffloadKind DeviceOffloadKind) const override;
139+
140+
// Returns a list of device library names shared by different languages
141+
llvm::SmallVector<std::string, 12>
142+
getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
143+
const std::string &GPUArch) const;
139144
};
140145

141146
} // end namespace toolchains

clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
#include "AMDGPUOpenMP.h"
1010
#include "AMDGPU.h"
1111
#include "CommonArgs.h"
12+
#include "ToolChains/ROCm.h"
1213
#include "clang/Basic/DiagnosticDriver.h"
1314
#include "clang/Driver/Compilation.h"
1415
#include "clang/Driver/Driver.h"
1516
#include "clang/Driver/DriverDiagnostic.h"
1617
#include "clang/Driver/InputInfo.h"
1718
#include "clang/Driver/Options.h"
19+
#include "llvm/ADT/STLExtras.h"
1820
#include "llvm/Support/FileSystem.h"
1921
#include "llvm/Support/FormatAdapters.h"
2022
#include "llvm/Support/FormatVariadic.h"
@@ -232,6 +234,27 @@ void AMDGPUOpenMPToolChain::addClangTargetOptions(
232234

233235
addOpenMPDeviceRTL(getDriver(), DriverArgs, CC1Args, BitcodeSuffix,
234236
getTriple());
237+
238+
if (!DriverArgs.hasArg(options::OPT_l))
239+
return;
240+
241+
auto Lm = DriverArgs.getAllArgValues(options::OPT_l);
242+
bool HasLibm = false;
243+
for (auto &Lib : Lm) {
244+
if (Lib == "m") {
245+
HasLibm = true;
246+
break;
247+
}
248+
}
249+
250+
if (HasLibm) {
251+
SmallVector<std::string, 12> BCLibs =
252+
getCommonDeviceLibNames(DriverArgs, GPUArch);
253+
llvm::for_each(BCLibs, [&](StringRef BCFile) {
254+
CC1Args.push_back("-mlink-builtin-bitcode");
255+
CC1Args.push_back(DriverArgs.MakeArgString(BCFile));
256+
});
257+
}
235258
}
236259

237260
llvm::opt::DerivedArgList *AMDGPUOpenMPToolChain::TranslateArgs(

clang/lib/Driver/ToolChains/HIP.cpp

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -395,35 +395,8 @@ HIPToolChain::getHIPDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
395395
}
396396
StringRef GpuArch = getGPUArch(DriverArgs);
397397
assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
398-
(void)GpuArch;
399-
auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
400-
const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
401-
402-
std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch);
403-
if (LibDeviceFile.empty()) {
404-
getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GpuArch;
405-
return {};
406-
}
407398

408399
// If --hip-device-lib is not set, add the default bitcode libraries.
409-
// TODO: There are way too many flags that change this. Do we need to check
410-
// them all?
411-
bool DAZ = DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
412-
options::OPT_fno_gpu_flush_denormals_to_zero,
413-
getDefaultDenormsAreZeroForTarget(Kind));
414-
bool FiniteOnly =
415-
DriverArgs.hasFlag(options::OPT_ffinite_math_only,
416-
options::OPT_fno_finite_math_only, false);
417-
bool UnsafeMathOpt =
418-
DriverArgs.hasFlag(options::OPT_funsafe_math_optimizations,
419-
options::OPT_fno_unsafe_math_optimizations, false);
420-
bool FastRelaxedMath = DriverArgs.hasFlag(
421-
options::OPT_ffast_math, options::OPT_fno_fast_math, false);
422-
bool CorrectSqrt = DriverArgs.hasFlag(
423-
options::OPT_fhip_fp32_correctly_rounded_divide_sqrt,
424-
options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt);
425-
bool Wave64 = isWave64(DriverArgs, Kind);
426-
427400
if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
428401
options::OPT_fno_gpu_sanitize, false)) {
429402
auto AsanRTL = RocmInstallation.getAsanRTLPath();
@@ -442,10 +415,8 @@ HIPToolChain::getHIPDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
442415
// Add the HIP specific bitcode library.
443416
BCLibs.push_back(RocmInstallation.getHIPPath().str());
444417

445-
// Add the generic set of libraries.
446-
BCLibs.append(RocmInstallation.getCommonBitcodeLibs(
447-
DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
448-
FastRelaxedMath, CorrectSqrt));
418+
// Add common device libraries like ocml etc.
419+
BCLibs.append(getCommonDeviceLibNames(DriverArgs, GpuArch.str()));
449420

450421
// Add instrument lib.
451422
auto InstLib =

clang/test/Driver/amdgpu-openmp-toolchain.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,6 @@
7474

7575
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -emit-llvm -S -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx803 -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-EMIT-LLVM-IR
7676
// CHECK-EMIT-LLVM-IR: clang{{.*}}"-cc1"{{.*}}"-triple" "amdgcn-amd-amdhsa"{{.*}}"-emit-llvm"
77+
78+
// RUN: env LIBRARY_PATH=%S/Inputs/hip_dev_lib %clang -### -target x86_64-pc-linux-gnu -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx803 -lm --rocm-device-lib-path=%S/Inputs/rocm/amdgcn/bitcode %s 2>&1 | FileCheck %s --check-prefix=CHECK-LIB-DEVICE
79+
// CHECK-LIB-DEVICE: clang{{.*}}"-cc1"{{.*}}"-triple" "amdgcn-amd-amdhsa"{{.*}}"-mlink-builtin-bitcode"{{.*}}libomptarget-amdgcn-gfx803.bc"{{.*}}"-mlink-builtin-bitcode"{{.*}}ocml.bc" "-mlink-builtin-bitcode"{{.*}}ockl.bc" "-mlink-builtin-bitcode"{{.*}}oclc_daz_opt_on.bc" "-mlink-builtin-bitcode"{{.*}}oclc_unsafe_math_off.bc" "-mlink-builtin-bitcode"{{.*}}oclc_finite_only_off.bc" "-mlink-builtin-bitcode"{{.*}}oclc_correctly_rounded_sqrt_on.bc" "-mlink-builtin-bitcode"{{.*}}oclc_wavefrontsize64_on.bc" "-mlink-builtin-bitcode"{{.*}}oclc_isa_version_803.bc"

0 commit comments

Comments
 (0)