Skip to content

Commit 646d352

Browse files
[OpenMP][ASan] Enable ASan Instrumentation for AMDGPUOpenMPToolChain. (#124754)
Enable device code ASan instrumentation for openmp offload applications using option '-fsanitize=address'.
1 parent 409fa78 commit 646d352

File tree

4 files changed

+86
-13
lines changed

4 files changed

+86
-13
lines changed

clang/lib/Driver/ToolChains/AMDGPU.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,24 @@ class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
146146
getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
147147
const std::string &GPUArch,
148148
bool isOpenMP = false) const;
149+
149150
SanitizerMask getSupportedSanitizers() const override {
150151
return SanitizerKind::Address;
151152
}
153+
154+
void diagnoseUnsupportedSanitizers(const llvm::opt::ArgList &Args) const {
155+
if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
156+
true))
157+
return;
158+
auto &Diags = getDriver().getDiags();
159+
for (auto *A : Args.filtered(options::OPT_fsanitize_EQ)) {
160+
SanitizerMask K =
161+
parseSanitizerValue(A->getValue(), /*Allow Groups*/ false);
162+
if (K != SanitizerKind::Address)
163+
Diags.Report(clang::diag::warn_drv_unsupported_option_for_target)
164+
<< A->getAsString(Args) << getTriple().str();
165+
}
166+
}
152167
};
153168

154169
} // end namespace toolchains

clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ AMDGPUOpenMPToolChain::AMDGPUOpenMPToolChain(const Driver &D,
3737
// Lookup binaries into the driver directory, this is used to
3838
// discover the 'amdgpu-arch' executable.
3939
getProgramPaths().push_back(getDriver().Dir);
40+
// Diagnose unsupported sanitizer options only once.
41+
diagnoseUnsupportedSanitizers(Args);
4042
}
4143

4244
void AMDGPUOpenMPToolChain::addClangTargetOptions(
@@ -71,10 +73,10 @@ llvm::opt::DerivedArgList *AMDGPUOpenMPToolChain::TranslateArgs(
7173

7274
const OptTable &Opts = getDriver().getOpts();
7375

74-
for (Arg *A : Args) {
75-
if (!llvm::is_contained(*DAL, A))
76+
for (Arg *A : Args)
77+
if (!shouldSkipSanitizeOption(*this, Args, BoundArch, A) &&
78+
!llvm::is_contained(*DAL, A))
7679
DAL->append(A);
77-
}
7880

7981
if (!BoundArch.empty()) {
8082
DAL->eraseArg(options::OPT_march_EQ);

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,8 @@ HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
216216
// Lookup binaries into the driver directory, this is used to
217217
// discover the clang-offload-bundler executable.
218218
getProgramPaths().push_back(getDriver().Dir);
219-
220219
// Diagnose unsupported sanitizer options only once.
221-
if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
222-
true))
223-
return;
224-
for (auto *A : Args.filtered(options::OPT_fsanitize_EQ)) {
225-
SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
226-
if (K != SanitizerKind::Address)
227-
D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target)
228-
<< A->getAsString(Args) << getTriple().str();
229-
}
220+
diagnoseUnsupportedSanitizers(Args);
230221
}
231222

232223
void HIPAMDToolChain::addClangTargetOptions(
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// REQUIRES: x86-registered-target, amdgpu-registered-target
2+
3+
// Fail on invalid ROCm Path.
4+
// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908:xnack+ -fsanitize=address -fgpu-sanitize -nogpuinc --rocm-path=%S/Inputs/rocm-invalid %s 2>&1 \
5+
// RUN: | FileCheck --check-prefix=FAIL %s
6+
7+
// Enable multiple sanitizer's apart from ASan with invalid rocm-path.
8+
// RUN: not %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908:xnack+ -fsanitize=address -fsanitize=leak -fgpu-sanitize --rocm-path=%S/Inputs/rocm-invalid -nogpuinc %s 2>&1 \
9+
// RUN: | FileCheck --check-prefixes=NOTSUPPORTED,FAIL %s
10+
11+
// Memory, Leak, UndefinedBehaviour and Thread Sanitizer are not supported on AMDGPU.
12+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908:xnack+ -fsanitize=address -fsanitize=leak -fgpu-sanitize --rocm-path=%S/Inputs/rocm -nogpuinc %s 2>&1 \
13+
// RUN: | FileCheck --check-prefix=NOTSUPPORTED %s
14+
15+
// GPU ASan Enabled Test Cases
16+
// ASan enabled for amdgpu-arch [gfx908]
17+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908 -fsanitize=address -fgpu-sanitize --rocm-path=%S/Inputs/rocm %s 2>&1 \
18+
// RUN: | FileCheck -check-prefixes=NOXNACK,GPUSAN %s
19+
20+
// GPU ASan enabled for amdgpu-arch [gfx908:xnack-]
21+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908:xnack- -fsanitize=address -fgpu-sanitize --rocm-path=%S/Inputs/rocm %s 2>&1 \
22+
// RUN: | FileCheck -check-prefixes=XNACKNEG,GPUSAN %s
23+
24+
// GPU ASan enabled for amdgpu-arch [gfx908:xnack+]
25+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908:xnack+ -fsanitize=address -fgpu-sanitize --rocm-path=%S/Inputs/rocm %s 2>&1 \
26+
// RUN: | FileCheck -check-prefixes=GPUSAN %s
27+
28+
// ASan enabled for multiple amdgpu-arch [gfx908:xnack+,gfx900:xnack+]
29+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908:xnack+ --offload-arch=gfx900:xnack+ -fsanitize=address -fgpu-sanitize --rocm-path=%S/Inputs/rocm %s 2>&1 \
30+
// RUN: | FileCheck -check-prefixes=GPUSAN %s
31+
32+
// GPU ASan Disabled Test Cases
33+
// ASan disabled for amdgpu-arch [gfx908]
34+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908 -fsanitize=address -fno-gpu-sanitize --rocm-path=%S/Inputs/rocm %s 2>&1 \
35+
// RUN: | FileCheck -check-prefixes=NOGPUSAN %s
36+
37+
// GPU ASan disabled for amdgpu-arch [gfx908:xnack-]
38+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908:xnack- -fsanitize=address -fno-gpu-sanitize --rocm-path=%S/Inputs/rocm %s 2>&1 \
39+
// RUN: | FileCheck -check-prefixes=NOGPUSAN %s
40+
41+
// GPU ASan disabled for amdgpu-arch [gfx908:xnack+]
42+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908:xnack+ -fsanitize=address -fno-gpu-sanitize --rocm-path=%S/Inputs/rocm %s 2>&1 \
43+
// RUN: | FileCheck -check-prefixes=NOGPUSAN %s
44+
45+
// ASan disabled for amdgpu-arch [gfx908:xnack+,gfx900:xnack+]
46+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx908:xnack+ --offload-arch=gfx900:xnack+ -fsanitize=address -fno-gpu-sanitize --rocm-path=%S/Inputs/rocm %s 2>&1 \
47+
// RUN: | FileCheck -check-prefixes=NOGPUSAN %s
48+
49+
// FAIL-DAG: error: cannot find ROCm device library for ABI version 5; provide its path via '--rocm-path' or '--rocm-device-lib-path', or pass '-nogpulib' to build without ROCm device library
50+
// NOTSUPPORTED-DAG: warning: ignoring '-fsanitize=leak' option as it is not currently supported for target 'amdgcn-amd-amdhsa'
51+
52+
// NOXNACK: warning: ignoring '-fsanitize=address' option for offload arch 'gfx908' as it is not currently supported there. Use it with an offload arch containing 'xnack+' instead
53+
// XNACKNEG: warning: ignoring '-fsanitize=address' option for offload arch 'gfx908:xnack-' as it is not currently supported there. Use it with an offload arch containing 'xnack+' instead
54+
55+
// GPUSAN: {{"[^"]*clang[^"]*" "-cc1" "-triple" "x86_64-unknown-linux-gnu".* "-fopenmp".* "-fsanitize=address".* "-fopenmp-targets=amdgcn-amd-amdhsa".* "-x" "c".*}}
56+
// GPUSAN: {{"[^"]*clang[^"]*" "-cc1" "-triple" "amdgcn-amd-amdhsa" "-aux-triple" "x86_64-unknown-linux-gnu".* "-emit-llvm-bc".* "-target-cpu" "(gfx908|gfx900)".* "-fopenmp".* "-fsanitize=address".* "-x" "c".*}}
57+
// GPUSAN: {{"[^"]*clang-offload-packager[^"]*" "-o".* "--image=file=.*.bc,triple=amdgcn-amd-amdhsa,arch=gfx908(:xnack\-|:xnack\+)?,kind=openmp(,feature=(\-xnack|\+xnack))?"}}
58+
// GPUSAN: {{"[^"]*clang[^"]*" "-cc1" "-triple" "x86_64-unknown-linux-gnu".* "-fopenmp".* "-fsanitize=address".* "-fopenmp-targets=amdgcn-amd-amdhsa".* "-x" "ir".*}}
59+
// GPUSAN: {{"[^"]*clang-linker-wrapper[^"]*" "--host-triple=x86_64-unknown-linux-gnu" "--linker-path=[^"]*".* "--whole-archive" "[^"]*libclang_rt.asan_static.a".* "--whole-archive" "[^"]*libclang_rt.asan.a".*}}
60+
61+
// NOGPUSAN: {{"[^"]*clang[^"]*" "-cc1" "-triple" "x86_64-unknown-linux-gnu".* "-fopenmp".* "-fsanitize=address".* "-fopenmp-targets=amdgcn-amd-amdhsa".* "-x" "c".*}}
62+
// NOGPUSAN: {{"[^"]*clang[^"]*" "-cc1" "-triple" "amdgcn-amd-amdhsa" "-aux-triple" "x86_64-unknown-linux-gnu".* "-emit-llvm-bc".* "-target-cpu" "(gfx908|gfx900)".* "-fopenmp".* "-x" "c".*}}
63+
// NOGPUSAN: {{"[^"]*clang-offload-packager[^"]*" "-o".* "--image=file=.*.bc,triple=amdgcn-amd-amdhsa,arch=gfx908(:xnack\-|:xnack\+)?,kind=openmp(,feature=(\-xnack|\+xnack))?"}}
64+
// NOGPUSAN: {{"[^"]*clang[^"]*" "-cc1" "-triple" "x86_64-unknown-linux-gnu".* "-fopenmp".* "-fsanitize=address".* "-fopenmp-targets=amdgcn-amd-amdhsa".* "-x" "ir".*}}
65+
// NOGPUSAN: {{"[^"]*clang-linker-wrapper[^"]*" "--host-triple=x86_64-unknown-linux-gnu" "--linker-path=[^"]*".* "--whole-archive" "[^"]*libclang_rt.asan_static.a".* "--whole-archive" "[^"]*libclang_rt.asan.a".*}}

0 commit comments

Comments
 (0)