|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2024 Intel Corporation |
| 4 | + * |
| 5 | + * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. |
| 6 | + * See LICENSE.TXT |
| 7 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | + * |
| 9 | + * @file asan_buffer.cpp |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +#include "asan_buffer.hpp" |
| 14 | +#include "asan_interceptor.hpp" |
| 15 | +#include "ur_sanitizer_layer.hpp" |
| 16 | +#include "ur_sanitizer_utils.hpp" |
| 17 | + |
| 18 | +namespace ur_sanitizer_layer { |
| 19 | + |
| 20 | +ur_result_t EnqueueMemCopyRectHelper( |
| 21 | + ur_queue_handle_t Queue, char *pSrc, char *pDst, ur_rect_offset_t SrcOffset, |
| 22 | + ur_rect_offset_t DstOffset, ur_rect_region_t Region, size_t SrcRowPitch, |
| 23 | + size_t SrcSlicePitch, size_t DstRowPitch, size_t DstSlicePitch, |
| 24 | + bool Blocking, uint32_t NumEventsInWaitList, |
| 25 | + const ur_event_handle_t *EventWaitList, ur_event_handle_t *Event) { |
| 26 | + // If user doesn't determine src/dst row pitch and slice pitch, just use |
| 27 | + // region for it. |
| 28 | + if (SrcRowPitch == 0) { |
| 29 | + SrcRowPitch = Region.width; |
| 30 | + } |
| 31 | + |
| 32 | + if (SrcSlicePitch == 0) { |
| 33 | + SrcSlicePitch = SrcRowPitch * Region.height; |
| 34 | + } |
| 35 | + |
| 36 | + if (DstRowPitch == 0) { |
| 37 | + DstRowPitch = Region.width; |
| 38 | + } |
| 39 | + |
| 40 | + if (DstSlicePitch == 0) { |
| 41 | + DstSlicePitch = DstRowPitch * Region.height; |
| 42 | + } |
| 43 | + |
| 44 | + // Calculate the src and dst addresses that actually will be copied. |
| 45 | + char *SrcOrigin = pSrc + SrcOffset.x + SrcRowPitch * SrcOffset.y + |
| 46 | + SrcSlicePitch * SrcOffset.z; |
| 47 | + char *DstOrigin = pDst + DstOffset.x + DstRowPitch * DstOffset.y + |
| 48 | + DstSlicePitch * DstOffset.z; |
| 49 | + |
| 50 | + std::vector<ur_event_handle_t> Events; |
| 51 | + Events.reserve(Region.depth); |
| 52 | + // For now, USM doesn't support 3D memory copy operation, so we can only |
| 53 | + // loop call 2D memory copy function to implement it. |
| 54 | + for (size_t i = 0; i < Region.depth; i++) { |
| 55 | + ur_event_handle_t NewEvent{}; |
| 56 | + UR_CALL(context.urDdiTable.Enqueue.pfnUSMMemcpy2D( |
| 57 | + Queue, Blocking, DstOrigin + (i * DstSlicePitch), DstRowPitch, |
| 58 | + SrcOrigin + (i * SrcSlicePitch), SrcRowPitch, Region.width, |
| 59 | + Region.height, NumEventsInWaitList, EventWaitList, &NewEvent)); |
| 60 | + |
| 61 | + Events.push_back(NewEvent); |
| 62 | + } |
| 63 | + |
| 64 | + UR_CALL(context.urDdiTable.Enqueue.pfnEventsWait(Queue, Events.size(), |
| 65 | + Events.data(), Event)); |
| 66 | + |
| 67 | + return UR_RESULT_SUCCESS; |
| 68 | +} |
| 69 | + |
| 70 | +ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) { |
| 71 | + // Sub-buffers don't maintain own allocations but rely on parent buffer. |
| 72 | + if (SubBuffer) { |
| 73 | + UR_CALL(SubBuffer->Parent->getHandle(Device, Handle)); |
| 74 | + Handle += SubBuffer->Origin; |
| 75 | + return UR_RESULT_SUCCESS; |
| 76 | + } |
| 77 | + |
| 78 | + auto &Allocation = Allocations[Device]; |
| 79 | + if (!Allocation) { |
| 80 | + ur_usm_desc_t USMDesc{}; |
| 81 | + USMDesc.align = getAlignment(); |
| 82 | + ur_usm_pool_handle_t Pool{}; |
| 83 | + ur_result_t URes = context.interceptor->allocateMemory( |
| 84 | + Context, Device, &USMDesc, Pool, Size, AllocType::MEM_BUFFER, |
| 85 | + ur_cast<void **>(&Allocation)); |
| 86 | + if (URes != UR_RESULT_SUCCESS) { |
| 87 | + context.logger.error( |
| 88 | + "Failed to allocate {} bytes memory for buffer {}", Size, this); |
| 89 | + return URes; |
| 90 | + } |
| 91 | + |
| 92 | + if (HostPtr) { |
| 93 | + ManagedQueue Queue(Context, Device); |
| 94 | + URes = context.urDdiTable.Enqueue.pfnUSMMemcpy( |
| 95 | + Queue, true, Allocation, HostPtr, Size, 0, nullptr, nullptr); |
| 96 | + if (URes != UR_RESULT_SUCCESS) { |
| 97 | + context.logger.error("Failed to copy {} bytes data from host " |
| 98 | + "pointer {} to buffer {}", |
| 99 | + Size, HostPtr, this); |
| 100 | + return URes; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + Handle = Allocation; |
| 106 | + |
| 107 | + return UR_RESULT_SUCCESS; |
| 108 | +} |
| 109 | + |
| 110 | +ur_result_t MemBuffer::free() { |
| 111 | + for (const auto &[_, Ptr] : Allocations) { |
| 112 | + ur_result_t URes = context.interceptor->releaseMemory(Context, Ptr); |
| 113 | + if (URes != UR_RESULT_SUCCESS) { |
| 114 | + context.logger.error("Failed to free buffer handle {}", Ptr); |
| 115 | + return URes; |
| 116 | + } |
| 117 | + } |
| 118 | + Allocations.clear(); |
| 119 | + return UR_RESULT_SUCCESS; |
| 120 | +} |
| 121 | + |
| 122 | +size_t MemBuffer::getAlignment() { |
| 123 | + // Choose an alignment that is at most 128 and is the next power of 2 |
| 124 | + // for sizes less than 128. |
| 125 | + // TODO: If we don't set the alignment size explicitly, the device will |
| 126 | + // usually choose a very large size (more than 1k). Then sanitizer will |
| 127 | + // allocate extra unnessary memory. Not sure if this will impact |
| 128 | + // performance. |
| 129 | + size_t MsbIdx = 63 - __builtin_clz(Size); |
| 130 | + size_t Alignment = (1 << (MsbIdx + 1)); |
| 131 | + if (Alignment > 128) { |
| 132 | + Alignment = 128; |
| 133 | + } |
| 134 | + return Alignment; |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace ur_sanitizer_layer |
0 commit comments