Skip to content

Commit f331ba2

Browse files
AllanZynezhaomaosu
andauthored
[DeviceSanitizer] Support CPU Device & Static Local Memory (#12248)
UR: oneapi-src/unified-runtime#1210 --------- Co-authored-by: Maosu Zhao <[email protected]>
1 parent 153ccbe commit f331ba2

File tree

19 files changed

+1304
-36
lines changed

19 files changed

+1304
-36
lines changed

libdevice/atomic.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ __spirv_AtomicCompareExchange(int SPIR_GLOBAL *, __spv::Scope::Flag,
6262
__spv::MemorySemanticsMask::Flag,
6363
__spv::MemorySemanticsMask::Flag, int, int);
6464

65+
extern DEVICE_EXTERNAL int
66+
__spirv_AtomicCompareExchange(int *, __spv::Scope::Flag,
67+
__spv::MemorySemanticsMask::Flag,
68+
__spv::MemorySemanticsMask::Flag, int, int);
69+
6570
extern DEVICE_EXTERNAL int __spirv_AtomicLoad(const int SPIR_GLOBAL *,
6671
__spv::Scope::Flag,
6772
__spv::MemorySemanticsMask::Flag);
@@ -70,6 +75,10 @@ extern DEVICE_EXTERNAL void
7075
__spirv_AtomicStore(int SPIR_GLOBAL *, __spv::Scope::Flag,
7176
__spv::MemorySemanticsMask::Flag, int);
7277

78+
extern DEVICE_EXTERNAL void
79+
__spirv_AtomicStore(int *, __spv::Scope::Flag, __spv::MemorySemanticsMask::Flag,
80+
int);
81+
7382
/// Atomically set the value in *Ptr with Desired if and only if it is Expected
7483
/// Return the value which already was in *Ptr
7584
static inline int atomicCompareAndSet(SPIR_GLOBAL int *Ptr, int Desired,
@@ -80,6 +89,13 @@ static inline int atomicCompareAndSet(SPIR_GLOBAL int *Ptr, int Desired,
8089
__spv::MemorySemanticsMask::SequentiallyConsistent, Desired, Expected);
8190
}
8291

92+
static inline int atomicCompareAndSet(int *Ptr, int Desired, int Expected) {
93+
return __spirv_AtomicCompareExchange(
94+
Ptr, __spv::Scope::Device,
95+
__spv::MemorySemanticsMask::SequentiallyConsistent,
96+
__spv::MemorySemanticsMask::SequentiallyConsistent, Desired, Expected);
97+
}
98+
8399
static inline int atomicLoad(SPIR_GLOBAL int *Ptr) {
84100
return __spirv_AtomicLoad(Ptr, __spv::Scope::Device,
85101
__spv::MemorySemanticsMask::SequentiallyConsistent);
@@ -90,4 +106,9 @@ static inline void atomicStore(SPIR_GLOBAL int *Ptr, int V) {
90106
__spv::MemorySemanticsMask::SequentiallyConsistent, V);
91107
}
92108

109+
static inline void atomicStore(int *Ptr, int V) {
110+
__spirv_AtomicStore(Ptr, __spv::Scope::Device,
111+
__spv::MemorySemanticsMask::SequentiallyConsistent, V);
112+
}
113+
93114
#endif // __SPIR__

libdevice/cmake/modules/SYCLLibdevice.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ set(imf_obj_deps device_imf.hpp imf_half.hpp imf_bf16.hpp imf_rounding_op.hpp im
109109
set(itt_obj_deps device_itt.h spirv_vars.h device.h sycl-compiler)
110110
set(bfloat16_obj_deps sycl-headers sycl-compiler)
111111
if (NOT MSVC)
112-
set(sanitizer_obj_deps device.h sycl-compiler)
112+
set(sanitizer_obj_deps device.h atomic.hpp spirv_vars.h include/sanitizer_device_utils.hpp include/spir_global_var.hpp sycl-compiler)
113113
endif()
114114

115115
add_devicelib_obj(libsycl-itt-stubs SRC itt_stubs.cpp DEP ${itt_obj_deps})
@@ -126,9 +126,9 @@ add_devicelib_obj(libsycl-imf-fp64 SRC imf_wrapper_fp64.cpp DEP ${imf_obj_deps})
126126
add_devicelib_obj(libsycl-imf-bf16 SRC imf_wrapper_bf16.cpp DEP ${imf_obj_deps})
127127
add_devicelib_obj(libsycl-bfloat16 SRC bfloat16_wrapper.cpp DEP ${cmath_obj_deps} )
128128
if(MSVC)
129-
add_devicelib_obj(libsycl-msvc-math SRC msvc_math.cpp DEP ${cmath_obj_deps})
129+
add_devicelib_obj(libsycl-msvc-math SRC msvc_math.cpp DEP ${cmath_obj_deps})
130130
else()
131-
add_devicelib_obj(libsycl-sanitizer SRC sanitizer_utils.cpp DEP ${sanitizer_obj_deps})
131+
add_devicelib_obj(libsycl-sanitizer SRC sanitizer_utils.cpp DEP ${sanitizer_obj_deps} EXTRA_ARGS -fno-sycl-instrument-device-code)
132132
endif()
133133

134134
add_fallback_devicelib(libsycl-fallback-cassert SRC fallback-cassert.cpp DEP ${crt_obj_deps} EXTRA_ARGS -fno-sycl-instrument-device-code)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//==-- device-sanitizer-report.hpp - Structure and declaration for assert
2+
// support --==//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
#pragma once
10+
11+
// Treat this header as system one to workaround frontend's restriction
12+
#pragma clang system_header
13+
14+
#include <cinttypes>
15+
16+
enum class DeviceSanitizerErrorType : int32_t {
17+
UNKNOWN,
18+
OUT_OF_BOUND,
19+
MISALIGNED,
20+
USE_AFTER_FREE,
21+
OUT_OF_SHADOW_BOUND,
22+
};
23+
24+
enum class DeviceSanitizerMemoryType : int32_t {
25+
UNKNOWN,
26+
USM_DEVICE,
27+
USM_HOST,
28+
USM_SHARED,
29+
LOCAL,
30+
PRIVATE,
31+
MEM_BUFFER,
32+
};
33+
34+
// NOTE Layout of this structure should be aligned with the one in
35+
// sycl/include/sycl/detail/device_sanitizer_report.hpp
36+
struct DeviceSanitizerReport {
37+
int Flag = 0;
38+
39+
char File[256 + 1] = "";
40+
char Func[256 + 1] = "";
41+
42+
int32_t Line = 0;
43+
44+
uint64_t GID0 = 0;
45+
uint64_t GID1 = 0;
46+
uint64_t GID2 = 0;
47+
48+
uint64_t LID0 = 0;
49+
uint64_t LID1 = 0;
50+
uint64_t LID2 = 0;
51+
52+
bool IsWrite = false;
53+
uint32_t AccessSize = 0;
54+
DeviceSanitizerMemoryType MemoryType = DeviceSanitizerMemoryType::UNKNOWN;
55+
DeviceSanitizerErrorType ErrorType = DeviceSanitizerErrorType::UNKNOWN;
56+
57+
bool IsRecover = false;
58+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//==-- sanitizer_device_utils.hpp - Declaration for sanitizer global var ---==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#pragma once
9+
10+
#include "spir_global_var.hpp"
11+
#include <cstdint>
12+
13+
// Treat this header as system one to workaround frontend's restriction
14+
#pragma clang system_header
15+
16+
template <typename T>
17+
class
18+
#ifdef __SYCL_DEVICE_ONLY__
19+
[[__sycl_detail__::global_variable_allowed, __sycl_detail__::device_global,
20+
__sycl_detail__::add_ir_attributes_global_variable(
21+
"sycl-device-global-size", "sycl-device-image-scope", sizeof(T),
22+
nullptr)]]
23+
#endif
24+
DeviceGlobal {
25+
public:
26+
DeviceGlobal() = default;
27+
DeviceGlobal(const DeviceGlobal &) = delete;
28+
DeviceGlobal(const DeviceGlobal &&) = delete;
29+
DeviceGlobal &operator=(const DeviceGlobal &) = delete;
30+
DeviceGlobal &operator=(const DeviceGlobal &&) = delete;
31+
32+
DeviceGlobal &operator=(const T newValue) noexcept {
33+
val = newValue;
34+
return *this;
35+
}
36+
37+
operator T &() noexcept { return val; }
38+
39+
operator const T &() const noexcept { return val; }
40+
41+
T &get() noexcept { return val; }
42+
43+
const T &get() const noexcept { return val; }
44+
45+
private:
46+
T val;
47+
};
48+
49+
enum DeviceType : uintptr_t { UNKNOWN, CPU, GPU_PVC, GPU_DG2 };

libdevice/include/spir_global_var.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//==- spir_global_var.hpp - Declaration for device global variable support -==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
// Treat this header as system one to workaround frontend's restriction
12+
#pragma clang system_header
13+
14+
#ifndef SPIR_GLOBAL_VAR
15+
#ifdef __SYCL_DEVICE_ONLY__
16+
#define SPIR_GLOBAL_VAR __attribute__((sycl_global_var))
17+
#else
18+
#warning "SPIR_GLOBAL_VAR not defined in host mode. Defining as empty macro."
19+
#define SPIR_GLOBAL_VAR
20+
#endif
21+
#endif
22+
23+
#define __SYCL_GLOBAL__ __attribute__((opencl_global))
24+
#define __SYCL_LOCAL__ __attribute__((opencl_local))
25+
#define __SYCL_PRIVATE__ __attribute__((opencl_private))
26+
#define __SYCL_CONSTANT__ __attribute__((opencl_constant))

0 commit comments

Comments
 (0)