-
Notifications
You must be signed in to change notification settings - Fork 828
Expand file tree
/
Copy pathasan_buffer.cpp
More file actions
207 lines (182 loc) · 6.8 KB
/
asan_buffer.cpp
File metadata and controls
207 lines (182 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
*
*
* Part of the LLVM Project, under the Apache License v2.0 with LLVM
* Exceptions. See https://llvm.org/LICENSE.txt for license information.
*
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
* @file asan_buffer.cpp
*
*/
#include "asan_buffer.hpp"
#include "asan_interceptor.hpp"
#include "sanitizer_common/sanitizer_utils.hpp"
#include "ur_sanitizer_layer.hpp"
namespace ur_sanitizer_layer {
namespace asan {
ur_result_t EnqueueMemCopyRectHelper(
ur_queue_handle_t Queue, char *pSrc, char *pDst, ur_rect_offset_t SrcOffset,
ur_rect_offset_t DstOffset, ur_rect_region_t Region, size_t SrcRowPitch,
size_t SrcSlicePitch, size_t DstRowPitch, size_t DstSlicePitch,
bool Blocking, uint32_t NumEventsInWaitList,
const ur_event_handle_t *EventWaitList, ur_event_handle_t *Event) {
// If user doesn't determine src/dst row pitch and slice pitch, just use
// region for it.
if (SrcRowPitch == 0) {
SrcRowPitch = Region.width;
}
if (SrcSlicePitch == 0) {
SrcSlicePitch = SrcRowPitch * Region.height;
}
if (DstRowPitch == 0) {
DstRowPitch = Region.width;
}
if (DstSlicePitch == 0) {
DstSlicePitch = DstRowPitch * Region.height;
}
// Calculate the src and dst addresses that actually will be copied.
char *SrcOrigin = pSrc + SrcOffset.x + SrcRowPitch * SrcOffset.y +
SrcSlicePitch * SrcOffset.z;
char *DstOrigin = pDst + DstOffset.x + DstRowPitch * DstOffset.y +
DstSlicePitch * DstOffset.z;
std::vector<ur_event_handle_t> Events;
Events.reserve(Region.depth);
// For now, USM doesn't support 3D memory copy operation, so we can only
// loop call 2D memory copy function to implement it.
for (size_t i = 0; i < Region.depth; i++) {
ur_event_handle_t NewEvent{};
UR_CALL(getContext()->urDdiTable.Enqueue.pfnUSMMemcpy2D(
Queue, Blocking, DstOrigin + (i * DstSlicePitch), DstRowPitch,
SrcOrigin + (i * SrcSlicePitch), SrcRowPitch, Region.width,
Region.height, NumEventsInWaitList, EventWaitList, &NewEvent));
Events.push_back(NewEvent);
}
UR_CALL(getContext()->urDdiTable.Enqueue.pfnEventsWait(Queue, Events.size(),
Events.data(), Event));
getAsanInterceptor()
->getContextInfo(GetContext(Queue))
->DeferredEvents.add(Events);
return UR_RESULT_SUCCESS;
}
ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
// Sub-buffers don't maintain own allocations but rely on parent buffer.
if (SubBuffer) {
UR_CALL(SubBuffer->Parent->getHandle(Device, Handle));
Handle += SubBuffer->Origin;
return UR_RESULT_SUCCESS;
}
// Device may be null, we follow the L0 adapter's practice to use the first
// device
if (!Device) {
auto Devices = GetDevices(Context);
assert(Devices.size() > 0 && "Devices should not be empty");
Device = Devices[0];
}
assert((void *)Device != nullptr && "Device cannot be nullptr");
std::scoped_lock<ur_shared_mutex> Guard(Mutex);
auto CI = getAsanInterceptor()->getContextInfo(Context);
auto &Allocation = Allocations[Device];
ur_result_t URes = UR_RESULT_SUCCESS;
if (!Allocation) {
ur_usm_desc_t USMDesc{};
USMDesc.align = getAlignment();
URes = getAsanInterceptor()->allocateMemory(
Context, Device, AllocMemoryParams::forUSM(&USMDesc, {}), Size,
AllocType::MEM_BUFFER, ur_cast<void **>(&Allocation));
if (URes != UR_RESULT_SUCCESS) {
UR_LOG_L(getContext()->logger, ERR,
"Failed to allocate {} bytes memory for buffer {}", Size, this);
return URes;
}
if (HostPtr) {
ur_queue_handle_t InternalQueue = CI->getInternalQueue(Device);
URes = getContext()->urDdiTable.Enqueue.pfnUSMMemcpy(
InternalQueue, true, Allocation, HostPtr, Size, 0, nullptr, nullptr);
if (URes != UR_RESULT_SUCCESS) {
UR_LOG_L(
getContext()->logger, ERR,
"Failed to copy {} bytes data from host pointer {} to buffer {}",
Size, (void *)HostPtr, this);
return URes;
}
}
}
Handle = Allocation;
if (!LastSyncedDevice.hDevice) {
LastSyncedDevice = MemBuffer::Device_t{Device, Handle};
return URes;
}
// If the device required to allocate memory is not the previous one, we
// need to do data migration.
if (Device != LastSyncedDevice.hDevice) {
auto &HostAllocation = Allocations[nullptr];
if (!HostAllocation) {
ur_usm_desc_t USMDesc{};
USMDesc.align = getAlignment();
URes = getAsanInterceptor()->allocateMemory(
Context, nullptr, AllocMemoryParams::forUSM(&USMDesc, {}), Size,
AllocType::HOST_USM, ur_cast<void **>(&HostAllocation));
if (URes != UR_RESULT_SUCCESS) {
UR_LOG_L(getContext()->logger, ERR,
"Failed to allocate {} bytes host USM for buffer {} migration",
Size, this);
return URes;
}
}
// Copy data from last synced device to host
{
ur_queue_handle_t InternalQueue = CI->getInternalQueue(Device);
URes = getContext()->urDdiTable.Enqueue.pfnUSMMemcpy(
InternalQueue, true, HostAllocation, LastSyncedDevice.MemHandle, Size,
0, nullptr, nullptr);
if (URes != UR_RESULT_SUCCESS) {
UR_LOG_L(getContext()->logger, ERR,
"Failed to migrate memory buffer data");
return URes;
}
}
// Sync data back to device
{
ur_queue_handle_t InternalQueue = CI->getInternalQueue(Device);
URes = getContext()->urDdiTable.Enqueue.pfnUSMMemcpy(
InternalQueue, true, Allocation, HostAllocation, Size, 0, nullptr,
nullptr);
if (URes != UR_RESULT_SUCCESS) {
UR_LOG_L(getContext()->logger, ERR,
"Failed to migrate memory buffer data");
return URes;
}
}
}
LastSyncedDevice = MemBuffer::Device_t{Device, Handle};
return URes;
}
ur_result_t MemBuffer::free() {
for (const auto &[_, Ptr] : Allocations) {
ur_result_t URes = getAsanInterceptor()->releaseMemory(Context, Ptr);
if (URes != UR_RESULT_SUCCESS) {
UR_LOG_L(getContext()->logger, ERR, "Failed to free buffer handle {}",
(void *)Ptr);
return URes;
}
}
Allocations.clear();
return UR_RESULT_SUCCESS;
}
size_t MemBuffer::getAlignment() {
// Choose an alignment that is at most 128 and is the next power of 2
// for sizes less than 128.
// TODO: If we don't set the alignment size explicitly, the device will
// usually choose a very large size (more than 1k). Then sanitizer will
// allocate extra unnessary memory. Not sure if this will impact
// performance.
size_t MsbIdx = 63 - __builtin_clzl(Size);
size_t Alignment = (1ULL << (MsbIdx + 1));
if (Alignment > 128) {
Alignment = 128;
}
return Alignment;
}
} // namespace asan
} // namespace ur_sanitizer_layer