Skip to content

[DeviceMSAN] Enable origin tracking #18693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 28 commits into
base: sycl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions clang/lib/Driver/SanitizerArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,12 @@ void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,

CmdArgs.push_back("-mllvm");
CmdArgs.push_back("-msan-poison-stack-with-call=1");

if (MsanTrackOrigins) {
CmdArgs.push_back("-mllvm");
CmdArgs.push_back(Args.MakeArgString("-msan-track-origins=" +
Twine(MsanTrackOrigins)));
}
} else if (Sanitizers.has(SanitizerKind::Thread)) {
CmdArgs.push_back("-fsanitize=thread");
// The tsan function entry/exit builtins are used to record stack
Expand Down
9 changes: 9 additions & 0 deletions libdevice/atomic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ extern DEVICE_EXTERNAL void __spirv_AtomicStore(int *, int, int, int) noexcept;
extern DEVICE_EXTERNAL int __spirv_AtomicIAdd(SPIR_GLOBAL int *, int, int,
int) noexcept;

extern DEVICE_EXTERNAL int __spirv_AtomicIAdd(SPIR_GLOBAL unsigned int *, int,
int, int) noexcept;

/// Atomically set the value in *Ptr with Desired if and only if it is Expected
/// Return the value which already was in *Ptr
static inline int atomicCompareAndSet(SPIR_GLOBAL int *Ptr, int Desired,
Expand Down Expand Up @@ -113,4 +116,10 @@ static inline int atomicAdd(SPIR_GLOBAL int *Ptr, int V) {
V);
}

static inline int atomicAdd(SPIR_GLOBAL unsigned int *Ptr, int V) {
return __spirv_AtomicIAdd(Ptr, __spv::Scope::Device,
__spv::MemorySemanticsMask::SequentiallyConsistent,
V);
}

#endif // __SPIR__ || __SPIRV__
6 changes: 3 additions & 3 deletions libdevice/cmake/modules/SYCLLibdevice.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ if (NOT MSVC AND UR_SANITIZER_INCLUDE_DIR)
include/asan_rtl.hpp
include/sanitizer_defs.hpp
include/spir_global_var.hpp
include/group_utils.hpp
include/sanitizer_utils.hpp
${sycl-compiler_deps})

set(sanitizer_generic_compile_opts ${compile_opts}
Expand Down Expand Up @@ -305,7 +305,7 @@ if (NOT MSVC AND UR_SANITIZER_INCLUDE_DIR)
include/msan_rtl.hpp
include/sanitizer_defs.hpp
include/spir_global_var.hpp
include/group_utils.hpp
include/sanitizer_utils.hpp
sycl-compiler)

set(tsan_obj_deps
Expand All @@ -314,7 +314,7 @@ if (NOT MSVC AND UR_SANITIZER_INCLUDE_DIR)
include/tsan_rtl.hpp
include/sanitizer_defs.hpp
include/spir_global_var.hpp
include/group_utils.hpp
include/sanitizer_utils.hpp
sycl-compiler)
endif()

Expand Down
1 change: 1 addition & 0 deletions libdevice/include/asan_rtl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include "sanitizer_defs.hpp"
#include "sanitizer_utils.hpp"
#include "spir_global_var.hpp"
#include <cstdint>

Expand Down
1 change: 1 addition & 0 deletions libdevice/include/msan_rtl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include "sanitizer_defs.hpp"
#include "sanitizer_utils.hpp"
#include "spir_global_var.hpp"

// Treat this header as system one to workaround frontend's restriction
Expand Down
16 changes: 5 additions & 11 deletions libdevice/include/sanitizer_defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#pragma once

#include "atomic.hpp"
#include "group_utils.hpp"
#include "spir_global_var.hpp"
#include <cstdint>

Expand All @@ -30,6 +29,11 @@ enum ADDRESS_SPACE : uint32_t {
ADDRESS_SPACE_GENERIC = 4,
};

#define __SYCL_GLOBAL__ __attribute__((opencl_global))
#define __SYCL_LOCAL__ __attribute__((opencl_local))
#define __SYCL_PRIVATE__ __attribute__((opencl_private))
#define __SYCL_CONSTANT__ __attribute__((opencl_constant))

#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#define NORETURN __declspec(noreturn)
Expand All @@ -56,14 +60,4 @@ __spirv_ControlBarrier(int32_t Execution, int32_t Memory,

extern "C" SYCL_EXTERNAL void __devicelib_exit();

__SYCL_GLOBAL__ void *ToGlobal(void *ptr) {
return __spirv_GenericCastToPtrExplicit_ToGlobal(ptr, 5);
}
__SYCL_LOCAL__ void *ToLocal(void *ptr) {
return __spirv_GenericCastToPtrExplicit_ToLocal(ptr, 4);
}
__SYCL_PRIVATE__ void *ToPrivate(void *ptr) {
return __spirv_GenericCastToPtrExplicit_ToPrivate(ptr, 7);
}

#endif // __SPIR__ || __SPIRV__
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
//===----------------------------------------------------------------------===//
#pragma once

#include "sanitizer_defs.hpp"
#include "spirv_vars.h"

#if defined(__SPIR__) || defined(__SPIRV__)

static inline size_t WorkGroupLinearId() {
inline size_t WorkGroupLinearId() {
return __spirv_BuiltInWorkgroupId.x * __spirv_BuiltInNumWorkgroups.y *
__spirv_BuiltInNumWorkgroups.z +
__spirv_BuiltInWorkgroupId.y * __spirv_BuiltInNumWorkgroups.z +
Expand All @@ -26,15 +27,54 @@ static inline size_t LocalLinearId() {
}

// For GPU device, each sub group is a hardware thread
static inline size_t SubGroupLinearId() {
inline size_t SubGroupLinearId() {
return __spirv_BuiltInGlobalLinearId / __spirv_BuiltInSubgroupSize;
}

static inline void SubGroupBarrier() {
inline void SubGroupBarrier() {
__spirv_ControlBarrier(__spv::Scope::Subgroup, __spv::Scope::Subgroup,
__spv::MemorySemanticsMask::SequentiallyConsistent |
__spv::MemorySemanticsMask::CrossWorkgroupMemory |
__spv::MemorySemanticsMask::WorkgroupMemory);
}

inline __SYCL_GLOBAL__ void *ToGlobal(void *ptr) {
return __spirv_GenericCastToPtrExplicit_ToGlobal(ptr, 5);
}
inline __SYCL_LOCAL__ void *ToLocal(void *ptr) {
return __spirv_GenericCastToPtrExplicit_ToLocal(ptr, 4);
}
inline __SYCL_PRIVATE__ void *ToPrivate(void *ptr) {
return __spirv_GenericCastToPtrExplicit_ToPrivate(ptr, 7);
}

template <typename T> T Memset(T ptr, int value, size_t size) {
for (size_t i = 0; i < size; i++) {
ptr[i] = value;
}
return ptr;
}

template <typename DstT, typename SrcT>
DstT Memcpy(DstT dst, SrcT src, size_t size) {
for (size_t i = 0; i < size; i++) {
dst[i] = src[i];
}
return dst;
}

template <typename DstT, typename SrcT>
DstT Memmove(DstT dst, SrcT src, size_t size) {
if ((uptr)dst < (uptr)src) {
for (size_t i = 0; i < size; i++) {
dst[i] = src[i];
}
} else {
for (size_t i = size; i > 0; i--) {
dst[i - 1] = src[i - 1];
}
}
return dst;
}

#endif // __SPIR__ || __SPIRV__
5 changes: 0 additions & 5 deletions libdevice/include/spir_global_var.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ class
T val;
};

#define __SYCL_GLOBAL__ __attribute__((opencl_global))
#define __SYCL_LOCAL__ __attribute__((opencl_local))
#define __SYCL_PRIVATE__ __attribute__((opencl_private))
#define __SYCL_CONSTANT__ __attribute__((opencl_constant))

#ifndef SPIR_GLOBAL_VAR
#ifdef __SYCL_DEVICE_ONLY__
#define SPIR_GLOBAL_VAR __attribute__((sycl_global_var))
Expand Down
1 change: 1 addition & 0 deletions libdevice/include/tsan_rtl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include "sanitizer_defs.hpp"
#include "sanitizer_utils.hpp"
#include "spir_global_var.hpp"
#include "tsan/tsan_libdevice.hpp"

Expand Down
Loading
Loading