Skip to content

Commit 65b6ba6

Browse files
committed
Link ockl.bc by default as common bitcode libs and asanrtl.bc when "-fsanitize=address" flag is mentioned.
Change-Id: I810e91cc85159df7e4c78f6476f30e70c51dc12a
1 parent fa5f943 commit 65b6ba6

File tree

8 files changed

+136
-48
lines changed

8 files changed

+136
-48
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def err_drv_no_rocm_device_lib : Error<
6868
"cannot find ROCm device library%select{| for %1|for ABI version %1}0; provide its path via "
6969
"'--rocm-path' or '--rocm-device-lib-path', or pass '-nogpulib' to build "
7070
"without ROCm device library">;
71+
def err_drv_no_asan_rt_lib : Error<
72+
"AMDGPU address sanitizer runtime library (asanrtl) not found. "
73+
"Please install ROCm device library which supports address sanitizer">;
7174
def err_drv_no_hip_runtime : Error<
7275
"cannot find HIP runtime; provide its path via '--rocm-path', or pass "
7376
"'-nogpuinc' to build without HIP runtime">;

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,7 @@ RocmInstallationDetector::getCommonBitcodeLibs(
916916
// offload-arch-specific bitcode files as is done for FORTRAN. Currently,
917917
// libomptarget-<offload-arch>.bc files is built by compiling headers with
918918
// __BUILD_MATH_BUILTINS_LIB__ turning static libm functions to extern.
919-
if (!isOpenMP)
920-
AddBCLib(getOCKLPath());
919+
AddBCLib(getOCKLPath());
921920
AddBCLib(getDenormalsAreZeroPath(DAZ));
922921
AddBCLib(getUnsafeMathPath(UnsafeMathOpt || FastRelaxedMath));
923922
AddBCLib(getFiniteOnlyPath(FiniteOnly || FastRelaxedMath));
@@ -931,6 +930,42 @@ RocmInstallationDetector::getCommonBitcodeLibs(
931930
return BCLibs;
932931
}
933932

933+
bool AMDGPUToolChain::shouldSkipSanitizeOption(
934+
const ToolChain &TC, const llvm::opt::ArgList &DriverArgs,
935+
StringRef TargetID, const llvm::opt::Arg *A) const {
936+
// For actions without targetID, do nothing.
937+
if (TargetID.empty())
938+
return false;
939+
Option O = A->getOption();
940+
if (!O.matches(options::OPT_fsanitize_EQ))
941+
return false;
942+
943+
if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
944+
options::OPT_fno_gpu_sanitize, true))
945+
return true;
946+
947+
auto &Diags = TC.getDriver().getDiags();
948+
949+
// For simplicity, we only allow -fsanitize=address
950+
SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
951+
if (K != SanitizerKind::Address)
952+
return true;
953+
954+
llvm::StringMap<bool> FeatureMap;
955+
auto OptionalGpuArch = parseTargetID(TC.getTriple(), TargetID, &FeatureMap);
956+
957+
assert(OptionalGpuArch && "Invalid Target ID");
958+
(void)OptionalGpuArch;
959+
auto Loc = FeatureMap.find("xnack");
960+
if (Loc == FeatureMap.end() || !Loc->second) {
961+
Diags.Report(
962+
clang::diag::warn_drv_unsupported_option_for_offload_arch_req_feature)
963+
<< A->getAsString(DriverArgs) << TargetID << "xnack+";
964+
return true;
965+
}
966+
return false;
967+
}
968+
934969
bool AMDGPUToolChain::shouldSkipArgument(const llvm::opt::Arg *A) const {
935970
Option O = A->getOption();
936971
if (O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie))

clang/lib/Driver/ToolChains/AMDGPU.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF {
100100
/// Needed for translating LTO options.
101101
const char *getDefaultLinker() const override { return "ld.lld"; }
102102

103+
/// Should skip Sanitize options
104+
bool shouldSkipSanitizeOption(const ToolChain &TC,
105+
const llvm::opt::ArgList &DriverArgs,
106+
StringRef TargetID,
107+
const llvm::opt::Arg *A) const;
108+
103109
/// Should skip argument.
104110
bool shouldSkipArgument(const llvm::opt::Arg *Arg) const;
105111

clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Driver/DriverDiagnostic.h"
1818
#include "clang/Driver/InputInfo.h"
1919
#include "clang/Driver/Options.h"
20+
#include "clang/Driver/SanitizerArgs.h"
2021
#include "clang/Driver/Tool.h"
2122
#include "llvm/ADT/STLExtras.h"
2223
#include "llvm/Support/FileSystem.h"
@@ -185,6 +186,19 @@ const char *AMDGCN::OpenMPLinker::constructLLVMLinkCommand(
185186
libpath = lib_debug_path;
186187

187188
llvm::SmallVector<std::string, 12> BCLibs;
189+
190+
if (Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
191+
true) &&
192+
AMDGPUOpenMPTC.getSanitizerArgs(Args).needsAsanRt()) {
193+
std::string AsanRTL(
194+
AMDGPUOpenMPTC.getRocmInstallationLoc().getAsanRTLPath());
195+
if (AsanRTL.empty()) {
196+
AMDGPUOpenMPTC.getDriver().Diag(diag::err_drv_no_asan_rt_lib);
197+
} else {
198+
BCLibs.push_back(AsanRTL);
199+
}
200+
}
201+
188202
if (Args.hasFlag(options::OPT_fopenmp_target_new_runtime,
189203
options::OPT_fno_openmp_target_new_runtime, false))
190204
BCLibs.push_back(Args.MakeArgString(libpath + "/libomptarget-new-amdgpu-" +
@@ -557,9 +571,11 @@ llvm::opt::DerivedArgList *AMDGPUOpenMPToolChain::TranslateArgs(
557571
const OptTable &Opts = getDriver().getOpts();
558572

559573
if (DeviceOffloadKind == Action::OFK_OpenMP) {
560-
for (Arg *A : Args)
561-
if (!llvm::is_contained(*DAL, A))
574+
for (Arg *A : Args) {
575+
if (!shouldSkipSanitizeOption(*this, Args, BoundArch, A) &&
576+
!llvm::is_contained(*DAL, A))
562577
DAL->append(A);
578+
}
563579

564580
std::string Arch = DAL->getLastArgValue(options::OPT_march_EQ).str();
565581
if (Arch.empty()) {

clang/lib/Driver/ToolChains/AMDGPUOpenMP.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUOpenMPToolChain final
131131

132132
SanitizerMask getSupportedSanitizers() const override;
133133

134+
RocmInstallationDetector getRocmInstallationLoc() const {
135+
return RocmInstallation;
136+
}
137+
134138
VersionTuple
135139
computeMSVCVersion(const Driver *D,
136140
const llvm::opt::ArgList &Args) const override;

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,6 @@ using namespace llvm::opt;
3636
#define NULL_FILE "/dev/null"
3737
#endif
3838

39-
static bool shouldSkipSanitizeOption(const ToolChain &TC,
40-
const llvm::opt::ArgList &DriverArgs,
41-
StringRef TargetID,
42-
const llvm::opt::Arg *A) {
43-
// For actions without targetID, do nothing.
44-
if (TargetID.empty())
45-
return false;
46-
Option O = A->getOption();
47-
if (!O.matches(options::OPT_fsanitize_EQ))
48-
return false;
49-
50-
if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
51-
options::OPT_fno_gpu_sanitize, true))
52-
return true;
53-
54-
auto &Diags = TC.getDriver().getDiags();
55-
56-
// For simplicity, we only allow -fsanitize=address
57-
SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
58-
if (K != SanitizerKind::Address)
59-
return true;
60-
61-
llvm::StringMap<bool> FeatureMap;
62-
auto OptionalGpuArch = parseTargetID(TC.getTriple(), TargetID, &FeatureMap);
63-
64-
assert(OptionalGpuArch && "Invalid Target ID");
65-
(void)OptionalGpuArch;
66-
auto Loc = FeatureMap.find("xnack");
67-
if (Loc == FeatureMap.end() || !Loc->second) {
68-
Diags.Report(
69-
clang::diag::warn_drv_unsupported_option_for_offload_arch_req_feature)
70-
<< A->getAsString(DriverArgs) << TargetID << "xnack+";
71-
return true;
72-
}
73-
return false;
74-
}
75-
7639
void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
7740
const InputInfoList &Inputs,
7841
const InputInfo &Output,
@@ -355,12 +318,7 @@ HIPAMDToolChain::getHIPDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
355318
getSanitizerArgs(DriverArgs).needsAsanRt()) {
356319
auto AsanRTL = RocmInstallation.getAsanRTLPath();
357320
if (AsanRTL.empty()) {
358-
unsigned DiagID = getDriver().getDiags().getCustomDiagID(
359-
DiagnosticsEngine::Error,
360-
"AMDGPU address sanitizer runtime library (asanrtl) is not found. "
361-
"Please install ROCm device library which supports address "
362-
"sanitizer");
363-
getDriver().Diag(DiagID);
321+
getDriver().Diag(diag::err_drv_no_asan_rt_lib);
364322
return {};
365323
} else
366324
BCLibs.push_back({AsanRTL.str(), /*ShouldInternalize=*/false});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
// RDC: {{"[^"]*clang[^"]*".* "-emit-llvm-bc".* "-fcuda-is-device".* "-mlink-bitcode-file" ".*asanrtl.bc".* "-mlink-builtin-bitcode" ".*hip.bc".* "-fsanitize=address".*}} "-o" "[[OUT:[^"]*.bc]]"
6363
// RDC-NOT: {{"[^"]*lld(\.exe){0,1}".*}} "[[OUT]]" {{".*asanrtl.bc" ".*hip.bc"}}
6464

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

6767
// XNACK-DAG: warning: ignoring '-fsanitize=leak' option as it is not currently supported for target 'amdgcn-amd-amdhsa'
6868
// XNACK-DAG: warning: ignoring '-fsanitize=address' option as it is not currently supported for offload arch 'gfx900:xnack-'. Use it with an offload arch containing 'xnack+' instead
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// REQUIRES: clang-driver
2+
// REQUIRES: x86-registered-target
3+
// REQUIRES: amdgpu-registered-target
4+
5+
// RUN: %clang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx908:xnack- -fsanitize=address -fgpu-sanitize %s 2>&1 \
6+
// RUN: | FileCheck -check-prefixes=XNACK-DAG,GPUSAN,XNACKNEG %s
7+
8+
// RUN: %clang -### -fopenmp --offload-arch=gfx908:xnack- -fsanitize=address -fgpu-sanitize %s 2>&1 \
9+
// RUN: | FileCheck -check-prefixes=XNACK-DAG,GPUSAN,XNACKNEG %s
10+
11+
// RUN: %clang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx908 -fsanitize=address -fgpu-sanitize %s 2>&1 \
12+
// RUN: | FileCheck -check-prefixes=XNACKNONE-DAG,GPUSAN,XNACKNONE %s
13+
14+
// RUN: %clang -### -fopenmp --offload-arch=gfx908 -fsanitize=address -fgpu-sanitize %s 2>&1 \
15+
// RUN: | FileCheck -check-prefixes=XNACKNONE-DAG,GPUSAN,XNACKNONE %s
16+
17+
// RUN: %clang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx908:xnack+ -fsanitize=address -fgpu-sanitize %s 2>&1 \
18+
// RUN: | FileCheck -check-prefixes=GPUSAN,XNACKPOS %s
19+
20+
// RUN: %clang -### -fopenmp --offload-arch=gfx908:xnack+ -fsanitize=address -fgpu-sanitize %s 2>&1 \
21+
// RUN: | FileCheck -check-prefixes=GPUSAN,XNACKPOS %s
22+
23+
// RUN: %clang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx908:xnack+ -fsanitize=address -fno-gpu-sanitize %s 2>&1 \
24+
// RUN: | FileCheck -check-prefixes=NOGPUSAN,XNACKPOS %s
25+
26+
// RUN: %clang -### -fopenmp --offload-arch=gfx908:xnack+ -fsanitize=address -fno-gpu-sanitize %s 2>&1 \
27+
// RUN: | FileCheck -check-prefixes=NOGPUSAN,XNACKPOS %s
28+
29+
// RUN: %clang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx908:xnack- -fsanitize=address -fno-gpu-sanitize %s 2>&1 \
30+
// RUN: | FileCheck -check-prefixes=NOGPUSAN,XNACKNEG %s
31+
32+
// RUN: %clang -### -fopenmp --offload-arch=gfx908:xnack- -fsanitize=address -fno-gpu-sanitize %s 2>&1 \
33+
// RUN: | FileCheck -check-prefixes=NOGPUSAN,XNACKNEG %s
34+
35+
// XNACK-DAG: warning: ignoring '-fsanitize=address' option as it is not currently supported for offload arch 'gfx908:xnack-'. Use it with an offload arch containing 'xnack+' instead
36+
// XNACKNONE-DAG: warning: ignoring '-fsanitize=address' option as it is not currently supported for offload arch 'gfx908'. Use it with an offload arch containing 'xnack+' instead
37+
38+
// GPUSAN: clang{{.*}}"-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-fopenmp"{{.*}}"-fsanitize=address"{{.*}}"-fopenmp-targets=amdgcn-amd-amdhsa"{{.*}}"-x" "c"{{.*}}
39+
// GPUSAN: clang{{.*}}"-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-fopenmp"{{.*}}"-fsanitize=address"{{.*}}"-fopenmp-targets=amdgcn-amd-amdhsa"{{.*}}"-x" "ir"{{.*}}
40+
// GPUSAN: clang{{.*}}"-cc1" "-triple" "amdgcn-amd-amdhsa" "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc"{{.*}}"-target-cpu" "gfx908"{{.*}}"-fcuda-is-device"{{.*}}"-fopenmp"{{.*}}"-fsanitize=address"{{.*}}
41+
// GPUSAN: llvm-link{{.*}}"--internalize" "--only-needed"{{.*}}"{{.*}}asanrtl.bc"{{.*}}"{{.*}}libomptarget-amdgcn-gfx908.bc"{{.*}}"-o" "{{.*}}.bc"
42+
43+
// NOGPUSAN: clang{{.*}}"-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-fopenmp"{{.*}}"-fsanitize=address"{{.*}}"-fopenmp-targets=amdgcn-amd-amdhsa"{{.*}}"-x" "c"{{.*}}
44+
// NOGPUSAN: clang{{.*}}"-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-fopenmp"{{.*}}"-fsanitize=address"{{.*}}"-fopenmp-targets=amdgcn-amd-amdhsa"{{.*}}"-x" "ir"{{.*}}
45+
// NOGPUSAN: clang{{.*}}"-cc1" "-triple" "amdgcn-amd-amdhsa" "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc"{{.*}}"-target-cpu" "gfx908"{{.*}}"-fcuda-is-device"{{.*}}"-fopenmp"{{.*}}
46+
// NOGPUSAN: llvm-link{{.*}}"--internalize" "--only-needed"{{.*}}"{{.*}}libomptarget-amdgcn-gfx908.bc"{{.*}}"-o" "{{.*}}.bc"
47+
48+
// XNACKNEG: opt{{.*}}"-mtriple=amdgcn-amd-amdhsa"{{.*}}"-mcpu=gfx908" "-mattr=-xnack"{{.*}}"-o" "{{.*}}.bc"
49+
// XNACKNEG: llc{{.*}}"-mtriple=amdgcn-amd-amdhsa"{{.*}}"-mcpu=gfx908" "-filetype=obj"{{.*}}"-mattr=-xnack"{{.*}}"-o" "{{.*}}.o"
50+
// XNACKNEG: lld{{.*}}"-flavor" "gnu" "--no-undefined" "-shared" "-o"{{.*}}.out{{.*}}"-plugin-opt=mcpu=gfx908" "-plugin-opt=-mattr=-xnack"
51+
// XNACKNEG: clang-offload-wrapper{{.*}}"-target" "x86_64-unknown-linux-gnu"{{.*}}"--offload-arch=gfx908:xnack-"{{.*}}
52+
53+
// XNACKNONE: opt{{.*}}"-mtriple=amdgcn-amd-amdhsa"{{.*}}"-mcpu=gfx908"{{.*}}"-o" "{{.*}}.bc"
54+
// XNACKNONE: llc{{.*}}"-mtriple=amdgcn-amd-amdhsa"{{.*}}"-mcpu=gfx908" "-filetype=obj"{{.*}}"-o" "{{.*}}.o"
55+
// XNACKNONE: lld{{.*}}"-flavor" "gnu" "--no-undefined" "-shared" "-o"{{.*}}.out{{.*}}"-plugin-opt=mcpu=gfx908"
56+
// XNACKNONE: clang-offload-wrapper{{.*}}"-target" "x86_64-unknown-linux-gnu"{{.*}}"--offload-arch=gfx908"{{.*}}
57+
58+
// XNACKPOS: opt{{.*}}"-mtriple=amdgcn-amd-amdhsa"{{.*}}"-mcpu=gfx908" "-mattr=+xnack"{{.*}}"-o" "{{.*}}.bc"
59+
// XNACKPOS: llc{{.*}}"-mtriple=amdgcn-amd-amdhsa"{{.*}}"-mcpu=gfx908" "-filetype=obj"{{.*}}"-mattr=+xnack"{{.*}}"-o" "{{.*}}.o"
60+
// XNACKPOS: lld{{.*}}"-flavor" "gnu" "--no-undefined" "-shared" "-o"{{.*}}.out{{.*}}"-plugin-opt=mcpu=gfx908" "-plugin-opt=-mattr=+xnack"
61+
// XNACKPOS: clang-offload-wrapper{{.*}}"-target" "x86_64-unknown-linux-gnu"{{.*}}"--offload-arch=gfx908:xnack+"{{.*}}
62+
63+
// RUN: %clang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx908:xnack+ -fsanitize=address -fgpu-sanitize -nogpuinc --rocm-path=%S/Inputs/rocm-invalid %s 2>&1 \
64+
// RUN: | FileCheck --check-prefix=FAIL %s
65+
66+
// FAIL: error: AMDGPU address sanitizer runtime library (asanrtl) not found. Please install ROCm device library which supports address sanitizer

0 commit comments

Comments
 (0)