Skip to content

Commit a6cb3f3

Browse files
authored
[SYCL][Driver] Link sanitize devicelib when passing ASAN flag via -Xsycl-target-frontend (#12356)
We support enable asan for device compiler only via -Xsycl-target-frontend, in this case, sanitize devicelib should be linked. --------- Signed-off-by: jinge90 <[email protected]>
1 parent 340858b commit a6cb3f3

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/Support/CommandLine.h"
1818
#include "llvm/Support/FileSystem.h"
1919
#include "llvm/Support/Path.h"
20+
#include <algorithm>
2021
#include <sstream>
2122

2223
using namespace clang::driver;
@@ -311,6 +312,21 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
311312
if (SanitizeVal == "address")
312313
addLibraries(SYCLDeviceSanitizerLibs);
313314
}
315+
} else {
316+
// User can pass -fsanitize=address to device compiler via
317+
// -Xsycl-target-frontend, sanitize device library must be
318+
// linked with user's device image if so.
319+
bool IsDeviceAsanEnabled = false;
320+
auto SyclFEArg = Args.getAllArgValues(options::OPT_Xsycl_frontend);
321+
IsDeviceAsanEnabled = (std::count(SyclFEArg.begin(), SyclFEArg.end(),
322+
"-fsanitize=address") > 0);
323+
if (!IsDeviceAsanEnabled) {
324+
auto SyclFEArgEq = Args.getAllArgValues(options::OPT_Xsycl_frontend_EQ);
325+
IsDeviceAsanEnabled = (std::count(SyclFEArgEq.begin(), SyclFEArgEq.end(),
326+
"-fsanitize=address") > 0);
327+
}
328+
if (IsDeviceAsanEnabled)
329+
addLibraries(SYCLDeviceSanitizerLibs);
314330
}
315331
#endif
316332
return LibraryList;

clang/test/Driver/sycl-device-lib.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@
216216
/// test behavior of libsycl-sanitizer.o linking when -fsanitize=address is available
217217
// RUN: %clangxx -fsycl %s --sysroot=%S/Inputs/SYCL -fsanitize=address -### 2>&1 \
218218
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_SANITIZER
219+
// RUN: %clangxx -fsycl %s --sysroot=%S/Inputs/SYCL -Xsycl-target-frontend -fsanitize=address -### 2>&1 \
220+
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_SANITIZER
221+
// RUN: %clangxx -fsycl %s --sysroot=%S/Inputs/SYCL -Xsycl-target-frontend=spir64 -fsanitize=address -### 2>&1 \
222+
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_SANITIZER
219223
// SYCL_DEVICE_LIB_SANITIZER: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-crt.o" "-output={{.*}}libsycl-crt-{{.*}}.o" "-unbundle"
220224
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-complex.o" "-output={{.*}}libsycl-complex-{{.*}}.o" "-unbundle"
221225
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-complex-fp64.o" "-output={{.*}}libsycl-complex-fp64-{{.*}}.o" "-unbundle"

0 commit comments

Comments
 (0)