Skip to content

Commit 0c62a28

Browse files
committed
[Driver][ASan] Refactor Clang-Driver "Sanitizer Bitcode" linking.
ASan bitcode linking is currently available for HIPAMD,OpenMP and OpenCL. Moving sanitizer specific common parts of logic to appropriate API's so as to reduce code redundancy and maintainability.
1 parent 65df99c commit 0c62a28

File tree

8 files changed

+71
-58
lines changed

8 files changed

+71
-58
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def err_drv_no_rocm_device_lib : Error<
7070
"cannot find ROCm device library%select{| for %1| for ABI version %1}0; provide its path via "
7171
"'--rocm-path' or '--rocm-device-lib-path', or pass '-nogpulib' to build "
7272
"without ROCm device library">;
73+
def err_drv_no_asan_rt_lib
74+
: Error<"AMDGPU address sanitizer runtime library (asanrtl) is not found. "
75+
"Please install ROCm device library which supports address "
76+
"sanitizer">;
7377
def err_drv_no_hip_runtime : Error<
7478
"cannot find HIP runtime; provide its path via '--rocm-path', or pass "
7579
"'-nogpuinc' to build without HIP runtime">;

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,12 @@ void ROCMToolChain::addClangTargetOptions(
946946
StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(CanonArch);
947947
auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
948948
getAMDGPUCodeObjectVersion(getDriver(), DriverArgs));
949+
std::tuple<bool, const SanitizerArgs> GPUSan(
950+
DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
951+
options::OPT_fno_gpu_sanitize, true),
952+
getSanitizerArgs(DriverArgs));
949953
if (!RocmInstallation->checkCommonBitcodeLibs(CanonArch, LibDeviceFile,
950-
ABIVer))
954+
ABIVer, GPUSan))
951955
return;
952956

953957
bool Wave64 = isWave64(DriverArgs, Kind);
@@ -965,28 +969,32 @@ void ROCMToolChain::addClangTargetOptions(
965969
DriverArgs.hasArg(options::OPT_cl_fp32_correctly_rounded_divide_sqrt);
966970

967971
// Add the OpenCL specific bitcode library.
968-
llvm::SmallVector<std::string, 12> BCLibs;
969-
BCLibs.push_back(RocmInstallation->getOpenCLPath().str());
972+
llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
973+
BCLibs.emplace_back(RocmInstallation->getOpenCLPath().str());
970974

971975
// Add the generic set of libraries.
972976
BCLibs.append(RocmInstallation->getCommonBitcodeLibs(
973977
DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
974-
FastRelaxedMath, CorrectSqrt, ABIVer, false));
978+
FastRelaxedMath, CorrectSqrt, ABIVer, GPUSan, false));
975979

976-
if (getSanitizerArgs(DriverArgs).needsAsanRt()) {
977-
CC1Args.push_back("-mlink-bitcode-file");
978-
CC1Args.push_back(
979-
DriverArgs.MakeArgString(RocmInstallation->getAsanRTLPath()));
980-
}
981-
for (StringRef BCFile : BCLibs) {
982-
CC1Args.push_back("-mlink-builtin-bitcode");
980+
for (auto [BCFile, Internalize] : BCLibs) {
981+
if (Internalize)
982+
CC1Args.push_back("-mlink-builtin-bitcode");
983+
else
984+
CC1Args.push_back("-mlink-bitcode-file");
983985
CC1Args.push_back(DriverArgs.MakeArgString(BCFile));
984986
}
985987
}
986988

987989
bool RocmInstallationDetector::checkCommonBitcodeLibs(
988-
StringRef GPUArch, StringRef LibDeviceFile,
989-
DeviceLibABIVersion ABIVer) const {
990+
StringRef GPUArch, StringRef LibDeviceFile, DeviceLibABIVersion ABIVer,
991+
std::tuple<bool, const SanitizerArgs> &GPUSan) const {
992+
if (std::get<bool>(GPUSan))
993+
if (std::get<const SanitizerArgs>(GPUSan).needsAsanRt() &&
994+
getAsanRTLPath().empty()) {
995+
D.Diag(diag::err_drv_no_asan_rt_lib);
996+
return false;
997+
}
990998
if (!hasDeviceLibrary()) {
991999
D.Diag(diag::err_drv_no_rocm_device_lib) << 0;
9921000
return false;
@@ -1002,18 +1010,35 @@ bool RocmInstallationDetector::checkCommonBitcodeLibs(
10021010
return true;
10031011
}
10041012

1005-
llvm::SmallVector<std::string, 12>
1013+
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
10061014
RocmInstallationDetector::getCommonBitcodeLibs(
10071015
const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile, bool Wave64,
10081016
bool DAZ, bool FiniteOnly, bool UnsafeMathOpt, bool FastRelaxedMath,
1009-
bool CorrectSqrt, DeviceLibABIVersion ABIVer, bool isOpenMP = false) const {
1010-
llvm::SmallVector<std::string, 12> BCLibs;
1011-
1012-
auto AddBCLib = [&](StringRef BCFile) { BCLibs.push_back(BCFile.str()); };
1017+
bool CorrectSqrt, DeviceLibABIVersion ABIVer,
1018+
const std::tuple<bool, const SanitizerArgs> &GPUSan,
1019+
bool isOpenMP = false) const {
1020+
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> BCLibs;
1021+
1022+
auto GPUSanEnabled = [GPUSan]() { return std::get<bool>(GPUSan); };
1023+
auto AddBCLib = [&](ToolChain::BitCodeLibraryInfo BCLib,
1024+
bool Internalize = true) {
1025+
BCLib.ShouldInternalize = Internalize;
1026+
BCLibs.emplace_back(BCLib);
1027+
};
1028+
auto AddSanBCLibs = [&]() {
1029+
auto SanArgs = std::get<const SanitizerArgs>(GPUSan);
1030+
if (GPUSanEnabled()) {
1031+
if (SanArgs.needsAsanRt())
1032+
AddBCLib(getAsanRTLPath(), false);
1033+
}
1034+
};
10131035

1036+
AddSanBCLibs();
10141037
AddBCLib(getOCMLPath());
10151038
if (!isOpenMP)
10161039
AddBCLib(getOCKLPath());
1040+
else if (GPUSanEnabled() && isOpenMP)
1041+
AddBCLib(getOCKLPath(), false);
10171042
AddBCLib(getDenormalsAreZeroPath(DAZ));
10181043
AddBCLib(getUnsafeMathPath(UnsafeMathOpt || FastRelaxedMath));
10191044
AddBCLib(getFiniteOnlyPath(FiniteOnly || FastRelaxedMath));
@@ -1027,7 +1052,7 @@ RocmInstallationDetector::getCommonBitcodeLibs(
10271052
return BCLibs;
10281053
}
10291054

1030-
llvm::SmallVector<std::string, 12>
1055+
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
10311056
ROCMToolChain::getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
10321057
const std::string &GPUArch,
10331058
bool isOpenMP) const {
@@ -1037,8 +1062,12 @@ ROCMToolChain::getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
10371062
StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(CanonArch);
10381063
auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
10391064
getAMDGPUCodeObjectVersion(getDriver(), DriverArgs));
1065+
std::tuple<bool, const SanitizerArgs> GPUSan(
1066+
DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
1067+
options::OPT_fno_gpu_sanitize, true),
1068+
getSanitizerArgs(DriverArgs));
10401069
if (!RocmInstallation->checkCommonBitcodeLibs(CanonArch, LibDeviceFile,
1041-
ABIVer))
1070+
ABIVer, GPUSan))
10421071
return {};
10431072

10441073
// If --hip-device-lib is not set, add the default bitcode libraries.
@@ -1061,7 +1090,7 @@ ROCMToolChain::getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
10611090

10621091
return RocmInstallation->getCommonBitcodeLibs(
10631092
DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
1064-
FastRelaxedMath, CorrectSqrt, ABIVer, isOpenMP);
1093+
FastRelaxedMath, CorrectSqrt, ABIVer, GPUSan, isOpenMP);
10651094
}
10661095

10671096
bool AMDGPUToolChain::shouldSkipSanitizeOption(

clang/lib/Driver/ToolChains/AMDGPU.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
142142
Action::OffloadKind DeviceOffloadKind) const override;
143143

144144
// Returns a list of device library names shared by different languages
145-
llvm::SmallVector<std::string, 12>
145+
llvm::SmallVector<BitCodeLibraryInfo, 12>
146146
getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
147147
const std::string &GPUArch,
148148
bool isOpenMP = false) const;

clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "AMDGPUOpenMP.h"
1010
#include "AMDGPU.h"
1111
#include "CommonArgs.h"
12-
#include "ToolChains/ROCm.h"
12+
#include "ROCm.h"
1313
#include "clang/Basic/DiagnosticDriver.h"
1414
#include "clang/Driver/Compilation.h"
1515
#include "clang/Driver/Driver.h"
@@ -159,11 +159,6 @@ AMDGPUOpenMPToolChain::getDeviceLibs(const llvm::opt::ArgList &Args) const {
159159
if (Args.hasArg(options::OPT_nogpulib))
160160
return {};
161161

162-
if (!RocmInstallation->hasDeviceLibrary()) {
163-
getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
164-
return {};
165-
}
166-
167162
StringRef GpuArch = getProcessorFromTargetID(
168163
getTriple(), Args.getLastArgValue(options::OPT_march_EQ));
169164

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -396,28 +396,11 @@ HIPAMDToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
396396
StringRef GpuArch = getGPUArch(DriverArgs);
397397
assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
398398

399-
// If --hip-device-lib is not set, add the default bitcode libraries.
400-
if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
401-
options::OPT_fno_gpu_sanitize, true) &&
402-
getSanitizerArgs(DriverArgs).needsAsanRt()) {
403-
auto AsanRTL = RocmInstallation->getAsanRTLPath();
404-
if (AsanRTL.empty()) {
405-
unsigned DiagID = getDriver().getDiags().getCustomDiagID(
406-
DiagnosticsEngine::Error,
407-
"AMDGPU address sanitizer runtime library (asanrtl) is not found. "
408-
"Please install ROCm device library which supports address "
409-
"sanitizer");
410-
getDriver().Diag(DiagID);
411-
return {};
412-
} else
413-
BCLibs.emplace_back(AsanRTL, /*ShouldInternalize=*/false);
414-
}
415-
416399
// Add the HIP specific bitcode library.
417400
BCLibs.push_back(RocmInstallation->getHIPPath());
418401

419402
// Add common device libraries like ocml etc.
420-
for (StringRef N : getCommonDeviceLibNames(DriverArgs, GpuArch.str()))
403+
for (auto N : getCommonDeviceLibNames(DriverArgs, GpuArch.str()))
421404
BCLibs.emplace_back(N);
422405

423406
// Add instrument lib.

clang/lib/Driver/ToolChains/ROCm.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "clang/Basic/LLVM.h"
1414
#include "clang/Driver/Driver.h"
1515
#include "clang/Driver/Options.h"
16+
#include "clang/Driver/SanitizerArgs.h"
1617
#include "llvm/ADT/SmallString.h"
1718
#include "llvm/ADT/StringMap.h"
1819
#include "llvm/Option/ArgList.h"
@@ -173,16 +174,17 @@ class RocmInstallationDetector {
173174

174175
/// Get file paths of default bitcode libraries common to AMDGPU based
175176
/// toolchains.
176-
llvm::SmallVector<std::string, 12>
177-
getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs,
178-
StringRef LibDeviceFile, bool Wave64, bool DAZ,
179-
bool FiniteOnly, bool UnsafeMathOpt,
180-
bool FastRelaxedMath, bool CorrectSqrt,
181-
DeviceLibABIVersion ABIVer, bool isOpenMP) const;
177+
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> getCommonBitcodeLibs(
178+
const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
179+
bool Wave64, bool DAZ, bool FiniteOnly, bool UnsafeMathOpt,
180+
bool FastRelaxedMath, bool CorrectSqrt, DeviceLibABIVersion ABIVer,
181+
const std::tuple<bool, const SanitizerArgs> &GPUSan, bool isOpenMP) const;
182182
/// Check file paths of default bitcode libraries common to AMDGPU based
183183
/// toolchains. \returns false if there are invalid or missing files.
184-
bool checkCommonBitcodeLibs(StringRef GPUArch, StringRef LibDeviceFile,
185-
DeviceLibABIVersion ABIVer) const;
184+
bool
185+
checkCommonBitcodeLibs(StringRef GPUArch, StringRef LibDeviceFile,
186+
DeviceLibABIVersion ABIVer,
187+
std::tuple<bool, const SanitizerArgs> &GPUSan) const;
186188

187189
/// Check whether we detected a valid HIP runtime.
188190
bool hasHIPRuntime() const { return HasHIPRuntime; }

clang/test/Driver/hip-sanitize-options.hip

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@
5252
// CHECK-NOT: {{"[^"]*lld(\.exe){0,1}".* ".*hip.bc"}}
5353
// CHECK: {{"[^"]*clang[^"]*".* "-triple" "x86_64-unknown-linux-gnu".* "-fsanitize=address"}}
5454

55-
// NORDC: {{"[^"]*clang[^"]*".* "-emit-obj".* "-fcuda-is-device".* "-mlink-bitcode-file" ".*asanrtl.bc".* "-mlink-builtin-bitcode" ".*hip.bc".* "-fsanitize=address".*}} "-o" "[[OUT:[^"]*.o]]"
55+
// NORDC: {{"[^"]*clang[^"]*".* "-emit-obj".* "-fcuda-is-device".* "-mlink-builtin-bitcode" ".*hip.bc".* "-mlink-bitcode-file" ".*asanrtl.bc".* "-fsanitize=address".*}} "-o" "[[OUT:[^"]*.o]]"
5656
// NORDC-NOT: {{"[^"]*lld(\.exe){0,1}".*}} "[[OUT]]" {{".*asanrtl.bc" ".*hip.bc"}}
5757
// NORDC: {{"[^"]*clang[^"]*".* "-triple" "x86_64-unknown-linux-gnu".* "-fsanitize=address"}}
5858

5959
// RDC: {{"[^"]*clang[^"]*".* "-triple" "x86_64-unknown-linux-gnu".* "-fsanitize=address"}}
60-
// RDC: {{"[^"]*clang[^"]*".* "-emit-llvm-bc".* "-fcuda-is-device".* "-mlink-bitcode-file" ".*asanrtl.bc".* "-mlink-builtin-bitcode" ".*hip.bc".* "-fsanitize=address".*}} "-o" "[[OUT:[^"]*.bc]]"
60+
// RDC: {{"[^"]*clang[^"]*".* "-emit-llvm-bc".* "-fcuda-is-device".* "-mlink-builtin-bitcode" ".*hip.bc".* "-mlink-bitcode-file" ".*asanrtl.bc".* "-fsanitize=address".*}} "-o" "[[OUT:[^"]*.bc]]"
6161
// RDC-NOT: {{"[^"]*lld(\.exe){0,1}".*}} "[[OUT]]" {{".*asanrtl.bc" ".*hip.bc"}}
6262

63-
// FAIL: AMDGPU address sanitizer runtime library (asanrtl) is not found. Please install ROCm device library which supports address sanitizer
63+
// FAIL: error: AMDGPU address sanitizer runtime library (asanrtl) is not found. Please install ROCm device library which supports address sanitizer
6464

6565
// XNACK-DAG: warning: ignoring '-fsanitize=leak' option as it is not currently supported for target 'amdgcn-amd-amdhsa'
6666
// XNACK-DAG: warning: ignoring '-fsanitize=address' option for offload arch 'gfx900:xnack-' as it is not currently supported there. Use it with an offload arch containing 'xnack+' instead

clang/test/Driver/rocm-device-libs.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@
145145
// RUN: 2>&1 | FileCheck --check-prefixes=NOASAN %s
146146

147147
// COMMON: "-triple" "amdgcn-amd-amdhsa"
148-
// ASAN-SAME: "-mlink-bitcode-file" "{{.*}}/amdgcn/bitcode/asanrtl.bc"
149148
// COMMON-SAME: "-mlink-builtin-bitcode" "{{.*}}/amdgcn/bitcode/opencl.bc"
149+
// ASAN-SAME: "-mlink-bitcode-file" "{{.*}}/amdgcn/bitcode/asanrtl.bc"
150150
// COMMON-SAME: "-mlink-builtin-bitcode" "{{.*}}/amdgcn/bitcode/ocml.bc"
151151
// COMMON-SAME: "-mlink-builtin-bitcode" "{{.*}}/amdgcn/bitcode/ockl.bc"
152152

0 commit comments

Comments
 (0)