|
| 1 | +//===----------- memory.cpp - LLVM Offload Adapter -----------------------===// |
| 2 | +// |
| 3 | +// Copyright (C) 2025 Intel Corporation |
| 4 | +// |
| 5 | +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM |
| 6 | +// Exceptions. See LICENSE.TXT |
| 7 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#include <OffloadAPI.h> |
| 12 | +#include <unordered_set> |
| 13 | +#include <ur/ur.hpp> |
| 14 | +#include <ur_api.h> |
| 15 | + |
| 16 | +#include "adapter.hpp" |
| 17 | +#include "context.hpp" |
| 18 | +#include "device.hpp" |
| 19 | +#include "memory.hpp" |
| 20 | +#include "ur2offload.hpp" |
| 21 | + |
| 22 | +UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate( |
| 23 | + ur_context_handle_t hContext, ur_mem_flags_t flags, size_t size, |
| 24 | + const ur_buffer_properties_t *pProperties, ur_mem_handle_t *phBuffer) { |
| 25 | + |
| 26 | + // TODO: We can avoid the initial copy with USE_HOST_POINTER by implementing |
| 27 | + // something like olMemRegister |
| 28 | + const bool PerformInitialCopy = |
| 29 | + (flags & UR_MEM_FLAG_ALLOC_COPY_HOST_POINTER) || |
| 30 | + (flags & UR_MEM_FLAG_USE_HOST_POINTER); |
| 31 | + |
| 32 | + void *Ptr = nullptr; |
| 33 | + auto HostPtr = pProperties ? pProperties->pHost : nullptr; |
| 34 | + auto OffloadDevice = hContext->Device->OffloadDevice; |
| 35 | + auto AllocMode = BufferMem::AllocMode::Default; |
| 36 | + |
| 37 | + if (flags & UR_MEM_FLAG_ALLOC_HOST_POINTER) { |
| 38 | + auto Res = olMemAlloc(OffloadDevice, OL_ALLOC_TYPE_HOST, size, &HostPtr); |
| 39 | + if (Res) { |
| 40 | + return offloadResultToUR(Res); |
| 41 | + } |
| 42 | + // TODO: We (probably) need something like cuMemHostGetDevicePointer |
| 43 | + // for this to work everywhere. For now assume the managed host pointer is |
| 44 | + // device-accessible. |
| 45 | + Ptr = HostPtr; |
| 46 | + AllocMode = BufferMem::AllocMode::AllocHostPtr; |
| 47 | + } else { |
| 48 | + auto Res = olMemAlloc(OffloadDevice, OL_ALLOC_TYPE_DEVICE, size, &Ptr); |
| 49 | + if (Res) { |
| 50 | + return offloadResultToUR(Res); |
| 51 | + } |
| 52 | + if (flags & UR_MEM_FLAG_ALLOC_COPY_HOST_POINTER) { |
| 53 | + AllocMode = BufferMem::AllocMode::CopyIn; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + ur_mem_handle_t ParentBuffer = nullptr; |
| 58 | + auto URMemObj = std::unique_ptr<ur_mem_handle_t_>(new ur_mem_handle_t_{ |
| 59 | + hContext, ParentBuffer, flags, AllocMode, Ptr, HostPtr, size}); |
| 60 | + |
| 61 | + if (PerformInitialCopy) { |
| 62 | + auto Res = olMemcpy(nullptr, Ptr, OffloadDevice, HostPtr, |
| 63 | + Adapter.HostDevice, size, nullptr); |
| 64 | + if (Res) { |
| 65 | + return offloadResultToUR(Res); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + *phBuffer = URMemObj.release(); |
| 70 | + |
| 71 | + return UR_RESULT_SUCCESS; |
| 72 | +} |
| 73 | + |
| 74 | +UR_APIEXPORT ur_result_t UR_APICALL urMemRetain(ur_mem_handle_t hMem) { |
| 75 | + hMem->RefCount++; |
| 76 | + return UR_RESULT_SUCCESS; |
| 77 | +} |
| 78 | + |
| 79 | +UR_APIEXPORT ur_result_t UR_APICALL urMemRelease(ur_mem_handle_t hMem) { |
| 80 | + if (--hMem->RefCount > 0) { |
| 81 | + return UR_RESULT_SUCCESS; |
| 82 | + } |
| 83 | + |
| 84 | + std::unique_ptr<ur_mem_handle_t_> MemObjPtr(hMem); |
| 85 | + if (hMem->MemType == ur_mem_handle_t_::Type::Buffer) { |
| 86 | + // TODO: Handle registered host memory |
| 87 | + auto &BufferImpl = std::get<BufferMem>(MemObjPtr->Mem); |
| 88 | + auto Res = olMemFree(BufferImpl.Ptr); |
| 89 | + if (Res) { |
| 90 | + return offloadResultToUR(Res); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return UR_RESULT_SUCCESS; |
| 95 | +} |
| 96 | + |
| 97 | +UR_APIEXPORT ur_result_t UR_APICALL urMemGetInfo(ur_mem_handle_t hMemory, |
| 98 | + ur_mem_info_t MemInfoType, |
| 99 | + size_t propSize, |
| 100 | + void *pMemInfo, |
| 101 | + size_t *pPropSizeRet) { |
| 102 | + UrReturnHelper ReturnValue(propSize, pMemInfo, pPropSizeRet); |
| 103 | + |
| 104 | + switch (MemInfoType) { |
| 105 | + case UR_MEM_INFO_SIZE: { |
| 106 | + return ReturnValue(std::get<BufferMem>(hMemory->Mem).Size); |
| 107 | + } |
| 108 | + case UR_MEM_INFO_CONTEXT: { |
| 109 | + return ReturnValue(hMemory->getContext()); |
| 110 | + } |
| 111 | + case UR_MEM_INFO_REFERENCE_COUNT: { |
| 112 | + return ReturnValue(hMemory->RefCount.load()); |
| 113 | + } |
| 114 | + |
| 115 | + default: |
| 116 | + return UR_RESULT_ERROR_INVALID_ENUMERATION; |
| 117 | + } |
| 118 | +} |
0 commit comments